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"
53 #define MALLOC_SIZE 128
54 #define INSERT_RFX_TAG
56 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
58 // inline wrapper functions
60 TAG * swf_NextTag(TAG * t) { return t->next; }
61 TAG * swf_PrevTag(TAG * t) { return t->prev; }
62 U16 swf_GetTagID(TAG * t) { return t->id; }
63 U32 swf_GetTagLen(TAG * t) { return t->len; }
64 U8* swf_GetTagLenPtr(TAG * t) { return &(t->data[t->len]); }
65 U32 swf_GetTagPos(TAG * t) { return t->pos; }
67 void swf_SetTagPos(TAG * t,U32 pos)
68 { swf_ResetReadBits(t);
69 if (pos<=t->len) t->pos = pos;
72 fprintf(stderr,"SetTagPos(%d) out of bounds: TagID = %i\n",pos, t->id);
77 char* swf_GetString(TAG*t)
80 while(t->pos < t->len && swf_GetU8(t));
81 /* make sure we always have a trailing zero byte */
82 if(t->pos == t->len) {
83 if(t->len == t->memsize) {
84 swf_ResetWriteBits(t);
90 return (char*)&(t->data[pos]);
94 { swf_ResetReadBits(t);
97 { 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));
282 U32 swf_GetU30(TAG*tag)
288 U8 b = swf_GetU8(tag);
292 if(!(b&128) || shift>=32)
295 /*int nr2= swf_SetU30(0, s);
297 printf("Unsigned value %d stored in %d bytes, I'd store it in %d bytes\n", s, nr, nr2);
302 int swf_SetU30(TAG*tag, U32 u)
307 swf_SetU8(tag, (u&~0x7f?0x80:0) | (u&0x7F));
314 void swf_SetABCU32(TAG*tag, U32 u)
317 swf_SetU8(tag, (u&~0x7f?0x80:0) | (u&0x7F));
321 U32 swf_GetABCU32(TAG*tag)
323 return swf_GetU30(tag);
325 void swf_SetABCS32(TAG*tag, S32 v)
327 swf_SetABCU32(tag, v);
329 S32 swf_GetABCS32(TAG*tag)
331 return swf_GetABCU32(tag);
336 /*The AVM2 spec is just plain wrong, claiming that S32 values are sign
337 extended. They're not.
338 This wastes up to 4 bytes for every negative value. */
340 void swf_SetABCS32(TAG*tag, S32 s)
342 printf("write S32: %d\n", s);
344 U8 sign = s<0?0x40:0;
350 if(s==neg && vsign==sign) {
351 /* if the value we now write has the same sign as s
352 and all the remaining bits are equal to the sign of s
355 printf("put %02x\n", val);
358 swf_SetU8(tag, 0x80 | val);
359 printf("put %02x\n", 0x80|val);
363 int swf_GetS30(TAG*tag)
369 U8 b = swf_GetU8(tag);
373 if(!(b&128) || shift>=32) {
376 s|=0xffffffff<<shift;
381 /* It's not uncommon for other applications (Flex for all negative numbers, and
382 Flash for -1) to generate a lot more bytes than would be necessary.
383 int nr2= swf_SetS30(0, s);
385 printf("Signed value %d stored in %d bytes, I'd store it in %d bytes\n", s, nr, nr2);
391 int swf_SetU30String(TAG*tag, const char*str, int l)
394 len+=swf_SetU30(tag, l);
396 swf_SetBlock(tag, (void*)str, l);
399 double swf_GetD64(TAG*tag)
401 /* FIXME: this is not big-endian compatible */
402 double value = *(double*)&tag->data[tag->pos];
407 int swf_SetD64(TAG*tag, double v)
409 /* FIXME: this is not big-endian compatible */
410 swf_SetU32(tag, ((U32*)&v)[0]);
411 swf_SetU32(tag, ((U32*)&v)[1]);
414 int swf_GetU24(TAG*tag)
416 int b1 = swf_GetU8(tag);
417 int b2 = swf_GetU8(tag);
418 int b3 = swf_GetU8(tag);
419 return b3<<16|b2<<8|b1;
421 int swf_GetS24(TAG*tag)
423 int b1 = swf_GetU8(tag);
424 int b2 = swf_GetU8(tag);
425 int b3 = swf_GetU8(tag);
427 return -1-((b3<<16|b2<<8|b1)^0xffffff);
429 return b3<<16|b2<<8|b1;
432 int swf_SetU24(TAG*tag, U32 v)
436 fprintf(stderr, "Error: Overflow in swf_SetU24()\n");
438 swf_SetU8(tag, v>>8);
439 swf_SetU8(tag, v>>16);
443 int swf_SetS24(TAG*tag, U32 v)
447 return swf_SetU24(tag, v);
448 if((v&0xff000000)!=0xff000000) {
449 fprintf(stderr, "Error: Overflow in swf_SetS24()\n");
452 swf_SetU8(tag, v>>8);
453 swf_SetU8(tag, v>>16);
459 int swf_SetRGB(TAG * t,RGBA * col)
462 { swf_SetU8(t,col->r);
465 } else swf_SetBlock(t,NULL,3);
468 void swf_GetRGB(TAG * t, RGBA * col)
473 col->r = swf_GetU8(t);
474 col->g = swf_GetU8(t);
475 col->b = swf_GetU8(t);
479 int swf_SetRGBA(TAG * t,RGBA * col)
482 { swf_SetU8(t,col->r);
486 } else swf_SetBlock(t,NULL,4);
489 void swf_GetRGBA(TAG * t, RGBA * col)
494 col->r = swf_GetU8(t);
495 col->g = swf_GetU8(t);
496 col->b = swf_GetU8(t);
497 col->a = swf_GetU8(t);
500 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
504 memset(gradient, 0, sizeof(GRADIENT));
507 U8 num = swf_GetU8(tag) & 15;
510 gradient->rgba = (RGBA*)rfx_calloc(sizeof(RGBA)*gradient->num);
511 gradient->ratios = (U8*)rfx_calloc(sizeof(gradient->ratios[0])*gradient->num);
515 U8 ratio = swf_GetU8(tag);
518 swf_GetRGB(tag, &color);
520 swf_GetRGBA(tag, &color);
522 gradient->ratios[t] = ratio;
523 gradient->rgba[t] = color;
528 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
532 memset(gradient, 0, sizeof(GRADIENT));
535 swf_SetU8(tag, gradient->num);
536 for(t=0; t<8 && t<gradient->num; t++)
538 swf_SetU8(tag, gradient->ratios[t]);
540 swf_SetRGB(tag, &gradient->rgba[t]);
542 swf_SetRGBA(tag, &gradient->rgba[t]);
546 void swf_FreeGradient(GRADIENT* gradient)
549 rfx_free(gradient->ratios);
551 rfx_free(gradient->rgba);
552 memset(gradient, 0, sizeof(GRADIENT));
555 int swf_CountUBits(U32 v,int nbits)
558 if(v == 0x00000000) n = 0;
564 return (n>nbits)?n:nbits;
567 int swf_CountBits(U32 v,int nbits)
571 { if(v == 0xffffffff) n = 1;
579 { if(v == 0x00000000) n = 0;
586 return (n>nbits)?n:nbits;
589 int swf_GetRect(TAG * t,SRECT * r)
592 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
594 nbits = (int) swf_GetBits(t,5);
595 r->xmin = swf_GetSBits(t,nbits);
596 r->xmax = swf_GetSBits(t,nbits);
597 r->ymin = swf_GetSBits(t,nbits);
598 r->ymax = swf_GetSBits(t,nbits);
602 int reader_GetRect(reader_t*reader,SRECT * r)
606 nbits = (int) reader_GetBits(reader,5);
607 r->xmin = reader_GetSBits(reader,nbits);
608 r->xmax = reader_GetSBits(reader,nbits);
609 r->ymin = reader_GetSBits(reader,nbits);
610 r->ymax = reader_GetSBits(reader,nbits);
614 int swf_SetRect(TAG * t,SRECT * r)
617 nbits = swf_CountBits(r->xmin,0);
618 nbits = swf_CountBits(r->xmax,nbits);
619 nbits = swf_CountBits(r->ymin,nbits);
620 nbits = swf_CountBits(r->ymax,nbits);
622 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
626 swf_SetBits(t,nbits,5);
627 swf_SetBits(t,r->xmin,nbits);
628 swf_SetBits(t,r->xmax,nbits);
629 swf_SetBits(t,r->ymin,nbits);
630 swf_SetBits(t,r->ymax,nbits);
635 SRECT swf_ClipRect(SRECT border, SRECT r)
637 if(r.xmax > border.xmax) r.xmax = border.xmax;
638 if(r.ymax > border.ymax) r.ymax = border.ymax;
639 if(r.xmax < border.xmin) r.xmax = border.xmin;
640 if(r.ymax < border.ymin) r.ymax = border.ymin;
642 if(r.xmin > border.xmax) r.xmin = border.xmax;
643 if(r.ymin > border.ymax) r.ymin = border.ymax;
644 if(r.xmin < border.xmin) r.xmin = border.xmin;
645 if(r.ymin < border.ymin) r.ymin = border.ymin;
649 void swf_ExpandRect(SRECT*src, SPOINT add)
651 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
656 if((add.x|add.y) == 0) src->xmax++; //make sure the bbox is not NULL anymore
659 if(add.x < src->xmin)
661 if(add.x > src->xmax)
663 if(add.y < src->ymin)
665 if(add.y > src->ymax)
668 void swf_ExpandRect2(SRECT*src, SRECT*add)
670 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
672 if((src->xmin | src->ymin | src->xmax | src->ymax)==0)
674 if(add->xmin < src->xmin)
675 src->xmin = add->xmin;
676 if(add->ymin < src->ymin)
677 src->ymin = add->ymin;
678 if(add->xmax > src->xmax)
679 src->xmax = add->xmax;
680 if(add->ymax > src->ymax)
681 src->ymax = add->ymax;
683 void swf_ExpandRect3(SRECT*src, SPOINT center, int radius)
685 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
686 src->xmin = center.x-radius;
687 src->ymin = center.y-radius;
688 src->xmax = center.x+radius;
689 src->ymax = center.y+radius;
690 if((center.x|center.y|radius) == 0) src->xmax++; //make sure the bbox is not NULL anymore
693 if(center.x - radius < src->xmin)
694 src->xmin = center.x - radius;
695 if(center.x + radius > src->xmax)
696 src->xmax = center.x + radius;
697 if(center.y - radius < src->ymin)
698 src->ymin = center.y - radius;
699 if(center.y + radius > src->ymax)
700 src->ymax = center.y + radius;
702 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
705 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
706 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
709 SRECT swf_TurnRect(SRECT r, MATRIX* m)
712 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
715 p1.x = r.xmin;p1.y = r.ymin;
716 p2.x = r.xmax;p2.y = r.ymin;
717 p3.x = r.xmin;p3.y = r.ymax;
718 p4.x = r.xmax;p4.y = r.ymax;
719 pp1 = swf_TurnPoint(p1, m);
720 pp2 = swf_TurnPoint(p2, m);
721 pp3 = swf_TurnPoint(p3, m);
722 pp4 = swf_TurnPoint(p4, m);
723 g.xmin = g.xmax = pp1.x;
724 g.ymin = g.ymax = pp1.y;
725 swf_ExpandRect(&g, pp2);
726 swf_ExpandRect(&g, pp3);
727 swf_ExpandRect(&g, pp4);
732 int swf_GetMatrix(TAG * t,MATRIX * m)
739 { m->sx = m->sy = 0x10000;
745 swf_ResetReadBits(t);
747 if (swf_GetBits(t,1))
748 { nbits = swf_GetBits(t,5);
749 m->sx = swf_GetSBits(t,nbits);
750 m->sy = swf_GetSBits(t,nbits);
752 else m->sx = m->sy = 0x10000;
754 if (swf_GetBits(t,1))
755 { nbits = swf_GetBits(t,5);
756 m->r0 = swf_GetSBits(t,nbits);
757 m->r1 = swf_GetSBits(t,nbits);
759 else m->r0 = m->r1 = 0x0;
761 nbits = swf_GetBits(t,5);
762 m->tx = swf_GetSBits(t,nbits);
763 m->ty = swf_GetSBits(t,nbits);
768 int swf_SetMatrix(TAG * t,MATRIX * m)
774 ma.sx = ma.sy = 0x10000;
779 swf_ResetWriteBits(t);
781 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
783 { swf_SetBits(t,1,1);
784 nbits = swf_CountBits(m->sx,0);
785 nbits = swf_CountBits(m->sy,nbits);
787 /* TODO: happens on AMD64 systems for normal values? */
788 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
791 swf_SetBits(t,nbits,5);
792 swf_SetBits(t,m->sx,nbits);
793 swf_SetBits(t,m->sy,nbits);
796 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
798 { swf_SetBits(t,1,1);
799 nbits = swf_CountBits(m->r0,0);
800 nbits = swf_CountBits(m->r1,nbits);
802 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
805 swf_SetBits(t,nbits,5);
806 swf_SetBits(t,m->r0,nbits);
807 swf_SetBits(t,m->r1,nbits);
810 nbits = swf_CountBits(m->tx,0);
811 nbits = swf_CountBits(m->ty,nbits);
813 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
816 swf_SetBits(t,nbits,5);
817 swf_SetBits(t,m->tx,nbits);
818 swf_SetBits(t,m->ty,nbits);
823 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha)
831 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
832 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
836 swf_ResetReadBits(t);
837 hasadd = swf_GetBits(t,1);
838 hasmul = swf_GetBits(t,1);
839 nbits = swf_GetBits(t,4);
842 { cx->r0 = (S16)swf_GetSBits(t,nbits);
843 cx->g0 = (S16)swf_GetSBits(t,nbits);
844 cx->b0 = (S16)swf_GetSBits(t,nbits);
846 cx->a0 = (S16)swf_GetSBits(t,nbits);
850 { cx->r1 = (S16)swf_GetSBits(t,nbits);
851 cx->g1 = (S16)swf_GetSBits(t,nbits);
852 cx->b1 = (S16)swf_GetSBits(t,nbits);
854 cx->a1 = (S16)swf_GetSBits(t,nbits);
860 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
868 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
869 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
879 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
880 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
883 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
884 nbits = swf_CountBits((S32)cx->r0,nbits);
885 nbits = swf_CountBits((S32)cx->g0,nbits);
886 nbits = swf_CountBits((S32)cx->b0,nbits);
890 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
891 nbits = swf_CountBits((S32)cx->r1,nbits);
892 nbits = swf_CountBits((S32)cx->g1,nbits);
893 nbits = swf_CountBits((S32)cx->b1,nbits);
896 swf_ResetWriteBits(t);
897 swf_SetBits(t,hasadd?1:0,1);
898 swf_SetBits(t,hasmul?1:0,1);
899 swf_SetBits(t,nbits,4);
902 { swf_SetBits(t,cx->r0,nbits);
903 swf_SetBits(t,cx->g0,nbits);
904 swf_SetBits(t,cx->b0,nbits);
905 if (alpha) swf_SetBits(t,cx->a0,nbits);
909 { swf_SetBits(t,cx->r1,nbits);
910 swf_SetBits(t,cx->g1,nbits);
911 swf_SetBits(t,cx->b1,nbits);
912 if (alpha) swf_SetBits(t,cx->a1,nbits);
918 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
919 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
921 void swf_SetPassword(TAG * t, const char * password)
923 /* WARNING: crypt_md5 is not reentrant */
927 #if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) && defined(HAVE_TIME_H) && defined(HAVE_TIME)
929 salt[0] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
930 salt[1] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
934 fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
935 fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
939 md5string = crypt_md5(password, salt);
942 swf_SetString(t, (U8*)md5string);
945 void swf_SetString(TAG*t, const char* s)
950 swf_SetBlock(t,s,strlen(s)+1);
954 int swf_VerifyPassword(TAG * t, const char * password)
956 char*md5string1, *md5string2;
961 if(t->len >= 5 && t->pos==0 &&
966 printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
969 md5string1 = swf_GetString(t);
971 if(strncmp(md5string1, "$1$",3 )) {
972 fprintf(stderr, "rfxswf: no salt in pw string\n");
975 x = strchr(md5string1+3, '$');
977 fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
980 n = x-(md5string1+3);
981 salt = (char*)rfx_alloc(n+1);
982 memcpy(salt, md5string1+3, n);
985 md5string2 = crypt_md5(password, salt);
987 if(strcmp(md5string1, md5string2) != 0)
992 // Tag List Manipulating Functions
994 TAG * swf_InsertTag(TAG * after,U16 id)
997 t = (TAG *)rfx_calloc(sizeof(TAG));
1003 t->next = after->next;
1005 if (t->next) t->next->prev = t;
1010 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
1013 t = (TAG *)rfx_calloc(sizeof(TAG));
1019 t->prev = before->prev;
1021 if (t->prev) t->prev->next = t;
1023 if(swf && swf->firstTag == before) {
1029 void swf_ClearTag(TAG * t)
1031 if (t->data) rfx_free(t->data);
1040 void swf_ResetTag(TAG*tag, U16 id)
1042 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
1046 TAG* swf_CopyTag(TAG*tag, TAG*to_copy)
1048 tag = swf_InsertTag(tag, to_copy->id);
1049 swf_SetBlock(tag, to_copy->data, to_copy->len);
1053 TAG* swf_DeleteTag(SWF*swf, TAG * t)
1057 if (swf && swf->firstTag==t)
1058 swf->firstTag = t->next;
1059 if (t->prev) t->prev->next = t->next;
1060 if (t->next) t->next->prev = t->prev;
1062 if (t->data) rfx_free(t->data);
1067 TAG * swf_ReadTag(reader_t*reader, TAG * prev)
1073 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
1081 if (reader->read(reader, &len, 4) != 4) return NULL;
1085 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
1086 // Sprite handling fix: Flatten sprite tree
1088 t = (TAG *)rfx_calloc(sizeof(TAG));
1094 { t->data = (U8*)rfx_alloc(t->len);
1095 t->memsize = t->len;
1096 if (reader->read(reader, t->data, t->len) != t->len) {
1097 fprintf(stderr, "rfxswf: Warning: Short read (tagid %d). File truncated?\n", t->id);
1098 free(t->data);t->data=0;
1113 int swf_DefineSprite_GetRealSize(TAG * t);
1115 int swf_WriteTag2(writer_t*writer, TAG * t)
1116 // returns tag length in bytes (incl. Header), -1 = Error
1117 // writer = 0 -> no output
1124 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
1126 short_tag = len<0x3f&&
1127 (t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2&&t->id!=ST_SOUNDSTREAMBLOCK&&
1128 t->id!=ST_DEFINEBITSJPEG&&t->id!=ST_DEFINEBITSJPEG2&&t->id!=ST_DEFINEBITSJPEG3);
1133 int oldpos = writer->pos;
1137 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
1138 if (writer->write(writer,raw,2)!=2)
1141 fprintf(stderr,"WriteTag() failed: Short Header.\n");
1148 raw[0] = SWAP16((t->id<<6)|0x3f);
1149 if (writer->write(writer,raw,2)!=2)
1152 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
1158 if (writer->write(writer,&len,4)!=4)
1161 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
1168 { if (writer->write(writer,t->data,t->len)!=t->len)
1171 fprintf(stderr,"WriteTag() failed: Data.\n");
1177 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
1181 writer->flush(writer);
1182 printf("TAG %s costs %d bytes\n", swf_TagGetName(t), writer->pos-oldpos);
1186 return t->len+(short_tag?2:6);
1189 int swf_WriteTag(int handle, TAG * t)
1194 return swf_WriteTag2(0, t);
1195 writer_init_filewriter(&writer, handle);
1196 len = swf_WriteTag2(&writer, t);
1197 writer.finish(&writer);
1201 int swf_DefineSprite_GetRealSize(TAG * t)
1202 // Sprite Handling: Helper function to pack DefineSprite-Tag
1204 if(len>4) { // folded sprite
1208 { t = swf_NextTag(t);
1209 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
1211 } while (t&&(t->id!=ST_END));
1215 void swf_UnFoldSprite(TAG * t)
1220 U16 spriteid,spriteframes;
1222 if(t->id!=ST_DEFINESPRITE)
1224 if(t->len<=4) // not folded
1229 spriteid = swf_GetU16(t); //id
1230 spriteframes = swf_GetU16(t); //frames
1237 tmp = swf_GetU16(t);
1242 if(id == ST_DEFINESPRITE && len<=4)
1246 len = swf_GetU32(t);
1247 it = swf_InsertTag(next, id);
1252 { it->data = (U8*)rfx_alloc(it->len);
1253 it->memsize = it->len;
1254 swf_GetBlock(t, it->data, it->len);
1261 rfx_free(t->data); t->data = 0;
1262 t->memsize = t->len = t->pos = 0;
1264 swf_SetU16(t, spriteid);
1265 swf_SetU16(t, spriteframes);
1268 void swf_FoldSprite(TAG * t)
1273 if(t->id!=ST_DEFINESPRITE)
1276 fprintf(stderr, "Error: Sprite has no ID!");
1280 /* sprite is already folded */
1287 t->len = t->pos = t->memsize = 0;
1292 t = swf_NextTag(sprtag);
1297 if(t->id==ST_SHOWFRAME) frames++;
1298 if(t->id == ST_DEFINESPRITE && t->len<=4)
1303 } while(t && level);
1305 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1307 swf_SetU16(sprtag, id);
1308 swf_SetU16(sprtag, frames);
1310 t = swf_NextTag(sprtag);
1316 (t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2&&t->id!=ST_SOUNDSTREAMBLOCK&&
1317 t->id!=ST_DEFINEBITSJPEG&&t->id!=ST_DEFINEBITSJPEG2&&t->id!=ST_DEFINEBITSJPEG3)
1319 swf_SetU16(sprtag,t->len|(t->id<<6));
1321 swf_SetU16(sprtag,0x3f|(t->id<<6));
1322 swf_SetU32(sprtag,t->len);
1325 swf_SetBlock(sprtag,t->data, t->len);
1327 if(t->id == ST_DEFINESPRITE && t->len<=4)
1332 swf_DeleteTag(0, tmp);
1336 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1338 // sprtag->next = t;
1339 // t->prev = sprtag;
1342 int swf_IsFolded(TAG * t)
1344 return (t->id == ST_DEFINESPRITE && t->len>4);
1347 void swf_FoldAll(SWF*swf)
1349 TAG*tag = swf->firstTag;
1350 //swf_DumpSWF(stdout, swf);
1352 if(tag->id == ST_DEFINESPRITE) {
1353 swf_FoldSprite(tag);
1354 //swf_DumpSWF(stdout, swf);
1356 tag = swf_NextTag(tag);
1360 void swf_UnFoldAll(SWF*swf)
1362 TAG*tag = swf->firstTag;
1364 if(tag->id == ST_DEFINESPRITE)
1365 swf_UnFoldSprite(tag);
1370 void swf_OptimizeTagOrder(SWF*swf)
1377 /* at the moment, we don't actually do optimizing,
1378 only fixing of non-spec-conformant things like
1385 tag = swf->firstTag;
1388 if(tag->id == ST_DEFINESPRITE) {
1390 /* ??? all sprites are supposed to be unfolded */
1391 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1401 /* move non-sprite tags out of sprite */
1402 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1403 /* remove tag from current position */
1404 tag->prev->next = tag->next;
1406 tag->next->prev = tag->prev;
1408 /* insert before tag level0 */
1410 tag->prev = level0->prev;
1413 tag->prev->next = tag;
1415 swf->firstTag = tag;
1419 if(tag->id == ST_END) {
1430 int swf_ReadSWF2(reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1432 if (!swf) return -1;
1433 memset(swf,0x00,sizeof(SWF));
1435 { char b[32]; // read Header
1441 if ((len = reader->read(reader ,b,8))<8) return -1;
1443 if (b[0]!='F' && b[0]!='C') return -1;
1444 if (b[1]!='W') return -1;
1445 if (b[2]!='S') return -1;
1446 swf->fileVersion = b[3];
1447 swf->compressed = (b[0]=='C')?1:0;
1448 swf->fileSize = GET32(&b[4]);
1450 if(swf->compressed) {
1451 reader_init_zlibinflate(&zreader, reader);
1454 swf->compressed = 0; // derive from version number from now on
1456 reader_GetRect(reader, &swf->movieSize);
1457 reader->read(reader, &swf->frameRate, 2);
1458 swf->frameRate = SWAP16(swf->frameRate);
1459 reader->read(reader, &swf->frameCount, 2);
1460 swf->frameCount = SWAP16(swf->frameCount);
1462 /* read tags and connect to list */
1465 t = swf_ReadTag(reader,t);
1466 if(t && t->id == ST_FILEATTRIBUTES) {
1467 swf->fileAttributes = swf_GetU32(t);
1468 swf_ResetReadBits(t);
1471 swf->firstTag = t1.next;
1472 t1.next->prev = NULL;
1478 int swf_ReadSWF(int handle, SWF * swf)
1481 reader_init_filereader(&reader, handle);
1482 return swf_ReadSWF2(&reader, swf);
1485 void swf_ReadABCfile(char*filename, SWF*swf)
1487 memset(swf, 0, sizeof(SWF));
1489 swf->fileAttributes=1; //as3
1490 TAG*tag = swf->firstTag = swf_InsertTag(0, ST_RAWABC);
1491 memfile_t*file = memfile_open(filename);
1492 swf_SetBlock(tag, file->data, file->len);
1493 memfile_close(file);
1496 int no_extra_tags = 0;
1498 int WriteExtraTags(SWF*swf, writer_t*writer)
1500 TAG*t = swf->firstTag;
1501 TAG* has_fileattributes=0;
1502 int has_scenedescription=0;
1503 int has_version_8_action=0;
1504 int has_version_9_action=0;
1507 if(t->id == ST_FILEATTRIBUTES)
1508 has_fileattributes = t;
1509 if(t->id == ST_SCENEDESCRIPTION)
1510 has_scenedescription = 1;
1511 if(t->id == ST_DOABC)
1512 has_version_9_action=1;
1513 /* FIXME: this doesn't yet find actionscript in buttons */
1514 if(t->id == ST_DOACTION || t->id == ST_DOINITACTION)
1515 has_version_8_action=1;
1516 if(t->id == ST_PLACEOBJECT2 && t->len && (t->data[0]&0x80))
1517 has_version_8_action=1;
1520 if(has_version_8_action && has_version_9_action) {
1521 fprintf(stderr, "Warning: File contains both flash 8 and flash 9 actionscript\n");
1524 if(swf->fileVersion >= 9) {
1525 if(!has_fileattributes) {
1526 U32 flags = swf->fileAttributes|0x08; // 16 = has symbolclass tag | 8 = actionscript3 | 1 = usenetwork
1527 if(has_version_8_action && !has_version_9_action)
1529 TAG*fileattrib = swf_InsertTag(0, ST_FILEATTRIBUTES);
1530 swf_SetU32(fileattrib, flags);
1532 if(swf_WriteTag2(writer, fileattrib)<0)
1535 len += swf_WriteTag(-1,fileattrib);
1537 swf_DeleteTag(0, fileattrib);
1539 if(swf_WriteTag2(writer, has_fileattributes)<0)
1542 if(!has_scenedescription) {
1543 TAG*scene = swf_InsertTag(0, ST_SCENEDESCRIPTION);
1544 swf_SetU16(scene, 1);
1545 swf_SetString(scene, (U8*)"Scene 1");
1546 swf_SetU8(scene, 0);
1548 if(swf_WriteTag2(writer, scene)<0)
1551 len += swf_WriteTag(-1,scene);
1553 swf_DeleteTag(0, scene);
1559 int swf_WriteSWF2(writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1567 writer_t*original_writer = writer;
1568 int writer_lastpos = 0;
1570 if (!swf) return -1;
1571 if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1573 if(original_writer) writer_lastpos = original_writer->pos;
1575 // Count Frames + File Size
1581 len += WriteExtraTags(swf, 0);
1583 len += swf_WriteTag(-1,t);
1584 if(t->id == ST_DEFINESPRITE && !swf_IsFolded(t)) inSprite++;
1585 else if(t->id == ST_END && inSprite) inSprite--;
1586 else if(t->id == ST_END && !inSprite) {
1587 if(t->prev && t->prev->id!=ST_SHOWFRAME)
1590 else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1598 memset(&t1,0x00,sizeof(TAG));
1602 { // measure header file size
1605 memset(&t2,0x00,sizeof(TAG));
1608 swf_SetRect(&t2, &swf->movieSize);
1609 swf_SetU16(&t2, swf->frameRate);
1610 swf_SetU16(&t2, swf->frameCount);
1611 l = swf_GetTagLen(&t2)+8;
1613 if(swf->compressed == 8) {
1618 if(len) {// don't touch headers without tags
1619 swf->fileSize = fileSize;
1620 swf->frameCount = frameCount;
1623 if(swf->compressed != 8) {
1624 /* compressed flag set to 8 means "skip first 8
1625 header bytes". This is necessary if the caller wants to
1626 create compressed SWFs himself .
1627 It also means that we don't initialize our own zlib
1628 writer, but assume the caller provided one.
1630 if(swf->compressed==1 || (swf->compressed==0 && swf->fileVersion>=6)) {
1632 writer->write(writer, id, 3);
1635 writer->write(writer, id, 3);
1638 writer->write(writer, &swf->fileVersion, 1);
1639 PUT32(b4, swf->fileSize);
1640 writer->write(writer, b4, 4);
1642 if(swf->compressed==1 || (swf->compressed==0 && swf->fileVersion>=6)) {
1643 writer_init_zlibdeflate(&zwriter, writer);
1648 swf_SetRect(&t1,&swf->movieSize);
1649 swf_SetU16(&t1,swf->frameRate);
1650 swf_SetU16(&t1,swf->frameCount);
1652 ret = writer->write(writer,b,swf_GetTagLen(&t1));
1653 if (ret!=swf_GetTagLen(&t1))
1656 fprintf(stderr, "ret:%d\n",ret);
1658 fprintf(stderr,"WriteSWF() failed: Header.\n");
1663 if(!no_extra_tags) {
1664 WriteExtraTags(swf, writer);
1669 if(no_extra_tags || t->id != ST_FILEATTRIBUTES) {
1670 if(swf_WriteTag2(writer, t)<0)
1675 if(swf->compressed==1 || (swf->compressed==0 && swf->fileVersion>=6) || swf->compressed==8) {
1676 if(swf->compressed != 8) {
1677 zwriter.finish(&zwriter);
1678 return original_writer->pos - writer_lastpos;
1680 return (int)fileSize;
1682 return (int)fileSize;
1687 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1693 writer_init_nullwriter(&writer);
1694 len = swf_WriteSWF2(&writer, swf);
1697 writer_init_filewriter(&writer, handle);
1698 len = swf_WriteSWF2(&writer, swf);
1699 writer.finish(&writer);
1703 int swf_WriteHeader2(writer_t*writer,SWF * swf)
1706 memcpy(&myswf,swf,sizeof(SWF));
1708 return swf_WriteSWF2(writer, &myswf);
1711 int swf_WriteHeader(int handle,SWF * swf)
1714 memcpy(&myswf,swf,sizeof(SWF));
1716 return swf_WriteSWF(handle, &myswf);
1719 int swf_WriteCGI(SWF * swf)
1723 len = swf_WriteSWF(-1,swf);
1725 if (len<0) return -1;
1727 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1728 "Accept-Ranges: bytes\n"
1729 "Content-Length: %lu\n"
1730 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1733 write(fileno(stdout),s,strlen(s));
1734 return swf_WriteSWF(fileno(stdout),swf);
1737 SWF* swf_CopySWF(SWF*swf)
1739 SWF*nswf = (SWF*)rfx_alloc(sizeof(SWF));
1741 memcpy(nswf, swf, sizeof(SWF));
1743 tag = swf->firstTag;
1746 ntag = swf_CopyTag(ntag, tag);
1748 nswf->firstTag = ntag;
1754 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1755 { TAG * t = swf->firstTag;
1758 { TAG * tnew = t->next;
1759 if (t->data) rfx_free(t->data);
1766 // include advanced functions
1768 //#include "modules/swfdump.c"
1769 //#include "modules/swfshape.c"
1770 //#include "modules/swftext.c"
1771 //#include "modules/swffont.c"
1772 //#include "modules/swfobject.c"
1773 //#include "modules/swfbutton.c"
1774 //#include "modules/swftools.c"
1775 //#include "modules/swfcgi.c"
1776 //#include "modules/swfbits.c"
1777 //#include "modules/swfaction.c"
1778 //#include "modules/swfabc.c"
1779 //#include "modules/swfsound.c"
1780 //#include "modules/swfdraw.c"
1781 //#include "modules/swfrender.c"
1782 //#include "modules/swffilter.c"