1 /* vi: set sts=2 sw=2 :*/
5 Library for creating and reading SWF files or parts of it.
6 There's a module directory which provides some extended functionality.
7 Most modules are included at the bottom of this file.
9 Part of the swftools package.
11 Copyright (c) 2000, 2001 Rainer Böhme <rfxswf@reflex-studio.de>
13 This file is distributed under the GPL, see file COPYING for details
22 #endif // HAVE_JPEGLIB
29 #include "lame/lame.h"
35 #define MALLOC_SIZE 128
36 #define INSERT_RFX_TAG
38 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
41 // inline wrapper functions
43 TAG * swf_NextTag(TAG * t) { return t->next; }
44 TAG * swf_PrevTag(TAG * t) { return t->prev; }
45 int swf_GetFrameNo(TAG * t) { return t->frame; }
46 U16 swf_GetTagID(TAG * t) { return t->id; }
47 U32 swf_GetTagLen(TAG * t) { return t->len; }
48 U8* swf_GetTagLenPtr(TAG * t) { return &(t->data[t->len]); }
49 U32 swf_GetTagPos(TAG * t) { return t->pos; }
51 // Basic Data Access Functions
53 #define swf_ResetReadBits(tag) if (tag->readBit) { tag->pos++; tag->readBit = 0; }
54 #define swf_ResetWriteBits(tag) if (tag->writeBit) { tag->writeBit = 0; }
56 // for future purpose: avoid high level lib functions to change tagpos/bitpos
58 #define swf_SaveTagPos(tag)
59 #define swf_RestoreTagPos(tag)
61 void swf_SetTagPos(TAG * t,U32 pos)
62 { swf_ResetReadBits(t);
63 if (pos<=t->len) t->pos = pos;
65 else fprintf(stderr,"SetTagPos() out of bounds: TagID = %i\n",t->id);
69 char* swf_GetString(TAG*t)
71 char* str = ((char*)(&(t)->data[(t)->pos]));
77 { swf_ResetReadBits(t);
80 { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
84 return t->data[t->pos++];
87 U16 swf_GetU16(TAG * t)
91 if (t->pos>(t->len-2))
92 { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
96 res = t->data[t->pos] | (t->data[t->pos+1]<<8);
101 U32 swf_GetU32(TAG * t)
103 swf_ResetReadBits(t);
105 if (t->pos>(t->len-4))
106 { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
110 res = t->data[t->pos] | (t->data[t->pos+1]<<8) |
111 (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
116 int swf_GetBlock(TAG * t,U8 * b,int l)
117 // returns number of bytes written (<=l)
118 // b = NULL -> skip data
119 { swf_ResetReadBits(t);
120 if ((t->len-t->pos)<l) l=t->len-t->pos;
121 if (b && l) memcpy(b,&t->data[t->pos],l);
126 int swf_SetBlock(TAG * t,U8 * b,int l)
127 // Appends Block to the end of Tagdata, returns size
128 { U32 newlen = t->len + l;
129 swf_ResetWriteBits(t);
130 if (newlen>t->memsize)
131 { U32 newmem = MEMSIZE(newlen);
132 U8 * newdata = (U8*)((t->data)?realloc(t->data,newmem):malloc(newmem));
136 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (1). (%d bytes)\n", newmem);
144 if (b) memcpy(&t->data[t->len],b,l);
145 else memset(&t->data[t->len],0x00,l);
150 int swf_SetU8(TAG * t,U8 v)
151 { swf_ResetWriteBits(t);
152 if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
153 t->data[t->len++] = v;
157 int swf_SetU16(TAG * t,U16 v)
162 swf_ResetWriteBits(t);
163 if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
164 t->data[t->len++] = a[0];
165 t->data[t->len++] = a[1];
169 int swf_SetU32(TAG * t,U32 v)
171 a[0] = v&0xff; // to ensure correct handling of non-intel byteorder
176 swf_ResetWriteBits(t);
177 if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
178 t->data[t->len++] = a[0];
179 t->data[t->len++] = a[1];
180 t->data[t->len++] = a[2];
181 t->data[t->len++] = a[3];
185 U32 swf_GetBits(TAG * t,int nbits)
187 if (!nbits) return 0;
188 if (!t->readBit) t->readBit = 0x80;
191 if (t->data[t->pos]&t->readBit) res|=1;
195 { if (nbits) t->readBit = 0x80;
198 { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
208 S32 swf_GetSBits(TAG * t,int nbits)
209 { U32 res = swf_GetBits(t,nbits);
210 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
214 U32 reader_GetBits(struct reader_t*reader, int nbits)
215 { return reader_readbits(reader, nbits);
217 S32 reader_GetSBits(struct reader_t*reader, int nbits)
218 { U32 res = reader_readbits(reader, nbits);
219 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
223 int swf_SetBits(TAG * t,U32 v,int nbits)
224 { U32 bm = 1<<(nbits-1);
228 { if (FAILED(swf_SetU8(t,0))) return -1;
231 if (v&bm) t->data[t->len-1] |= t->writeBit;
239 // Advanced Data Access Functions
241 int swf_SetRGB(TAG * t,RGBA * col)
244 { swf_SetU8(t,col->r);
247 } else swf_SetBlock(t,NULL,3);
250 void swf_GetRGB(TAG * t, RGBA * col)
255 col->r = swf_GetU8(t);
256 col->g = swf_GetU8(t);
257 col->b = swf_GetU8(t);
261 int swf_SetRGBA(TAG * t,RGBA * col)
264 { swf_SetU8(t,col->r);
268 } else swf_SetBlock(t,NULL,4);
271 void swf_GetRGBA(TAG * t, RGBA * col)
276 col->r = swf_GetU8(t);
277 col->g = swf_GetU8(t);
278 col->b = swf_GetU8(t);
279 col->a = swf_GetU8(t);
282 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
288 gradient->num = swf_GetU8(tag);
289 for(t=0;t<gradient->num;t++)
294 gradient->ratios[t] = swf_GetU8(tag);
296 swf_GetRGB(tag, &gradient->rgba[t]);
298 swf_GetRGBA(tag, &gradient->rgba[t]);
302 int swf_CountBits(U32 v,int nbits)
320 return (n>nbits)?n:nbits;
323 int swf_GetRect(TAG * t,SRECT * r)
326 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
328 nbits = (int) swf_GetBits(t,5);
329 r->xmin = swf_GetSBits(t,nbits);
330 r->xmax = swf_GetSBits(t,nbits);
331 r->ymin = swf_GetSBits(t,nbits);
332 r->ymax = swf_GetSBits(t,nbits);
336 int reader_GetRect(struct reader_t*reader,SRECT * r)
340 nbits = (int) reader_GetBits(reader,5);
341 r->xmin = reader_GetSBits(reader,nbits);
342 r->xmax = reader_GetSBits(reader,nbits);
343 r->ymin = reader_GetSBits(reader,nbits);
344 r->ymax = reader_GetSBits(reader,nbits);
348 int swf_SetRect(TAG * t,SRECT * r)
351 nbits = swf_CountBits(r->xmin,0);
352 nbits = swf_CountBits(r->xmax,nbits);
353 nbits = swf_CountBits(r->ymin,nbits);
354 nbits = swf_CountBits(r->ymax,nbits);
356 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
360 swf_SetBits(t,nbits,5);
361 swf_SetBits(t,r->xmin,nbits);
362 swf_SetBits(t,r->xmax,nbits);
363 swf_SetBits(t,r->ymin,nbits);
364 swf_SetBits(t,r->ymax,nbits);
369 void swf_ExpandRect(SRECT*src, SPOINT add)
371 if(add.x < src->xmin)
373 if(add.x > src->xmax)
375 if(add.y < src->ymin)
377 if(add.y > src->ymax)
380 void swf_ExpandRect2(SRECT*src, SRECT*add)
382 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
384 if(add->xmin < src->xmin)
385 src->xmin = add->xmin;
386 if(add->ymin < src->ymin)
387 src->ymin = add->ymin;
388 if(add->xmax > src->xmax)
389 src->xmax = add->xmax;
390 if(add->ymax > src->ymax)
391 src->ymax = add->ymax;
393 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
396 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
397 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
400 SRECT swf_TurnRect(SRECT r, MATRIX* m)
403 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
404 p1.x = r.xmin;p1.y = r.ymin;
405 p2.x = r.xmax;p2.y = r.ymin;
406 p3.x = r.xmin;p3.y = r.ymax;
407 p4.x = r.xmax;p4.y = r.ymax;
408 pp1 = swf_TurnPoint(p1, m);
409 pp2 = swf_TurnPoint(p2, m);
410 pp3 = swf_TurnPoint(p3, m);
411 pp4 = swf_TurnPoint(p4, m);
412 g.xmin = g.xmax = pp1.x;
413 g.ymin = g.ymax = pp1.y;
414 swf_ExpandRect(&g, pp2);
415 swf_ExpandRect(&g, pp3);
416 swf_ExpandRect(&g, pp4);
421 int swf_GetMatrix(TAG * t,MATRIX * m)
428 { m->sx = m->sy = 0x10000;
434 swf_ResetReadBits(t);
436 if (swf_GetBits(t,1))
437 { nbits = swf_GetBits(t,5);
438 m->sx = swf_GetSBits(t,nbits);
439 m->sy = swf_GetSBits(t,nbits);
441 else m->sx = m->sy = 0x10000;
443 if (swf_GetBits(t,1))
444 { nbits = swf_GetBits(t,5);
445 m->r0 = swf_GetSBits(t,nbits);
446 m->r1 = swf_GetSBits(t,nbits);
448 else m->r0 = m->r1 = 0x0;
450 nbits = swf_GetBits(t,5);
451 m->tx = swf_GetSBits(t,nbits);
452 m->ty = swf_GetSBits(t,nbits);
457 int swf_SetMatrix(TAG * t,MATRIX * m)
463 ma.sx = ma.sy = 0x10000;
468 swf_ResetWriteBits(t);
470 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
472 { swf_SetBits(t,1,1);
473 nbits = swf_CountBits(m->sx,0);
474 nbits = swf_CountBits(m->sy,nbits);
476 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
479 swf_SetBits(t,nbits,5);
480 swf_SetBits(t,m->sx,nbits);
481 swf_SetBits(t,m->sy,nbits);
484 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
486 { swf_SetBits(t,1,1);
487 nbits = swf_CountBits(m->r0,0);
488 nbits = swf_CountBits(m->r1,nbits);
490 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
493 swf_SetBits(t,nbits,5);
494 swf_SetBits(t,m->r0,nbits);
495 swf_SetBits(t,m->r1,nbits);
498 nbits = swf_CountBits(m->tx,0);
499 nbits = swf_CountBits(m->ty,nbits);
501 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
504 swf_SetBits(t,nbits,5);
505 swf_SetBits(t,m->tx,nbits);
506 swf_SetBits(t,m->ty,nbits);
511 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha) //FIXME: alpha should be type bool
519 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
520 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
524 swf_ResetReadBits(t);
525 hasadd = swf_GetBits(t,1);
526 hasmul = swf_GetBits(t,1);
527 nbits = swf_GetBits(t,4);
530 { cx->r0 = (S16)swf_GetSBits(t,nbits);
531 cx->g0 = (S16)swf_GetSBits(t,nbits);
532 cx->b0 = (S16)swf_GetSBits(t,nbits);
534 cx->a0 = (S16)swf_GetSBits(t,nbits);
538 { cx->r1 = (S16)swf_GetSBits(t,nbits);
539 cx->g1 = (S16)swf_GetSBits(t,nbits);
540 cx->b1 = (S16)swf_GetSBits(t,nbits);
542 cx->a1 = (S16)swf_GetSBits(t,nbits);
548 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
556 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
557 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
567 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
568 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
571 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
572 nbits = swf_CountBits((S32)cx->r0,nbits);
573 nbits = swf_CountBits((S32)cx->g0,nbits);
574 nbits = swf_CountBits((S32)cx->b0,nbits);
578 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
579 nbits = swf_CountBits((S32)cx->r1,nbits);
580 nbits = swf_CountBits((S32)cx->g1,nbits);
581 nbits = swf_CountBits((S32)cx->b1,nbits);
584 swf_ResetWriteBits(t);
585 swf_SetBits(t,hasadd?1:0,1);
586 swf_SetBits(t,hasmul?1:0,1);
587 swf_SetBits(t,nbits,4);
590 { swf_SetBits(t,cx->r0,nbits);
591 swf_SetBits(t,cx->g0,nbits);
592 swf_SetBits(t,cx->b0,nbits);
593 if (alpha) swf_SetBits(t,cx->a0,nbits);
597 { swf_SetBits(t,cx->r1,nbits);
598 swf_SetBits(t,cx->g1,nbits);
599 swf_SetBits(t,cx->b1,nbits);
600 if (alpha) swf_SetBits(t,cx->a1,nbits);
606 int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
607 int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
609 // Tag List Manipulating Functions
611 int swf_UpdateFrame(TAG * t,S8 delta)
612 // returns number of frames
622 TAG * swf_InsertTag(TAG * after,U16 id) // updates frames, if nescessary
625 t = (TAG *)malloc(sizeof(TAG));
627 { memset(t,0x00,sizeof(TAG));
631 { t->frame = after->frame;
633 t->next = after->next;
635 if (t->next) t->next->prev = t;
637 if (id==ST_SHOWFRAME) swf_UpdateFrame(t->next,+1);
643 void swf_ClearTag(TAG * t)
645 if (t->data) free(t->data);
654 int swf_DeleteTag(TAG * t)
657 if (t->id==ST_SHOWFRAME) swf_UpdateFrame(t->next,-1);
659 if (t->prev) t->prev->next = t->next;
660 if (t->next) t->next->prev = t->prev;
662 if (t->data) free(t->data);
667 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
673 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
681 if (reader->read(reader, &len, 4) != 4) return NULL;
685 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
686 // Sprite handling fix: Flaten sprite tree
688 t = (TAG *)malloc(sizeof(TAG));
693 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (2). (%d bytes)\n", sizeof(TAG));
698 memset(t,0x00,sizeof(TAG));
704 { t->data = (U8*)malloc(t->len);
708 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (3). (%d bytes)\n", t->len);
713 if (reader->read(reader, t->data, t->len) != t->len) return NULL;
717 { t->frame = prev->frame+((prev->id==ST_SHOWFRAME)?1:0);
725 int swf_DefineSprite_GetRealSize(TAG * t);
727 int swf_WriteTag2(struct writer_t*writer, TAG * t)
728 // returns tag length in bytes (incl. Header), -1 = Error
729 // writer = 0 -> no output
736 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
738 short_tag = len<0x3f;
742 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
743 if (writer->write(writer,raw,2)!=2)
746 fprintf(stderr,"WriteTag() failed: Short Header.\n");
753 raw[0] = SWAP16((t->id<<6)|0x3f);
754 if (writer->write(writer,raw,2)!=2)
757 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
763 if (writer->write(writer,&len,4)!=4)
766 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
773 { if (writer->write(writer,t->data,t->len)!=t->len)
776 fprintf(stderr,"WriteTag() failed: Data.\n");
782 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
786 return t->len+(short_tag?2:6);
789 int swf_WriteTag(int handle, TAG * t)
791 struct writer_t writer;
793 return swf_WriteTag2(0, t);
794 writer_init_filewriter(&writer, handle);
795 return swf_WriteTag2(&writer, t);
798 int swf_DefineSprite_GetRealSize(TAG * t)
799 // Sprite Handling: Helper function to pack DefineSprite-Tag
801 if(len>4) { // folded sprite
805 { t = swf_NextTag(t);
806 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
808 } while (t&&(t->id!=ST_END));
812 void swf_UnFoldSprite(TAG * t)
817 U16 spriteid,spriteframes;
819 if(t->id!=ST_DEFINESPRITE)
821 if(t->len<=4) // not folded
826 spriteid = swf_GetU16(t); //id
827 spriteframes = swf_GetU16(t); //frames
839 if(id == ST_DEFINESPRITE && len<=4)
844 it = swf_InsertTag(next, id);
849 { it->data = (U8*)malloc(it->len);
850 it->memsize = it->len;
851 swf_GetBlock(t, it->data, it->len);
858 free(t->data); t->data = 0;
859 t->memsize = t->len = t->pos = 0;
861 swf_SetU16(t, spriteid);
862 swf_SetU16(t, spriteframes);
865 void swf_FoldSprite(TAG * t)
870 if(t->id!=ST_DEFINESPRITE)
873 fprintf(stderr, "Error: Sprite has no ID!");
877 /* sprite is already folded */
884 t->len = t->pos = t->memsize = 0;
889 t = swf_NextTag(sprtag);
894 if(t->id==ST_SHOWFRAME) frames++;
895 if(t->id == ST_DEFINESPRITE && t->len<=4)
902 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
904 swf_SetU16(sprtag, id);
905 swf_SetU16(sprtag, frames);
907 t = swf_NextTag(sprtag);
913 swf_SetU16(sprtag,t->len|(t->id<<6));
915 swf_SetU16(sprtag,0x3f|(t->id<<6));
916 swf_SetU32(sprtag,t->len);
919 swf_SetBlock(sprtag,t->data, t->len);
921 if(t->id == ST_DEFINESPRITE && t->len<=4)
930 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
936 int swf_IsFolded(TAG * t)
938 return (t->id == ST_DEFINESPRITE && t->len>4);
941 void swf_FoldAll(SWF*swf)
943 TAG*tag = swf->firstTag;
945 if(tag->id == ST_DEFINESPRITE)
947 tag = swf_NextTag(tag);
951 void swf_UnFoldAll(SWF*swf)
953 TAG*tag = swf->firstTag;
955 if(tag->id == ST_DEFINESPRITE)
956 swf_UnFoldSprite(tag);
961 void swf_OptimizeTagOrder(SWF*swf)
968 /* at the moment, we don't actually do optimizing,
969 only fixing of non-spec-conformant things like
979 if(tag->id == ST_DEFINESPRITE) {
981 /* ??? all sprites are supposed to be unfolded */
982 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
992 /* move non-sprite tags out of sprite */
993 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
994 /* remove tag from current position */
995 tag->prev->next = tag->next;
997 tag->next->prev = tag->prev;
999 /* insert before tag level0 */
1001 tag->prev = level0->prev;
1003 tag->prev->next = tag;
1007 if(tag->id == ST_END) {
1018 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1020 if (!swf) return -1;
1021 memset(swf,0x00,sizeof(SWF));
1023 { char b[32]; // read Header
1027 struct reader_t zreader;
1029 if ((len = reader->read(reader ,b,8))<8) return -1;
1031 if (b[0]!='F' && b[0]!='C') return -1;
1032 if (b[1]!='W') return -1;
1033 if (b[2]!='S') return -1;
1034 swf->fileVersion = b[3];
1035 swf->compressed = (b[0]=='C')?1:0;
1036 swf->fileSize = GET32(&b[4]);
1038 if(swf->compressed) {
1039 reader_init_zlibinflate(&zreader, reader);
1043 reader_GetRect(reader, &swf->movieSize);
1044 reader->read(reader, &swf->frameRate, 2);
1045 swf->frameRate = SWAP16(swf->frameRate);
1046 reader->read(reader, &swf->frameCount, 2);
1047 swf->frameCount = SWAP16(swf->frameCount);
1049 /* read tags and connect to list */
1051 while (t) t = swf_ReadTag(reader,t);
1052 swf->firstTag = t1.next;
1053 t1.next->prev = NULL;
1059 int swf_ReadSWF(int handle, SWF * swf)
1061 struct reader_t reader;
1062 reader_init_filereader(&reader, handle);
1063 return swf_ReadSWF2(&reader, swf);
1066 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1070 struct writer_t zwriter;
1073 if (!swf) return -1;
1075 // Insert REFLEX Tag
1077 #ifdef INSERT_RFX_TAG
1079 if (swf->firstTag && swf_NextTag(swf->firstTag))
1080 if (swf_GetTagID(swf_NextTag(swf->firstTag))!=ST_REFLEX)
1081 swf_SetBlock(swf_InsertTag(swf->firstTag,ST_REFLEX),"rfx",3);
1083 #endif // INSERT_RFX_TAG
1085 // Count Frames + File Size
1092 { len += swf_WriteTag(-1, t);
1093 if (t->id==ST_SHOWFRAME) frameCount++;
1101 memset(&t1,0x00,sizeof(TAG));
1105 { // measure header file size
1108 memset(&t2,0x00,sizeof(TAG));
1111 swf_SetRect(&t2, &swf->movieSize);
1112 swf_SetU16(&t2, swf->frameRate);
1113 swf_SetU16(&t2, swf->frameCount);
1114 l = swf_GetTagLen(&t2)+8;
1118 if(len) {// don't touch headers without tags
1119 swf->fileSize = fileSize;
1120 swf->frameCount = frameCount;
1123 if(swf->compressed) {
1125 writer->write(writer, id, 3);
1129 writer->write(writer, id, 3);
1132 writer->write(writer, &swf->fileVersion, 1);
1133 PUT32(b4, swf->fileSize);
1134 writer->write(writer, b4, 4);
1136 if(swf->compressed) {
1137 writer_init_zlibdeflate(&zwriter, writer);
1141 swf_SetRect(&t1,&swf->movieSize);
1142 swf_SetU16(&t1,swf->frameRate);
1143 swf_SetU16(&t1,swf->frameCount);
1147 int ret = writer->write(writer,b,swf_GetTagLen(&t1));
1148 if (ret!=swf_GetTagLen(&t1))
1151 fprintf(stderr, "ret:%d\n",ret);
1153 fprintf(stderr,"WriteSWF() failed: Header.\n");
1160 { if (swf_WriteTag2(writer, t)<0) return -1;
1163 writer->finish(writer); //e.g. flush zlib buffers
1166 return (int)fileSize;
1169 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1171 struct writer_t writer;
1172 swf->compressed = 0;
1174 return swf_WriteSWF2(&writer, swf);
1175 writer_init_filewriter(&writer, handle);
1176 return swf_WriteSWF2(&writer, swf);
1179 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1181 struct writer_t writer;
1182 swf->compressed = 1;
1184 return swf_WriteSWF2(&writer, swf);
1185 writer_init_filewriter(&writer, handle);
1186 return swf_WriteSWF2(&writer, swf);
1189 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1192 memcpy(&myswf,swf,sizeof(SWF));
1194 return swf_WriteSWF2(writer, &myswf);
1197 int swf_WriteHeader(int handle,SWF * swf)
1200 memcpy(&myswf,swf,sizeof(SWF));
1202 return swf_WriteSWF(handle, &myswf);
1205 int swf_WriteCGI(SWF * swf)
1209 len = swf_WriteSWF(-1,swf);
1211 if (len<0) return -1;
1213 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1214 "Accept-Ranges: bytes\n"
1215 "Content-Length: %lu\n"
1216 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1219 write(fileno(stdout),s,strlen(s));
1220 return swf_WriteSWF(fileno(stdout),swf);
1223 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1224 { TAG * t = swf->firstTag;
1227 { TAG * tnew = t->next;
1228 if (t->data) free(t->data);
1234 // include advanced functions
1236 #include "modules/swfdump.c"
1237 #include "modules/swfshape.c"
1238 #include "modules/swftext.c"
1239 #include "modules/swfobject.c"
1240 #include "modules/swfbutton.c"
1241 #include "modules/swftools.c"
1242 #include "modules/swfcgi.c"
1243 #include "modules/swfbits.c"
1244 #include "modules/swfaction.c"
1245 #include "modules/swfsound.c"