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 */
33 #ifndef RFXSWF_DISABLESOUND
35 #include "lame/lame.h"
52 #define MALLOC_SIZE 128
53 #define INSERT_RFX_TAG
55 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
57 // inline wrapper functions
59 TAG * swf_NextTag(TAG * t) { return t->next; }
60 TAG * swf_PrevTag(TAG * t) { return t->prev; }
61 U16 swf_GetTagID(TAG * t) { return t->id; }
62 U32 swf_GetTagLen(TAG * t) { return t->len; }
63 U8* swf_GetTagLenPtr(TAG * t) { return &(t->data[t->len]); }
64 U32 swf_GetTagPos(TAG * t) { return t->pos; }
66 void swf_SetTagPos(TAG * t,U32 pos)
67 { swf_ResetReadBits(t);
68 if (pos<=t->len) t->pos = pos;
71 fprintf(stderr,"SetTagPos(%d) out of bounds: TagID = %i\n",pos, t->id);
76 char* swf_GetString(TAG*t)
79 while(t->pos < t->len && swf_GetU8(t));
80 /* make sure we always have a trailing zero byte */
81 if(t->pos == t->len) {
82 if(t->len == t->memsize) {
83 swf_ResetWriteBits(t);
89 return (char*)&(t->data[pos]);
93 { swf_ResetReadBits(t);
96 { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
101 return t->data[t->pos++];
104 U16 swf_GetU16(TAG * t)
106 swf_ResetReadBits(t);
108 if (t->pos>(t->len-2))
109 { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
113 res = t->data[t->pos] | (t->data[t->pos+1]<<8);
118 U32 swf_GetU32(TAG * t)
120 swf_ResetReadBits(t);
122 if (t->pos>(t->len-4))
123 { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
127 res = t->data[t->pos] | (t->data[t->pos+1]<<8) |
128 (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
133 int swf_GetBlock(TAG * t,U8 * b,int l)
134 // returns number of bytes written (<=l)
135 // b = NULL -> skip data
136 { swf_ResetReadBits(t);
137 if ((t->len-t->pos)<l) l=t->len-t->pos;
138 if (b && l) memcpy(b,&t->data[t->pos],l);
143 int swf_SetBlock(TAG * t,const U8 * b,int l)
144 // Appends Block to the end of Tagdata, returns size
145 { U32 newlen = t->len + l;
146 swf_ResetWriteBits(t);
147 if (newlen>t->memsize)
148 { U32 newmem = MEMSIZE(newlen);
149 U8 * newdata = (U8*)(rfx_realloc(t->data,newmem));
153 if (b) memcpy(&t->data[t->len],b,l);
154 else memset(&t->data[t->len],0x00,l);
159 int swf_SetU8(TAG * t,U8 v)
160 { swf_ResetWriteBits(t);
161 if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
162 t->data[t->len++] = v;
166 int swf_SetU16(TAG * t,U16 v)
171 swf_ResetWriteBits(t);
172 if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
173 t->data[t->len++] = a[0];
174 t->data[t->len++] = a[1];
177 void swf_SetS16(TAG * t,int v)
179 if(v>32767 || v<-32768) {
180 fprintf(stderr, "Warning: S16 overflow: %d\n", v);
182 swf_SetU16(t, (S16)v);
185 int swf_SetU32(TAG * t,U32 v)
187 a[0] = v&0xff; // to ensure correct handling of non-intel byteorder
192 swf_ResetWriteBits(t);
193 if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
194 t->data[t->len++] = a[0];
195 t->data[t->len++] = a[1];
196 t->data[t->len++] = a[2];
197 t->data[t->len++] = a[3];
201 U32 swf_GetBits(TAG * t,int nbits)
203 if (!nbits) return 0;
204 if (!t->readBit) t->readBit = 0x80;
207 if (t->data[t->pos]&t->readBit) res|=1;
211 { if (nbits) t->readBit = 0x80;
214 { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
224 S32 swf_GetSBits(TAG * t,int nbits)
225 { U32 res = swf_GetBits(t,nbits);
226 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
230 U32 reader_GetBits(reader_t*reader, int nbits)
231 { return reader_readbits(reader, nbits);
233 S32 reader_GetSBits(reader_t*reader, int nbits)
234 { U32 res = reader_readbits(reader, nbits);
235 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
239 int swf_SetBits(TAG * t,U32 v,int nbits)
240 { U32 bm = 1<<(nbits-1);
244 { if (FAILED(swf_SetU8(t,0))) return -1;
247 if (v&bm) t->data[t->len-1] |= t->writeBit;
255 // Advanced Data Access Functions
257 double swf_GetFixed(TAG * t)
259 U16 low = swf_GetU16(t);
260 U16 high = swf_GetU16(t);
261 return high + low*(1/65536.0);
263 void swf_SetFixed(TAG * t, double f)
265 U16 fr = (U16)((f-(int)f)*65536);
267 swf_SetU16(t, (U16)f - (f<0 && fr!=0));
269 float swf_GetFixed8(TAG * t)
271 U8 low = swf_GetU8(t);
272 U8 high = swf_GetU8(t);
273 return (float)(high + low*(1/256.0));
275 void swf_SetFixed8(TAG * t, float f)
277 U8 fr = (U8)((f-(int)f)*256);
279 swf_SetU8(t, (U8)f - (f<0 && fr!=0));
281 int swf_GetU30(TAG*tag)
286 U8 b = swf_GetU8(tag);
294 int swf_GetS30(TAG*tag)
300 U8 b = swf_GetU8(tag);
306 s|=0xffffffff<<shift;
313 int swf_SetS30(TAG*tag, S32 s)
316 U8 sign = s<0?0x80:0;
317 int nr=0,pos=tag->len;
323 if(s==neg && vsign==sign) {
324 /* if the value we just wrote has the same sign as s
325 and all the remaining bits are equal to the sign of s
333 swf_SetU8(tag, 0x80 | val);
340 int swf_SetU30(TAG*tag, U32 u)
345 swf_SetU8(tag, (u&~0x7f?0x80:0) | (u&0x7F));
351 int swf_SetU30String(TAG*tag, const char*str)
355 len+=swf_SetU30(tag, l);
357 swf_SetBlock(tag, (void*)str, l);
360 double swf_GetD64(TAG*tag)
362 /* FIXME: this is not big-endian compatible */
363 double value = *(double*)&tag->data[tag->pos];
368 int swf_SetD64(TAG*tag, double v)
370 /* FIXME: this is not big-endian compatible */
371 swf_SetU32(tag, ((U32*)&v)[0]);
372 swf_SetU32(tag, ((U32*)&v)[1]);
375 int swf_GetU24(TAG*tag)
377 int b1 = swf_GetU8(tag);
378 int b2 = swf_GetU8(tag);
379 int b3 = swf_GetU8(tag);
380 return b3<<16|b2<<8|b1;
382 int swf_GetS24(TAG*tag)
384 int b1 = swf_GetU8(tag);
385 int b2 = swf_GetU8(tag);
386 int b3 = swf_GetU8(tag);
388 return -1-((b3<<16|b2<<8|b1)^0xffffff);
390 return b3<<16|b2<<8|b1;
393 int swf_SetU24(TAG*tag, U32 v)
397 fprintf(stderr, "Error: Overflow in swf_SetU24()\n");
399 swf_SetU8(tag, v>>8);
400 swf_SetU8(tag, v>>16);
404 int swf_SetS24(TAG*tag, U32 v)
408 return swf_SetU24(tag, v);
409 if((v&0xff000000)!=0xff000000) {
410 fprintf(stderr, "Error: Overflow in swf_SetS24()\n");
413 swf_SetU8(tag, v>>8);
414 swf_SetU8(tag, v>>16);
420 int swf_SetRGB(TAG * t,RGBA * col)
423 { swf_SetU8(t,col->r);
426 } else swf_SetBlock(t,NULL,3);
429 void swf_GetRGB(TAG * t, RGBA * col)
434 col->r = swf_GetU8(t);
435 col->g = swf_GetU8(t);
436 col->b = swf_GetU8(t);
440 int swf_SetRGBA(TAG * t,RGBA * col)
443 { swf_SetU8(t,col->r);
447 } else swf_SetBlock(t,NULL,4);
450 void swf_GetRGBA(TAG * t, RGBA * col)
455 col->r = swf_GetU8(t);
456 col->g = swf_GetU8(t);
457 col->b = swf_GetU8(t);
458 col->a = swf_GetU8(t);
461 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
465 memset(gradient, 0, sizeof(GRADIENT));
468 U8 num = swf_GetU8(tag) & 15;
471 gradient->rgba = (RGBA*)rfx_calloc(sizeof(RGBA)*gradient->num);
472 gradient->ratios = (U8*)rfx_calloc(sizeof(gradient->ratios[0])*gradient->num);
476 U8 ratio = swf_GetU8(tag);
479 swf_GetRGB(tag, &color);
481 swf_GetRGBA(tag, &color);
483 gradient->ratios[t] = ratio;
484 gradient->rgba[t] = color;
489 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
493 memset(gradient, 0, sizeof(GRADIENT));
496 swf_SetU8(tag, gradient->num);
497 for(t=0; t<8 && t<gradient->num; t++)
499 swf_SetU8(tag, gradient->ratios[t]);
501 swf_SetRGB(tag, &gradient->rgba[t]);
503 swf_SetRGBA(tag, &gradient->rgba[t]);
507 void swf_FreeGradient(GRADIENT* gradient)
510 rfx_free(gradient->ratios);
512 rfx_free(gradient->rgba);
513 memset(gradient, 0, sizeof(GRADIENT));
516 int swf_CountUBits(U32 v,int nbits)
519 if(v == 0x00000000) n = 0;
525 return (n>nbits)?n:nbits;
528 int swf_CountBits(U32 v,int nbits)
532 { if(v == 0xffffffff) n = 1;
540 { if(v == 0x00000000) n = 0;
547 return (n>nbits)?n:nbits;
550 int swf_GetRect(TAG * t,SRECT * r)
553 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
555 nbits = (int) swf_GetBits(t,5);
556 r->xmin = swf_GetSBits(t,nbits);
557 r->xmax = swf_GetSBits(t,nbits);
558 r->ymin = swf_GetSBits(t,nbits);
559 r->ymax = swf_GetSBits(t,nbits);
563 int reader_GetRect(reader_t*reader,SRECT * r)
567 nbits = (int) reader_GetBits(reader,5);
568 r->xmin = reader_GetSBits(reader,nbits);
569 r->xmax = reader_GetSBits(reader,nbits);
570 r->ymin = reader_GetSBits(reader,nbits);
571 r->ymax = reader_GetSBits(reader,nbits);
575 int swf_SetRect(TAG * t,SRECT * r)
578 nbits = swf_CountBits(r->xmin,0);
579 nbits = swf_CountBits(r->xmax,nbits);
580 nbits = swf_CountBits(r->ymin,nbits);
581 nbits = swf_CountBits(r->ymax,nbits);
583 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
587 swf_SetBits(t,nbits,5);
588 swf_SetBits(t,r->xmin,nbits);
589 swf_SetBits(t,r->xmax,nbits);
590 swf_SetBits(t,r->ymin,nbits);
591 swf_SetBits(t,r->ymax,nbits);
596 SRECT swf_ClipRect(SRECT border, SRECT r)
598 if(r.xmax > border.xmax) r.xmax = border.xmax;
599 if(r.ymax > border.ymax) r.ymax = border.ymax;
600 if(r.xmax < border.xmin) r.xmax = border.xmin;
601 if(r.ymax < border.ymin) r.ymax = border.ymin;
603 if(r.xmin > border.xmax) r.xmin = border.xmax;
604 if(r.ymin > border.ymax) r.ymin = border.ymax;
605 if(r.xmin < border.xmin) r.xmin = border.xmin;
606 if(r.ymin < border.ymin) r.ymin = border.ymin;
610 void swf_ExpandRect(SRECT*src, SPOINT add)
612 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
617 if((add.x|add.y) == 0) src->xmax++; //make sure the bbox is not NULL anymore
620 if(add.x < src->xmin)
622 if(add.x > src->xmax)
624 if(add.y < src->ymin)
626 if(add.y > src->ymax)
629 void swf_ExpandRect2(SRECT*src, SRECT*add)
631 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
633 if((src->xmin | src->ymin | src->xmax | src->ymax)==0)
635 if(add->xmin < src->xmin)
636 src->xmin = add->xmin;
637 if(add->ymin < src->ymin)
638 src->ymin = add->ymin;
639 if(add->xmax > src->xmax)
640 src->xmax = add->xmax;
641 if(add->ymax > src->ymax)
642 src->ymax = add->ymax;
644 void swf_ExpandRect3(SRECT*src, SPOINT center, int radius)
646 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
647 src->xmin = center.x-radius;
648 src->ymin = center.y-radius;
649 src->xmax = center.x+radius;
650 src->ymax = center.y+radius;
651 if((center.x|center.y|radius) == 0) src->xmax++; //make sure the bbox is not NULL anymore
654 if(center.x - radius < src->xmin)
655 src->xmin = center.x - radius;
656 if(center.x + radius > src->xmax)
657 src->xmax = center.x + radius;
658 if(center.y - radius < src->ymin)
659 src->ymin = center.y - radius;
660 if(center.y + radius > src->ymax)
661 src->ymax = center.y + radius;
663 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
666 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
667 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
670 SRECT swf_TurnRect(SRECT r, MATRIX* m)
673 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
676 p1.x = r.xmin;p1.y = r.ymin;
677 p2.x = r.xmax;p2.y = r.ymin;
678 p3.x = r.xmin;p3.y = r.ymax;
679 p4.x = r.xmax;p4.y = r.ymax;
680 pp1 = swf_TurnPoint(p1, m);
681 pp2 = swf_TurnPoint(p2, m);
682 pp3 = swf_TurnPoint(p3, m);
683 pp4 = swf_TurnPoint(p4, m);
684 g.xmin = g.xmax = pp1.x;
685 g.ymin = g.ymax = pp1.y;
686 swf_ExpandRect(&g, pp2);
687 swf_ExpandRect(&g, pp3);
688 swf_ExpandRect(&g, pp4);
693 int swf_GetMatrix(TAG * t,MATRIX * m)
700 { m->sx = m->sy = 0x10000;
706 swf_ResetReadBits(t);
708 if (swf_GetBits(t,1))
709 { nbits = swf_GetBits(t,5);
710 m->sx = swf_GetSBits(t,nbits);
711 m->sy = swf_GetSBits(t,nbits);
713 else m->sx = m->sy = 0x10000;
715 if (swf_GetBits(t,1))
716 { nbits = swf_GetBits(t,5);
717 m->r0 = swf_GetSBits(t,nbits);
718 m->r1 = swf_GetSBits(t,nbits);
720 else m->r0 = m->r1 = 0x0;
722 nbits = swf_GetBits(t,5);
723 m->tx = swf_GetSBits(t,nbits);
724 m->ty = swf_GetSBits(t,nbits);
729 int swf_SetMatrix(TAG * t,MATRIX * m)
735 ma.sx = ma.sy = 0x10000;
740 swf_ResetWriteBits(t);
742 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
744 { swf_SetBits(t,1,1);
745 nbits = swf_CountBits(m->sx,0);
746 nbits = swf_CountBits(m->sy,nbits);
748 /* TODO: happens on AMD64 systems for normal values? */
749 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
752 swf_SetBits(t,nbits,5);
753 swf_SetBits(t,m->sx,nbits);
754 swf_SetBits(t,m->sy,nbits);
757 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
759 { swf_SetBits(t,1,1);
760 nbits = swf_CountBits(m->r0,0);
761 nbits = swf_CountBits(m->r1,nbits);
763 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
766 swf_SetBits(t,nbits,5);
767 swf_SetBits(t,m->r0,nbits);
768 swf_SetBits(t,m->r1,nbits);
771 nbits = swf_CountBits(m->tx,0);
772 nbits = swf_CountBits(m->ty,nbits);
774 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
777 swf_SetBits(t,nbits,5);
778 swf_SetBits(t,m->tx,nbits);
779 swf_SetBits(t,m->ty,nbits);
784 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha)
792 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
793 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
797 swf_ResetReadBits(t);
798 hasadd = swf_GetBits(t,1);
799 hasmul = swf_GetBits(t,1);
800 nbits = swf_GetBits(t,4);
803 { cx->r0 = (S16)swf_GetSBits(t,nbits);
804 cx->g0 = (S16)swf_GetSBits(t,nbits);
805 cx->b0 = (S16)swf_GetSBits(t,nbits);
807 cx->a0 = (S16)swf_GetSBits(t,nbits);
811 { cx->r1 = (S16)swf_GetSBits(t,nbits);
812 cx->g1 = (S16)swf_GetSBits(t,nbits);
813 cx->b1 = (S16)swf_GetSBits(t,nbits);
815 cx->a1 = (S16)swf_GetSBits(t,nbits);
821 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
829 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
830 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
840 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
841 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
844 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
845 nbits = swf_CountBits((S32)cx->r0,nbits);
846 nbits = swf_CountBits((S32)cx->g0,nbits);
847 nbits = swf_CountBits((S32)cx->b0,nbits);
851 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
852 nbits = swf_CountBits((S32)cx->r1,nbits);
853 nbits = swf_CountBits((S32)cx->g1,nbits);
854 nbits = swf_CountBits((S32)cx->b1,nbits);
857 swf_ResetWriteBits(t);
858 swf_SetBits(t,hasadd?1:0,1);
859 swf_SetBits(t,hasmul?1:0,1);
860 swf_SetBits(t,nbits,4);
863 { swf_SetBits(t,cx->r0,nbits);
864 swf_SetBits(t,cx->g0,nbits);
865 swf_SetBits(t,cx->b0,nbits);
866 if (alpha) swf_SetBits(t,cx->a0,nbits);
870 { swf_SetBits(t,cx->r1,nbits);
871 swf_SetBits(t,cx->g1,nbits);
872 swf_SetBits(t,cx->b1,nbits);
873 if (alpha) swf_SetBits(t,cx->a1,nbits);
879 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
880 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
882 void swf_SetPassword(TAG * t, const char * password)
884 /* WARNING: crypt_md5 is not reentrant */
888 #if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) && defined(HAVE_TIME_H) && defined(HAVE_TIME)
890 salt[0] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
891 salt[1] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
895 fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
896 fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
900 md5string = crypt_md5(password, salt);
903 swf_SetString(t, (U8*)md5string);
906 void swf_SetString(TAG*t, const char* s)
911 swf_SetBlock(t,s,strlen(s)+1);
915 int swf_VerifyPassword(TAG * t, const char * password)
917 char*md5string1, *md5string2;
922 if(t->len >= 5 && t->pos==0 &&
927 printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
930 md5string1 = swf_GetString(t);
932 if(strncmp(md5string1, "$1$",3 )) {
933 fprintf(stderr, "rfxswf: no salt in pw string\n");
936 x = strchr(md5string1+3, '$');
938 fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
941 n = x-(md5string1+3);
942 salt = (char*)rfx_alloc(n+1);
943 memcpy(salt, md5string1+3, n);
946 md5string2 = crypt_md5(password, salt);
948 if(strcmp(md5string1, md5string2) != 0)
953 // Tag List Manipulating Functions
955 TAG * swf_InsertTag(TAG * after,U16 id)
958 t = (TAG *)rfx_calloc(sizeof(TAG));
964 t->next = after->next;
966 if (t->next) t->next->prev = t;
971 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
974 t = (TAG *)rfx_calloc(sizeof(TAG));
980 t->prev = before->prev;
982 if (t->prev) t->prev->next = t;
984 if(swf && swf->firstTag == before) {
990 void swf_ClearTag(TAG * t)
992 if (t->data) rfx_free(t->data);
1001 void swf_ResetTag(TAG*tag, U16 id)
1003 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
1007 TAG* swf_CopyTag(TAG*tag, TAG*to_copy)
1009 tag = swf_InsertTag(tag, to_copy->id);
1010 swf_SetBlock(tag, to_copy->data, to_copy->len);
1014 TAG* swf_DeleteTag(SWF*swf, TAG * t)
1018 if (swf && swf->firstTag==t)
1019 swf->firstTag = t->next;
1020 if (t->prev) t->prev->next = t->next;
1021 if (t->next) t->next->prev = t->prev;
1023 if (t->data) rfx_free(t->data);
1028 TAG * swf_ReadTag(reader_t*reader, TAG * prev)
1034 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
1042 if (reader->read(reader, &len, 4) != 4) return NULL;
1046 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
1047 // Sprite handling fix: Flatten sprite tree
1049 t = (TAG *)rfx_calloc(sizeof(TAG));
1055 { t->data = (U8*)rfx_alloc(t->len);
1056 t->memsize = t->len;
1057 if (reader->read(reader, t->data, t->len) != t->len) {
1058 fprintf(stderr, "rfxswf: Warning: Short read (tagid %d). File truncated?\n", t->id);
1059 free(t->data);t->data=0;
1074 int swf_DefineSprite_GetRealSize(TAG * t);
1076 int swf_WriteTag2(writer_t*writer, TAG * t)
1077 // returns tag length in bytes (incl. Header), -1 = Error
1078 // writer = 0 -> no output
1085 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
1087 short_tag = len<0x3f&&
1088 (t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2&&t->id!=ST_SOUNDSTREAMBLOCK&&
1089 t->id!=ST_DEFINEBITSJPEG&&t->id!=ST_DEFINEBITSJPEG2&&t->id!=ST_DEFINEBITSJPEG3);
1094 int oldpos = writer->pos;
1098 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
1099 if (writer->write(writer,raw,2)!=2)
1102 fprintf(stderr,"WriteTag() failed: Short Header.\n");
1109 raw[0] = SWAP16((t->id<<6)|0x3f);
1110 if (writer->write(writer,raw,2)!=2)
1113 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
1119 if (writer->write(writer,&len,4)!=4)
1122 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
1129 { if (writer->write(writer,t->data,t->len)!=t->len)
1132 fprintf(stderr,"WriteTag() failed: Data.\n");
1138 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
1142 writer->flush(writer);
1143 printf("TAG %s costs %d bytes\n", swf_TagGetName(t), writer->pos-oldpos);
1147 return t->len+(short_tag?2:6);
1150 int swf_WriteTag(int handle, TAG * t)
1155 return swf_WriteTag2(0, t);
1156 writer_init_filewriter(&writer, handle);
1157 len = swf_WriteTag2(&writer, t);
1158 writer.finish(&writer);
1162 int swf_DefineSprite_GetRealSize(TAG * t)
1163 // Sprite Handling: Helper function to pack DefineSprite-Tag
1165 if(len>4) { // folded sprite
1169 { t = swf_NextTag(t);
1170 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
1172 } while (t&&(t->id!=ST_END));
1176 void swf_UnFoldSprite(TAG * t)
1181 U16 spriteid,spriteframes;
1183 if(t->id!=ST_DEFINESPRITE)
1185 if(t->len<=4) // not folded
1190 spriteid = swf_GetU16(t); //id
1191 spriteframes = swf_GetU16(t); //frames
1198 tmp = swf_GetU16(t);
1203 if(id == ST_DEFINESPRITE && len<=4)
1207 len = swf_GetU32(t);
1208 it = swf_InsertTag(next, id);
1213 { it->data = (U8*)rfx_alloc(it->len);
1214 it->memsize = it->len;
1215 swf_GetBlock(t, it->data, it->len);
1222 rfx_free(t->data); t->data = 0;
1223 t->memsize = t->len = t->pos = 0;
1225 swf_SetU16(t, spriteid);
1226 swf_SetU16(t, spriteframes);
1229 void swf_FoldSprite(TAG * t)
1234 if(t->id!=ST_DEFINESPRITE)
1237 fprintf(stderr, "Error: Sprite has no ID!");
1241 /* sprite is already folded */
1248 t->len = t->pos = t->memsize = 0;
1253 t = swf_NextTag(sprtag);
1258 if(t->id==ST_SHOWFRAME) frames++;
1259 if(t->id == ST_DEFINESPRITE && t->len<=4)
1264 } while(t && level);
1266 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1268 swf_SetU16(sprtag, id);
1269 swf_SetU16(sprtag, frames);
1271 t = swf_NextTag(sprtag);
1277 (t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2&&t->id!=ST_SOUNDSTREAMBLOCK&&
1278 t->id!=ST_DEFINEBITSJPEG&&t->id!=ST_DEFINEBITSJPEG2&&t->id!=ST_DEFINEBITSJPEG3)
1280 swf_SetU16(sprtag,t->len|(t->id<<6));
1282 swf_SetU16(sprtag,0x3f|(t->id<<6));
1283 swf_SetU32(sprtag,t->len);
1286 swf_SetBlock(sprtag,t->data, t->len);
1288 if(t->id == ST_DEFINESPRITE && t->len<=4)
1293 swf_DeleteTag(0, tmp);
1297 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1299 // sprtag->next = t;
1300 // t->prev = sprtag;
1303 int swf_IsFolded(TAG * t)
1305 return (t->id == ST_DEFINESPRITE && t->len>4);
1308 void swf_FoldAll(SWF*swf)
1310 TAG*tag = swf->firstTag;
1311 //swf_DumpSWF(stdout, swf);
1313 if(tag->id == ST_DEFINESPRITE) {
1314 swf_FoldSprite(tag);
1315 //swf_DumpSWF(stdout, swf);
1317 tag = swf_NextTag(tag);
1321 void swf_UnFoldAll(SWF*swf)
1323 TAG*tag = swf->firstTag;
1325 if(tag->id == ST_DEFINESPRITE)
1326 swf_UnFoldSprite(tag);
1331 void swf_OptimizeTagOrder(SWF*swf)
1338 /* at the moment, we don't actually do optimizing,
1339 only fixing of non-spec-conformant things like
1346 tag = swf->firstTag;
1349 if(tag->id == ST_DEFINESPRITE) {
1351 /* ??? all sprites are supposed to be unfolded */
1352 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1362 /* move non-sprite tags out of sprite */
1363 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1364 /* remove tag from current position */
1365 tag->prev->next = tag->next;
1367 tag->next->prev = tag->prev;
1369 /* insert before tag level0 */
1371 tag->prev = level0->prev;
1374 tag->prev->next = tag;
1376 swf->firstTag = tag;
1380 if(tag->id == ST_END) {
1391 int swf_ReadSWF2(reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1393 if (!swf) return -1;
1394 memset(swf,0x00,sizeof(SWF));
1396 { char b[32]; // read Header
1402 if ((len = reader->read(reader ,b,8))<8) return -1;
1404 if (b[0]!='F' && b[0]!='C') return -1;
1405 if (b[1]!='W') return -1;
1406 if (b[2]!='S') return -1;
1407 swf->fileVersion = b[3];
1408 swf->compressed = (b[0]=='C')?1:0;
1409 swf->fileSize = GET32(&b[4]);
1411 if(swf->compressed) {
1412 reader_init_zlibinflate(&zreader, reader);
1415 swf->compressed = 0; // derive from version number from now on
1417 reader_GetRect(reader, &swf->movieSize);
1418 reader->read(reader, &swf->frameRate, 2);
1419 swf->frameRate = SWAP16(swf->frameRate);
1420 reader->read(reader, &swf->frameCount, 2);
1421 swf->frameCount = SWAP16(swf->frameCount);
1423 /* read tags and connect to list */
1426 t = swf_ReadTag(reader,t);
1427 if(t && t->id == ST_FILEATTRIBUTES) {
1428 swf->fileAttributes = swf_GetU32(t);
1429 swf_ResetReadBits(t);
1432 swf->firstTag = t1.next;
1433 t1.next->prev = NULL;
1439 int swf_ReadSWF(int handle, SWF * swf)
1442 reader_init_filereader(&reader, handle);
1443 return swf_ReadSWF2(&reader, swf);
1446 int no_extra_tags = 0;
1448 int WriteExtraTags(SWF*swf, writer_t*writer)
1450 TAG*t = swf->firstTag;
1451 TAG* has_fileattributes=0;
1452 int has_scenedescription=0;
1453 int has_version_8_action=0;
1454 int has_version_9_action=0;
1457 if(t->id == ST_FILEATTRIBUTES)
1458 has_fileattributes = t;
1459 if(t->id == ST_SCENEDESCRIPTION)
1460 has_scenedescription = 1;
1461 if(t->id == ST_DOABC)
1462 has_version_9_action=1;
1463 /* FIXME: this doesn't yet find actionscript in buttons */
1464 if(t->id == ST_DOACTION || t->id == ST_DOINITACTION)
1465 has_version_8_action=1;
1466 if(t->id == ST_PLACEOBJECT2 && t->len && (t->data[0]&0x80))
1467 has_version_8_action=1;
1470 if(has_version_8_action && has_version_9_action) {
1471 fprintf(stderr, "Warning: File contains both flash 8 and flash 9 actionscript\n");
1474 if(swf->fileVersion >= 9) {
1475 if(!has_fileattributes) {
1476 U32 flags = swf->fileAttributes|0x08; // 16 = has symbolclass tag | 8 = actionscript3 | 1 = usenetwork
1477 if(has_version_8_action && !has_version_9_action)
1479 TAG*fileattrib = swf_InsertTag(0, ST_FILEATTRIBUTES);
1480 swf_SetU32(fileattrib, flags);
1482 if(swf_WriteTag2(writer, fileattrib)<0)
1485 len += swf_WriteTag(-1,fileattrib);
1487 swf_DeleteTag(0, fileattrib);
1489 if(swf_WriteTag2(writer, has_fileattributes)<0)
1492 if(!has_scenedescription) {
1493 TAG*scene = swf_InsertTag(0, ST_SCENEDESCRIPTION);
1494 swf_SetU16(scene, 1);
1495 swf_SetString(scene, (U8*)"Scene 1");
1496 swf_SetU8(scene, 0);
1498 if(swf_WriteTag2(writer, scene)<0)
1501 len += swf_WriteTag(-1,scene);
1503 swf_DeleteTag(0, scene);
1509 int swf_WriteSWF2(writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1517 writer_t*original_writer = writer;
1518 int writer_lastpos = 0;
1520 if (!swf) return -1;
1521 if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1523 if(original_writer) writer_lastpos = original_writer->pos;
1525 // Count Frames + File Size
1531 len += WriteExtraTags(swf, 0);
1533 len += swf_WriteTag(-1,t);
1534 if(t->id == ST_DEFINESPRITE && !swf_IsFolded(t)) inSprite++;
1535 else if(t->id == ST_END && inSprite) inSprite--;
1536 else if(t->id == ST_END && !inSprite) {
1537 if(t->prev && t->prev->id!=ST_SHOWFRAME)
1540 else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1548 memset(&t1,0x00,sizeof(TAG));
1552 { // measure header file size
1555 memset(&t2,0x00,sizeof(TAG));
1558 swf_SetRect(&t2, &swf->movieSize);
1559 swf_SetU16(&t2, swf->frameRate);
1560 swf_SetU16(&t2, swf->frameCount);
1561 l = swf_GetTagLen(&t2)+8;
1563 if(swf->compressed == 8) {
1568 if(len) {// don't touch headers without tags
1569 swf->fileSize = fileSize;
1570 swf->frameCount = frameCount;
1573 if(swf->compressed != 8) {
1574 /* compressed flag set to 8 means "skip first 8
1575 header bytes". This is necessary if the caller wants to
1576 create compressed SWFs himself .
1577 It also means that we don't initialize our own zlib
1578 writer, but assume the caller provided one.
1580 if(swf->compressed==1 || (swf->compressed==0 && swf->fileVersion>=6)) {
1582 writer->write(writer, id, 3);
1585 writer->write(writer, id, 3);
1588 writer->write(writer, &swf->fileVersion, 1);
1589 PUT32(b4, swf->fileSize);
1590 writer->write(writer, b4, 4);
1592 if(swf->compressed==1 || (swf->compressed==0 && swf->fileVersion>=6)) {
1593 writer_init_zlibdeflate(&zwriter, writer);
1598 swf_SetRect(&t1,&swf->movieSize);
1599 swf_SetU16(&t1,swf->frameRate);
1600 swf_SetU16(&t1,swf->frameCount);
1602 ret = writer->write(writer,b,swf_GetTagLen(&t1));
1603 if (ret!=swf_GetTagLen(&t1))
1606 fprintf(stderr, "ret:%d\n",ret);
1608 fprintf(stderr,"WriteSWF() failed: Header.\n");
1613 if(!no_extra_tags) {
1614 WriteExtraTags(swf, writer);
1619 if(no_extra_tags || t->id != ST_FILEATTRIBUTES) {
1620 if(swf_WriteTag2(writer, t)<0)
1625 if(swf->compressed==1 || (swf->compressed==0 && swf->fileVersion>=6) || swf->compressed==8) {
1626 if(swf->compressed != 8) {
1627 zwriter.finish(&zwriter);
1628 return original_writer->pos - writer_lastpos;
1630 return (int)fileSize;
1632 return (int)fileSize;
1637 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1643 writer_init_nullwriter(&writer);
1644 len = swf_WriteSWF2(&writer, swf);
1647 writer_init_filewriter(&writer, handle);
1648 len = swf_WriteSWF2(&writer, swf);
1649 writer.finish(&writer);
1653 int swf_WriteHeader2(writer_t*writer,SWF * swf)
1656 memcpy(&myswf,swf,sizeof(SWF));
1658 return swf_WriteSWF2(writer, &myswf);
1661 int swf_WriteHeader(int handle,SWF * swf)
1664 memcpy(&myswf,swf,sizeof(SWF));
1666 return swf_WriteSWF(handle, &myswf);
1669 int swf_WriteCGI(SWF * swf)
1673 len = swf_WriteSWF(-1,swf);
1675 if (len<0) return -1;
1677 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1678 "Accept-Ranges: bytes\n"
1679 "Content-Length: %lu\n"
1680 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1683 write(fileno(stdout),s,strlen(s));
1684 return swf_WriteSWF(fileno(stdout),swf);
1687 SWF* swf_CopySWF(SWF*swf)
1689 SWF*nswf = (SWF*)rfx_alloc(sizeof(SWF));
1691 memcpy(nswf, swf, sizeof(SWF));
1693 tag = swf->firstTag;
1696 ntag = swf_CopyTag(ntag, tag);
1698 nswf->firstTag = ntag;
1704 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1705 { TAG * t = swf->firstTag;
1708 { TAG * tnew = t->next;
1709 if (t->data) rfx_free(t->data);
1716 // include advanced functions
1718 //#include "modules/swfdump.c"
1719 //#include "modules/swfshape.c"
1720 //#include "modules/swftext.c"
1721 //#include "modules/swffont.c"
1722 //#include "modules/swfobject.c"
1723 //#include "modules/swfbutton.c"
1724 //#include "modules/swftools.c"
1725 //#include "modules/swfcgi.c"
1726 //#include "modules/swfbits.c"
1727 //#include "modules/swfaction.c"
1728 //#include "modules/swfabc.c"
1729 //#include "modules/swfsound.c"
1730 //#include "modules/swfdraw.c"
1731 //#include "modules/swfrender.c"
1732 //#include "modules/swffilter.c"