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 */
32 #endif // HAVE_JPEGLIB
38 #ifndef RFXSWF_DISABLESOUND
40 #include "lame/lame.h"
53 void* rfx_alloc(int size)
58 fprintf(stderr, "Warning: Zero alloc\n");
64 fprintf(stderr, "FATAL: Out of memory\n");
65 /* TODO: we should send a signal, so that the debugger kicks in */
70 void* rfx_realloc(void*data, int size)
75 fprintf(stderr, "Warning: Zero realloc\n");
82 ptr = realloc(data, size);
86 fprintf(stderr, "FATAL: Out of memory\n");
87 /* TODO: we should send a signal, so that the debugger kicks in */
92 void* rfx_calloc(int size)
97 fprintf(stderr, "Warning: Zero alloc\n");
106 fprintf(stderr, "FATAL: Out of memory\n");
107 /* TODO: we should send a signal, so that the debugger kicks in */
111 memset(ptr, 0, size);
116 void rfx_free(void*ptr)
123 // internal constants
125 #define MALLOC_SIZE 128
126 #define INSERT_RFX_TAG
128 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
130 // inline wrapper functions
132 TAG * swf_NextTag(TAG * t) { return t->next; }
133 TAG * swf_PrevTag(TAG * t) { return t->prev; }
134 U16 swf_GetTagID(TAG * t) { return t->id; }
135 U32 swf_GetTagLen(TAG * t) { return t->len; }
136 U8* swf_GetTagLenPtr(TAG * t) { return &(t->data[t->len]); }
137 U32 swf_GetTagPos(TAG * t) { return t->pos; }
139 // for future purpose: avoid high level lib functions to change tagpos/bitpos
141 #define swf_SaveTagPos(tag)
142 #define swf_RestoreTagPos(tag)
144 void swf_SetTagPos(TAG * t,U32 pos)
145 { swf_ResetReadBits(t);
146 if (pos<=t->len) t->pos = pos;
149 fprintf(stderr,"SetTagPos(%d) out of bounds: TagID = %i\n",pos, t->id);
154 char* swf_GetString(TAG*t)
156 char* str = ((char*)(&(t)->data[(t)->pos]));
161 U8 swf_GetU8(TAG * t)
162 { swf_ResetReadBits(t);
165 { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
169 return t->data[t->pos++];
172 U16 swf_GetU16(TAG * t)
174 swf_ResetReadBits(t);
176 if (t->pos>(t->len-2))
177 { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
181 res = t->data[t->pos] | (t->data[t->pos+1]<<8);
186 U32 swf_GetU32(TAG * t)
188 swf_ResetReadBits(t);
190 if (t->pos>(t->len-4))
191 { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
195 res = t->data[t->pos] | (t->data[t->pos+1]<<8) |
196 (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
201 int swf_GetBlock(TAG * t,U8 * b,int l)
202 // returns number of bytes written (<=l)
203 // b = NULL -> skip data
204 { swf_ResetReadBits(t);
205 if ((t->len-t->pos)<l) l=t->len-t->pos;
206 if (b && l) memcpy(b,&t->data[t->pos],l);
211 int swf_SetBlock(TAG * t,U8 * b,int l)
212 // Appends Block to the end of Tagdata, returns size
213 { U32 newlen = t->len + l;
214 swf_ResetWriteBits(t);
215 if (newlen>t->memsize)
216 { U32 newmem = MEMSIZE(newlen);
217 U8 * newdata = (U8*)((t->data)?realloc(t->data,newmem):malloc(newmem));
221 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (1). (%d bytes)\n", newmem);
229 if (b) memcpy(&t->data[t->len],b,l);
230 else memset(&t->data[t->len],0x00,l);
235 int swf_SetU8(TAG * t,U8 v)
236 { swf_ResetWriteBits(t);
237 if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
238 t->data[t->len++] = v;
242 int swf_SetU16(TAG * t,U16 v)
247 swf_ResetWriteBits(t);
248 if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
249 t->data[t->len++] = a[0];
250 t->data[t->len++] = a[1];
253 void swf_SetS16(TAG * t,int v)
255 if(v>32767 || v<-32768) {
256 fprintf(stderr, "Warning: S16 overflow: %d\n", v);
258 swf_SetU16(t, (S16)v);
261 int swf_SetU32(TAG * t,U32 v)
263 a[0] = v&0xff; // to ensure correct handling of non-intel byteorder
268 swf_ResetWriteBits(t);
269 if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
270 t->data[t->len++] = a[0];
271 t->data[t->len++] = a[1];
272 t->data[t->len++] = a[2];
273 t->data[t->len++] = a[3];
277 U32 swf_GetBits(TAG * t,int nbits)
279 if (!nbits) return 0;
280 if (!t->readBit) t->readBit = 0x80;
283 if (t->data[t->pos]&t->readBit) res|=1;
287 { if (nbits) t->readBit = 0x80;
290 { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
300 S32 swf_GetSBits(TAG * t,int nbits)
301 { U32 res = swf_GetBits(t,nbits);
302 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
306 U32 reader_GetBits(struct reader_t*reader, int nbits)
307 { return reader_readbits(reader, nbits);
309 S32 reader_GetSBits(struct reader_t*reader, int nbits)
310 { U32 res = reader_readbits(reader, nbits);
311 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
315 int swf_SetBits(TAG * t,U32 v,int nbits)
316 { U32 bm = 1<<(nbits-1);
320 { if (FAILED(swf_SetU8(t,0))) return -1;
323 if (v&bm) t->data[t->len-1] |= t->writeBit;
331 // Advanced Data Access Functions
333 int swf_SetRGB(TAG * t,RGBA * col)
336 { swf_SetU8(t,col->r);
339 } else swf_SetBlock(t,NULL,3);
342 void swf_GetRGB(TAG * t, RGBA * col)
347 col->r = swf_GetU8(t);
348 col->g = swf_GetU8(t);
349 col->b = swf_GetU8(t);
353 int swf_SetRGBA(TAG * t,RGBA * col)
356 { swf_SetU8(t,col->r);
360 } else swf_SetBlock(t,NULL,4);
363 void swf_GetRGBA(TAG * t, RGBA * col)
368 col->r = swf_GetU8(t);
369 col->g = swf_GetU8(t);
370 col->b = swf_GetU8(t);
371 col->a = swf_GetU8(t);
374 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
379 memset(gradient, 0, sizeof(GRADIENT));
384 gradient->num = swf_GetU8(tag);
385 for(t=0;t<gradient->num;t++)
390 gradient->ratios[t] = swf_GetU8(tag);
392 swf_GetRGB(tag, &gradient->rgba[t]);
394 swf_GetRGBA(tag, &gradient->rgba[t]);
398 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
402 memset(gradient, 0, sizeof(GRADIENT));
405 swf_SetU8(tag, gradient->num);
406 for(t=0; t<8 && t<gradient->num; t++)
408 swf_SetU8(tag, gradient->ratios[t]);
410 swf_SetRGB(tag, &gradient->rgba[t]);
412 swf_SetRGBA(tag, &gradient->rgba[t]);
416 int swf_CountUBits(U32 v,int nbits)
419 if(v == 0x00000000) n = 0;
425 return (n>nbits)?n:nbits;
428 int swf_CountBits(U32 v,int nbits)
432 { if(v == 0xffffffff) n = 1;
440 { if(v == 0x00000000) n = 0;
447 return (n>nbits)?n:nbits;
450 int swf_GetRect(TAG * t,SRECT * r)
453 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
455 nbits = (int) swf_GetBits(t,5);
456 r->xmin = swf_GetSBits(t,nbits);
457 r->xmax = swf_GetSBits(t,nbits);
458 r->ymin = swf_GetSBits(t,nbits);
459 r->ymax = swf_GetSBits(t,nbits);
463 int reader_GetRect(struct reader_t*reader,SRECT * r)
467 nbits = (int) reader_GetBits(reader,5);
468 r->xmin = reader_GetSBits(reader,nbits);
469 r->xmax = reader_GetSBits(reader,nbits);
470 r->ymin = reader_GetSBits(reader,nbits);
471 r->ymax = reader_GetSBits(reader,nbits);
475 int swf_SetRect(TAG * t,SRECT * r)
478 nbits = swf_CountBits(r->xmin,0);
479 nbits = swf_CountBits(r->xmax,nbits);
480 nbits = swf_CountBits(r->ymin,nbits);
481 nbits = swf_CountBits(r->ymax,nbits);
483 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
487 swf_SetBits(t,nbits,5);
488 swf_SetBits(t,r->xmin,nbits);
489 swf_SetBits(t,r->xmax,nbits);
490 swf_SetBits(t,r->ymin,nbits);
491 swf_SetBits(t,r->ymax,nbits);
496 void swf_ExpandRect(SRECT*src, SPOINT add)
498 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
503 if((add.x|add.y) == 0) src->xmax++; //make sure the bbox is not NULL anymore
506 if(add.x < src->xmin)
508 if(add.x > src->xmax)
510 if(add.y < src->ymin)
512 if(add.y > src->ymax)
515 void swf_ExpandRect2(SRECT*src, SRECT*add)
517 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
519 if((src->xmin | src->ymin | src->xmax | src->ymax)==0)
521 if(add->xmin < src->xmin)
522 src->xmin = add->xmin;
523 if(add->ymin < src->ymin)
524 src->ymin = add->ymin;
525 if(add->xmax > src->xmax)
526 src->xmax = add->xmax;
527 if(add->ymax > src->ymax)
528 src->ymax = add->ymax;
530 void swf_ExpandRect3(SRECT*src, SPOINT center, int radius)
532 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
533 src->xmin = center.x-radius;
534 src->ymin = center.y-radius;
535 src->xmax = center.x+radius;
536 src->ymax = center.y+radius;
537 if((center.x|center.y|radius) == 0) src->xmax++; //make sure the bbox is not NULL anymore
540 if(center.x - radius < src->xmin)
541 src->xmin = center.x - radius;
542 if(center.x + radius > src->xmax)
543 src->xmax = center.x - radius;
544 if(center.y - radius < src->ymin)
545 src->ymin = center.y - radius;
546 if(center.y + radius > src->ymax)
547 src->ymax = center.y - radius;
549 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
552 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
553 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
556 SRECT swf_TurnRect(SRECT r, MATRIX* m)
559 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
560 p1.x = r.xmin;p1.y = r.ymin;
561 p2.x = r.xmax;p2.y = r.ymin;
562 p3.x = r.xmin;p3.y = r.ymax;
563 p4.x = r.xmax;p4.y = r.ymax;
564 pp1 = swf_TurnPoint(p1, m);
565 pp2 = swf_TurnPoint(p2, m);
566 pp3 = swf_TurnPoint(p3, m);
567 pp4 = swf_TurnPoint(p4, m);
568 g.xmin = g.xmax = pp1.x;
569 g.ymin = g.ymax = pp1.y;
570 swf_ExpandRect(&g, pp2);
571 swf_ExpandRect(&g, pp3);
572 swf_ExpandRect(&g, pp4);
577 int swf_GetMatrix(TAG * t,MATRIX * m)
584 { m->sx = m->sy = 0x10000;
590 swf_ResetReadBits(t);
592 if (swf_GetBits(t,1))
593 { nbits = swf_GetBits(t,5);
594 m->sx = swf_GetSBits(t,nbits);
595 m->sy = swf_GetSBits(t,nbits);
597 else m->sx = m->sy = 0x10000;
599 if (swf_GetBits(t,1))
600 { nbits = swf_GetBits(t,5);
601 m->r0 = swf_GetSBits(t,nbits);
602 m->r1 = swf_GetSBits(t,nbits);
604 else m->r0 = m->r1 = 0x0;
606 nbits = swf_GetBits(t,5);
607 m->tx = swf_GetSBits(t,nbits);
608 m->ty = swf_GetSBits(t,nbits);
613 int swf_SetMatrix(TAG * t,MATRIX * m)
619 ma.sx = ma.sy = 0x10000;
624 swf_ResetWriteBits(t);
626 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
628 { swf_SetBits(t,1,1);
629 nbits = swf_CountBits(m->sx,0);
630 nbits = swf_CountBits(m->sy,nbits);
632 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
635 swf_SetBits(t,nbits,5);
636 swf_SetBits(t,m->sx,nbits);
637 swf_SetBits(t,m->sy,nbits);
640 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
642 { swf_SetBits(t,1,1);
643 nbits = swf_CountBits(m->r0,0);
644 nbits = swf_CountBits(m->r1,nbits);
646 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
649 swf_SetBits(t,nbits,5);
650 swf_SetBits(t,m->r0,nbits);
651 swf_SetBits(t,m->r1,nbits);
654 nbits = swf_CountBits(m->tx,0);
655 nbits = swf_CountBits(m->ty,nbits);
657 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
660 swf_SetBits(t,nbits,5);
661 swf_SetBits(t,m->tx,nbits);
662 swf_SetBits(t,m->ty,nbits);
667 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha) //FIXME: alpha should be type bool
675 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
676 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
680 swf_ResetReadBits(t);
681 hasadd = swf_GetBits(t,1);
682 hasmul = swf_GetBits(t,1);
683 nbits = swf_GetBits(t,4);
686 { cx->r0 = (S16)swf_GetSBits(t,nbits);
687 cx->g0 = (S16)swf_GetSBits(t,nbits);
688 cx->b0 = (S16)swf_GetSBits(t,nbits);
690 cx->a0 = (S16)swf_GetSBits(t,nbits);
694 { cx->r1 = (S16)swf_GetSBits(t,nbits);
695 cx->g1 = (S16)swf_GetSBits(t,nbits);
696 cx->b1 = (S16)swf_GetSBits(t,nbits);
698 cx->a1 = (S16)swf_GetSBits(t,nbits);
704 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
712 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
713 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
723 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
724 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
727 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
728 nbits = swf_CountBits((S32)cx->r0,nbits);
729 nbits = swf_CountBits((S32)cx->g0,nbits);
730 nbits = swf_CountBits((S32)cx->b0,nbits);
734 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
735 nbits = swf_CountBits((S32)cx->r1,nbits);
736 nbits = swf_CountBits((S32)cx->g1,nbits);
737 nbits = swf_CountBits((S32)cx->b1,nbits);
740 swf_ResetWriteBits(t);
741 swf_SetBits(t,hasadd?1:0,1);
742 swf_SetBits(t,hasmul?1:0,1);
743 swf_SetBits(t,nbits,4);
746 { swf_SetBits(t,cx->r0,nbits);
747 swf_SetBits(t,cx->g0,nbits);
748 swf_SetBits(t,cx->b0,nbits);
749 if (alpha) swf_SetBits(t,cx->a0,nbits);
753 { swf_SetBits(t,cx->r1,nbits);
754 swf_SetBits(t,cx->g1,nbits);
755 swf_SetBits(t,cx->b1,nbits);
756 if (alpha) swf_SetBits(t,cx->a1,nbits);
762 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
763 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
765 void swf_SetPassword(TAG * t, const char * password)
767 /* WARNING: crypt_md5 is not reentrant */
771 #if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) && defined(HAVE_TIME_H) && defined(HAVE_TIME)
773 salt[0] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
774 salt[1] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
778 fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
779 fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
782 md5string = crypt_md5(password, salt);
785 swf_SetString(t, md5string);
788 int swf_VerifyPassword(TAG * t, const char * password)
790 char*md5string1, *md5string2;
795 if(t->len >= 5 && t->pos==0 &&
800 printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
803 md5string1 = swf_GetString(t);
805 if(strncmp(md5string1, "$1$",3 )) {
806 fprintf(stderr, "rfxswf: no salt in pw string\n");
809 x = strchr(md5string1+3, '$');
811 fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
814 n = x-(md5string1+3);
815 salt = (char*)malloc(n+1);
816 memcpy(salt, md5string1+3, n);
819 md5string2 = crypt_md5(password, salt);
821 if(strcmp(md5string1, md5string2) != 0)
826 // Tag List Manipulating Functions
828 TAG * swf_InsertTag(TAG * after,U16 id)
831 t = (TAG *)malloc(sizeof(TAG));
833 { memset(t,0x00,sizeof(TAG));
839 t->next = after->next;
841 if (t->next) t->next->prev = t;
847 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
850 t = (TAG *)malloc(sizeof(TAG));
852 { memset(t,0x00,sizeof(TAG));
858 t->prev = before->prev;
860 if (t->prev) t->prev->next = t;
863 if(swf && swf->firstTag == before) {
869 void swf_ClearTag(TAG * t)
871 if (t->data) free(t->data);
880 void swf_ResetTag(TAG*tag, U16 id)
882 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
886 TAG* swf_CopyTag(TAG*tag, TAG*to_copy)
888 tag = swf_InsertTag(tag, to_copy->id);
889 swf_SetBlock(tag, to_copy->data, to_copy->len);
893 int swf_DeleteTag(TAG * t)
896 if (t->prev) t->prev->next = t->next;
897 if (t->next) t->next->prev = t->prev;
899 if (t->data) free(t->data);
904 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
910 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
918 if (reader->read(reader, &len, 4) != 4) return NULL;
922 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
923 // Sprite handling fix: Flatten sprite tree
925 t = (TAG *)malloc(sizeof(TAG));
930 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (2). (%d bytes)\n", sizeof(TAG));
935 memset(t,0x00,sizeof(TAG));
941 { t->data = (U8*)malloc(t->len);
945 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (3). (%d bytes)\n", t->len);
950 if (reader->read(reader, t->data, t->len) != t->len) return NULL;
962 int swf_DefineSprite_GetRealSize(TAG * t);
964 int swf_WriteTag2(struct writer_t*writer, TAG * t)
965 // returns tag length in bytes (incl. Header), -1 = Error
966 // writer = 0 -> no output
973 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
975 short_tag = len<0x3f&&(t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2);
979 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
980 if (writer->write(writer,raw,2)!=2)
983 fprintf(stderr,"WriteTag() failed: Short Header.\n");
990 raw[0] = SWAP16((t->id<<6)|0x3f);
991 if (writer->write(writer,raw,2)!=2)
994 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
1000 if (writer->write(writer,&len,4)!=4)
1003 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
1010 { if (writer->write(writer,t->data,t->len)!=t->len)
1013 fprintf(stderr,"WriteTag() failed: Data.\n");
1019 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
1023 return t->len+(short_tag?2:6);
1026 int swf_WriteTag(int handle, TAG * t)
1028 struct writer_t writer;
1031 return swf_WriteTag2(0, t);
1032 writer_init_filewriter(&writer, handle);
1033 len = swf_WriteTag2(&writer, t);
1034 writer.finish(&writer);
1038 int swf_DefineSprite_GetRealSize(TAG * t)
1039 // Sprite Handling: Helper function to pack DefineSprite-Tag
1041 if(len>4) { // folded sprite
1045 { t = swf_NextTag(t);
1046 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
1048 } while (t&&(t->id!=ST_END));
1052 void swf_UnFoldSprite(TAG * t)
1057 U16 spriteid,spriteframes;
1059 if(t->id!=ST_DEFINESPRITE)
1061 if(t->len<=4) // not folded
1066 spriteid = swf_GetU16(t); //id
1067 spriteframes = swf_GetU16(t); //frames
1074 tmp = swf_GetU16(t);
1079 if(id == ST_DEFINESPRITE && len<=4)
1083 len = swf_GetU32(t);
1084 it = swf_InsertTag(next, id);
1089 { it->data = (U8*)malloc(it->len);
1090 it->memsize = it->len;
1091 swf_GetBlock(t, it->data, it->len);
1098 free(t->data); t->data = 0;
1099 t->memsize = t->len = t->pos = 0;
1101 swf_SetU16(t, spriteid);
1102 swf_SetU16(t, spriteframes);
1105 void swf_FoldSprite(TAG * t)
1108 U16 id,frames,tmpid;
1110 if(t->id!=ST_DEFINESPRITE)
1113 fprintf(stderr, "Error: Sprite has no ID!");
1117 /* sprite is already folded */
1124 t->len = t->pos = t->memsize = 0;
1129 t = swf_NextTag(sprtag);
1134 if(t->id==ST_SHOWFRAME) frames++;
1135 if(t->id == ST_DEFINESPRITE && t->len<=4)
1140 } while(t && level);
1142 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1144 swf_SetU16(sprtag, id);
1145 swf_SetU16(sprtag, frames);
1147 t = swf_NextTag(sprtag);
1152 if(t->len<0x3f&&t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2) {
1153 swf_SetU16(sprtag,t->len|(t->id<<6));
1155 swf_SetU16(sprtag,0x3f|(t->id<<6));
1156 swf_SetU32(sprtag,t->len);
1159 swf_SetBlock(sprtag,t->data, t->len);
1161 if(t->id == ST_DEFINESPRITE && t->len<=4)
1170 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1172 // sprtag->next = t;
1173 // t->prev = sprtag;
1176 int swf_IsFolded(TAG * t)
1178 return (t->id == ST_DEFINESPRITE && t->len>4);
1181 void swf_FoldAll(SWF*swf)
1183 TAG*tag = swf->firstTag;
1184 //swf_DumpSWF(stdout, swf);
1186 if(tag->id == ST_DEFINESPRITE) {
1187 swf_FoldSprite(tag);
1188 //swf_DumpSWF(stdout, swf);
1190 tag = swf_NextTag(tag);
1194 void swf_UnFoldAll(SWF*swf)
1196 TAG*tag = swf->firstTag;
1198 if(tag->id == ST_DEFINESPRITE)
1199 swf_UnFoldSprite(tag);
1204 void swf_OptimizeTagOrder(SWF*swf)
1211 /* at the moment, we don't actually do optimizing,
1212 only fixing of non-spec-conformant things like
1219 tag = swf->firstTag;
1222 if(tag->id == ST_DEFINESPRITE) {
1224 /* ??? all sprites are supposed to be unfolded */
1225 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1235 /* move non-sprite tags out of sprite */
1236 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1237 /* remove tag from current position */
1238 tag->prev->next = tag->next;
1240 tag->next->prev = tag->prev;
1242 /* insert before tag level0 */
1244 tag->prev = level0->prev;
1246 tag->prev->next = tag;
1250 if(tag->id == ST_END) {
1261 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1263 if (!swf) return -1;
1264 memset(swf,0x00,sizeof(SWF));
1266 { char b[32]; // read Header
1270 struct reader_t zreader;
1272 if ((len = reader->read(reader ,b,8))<8) return -1;
1274 if (b[0]!='F' && b[0]!='C') return -1;
1275 if (b[1]!='W') return -1;
1276 if (b[2]!='S') return -1;
1277 swf->fileVersion = b[3];
1278 swf->compressed = (b[0]=='C')?1:0;
1279 swf->fileSize = GET32(&b[4]);
1281 if(swf->compressed) {
1282 reader_init_zlibinflate(&zreader, reader);
1286 reader_GetRect(reader, &swf->movieSize);
1287 reader->read(reader, &swf->frameRate, 2);
1288 swf->frameRate = SWAP16(swf->frameRate);
1289 reader->read(reader, &swf->frameCount, 2);
1290 swf->frameCount = SWAP16(swf->frameCount);
1292 /* read tags and connect to list */
1294 while (t) t = swf_ReadTag(reader,t);
1295 swf->firstTag = t1.next;
1296 t1.next->prev = NULL;
1302 int swf_ReadSWF(int handle, SWF * swf)
1304 struct reader_t reader;
1305 reader_init_filereader(&reader, handle);
1306 return swf_ReadSWF2(&reader, swf);
1309 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1313 struct writer_t zwriter;
1316 int writer_lastpos = 0;
1319 if (!swf) return -1;
1320 if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1322 if(writer) writer_lastpos = writer->pos;
1324 // Insert REFLEX Tag
1326 #ifdef INSERT_RFX_TAG
1328 if (swf->firstTag && swf_NextTag(swf->firstTag))
1329 if (swf_GetTagID(swf_NextTag(swf->firstTag))!=ST_REFLEX)
1330 swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),"rfx",3);
1332 #endif // INSERT_RFX_TAG
1334 // Count Frames + File Size
1341 len += swf_WriteTag(-1,t);
1342 if(t->id == ST_DEFINESPRITE && !swf_IsFolded(t)) inSprite++;
1343 else if(t->id == ST_END && inSprite) inSprite--;
1344 else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1352 memset(&t1,0x00,sizeof(TAG));
1356 { // measure header file size
1359 memset(&t2,0x00,sizeof(TAG));
1362 swf_SetRect(&t2, &swf->movieSize);
1363 swf_SetU16(&t2, swf->frameRate);
1364 swf_SetU16(&t2, swf->frameCount);
1365 l = swf_GetTagLen(&t2)+8;
1367 if(swf->compressed == 8) {
1372 if(len) {// don't touch headers without tags
1373 swf->fileSize = fileSize;
1374 swf->frameCount = frameCount;
1377 if(swf->compressed != 8) {
1378 /* compressed flag set to 8 means "skip first 8
1379 header bytes". This is necessary if the caller wants to
1380 create compressed SWFs himself .
1381 It also means that we don't initialize our own zlib
1382 writer, but assume the caller provided one.
1384 if(swf->compressed) {
1386 writer->write(writer, id, 3);
1390 writer->write(writer, id, 3);
1393 writer->write(writer, &swf->fileVersion, 1);
1394 PUT32(b4, swf->fileSize);
1395 writer->write(writer, b4, 4);
1397 if(swf->compressed) {
1398 writer_init_zlibdeflate(&zwriter, writer);
1403 swf_SetRect(&t1,&swf->movieSize);
1404 swf_SetU16(&t1,swf->frameRate);
1405 swf_SetU16(&t1,swf->frameCount);
1407 ret = writer->write(writer,b,swf_GetTagLen(&t1));
1408 if (ret!=swf_GetTagLen(&t1))
1411 fprintf(stderr, "ret:%d\n",ret);
1413 fprintf(stderr,"WriteSWF() failed: Header.\n");
1420 { if (swf_WriteTag2(writer, t)<0) return -1;
1423 if(swf->compressed) {
1424 if(swf->compressed != 8) {
1425 zwriter.finish(&zwriter);
1426 return writer->pos - writer_lastpos;
1428 return (int)fileSize;
1430 return (int)fileSize;
1435 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1437 struct writer_t writer;
1439 swf->compressed = 0;
1442 writer_init_nullwriter(&writer);
1443 len = swf_WriteSWF2(&writer, swf);
1445 writer_init_filewriter(&writer, handle);
1446 len = swf_WriteSWF2(&writer, swf);
1447 writer.finish(&writer);
1451 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1453 struct writer_t writer;
1455 swf->compressed = 1;
1458 writer_init_nullwriter(&writer);
1459 len = swf_WriteSWF2(&writer, swf);
1461 writer_init_filewriter(&writer, handle);
1462 len = swf_WriteSWF2(&writer, swf);
1463 writer.finish(&writer);
1467 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1470 memcpy(&myswf,swf,sizeof(SWF));
1472 return swf_WriteSWF2(writer, &myswf);
1475 int swf_WriteHeader(int handle,SWF * swf)
1478 memcpy(&myswf,swf,sizeof(SWF));
1480 return swf_WriteSWF(handle, &myswf);
1483 int swf_WriteCGI(SWF * swf)
1487 len = swf_WriteSWF(-1,swf);
1489 if (len<0) return -1;
1491 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1492 "Accept-Ranges: bytes\n"
1493 "Content-Length: %lu\n"
1494 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1497 write(fileno(stdout),s,strlen(s));
1498 return swf_WriteSWF(fileno(stdout),swf);
1501 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1502 { TAG * t = swf->firstTag;
1505 { TAG * tnew = t->next;
1506 if (t->data) free(t->data);
1513 // include advanced functions
1515 #include "modules/swfdump.c"
1516 #include "modules/swfshape.c"
1517 #include "modules/swftext.c"
1518 #include "modules/swffont.c"
1519 #include "modules/swfobject.c"
1520 #include "modules/swfbutton.c"
1521 #include "modules/swftools.c"
1522 #include "modules/swfcgi.c"
1523 #include "modules/swfbits.c"
1524 #include "modules/swfaction.c"
1525 #include "modules/swfsound.c"
1526 #include "modules/swfdraw.c"