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)
287 U8 b = swf_GetU8(tag);
291 if(!(b&128) || shift>=32)
294 /*int nr2= swf_SetU30(0, s);
296 printf("Signed value %d stored in %d bytes, I'd store it in %d bytes\n", s, nr, nr2);
300 int swf_GetS30(TAG*tag)
306 U8 b = swf_GetU8(tag);
310 if(!(b&128) || shift>=32) {
312 s|=0xffffffff<<shift;
317 /* It's not uncommon for other applications (Flex for all negative numbers, and
318 Flash for -1) to generate a lot more bytes than would be necessary.
319 int nr2= swf_SetS30(0, s);
321 printf("Signed value %d stored in %d bytes, I'd store it in %d bytes\n", s, nr, nr2);
325 int swf_SetS30(TAG*tag, S32 s)
328 U8 sign = s<0?0x80:0;
335 if(s==neg && vsign==sign) {
336 /* if the value we just wrote has the same sign as s
337 and all the remaining bits are equal to the sign of s
345 swf_SetU8(tag, 0x80 | val);
352 int swf_SetU30(TAG*tag, U32 u)
355 fprintf(stderr, "Bit 31 set in U30 value");
361 swf_SetU8(tag, (u&~0x7f?0x80:0) | (u&0x7F));
367 int swf_SetU30String(TAG*tag, const char*str)
371 len+=swf_SetU30(tag, l);
373 swf_SetBlock(tag, (void*)str, l);
376 double swf_GetD64(TAG*tag)
378 /* FIXME: this is not big-endian compatible */
379 double value = *(double*)&tag->data[tag->pos];
384 int swf_SetD64(TAG*tag, double v)
386 /* FIXME: this is not big-endian compatible */
387 swf_SetU32(tag, ((U32*)&v)[0]);
388 swf_SetU32(tag, ((U32*)&v)[1]);
391 int swf_GetU24(TAG*tag)
393 int b1 = swf_GetU8(tag);
394 int b2 = swf_GetU8(tag);
395 int b3 = swf_GetU8(tag);
396 return b3<<16|b2<<8|b1;
398 int swf_GetS24(TAG*tag)
400 int b1 = swf_GetU8(tag);
401 int b2 = swf_GetU8(tag);
402 int b3 = swf_GetU8(tag);
404 return -1-((b3<<16|b2<<8|b1)^0xffffff);
406 return b3<<16|b2<<8|b1;
409 int swf_SetU24(TAG*tag, U32 v)
413 fprintf(stderr, "Error: Overflow in swf_SetU24()\n");
415 swf_SetU8(tag, v>>8);
416 swf_SetU8(tag, v>>16);
420 int swf_SetS24(TAG*tag, U32 v)
424 return swf_SetU24(tag, v);
425 if((v&0xff000000)!=0xff000000) {
426 fprintf(stderr, "Error: Overflow in swf_SetS24()\n");
429 swf_SetU8(tag, v>>8);
430 swf_SetU8(tag, v>>16);
436 int swf_SetRGB(TAG * t,RGBA * col)
439 { swf_SetU8(t,col->r);
442 } else swf_SetBlock(t,NULL,3);
445 void swf_GetRGB(TAG * t, RGBA * col)
450 col->r = swf_GetU8(t);
451 col->g = swf_GetU8(t);
452 col->b = swf_GetU8(t);
456 int swf_SetRGBA(TAG * t,RGBA * col)
459 { swf_SetU8(t,col->r);
463 } else swf_SetBlock(t,NULL,4);
466 void swf_GetRGBA(TAG * t, RGBA * col)
471 col->r = swf_GetU8(t);
472 col->g = swf_GetU8(t);
473 col->b = swf_GetU8(t);
474 col->a = swf_GetU8(t);
477 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
481 memset(gradient, 0, sizeof(GRADIENT));
484 U8 num = swf_GetU8(tag) & 15;
487 gradient->rgba = (RGBA*)rfx_calloc(sizeof(RGBA)*gradient->num);
488 gradient->ratios = (U8*)rfx_calloc(sizeof(gradient->ratios[0])*gradient->num);
492 U8 ratio = swf_GetU8(tag);
495 swf_GetRGB(tag, &color);
497 swf_GetRGBA(tag, &color);
499 gradient->ratios[t] = ratio;
500 gradient->rgba[t] = color;
505 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
509 memset(gradient, 0, sizeof(GRADIENT));
512 swf_SetU8(tag, gradient->num);
513 for(t=0; t<8 && t<gradient->num; t++)
515 swf_SetU8(tag, gradient->ratios[t]);
517 swf_SetRGB(tag, &gradient->rgba[t]);
519 swf_SetRGBA(tag, &gradient->rgba[t]);
523 void swf_FreeGradient(GRADIENT* gradient)
526 rfx_free(gradient->ratios);
528 rfx_free(gradient->rgba);
529 memset(gradient, 0, sizeof(GRADIENT));
532 int swf_CountUBits(U32 v,int nbits)
535 if(v == 0x00000000) n = 0;
541 return (n>nbits)?n:nbits;
544 int swf_CountBits(U32 v,int nbits)
548 { if(v == 0xffffffff) n = 1;
556 { if(v == 0x00000000) n = 0;
563 return (n>nbits)?n:nbits;
566 int swf_GetRect(TAG * t,SRECT * r)
569 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
571 nbits = (int) swf_GetBits(t,5);
572 r->xmin = swf_GetSBits(t,nbits);
573 r->xmax = swf_GetSBits(t,nbits);
574 r->ymin = swf_GetSBits(t,nbits);
575 r->ymax = swf_GetSBits(t,nbits);
579 int reader_GetRect(reader_t*reader,SRECT * r)
583 nbits = (int) reader_GetBits(reader,5);
584 r->xmin = reader_GetSBits(reader,nbits);
585 r->xmax = reader_GetSBits(reader,nbits);
586 r->ymin = reader_GetSBits(reader,nbits);
587 r->ymax = reader_GetSBits(reader,nbits);
591 int swf_SetRect(TAG * t,SRECT * r)
594 nbits = swf_CountBits(r->xmin,0);
595 nbits = swf_CountBits(r->xmax,nbits);
596 nbits = swf_CountBits(r->ymin,nbits);
597 nbits = swf_CountBits(r->ymax,nbits);
599 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
603 swf_SetBits(t,nbits,5);
604 swf_SetBits(t,r->xmin,nbits);
605 swf_SetBits(t,r->xmax,nbits);
606 swf_SetBits(t,r->ymin,nbits);
607 swf_SetBits(t,r->ymax,nbits);
612 SRECT swf_ClipRect(SRECT border, SRECT r)
614 if(r.xmax > border.xmax) r.xmax = border.xmax;
615 if(r.ymax > border.ymax) r.ymax = border.ymax;
616 if(r.xmax < border.xmin) r.xmax = border.xmin;
617 if(r.ymax < border.ymin) r.ymax = border.ymin;
619 if(r.xmin > border.xmax) r.xmin = border.xmax;
620 if(r.ymin > border.ymax) r.ymin = border.ymax;
621 if(r.xmin < border.xmin) r.xmin = border.xmin;
622 if(r.ymin < border.ymin) r.ymin = border.ymin;
626 void swf_ExpandRect(SRECT*src, SPOINT add)
628 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
633 if((add.x|add.y) == 0) src->xmax++; //make sure the bbox is not NULL anymore
636 if(add.x < src->xmin)
638 if(add.x > src->xmax)
640 if(add.y < src->ymin)
642 if(add.y > src->ymax)
645 void swf_ExpandRect2(SRECT*src, SRECT*add)
647 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
649 if((src->xmin | src->ymin | src->xmax | src->ymax)==0)
651 if(add->xmin < src->xmin)
652 src->xmin = add->xmin;
653 if(add->ymin < src->ymin)
654 src->ymin = add->ymin;
655 if(add->xmax > src->xmax)
656 src->xmax = add->xmax;
657 if(add->ymax > src->ymax)
658 src->ymax = add->ymax;
660 void swf_ExpandRect3(SRECT*src, SPOINT center, int radius)
662 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
663 src->xmin = center.x-radius;
664 src->ymin = center.y-radius;
665 src->xmax = center.x+radius;
666 src->ymax = center.y+radius;
667 if((center.x|center.y|radius) == 0) src->xmax++; //make sure the bbox is not NULL anymore
670 if(center.x - radius < src->xmin)
671 src->xmin = center.x - radius;
672 if(center.x + radius > src->xmax)
673 src->xmax = center.x + radius;
674 if(center.y - radius < src->ymin)
675 src->ymin = center.y - radius;
676 if(center.y + radius > src->ymax)
677 src->ymax = center.y + radius;
679 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
682 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
683 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
686 SRECT swf_TurnRect(SRECT r, MATRIX* m)
689 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
692 p1.x = r.xmin;p1.y = r.ymin;
693 p2.x = r.xmax;p2.y = r.ymin;
694 p3.x = r.xmin;p3.y = r.ymax;
695 p4.x = r.xmax;p4.y = r.ymax;
696 pp1 = swf_TurnPoint(p1, m);
697 pp2 = swf_TurnPoint(p2, m);
698 pp3 = swf_TurnPoint(p3, m);
699 pp4 = swf_TurnPoint(p4, m);
700 g.xmin = g.xmax = pp1.x;
701 g.ymin = g.ymax = pp1.y;
702 swf_ExpandRect(&g, pp2);
703 swf_ExpandRect(&g, pp3);
704 swf_ExpandRect(&g, pp4);
709 int swf_GetMatrix(TAG * t,MATRIX * m)
716 { m->sx = m->sy = 0x10000;
722 swf_ResetReadBits(t);
724 if (swf_GetBits(t,1))
725 { nbits = swf_GetBits(t,5);
726 m->sx = swf_GetSBits(t,nbits);
727 m->sy = swf_GetSBits(t,nbits);
729 else m->sx = m->sy = 0x10000;
731 if (swf_GetBits(t,1))
732 { nbits = swf_GetBits(t,5);
733 m->r0 = swf_GetSBits(t,nbits);
734 m->r1 = swf_GetSBits(t,nbits);
736 else m->r0 = m->r1 = 0x0;
738 nbits = swf_GetBits(t,5);
739 m->tx = swf_GetSBits(t,nbits);
740 m->ty = swf_GetSBits(t,nbits);
745 int swf_SetMatrix(TAG * t,MATRIX * m)
751 ma.sx = ma.sy = 0x10000;
756 swf_ResetWriteBits(t);
758 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
760 { swf_SetBits(t,1,1);
761 nbits = swf_CountBits(m->sx,0);
762 nbits = swf_CountBits(m->sy,nbits);
764 /* TODO: happens on AMD64 systems for normal values? */
765 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
768 swf_SetBits(t,nbits,5);
769 swf_SetBits(t,m->sx,nbits);
770 swf_SetBits(t,m->sy,nbits);
773 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
775 { swf_SetBits(t,1,1);
776 nbits = swf_CountBits(m->r0,0);
777 nbits = swf_CountBits(m->r1,nbits);
779 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
782 swf_SetBits(t,nbits,5);
783 swf_SetBits(t,m->r0,nbits);
784 swf_SetBits(t,m->r1,nbits);
787 nbits = swf_CountBits(m->tx,0);
788 nbits = swf_CountBits(m->ty,nbits);
790 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
793 swf_SetBits(t,nbits,5);
794 swf_SetBits(t,m->tx,nbits);
795 swf_SetBits(t,m->ty,nbits);
800 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha)
808 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
809 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
813 swf_ResetReadBits(t);
814 hasadd = swf_GetBits(t,1);
815 hasmul = swf_GetBits(t,1);
816 nbits = swf_GetBits(t,4);
819 { cx->r0 = (S16)swf_GetSBits(t,nbits);
820 cx->g0 = (S16)swf_GetSBits(t,nbits);
821 cx->b0 = (S16)swf_GetSBits(t,nbits);
823 cx->a0 = (S16)swf_GetSBits(t,nbits);
827 { cx->r1 = (S16)swf_GetSBits(t,nbits);
828 cx->g1 = (S16)swf_GetSBits(t,nbits);
829 cx->b1 = (S16)swf_GetSBits(t,nbits);
831 cx->a1 = (S16)swf_GetSBits(t,nbits);
837 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
845 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
846 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
856 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
857 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
860 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
861 nbits = swf_CountBits((S32)cx->r0,nbits);
862 nbits = swf_CountBits((S32)cx->g0,nbits);
863 nbits = swf_CountBits((S32)cx->b0,nbits);
867 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
868 nbits = swf_CountBits((S32)cx->r1,nbits);
869 nbits = swf_CountBits((S32)cx->g1,nbits);
870 nbits = swf_CountBits((S32)cx->b1,nbits);
873 swf_ResetWriteBits(t);
874 swf_SetBits(t,hasadd?1:0,1);
875 swf_SetBits(t,hasmul?1:0,1);
876 swf_SetBits(t,nbits,4);
879 { swf_SetBits(t,cx->r0,nbits);
880 swf_SetBits(t,cx->g0,nbits);
881 swf_SetBits(t,cx->b0,nbits);
882 if (alpha) swf_SetBits(t,cx->a0,nbits);
886 { swf_SetBits(t,cx->r1,nbits);
887 swf_SetBits(t,cx->g1,nbits);
888 swf_SetBits(t,cx->b1,nbits);
889 if (alpha) swf_SetBits(t,cx->a1,nbits);
895 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
896 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
898 void swf_SetPassword(TAG * t, const char * password)
900 /* WARNING: crypt_md5 is not reentrant */
904 #if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) && defined(HAVE_TIME_H) && defined(HAVE_TIME)
906 salt[0] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
907 salt[1] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
911 fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
912 fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
916 md5string = crypt_md5(password, salt);
919 swf_SetString(t, (U8*)md5string);
922 void swf_SetString(TAG*t, const char* s)
927 swf_SetBlock(t,s,strlen(s)+1);
931 int swf_VerifyPassword(TAG * t, const char * password)
933 char*md5string1, *md5string2;
938 if(t->len >= 5 && t->pos==0 &&
943 printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
946 md5string1 = swf_GetString(t);
948 if(strncmp(md5string1, "$1$",3 )) {
949 fprintf(stderr, "rfxswf: no salt in pw string\n");
952 x = strchr(md5string1+3, '$');
954 fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
957 n = x-(md5string1+3);
958 salt = (char*)rfx_alloc(n+1);
959 memcpy(salt, md5string1+3, n);
962 md5string2 = crypt_md5(password, salt);
964 if(strcmp(md5string1, md5string2) != 0)
969 // Tag List Manipulating Functions
971 TAG * swf_InsertTag(TAG * after,U16 id)
974 t = (TAG *)rfx_calloc(sizeof(TAG));
980 t->next = after->next;
982 if (t->next) t->next->prev = t;
987 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
990 t = (TAG *)rfx_calloc(sizeof(TAG));
996 t->prev = before->prev;
998 if (t->prev) t->prev->next = t;
1000 if(swf && swf->firstTag == before) {
1006 void swf_ClearTag(TAG * t)
1008 if (t->data) rfx_free(t->data);
1017 void swf_ResetTag(TAG*tag, U16 id)
1019 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
1023 TAG* swf_CopyTag(TAG*tag, TAG*to_copy)
1025 tag = swf_InsertTag(tag, to_copy->id);
1026 swf_SetBlock(tag, to_copy->data, to_copy->len);
1030 TAG* swf_DeleteTag(SWF*swf, TAG * t)
1034 if (swf && swf->firstTag==t)
1035 swf->firstTag = t->next;
1036 if (t->prev) t->prev->next = t->next;
1037 if (t->next) t->next->prev = t->prev;
1039 if (t->data) rfx_free(t->data);
1044 TAG * swf_ReadTag(reader_t*reader, TAG * prev)
1050 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
1058 if (reader->read(reader, &len, 4) != 4) return NULL;
1062 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
1063 // Sprite handling fix: Flatten sprite tree
1065 t = (TAG *)rfx_calloc(sizeof(TAG));
1071 { t->data = (U8*)rfx_alloc(t->len);
1072 t->memsize = t->len;
1073 if (reader->read(reader, t->data, t->len) != t->len) {
1074 fprintf(stderr, "rfxswf: Warning: Short read (tagid %d). File truncated?\n", t->id);
1075 free(t->data);t->data=0;
1090 int swf_DefineSprite_GetRealSize(TAG * t);
1092 int swf_WriteTag2(writer_t*writer, TAG * t)
1093 // returns tag length in bytes (incl. Header), -1 = Error
1094 // writer = 0 -> no output
1101 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
1103 short_tag = len<0x3f&&
1104 (t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2&&t->id!=ST_SOUNDSTREAMBLOCK&&
1105 t->id!=ST_DEFINEBITSJPEG&&t->id!=ST_DEFINEBITSJPEG2&&t->id!=ST_DEFINEBITSJPEG3);
1110 int oldpos = writer->pos;
1114 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
1115 if (writer->write(writer,raw,2)!=2)
1118 fprintf(stderr,"WriteTag() failed: Short Header.\n");
1125 raw[0] = SWAP16((t->id<<6)|0x3f);
1126 if (writer->write(writer,raw,2)!=2)
1129 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
1135 if (writer->write(writer,&len,4)!=4)
1138 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
1145 { if (writer->write(writer,t->data,t->len)!=t->len)
1148 fprintf(stderr,"WriteTag() failed: Data.\n");
1154 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
1158 writer->flush(writer);
1159 printf("TAG %s costs %d bytes\n", swf_TagGetName(t), writer->pos-oldpos);
1163 return t->len+(short_tag?2:6);
1166 int swf_WriteTag(int handle, TAG * t)
1171 return swf_WriteTag2(0, t);
1172 writer_init_filewriter(&writer, handle);
1173 len = swf_WriteTag2(&writer, t);
1174 writer.finish(&writer);
1178 int swf_DefineSprite_GetRealSize(TAG * t)
1179 // Sprite Handling: Helper function to pack DefineSprite-Tag
1181 if(len>4) { // folded sprite
1185 { t = swf_NextTag(t);
1186 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
1188 } while (t&&(t->id!=ST_END));
1192 void swf_UnFoldSprite(TAG * t)
1197 U16 spriteid,spriteframes;
1199 if(t->id!=ST_DEFINESPRITE)
1201 if(t->len<=4) // not folded
1206 spriteid = swf_GetU16(t); //id
1207 spriteframes = swf_GetU16(t); //frames
1214 tmp = swf_GetU16(t);
1219 if(id == ST_DEFINESPRITE && len<=4)
1223 len = swf_GetU32(t);
1224 it = swf_InsertTag(next, id);
1229 { it->data = (U8*)rfx_alloc(it->len);
1230 it->memsize = it->len;
1231 swf_GetBlock(t, it->data, it->len);
1238 rfx_free(t->data); t->data = 0;
1239 t->memsize = t->len = t->pos = 0;
1241 swf_SetU16(t, spriteid);
1242 swf_SetU16(t, spriteframes);
1245 void swf_FoldSprite(TAG * t)
1250 if(t->id!=ST_DEFINESPRITE)
1253 fprintf(stderr, "Error: Sprite has no ID!");
1257 /* sprite is already folded */
1264 t->len = t->pos = t->memsize = 0;
1269 t = swf_NextTag(sprtag);
1274 if(t->id==ST_SHOWFRAME) frames++;
1275 if(t->id == ST_DEFINESPRITE && t->len<=4)
1280 } while(t && level);
1282 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1284 swf_SetU16(sprtag, id);
1285 swf_SetU16(sprtag, frames);
1287 t = swf_NextTag(sprtag);
1293 (t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2&&t->id!=ST_SOUNDSTREAMBLOCK&&
1294 t->id!=ST_DEFINEBITSJPEG&&t->id!=ST_DEFINEBITSJPEG2&&t->id!=ST_DEFINEBITSJPEG3)
1296 swf_SetU16(sprtag,t->len|(t->id<<6));
1298 swf_SetU16(sprtag,0x3f|(t->id<<6));
1299 swf_SetU32(sprtag,t->len);
1302 swf_SetBlock(sprtag,t->data, t->len);
1304 if(t->id == ST_DEFINESPRITE && t->len<=4)
1309 swf_DeleteTag(0, tmp);
1313 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1315 // sprtag->next = t;
1316 // t->prev = sprtag;
1319 int swf_IsFolded(TAG * t)
1321 return (t->id == ST_DEFINESPRITE && t->len>4);
1324 void swf_FoldAll(SWF*swf)
1326 TAG*tag = swf->firstTag;
1327 //swf_DumpSWF(stdout, swf);
1329 if(tag->id == ST_DEFINESPRITE) {
1330 swf_FoldSprite(tag);
1331 //swf_DumpSWF(stdout, swf);
1333 tag = swf_NextTag(tag);
1337 void swf_UnFoldAll(SWF*swf)
1339 TAG*tag = swf->firstTag;
1341 if(tag->id == ST_DEFINESPRITE)
1342 swf_UnFoldSprite(tag);
1347 void swf_OptimizeTagOrder(SWF*swf)
1354 /* at the moment, we don't actually do optimizing,
1355 only fixing of non-spec-conformant things like
1362 tag = swf->firstTag;
1365 if(tag->id == ST_DEFINESPRITE) {
1367 /* ??? all sprites are supposed to be unfolded */
1368 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1378 /* move non-sprite tags out of sprite */
1379 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1380 /* remove tag from current position */
1381 tag->prev->next = tag->next;
1383 tag->next->prev = tag->prev;
1385 /* insert before tag level0 */
1387 tag->prev = level0->prev;
1390 tag->prev->next = tag;
1392 swf->firstTag = tag;
1396 if(tag->id == ST_END) {
1407 int swf_ReadSWF2(reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1409 if (!swf) return -1;
1410 memset(swf,0x00,sizeof(SWF));
1412 { char b[32]; // read Header
1418 if ((len = reader->read(reader ,b,8))<8) return -1;
1420 if (b[0]!='F' && b[0]!='C') return -1;
1421 if (b[1]!='W') return -1;
1422 if (b[2]!='S') return -1;
1423 swf->fileVersion = b[3];
1424 swf->compressed = (b[0]=='C')?1:0;
1425 swf->fileSize = GET32(&b[4]);
1427 if(swf->compressed) {
1428 reader_init_zlibinflate(&zreader, reader);
1431 swf->compressed = 0; // derive from version number from now on
1433 reader_GetRect(reader, &swf->movieSize);
1434 reader->read(reader, &swf->frameRate, 2);
1435 swf->frameRate = SWAP16(swf->frameRate);
1436 reader->read(reader, &swf->frameCount, 2);
1437 swf->frameCount = SWAP16(swf->frameCount);
1439 /* read tags and connect to list */
1442 t = swf_ReadTag(reader,t);
1443 if(t && t->id == ST_FILEATTRIBUTES) {
1444 swf->fileAttributes = swf_GetU32(t);
1445 swf_ResetReadBits(t);
1448 swf->firstTag = t1.next;
1449 t1.next->prev = NULL;
1455 int swf_ReadSWF(int handle, SWF * swf)
1458 reader_init_filereader(&reader, handle);
1459 return swf_ReadSWF2(&reader, swf);
1462 int no_extra_tags = 0;
1464 int WriteExtraTags(SWF*swf, writer_t*writer)
1466 TAG*t = swf->firstTag;
1467 TAG* has_fileattributes=0;
1468 int has_scenedescription=0;
1469 int has_version_8_action=0;
1470 int has_version_9_action=0;
1473 if(t->id == ST_FILEATTRIBUTES)
1474 has_fileattributes = t;
1475 if(t->id == ST_SCENEDESCRIPTION)
1476 has_scenedescription = 1;
1477 if(t->id == ST_DOABC)
1478 has_version_9_action=1;
1479 /* FIXME: this doesn't yet find actionscript in buttons */
1480 if(t->id == ST_DOACTION || t->id == ST_DOINITACTION)
1481 has_version_8_action=1;
1482 if(t->id == ST_PLACEOBJECT2 && t->len && (t->data[0]&0x80))
1483 has_version_8_action=1;
1486 if(has_version_8_action && has_version_9_action) {
1487 fprintf(stderr, "Warning: File contains both flash 8 and flash 9 actionscript\n");
1490 if(swf->fileVersion >= 9) {
1491 if(!has_fileattributes) {
1492 U32 flags = swf->fileAttributes|0x08; // 16 = has symbolclass tag | 8 = actionscript3 | 1 = usenetwork
1493 if(has_version_8_action && !has_version_9_action)
1495 TAG*fileattrib = swf_InsertTag(0, ST_FILEATTRIBUTES);
1496 swf_SetU32(fileattrib, flags);
1498 if(swf_WriteTag2(writer, fileattrib)<0)
1501 len += swf_WriteTag(-1,fileattrib);
1503 swf_DeleteTag(0, fileattrib);
1505 if(swf_WriteTag2(writer, has_fileattributes)<0)
1508 if(!has_scenedescription) {
1509 TAG*scene = swf_InsertTag(0, ST_SCENEDESCRIPTION);
1510 swf_SetU16(scene, 1);
1511 swf_SetString(scene, (U8*)"Scene 1");
1512 swf_SetU8(scene, 0);
1514 if(swf_WriteTag2(writer, scene)<0)
1517 len += swf_WriteTag(-1,scene);
1519 swf_DeleteTag(0, scene);
1525 int swf_WriteSWF2(writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1533 writer_t*original_writer = writer;
1534 int writer_lastpos = 0;
1536 if (!swf) return -1;
1537 if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1539 if(original_writer) writer_lastpos = original_writer->pos;
1541 // Count Frames + File Size
1547 len += WriteExtraTags(swf, 0);
1549 len += swf_WriteTag(-1,t);
1550 if(t->id == ST_DEFINESPRITE && !swf_IsFolded(t)) inSprite++;
1551 else if(t->id == ST_END && inSprite) inSprite--;
1552 else if(t->id == ST_END && !inSprite) {
1553 if(t->prev && t->prev->id!=ST_SHOWFRAME)
1556 else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1564 memset(&t1,0x00,sizeof(TAG));
1568 { // measure header file size
1571 memset(&t2,0x00,sizeof(TAG));
1574 swf_SetRect(&t2, &swf->movieSize);
1575 swf_SetU16(&t2, swf->frameRate);
1576 swf_SetU16(&t2, swf->frameCount);
1577 l = swf_GetTagLen(&t2)+8;
1579 if(swf->compressed == 8) {
1584 if(len) {// don't touch headers without tags
1585 swf->fileSize = fileSize;
1586 swf->frameCount = frameCount;
1589 if(swf->compressed != 8) {
1590 /* compressed flag set to 8 means "skip first 8
1591 header bytes". This is necessary if the caller wants to
1592 create compressed SWFs himself .
1593 It also means that we don't initialize our own zlib
1594 writer, but assume the caller provided one.
1596 if(swf->compressed==1 || (swf->compressed==0 && swf->fileVersion>=6)) {
1598 writer->write(writer, id, 3);
1601 writer->write(writer, id, 3);
1604 writer->write(writer, &swf->fileVersion, 1);
1605 PUT32(b4, swf->fileSize);
1606 writer->write(writer, b4, 4);
1608 if(swf->compressed==1 || (swf->compressed==0 && swf->fileVersion>=6)) {
1609 writer_init_zlibdeflate(&zwriter, writer);
1614 swf_SetRect(&t1,&swf->movieSize);
1615 swf_SetU16(&t1,swf->frameRate);
1616 swf_SetU16(&t1,swf->frameCount);
1618 ret = writer->write(writer,b,swf_GetTagLen(&t1));
1619 if (ret!=swf_GetTagLen(&t1))
1622 fprintf(stderr, "ret:%d\n",ret);
1624 fprintf(stderr,"WriteSWF() failed: Header.\n");
1629 if(!no_extra_tags) {
1630 WriteExtraTags(swf, writer);
1635 if(no_extra_tags || t->id != ST_FILEATTRIBUTES) {
1636 if(swf_WriteTag2(writer, t)<0)
1641 if(swf->compressed==1 || (swf->compressed==0 && swf->fileVersion>=6) || swf->compressed==8) {
1642 if(swf->compressed != 8) {
1643 zwriter.finish(&zwriter);
1644 return original_writer->pos - writer_lastpos;
1646 return (int)fileSize;
1648 return (int)fileSize;
1653 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1659 writer_init_nullwriter(&writer);
1660 len = swf_WriteSWF2(&writer, swf);
1663 writer_init_filewriter(&writer, handle);
1664 len = swf_WriteSWF2(&writer, swf);
1665 writer.finish(&writer);
1669 int swf_WriteHeader2(writer_t*writer,SWF * swf)
1672 memcpy(&myswf,swf,sizeof(SWF));
1674 return swf_WriteSWF2(writer, &myswf);
1677 int swf_WriteHeader(int handle,SWF * swf)
1680 memcpy(&myswf,swf,sizeof(SWF));
1682 return swf_WriteSWF(handle, &myswf);
1685 int swf_WriteCGI(SWF * swf)
1689 len = swf_WriteSWF(-1,swf);
1691 if (len<0) return -1;
1693 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1694 "Accept-Ranges: bytes\n"
1695 "Content-Length: %lu\n"
1696 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1699 write(fileno(stdout),s,strlen(s));
1700 return swf_WriteSWF(fileno(stdout),swf);
1703 SWF* swf_CopySWF(SWF*swf)
1705 SWF*nswf = (SWF*)rfx_alloc(sizeof(SWF));
1707 memcpy(nswf, swf, sizeof(SWF));
1709 tag = swf->firstTag;
1712 ntag = swf_CopyTag(ntag, tag);
1714 nswf->firstTag = ntag;
1720 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1721 { TAG * t = swf->firstTag;
1724 { TAG * tnew = t->next;
1725 if (t->data) rfx_free(t->data);
1732 // include advanced functions
1734 //#include "modules/swfdump.c"
1735 //#include "modules/swfshape.c"
1736 //#include "modules/swftext.c"
1737 //#include "modules/swffont.c"
1738 //#include "modules/swfobject.c"
1739 //#include "modules/swfbutton.c"
1740 //#include "modules/swftools.c"
1741 //#include "modules/swfcgi.c"
1742 //#include "modules/swfbits.c"
1743 //#include "modules/swfaction.c"
1744 //#include "modules/swfabc.c"
1745 //#include "modules/swfsound.c"
1746 //#include "modules/swfdraw.c"
1747 //#include "modules/swfrender.c"
1748 //#include "modules/swffilter.c"