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
39 #include "lame/lame.h"
46 #define MALLOC_SIZE 128
47 #define INSERT_RFX_TAG
49 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
52 // inline wrapper functions
54 TAG * swf_NextTag(TAG * t) { return t->next; }
55 TAG * swf_PrevTag(TAG * t) { return t->prev; }
56 U16 swf_GetTagID(TAG * t) { return t->id; }
57 U32 swf_GetTagLen(TAG * t) { return t->len; }
58 U8* swf_GetTagLenPtr(TAG * t) { return &(t->data[t->len]); }
59 U32 swf_GetTagPos(TAG * t) { return t->pos; }
61 // Basic Data Access Functions
63 #define swf_ResetReadBits(tag) if (tag->readBit) { tag->pos++; tag->readBit = 0; }
64 #define swf_ResetWriteBits(tag) if (tag->writeBit) { tag->writeBit = 0; }
66 // for future purpose: avoid high level lib functions to change tagpos/bitpos
68 #define swf_SaveTagPos(tag)
69 #define swf_RestoreTagPos(tag)
71 void swf_SetTagPos(TAG * t,U32 pos)
72 { swf_ResetReadBits(t);
73 if (pos<=t->len) t->pos = pos;
75 else fprintf(stderr,"SetTagPos() out of bounds: TagID = %i\n",t->id);
79 char* swf_GetString(TAG*t)
81 char* str = ((char*)(&(t)->data[(t)->pos]));
87 { swf_ResetReadBits(t);
90 { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
94 return t->data[t->pos++];
97 U16 swf_GetU16(TAG * t)
101 if (t->pos>(t->len-2))
102 { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
106 res = t->data[t->pos] | (t->data[t->pos+1]<<8);
111 U32 swf_GetU32(TAG * t)
113 swf_ResetReadBits(t);
115 if (t->pos>(t->len-4))
116 { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
120 res = t->data[t->pos] | (t->data[t->pos+1]<<8) |
121 (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
126 int swf_GetBlock(TAG * t,U8 * b,int l)
127 // returns number of bytes written (<=l)
128 // b = NULL -> skip data
129 { swf_ResetReadBits(t);
130 if ((t->len-t->pos)<l) l=t->len-t->pos;
131 if (b && l) memcpy(b,&t->data[t->pos],l);
136 int swf_SetBlock(TAG * t,U8 * b,int l)
137 // Appends Block to the end of Tagdata, returns size
138 { U32 newlen = t->len + l;
139 swf_ResetWriteBits(t);
140 if (newlen>t->memsize)
141 { U32 newmem = MEMSIZE(newlen);
142 U8 * newdata = (U8*)((t->data)?realloc(t->data,newmem):malloc(newmem));
146 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (1). (%d bytes)\n", newmem);
154 if (b) memcpy(&t->data[t->len],b,l);
155 else memset(&t->data[t->len],0x00,l);
160 int swf_SetU8(TAG * t,U8 v)
161 { swf_ResetWriteBits(t);
162 if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
163 t->data[t->len++] = v;
167 int swf_SetU16(TAG * t,U16 v)
172 swf_ResetWriteBits(t);
173 if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
174 t->data[t->len++] = a[0];
175 t->data[t->len++] = a[1];
179 int swf_SetU32(TAG * t,U32 v)
181 a[0] = v&0xff; // to ensure correct handling of non-intel byteorder
186 swf_ResetWriteBits(t);
187 if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
188 t->data[t->len++] = a[0];
189 t->data[t->len++] = a[1];
190 t->data[t->len++] = a[2];
191 t->data[t->len++] = a[3];
195 U32 swf_GetBits(TAG * t,int nbits)
197 if (!nbits) return 0;
198 if (!t->readBit) t->readBit = 0x80;
201 if (t->data[t->pos]&t->readBit) res|=1;
205 { if (nbits) t->readBit = 0x80;
208 { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
218 S32 swf_GetSBits(TAG * t,int nbits)
219 { U32 res = swf_GetBits(t,nbits);
220 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
224 U32 reader_GetBits(struct reader_t*reader, int nbits)
225 { return reader_readbits(reader, nbits);
227 S32 reader_GetSBits(struct reader_t*reader, int nbits)
228 { U32 res = reader_readbits(reader, nbits);
229 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
233 int swf_SetBits(TAG * t,U32 v,int nbits)
234 { U32 bm = 1<<(nbits-1);
238 { if (FAILED(swf_SetU8(t,0))) return -1;
241 if (v&bm) t->data[t->len-1] |= t->writeBit;
249 // Advanced Data Access Functions
251 int swf_SetRGB(TAG * t,RGBA * col)
254 { swf_SetU8(t,col->r);
257 } else swf_SetBlock(t,NULL,3);
260 void swf_GetRGB(TAG * t, RGBA * col)
265 col->r = swf_GetU8(t);
266 col->g = swf_GetU8(t);
267 col->b = swf_GetU8(t);
271 int swf_SetRGBA(TAG * t,RGBA * col)
274 { swf_SetU8(t,col->r);
278 } else swf_SetBlock(t,NULL,4);
281 void swf_GetRGBA(TAG * t, RGBA * col)
286 col->r = swf_GetU8(t);
287 col->g = swf_GetU8(t);
288 col->b = swf_GetU8(t);
289 col->a = swf_GetU8(t);
292 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
298 gradient->num = swf_GetU8(tag);
299 for(t=0;t<gradient->num;t++)
304 gradient->ratios[t] = swf_GetU8(tag);
306 swf_GetRGB(tag, &gradient->rgba[t]);
308 swf_GetRGBA(tag, &gradient->rgba[t]);
312 int swf_CountUBits(U32 v,int nbits)
315 if(v == 0x00000000) n = 0;
321 return (n>nbits)?n:nbits;
324 int swf_CountBits(U32 v,int nbits)
328 { if(v == 0xffffffff) n = 1;
336 { if(v == 0x00000000) n = 0;
343 return (n>nbits)?n:nbits;
346 int swf_GetRect(TAG * t,SRECT * r)
349 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
351 nbits = (int) swf_GetBits(t,5);
352 r->xmin = swf_GetSBits(t,nbits);
353 r->xmax = swf_GetSBits(t,nbits);
354 r->ymin = swf_GetSBits(t,nbits);
355 r->ymax = swf_GetSBits(t,nbits);
359 int reader_GetRect(struct reader_t*reader,SRECT * r)
363 nbits = (int) reader_GetBits(reader,5);
364 r->xmin = reader_GetSBits(reader,nbits);
365 r->xmax = reader_GetSBits(reader,nbits);
366 r->ymin = reader_GetSBits(reader,nbits);
367 r->ymax = reader_GetSBits(reader,nbits);
371 int swf_SetRect(TAG * t,SRECT * r)
374 nbits = swf_CountBits(r->xmin,0);
375 nbits = swf_CountBits(r->xmax,nbits);
376 nbits = swf_CountBits(r->ymin,nbits);
377 nbits = swf_CountBits(r->ymax,nbits);
379 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
383 swf_SetBits(t,nbits,5);
384 swf_SetBits(t,r->xmin,nbits);
385 swf_SetBits(t,r->xmax,nbits);
386 swf_SetBits(t,r->ymin,nbits);
387 swf_SetBits(t,r->ymax,nbits);
392 void swf_ExpandRect(SRECT*src, SPOINT add)
394 if(add.x < src->xmin)
396 if(add.x > src->xmax)
398 if(add.y < src->ymin)
400 if(add.y > src->ymax)
403 void swf_ExpandRect2(SRECT*src, SRECT*add)
405 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
407 if(add->xmin < src->xmin)
408 src->xmin = add->xmin;
409 if(add->ymin < src->ymin)
410 src->ymin = add->ymin;
411 if(add->xmax > src->xmax)
412 src->xmax = add->xmax;
413 if(add->ymax > src->ymax)
414 src->ymax = add->ymax;
416 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
419 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
420 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
423 SRECT swf_TurnRect(SRECT r, MATRIX* m)
426 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
427 p1.x = r.xmin;p1.y = r.ymin;
428 p2.x = r.xmax;p2.y = r.ymin;
429 p3.x = r.xmin;p3.y = r.ymax;
430 p4.x = r.xmax;p4.y = r.ymax;
431 pp1 = swf_TurnPoint(p1, m);
432 pp2 = swf_TurnPoint(p2, m);
433 pp3 = swf_TurnPoint(p3, m);
434 pp4 = swf_TurnPoint(p4, m);
435 g.xmin = g.xmax = pp1.x;
436 g.ymin = g.ymax = pp1.y;
437 swf_ExpandRect(&g, pp2);
438 swf_ExpandRect(&g, pp3);
439 swf_ExpandRect(&g, pp4);
444 int swf_GetMatrix(TAG * t,MATRIX * m)
451 { m->sx = m->sy = 0x10000;
457 swf_ResetReadBits(t);
459 if (swf_GetBits(t,1))
460 { nbits = swf_GetBits(t,5);
461 m->sx = swf_GetSBits(t,nbits);
462 m->sy = swf_GetSBits(t,nbits);
464 else m->sx = m->sy = 0x10000;
466 if (swf_GetBits(t,1))
467 { nbits = swf_GetBits(t,5);
468 m->r0 = swf_GetSBits(t,nbits);
469 m->r1 = swf_GetSBits(t,nbits);
471 else m->r0 = m->r1 = 0x0;
473 nbits = swf_GetBits(t,5);
474 m->tx = swf_GetSBits(t,nbits);
475 m->ty = swf_GetSBits(t,nbits);
480 int swf_SetMatrix(TAG * t,MATRIX * m)
486 ma.sx = ma.sy = 0x10000;
491 swf_ResetWriteBits(t);
493 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
495 { swf_SetBits(t,1,1);
496 nbits = swf_CountBits(m->sx,0);
497 nbits = swf_CountBits(m->sy,nbits);
499 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
502 swf_SetBits(t,nbits,5);
503 swf_SetBits(t,m->sx,nbits);
504 swf_SetBits(t,m->sy,nbits);
507 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
509 { swf_SetBits(t,1,1);
510 nbits = swf_CountBits(m->r0,0);
511 nbits = swf_CountBits(m->r1,nbits);
513 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
516 swf_SetBits(t,nbits,5);
517 swf_SetBits(t,m->r0,nbits);
518 swf_SetBits(t,m->r1,nbits);
521 nbits = swf_CountBits(m->tx,0);
522 nbits = swf_CountBits(m->ty,nbits);
524 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
527 swf_SetBits(t,nbits,5);
528 swf_SetBits(t,m->tx,nbits);
529 swf_SetBits(t,m->ty,nbits);
534 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha) //FIXME: alpha should be type bool
542 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
543 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
547 swf_ResetReadBits(t);
548 hasadd = swf_GetBits(t,1);
549 hasmul = swf_GetBits(t,1);
550 nbits = swf_GetBits(t,4);
553 { cx->r0 = (S16)swf_GetSBits(t,nbits);
554 cx->g0 = (S16)swf_GetSBits(t,nbits);
555 cx->b0 = (S16)swf_GetSBits(t,nbits);
557 cx->a0 = (S16)swf_GetSBits(t,nbits);
561 { cx->r1 = (S16)swf_GetSBits(t,nbits);
562 cx->g1 = (S16)swf_GetSBits(t,nbits);
563 cx->b1 = (S16)swf_GetSBits(t,nbits);
565 cx->a1 = (S16)swf_GetSBits(t,nbits);
571 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
579 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
580 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
590 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
591 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
594 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
595 nbits = swf_CountBits((S32)cx->r0,nbits);
596 nbits = swf_CountBits((S32)cx->g0,nbits);
597 nbits = swf_CountBits((S32)cx->b0,nbits);
601 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
602 nbits = swf_CountBits((S32)cx->r1,nbits);
603 nbits = swf_CountBits((S32)cx->g1,nbits);
604 nbits = swf_CountBits((S32)cx->b1,nbits);
607 swf_ResetWriteBits(t);
608 swf_SetBits(t,hasadd?1:0,1);
609 swf_SetBits(t,hasmul?1:0,1);
610 swf_SetBits(t,nbits,4);
613 { swf_SetBits(t,cx->r0,nbits);
614 swf_SetBits(t,cx->g0,nbits);
615 swf_SetBits(t,cx->b0,nbits);
616 if (alpha) swf_SetBits(t,cx->a0,nbits);
620 { swf_SetBits(t,cx->r1,nbits);
621 swf_SetBits(t,cx->g1,nbits);
622 swf_SetBits(t,cx->b1,nbits);
623 if (alpha) swf_SetBits(t,cx->a1,nbits);
629 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
630 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
632 void swf_SetPassword(TAG * t, const char * password)
634 /* WARNING: crypt_md5 is not reentrant */
635 char* md5string = crypt_md5(password, "salt"); /* FIXME- get random salt */
636 swf_SetString(t, md5string);
639 int swf_VerifyPassword(TAG * t, const char * password)
641 char*md5string1, *md5string2;
646 md5string1 = swf_GetString(t);
648 if(!strncmp(md5string1, "$1$",3 )) {
651 x = strchr(md5string1+3, '$');
654 n = x-(md5string1+3);
655 salt = (char*)malloc(n+1);
656 memcpy(salt, md5string1+3, n);
659 md5string2 = crypt_md5(password, salt);
661 if(strcmp(md5string1, md5string2) != 0)
666 // Tag List Manipulating Functions
668 TAG * swf_InsertTag(TAG * after,U16 id)
671 t = (TAG *)malloc(sizeof(TAG));
673 { memset(t,0x00,sizeof(TAG));
679 t->next = after->next;
681 if (t->next) t->next->prev = t;
687 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
690 t = (TAG *)malloc(sizeof(TAG));
692 { memset(t,0x00,sizeof(TAG));
698 t->prev = before->prev;
700 if (t->prev) t->prev->next = t;
703 if(swf && swf->firstTag == before) {
709 void swf_ClearTag(TAG * t)
711 if (t->data) free(t->data);
720 void swf_ResetTag(TAG*tag, U16 id)
722 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
726 int swf_DeleteTag(TAG * t)
729 if (t->prev) t->prev->next = t->next;
730 if (t->next) t->next->prev = t->prev;
732 if (t->data) free(t->data);
737 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
743 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
751 if (reader->read(reader, &len, 4) != 4) return NULL;
755 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
756 // Sprite handling fix: Flatten sprite tree
758 t = (TAG *)malloc(sizeof(TAG));
763 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (2). (%d bytes)\n", sizeof(TAG));
768 memset(t,0x00,sizeof(TAG));
774 { t->data = (U8*)malloc(t->len);
778 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (3). (%d bytes)\n", t->len);
783 if (reader->read(reader, t->data, t->len) != t->len) return NULL;
795 int swf_DefineSprite_GetRealSize(TAG * t);
797 int swf_WriteTag2(struct writer_t*writer, TAG * t)
798 // returns tag length in bytes (incl. Header), -1 = Error
799 // writer = 0 -> no output
806 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
808 short_tag = len<0x3f;
812 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
813 if (writer->write(writer,raw,2)!=2)
816 fprintf(stderr,"WriteTag() failed: Short Header.\n");
823 raw[0] = SWAP16((t->id<<6)|0x3f);
824 if (writer->write(writer,raw,2)!=2)
827 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
833 if (writer->write(writer,&len,4)!=4)
836 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
843 { if (writer->write(writer,t->data,t->len)!=t->len)
846 fprintf(stderr,"WriteTag() failed: Data.\n");
852 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
856 return t->len+(short_tag?2:6);
859 int swf_WriteTag(int handle, TAG * t)
861 struct writer_t writer;
863 return swf_WriteTag2(0, t);
864 writer_init_filewriter(&writer, handle);
865 return swf_WriteTag2(&writer, t);
868 int swf_DefineSprite_GetRealSize(TAG * t)
869 // Sprite Handling: Helper function to pack DefineSprite-Tag
871 if(len>4) { // folded sprite
875 { t = swf_NextTag(t);
876 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
878 } while (t&&(t->id!=ST_END));
882 void swf_UnFoldSprite(TAG * t)
887 U16 spriteid,spriteframes;
889 if(t->id!=ST_DEFINESPRITE)
891 if(t->len<=4) // not folded
896 spriteid = swf_GetU16(t); //id
897 spriteframes = swf_GetU16(t); //frames
909 if(id == ST_DEFINESPRITE && len<=4)
914 it = swf_InsertTag(next, id);
919 { it->data = (U8*)malloc(it->len);
920 it->memsize = it->len;
921 swf_GetBlock(t, it->data, it->len);
928 free(t->data); t->data = 0;
929 t->memsize = t->len = t->pos = 0;
931 swf_SetU16(t, spriteid);
932 swf_SetU16(t, spriteframes);
935 void swf_FoldSprite(TAG * t)
940 if(t->id!=ST_DEFINESPRITE)
943 fprintf(stderr, "Error: Sprite has no ID!");
947 /* sprite is already folded */
954 t->len = t->pos = t->memsize = 0;
959 t = swf_NextTag(sprtag);
964 if(t->id==ST_SHOWFRAME) frames++;
965 if(t->id == ST_DEFINESPRITE && t->len<=4)
972 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
974 swf_SetU16(sprtag, id);
975 swf_SetU16(sprtag, frames);
977 t = swf_NextTag(sprtag);
983 swf_SetU16(sprtag,t->len|(t->id<<6));
985 swf_SetU16(sprtag,0x3f|(t->id<<6));
986 swf_SetU32(sprtag,t->len);
989 swf_SetBlock(sprtag,t->data, t->len);
991 if(t->id == ST_DEFINESPRITE && t->len<=4)
1000 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1002 // sprtag->next = t;
1003 // t->prev = sprtag;
1006 int swf_IsFolded(TAG * t)
1008 return (t->id == ST_DEFINESPRITE && t->len>4);
1011 void swf_FoldAll(SWF*swf)
1013 TAG*tag = swf->firstTag;
1014 //swf_DumpSWF(stdout, swf);
1016 if(tag->id == ST_DEFINESPRITE) {
1017 swf_FoldSprite(tag);
1018 //swf_DumpSWF(stdout, swf);
1020 tag = swf_NextTag(tag);
1024 void swf_UnFoldAll(SWF*swf)
1026 TAG*tag = swf->firstTag;
1028 if(tag->id == ST_DEFINESPRITE)
1029 swf_UnFoldSprite(tag);
1034 void swf_OptimizeTagOrder(SWF*swf)
1041 /* at the moment, we don't actually do optimizing,
1042 only fixing of non-spec-conformant things like
1049 tag = swf->firstTag;
1052 if(tag->id == ST_DEFINESPRITE) {
1054 /* ??? all sprites are supposed to be unfolded */
1055 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1065 /* move non-sprite tags out of sprite */
1066 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1067 /* remove tag from current position */
1068 tag->prev->next = tag->next;
1070 tag->next->prev = tag->prev;
1072 /* insert before tag level0 */
1074 tag->prev = level0->prev;
1076 tag->prev->next = tag;
1080 if(tag->id == ST_END) {
1091 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1093 if (!swf) return -1;
1094 memset(swf,0x00,sizeof(SWF));
1096 { char b[32]; // read Header
1100 struct reader_t zreader;
1102 if ((len = reader->read(reader ,b,8))<8) return -1;
1104 if (b[0]!='F' && b[0]!='C') return -1;
1105 if (b[1]!='W') return -1;
1106 if (b[2]!='S') return -1;
1107 swf->fileVersion = b[3];
1108 swf->compressed = (b[0]=='C')?1:0;
1109 swf->fileSize = GET32(&b[4]);
1111 if(swf->compressed) {
1112 reader_init_zlibinflate(&zreader, reader);
1116 reader_GetRect(reader, &swf->movieSize);
1117 reader->read(reader, &swf->frameRate, 2);
1118 swf->frameRate = SWAP16(swf->frameRate);
1119 reader->read(reader, &swf->frameCount, 2);
1120 swf->frameCount = SWAP16(swf->frameCount);
1122 /* read tags and connect to list */
1124 while (t) t = swf_ReadTag(reader,t);
1125 swf->firstTag = t1.next;
1126 t1.next->prev = NULL;
1132 int swf_ReadSWF(int handle, SWF * swf)
1134 struct reader_t reader;
1135 reader_init_filereader(&reader, handle);
1136 return swf_ReadSWF2(&reader, swf);
1139 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1143 struct writer_t zwriter;
1146 if (!swf) return -1;
1148 // Insert REFLEX Tag
1150 #ifdef INSERT_RFX_TAG
1152 if (swf->firstTag && swf_NextTag(swf->firstTag))
1153 if (swf_GetTagID(swf_NextTag(swf->firstTag))!=ST_REFLEX)
1154 swf_SetBlock(swf_InsertTag(swf->firstTag,ST_REFLEX),"rfx",3);
1156 #endif // INSERT_RFX_TAG
1158 // Count Frames + File Size
1165 { len += swf_WriteTag(-1, t);
1166 if (t->id==ST_SHOWFRAME) frameCount++;
1174 memset(&t1,0x00,sizeof(TAG));
1178 { // measure header file size
1181 memset(&t2,0x00,sizeof(TAG));
1184 swf_SetRect(&t2, &swf->movieSize);
1185 swf_SetU16(&t2, swf->frameRate);
1186 swf_SetU16(&t2, swf->frameCount);
1187 l = swf_GetTagLen(&t2)+8;
1189 if(swf->compressed == 8) {
1194 if(len) {// don't touch headers without tags
1195 swf->fileSize = fileSize;
1196 swf->frameCount = frameCount;
1199 if(swf->compressed != 8) {
1200 /* compressed flag set to 8 means "skip first 8
1201 header bytes". This is necessary if the caller wants to
1202 create compressed SWFs himself */
1203 if(swf->compressed) {
1205 writer->write(writer, id, 3);
1209 writer->write(writer, id, 3);
1212 writer->write(writer, &swf->fileVersion, 1);
1213 PUT32(b4, swf->fileSize);
1214 writer->write(writer, b4, 4);
1216 if(swf->compressed) {
1217 writer_init_zlibdeflate(&zwriter, writer);
1222 swf_SetRect(&t1,&swf->movieSize);
1223 swf_SetU16(&t1,swf->frameRate);
1224 swf_SetU16(&t1,swf->frameCount);
1228 int ret = writer->write(writer,b,swf_GetTagLen(&t1));
1229 if (ret!=swf_GetTagLen(&t1))
1232 fprintf(stderr, "ret:%d\n",ret);
1234 fprintf(stderr,"WriteSWF() failed: Header.\n");
1241 { if (swf_WriteTag2(writer, t)<0) return -1;
1244 if(swf->compressed != 8)
1245 writer->finish(writer); // flush zlib buffers - only if _we_ initialized that writer.
1248 return (int)fileSize;
1251 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1253 struct writer_t writer;
1254 swf->compressed = 0;
1256 writer_init_nullwriter(&writer);
1257 return swf_WriteSWF2(&writer, swf);
1259 writer_init_filewriter(&writer, handle);
1260 return swf_WriteSWF2(&writer, swf);
1263 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1265 struct writer_t writer;
1266 swf->compressed = 1;
1268 writer_init_nullwriter(&writer);
1269 return swf_WriteSWF2(&writer, swf);
1271 writer_init_filewriter(&writer, handle);
1272 return swf_WriteSWF2(&writer, swf);
1275 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1278 memcpy(&myswf,swf,sizeof(SWF));
1280 return swf_WriteSWF2(writer, &myswf);
1283 int swf_WriteHeader(int handle,SWF * swf)
1286 memcpy(&myswf,swf,sizeof(SWF));
1288 return swf_WriteSWF(handle, &myswf);
1291 int swf_WriteCGI(SWF * swf)
1295 len = swf_WriteSWF(-1,swf);
1297 if (len<0) return -1;
1299 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1300 "Accept-Ranges: bytes\n"
1301 "Content-Length: %lu\n"
1302 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1305 write(fileno(stdout),s,strlen(s));
1306 return swf_WriteSWF(fileno(stdout),swf);
1309 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1310 { TAG * t = swf->firstTag;
1313 { TAG * tnew = t->next;
1314 if (t->data) free(t->data);
1320 // include advanced functions
1322 #include "modules/swfdump.c"
1323 #include "modules/swfshape.c"
1324 #include "modules/swftext.c"
1325 #include "modules/swfobject.c"
1326 #include "modules/swfbutton.c"
1327 #include "modules/swftools.c"
1328 #include "modules/swfcgi.c"
1329 #include "modules/swfbits.c"
1330 #include "modules/swfaction.c"
1331 #include "modules/swfsound.c"