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 #endif // HAVE_JPEGLIB
39 #ifndef RFXSWF_DISABLESOUND
41 #include "lame/lame.h"
54 #define MALLOC_SIZE 128
55 #define INSERT_RFX_TAG
57 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
59 // inline wrapper functions
61 TAG * swf_NextTag(TAG * t) { return t->next; }
62 TAG * swf_PrevTag(TAG * t) { return t->prev; }
63 U16 swf_GetTagID(TAG * t) { return t->id; }
64 U32 swf_GetTagLen(TAG * t) { return t->len; }
65 U8* swf_GetTagLenPtr(TAG * t) { return &(t->data[t->len]); }
66 U32 swf_GetTagPos(TAG * t) { return t->pos; }
68 // for future purpose: avoid high level lib functions to change tagpos/bitpos
70 #define swf_SaveTagPos(tag)
71 #define swf_RestoreTagPos(tag)
73 void swf_SetTagPos(TAG * t,U32 pos)
74 { swf_ResetReadBits(t);
75 if (pos<=t->len) t->pos = pos;
78 fprintf(stderr,"SetTagPos(%d) out of bounds: TagID = %i\n",pos, t->id);
83 char* swf_GetString(TAG*t)
86 while(t->pos < t->len && swf_GetU8(t));
87 /* make sure we always have a trailing zero byte */
88 if(t->pos == t->len) {
89 if(t->len == t->memsize) {
90 swf_ResetWriteBits(t);
96 return (char*)&(t->data[pos]);
100 { swf_ResetReadBits(t);
103 { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
107 return t->data[t->pos++];
110 U16 swf_GetU16(TAG * t)
112 swf_ResetReadBits(t);
114 if (t->pos>(t->len-2))
115 { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
119 res = t->data[t->pos] | (t->data[t->pos+1]<<8);
124 U32 swf_GetU32(TAG * t)
126 swf_ResetReadBits(t);
128 if (t->pos>(t->len-4))
129 { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
133 res = t->data[t->pos] | (t->data[t->pos+1]<<8) |
134 (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
139 int swf_GetBlock(TAG * t,U8 * b,int l)
140 // returns number of bytes written (<=l)
141 // b = NULL -> skip data
142 { swf_ResetReadBits(t);
143 if ((t->len-t->pos)<l) l=t->len-t->pos;
144 if (b && l) memcpy(b,&t->data[t->pos],l);
149 int swf_SetBlock(TAG * t,U8 * b,int l)
150 // Appends Block to the end of Tagdata, returns size
151 { U32 newlen = t->len + l;
152 swf_ResetWriteBits(t);
153 if (newlen>t->memsize)
154 { U32 newmem = MEMSIZE(newlen);
155 U8 * newdata = (U8*)(rfx_realloc(t->data,newmem));
159 if (b) memcpy(&t->data[t->len],b,l);
160 else memset(&t->data[t->len],0x00,l);
165 int swf_SetU8(TAG * t,U8 v)
166 { swf_ResetWriteBits(t);
167 if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
168 t->data[t->len++] = v;
172 int swf_SetU16(TAG * t,U16 v)
177 swf_ResetWriteBits(t);
178 if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
179 t->data[t->len++] = a[0];
180 t->data[t->len++] = a[1];
183 void swf_SetS16(TAG * t,int v)
185 if(v>32767 || v<-32768) {
186 fprintf(stderr, "Warning: S16 overflow: %d\n", v);
188 swf_SetU16(t, (S16)v);
191 int swf_SetU32(TAG * t,U32 v)
193 a[0] = v&0xff; // to ensure correct handling of non-intel byteorder
198 swf_ResetWriteBits(t);
199 if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
200 t->data[t->len++] = a[0];
201 t->data[t->len++] = a[1];
202 t->data[t->len++] = a[2];
203 t->data[t->len++] = a[3];
207 U32 swf_GetBits(TAG * t,int nbits)
209 if (!nbits) return 0;
210 if (!t->readBit) t->readBit = 0x80;
213 if (t->data[t->pos]&t->readBit) res|=1;
217 { if (nbits) t->readBit = 0x80;
220 { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
230 S32 swf_GetSBits(TAG * t,int nbits)
231 { U32 res = swf_GetBits(t,nbits);
232 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
236 U32 reader_GetBits(reader_t*reader, int nbits)
237 { return reader_readbits(reader, nbits);
239 S32 reader_GetSBits(reader_t*reader, int nbits)
240 { U32 res = reader_readbits(reader, nbits);
241 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
245 int swf_SetBits(TAG * t,U32 v,int nbits)
246 { U32 bm = 1<<(nbits-1);
250 { if (FAILED(swf_SetU8(t,0))) return -1;
253 if (v&bm) t->data[t->len-1] |= t->writeBit;
261 // Advanced Data Access Functions
263 double swf_GetFixed(TAG * t)
265 U16 low = swf_GetU16(t);
266 U16 high = swf_GetU16(t);
267 return high + low*(1/65536.0);
269 void swf_SetFixed(TAG * t, double f)
271 swf_SetU16(t, (U16)((f-(int)f)*65536));
272 swf_SetU16(t, (U16)f);
274 float swf_GetFixed8(TAG * t)
276 U8 low = swf_GetU8(t);
277 U8 high = swf_GetU8(t);
278 return high + low*(1/256.0);
280 void swf_SetFixed8(TAG * t, float f)
282 swf_SetU8(t, (U8)((f-(int)f)*256));
286 int swf_SetRGB(TAG * t,RGBA * col)
289 { swf_SetU8(t,col->r);
292 } else swf_SetBlock(t,NULL,3);
295 void swf_GetRGB(TAG * t, RGBA * col)
300 col->r = swf_GetU8(t);
301 col->g = swf_GetU8(t);
302 col->b = swf_GetU8(t);
306 int swf_SetRGBA(TAG * t,RGBA * col)
309 { swf_SetU8(t,col->r);
313 } else swf_SetBlock(t,NULL,4);
316 void swf_GetRGBA(TAG * t, RGBA * col)
321 col->r = swf_GetU8(t);
322 col->g = swf_GetU8(t);
323 col->b = swf_GetU8(t);
324 col->a = swf_GetU8(t);
327 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
332 memset(gradient, 0, sizeof(GRADIENT));
335 U8 num = swf_GetU8(tag);
338 gradient->rgba = rfx_calloc(sizeof(RGBA)*gradient->num);
339 gradient->ratios = rfx_calloc(sizeof(gradient->ratios[0])*gradient->num);
341 for(t=0;t<gradient->num;t++)
343 U8 ratio = swf_GetU8(tag);
346 swf_GetRGB(tag, &color);
348 swf_GetRGBA(tag, &color);
350 gradient->ratios[t] = ratio;
351 gradient->rgba[t] = color;
356 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
360 memset(gradient, 0, sizeof(GRADIENT));
363 swf_SetU8(tag, gradient->num);
364 for(t=0; t<8 && t<gradient->num; t++)
366 swf_SetU8(tag, gradient->ratios[t]);
368 swf_SetRGB(tag, &gradient->rgba[t]);
370 swf_SetRGBA(tag, &gradient->rgba[t]);
374 void swf_FreeGradient(GRADIENT* gradient)
377 rfx_free(gradient->ratios);
379 rfx_free(gradient->rgba);
380 memset(gradient, 0, sizeof(GRADIENT));
383 int swf_CountUBits(U32 v,int nbits)
386 if(v == 0x00000000) n = 0;
392 return (n>nbits)?n:nbits;
395 int swf_CountBits(U32 v,int nbits)
399 { if(v == 0xffffffff) n = 1;
407 { if(v == 0x00000000) n = 0;
414 return (n>nbits)?n:nbits;
417 int swf_GetRect(TAG * t,SRECT * r)
420 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
422 nbits = (int) swf_GetBits(t,5);
423 r->xmin = swf_GetSBits(t,nbits);
424 r->xmax = swf_GetSBits(t,nbits);
425 r->ymin = swf_GetSBits(t,nbits);
426 r->ymax = swf_GetSBits(t,nbits);
430 int reader_GetRect(reader_t*reader,SRECT * r)
434 nbits = (int) reader_GetBits(reader,5);
435 r->xmin = reader_GetSBits(reader,nbits);
436 r->xmax = reader_GetSBits(reader,nbits);
437 r->ymin = reader_GetSBits(reader,nbits);
438 r->ymax = reader_GetSBits(reader,nbits);
442 int swf_SetRect(TAG * t,SRECT * r)
445 nbits = swf_CountBits(r->xmin,0);
446 nbits = swf_CountBits(r->xmax,nbits);
447 nbits = swf_CountBits(r->ymin,nbits);
448 nbits = swf_CountBits(r->ymax,nbits);
450 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
454 swf_SetBits(t,nbits,5);
455 swf_SetBits(t,r->xmin,nbits);
456 swf_SetBits(t,r->xmax,nbits);
457 swf_SetBits(t,r->ymin,nbits);
458 swf_SetBits(t,r->ymax,nbits);
463 void swf_ExpandRect(SRECT*src, SPOINT add)
465 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
470 if((add.x|add.y) == 0) src->xmax++; //make sure the bbox is not NULL anymore
473 if(add.x < src->xmin)
475 if(add.x > src->xmax)
477 if(add.y < src->ymin)
479 if(add.y > src->ymax)
482 void swf_ExpandRect2(SRECT*src, SRECT*add)
484 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
486 if((src->xmin | src->ymin | src->xmax | src->ymax)==0)
488 if(add->xmin < src->xmin)
489 src->xmin = add->xmin;
490 if(add->ymin < src->ymin)
491 src->ymin = add->ymin;
492 if(add->xmax > src->xmax)
493 src->xmax = add->xmax;
494 if(add->ymax > src->ymax)
495 src->ymax = add->ymax;
497 void swf_ExpandRect3(SRECT*src, SPOINT center, int radius)
499 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
500 src->xmin = center.x-radius;
501 src->ymin = center.y-radius;
502 src->xmax = center.x+radius;
503 src->ymax = center.y+radius;
504 if((center.x|center.y|radius) == 0) src->xmax++; //make sure the bbox is not NULL anymore
507 if(center.x - radius < src->xmin)
508 src->xmin = center.x - radius;
509 if(center.x + radius > src->xmax)
510 src->xmax = center.x + radius;
511 if(center.y - radius < src->ymin)
512 src->ymin = center.y - radius;
513 if(center.y + radius > src->ymax)
514 src->ymax = center.y + radius;
516 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
519 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
520 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
523 SRECT swf_TurnRect(SRECT r, MATRIX* m)
526 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
529 p1.x = r.xmin;p1.y = r.ymin;
530 p2.x = r.xmax;p2.y = r.ymin;
531 p3.x = r.xmin;p3.y = r.ymax;
532 p4.x = r.xmax;p4.y = r.ymax;
533 pp1 = swf_TurnPoint(p1, m);
534 pp2 = swf_TurnPoint(p2, m);
535 pp3 = swf_TurnPoint(p3, m);
536 pp4 = swf_TurnPoint(p4, m);
537 g.xmin = g.xmax = pp1.x;
538 g.ymin = g.ymax = pp1.y;
539 swf_ExpandRect(&g, pp2);
540 swf_ExpandRect(&g, pp3);
541 swf_ExpandRect(&g, pp4);
546 int swf_GetMatrix(TAG * t,MATRIX * m)
553 { m->sx = m->sy = 0x10000;
559 swf_ResetReadBits(t);
561 if (swf_GetBits(t,1))
562 { nbits = swf_GetBits(t,5);
563 m->sx = swf_GetSBits(t,nbits);
564 m->sy = swf_GetSBits(t,nbits);
566 else m->sx = m->sy = 0x10000;
568 if (swf_GetBits(t,1))
569 { nbits = swf_GetBits(t,5);
570 m->r0 = swf_GetSBits(t,nbits);
571 m->r1 = swf_GetSBits(t,nbits);
573 else m->r0 = m->r1 = 0x0;
575 nbits = swf_GetBits(t,5);
576 m->tx = swf_GetSBits(t,nbits);
577 m->ty = swf_GetSBits(t,nbits);
582 int swf_SetMatrix(TAG * t,MATRIX * m)
588 ma.sx = ma.sy = 0x10000;
593 swf_ResetWriteBits(t);
595 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
597 { swf_SetBits(t,1,1);
598 nbits = swf_CountBits(m->sx,0);
599 nbits = swf_CountBits(m->sy,nbits);
601 /* TODO: happens on AMD64 systems for normal values? */
602 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
605 swf_SetBits(t,nbits,5);
606 swf_SetBits(t,m->sx,nbits);
607 swf_SetBits(t,m->sy,nbits);
610 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
612 { swf_SetBits(t,1,1);
613 nbits = swf_CountBits(m->r0,0);
614 nbits = swf_CountBits(m->r1,nbits);
616 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
619 swf_SetBits(t,nbits,5);
620 swf_SetBits(t,m->r0,nbits);
621 swf_SetBits(t,m->r1,nbits);
624 nbits = swf_CountBits(m->tx,0);
625 nbits = swf_CountBits(m->ty,nbits);
627 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
630 swf_SetBits(t,nbits,5);
631 swf_SetBits(t,m->tx,nbits);
632 swf_SetBits(t,m->ty,nbits);
637 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha)
645 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
646 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
650 swf_ResetReadBits(t);
651 hasadd = swf_GetBits(t,1);
652 hasmul = swf_GetBits(t,1);
653 nbits = swf_GetBits(t,4);
656 { cx->r0 = (S16)swf_GetSBits(t,nbits);
657 cx->g0 = (S16)swf_GetSBits(t,nbits);
658 cx->b0 = (S16)swf_GetSBits(t,nbits);
660 cx->a0 = (S16)swf_GetSBits(t,nbits);
664 { cx->r1 = (S16)swf_GetSBits(t,nbits);
665 cx->g1 = (S16)swf_GetSBits(t,nbits);
666 cx->b1 = (S16)swf_GetSBits(t,nbits);
668 cx->a1 = (S16)swf_GetSBits(t,nbits);
674 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
682 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
683 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
693 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
694 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
697 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
698 nbits = swf_CountBits((S32)cx->r0,nbits);
699 nbits = swf_CountBits((S32)cx->g0,nbits);
700 nbits = swf_CountBits((S32)cx->b0,nbits);
704 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
705 nbits = swf_CountBits((S32)cx->r1,nbits);
706 nbits = swf_CountBits((S32)cx->g1,nbits);
707 nbits = swf_CountBits((S32)cx->b1,nbits);
710 swf_ResetWriteBits(t);
711 swf_SetBits(t,hasadd?1:0,1);
712 swf_SetBits(t,hasmul?1:0,1);
713 swf_SetBits(t,nbits,4);
716 { swf_SetBits(t,cx->r0,nbits);
717 swf_SetBits(t,cx->g0,nbits);
718 swf_SetBits(t,cx->b0,nbits);
719 if (alpha) swf_SetBits(t,cx->a0,nbits);
723 { swf_SetBits(t,cx->r1,nbits);
724 swf_SetBits(t,cx->g1,nbits);
725 swf_SetBits(t,cx->b1,nbits);
726 if (alpha) swf_SetBits(t,cx->a1,nbits);
732 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
733 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
735 void swf_SetPassword(TAG * t, const char * password)
737 /* WARNING: crypt_md5 is not reentrant */
741 #if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) && defined(HAVE_TIME_H) && defined(HAVE_TIME)
743 salt[0] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
744 salt[1] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
748 fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
749 fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
753 md5string = crypt_md5(password, salt);
756 swf_SetString(t, md5string);
759 int swf_VerifyPassword(TAG * t, const char * password)
761 char*md5string1, *md5string2;
766 if(t->len >= 5 && t->pos==0 &&
771 printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
774 md5string1 = swf_GetString(t);
776 if(strncmp(md5string1, "$1$",3 )) {
777 fprintf(stderr, "rfxswf: no salt in pw string\n");
780 x = strchr(md5string1+3, '$');
782 fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
785 n = x-(md5string1+3);
786 salt = (char*)rfx_alloc(n+1);
787 memcpy(salt, md5string1+3, n);
790 md5string2 = crypt_md5(password, salt);
792 if(strcmp(md5string1, md5string2) != 0)
797 // Tag List Manipulating Functions
799 TAG * swf_InsertTag(TAG * after,U16 id)
802 t = (TAG *)rfx_calloc(sizeof(TAG));
808 t->next = after->next;
810 if (t->next) t->next->prev = t;
815 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
818 t = (TAG *)rfx_calloc(sizeof(TAG));
824 t->prev = before->prev;
826 if (t->prev) t->prev->next = t;
828 if(swf && swf->firstTag == before) {
834 void swf_ClearTag(TAG * t)
836 if (t->data) rfx_free(t->data);
845 void swf_ResetTag(TAG*tag, U16 id)
847 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
851 TAG* swf_CopyTag(TAG*tag, TAG*to_copy)
853 tag = swf_InsertTag(tag, to_copy->id);
854 swf_SetBlock(tag, to_copy->data, to_copy->len);
858 int swf_DeleteTag(TAG * t)
861 if (t->prev) t->prev->next = t->next;
862 if (t->next) t->next->prev = t->prev;
864 if (t->data) rfx_free(t->data);
869 TAG * swf_ReadTag(reader_t*reader, TAG * prev)
875 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
883 if (reader->read(reader, &len, 4) != 4) return NULL;
887 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
888 // Sprite handling fix: Flatten sprite tree
890 t = (TAG *)rfx_calloc(sizeof(TAG));
896 { t->data = (U8*)rfx_alloc(t->len);
898 if (reader->read(reader, t->data, t->len) != t->len) {
899 fprintf(stderr, "rfxswf: Warning: Short read (tagid %d). File truncated?\n", t->id);
900 free(t->data);t->data=0;
915 int swf_DefineSprite_GetRealSize(TAG * t);
917 int swf_WriteTag2(writer_t*writer, TAG * t)
918 // returns tag length in bytes (incl. Header), -1 = Error
919 // writer = 0 -> no output
926 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
928 short_tag = len<0x3f&&
929 (t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2&&t->id!=ST_SOUNDSTREAMBLOCK&&
930 t->id!=ST_DEFINEBITSJPEG&&t->id!=ST_DEFINEBITSJPEG2&&t->id!=ST_DEFINEBITSJPEG3);
934 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
935 if (writer->write(writer,raw,2)!=2)
938 fprintf(stderr,"WriteTag() failed: Short Header.\n");
945 raw[0] = SWAP16((t->id<<6)|0x3f);
946 if (writer->write(writer,raw,2)!=2)
949 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
955 if (writer->write(writer,&len,4)!=4)
958 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
965 { if (writer->write(writer,t->data,t->len)!=t->len)
968 fprintf(stderr,"WriteTag() failed: Data.\n");
974 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
978 return t->len+(short_tag?2:6);
981 int swf_WriteTag(int handle, TAG * t)
986 return swf_WriteTag2(0, t);
987 writer_init_filewriter(&writer, handle);
988 len = swf_WriteTag2(&writer, t);
989 writer.finish(&writer);
993 int swf_DefineSprite_GetRealSize(TAG * t)
994 // Sprite Handling: Helper function to pack DefineSprite-Tag
996 if(len>4) { // folded sprite
1000 { t = swf_NextTag(t);
1001 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
1003 } while (t&&(t->id!=ST_END));
1007 void swf_UnFoldSprite(TAG * t)
1012 U16 spriteid,spriteframes;
1014 if(t->id!=ST_DEFINESPRITE)
1016 if(t->len<=4) // not folded
1021 spriteid = swf_GetU16(t); //id
1022 spriteframes = swf_GetU16(t); //frames
1029 tmp = swf_GetU16(t);
1034 if(id == ST_DEFINESPRITE && len<=4)
1038 len = swf_GetU32(t);
1039 it = swf_InsertTag(next, id);
1044 { it->data = (U8*)rfx_alloc(it->len);
1045 it->memsize = it->len;
1046 swf_GetBlock(t, it->data, it->len);
1053 rfx_free(t->data); t->data = 0;
1054 t->memsize = t->len = t->pos = 0;
1056 swf_SetU16(t, spriteid);
1057 swf_SetU16(t, spriteframes);
1060 void swf_FoldSprite(TAG * t)
1063 U16 id,frames,tmpid;
1065 if(t->id!=ST_DEFINESPRITE)
1068 fprintf(stderr, "Error: Sprite has no ID!");
1072 /* sprite is already folded */
1079 t->len = t->pos = t->memsize = 0;
1084 t = swf_NextTag(sprtag);
1089 if(t->id==ST_SHOWFRAME) frames++;
1090 if(t->id == ST_DEFINESPRITE && t->len<=4)
1095 } while(t && level);
1097 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1099 swf_SetU16(sprtag, id);
1100 swf_SetU16(sprtag, frames);
1102 t = swf_NextTag(sprtag);
1108 (t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2&&t->id!=ST_SOUNDSTREAMBLOCK&&
1109 t->id!=ST_DEFINEBITSJPEG&&t->id!=ST_DEFINEBITSJPEG2&&t->id!=ST_DEFINEBITSJPEG3)
1111 swf_SetU16(sprtag,t->len|(t->id<<6));
1113 swf_SetU16(sprtag,0x3f|(t->id<<6));
1114 swf_SetU32(sprtag,t->len);
1117 swf_SetBlock(sprtag,t->data, t->len);
1119 if(t->id == ST_DEFINESPRITE && t->len<=4)
1128 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1130 // sprtag->next = t;
1131 // t->prev = sprtag;
1134 int swf_IsFolded(TAG * t)
1136 return (t->id == ST_DEFINESPRITE && t->len>4);
1139 void swf_FoldAll(SWF*swf)
1141 TAG*tag = swf->firstTag;
1142 //swf_DumpSWF(stdout, swf);
1144 if(tag->id == ST_DEFINESPRITE) {
1145 swf_FoldSprite(tag);
1146 //swf_DumpSWF(stdout, swf);
1148 tag = swf_NextTag(tag);
1152 void swf_UnFoldAll(SWF*swf)
1154 TAG*tag = swf->firstTag;
1156 if(tag->id == ST_DEFINESPRITE)
1157 swf_UnFoldSprite(tag);
1162 void swf_OptimizeTagOrder(SWF*swf)
1169 /* at the moment, we don't actually do optimizing,
1170 only fixing of non-spec-conformant things like
1177 tag = swf->firstTag;
1180 if(tag->id == ST_DEFINESPRITE) {
1182 /* ??? all sprites are supposed to be unfolded */
1183 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1193 /* move non-sprite tags out of sprite */
1194 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1195 /* remove tag from current position */
1196 tag->prev->next = tag->next;
1198 tag->next->prev = tag->prev;
1200 /* insert before tag level0 */
1202 tag->prev = level0->prev;
1205 tag->prev->next = tag;
1207 swf->firstTag = tag;
1211 if(tag->id == ST_END) {
1222 int swf_ReadSWF2(reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1224 if (!swf) return -1;
1225 memset(swf,0x00,sizeof(SWF));
1227 { char b[32]; // read Header
1233 if ((len = reader->read(reader ,b,8))<8) return -1;
1235 if (b[0]!='F' && b[0]!='C') return -1;
1236 if (b[1]!='W') return -1;
1237 if (b[2]!='S') return -1;
1238 swf->fileVersion = b[3];
1239 swf->compressed = (b[0]=='C')?1:0;
1240 swf->fileSize = GET32(&b[4]);
1242 if(swf->compressed) {
1243 reader_init_zlibinflate(&zreader, reader);
1247 reader_GetRect(reader, &swf->movieSize);
1248 reader->read(reader, &swf->frameRate, 2);
1249 swf->frameRate = SWAP16(swf->frameRate);
1250 reader->read(reader, &swf->frameCount, 2);
1251 swf->frameCount = SWAP16(swf->frameCount);
1253 /* read tags and connect to list */
1255 while (t) t = swf_ReadTag(reader,t);
1256 swf->firstTag = t1.next;
1257 t1.next->prev = NULL;
1263 int swf_ReadSWF(int handle, SWF * swf)
1266 reader_init_filereader(&reader, handle);
1267 return swf_ReadSWF2(&reader, swf);
1270 int swf_WriteSWF2(writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1277 int writer_lastpos = 0;
1280 if (!swf) return -1;
1281 if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1283 if(writer) writer_lastpos = writer->pos;
1285 if(swf->fileVersion >= 8) {
1286 if ((swf->firstTag && swf->firstTag->id != ST_FILEATTRIBUTES) &&
1287 (!swf->firstTag->next || swf->firstTag->next->id != ST_FILEATTRIBUTES))
1289 U32 flags = 0; // | 128 = usenetwork, | 8 = hasmetadata
1290 swf_SetU32(swf_InsertTagBefore(swf, swf->firstTag,ST_FILEATTRIBUTES),flags);
1294 // Insert REFLEX Tag
1296 #ifdef INSERT_RFX_TAG
1298 if ((swf->firstTag && swf->firstTag->id != ST_REFLEX) &&
1299 (!swf->firstTag->next || swf->firstTag->next->id != ST_REFLEX))
1301 swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),"rfx",3);
1304 #endif // INSERT_RFX_TAG
1306 // Count Frames + File Size
1313 len += swf_WriteTag(-1,t);
1314 if(t->id == ST_DEFINESPRITE && !swf_IsFolded(t)) inSprite++;
1315 else if(t->id == ST_END && inSprite) inSprite--;
1316 else if(t->id == ST_END && !inSprite) {
1317 if(t->prev && t->prev->id!=ST_SHOWFRAME)
1320 else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1328 memset(&t1,0x00,sizeof(TAG));
1332 { // measure header file size
1335 memset(&t2,0x00,sizeof(TAG));
1338 swf_SetRect(&t2, &swf->movieSize);
1339 swf_SetU16(&t2, swf->frameRate);
1340 swf_SetU16(&t2, swf->frameCount);
1341 l = swf_GetTagLen(&t2)+8;
1343 if(swf->compressed == 8) {
1348 if(len) {// don't touch headers without tags
1349 swf->fileSize = fileSize;
1350 swf->frameCount = frameCount;
1353 if(swf->compressed != 8) {
1354 /* compressed flag set to 8 means "skip first 8
1355 header bytes". This is necessary if the caller wants to
1356 create compressed SWFs himself .
1357 It also means that we don't initialize our own zlib
1358 writer, but assume the caller provided one.
1360 if(swf->compressed) {
1362 writer->write(writer, id, 3);
1366 writer->write(writer, id, 3);
1369 writer->write(writer, &swf->fileVersion, 1);
1370 PUT32(b4, swf->fileSize);
1371 writer->write(writer, b4, 4);
1373 if(swf->compressed) {
1374 writer_init_zlibdeflate(&zwriter, writer);
1379 swf_SetRect(&t1,&swf->movieSize);
1380 swf_SetU16(&t1,swf->frameRate);
1381 swf_SetU16(&t1,swf->frameCount);
1383 ret = writer->write(writer,b,swf_GetTagLen(&t1));
1384 if (ret!=swf_GetTagLen(&t1))
1387 fprintf(stderr, "ret:%d\n",ret);
1389 fprintf(stderr,"WriteSWF() failed: Header.\n");
1396 { if (swf_WriteTag2(writer, t)<0) return -1;
1399 if(swf->compressed) {
1400 if(swf->compressed != 8) {
1401 zwriter.finish(&zwriter);
1402 return writer->pos - writer_lastpos;
1404 return (int)fileSize;
1406 return (int)fileSize;
1411 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1415 swf->compressed = 0;
1418 writer_init_nullwriter(&writer);
1419 len = swf_WriteSWF2(&writer, swf);
1422 writer_init_filewriter(&writer, handle);
1423 len = swf_WriteSWF2(&writer, swf);
1424 writer.finish(&writer);
1428 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1432 swf->compressed = 1;
1435 writer_init_nullwriter(&writer);
1436 len = swf_WriteSWF2(&writer, swf);
1438 writer_init_filewriter(&writer, handle);
1439 len = swf_WriteSWF2(&writer, swf);
1440 writer.finish(&writer);
1444 int swf_WriteHeader2(writer_t*writer,SWF * swf)
1447 memcpy(&myswf,swf,sizeof(SWF));
1449 return swf_WriteSWF2(writer, &myswf);
1452 int swf_WriteHeader(int handle,SWF * swf)
1455 memcpy(&myswf,swf,sizeof(SWF));
1457 return swf_WriteSWF(handle, &myswf);
1460 int swf_WriteCGI(SWF * swf)
1464 len = swf_WriteSWF(-1,swf);
1466 if (len<0) return -1;
1468 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1469 "Accept-Ranges: bytes\n"
1470 "Content-Length: %lu\n"
1471 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1474 write(fileno(stdout),s,strlen(s));
1475 return swf_WriteSWF(fileno(stdout),swf);
1478 SWF* swf_CopySWF(SWF*swf)
1480 SWF*nswf = rfx_alloc(sizeof(SWF));
1482 memcpy(nswf, swf, sizeof(SWF));
1484 tag = swf->firstTag;
1487 ntag = swf_CopyTag(ntag, tag);
1489 nswf->firstTag = ntag;
1495 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1496 { TAG * t = swf->firstTag;
1499 { TAG * tnew = t->next;
1500 if (t->data) rfx_free(t->data);
1507 // include advanced functions
1509 #include "modules/swfdump.c"
1510 #include "modules/swfshape.c"
1511 #include "modules/swftext.c"
1512 #include "modules/swffont.c"
1513 #include "modules/swfobject.c"
1514 #include "modules/swfbutton.c"
1515 #include "modules/swftools.c"
1516 #include "modules/swfcgi.c"
1517 #include "modules/swfbits.c"
1518 #include "modules/swfaction.c"
1519 #include "modules/swfsound.c"
1520 #include "modules/swfdraw.c"
1521 #include "modules/swfrender.c"
1522 #include "modules/swffilter.c"