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"
45 #define MALLOC_SIZE 128
46 #define INSERT_RFX_TAG
48 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
51 // inline wrapper functions
53 TAG * swf_NextTag(TAG * t) { return t->next; }
54 TAG * swf_PrevTag(TAG * t) { return t->prev; }
55 U16 swf_GetTagID(TAG * t) { return t->id; }
56 U32 swf_GetTagLen(TAG * t) { return t->len; }
57 U8* swf_GetTagLenPtr(TAG * t) { return &(t->data[t->len]); }
58 U32 swf_GetTagPos(TAG * t) { return t->pos; }
60 // Basic Data Access Functions
62 #define swf_ResetReadBits(tag) if (tag->readBit) { tag->pos++; tag->readBit = 0; }
63 #define swf_ResetWriteBits(tag) if (tag->writeBit) { tag->writeBit = 0; }
65 // for future purpose: avoid high level lib functions to change tagpos/bitpos
67 #define swf_SaveTagPos(tag)
68 #define swf_RestoreTagPos(tag)
70 void swf_SetTagPos(TAG * t,U32 pos)
71 { swf_ResetReadBits(t);
72 if (pos<=t->len) t->pos = pos;
74 else fprintf(stderr,"SetTagPos() out of bounds: TagID = %i\n",t->id);
78 char* swf_GetString(TAG*t)
80 char* str = ((char*)(&(t)->data[(t)->pos]));
86 { swf_ResetReadBits(t);
89 { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
93 return t->data[t->pos++];
96 U16 swf_GetU16(TAG * t)
100 if (t->pos>(t->len-2))
101 { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
105 res = t->data[t->pos] | (t->data[t->pos+1]<<8);
110 U32 swf_GetU32(TAG * t)
112 swf_ResetReadBits(t);
114 if (t->pos>(t->len-4))
115 { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
119 res = t->data[t->pos] | (t->data[t->pos+1]<<8) |
120 (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
125 int swf_GetBlock(TAG * t,U8 * b,int l)
126 // returns number of bytes written (<=l)
127 // b = NULL -> skip data
128 { swf_ResetReadBits(t);
129 if ((t->len-t->pos)<l) l=t->len-t->pos;
130 if (b && l) memcpy(b,&t->data[t->pos],l);
135 int swf_SetBlock(TAG * t,U8 * b,int l)
136 // Appends Block to the end of Tagdata, returns size
137 { U32 newlen = t->len + l;
138 swf_ResetWriteBits(t);
139 if (newlen>t->memsize)
140 { U32 newmem = MEMSIZE(newlen);
141 U8 * newdata = (U8*)((t->data)?realloc(t->data,newmem):malloc(newmem));
145 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (1). (%d bytes)\n", newmem);
153 if (b) memcpy(&t->data[t->len],b,l);
154 else memset(&t->data[t->len],0x00,l);
159 int swf_SetU8(TAG * t,U8 v)
160 { swf_ResetWriteBits(t);
161 if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
162 t->data[t->len++] = v;
166 int swf_SetU16(TAG * t,U16 v)
171 swf_ResetWriteBits(t);
172 if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
173 t->data[t->len++] = a[0];
174 t->data[t->len++] = a[1];
178 int swf_SetU32(TAG * t,U32 v)
180 a[0] = v&0xff; // to ensure correct handling of non-intel byteorder
185 swf_ResetWriteBits(t);
186 if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
187 t->data[t->len++] = a[0];
188 t->data[t->len++] = a[1];
189 t->data[t->len++] = a[2];
190 t->data[t->len++] = a[3];
194 U32 swf_GetBits(TAG * t,int nbits)
196 if (!nbits) return 0;
197 if (!t->readBit) t->readBit = 0x80;
200 if (t->data[t->pos]&t->readBit) res|=1;
204 { if (nbits) t->readBit = 0x80;
207 { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
217 S32 swf_GetSBits(TAG * t,int nbits)
218 { U32 res = swf_GetBits(t,nbits);
219 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
223 U32 reader_GetBits(struct reader_t*reader, int nbits)
224 { return reader_readbits(reader, nbits);
226 S32 reader_GetSBits(struct reader_t*reader, int nbits)
227 { U32 res = reader_readbits(reader, nbits);
228 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
232 int swf_SetBits(TAG * t,U32 v,int nbits)
233 { U32 bm = 1<<(nbits-1);
237 { if (FAILED(swf_SetU8(t,0))) return -1;
240 if (v&bm) t->data[t->len-1] |= t->writeBit;
248 // Advanced Data Access Functions
250 int swf_SetRGB(TAG * t,RGBA * col)
253 { swf_SetU8(t,col->r);
256 } else swf_SetBlock(t,NULL,3);
259 void swf_GetRGB(TAG * t, RGBA * col)
264 col->r = swf_GetU8(t);
265 col->g = swf_GetU8(t);
266 col->b = swf_GetU8(t);
270 int swf_SetRGBA(TAG * t,RGBA * col)
273 { swf_SetU8(t,col->r);
277 } else swf_SetBlock(t,NULL,4);
280 void swf_GetRGBA(TAG * t, RGBA * col)
285 col->r = swf_GetU8(t);
286 col->g = swf_GetU8(t);
287 col->b = swf_GetU8(t);
288 col->a = swf_GetU8(t);
291 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
297 gradient->num = swf_GetU8(tag);
298 for(t=0;t<gradient->num;t++)
303 gradient->ratios[t] = swf_GetU8(tag);
305 swf_GetRGB(tag, &gradient->rgba[t]);
307 swf_GetRGBA(tag, &gradient->rgba[t]);
311 int swf_CountUBits(U32 v,int nbits)
314 if(v == 0x00000000) n = 0;
320 return (n>nbits)?n:nbits;
323 int swf_CountBits(U32 v,int nbits)
327 { if(v == 0xffffffff) n = 1;
335 { if(v == 0x00000000) n = 0;
342 return (n>nbits)?n:nbits;
345 int swf_GetRect(TAG * t,SRECT * r)
348 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
350 nbits = (int) swf_GetBits(t,5);
351 r->xmin = swf_GetSBits(t,nbits);
352 r->xmax = swf_GetSBits(t,nbits);
353 r->ymin = swf_GetSBits(t,nbits);
354 r->ymax = swf_GetSBits(t,nbits);
358 int reader_GetRect(struct reader_t*reader,SRECT * r)
362 nbits = (int) reader_GetBits(reader,5);
363 r->xmin = reader_GetSBits(reader,nbits);
364 r->xmax = reader_GetSBits(reader,nbits);
365 r->ymin = reader_GetSBits(reader,nbits);
366 r->ymax = reader_GetSBits(reader,nbits);
370 int swf_SetRect(TAG * t,SRECT * r)
373 nbits = swf_CountBits(r->xmin,0);
374 nbits = swf_CountBits(r->xmax,nbits);
375 nbits = swf_CountBits(r->ymin,nbits);
376 nbits = swf_CountBits(r->ymax,nbits);
378 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
382 swf_SetBits(t,nbits,5);
383 swf_SetBits(t,r->xmin,nbits);
384 swf_SetBits(t,r->xmax,nbits);
385 swf_SetBits(t,r->ymin,nbits);
386 swf_SetBits(t,r->ymax,nbits);
391 void swf_ExpandRect(SRECT*src, SPOINT add)
393 if(add.x < src->xmin)
395 if(add.x > src->xmax)
397 if(add.y < src->ymin)
399 if(add.y > src->ymax)
402 void swf_ExpandRect2(SRECT*src, SRECT*add)
404 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
406 if(add->xmin < src->xmin)
407 src->xmin = add->xmin;
408 if(add->ymin < src->ymin)
409 src->ymin = add->ymin;
410 if(add->xmax > src->xmax)
411 src->xmax = add->xmax;
412 if(add->ymax > src->ymax)
413 src->ymax = add->ymax;
415 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
418 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
419 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
422 SRECT swf_TurnRect(SRECT r, MATRIX* m)
425 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
426 p1.x = r.xmin;p1.y = r.ymin;
427 p2.x = r.xmax;p2.y = r.ymin;
428 p3.x = r.xmin;p3.y = r.ymax;
429 p4.x = r.xmax;p4.y = r.ymax;
430 pp1 = swf_TurnPoint(p1, m);
431 pp2 = swf_TurnPoint(p2, m);
432 pp3 = swf_TurnPoint(p3, m);
433 pp4 = swf_TurnPoint(p4, m);
434 g.xmin = g.xmax = pp1.x;
435 g.ymin = g.ymax = pp1.y;
436 swf_ExpandRect(&g, pp2);
437 swf_ExpandRect(&g, pp3);
438 swf_ExpandRect(&g, pp4);
443 int swf_GetMatrix(TAG * t,MATRIX * m)
450 { m->sx = m->sy = 0x10000;
456 swf_ResetReadBits(t);
458 if (swf_GetBits(t,1))
459 { nbits = swf_GetBits(t,5);
460 m->sx = swf_GetSBits(t,nbits);
461 m->sy = swf_GetSBits(t,nbits);
463 else m->sx = m->sy = 0x10000;
465 if (swf_GetBits(t,1))
466 { nbits = swf_GetBits(t,5);
467 m->r0 = swf_GetSBits(t,nbits);
468 m->r1 = swf_GetSBits(t,nbits);
470 else m->r0 = m->r1 = 0x0;
472 nbits = swf_GetBits(t,5);
473 m->tx = swf_GetSBits(t,nbits);
474 m->ty = swf_GetSBits(t,nbits);
479 int swf_SetMatrix(TAG * t,MATRIX * m)
485 ma.sx = ma.sy = 0x10000;
490 swf_ResetWriteBits(t);
492 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
494 { swf_SetBits(t,1,1);
495 nbits = swf_CountBits(m->sx,0);
496 nbits = swf_CountBits(m->sy,nbits);
498 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
501 swf_SetBits(t,nbits,5);
502 swf_SetBits(t,m->sx,nbits);
503 swf_SetBits(t,m->sy,nbits);
506 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
508 { swf_SetBits(t,1,1);
509 nbits = swf_CountBits(m->r0,0);
510 nbits = swf_CountBits(m->r1,nbits);
512 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
515 swf_SetBits(t,nbits,5);
516 swf_SetBits(t,m->r0,nbits);
517 swf_SetBits(t,m->r1,nbits);
520 nbits = swf_CountBits(m->tx,0);
521 nbits = swf_CountBits(m->ty,nbits);
523 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
526 swf_SetBits(t,nbits,5);
527 swf_SetBits(t,m->tx,nbits);
528 swf_SetBits(t,m->ty,nbits);
533 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha) //FIXME: alpha should be type bool
541 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
542 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
546 swf_ResetReadBits(t);
547 hasadd = swf_GetBits(t,1);
548 hasmul = swf_GetBits(t,1);
549 nbits = swf_GetBits(t,4);
552 { cx->r0 = (S16)swf_GetSBits(t,nbits);
553 cx->g0 = (S16)swf_GetSBits(t,nbits);
554 cx->b0 = (S16)swf_GetSBits(t,nbits);
556 cx->a0 = (S16)swf_GetSBits(t,nbits);
560 { cx->r1 = (S16)swf_GetSBits(t,nbits);
561 cx->g1 = (S16)swf_GetSBits(t,nbits);
562 cx->b1 = (S16)swf_GetSBits(t,nbits);
564 cx->a1 = (S16)swf_GetSBits(t,nbits);
570 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
578 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
579 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
589 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
590 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
593 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
594 nbits = swf_CountBits((S32)cx->r0,nbits);
595 nbits = swf_CountBits((S32)cx->g0,nbits);
596 nbits = swf_CountBits((S32)cx->b0,nbits);
600 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
601 nbits = swf_CountBits((S32)cx->r1,nbits);
602 nbits = swf_CountBits((S32)cx->g1,nbits);
603 nbits = swf_CountBits((S32)cx->b1,nbits);
606 swf_ResetWriteBits(t);
607 swf_SetBits(t,hasadd?1:0,1);
608 swf_SetBits(t,hasmul?1:0,1);
609 swf_SetBits(t,nbits,4);
612 { swf_SetBits(t,cx->r0,nbits);
613 swf_SetBits(t,cx->g0,nbits);
614 swf_SetBits(t,cx->b0,nbits);
615 if (alpha) swf_SetBits(t,cx->a0,nbits);
619 { swf_SetBits(t,cx->r1,nbits);
620 swf_SetBits(t,cx->g1,nbits);
621 swf_SetBits(t,cx->b1,nbits);
622 if (alpha) swf_SetBits(t,cx->a1,nbits);
628 int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
629 int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
631 // Tag List Manipulating Functions
633 TAG * swf_InsertTag(TAG * after,U16 id)
636 t = (TAG *)malloc(sizeof(TAG));
638 { memset(t,0x00,sizeof(TAG));
644 t->next = after->next;
646 if (t->next) t->next->prev = t;
652 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
655 t = (TAG *)malloc(sizeof(TAG));
657 { memset(t,0x00,sizeof(TAG));
663 t->prev = before->prev;
665 if (t->prev) t->prev->next = t;
668 if(swf && swf->firstTag == before) {
674 void swf_ClearTag(TAG * t)
676 if (t->data) free(t->data);
685 void swf_ResetTag(TAG*tag, U16 id)
687 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
691 int swf_DeleteTag(TAG * t)
694 if (t->prev) t->prev->next = t->next;
695 if (t->next) t->next->prev = t->prev;
697 if (t->data) free(t->data);
702 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
708 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
716 if (reader->read(reader, &len, 4) != 4) return NULL;
720 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
721 // Sprite handling fix: Flatten sprite tree
723 t = (TAG *)malloc(sizeof(TAG));
728 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (2). (%d bytes)\n", sizeof(TAG));
733 memset(t,0x00,sizeof(TAG));
739 { t->data = (U8*)malloc(t->len);
743 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (3). (%d bytes)\n", t->len);
748 if (reader->read(reader, t->data, t->len) != t->len) return NULL;
760 int swf_DefineSprite_GetRealSize(TAG * t);
762 int swf_WriteTag2(struct writer_t*writer, TAG * t)
763 // returns tag length in bytes (incl. Header), -1 = Error
764 // writer = 0 -> no output
771 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
773 short_tag = len<0x3f;
777 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
778 if (writer->write(writer,raw,2)!=2)
781 fprintf(stderr,"WriteTag() failed: Short Header.\n");
788 raw[0] = SWAP16((t->id<<6)|0x3f);
789 if (writer->write(writer,raw,2)!=2)
792 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
798 if (writer->write(writer,&len,4)!=4)
801 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
808 { if (writer->write(writer,t->data,t->len)!=t->len)
811 fprintf(stderr,"WriteTag() failed: Data.\n");
817 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
821 return t->len+(short_tag?2:6);
824 int swf_WriteTag(int handle, TAG * t)
826 struct writer_t writer;
828 return swf_WriteTag2(0, t);
829 writer_init_filewriter(&writer, handle);
830 return swf_WriteTag2(&writer, t);
833 int swf_DefineSprite_GetRealSize(TAG * t)
834 // Sprite Handling: Helper function to pack DefineSprite-Tag
836 if(len>4) { // folded sprite
840 { t = swf_NextTag(t);
841 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
843 } while (t&&(t->id!=ST_END));
847 void swf_UnFoldSprite(TAG * t)
852 U16 spriteid,spriteframes;
854 if(t->id!=ST_DEFINESPRITE)
856 if(t->len<=4) // not folded
861 spriteid = swf_GetU16(t); //id
862 spriteframes = swf_GetU16(t); //frames
874 if(id == ST_DEFINESPRITE && len<=4)
879 it = swf_InsertTag(next, id);
884 { it->data = (U8*)malloc(it->len);
885 it->memsize = it->len;
886 swf_GetBlock(t, it->data, it->len);
893 free(t->data); t->data = 0;
894 t->memsize = t->len = t->pos = 0;
896 swf_SetU16(t, spriteid);
897 swf_SetU16(t, spriteframes);
900 void swf_FoldSprite(TAG * t)
905 if(t->id!=ST_DEFINESPRITE)
908 fprintf(stderr, "Error: Sprite has no ID!");
912 /* sprite is already folded */
919 t->len = t->pos = t->memsize = 0;
924 t = swf_NextTag(sprtag);
929 if(t->id==ST_SHOWFRAME) frames++;
930 if(t->id == ST_DEFINESPRITE && t->len<=4)
937 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
939 swf_SetU16(sprtag, id);
940 swf_SetU16(sprtag, frames);
942 t = swf_NextTag(sprtag);
948 swf_SetU16(sprtag,t->len|(t->id<<6));
950 swf_SetU16(sprtag,0x3f|(t->id<<6));
951 swf_SetU32(sprtag,t->len);
954 swf_SetBlock(sprtag,t->data, t->len);
956 if(t->id == ST_DEFINESPRITE && t->len<=4)
965 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
971 int swf_IsFolded(TAG * t)
973 return (t->id == ST_DEFINESPRITE && t->len>4);
976 void swf_FoldAll(SWF*swf)
978 TAG*tag = swf->firstTag;
979 //swf_DumpSWF(stdout, swf);
981 if(tag->id == ST_DEFINESPRITE) {
983 //swf_DumpSWF(stdout, swf);
985 tag = swf_NextTag(tag);
989 void swf_UnFoldAll(SWF*swf)
991 TAG*tag = swf->firstTag;
993 if(tag->id == ST_DEFINESPRITE)
994 swf_UnFoldSprite(tag);
999 void swf_OptimizeTagOrder(SWF*swf)
1006 /* at the moment, we don't actually do optimizing,
1007 only fixing of non-spec-conformant things like
1014 tag = swf->firstTag;
1017 if(tag->id == ST_DEFINESPRITE) {
1019 /* ??? all sprites are supposed to be unfolded */
1020 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1030 /* move non-sprite tags out of sprite */
1031 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1032 /* remove tag from current position */
1033 tag->prev->next = tag->next;
1035 tag->next->prev = tag->prev;
1037 /* insert before tag level0 */
1039 tag->prev = level0->prev;
1041 tag->prev->next = tag;
1045 if(tag->id == ST_END) {
1056 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1058 if (!swf) return -1;
1059 memset(swf,0x00,sizeof(SWF));
1061 { char b[32]; // read Header
1065 struct reader_t zreader;
1067 if ((len = reader->read(reader ,b,8))<8) return -1;
1069 if (b[0]!='F' && b[0]!='C') return -1;
1070 if (b[1]!='W') return -1;
1071 if (b[2]!='S') return -1;
1072 swf->fileVersion = b[3];
1073 swf->compressed = (b[0]=='C')?1:0;
1074 swf->fileSize = GET32(&b[4]);
1076 if(swf->compressed) {
1077 reader_init_zlibinflate(&zreader, reader);
1081 reader_GetRect(reader, &swf->movieSize);
1082 reader->read(reader, &swf->frameRate, 2);
1083 swf->frameRate = SWAP16(swf->frameRate);
1084 reader->read(reader, &swf->frameCount, 2);
1085 swf->frameCount = SWAP16(swf->frameCount);
1087 /* read tags and connect to list */
1089 while (t) t = swf_ReadTag(reader,t);
1090 swf->firstTag = t1.next;
1091 t1.next->prev = NULL;
1097 int swf_ReadSWF(int handle, SWF * swf)
1099 struct reader_t reader;
1100 reader_init_filereader(&reader, handle);
1101 return swf_ReadSWF2(&reader, swf);
1104 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1108 struct writer_t zwriter;
1111 if (!swf) return -1;
1113 // Insert REFLEX Tag
1115 #ifdef INSERT_RFX_TAG
1117 if (swf->firstTag && swf_NextTag(swf->firstTag))
1118 if (swf_GetTagID(swf_NextTag(swf->firstTag))!=ST_REFLEX)
1119 swf_SetBlock(swf_InsertTag(swf->firstTag,ST_REFLEX),"rfx",3);
1121 #endif // INSERT_RFX_TAG
1123 // Count Frames + File Size
1130 { len += swf_WriteTag(-1, t);
1131 if (t->id==ST_SHOWFRAME) frameCount++;
1139 memset(&t1,0x00,sizeof(TAG));
1143 { // measure header file size
1146 memset(&t2,0x00,sizeof(TAG));
1149 swf_SetRect(&t2, &swf->movieSize);
1150 swf_SetU16(&t2, swf->frameRate);
1151 swf_SetU16(&t2, swf->frameCount);
1152 l = swf_GetTagLen(&t2)+8;
1154 if(swf->compressed == 8) {
1159 if(len) {// don't touch headers without tags
1160 swf->fileSize = fileSize;
1161 swf->frameCount = frameCount;
1164 if(swf->compressed != 8) {
1165 /* compressed flag set to 8 means "skip first 8
1166 header bytes". This is necessary if the caller wants to
1167 create compressed SWFs himself */
1168 if(swf->compressed) {
1170 writer->write(writer, id, 3);
1174 writer->write(writer, id, 3);
1177 writer->write(writer, &swf->fileVersion, 1);
1178 PUT32(b4, swf->fileSize);
1179 writer->write(writer, b4, 4);
1181 if(swf->compressed) {
1182 writer_init_zlibdeflate(&zwriter, writer);
1187 swf_SetRect(&t1,&swf->movieSize);
1188 swf_SetU16(&t1,swf->frameRate);
1189 swf_SetU16(&t1,swf->frameCount);
1193 int ret = writer->write(writer,b,swf_GetTagLen(&t1));
1194 if (ret!=swf_GetTagLen(&t1))
1197 fprintf(stderr, "ret:%d\n",ret);
1199 fprintf(stderr,"WriteSWF() failed: Header.\n");
1206 { if (swf_WriteTag2(writer, t)<0) return -1;
1209 if(swf->compressed != 8)
1210 writer->finish(writer); // flush zlib buffers - only if _we_ initialized that writer.
1213 return (int)fileSize;
1216 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1218 struct writer_t writer;
1219 swf->compressed = 0;
1221 writer_init_nullwriter(&writer);
1222 return swf_WriteSWF2(&writer, swf);
1224 writer_init_filewriter(&writer, handle);
1225 return swf_WriteSWF2(&writer, swf);
1228 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1230 struct writer_t writer;
1231 swf->compressed = 1;
1233 writer_init_nullwriter(&writer);
1234 return swf_WriteSWF2(&writer, swf);
1236 writer_init_filewriter(&writer, handle);
1237 return swf_WriteSWF2(&writer, swf);
1240 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1243 memcpy(&myswf,swf,sizeof(SWF));
1245 return swf_WriteSWF2(writer, &myswf);
1248 int swf_WriteHeader(int handle,SWF * swf)
1251 memcpy(&myswf,swf,sizeof(SWF));
1253 return swf_WriteSWF(handle, &myswf);
1256 int swf_WriteCGI(SWF * swf)
1260 len = swf_WriteSWF(-1,swf);
1262 if (len<0) return -1;
1264 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1265 "Accept-Ranges: bytes\n"
1266 "Content-Length: %lu\n"
1267 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1270 write(fileno(stdout),s,strlen(s));
1271 return swf_WriteSWF(fileno(stdout),swf);
1274 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1275 { TAG * t = swf->firstTag;
1278 { TAG * tnew = t->next;
1279 if (t->data) free(t->data);
1285 // include advanced functions
1287 #include "modules/swfdump.c"
1288 #include "modules/swfshape.c"
1289 #include "modules/swftext.c"
1290 #include "modules/swfobject.c"
1291 #include "modules/swfbutton.c"
1292 #include "modules/swftools.c"
1293 #include "modules/swfcgi.c"
1294 #include "modules/swfbits.c"
1295 #include "modules/swfaction.c"
1296 #include "modules/swfsound.c"