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)
297 memset(gradient, 0, sizeof(GRADIENT));
302 gradient->num = swf_GetU8(tag);
303 for(t=0;t<gradient->num;t++)
308 gradient->ratios[t] = swf_GetU8(tag);
310 swf_GetRGB(tag, &gradient->rgba[t]);
312 swf_GetRGBA(tag, &gradient->rgba[t]);
316 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
320 memset(gradient, 0, sizeof(GRADIENT));
323 swf_SetU8(tag, gradient->num);
324 for(t=0; t<8 && t<gradient->num; t++)
326 swf_SetU8(tag, gradient->ratios[t]);
328 swf_SetRGB(tag, &gradient->rgba[t]);
330 swf_SetRGBA(tag, &gradient->rgba[t]);
334 int swf_CountUBits(U32 v,int nbits)
337 if(v == 0x00000000) n = 0;
343 return (n>nbits)?n:nbits;
346 int swf_CountBits(U32 v,int nbits)
350 { if(v == 0xffffffff) n = 1;
358 { if(v == 0x00000000) n = 0;
365 return (n>nbits)?n:nbits;
368 int swf_GetRect(TAG * t,SRECT * r)
371 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
373 nbits = (int) swf_GetBits(t,5);
374 r->xmin = swf_GetSBits(t,nbits);
375 r->xmax = swf_GetSBits(t,nbits);
376 r->ymin = swf_GetSBits(t,nbits);
377 r->ymax = swf_GetSBits(t,nbits);
381 int reader_GetRect(struct reader_t*reader,SRECT * r)
385 nbits = (int) reader_GetBits(reader,5);
386 r->xmin = reader_GetSBits(reader,nbits);
387 r->xmax = reader_GetSBits(reader,nbits);
388 r->ymin = reader_GetSBits(reader,nbits);
389 r->ymax = reader_GetSBits(reader,nbits);
393 int swf_SetRect(TAG * t,SRECT * r)
396 nbits = swf_CountBits(r->xmin,0);
397 nbits = swf_CountBits(r->xmax,nbits);
398 nbits = swf_CountBits(r->ymin,nbits);
399 nbits = swf_CountBits(r->ymax,nbits);
401 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
405 swf_SetBits(t,nbits,5);
406 swf_SetBits(t,r->xmin,nbits);
407 swf_SetBits(t,r->xmax,nbits);
408 swf_SetBits(t,r->ymin,nbits);
409 swf_SetBits(t,r->ymax,nbits);
414 void swf_ExpandRect(SRECT*src, SPOINT add)
416 if(add.x < src->xmin)
418 if(add.x > src->xmax)
420 if(add.y < src->ymin)
422 if(add.y > src->ymax)
425 void swf_ExpandRect2(SRECT*src, SRECT*add)
427 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
429 if(add->xmin < src->xmin)
430 src->xmin = add->xmin;
431 if(add->ymin < src->ymin)
432 src->ymin = add->ymin;
433 if(add->xmax > src->xmax)
434 src->xmax = add->xmax;
435 if(add->ymax > src->ymax)
436 src->ymax = add->ymax;
438 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
441 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
442 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
445 SRECT swf_TurnRect(SRECT r, MATRIX* m)
448 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
449 p1.x = r.xmin;p1.y = r.ymin;
450 p2.x = r.xmax;p2.y = r.ymin;
451 p3.x = r.xmin;p3.y = r.ymax;
452 p4.x = r.xmax;p4.y = r.ymax;
453 pp1 = swf_TurnPoint(p1, m);
454 pp2 = swf_TurnPoint(p2, m);
455 pp3 = swf_TurnPoint(p3, m);
456 pp4 = swf_TurnPoint(p4, m);
457 g.xmin = g.xmax = pp1.x;
458 g.ymin = g.ymax = pp1.y;
459 swf_ExpandRect(&g, pp2);
460 swf_ExpandRect(&g, pp3);
461 swf_ExpandRect(&g, pp4);
466 int swf_GetMatrix(TAG * t,MATRIX * m)
473 { m->sx = m->sy = 0x10000;
479 swf_ResetReadBits(t);
481 if (swf_GetBits(t,1))
482 { nbits = swf_GetBits(t,5);
483 m->sx = swf_GetSBits(t,nbits);
484 m->sy = swf_GetSBits(t,nbits);
486 else m->sx = m->sy = 0x10000;
488 if (swf_GetBits(t,1))
489 { nbits = swf_GetBits(t,5);
490 m->r0 = swf_GetSBits(t,nbits);
491 m->r1 = swf_GetSBits(t,nbits);
493 else m->r0 = m->r1 = 0x0;
495 nbits = swf_GetBits(t,5);
496 m->tx = swf_GetSBits(t,nbits);
497 m->ty = swf_GetSBits(t,nbits);
502 int swf_SetMatrix(TAG * t,MATRIX * m)
508 ma.sx = ma.sy = 0x10000;
513 swf_ResetWriteBits(t);
515 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
517 { swf_SetBits(t,1,1);
518 nbits = swf_CountBits(m->sx,0);
519 nbits = swf_CountBits(m->sy,nbits);
521 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
524 swf_SetBits(t,nbits,5);
525 swf_SetBits(t,m->sx,nbits);
526 swf_SetBits(t,m->sy,nbits);
529 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
531 { swf_SetBits(t,1,1);
532 nbits = swf_CountBits(m->r0,0);
533 nbits = swf_CountBits(m->r1,nbits);
535 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
538 swf_SetBits(t,nbits,5);
539 swf_SetBits(t,m->r0,nbits);
540 swf_SetBits(t,m->r1,nbits);
543 nbits = swf_CountBits(m->tx,0);
544 nbits = swf_CountBits(m->ty,nbits);
546 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
549 swf_SetBits(t,nbits,5);
550 swf_SetBits(t,m->tx,nbits);
551 swf_SetBits(t,m->ty,nbits);
556 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha) //FIXME: alpha should be type bool
564 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
565 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
569 swf_ResetReadBits(t);
570 hasadd = swf_GetBits(t,1);
571 hasmul = swf_GetBits(t,1);
572 nbits = swf_GetBits(t,4);
575 { cx->r0 = (S16)swf_GetSBits(t,nbits);
576 cx->g0 = (S16)swf_GetSBits(t,nbits);
577 cx->b0 = (S16)swf_GetSBits(t,nbits);
579 cx->a0 = (S16)swf_GetSBits(t,nbits);
583 { cx->r1 = (S16)swf_GetSBits(t,nbits);
584 cx->g1 = (S16)swf_GetSBits(t,nbits);
585 cx->b1 = (S16)swf_GetSBits(t,nbits);
587 cx->a1 = (S16)swf_GetSBits(t,nbits);
593 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
601 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
602 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
612 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
613 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
616 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
617 nbits = swf_CountBits((S32)cx->r0,nbits);
618 nbits = swf_CountBits((S32)cx->g0,nbits);
619 nbits = swf_CountBits((S32)cx->b0,nbits);
623 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
624 nbits = swf_CountBits((S32)cx->r1,nbits);
625 nbits = swf_CountBits((S32)cx->g1,nbits);
626 nbits = swf_CountBits((S32)cx->b1,nbits);
629 swf_ResetWriteBits(t);
630 swf_SetBits(t,hasadd?1:0,1);
631 swf_SetBits(t,hasmul?1:0,1);
632 swf_SetBits(t,nbits,4);
635 { swf_SetBits(t,cx->r0,nbits);
636 swf_SetBits(t,cx->g0,nbits);
637 swf_SetBits(t,cx->b0,nbits);
638 if (alpha) swf_SetBits(t,cx->a0,nbits);
642 { swf_SetBits(t,cx->r1,nbits);
643 swf_SetBits(t,cx->g1,nbits);
644 swf_SetBits(t,cx->b1,nbits);
645 if (alpha) swf_SetBits(t,cx->a1,nbits);
651 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
652 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
654 void swf_SetPassword(TAG * t, const char * password)
656 /* WARNING: crypt_md5 is not reentrant */
657 char* md5string = crypt_md5(password, "salt"); /* FIXME- get random salt */
658 swf_SetString(t, md5string);
661 int swf_VerifyPassword(TAG * t, const char * password)
663 char*md5string1, *md5string2;
668 md5string1 = swf_GetString(t);
670 if(!strncmp(md5string1, "$1$",3 )) {
673 x = strchr(md5string1+3, '$');
676 n = x-(md5string1+3);
677 salt = (char*)malloc(n+1);
678 memcpy(salt, md5string1+3, n);
681 md5string2 = crypt_md5(password, salt);
683 if(strcmp(md5string1, md5string2) != 0)
688 // Tag List Manipulating Functions
690 TAG * swf_InsertTag(TAG * after,U16 id)
693 t = (TAG *)malloc(sizeof(TAG));
695 { memset(t,0x00,sizeof(TAG));
701 t->next = after->next;
703 if (t->next) t->next->prev = t;
709 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
712 t = (TAG *)malloc(sizeof(TAG));
714 { memset(t,0x00,sizeof(TAG));
720 t->prev = before->prev;
722 if (t->prev) t->prev->next = t;
725 if(swf && swf->firstTag == before) {
731 void swf_ClearTag(TAG * t)
733 if (t->data) free(t->data);
742 void swf_ResetTag(TAG*tag, U16 id)
744 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
748 int swf_DeleteTag(TAG * t)
751 if (t->prev) t->prev->next = t->next;
752 if (t->next) t->next->prev = t->prev;
754 if (t->data) free(t->data);
759 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
765 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
773 if (reader->read(reader, &len, 4) != 4) return NULL;
777 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
778 // Sprite handling fix: Flatten sprite tree
780 t = (TAG *)malloc(sizeof(TAG));
785 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (2). (%d bytes)\n", sizeof(TAG));
790 memset(t,0x00,sizeof(TAG));
796 { t->data = (U8*)malloc(t->len);
800 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (3). (%d bytes)\n", t->len);
805 if (reader->read(reader, t->data, t->len) != t->len) return NULL;
817 int swf_DefineSprite_GetRealSize(TAG * t);
819 int swf_WriteTag2(struct writer_t*writer, TAG * t)
820 // returns tag length in bytes (incl. Header), -1 = Error
821 // writer = 0 -> no output
828 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
830 short_tag = len<0x3f;
834 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
835 if (writer->write(writer,raw,2)!=2)
838 fprintf(stderr,"WriteTag() failed: Short Header.\n");
845 raw[0] = SWAP16((t->id<<6)|0x3f);
846 if (writer->write(writer,raw,2)!=2)
849 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
855 if (writer->write(writer,&len,4)!=4)
858 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
865 { if (writer->write(writer,t->data,t->len)!=t->len)
868 fprintf(stderr,"WriteTag() failed: Data.\n");
874 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
878 return t->len+(short_tag?2:6);
881 int swf_WriteTag(int handle, TAG * t)
883 struct writer_t writer;
885 return swf_WriteTag2(0, t);
886 writer_init_filewriter(&writer, handle);
887 return swf_WriteTag2(&writer, t);
890 int swf_DefineSprite_GetRealSize(TAG * t)
891 // Sprite Handling: Helper function to pack DefineSprite-Tag
893 if(len>4) { // folded sprite
897 { t = swf_NextTag(t);
898 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
900 } while (t&&(t->id!=ST_END));
904 void swf_UnFoldSprite(TAG * t)
909 U16 spriteid,spriteframes;
911 if(t->id!=ST_DEFINESPRITE)
913 if(t->len<=4) // not folded
918 spriteid = swf_GetU16(t); //id
919 spriteframes = swf_GetU16(t); //frames
931 if(id == ST_DEFINESPRITE && len<=4)
936 it = swf_InsertTag(next, id);
941 { it->data = (U8*)malloc(it->len);
942 it->memsize = it->len;
943 swf_GetBlock(t, it->data, it->len);
950 free(t->data); t->data = 0;
951 t->memsize = t->len = t->pos = 0;
953 swf_SetU16(t, spriteid);
954 swf_SetU16(t, spriteframes);
957 void swf_FoldSprite(TAG * t)
962 if(t->id!=ST_DEFINESPRITE)
965 fprintf(stderr, "Error: Sprite has no ID!");
969 /* sprite is already folded */
976 t->len = t->pos = t->memsize = 0;
981 t = swf_NextTag(sprtag);
986 if(t->id==ST_SHOWFRAME) frames++;
987 if(t->id == ST_DEFINESPRITE && t->len<=4)
994 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
996 swf_SetU16(sprtag, id);
997 swf_SetU16(sprtag, frames);
999 t = swf_NextTag(sprtag);
1005 swf_SetU16(sprtag,t->len|(t->id<<6));
1007 swf_SetU16(sprtag,0x3f|(t->id<<6));
1008 swf_SetU32(sprtag,t->len);
1011 swf_SetBlock(sprtag,t->data, t->len);
1013 if(t->id == ST_DEFINESPRITE && t->len<=4)
1022 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1024 // sprtag->next = t;
1025 // t->prev = sprtag;
1028 int swf_IsFolded(TAG * t)
1030 return (t->id == ST_DEFINESPRITE && t->len>4);
1033 void swf_FoldAll(SWF*swf)
1035 TAG*tag = swf->firstTag;
1036 //swf_DumpSWF(stdout, swf);
1038 if(tag->id == ST_DEFINESPRITE) {
1039 swf_FoldSprite(tag);
1040 //swf_DumpSWF(stdout, swf);
1042 tag = swf_NextTag(tag);
1046 void swf_UnFoldAll(SWF*swf)
1048 TAG*tag = swf->firstTag;
1050 if(tag->id == ST_DEFINESPRITE)
1051 swf_UnFoldSprite(tag);
1056 void swf_OptimizeTagOrder(SWF*swf)
1063 /* at the moment, we don't actually do optimizing,
1064 only fixing of non-spec-conformant things like
1071 tag = swf->firstTag;
1074 if(tag->id == ST_DEFINESPRITE) {
1076 /* ??? all sprites are supposed to be unfolded */
1077 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1087 /* move non-sprite tags out of sprite */
1088 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1089 /* remove tag from current position */
1090 tag->prev->next = tag->next;
1092 tag->next->prev = tag->prev;
1094 /* insert before tag level0 */
1096 tag->prev = level0->prev;
1098 tag->prev->next = tag;
1102 if(tag->id == ST_END) {
1113 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1115 if (!swf) return -1;
1116 memset(swf,0x00,sizeof(SWF));
1118 { char b[32]; // read Header
1122 struct reader_t zreader;
1124 if ((len = reader->read(reader ,b,8))<8) return -1;
1126 if (b[0]!='F' && b[0]!='C') return -1;
1127 if (b[1]!='W') return -1;
1128 if (b[2]!='S') return -1;
1129 swf->fileVersion = b[3];
1130 swf->compressed = (b[0]=='C')?1:0;
1131 swf->fileSize = GET32(&b[4]);
1133 if(swf->compressed) {
1134 reader_init_zlibinflate(&zreader, reader);
1138 reader_GetRect(reader, &swf->movieSize);
1139 reader->read(reader, &swf->frameRate, 2);
1140 swf->frameRate = SWAP16(swf->frameRate);
1141 reader->read(reader, &swf->frameCount, 2);
1142 swf->frameCount = SWAP16(swf->frameCount);
1144 /* read tags and connect to list */
1146 while (t) t = swf_ReadTag(reader,t);
1147 swf->firstTag = t1.next;
1148 t1.next->prev = NULL;
1154 int swf_ReadSWF(int handle, SWF * swf)
1156 struct reader_t reader;
1157 reader_init_filereader(&reader, handle);
1158 return swf_ReadSWF2(&reader, swf);
1161 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1165 struct writer_t zwriter;
1168 if (!swf) return -1;
1170 // Insert REFLEX Tag
1172 #ifdef INSERT_RFX_TAG
1174 if (swf->firstTag && swf_NextTag(swf->firstTag))
1175 if (swf_GetTagID(swf_NextTag(swf->firstTag))!=ST_REFLEX)
1176 swf_SetBlock(swf_InsertTag(swf->firstTag,ST_REFLEX),"rfx",3);
1178 #endif // INSERT_RFX_TAG
1180 // Count Frames + File Size
1187 { len += swf_WriteTag(-1, t);
1188 if (t->id==ST_SHOWFRAME) frameCount++;
1196 memset(&t1,0x00,sizeof(TAG));
1200 { // measure header file size
1203 memset(&t2,0x00,sizeof(TAG));
1206 swf_SetRect(&t2, &swf->movieSize);
1207 swf_SetU16(&t2, swf->frameRate);
1208 swf_SetU16(&t2, swf->frameCount);
1209 l = swf_GetTagLen(&t2)+8;
1211 if(swf->compressed == 8) {
1216 if(len) {// don't touch headers without tags
1217 swf->fileSize = fileSize;
1218 swf->frameCount = frameCount;
1221 if(swf->compressed != 8) {
1222 /* compressed flag set to 8 means "skip first 8
1223 header bytes". This is necessary if the caller wants to
1224 create compressed SWFs himself */
1225 if(swf->compressed) {
1227 writer->write(writer, id, 3);
1231 writer->write(writer, id, 3);
1234 writer->write(writer, &swf->fileVersion, 1);
1235 PUT32(b4, swf->fileSize);
1236 writer->write(writer, b4, 4);
1238 if(swf->compressed) {
1239 writer_init_zlibdeflate(&zwriter, writer);
1244 swf_SetRect(&t1,&swf->movieSize);
1245 swf_SetU16(&t1,swf->frameRate);
1246 swf_SetU16(&t1,swf->frameCount);
1250 int ret = writer->write(writer,b,swf_GetTagLen(&t1));
1251 if (ret!=swf_GetTagLen(&t1))
1254 fprintf(stderr, "ret:%d\n",ret);
1256 fprintf(stderr,"WriteSWF() failed: Header.\n");
1263 { if (swf_WriteTag2(writer, t)<0) return -1;
1266 if(swf->compressed != 8)
1267 writer->finish(writer); // flush zlib buffers - only if _we_ initialized that writer.
1270 return (int)fileSize;
1273 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1275 struct writer_t writer;
1276 swf->compressed = 0;
1278 writer_init_nullwriter(&writer);
1279 return swf_WriteSWF2(&writer, swf);
1281 writer_init_filewriter(&writer, handle);
1282 return swf_WriteSWF2(&writer, swf);
1285 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1287 struct writer_t writer;
1288 swf->compressed = 1;
1290 writer_init_nullwriter(&writer);
1291 return swf_WriteSWF2(&writer, swf);
1293 writer_init_filewriter(&writer, handle);
1294 return swf_WriteSWF2(&writer, swf);
1297 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1300 memcpy(&myswf,swf,sizeof(SWF));
1302 return swf_WriteSWF2(writer, &myswf);
1305 int swf_WriteHeader(int handle,SWF * swf)
1308 memcpy(&myswf,swf,sizeof(SWF));
1310 return swf_WriteSWF(handle, &myswf);
1313 int swf_WriteCGI(SWF * swf)
1317 len = swf_WriteSWF(-1,swf);
1319 if (len<0) return -1;
1321 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1322 "Accept-Ranges: bytes\n"
1323 "Content-Length: %lu\n"
1324 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1327 write(fileno(stdout),s,strlen(s));
1328 return swf_WriteSWF(fileno(stdout),swf);
1331 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1332 { TAG * t = swf->firstTag;
1335 { TAG * tnew = t->next;
1336 if (t->data) free(t->data);
1342 // include advanced functions
1344 #include "modules/swfdump.c"
1345 #include "modules/swfshape.c"
1346 #include "modules/swftext.c"
1347 #include "modules/swfobject.c"
1348 #include "modules/swfbutton.c"
1349 #include "modules/swftools.c"
1350 #include "modules/swfcgi.c"
1351 #include "modules/swfbits.c"
1352 #include "modules/swfaction.c"
1353 #include "modules/swfsound.c"
1354 #include "modules/swfdraw.c"