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 int swf_GetFrameNo(TAG * t) { return t->frame; }
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 // Tag List Manipulating Functions
634 int swf_UpdateFrame(TAG * t,S8 delta)
635 // returns number of frames
645 TAG * swf_InsertTag(TAG * after,U16 id) // updates frames, if nescessary
648 t = (TAG *)malloc(sizeof(TAG));
650 { memset(t,0x00,sizeof(TAG));
654 { t->frame = after->frame;
656 t->next = after->next;
658 if (t->next) t->next->prev = t;
660 if (id==ST_SHOWFRAME) swf_UpdateFrame(t->next,+1);
666 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id) // updates frames, if nescessary
669 t = (TAG *)malloc(sizeof(TAG));
671 { memset(t,0x00,sizeof(TAG));
675 { t->frame = before->frame;
677 t->prev = before->prev;
679 if (t->prev) t->prev->next = t;
681 if (id==ST_SHOWFRAME) swf_UpdateFrame(t->next,+1);
684 if(swf && swf->firstTag == before) {
690 void swf_ClearTag(TAG * t)
692 if (t->data) free(t->data);
701 void swf_ResetTag(TAG*tag, U16 id)
703 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
707 int swf_DeleteTag(TAG * t)
710 if (t->id==ST_SHOWFRAME) swf_UpdateFrame(t->next,-1);
712 if (t->prev) t->prev->next = t->next;
713 if (t->next) t->next->prev = t->prev;
715 if (t->data) free(t->data);
720 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
726 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
734 if (reader->read(reader, &len, 4) != 4) return NULL;
738 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
739 // Sprite handling fix: Flaten sprite tree
741 t = (TAG *)malloc(sizeof(TAG));
746 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (2). (%d bytes)\n", sizeof(TAG));
751 memset(t,0x00,sizeof(TAG));
757 { t->data = (U8*)malloc(t->len);
761 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (3). (%d bytes)\n", t->len);
766 if (reader->read(reader, t->data, t->len) != t->len) return NULL;
770 { t->frame = prev->frame+((prev->id==ST_SHOWFRAME)?1:0);
778 int swf_DefineSprite_GetRealSize(TAG * t);
780 int swf_WriteTag2(struct writer_t*writer, TAG * t)
781 // returns tag length in bytes (incl. Header), -1 = Error
782 // writer = 0 -> no output
789 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
791 short_tag = len<0x3f;
795 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
796 if (writer->write(writer,raw,2)!=2)
799 fprintf(stderr,"WriteTag() failed: Short Header.\n");
806 raw[0] = SWAP16((t->id<<6)|0x3f);
807 if (writer->write(writer,raw,2)!=2)
810 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
816 if (writer->write(writer,&len,4)!=4)
819 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
826 { if (writer->write(writer,t->data,t->len)!=t->len)
829 fprintf(stderr,"WriteTag() failed: Data.\n");
835 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
839 return t->len+(short_tag?2:6);
842 int swf_WriteTag(int handle, TAG * t)
844 struct writer_t writer;
846 return swf_WriteTag2(0, t);
847 writer_init_filewriter(&writer, handle);
848 return swf_WriteTag2(&writer, t);
851 int swf_DefineSprite_GetRealSize(TAG * t)
852 // Sprite Handling: Helper function to pack DefineSprite-Tag
854 if(len>4) { // folded sprite
858 { t = swf_NextTag(t);
859 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
861 } while (t&&(t->id!=ST_END));
865 void swf_UnFoldSprite(TAG * t)
870 U16 spriteid,spriteframes;
872 if(t->id!=ST_DEFINESPRITE)
874 if(t->len<=4) // not folded
879 spriteid = swf_GetU16(t); //id
880 spriteframes = swf_GetU16(t); //frames
892 if(id == ST_DEFINESPRITE && len<=4)
897 it = swf_InsertTag(next, id);
902 { it->data = (U8*)malloc(it->len);
903 it->memsize = it->len;
904 swf_GetBlock(t, it->data, it->len);
911 free(t->data); t->data = 0;
912 t->memsize = t->len = t->pos = 0;
914 swf_SetU16(t, spriteid);
915 swf_SetU16(t, spriteframes);
918 void swf_FoldSprite(TAG * t)
923 if(t->id!=ST_DEFINESPRITE)
926 fprintf(stderr, "Error: Sprite has no ID!");
930 /* sprite is already folded */
937 t->len = t->pos = t->memsize = 0;
942 t = swf_NextTag(sprtag);
947 if(t->id==ST_SHOWFRAME) frames++;
948 if(t->id == ST_DEFINESPRITE && t->len<=4)
955 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
957 swf_SetU16(sprtag, id);
958 swf_SetU16(sprtag, frames);
960 t = swf_NextTag(sprtag);
966 swf_SetU16(sprtag,t->len|(t->id<<6));
968 swf_SetU16(sprtag,0x3f|(t->id<<6));
969 swf_SetU32(sprtag,t->len);
972 swf_SetBlock(sprtag,t->data, t->len);
974 if(t->id == ST_DEFINESPRITE && t->len<=4)
983 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
989 int swf_IsFolded(TAG * t)
991 return (t->id == ST_DEFINESPRITE && t->len>4);
994 void swf_FoldAll(SWF*swf)
996 TAG*tag = swf->firstTag;
998 if(tag->id == ST_DEFINESPRITE)
1000 tag = swf_NextTag(tag);
1004 void swf_UnFoldAll(SWF*swf)
1006 TAG*tag = swf->firstTag;
1008 if(tag->id == ST_DEFINESPRITE)
1009 swf_UnFoldSprite(tag);
1014 void swf_OptimizeTagOrder(SWF*swf)
1021 /* at the moment, we don't actually do optimizing,
1022 only fixing of non-spec-conformant things like
1029 tag = swf->firstTag;
1032 if(tag->id == ST_DEFINESPRITE) {
1034 /* ??? all sprites are supposed to be unfolded */
1035 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1045 /* move non-sprite tags out of sprite */
1046 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1047 /* remove tag from current position */
1048 tag->prev->next = tag->next;
1050 tag->next->prev = tag->prev;
1052 /* insert before tag level0 */
1054 tag->prev = level0->prev;
1056 tag->prev->next = tag;
1060 if(tag->id == ST_END) {
1071 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1073 if (!swf) return -1;
1074 memset(swf,0x00,sizeof(SWF));
1076 { char b[32]; // read Header
1080 struct reader_t zreader;
1082 if ((len = reader->read(reader ,b,8))<8) return -1;
1084 if (b[0]!='F' && b[0]!='C') return -1;
1085 if (b[1]!='W') return -1;
1086 if (b[2]!='S') return -1;
1087 swf->fileVersion = b[3];
1088 swf->compressed = (b[0]=='C')?1:0;
1089 swf->fileSize = GET32(&b[4]);
1091 if(swf->compressed) {
1092 reader_init_zlibinflate(&zreader, reader);
1096 reader_GetRect(reader, &swf->movieSize);
1097 reader->read(reader, &swf->frameRate, 2);
1098 swf->frameRate = SWAP16(swf->frameRate);
1099 reader->read(reader, &swf->frameCount, 2);
1100 swf->frameCount = SWAP16(swf->frameCount);
1102 /* read tags and connect to list */
1104 while (t) t = swf_ReadTag(reader,t);
1105 swf->firstTag = t1.next;
1106 t1.next->prev = NULL;
1112 int swf_ReadSWF(int handle, SWF * swf)
1114 struct reader_t reader;
1115 reader_init_filereader(&reader, handle);
1116 return swf_ReadSWF2(&reader, swf);
1119 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1123 struct writer_t zwriter;
1126 if (!swf) return -1;
1128 // Insert REFLEX Tag
1130 #ifdef INSERT_RFX_TAG
1132 if (swf->firstTag && swf_NextTag(swf->firstTag))
1133 if (swf_GetTagID(swf_NextTag(swf->firstTag))!=ST_REFLEX)
1134 swf_SetBlock(swf_InsertTag(swf->firstTag,ST_REFLEX),"rfx",3);
1136 #endif // INSERT_RFX_TAG
1138 // Count Frames + File Size
1145 { len += swf_WriteTag(-1, t);
1146 if (t->id==ST_SHOWFRAME) frameCount++;
1154 memset(&t1,0x00,sizeof(TAG));
1158 { // measure header file size
1161 memset(&t2,0x00,sizeof(TAG));
1164 swf_SetRect(&t2, &swf->movieSize);
1165 swf_SetU16(&t2, swf->frameRate);
1166 swf_SetU16(&t2, swf->frameCount);
1167 l = swf_GetTagLen(&t2)+8;
1169 if(swf->compressed == 8) {
1174 if(len) {// don't touch headers without tags
1175 swf->fileSize = fileSize;
1176 swf->frameCount = frameCount;
1179 if(swf->compressed != 8) {
1180 /* compressed flag set to 8 means "skip first 8
1181 header bytes". This is necessary if the caller wants to
1182 create compressed SWFs himself */
1183 if(swf->compressed) {
1185 writer->write(writer, id, 3);
1189 writer->write(writer, id, 3);
1192 writer->write(writer, &swf->fileVersion, 1);
1193 PUT32(b4, swf->fileSize);
1194 writer->write(writer, b4, 4);
1196 if(swf->compressed) {
1197 writer_init_zlibdeflate(&zwriter, writer);
1202 swf_SetRect(&t1,&swf->movieSize);
1203 swf_SetU16(&t1,swf->frameRate);
1204 swf_SetU16(&t1,swf->frameCount);
1208 int ret = writer->write(writer,b,swf_GetTagLen(&t1));
1209 if (ret!=swf_GetTagLen(&t1))
1212 fprintf(stderr, "ret:%d\n",ret);
1214 fprintf(stderr,"WriteSWF() failed: Header.\n");
1221 { if (swf_WriteTag2(writer, t)<0) return -1;
1224 if(swf->compressed != 8)
1225 writer->finish(writer); // flush zlib buffers - only if _we_ initialized that writer.
1228 return (int)fileSize;
1231 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1233 struct writer_t writer;
1234 swf->compressed = 0;
1236 writer_init_nullwriter(&writer);
1237 return swf_WriteSWF2(&writer, swf);
1239 writer_init_filewriter(&writer, handle);
1240 return swf_WriteSWF2(&writer, swf);
1243 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1245 struct writer_t writer;
1246 swf->compressed = 1;
1248 writer_init_nullwriter(&writer);
1249 return swf_WriteSWF2(&writer, swf);
1251 writer_init_filewriter(&writer, handle);
1252 return swf_WriteSWF2(&writer, swf);
1255 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1258 memcpy(&myswf,swf,sizeof(SWF));
1260 return swf_WriteSWF2(writer, &myswf);
1263 int swf_WriteHeader(int handle,SWF * swf)
1266 memcpy(&myswf,swf,sizeof(SWF));
1268 return swf_WriteSWF(handle, &myswf);
1271 int swf_WriteCGI(SWF * swf)
1275 len = swf_WriteSWF(-1,swf);
1277 if (len<0) return -1;
1279 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1280 "Accept-Ranges: bytes\n"
1281 "Content-Length: %lu\n"
1282 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1285 write(fileno(stdout),s,strlen(s));
1286 return swf_WriteSWF(fileno(stdout),swf);
1289 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1290 { TAG * t = swf->firstTag;
1293 { TAG * tnew = t->next;
1294 if (t->data) free(t->data);
1300 // include advanced functions
1302 #include "modules/swfdump.c"
1303 #include "modules/swfshape.c"
1304 #include "modules/swftext.c"
1305 #include "modules/swfobject.c"
1306 #include "modules/swfbutton.c"
1307 #include "modules/swftools.c"
1308 #include "modules/swfcgi.c"
1309 #include "modules/swfbits.c"
1310 #include "modules/swfaction.c"
1311 #include "modules/swfsound.c"