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 U16 fr = (f-(int)f)*65536;
273 swf_SetU16(t, (U16)f - (f<0 && fr!=0));
275 float swf_GetFixed8(TAG * t)
277 U8 low = swf_GetU8(t);
278 U8 high = swf_GetU8(t);
279 return high + low*(1/256.0);
281 void swf_SetFixed8(TAG * t, float f)
283 U8 fr = (f-(int)f)*256;
285 swf_SetU8(t, (U8)f - (f<0 && fr!=0));
288 int swf_SetRGB(TAG * t,RGBA * col)
291 { swf_SetU8(t,col->r);
294 } else swf_SetBlock(t,NULL,3);
297 void swf_GetRGB(TAG * t, RGBA * col)
302 col->r = swf_GetU8(t);
303 col->g = swf_GetU8(t);
304 col->b = swf_GetU8(t);
308 int swf_SetRGBA(TAG * t,RGBA * col)
311 { swf_SetU8(t,col->r);
315 } else swf_SetBlock(t,NULL,4);
318 void swf_GetRGBA(TAG * t, RGBA * col)
323 col->r = swf_GetU8(t);
324 col->g = swf_GetU8(t);
325 col->b = swf_GetU8(t);
326 col->a = swf_GetU8(t);
329 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
334 memset(gradient, 0, sizeof(GRADIENT));
337 U8 num = swf_GetU8(tag) & 15;
340 gradient->rgba = rfx_calloc(sizeof(RGBA)*gradient->num);
341 gradient->ratios = rfx_calloc(sizeof(gradient->ratios[0])*gradient->num);
345 U8 ratio = swf_GetU8(tag);
348 swf_GetRGB(tag, &color);
350 swf_GetRGBA(tag, &color);
352 gradient->ratios[t] = ratio;
353 gradient->rgba[t] = color;
358 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
362 memset(gradient, 0, sizeof(GRADIENT));
365 swf_SetU8(tag, gradient->num);
366 for(t=0; t<8 && t<gradient->num; t++)
368 swf_SetU8(tag, gradient->ratios[t]);
370 swf_SetRGB(tag, &gradient->rgba[t]);
372 swf_SetRGBA(tag, &gradient->rgba[t]);
376 void swf_FreeGradient(GRADIENT* gradient)
379 rfx_free(gradient->ratios);
381 rfx_free(gradient->rgba);
382 memset(gradient, 0, sizeof(GRADIENT));
385 int swf_CountUBits(U32 v,int nbits)
388 if(v == 0x00000000) n = 0;
394 return (n>nbits)?n:nbits;
397 int swf_CountBits(U32 v,int nbits)
401 { if(v == 0xffffffff) n = 1;
409 { if(v == 0x00000000) n = 0;
416 return (n>nbits)?n:nbits;
419 int swf_GetRect(TAG * t,SRECT * r)
422 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
424 nbits = (int) swf_GetBits(t,5);
425 r->xmin = swf_GetSBits(t,nbits);
426 r->xmax = swf_GetSBits(t,nbits);
427 r->ymin = swf_GetSBits(t,nbits);
428 r->ymax = swf_GetSBits(t,nbits);
432 int reader_GetRect(reader_t*reader,SRECT * r)
436 nbits = (int) reader_GetBits(reader,5);
437 r->xmin = reader_GetSBits(reader,nbits);
438 r->xmax = reader_GetSBits(reader,nbits);
439 r->ymin = reader_GetSBits(reader,nbits);
440 r->ymax = reader_GetSBits(reader,nbits);
444 int swf_SetRect(TAG * t,SRECT * r)
447 nbits = swf_CountBits(r->xmin,0);
448 nbits = swf_CountBits(r->xmax,nbits);
449 nbits = swf_CountBits(r->ymin,nbits);
450 nbits = swf_CountBits(r->ymax,nbits);
452 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
456 swf_SetBits(t,nbits,5);
457 swf_SetBits(t,r->xmin,nbits);
458 swf_SetBits(t,r->xmax,nbits);
459 swf_SetBits(t,r->ymin,nbits);
460 swf_SetBits(t,r->ymax,nbits);
465 void swf_ExpandRect(SRECT*src, SPOINT add)
467 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
472 if((add.x|add.y) == 0) src->xmax++; //make sure the bbox is not NULL anymore
475 if(add.x < src->xmin)
477 if(add.x > src->xmax)
479 if(add.y < src->ymin)
481 if(add.y > src->ymax)
484 void swf_ExpandRect2(SRECT*src, SRECT*add)
486 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
488 if((src->xmin | src->ymin | src->xmax | src->ymax)==0)
490 if(add->xmin < src->xmin)
491 src->xmin = add->xmin;
492 if(add->ymin < src->ymin)
493 src->ymin = add->ymin;
494 if(add->xmax > src->xmax)
495 src->xmax = add->xmax;
496 if(add->ymax > src->ymax)
497 src->ymax = add->ymax;
499 void swf_ExpandRect3(SRECT*src, SPOINT center, int radius)
501 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
502 src->xmin = center.x-radius;
503 src->ymin = center.y-radius;
504 src->xmax = center.x+radius;
505 src->ymax = center.y+radius;
506 if((center.x|center.y|radius) == 0) src->xmax++; //make sure the bbox is not NULL anymore
509 if(center.x - radius < src->xmin)
510 src->xmin = center.x - radius;
511 if(center.x + radius > src->xmax)
512 src->xmax = center.x + radius;
513 if(center.y - radius < src->ymin)
514 src->ymin = center.y - radius;
515 if(center.y + radius > src->ymax)
516 src->ymax = center.y + radius;
518 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
521 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
522 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
525 SRECT swf_TurnRect(SRECT r, MATRIX* m)
528 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
531 p1.x = r.xmin;p1.y = r.ymin;
532 p2.x = r.xmax;p2.y = r.ymin;
533 p3.x = r.xmin;p3.y = r.ymax;
534 p4.x = r.xmax;p4.y = r.ymax;
535 pp1 = swf_TurnPoint(p1, m);
536 pp2 = swf_TurnPoint(p2, m);
537 pp3 = swf_TurnPoint(p3, m);
538 pp4 = swf_TurnPoint(p4, m);
539 g.xmin = g.xmax = pp1.x;
540 g.ymin = g.ymax = pp1.y;
541 swf_ExpandRect(&g, pp2);
542 swf_ExpandRect(&g, pp3);
543 swf_ExpandRect(&g, pp4);
548 int swf_GetMatrix(TAG * t,MATRIX * m)
555 { m->sx = m->sy = 0x10000;
561 swf_ResetReadBits(t);
563 if (swf_GetBits(t,1))
564 { nbits = swf_GetBits(t,5);
565 m->sx = swf_GetSBits(t,nbits);
566 m->sy = swf_GetSBits(t,nbits);
568 else m->sx = m->sy = 0x10000;
570 if (swf_GetBits(t,1))
571 { nbits = swf_GetBits(t,5);
572 m->r0 = swf_GetSBits(t,nbits);
573 m->r1 = swf_GetSBits(t,nbits);
575 else m->r0 = m->r1 = 0x0;
577 nbits = swf_GetBits(t,5);
578 m->tx = swf_GetSBits(t,nbits);
579 m->ty = swf_GetSBits(t,nbits);
584 int swf_SetMatrix(TAG * t,MATRIX * m)
590 ma.sx = ma.sy = 0x10000;
595 swf_ResetWriteBits(t);
597 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
599 { swf_SetBits(t,1,1);
600 nbits = swf_CountBits(m->sx,0);
601 nbits = swf_CountBits(m->sy,nbits);
603 /* TODO: happens on AMD64 systems for normal values? */
604 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
607 swf_SetBits(t,nbits,5);
608 swf_SetBits(t,m->sx,nbits);
609 swf_SetBits(t,m->sy,nbits);
612 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
614 { swf_SetBits(t,1,1);
615 nbits = swf_CountBits(m->r0,0);
616 nbits = swf_CountBits(m->r1,nbits);
618 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
621 swf_SetBits(t,nbits,5);
622 swf_SetBits(t,m->r0,nbits);
623 swf_SetBits(t,m->r1,nbits);
626 nbits = swf_CountBits(m->tx,0);
627 nbits = swf_CountBits(m->ty,nbits);
629 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
632 swf_SetBits(t,nbits,5);
633 swf_SetBits(t,m->tx,nbits);
634 swf_SetBits(t,m->ty,nbits);
639 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha)
647 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
648 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
652 swf_ResetReadBits(t);
653 hasadd = swf_GetBits(t,1);
654 hasmul = swf_GetBits(t,1);
655 nbits = swf_GetBits(t,4);
658 { cx->r0 = (S16)swf_GetSBits(t,nbits);
659 cx->g0 = (S16)swf_GetSBits(t,nbits);
660 cx->b0 = (S16)swf_GetSBits(t,nbits);
662 cx->a0 = (S16)swf_GetSBits(t,nbits);
666 { cx->r1 = (S16)swf_GetSBits(t,nbits);
667 cx->g1 = (S16)swf_GetSBits(t,nbits);
668 cx->b1 = (S16)swf_GetSBits(t,nbits);
670 cx->a1 = (S16)swf_GetSBits(t,nbits);
676 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
684 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
685 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
695 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
696 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
699 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
700 nbits = swf_CountBits((S32)cx->r0,nbits);
701 nbits = swf_CountBits((S32)cx->g0,nbits);
702 nbits = swf_CountBits((S32)cx->b0,nbits);
706 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
707 nbits = swf_CountBits((S32)cx->r1,nbits);
708 nbits = swf_CountBits((S32)cx->g1,nbits);
709 nbits = swf_CountBits((S32)cx->b1,nbits);
712 swf_ResetWriteBits(t);
713 swf_SetBits(t,hasadd?1:0,1);
714 swf_SetBits(t,hasmul?1:0,1);
715 swf_SetBits(t,nbits,4);
718 { swf_SetBits(t,cx->r0,nbits);
719 swf_SetBits(t,cx->g0,nbits);
720 swf_SetBits(t,cx->b0,nbits);
721 if (alpha) swf_SetBits(t,cx->a0,nbits);
725 { swf_SetBits(t,cx->r1,nbits);
726 swf_SetBits(t,cx->g1,nbits);
727 swf_SetBits(t,cx->b1,nbits);
728 if (alpha) swf_SetBits(t,cx->a1,nbits);
734 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
735 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
737 void swf_SetPassword(TAG * t, const char * password)
739 /* WARNING: crypt_md5 is not reentrant */
743 #if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) && defined(HAVE_TIME_H) && defined(HAVE_TIME)
745 salt[0] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
746 salt[1] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
750 fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
751 fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
755 md5string = crypt_md5(password, salt);
758 swf_SetString(t, md5string);
761 int swf_VerifyPassword(TAG * t, const char * password)
763 char*md5string1, *md5string2;
768 if(t->len >= 5 && t->pos==0 &&
773 printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
776 md5string1 = swf_GetString(t);
778 if(strncmp(md5string1, "$1$",3 )) {
779 fprintf(stderr, "rfxswf: no salt in pw string\n");
782 x = strchr(md5string1+3, '$');
784 fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
787 n = x-(md5string1+3);
788 salt = (char*)rfx_alloc(n+1);
789 memcpy(salt, md5string1+3, n);
792 md5string2 = crypt_md5(password, salt);
794 if(strcmp(md5string1, md5string2) != 0)
799 // Tag List Manipulating Functions
801 TAG * swf_InsertTag(TAG * after,U16 id)
804 t = (TAG *)rfx_calloc(sizeof(TAG));
810 t->next = after->next;
812 if (t->next) t->next->prev = t;
817 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
820 t = (TAG *)rfx_calloc(sizeof(TAG));
826 t->prev = before->prev;
828 if (t->prev) t->prev->next = t;
830 if(swf && swf->firstTag == before) {
836 void swf_ClearTag(TAG * t)
838 if (t->data) rfx_free(t->data);
847 void swf_ResetTag(TAG*tag, U16 id)
849 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
853 TAG* swf_CopyTag(TAG*tag, TAG*to_copy)
855 tag = swf_InsertTag(tag, to_copy->id);
856 swf_SetBlock(tag, to_copy->data, to_copy->len);
860 int swf_DeleteTag(TAG * t)
863 if (t->prev) t->prev->next = t->next;
864 if (t->next) t->next->prev = t->prev;
866 if (t->data) rfx_free(t->data);
871 TAG * swf_ReadTag(reader_t*reader, TAG * prev)
877 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
885 if (reader->read(reader, &len, 4) != 4) return NULL;
889 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
890 // Sprite handling fix: Flatten sprite tree
892 t = (TAG *)rfx_calloc(sizeof(TAG));
898 { t->data = (U8*)rfx_alloc(t->len);
900 if (reader->read(reader, t->data, t->len) != t->len) {
901 fprintf(stderr, "rfxswf: Warning: Short read (tagid %d). File truncated?\n", t->id);
902 free(t->data);t->data=0;
917 int swf_DefineSprite_GetRealSize(TAG * t);
919 int swf_WriteTag2(writer_t*writer, TAG * t)
920 // returns tag length in bytes (incl. Header), -1 = Error
921 // writer = 0 -> no output
928 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
930 short_tag = len<0x3f&&
931 (t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2&&t->id!=ST_SOUNDSTREAMBLOCK&&
932 t->id!=ST_DEFINEBITSJPEG&&t->id!=ST_DEFINEBITSJPEG2&&t->id!=ST_DEFINEBITSJPEG3);
936 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
937 if (writer->write(writer,raw,2)!=2)
940 fprintf(stderr,"WriteTag() failed: Short Header.\n");
947 raw[0] = SWAP16((t->id<<6)|0x3f);
948 if (writer->write(writer,raw,2)!=2)
951 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
957 if (writer->write(writer,&len,4)!=4)
960 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
967 { if (writer->write(writer,t->data,t->len)!=t->len)
970 fprintf(stderr,"WriteTag() failed: Data.\n");
976 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
980 return t->len+(short_tag?2:6);
983 int swf_WriteTag(int handle, TAG * t)
988 return swf_WriteTag2(0, t);
989 writer_init_filewriter(&writer, handle);
990 len = swf_WriteTag2(&writer, t);
991 writer.finish(&writer);
995 int swf_DefineSprite_GetRealSize(TAG * t)
996 // Sprite Handling: Helper function to pack DefineSprite-Tag
998 if(len>4) { // folded sprite
1002 { t = swf_NextTag(t);
1003 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
1005 } while (t&&(t->id!=ST_END));
1009 void swf_UnFoldSprite(TAG * t)
1014 U16 spriteid,spriteframes;
1016 if(t->id!=ST_DEFINESPRITE)
1018 if(t->len<=4) // not folded
1023 spriteid = swf_GetU16(t); //id
1024 spriteframes = swf_GetU16(t); //frames
1031 tmp = swf_GetU16(t);
1036 if(id == ST_DEFINESPRITE && len<=4)
1040 len = swf_GetU32(t);
1041 it = swf_InsertTag(next, id);
1046 { it->data = (U8*)rfx_alloc(it->len);
1047 it->memsize = it->len;
1048 swf_GetBlock(t, it->data, it->len);
1055 rfx_free(t->data); t->data = 0;
1056 t->memsize = t->len = t->pos = 0;
1058 swf_SetU16(t, spriteid);
1059 swf_SetU16(t, spriteframes);
1062 void swf_FoldSprite(TAG * t)
1065 U16 id,frames,tmpid;
1067 if(t->id!=ST_DEFINESPRITE)
1070 fprintf(stderr, "Error: Sprite has no ID!");
1074 /* sprite is already folded */
1081 t->len = t->pos = t->memsize = 0;
1086 t = swf_NextTag(sprtag);
1091 if(t->id==ST_SHOWFRAME) frames++;
1092 if(t->id == ST_DEFINESPRITE && t->len<=4)
1097 } while(t && level);
1099 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1101 swf_SetU16(sprtag, id);
1102 swf_SetU16(sprtag, frames);
1104 t = swf_NextTag(sprtag);
1110 (t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2&&t->id!=ST_SOUNDSTREAMBLOCK&&
1111 t->id!=ST_DEFINEBITSJPEG&&t->id!=ST_DEFINEBITSJPEG2&&t->id!=ST_DEFINEBITSJPEG3)
1113 swf_SetU16(sprtag,t->len|(t->id<<6));
1115 swf_SetU16(sprtag,0x3f|(t->id<<6));
1116 swf_SetU32(sprtag,t->len);
1119 swf_SetBlock(sprtag,t->data, t->len);
1121 if(t->id == ST_DEFINESPRITE && t->len<=4)
1130 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1132 // sprtag->next = t;
1133 // t->prev = sprtag;
1136 int swf_IsFolded(TAG * t)
1138 return (t->id == ST_DEFINESPRITE && t->len>4);
1141 void swf_FoldAll(SWF*swf)
1143 TAG*tag = swf->firstTag;
1144 //swf_DumpSWF(stdout, swf);
1146 if(tag->id == ST_DEFINESPRITE) {
1147 swf_FoldSprite(tag);
1148 //swf_DumpSWF(stdout, swf);
1150 tag = swf_NextTag(tag);
1154 void swf_UnFoldAll(SWF*swf)
1156 TAG*tag = swf->firstTag;
1158 if(tag->id == ST_DEFINESPRITE)
1159 swf_UnFoldSprite(tag);
1164 void swf_OptimizeTagOrder(SWF*swf)
1171 /* at the moment, we don't actually do optimizing,
1172 only fixing of non-spec-conformant things like
1179 tag = swf->firstTag;
1182 if(tag->id == ST_DEFINESPRITE) {
1184 /* ??? all sprites are supposed to be unfolded */
1185 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1195 /* move non-sprite tags out of sprite */
1196 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1197 /* remove tag from current position */
1198 tag->prev->next = tag->next;
1200 tag->next->prev = tag->prev;
1202 /* insert before tag level0 */
1204 tag->prev = level0->prev;
1207 tag->prev->next = tag;
1209 swf->firstTag = tag;
1213 if(tag->id == ST_END) {
1224 int swf_ReadSWF2(reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1226 if (!swf) return -1;
1227 memset(swf,0x00,sizeof(SWF));
1229 { char b[32]; // read Header
1235 if ((len = reader->read(reader ,b,8))<8) return -1;
1237 if (b[0]!='F' && b[0]!='C') return -1;
1238 if (b[1]!='W') return -1;
1239 if (b[2]!='S') return -1;
1240 swf->fileVersion = b[3];
1241 swf->compressed = (b[0]=='C')?1:0;
1242 swf->fileSize = GET32(&b[4]);
1244 if(swf->compressed) {
1245 reader_init_zlibinflate(&zreader, reader);
1249 reader_GetRect(reader, &swf->movieSize);
1250 reader->read(reader, &swf->frameRate, 2);
1251 swf->frameRate = SWAP16(swf->frameRate);
1252 reader->read(reader, &swf->frameCount, 2);
1253 swf->frameCount = SWAP16(swf->frameCount);
1255 /* read tags and connect to list */
1257 while (t) t = swf_ReadTag(reader,t);
1258 swf->firstTag = t1.next;
1259 t1.next->prev = NULL;
1265 int swf_ReadSWF(int handle, SWF * swf)
1268 reader_init_filereader(&reader, handle);
1269 return swf_ReadSWF2(&reader, swf);
1272 int swf_WriteSWF2(writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1279 int writer_lastpos = 0;
1282 if (!swf) return -1;
1283 if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1285 if(writer) writer_lastpos = writer->pos;
1287 // Insert REFLEX Tag
1289 #ifdef INSERT_RFX_TAG
1291 if ((swf->firstTag && swf->firstTag->id != ST_REFLEX) &&
1292 (!swf->firstTag->next || swf->firstTag->next->id != ST_REFLEX))
1294 swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),"rfx",3);
1297 #endif // INSERT_RFX_TAG
1299 if(swf->fileVersion >= 9) {
1300 if ((!swf->firstTag || swf->firstTag->id != ST_SCENEDESCRIPTION) &&
1302 !swf->firstTag->next || swf->firstTag->next->id != ST_SCENEDESCRIPTION) &&
1304 !swf->firstTag->next ||
1305 !swf->firstTag->next->next || swf->firstTag->next->next->id != ST_SCENEDESCRIPTION))
1307 TAG*scene = swf_InsertTagBefore(swf, swf->firstTag,ST_SCENEDESCRIPTION);
1308 swf_SetU16(scene, 1);
1309 swf_SetString(scene, "Scene 1");
1310 swf_SetU8(scene, 0);
1314 if(swf->fileVersion >= 9) {
1315 if (swf->firstTag && swf->firstTag->id != ST_FILEATTRIBUTES)
1317 U32 flags = 0x8; // | 128 = usenetwork, | 8 = hasmetadata
1318 swf_SetU32(swf_InsertTagBefore(swf, swf->firstTag,ST_FILEATTRIBUTES),flags);
1322 // Count Frames + File Size
1329 len += swf_WriteTag(-1,t);
1330 if(t->id == ST_DEFINESPRITE && !swf_IsFolded(t)) inSprite++;
1331 else if(t->id == ST_END && inSprite) inSprite--;
1332 else if(t->id == ST_END && !inSprite) {
1333 if(t->prev && t->prev->id!=ST_SHOWFRAME)
1336 else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1344 memset(&t1,0x00,sizeof(TAG));
1348 { // measure header file size
1351 memset(&t2,0x00,sizeof(TAG));
1354 swf_SetRect(&t2, &swf->movieSize);
1355 swf_SetU16(&t2, swf->frameRate);
1356 swf_SetU16(&t2, swf->frameCount);
1357 l = swf_GetTagLen(&t2)+8;
1359 if(swf->compressed == 8) {
1364 if(len) {// don't touch headers without tags
1365 swf->fileSize = fileSize;
1366 swf->frameCount = frameCount;
1369 if(swf->compressed != 8) {
1370 /* compressed flag set to 8 means "skip first 8
1371 header bytes". This is necessary if the caller wants to
1372 create compressed SWFs himself .
1373 It also means that we don't initialize our own zlib
1374 writer, but assume the caller provided one.
1376 if(swf->compressed) {
1378 writer->write(writer, id, 3);
1382 writer->write(writer, id, 3);
1385 writer->write(writer, &swf->fileVersion, 1);
1386 PUT32(b4, swf->fileSize);
1387 writer->write(writer, b4, 4);
1389 if(swf->compressed) {
1390 writer_init_zlibdeflate(&zwriter, writer);
1395 swf_SetRect(&t1,&swf->movieSize);
1396 swf_SetU16(&t1,swf->frameRate);
1397 swf_SetU16(&t1,swf->frameCount);
1399 ret = writer->write(writer,b,swf_GetTagLen(&t1));
1400 if (ret!=swf_GetTagLen(&t1))
1403 fprintf(stderr, "ret:%d\n",ret);
1405 fprintf(stderr,"WriteSWF() failed: Header.\n");
1412 { if (swf_WriteTag2(writer, t)<0) return -1;
1415 if(swf->compressed) {
1416 if(swf->compressed != 8) {
1417 zwriter.finish(&zwriter);
1418 return writer->pos - writer_lastpos;
1420 return (int)fileSize;
1422 return (int)fileSize;
1427 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1431 swf->compressed = 0;
1434 writer_init_nullwriter(&writer);
1435 len = swf_WriteSWF2(&writer, swf);
1438 writer_init_filewriter(&writer, handle);
1439 len = swf_WriteSWF2(&writer, swf);
1440 writer.finish(&writer);
1444 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1448 swf->compressed = 1;
1451 writer_init_nullwriter(&writer);
1452 len = swf_WriteSWF2(&writer, swf);
1454 writer_init_filewriter(&writer, handle);
1455 len = swf_WriteSWF2(&writer, swf);
1456 writer.finish(&writer);
1460 int swf_WriteHeader2(writer_t*writer,SWF * swf)
1463 memcpy(&myswf,swf,sizeof(SWF));
1465 return swf_WriteSWF2(writer, &myswf);
1468 int swf_WriteHeader(int handle,SWF * swf)
1471 memcpy(&myswf,swf,sizeof(SWF));
1473 return swf_WriteSWF(handle, &myswf);
1476 int swf_WriteCGI(SWF * swf)
1480 len = swf_WriteSWF(-1,swf);
1482 if (len<0) return -1;
1484 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1485 "Accept-Ranges: bytes\n"
1486 "Content-Length: %lu\n"
1487 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1490 write(fileno(stdout),s,strlen(s));
1491 return swf_WriteSWF(fileno(stdout),swf);
1494 SWF* swf_CopySWF(SWF*swf)
1496 SWF*nswf = rfx_alloc(sizeof(SWF));
1498 memcpy(nswf, swf, sizeof(SWF));
1500 tag = swf->firstTag;
1503 ntag = swf_CopyTag(ntag, tag);
1505 nswf->firstTag = ntag;
1511 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1512 { TAG * t = swf->firstTag;
1515 { TAG * tnew = t->next;
1516 if (t->data) rfx_free(t->data);
1523 // include advanced functions
1525 #include "modules/swfdump.c"
1526 #include "modules/swfshape.c"
1527 #include "modules/swftext.c"
1528 #include "modules/swffont.c"
1529 #include "modules/swfobject.c"
1530 #include "modules/swfbutton.c"
1531 #include "modules/swftools.c"
1532 #include "modules/swfcgi.c"
1533 #include "modules/swfbits.c"
1534 #include "modules/swfaction.c"
1535 #include "modules/swfsound.c"
1536 #include "modules/swfdraw.c"
1537 #include "modules/swfrender.c"
1538 #include "modules/swffilter.c"