1 /* vi: set sts=2 sw=2 :*/
4 Library for creating and reading SWF files or parts of it.
5 There's a module directory which provides some extended functionality.
6 Most modules are included at the bottom of this file.
8 Part of the swftools package.
10 Copyright (c) 2000-2003 Rainer Böhme <rfxswf@reflex-studio.de>
11 Copyright (c) 2003 Matthias Kramm <kramm@quiss.org>
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
39 #endif // HAVE_JPEGLIB
45 #ifndef RFXSWF_DISABLESOUND
47 #include "lame/lame.h"
64 #define MALLOC_SIZE 128
65 #define INSERT_RFX_TAG
67 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
69 // inline wrapper functions
71 TAG * swf_NextTag(TAG * t) { return t->next; }
72 TAG * swf_PrevTag(TAG * t) { return t->prev; }
73 U16 swf_GetTagID(TAG * t) { return t->id; }
74 U32 swf_GetTagLen(TAG * t) { return t->len; }
75 U8* swf_GetTagLenPtr(TAG * t) { return &(t->data[t->len]); }
76 U32 swf_GetTagPos(TAG * t) { return t->pos; }
78 // for future purpose: avoid high level lib functions to change tagpos/bitpos
80 #define swf_SaveTagPos(tag)
81 #define swf_RestoreTagPos(tag)
83 void swf_SetTagPos(TAG * t,U32 pos)
84 { swf_ResetReadBits(t);
85 if (pos<=t->len) t->pos = pos;
88 fprintf(stderr,"SetTagPos(%d) out of bounds: TagID = %i\n",pos, t->id);
93 char* swf_GetString(TAG*t)
96 while(t->pos < t->len && swf_GetU8(t));
97 /* make sure we always have a trailing zero byte */
98 if(t->pos == t->len) {
99 if(t->len == t->memsize) {
100 swf_ResetWriteBits(t);
106 return (char*)&(t->data[pos]);
109 U8 swf_GetU8(TAG * t)
110 { swf_ResetReadBits(t);
113 { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
117 return t->data[t->pos++];
120 U16 swf_GetU16(TAG * t)
122 swf_ResetReadBits(t);
124 if (t->pos>(t->len-2))
125 { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
129 res = t->data[t->pos] | (t->data[t->pos+1]<<8);
134 U32 swf_GetU32(TAG * t)
136 swf_ResetReadBits(t);
138 if (t->pos>(t->len-4))
139 { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
143 res = t->data[t->pos] | (t->data[t->pos+1]<<8) |
144 (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
149 int swf_GetBlock(TAG * t,U8 * b,int l)
150 // returns number of bytes written (<=l)
151 // b = NULL -> skip data
152 { swf_ResetReadBits(t);
153 if ((t->len-t->pos)<l) l=t->len-t->pos;
154 if (b && l) memcpy(b,&t->data[t->pos],l);
159 int swf_SetBlock(TAG * t,U8 * b,int l)
160 // Appends Block to the end of Tagdata, returns size
161 { U32 newlen = t->len + l;
162 swf_ResetWriteBits(t);
163 if (newlen>t->memsize)
164 { U32 newmem = MEMSIZE(newlen);
165 U8 * newdata = (U8*)(rfx_realloc(t->data,newmem));
169 if (b) memcpy(&t->data[t->len],b,l);
170 else memset(&t->data[t->len],0x00,l);
175 int swf_SetU8(TAG * t,U8 v)
176 { swf_ResetWriteBits(t);
177 if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
178 t->data[t->len++] = v;
182 int swf_SetU16(TAG * t,U16 v)
187 swf_ResetWriteBits(t);
188 if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
189 t->data[t->len++] = a[0];
190 t->data[t->len++] = a[1];
193 void swf_SetS16(TAG * t,int v)
195 if(v>32767 || v<-32768) {
196 fprintf(stderr, "Warning: S16 overflow: %d\n", v);
198 swf_SetU16(t, (S16)v);
201 int swf_SetU32(TAG * t,U32 v)
203 a[0] = v&0xff; // to ensure correct handling of non-intel byteorder
208 swf_ResetWriteBits(t);
209 if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
210 t->data[t->len++] = a[0];
211 t->data[t->len++] = a[1];
212 t->data[t->len++] = a[2];
213 t->data[t->len++] = a[3];
217 U32 swf_GetBits(TAG * t,int nbits)
219 if (!nbits) return 0;
220 if (!t->readBit) t->readBit = 0x80;
223 if (t->data[t->pos]&t->readBit) res|=1;
227 { if (nbits) t->readBit = 0x80;
230 { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
240 S32 swf_GetSBits(TAG * t,int nbits)
241 { U32 res = swf_GetBits(t,nbits);
242 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
246 U32 reader_GetBits(reader_t*reader, int nbits)
247 { return reader_readbits(reader, nbits);
249 S32 reader_GetSBits(reader_t*reader, int nbits)
250 { U32 res = reader_readbits(reader, nbits);
251 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
255 int swf_SetBits(TAG * t,U32 v,int nbits)
256 { U32 bm = 1<<(nbits-1);
260 { if (FAILED(swf_SetU8(t,0))) return -1;
263 if (v&bm) t->data[t->len-1] |= t->writeBit;
271 // Advanced Data Access Functions
273 double swf_GetFixed(TAG * t)
275 U16 low = swf_GetU16(t);
276 U16 high = swf_GetU16(t);
277 return high + low*(1/65536.0);
279 void swf_SetFixed(TAG * t, double f)
281 U16 fr = (U16)(f-(int)f)*65536;
283 swf_SetU16(t, (U16)f - (f<0 && fr!=0));
285 float swf_GetFixed8(TAG * t)
287 U8 low = swf_GetU8(t);
288 U8 high = swf_GetU8(t);
289 return (float)(high + low*(1/256.0));
291 void swf_SetFixed8(TAG * t, float f)
293 U8 fr = (U8)(f-(int)f)*256;
295 swf_SetU8(t, (U8)f - (f<0 && fr!=0));
298 int swf_SetRGB(TAG * t,RGBA * col)
301 { swf_SetU8(t,col->r);
304 } else swf_SetBlock(t,NULL,3);
307 void swf_GetRGB(TAG * t, RGBA * col)
312 col->r = swf_GetU8(t);
313 col->g = swf_GetU8(t);
314 col->b = swf_GetU8(t);
318 int swf_SetRGBA(TAG * t,RGBA * col)
321 { swf_SetU8(t,col->r);
325 } else swf_SetBlock(t,NULL,4);
328 void swf_GetRGBA(TAG * t, RGBA * col)
333 col->r = swf_GetU8(t);
334 col->g = swf_GetU8(t);
335 col->b = swf_GetU8(t);
336 col->a = swf_GetU8(t);
339 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
343 memset(gradient, 0, sizeof(GRADIENT));
346 U8 num = swf_GetU8(tag) & 15;
349 gradient->rgba = (RGBA*)rfx_calloc(sizeof(RGBA)*gradient->num);
350 gradient->ratios = (U8*)rfx_calloc(sizeof(gradient->ratios[0])*gradient->num);
354 U8 ratio = swf_GetU8(tag);
357 swf_GetRGB(tag, &color);
359 swf_GetRGBA(tag, &color);
361 gradient->ratios[t] = ratio;
362 gradient->rgba[t] = color;
367 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
371 memset(gradient, 0, sizeof(GRADIENT));
374 swf_SetU8(tag, gradient->num);
375 for(t=0; t<8 && t<gradient->num; t++)
377 swf_SetU8(tag, gradient->ratios[t]);
379 swf_SetRGB(tag, &gradient->rgba[t]);
381 swf_SetRGBA(tag, &gradient->rgba[t]);
385 void swf_FreeGradient(GRADIENT* gradient)
388 rfx_free(gradient->ratios);
390 rfx_free(gradient->rgba);
391 memset(gradient, 0, sizeof(GRADIENT));
394 int swf_CountUBits(U32 v,int nbits)
397 if(v == 0x00000000) n = 0;
403 return (n>nbits)?n:nbits;
406 int swf_CountBits(U32 v,int nbits)
410 { if(v == 0xffffffff) n = 1;
418 { if(v == 0x00000000) n = 0;
425 return (n>nbits)?n:nbits;
428 int swf_GetRect(TAG * t,SRECT * r)
431 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
433 nbits = (int) swf_GetBits(t,5);
434 r->xmin = swf_GetSBits(t,nbits);
435 r->xmax = swf_GetSBits(t,nbits);
436 r->ymin = swf_GetSBits(t,nbits);
437 r->ymax = swf_GetSBits(t,nbits);
441 int reader_GetRect(reader_t*reader,SRECT * r)
445 nbits = (int) reader_GetBits(reader,5);
446 r->xmin = reader_GetSBits(reader,nbits);
447 r->xmax = reader_GetSBits(reader,nbits);
448 r->ymin = reader_GetSBits(reader,nbits);
449 r->ymax = reader_GetSBits(reader,nbits);
453 int swf_SetRect(TAG * t,SRECT * r)
456 nbits = swf_CountBits(r->xmin,0);
457 nbits = swf_CountBits(r->xmax,nbits);
458 nbits = swf_CountBits(r->ymin,nbits);
459 nbits = swf_CountBits(r->ymax,nbits);
461 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
465 swf_SetBits(t,nbits,5);
466 swf_SetBits(t,r->xmin,nbits);
467 swf_SetBits(t,r->xmax,nbits);
468 swf_SetBits(t,r->ymin,nbits);
469 swf_SetBits(t,r->ymax,nbits);
474 void swf_ExpandRect(SRECT*src, SPOINT add)
476 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
481 if((add.x|add.y) == 0) src->xmax++; //make sure the bbox is not NULL anymore
484 if(add.x < src->xmin)
486 if(add.x > src->xmax)
488 if(add.y < src->ymin)
490 if(add.y > src->ymax)
493 void swf_ExpandRect2(SRECT*src, SRECT*add)
495 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
497 if((src->xmin | src->ymin | src->xmax | src->ymax)==0)
499 if(add->xmin < src->xmin)
500 src->xmin = add->xmin;
501 if(add->ymin < src->ymin)
502 src->ymin = add->ymin;
503 if(add->xmax > src->xmax)
504 src->xmax = add->xmax;
505 if(add->ymax > src->ymax)
506 src->ymax = add->ymax;
508 void swf_ExpandRect3(SRECT*src, SPOINT center, int radius)
510 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
511 src->xmin = center.x-radius;
512 src->ymin = center.y-radius;
513 src->xmax = center.x+radius;
514 src->ymax = center.y+radius;
515 if((center.x|center.y|radius) == 0) src->xmax++; //make sure the bbox is not NULL anymore
518 if(center.x - radius < src->xmin)
519 src->xmin = center.x - radius;
520 if(center.x + radius > src->xmax)
521 src->xmax = center.x + radius;
522 if(center.y - radius < src->ymin)
523 src->ymin = center.y - radius;
524 if(center.y + radius > src->ymax)
525 src->ymax = center.y + radius;
527 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
530 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
531 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
534 SRECT swf_TurnRect(SRECT r, MATRIX* m)
537 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
540 p1.x = r.xmin;p1.y = r.ymin;
541 p2.x = r.xmax;p2.y = r.ymin;
542 p3.x = r.xmin;p3.y = r.ymax;
543 p4.x = r.xmax;p4.y = r.ymax;
544 pp1 = swf_TurnPoint(p1, m);
545 pp2 = swf_TurnPoint(p2, m);
546 pp3 = swf_TurnPoint(p3, m);
547 pp4 = swf_TurnPoint(p4, m);
548 g.xmin = g.xmax = pp1.x;
549 g.ymin = g.ymax = pp1.y;
550 swf_ExpandRect(&g, pp2);
551 swf_ExpandRect(&g, pp3);
552 swf_ExpandRect(&g, pp4);
557 int swf_GetMatrix(TAG * t,MATRIX * m)
564 { m->sx = m->sy = 0x10000;
570 swf_ResetReadBits(t);
572 if (swf_GetBits(t,1))
573 { nbits = swf_GetBits(t,5);
574 m->sx = swf_GetSBits(t,nbits);
575 m->sy = swf_GetSBits(t,nbits);
577 else m->sx = m->sy = 0x10000;
579 if (swf_GetBits(t,1))
580 { nbits = swf_GetBits(t,5);
581 m->r0 = swf_GetSBits(t,nbits);
582 m->r1 = swf_GetSBits(t,nbits);
584 else m->r0 = m->r1 = 0x0;
586 nbits = swf_GetBits(t,5);
587 m->tx = swf_GetSBits(t,nbits);
588 m->ty = swf_GetSBits(t,nbits);
593 int swf_SetMatrix(TAG * t,MATRIX * m)
599 ma.sx = ma.sy = 0x10000;
604 swf_ResetWriteBits(t);
606 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
608 { swf_SetBits(t,1,1);
609 nbits = swf_CountBits(m->sx,0);
610 nbits = swf_CountBits(m->sy,nbits);
612 /* TODO: happens on AMD64 systems for normal values? */
613 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
616 swf_SetBits(t,nbits,5);
617 swf_SetBits(t,m->sx,nbits);
618 swf_SetBits(t,m->sy,nbits);
621 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
623 { swf_SetBits(t,1,1);
624 nbits = swf_CountBits(m->r0,0);
625 nbits = swf_CountBits(m->r1,nbits);
627 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
630 swf_SetBits(t,nbits,5);
631 swf_SetBits(t,m->r0,nbits);
632 swf_SetBits(t,m->r1,nbits);
635 nbits = swf_CountBits(m->tx,0);
636 nbits = swf_CountBits(m->ty,nbits);
638 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
641 swf_SetBits(t,nbits,5);
642 swf_SetBits(t,m->tx,nbits);
643 swf_SetBits(t,m->ty,nbits);
648 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha)
656 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
657 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
661 swf_ResetReadBits(t);
662 hasadd = swf_GetBits(t,1);
663 hasmul = swf_GetBits(t,1);
664 nbits = swf_GetBits(t,4);
667 { cx->r0 = (S16)swf_GetSBits(t,nbits);
668 cx->g0 = (S16)swf_GetSBits(t,nbits);
669 cx->b0 = (S16)swf_GetSBits(t,nbits);
671 cx->a0 = (S16)swf_GetSBits(t,nbits);
675 { cx->r1 = (S16)swf_GetSBits(t,nbits);
676 cx->g1 = (S16)swf_GetSBits(t,nbits);
677 cx->b1 = (S16)swf_GetSBits(t,nbits);
679 cx->a1 = (S16)swf_GetSBits(t,nbits);
685 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
693 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
694 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
704 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
705 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
708 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
709 nbits = swf_CountBits((S32)cx->r0,nbits);
710 nbits = swf_CountBits((S32)cx->g0,nbits);
711 nbits = swf_CountBits((S32)cx->b0,nbits);
715 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
716 nbits = swf_CountBits((S32)cx->r1,nbits);
717 nbits = swf_CountBits((S32)cx->g1,nbits);
718 nbits = swf_CountBits((S32)cx->b1,nbits);
721 swf_ResetWriteBits(t);
722 swf_SetBits(t,hasadd?1:0,1);
723 swf_SetBits(t,hasmul?1:0,1);
724 swf_SetBits(t,nbits,4);
727 { swf_SetBits(t,cx->r0,nbits);
728 swf_SetBits(t,cx->g0,nbits);
729 swf_SetBits(t,cx->b0,nbits);
730 if (alpha) swf_SetBits(t,cx->a0,nbits);
734 { swf_SetBits(t,cx->r1,nbits);
735 swf_SetBits(t,cx->g1,nbits);
736 swf_SetBits(t,cx->b1,nbits);
737 if (alpha) swf_SetBits(t,cx->a1,nbits);
743 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
744 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
746 void swf_SetPassword(TAG * t, const char * password)
748 /* WARNING: crypt_md5 is not reentrant */
752 #if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) && defined(HAVE_TIME_H) && defined(HAVE_TIME)
754 salt[0] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
755 salt[1] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
759 fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
760 fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
764 md5string = crypt_md5(password, salt);
767 swf_SetString(t, (U8*)md5string);
770 int swf_VerifyPassword(TAG * t, const char * password)
772 char*md5string1, *md5string2;
777 if(t->len >= 5 && t->pos==0 &&
782 printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
785 md5string1 = swf_GetString(t);
787 if(strncmp(md5string1, "$1$",3 )) {
788 fprintf(stderr, "rfxswf: no salt in pw string\n");
791 x = strchr(md5string1+3, '$');
793 fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
796 n = x-(md5string1+3);
797 salt = (char*)rfx_alloc(n+1);
798 memcpy(salt, md5string1+3, n);
801 md5string2 = crypt_md5(password, salt);
803 if(strcmp(md5string1, md5string2) != 0)
808 // Tag List Manipulating Functions
810 TAG * swf_InsertTag(TAG * after,U16 id)
813 t = (TAG *)rfx_calloc(sizeof(TAG));
819 t->next = after->next;
821 if (t->next) t->next->prev = t;
826 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
829 t = (TAG *)rfx_calloc(sizeof(TAG));
835 t->prev = before->prev;
837 if (t->prev) t->prev->next = t;
839 if(swf && swf->firstTag == before) {
845 void swf_ClearTag(TAG * t)
847 if (t->data) rfx_free(t->data);
856 void swf_ResetTag(TAG*tag, U16 id)
858 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
862 TAG* swf_CopyTag(TAG*tag, TAG*to_copy)
864 tag = swf_InsertTag(tag, to_copy->id);
865 swf_SetBlock(tag, to_copy->data, to_copy->len);
869 int swf_DeleteTag(TAG * t)
872 if (t->prev) t->prev->next = t->next;
873 if (t->next) t->next->prev = t->prev;
875 if (t->data) rfx_free(t->data);
880 TAG * swf_ReadTag(reader_t*reader, TAG * prev)
886 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
894 if (reader->read(reader, &len, 4) != 4) return NULL;
898 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
899 // Sprite handling fix: Flatten sprite tree
901 t = (TAG *)rfx_calloc(sizeof(TAG));
907 { t->data = (U8*)rfx_alloc(t->len);
909 if (reader->read(reader, t->data, t->len) != t->len) {
910 fprintf(stderr, "rfxswf: Warning: Short read (tagid %d). File truncated?\n", t->id);
911 free(t->data);t->data=0;
926 int swf_DefineSprite_GetRealSize(TAG * t);
928 int swf_WriteTag2(writer_t*writer, TAG * t)
929 // returns tag length in bytes (incl. Header), -1 = Error
930 // writer = 0 -> no output
937 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
939 short_tag = len<0x3f&&
940 (t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2&&t->id!=ST_SOUNDSTREAMBLOCK&&
941 t->id!=ST_DEFINEBITSJPEG&&t->id!=ST_DEFINEBITSJPEG2&&t->id!=ST_DEFINEBITSJPEG3);
945 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
946 if (writer->write(writer,raw,2)!=2)
949 fprintf(stderr,"WriteTag() failed: Short Header.\n");
956 raw[0] = SWAP16((t->id<<6)|0x3f);
957 if (writer->write(writer,raw,2)!=2)
960 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
966 if (writer->write(writer,&len,4)!=4)
969 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
976 { if (writer->write(writer,t->data,t->len)!=t->len)
979 fprintf(stderr,"WriteTag() failed: Data.\n");
985 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
989 return t->len+(short_tag?2:6);
992 int swf_WriteTag(int handle, TAG * t)
997 return swf_WriteTag2(0, t);
998 writer_init_filewriter(&writer, handle);
999 len = swf_WriteTag2(&writer, t);
1000 writer.finish(&writer);
1004 int swf_DefineSprite_GetRealSize(TAG * t)
1005 // Sprite Handling: Helper function to pack DefineSprite-Tag
1007 if(len>4) { // folded sprite
1011 { t = swf_NextTag(t);
1012 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
1014 } while (t&&(t->id!=ST_END));
1018 void swf_UnFoldSprite(TAG * t)
1023 U16 spriteid,spriteframes;
1025 if(t->id!=ST_DEFINESPRITE)
1027 if(t->len<=4) // not folded
1032 spriteid = swf_GetU16(t); //id
1033 spriteframes = swf_GetU16(t); //frames
1040 tmp = swf_GetU16(t);
1045 if(id == ST_DEFINESPRITE && len<=4)
1049 len = swf_GetU32(t);
1050 it = swf_InsertTag(next, id);
1055 { it->data = (U8*)rfx_alloc(it->len);
1056 it->memsize = it->len;
1057 swf_GetBlock(t, it->data, it->len);
1064 rfx_free(t->data); t->data = 0;
1065 t->memsize = t->len = t->pos = 0;
1067 swf_SetU16(t, spriteid);
1068 swf_SetU16(t, spriteframes);
1071 void swf_FoldSprite(TAG * t)
1076 if(t->id!=ST_DEFINESPRITE)
1079 fprintf(stderr, "Error: Sprite has no ID!");
1083 /* sprite is already folded */
1090 t->len = t->pos = t->memsize = 0;
1095 t = swf_NextTag(sprtag);
1100 if(t->id==ST_SHOWFRAME) frames++;
1101 if(t->id == ST_DEFINESPRITE && t->len<=4)
1106 } while(t && level);
1108 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1110 swf_SetU16(sprtag, id);
1111 swf_SetU16(sprtag, frames);
1113 t = swf_NextTag(sprtag);
1119 (t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2&&t->id!=ST_SOUNDSTREAMBLOCK&&
1120 t->id!=ST_DEFINEBITSJPEG&&t->id!=ST_DEFINEBITSJPEG2&&t->id!=ST_DEFINEBITSJPEG3)
1122 swf_SetU16(sprtag,t->len|(t->id<<6));
1124 swf_SetU16(sprtag,0x3f|(t->id<<6));
1125 swf_SetU32(sprtag,t->len);
1128 swf_SetBlock(sprtag,t->data, t->len);
1130 if(t->id == ST_DEFINESPRITE && t->len<=4)
1139 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1141 // sprtag->next = t;
1142 // t->prev = sprtag;
1145 int swf_IsFolded(TAG * t)
1147 return (t->id == ST_DEFINESPRITE && t->len>4);
1150 void swf_FoldAll(SWF*swf)
1152 TAG*tag = swf->firstTag;
1153 //swf_DumpSWF(stdout, swf);
1155 if(tag->id == ST_DEFINESPRITE) {
1156 swf_FoldSprite(tag);
1157 //swf_DumpSWF(stdout, swf);
1159 tag = swf_NextTag(tag);
1163 void swf_UnFoldAll(SWF*swf)
1165 TAG*tag = swf->firstTag;
1167 if(tag->id == ST_DEFINESPRITE)
1168 swf_UnFoldSprite(tag);
1173 void swf_OptimizeTagOrder(SWF*swf)
1180 /* at the moment, we don't actually do optimizing,
1181 only fixing of non-spec-conformant things like
1188 tag = swf->firstTag;
1191 if(tag->id == ST_DEFINESPRITE) {
1193 /* ??? all sprites are supposed to be unfolded */
1194 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1204 /* move non-sprite tags out of sprite */
1205 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1206 /* remove tag from current position */
1207 tag->prev->next = tag->next;
1209 tag->next->prev = tag->prev;
1211 /* insert before tag level0 */
1213 tag->prev = level0->prev;
1216 tag->prev->next = tag;
1218 swf->firstTag = tag;
1222 if(tag->id == ST_END) {
1233 int swf_ReadSWF2(reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1235 if (!swf) return -1;
1236 memset(swf,0x00,sizeof(SWF));
1238 { char b[32]; // read Header
1244 if ((len = reader->read(reader ,b,8))<8) return -1;
1246 if (b[0]!='F' && b[0]!='C') return -1;
1247 if (b[1]!='W') return -1;
1248 if (b[2]!='S') return -1;
1249 swf->fileVersion = b[3];
1250 swf->compressed = (b[0]=='C')?1:0;
1251 swf->fileSize = GET32(&b[4]);
1253 if(swf->compressed) {
1254 reader_init_zlibinflate(&zreader, reader);
1258 reader_GetRect(reader, &swf->movieSize);
1259 reader->read(reader, &swf->frameRate, 2);
1260 swf->frameRate = SWAP16(swf->frameRate);
1261 reader->read(reader, &swf->frameCount, 2);
1262 swf->frameCount = SWAP16(swf->frameCount);
1264 /* read tags and connect to list */
1266 while (t) t = swf_ReadTag(reader,t);
1267 swf->firstTag = t1.next;
1268 t1.next->prev = NULL;
1274 int swf_ReadSWF(int handle, SWF * swf)
1277 reader_init_filereader(&reader, handle);
1278 return swf_ReadSWF2(&reader, swf);
1281 int swf_WriteSWF2(writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1288 int writer_lastpos = 0;
1291 if (!swf) return -1;
1292 if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1294 if(writer) writer_lastpos = writer->pos;
1296 // Insert REFLEX Tag
1298 #ifdef INSERT_RFX_TAG
1300 if ((swf->firstTag && swf->firstTag->id != ST_REFLEX) &&
1301 (!swf->firstTag->next || swf->firstTag->next->id != ST_REFLEX))
1303 swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),(U8*)"rfx",3);
1306 #endif // INSERT_RFX_TAG
1308 if(swf->fileVersion >= 9) {
1309 if ((!swf->firstTag || swf->firstTag->id != ST_SCENEDESCRIPTION) &&
1311 !swf->firstTag->next || swf->firstTag->next->id != ST_SCENEDESCRIPTION) &&
1313 !swf->firstTag->next ||
1314 !swf->firstTag->next->next || swf->firstTag->next->next->id != ST_SCENEDESCRIPTION))
1316 TAG*scene = swf_InsertTagBefore(swf, swf->firstTag,ST_SCENEDESCRIPTION);
1317 swf_SetU16(scene, 1);
1318 swf_SetString(scene, (U8*)"Scene 1");
1319 swf_SetU8(scene, 0);
1323 if(swf->fileVersion >= 9) {
1324 if (swf->firstTag && swf->firstTag->id != ST_FILEATTRIBUTES)
1326 U32 flags = 0x8; // | 128 = usenetwork, | 16 = Actionscript3 | 8 = hasmetadata
1327 swf_SetU32(swf_InsertTagBefore(swf, swf->firstTag,ST_FILEATTRIBUTES),flags);
1331 // Count Frames + File Size
1338 len += swf_WriteTag(-1,t);
1339 if(t->id == ST_DEFINESPRITE && !swf_IsFolded(t)) inSprite++;
1340 else if(t->id == ST_END && inSprite) inSprite--;
1341 else if(t->id == ST_END && !inSprite) {
1342 if(t->prev && t->prev->id!=ST_SHOWFRAME)
1345 else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1353 memset(&t1,0x00,sizeof(TAG));
1357 { // measure header file size
1360 memset(&t2,0x00,sizeof(TAG));
1363 swf_SetRect(&t2, &swf->movieSize);
1364 swf_SetU16(&t2, swf->frameRate);
1365 swf_SetU16(&t2, swf->frameCount);
1366 l = swf_GetTagLen(&t2)+8;
1368 if(swf->compressed == 8) {
1373 if(len) {// don't touch headers without tags
1374 swf->fileSize = fileSize;
1375 swf->frameCount = frameCount;
1378 if(swf->compressed != 8) {
1379 /* compressed flag set to 8 means "skip first 8
1380 header bytes". This is necessary if the caller wants to
1381 create compressed SWFs himself .
1382 It also means that we don't initialize our own zlib
1383 writer, but assume the caller provided one.
1385 if(swf->compressed) {
1387 writer->write(writer, id, 3);
1391 writer->write(writer, id, 3);
1394 writer->write(writer, &swf->fileVersion, 1);
1395 PUT32(b4, swf->fileSize);
1396 writer->write(writer, b4, 4);
1398 if(swf->compressed) {
1399 writer_init_zlibdeflate(&zwriter, writer);
1404 swf_SetRect(&t1,&swf->movieSize);
1405 swf_SetU16(&t1,swf->frameRate);
1406 swf_SetU16(&t1,swf->frameCount);
1408 ret = writer->write(writer,b,swf_GetTagLen(&t1));
1409 if (ret!=swf_GetTagLen(&t1))
1412 fprintf(stderr, "ret:%d\n",ret);
1414 fprintf(stderr,"WriteSWF() failed: Header.\n");
1421 { if (swf_WriteTag2(writer, t)<0) return -1;
1424 if(swf->compressed) {
1425 if(swf->compressed != 8) {
1426 zwriter.finish(&zwriter);
1427 return writer->pos - writer_lastpos;
1429 return (int)fileSize;
1431 return (int)fileSize;
1436 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1440 swf->compressed = 0;
1443 writer_init_nullwriter(&writer);
1444 len = swf_WriteSWF2(&writer, swf);
1447 writer_init_filewriter(&writer, handle);
1448 len = swf_WriteSWF2(&writer, swf);
1449 writer.finish(&writer);
1453 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1457 swf->compressed = 1;
1460 writer_init_nullwriter(&writer);
1461 len = swf_WriteSWF2(&writer, swf);
1463 writer_init_filewriter(&writer, handle);
1464 len = swf_WriteSWF2(&writer, swf);
1465 writer.finish(&writer);
1469 int swf_WriteHeader2(writer_t*writer,SWF * swf)
1472 memcpy(&myswf,swf,sizeof(SWF));
1474 return swf_WriteSWF2(writer, &myswf);
1477 int swf_WriteHeader(int handle,SWF * swf)
1480 memcpy(&myswf,swf,sizeof(SWF));
1482 return swf_WriteSWF(handle, &myswf);
1485 int swf_WriteCGI(SWF * swf)
1489 len = swf_WriteSWF(-1,swf);
1491 if (len<0) return -1;
1493 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1494 "Accept-Ranges: bytes\n"
1495 "Content-Length: %lu\n"
1496 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1499 write(fileno(stdout),s,strlen(s));
1500 return swf_WriteSWF(fileno(stdout),swf);
1503 SWF* swf_CopySWF(SWF*swf)
1505 SWF*nswf = (SWF*)rfx_alloc(sizeof(SWF));
1507 memcpy(nswf, swf, sizeof(SWF));
1509 tag = swf->firstTag;
1512 ntag = swf_CopyTag(ntag, tag);
1514 nswf->firstTag = ntag;
1520 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1521 { TAG * t = swf->firstTag;
1524 { TAG * tnew = t->next;
1525 if (t->data) rfx_free(t->data);
1532 // include advanced functions
1534 #include "modules/swfdump.c"
1535 #include "modules/swfshape.c"
1536 #include "modules/swftext.c"
1537 #include "modules/swffont.c"
1538 #include "modules/swfobject.c"
1539 #include "modules/swfbutton.c"
1540 #include "modules/swftools.c"
1541 #include "modules/swfcgi.c"
1542 #include "modules/swfbits.c"
1543 #include "modules/swfaction.c"
1544 #include "modules/swfabc.c"
1545 #include "modules/swfsound.c"
1546 #include "modules/swfdraw.c"
1547 #include "modules/swfrender.c"
1548 #include "modules/swffilter.c"