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
38 #ifndef RFXSWF_DISABLESOUND
40 #include "lame/lame.h"
53 #define MALLOC_SIZE 128
54 #define INSERT_RFX_TAG
56 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
59 // inline wrapper functions
61 TAG * swf_NextTag(TAG * t) { return t->next; }
62 TAG * swf_PrevTag(TAG * t) { return t->prev; }
63 U16 swf_GetTagID(TAG * t) { return t->id; }
64 U32 swf_GetTagLen(TAG * t) { return t->len; }
65 U8* swf_GetTagLenPtr(TAG * t) { return &(t->data[t->len]); }
66 U32 swf_GetTagPos(TAG * t) { return t->pos; }
68 // Basic Data Access Functions
70 #define swf_ResetReadBits(tag) if (tag->readBit) { tag->pos++; tag->readBit = 0; }
71 #define swf_ResetWriteBits(tag) if (tag->writeBit) { tag->writeBit = 0; }
73 // for future purpose: avoid high level lib functions to change tagpos/bitpos
75 #define swf_SaveTagPos(tag)
76 #define swf_RestoreTagPos(tag)
78 void swf_SetTagPos(TAG * t,U32 pos)
79 { swf_ResetReadBits(t);
80 if (pos<=t->len) t->pos = pos;
82 else fprintf(stderr,"SetTagPos() out of bounds: TagID = %i\n",t->id);
86 char* swf_GetString(TAG*t)
88 char* str = ((char*)(&(t)->data[(t)->pos]));
94 { swf_ResetReadBits(t);
97 { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
101 return t->data[t->pos++];
104 U16 swf_GetU16(TAG * t)
106 swf_ResetReadBits(t);
108 if (t->pos>(t->len-2))
109 { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
113 res = t->data[t->pos] | (t->data[t->pos+1]<<8);
118 U32 swf_GetU32(TAG * t)
120 swf_ResetReadBits(t);
122 if (t->pos>(t->len-4))
123 { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
127 res = t->data[t->pos] | (t->data[t->pos+1]<<8) |
128 (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
133 int swf_GetBlock(TAG * t,U8 * b,int l)
134 // returns number of bytes written (<=l)
135 // b = NULL -> skip data
136 { swf_ResetReadBits(t);
137 if ((t->len-t->pos)<l) l=t->len-t->pos;
138 if (b && l) memcpy(b,&t->data[t->pos],l);
143 int swf_SetBlock(TAG * t,U8 * b,int l)
144 // Appends Block to the end of Tagdata, returns size
145 { U32 newlen = t->len + l;
146 swf_ResetWriteBits(t);
147 if (newlen>t->memsize)
148 { U32 newmem = MEMSIZE(newlen);
149 U8 * newdata = (U8*)((t->data)?realloc(t->data,newmem):malloc(newmem));
153 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (1). (%d bytes)\n", newmem);
161 if (b) memcpy(&t->data[t->len],b,l);
162 else memset(&t->data[t->len],0x00,l);
167 int swf_SetU8(TAG * t,U8 v)
168 { swf_ResetWriteBits(t);
169 if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
170 t->data[t->len++] = v;
174 int swf_SetU16(TAG * t,U16 v)
179 swf_ResetWriteBits(t);
180 if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
181 t->data[t->len++] = a[0];
182 t->data[t->len++] = a[1];
186 int swf_SetU32(TAG * t,U32 v)
188 a[0] = v&0xff; // to ensure correct handling of non-intel byteorder
193 swf_ResetWriteBits(t);
194 if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
195 t->data[t->len++] = a[0];
196 t->data[t->len++] = a[1];
197 t->data[t->len++] = a[2];
198 t->data[t->len++] = a[3];
202 U32 swf_GetBits(TAG * t,int nbits)
204 if (!nbits) return 0;
205 if (!t->readBit) t->readBit = 0x80;
208 if (t->data[t->pos]&t->readBit) res|=1;
212 { if (nbits) t->readBit = 0x80;
215 { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
225 S32 swf_GetSBits(TAG * t,int nbits)
226 { U32 res = swf_GetBits(t,nbits);
227 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
231 U32 reader_GetBits(struct reader_t*reader, int nbits)
232 { return reader_readbits(reader, nbits);
234 S32 reader_GetSBits(struct reader_t*reader, int nbits)
235 { U32 res = reader_readbits(reader, nbits);
236 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
240 int swf_SetBits(TAG * t,U32 v,int nbits)
241 { U32 bm = 1<<(nbits-1);
245 { if (FAILED(swf_SetU8(t,0))) return -1;
248 if (v&bm) t->data[t->len-1] |= t->writeBit;
256 // Advanced Data Access Functions
258 int swf_SetRGB(TAG * t,RGBA * col)
261 { swf_SetU8(t,col->r);
264 } else swf_SetBlock(t,NULL,3);
267 void swf_GetRGB(TAG * t, RGBA * col)
272 col->r = swf_GetU8(t);
273 col->g = swf_GetU8(t);
274 col->b = swf_GetU8(t);
278 int swf_SetRGBA(TAG * t,RGBA * col)
281 { swf_SetU8(t,col->r);
285 } else swf_SetBlock(t,NULL,4);
288 void swf_GetRGBA(TAG * t, RGBA * col)
293 col->r = swf_GetU8(t);
294 col->g = swf_GetU8(t);
295 col->b = swf_GetU8(t);
296 col->a = swf_GetU8(t);
299 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
304 memset(gradient, 0, sizeof(GRADIENT));
309 gradient->num = swf_GetU8(tag);
310 for(t=0;t<gradient->num;t++)
315 gradient->ratios[t] = swf_GetU8(tag);
317 swf_GetRGB(tag, &gradient->rgba[t]);
319 swf_GetRGBA(tag, &gradient->rgba[t]);
323 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
327 memset(gradient, 0, sizeof(GRADIENT));
330 swf_SetU8(tag, gradient->num);
331 for(t=0; t<8 && t<gradient->num; t++)
333 swf_SetU8(tag, gradient->ratios[t]);
335 swf_SetRGB(tag, &gradient->rgba[t]);
337 swf_SetRGBA(tag, &gradient->rgba[t]);
341 int swf_CountUBits(U32 v,int nbits)
344 if(v == 0x00000000) n = 0;
350 return (n>nbits)?n:nbits;
353 int swf_CountBits(U32 v,int nbits)
357 { if(v == 0xffffffff) n = 1;
365 { if(v == 0x00000000) n = 0;
372 return (n>nbits)?n:nbits;
375 int swf_GetRect(TAG * t,SRECT * r)
378 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
380 nbits = (int) swf_GetBits(t,5);
381 r->xmin = swf_GetSBits(t,nbits);
382 r->xmax = swf_GetSBits(t,nbits);
383 r->ymin = swf_GetSBits(t,nbits);
384 r->ymax = swf_GetSBits(t,nbits);
388 int reader_GetRect(struct reader_t*reader,SRECT * r)
392 nbits = (int) reader_GetBits(reader,5);
393 r->xmin = reader_GetSBits(reader,nbits);
394 r->xmax = reader_GetSBits(reader,nbits);
395 r->ymin = reader_GetSBits(reader,nbits);
396 r->ymax = reader_GetSBits(reader,nbits);
400 int swf_SetRect(TAG * t,SRECT * r)
403 nbits = swf_CountBits(r->xmin,0);
404 nbits = swf_CountBits(r->xmax,nbits);
405 nbits = swf_CountBits(r->ymin,nbits);
406 nbits = swf_CountBits(r->ymax,nbits);
408 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
412 swf_SetBits(t,nbits,5);
413 swf_SetBits(t,r->xmin,nbits);
414 swf_SetBits(t,r->xmax,nbits);
415 swf_SetBits(t,r->ymin,nbits);
416 swf_SetBits(t,r->ymax,nbits);
421 void swf_ExpandRect(SRECT*src, SPOINT add)
423 if(add.x < src->xmin)
425 if(add.x > src->xmax)
427 if(add.y < src->ymin)
429 if(add.y > src->ymax)
432 void swf_ExpandRect2(SRECT*src, SRECT*add)
434 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
436 if(add->xmin < src->xmin)
437 src->xmin = add->xmin;
438 if(add->ymin < src->ymin)
439 src->ymin = add->ymin;
440 if(add->xmax > src->xmax)
441 src->xmax = add->xmax;
442 if(add->ymax > src->ymax)
443 src->ymax = add->ymax;
445 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
448 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
449 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
452 SRECT swf_TurnRect(SRECT r, MATRIX* m)
455 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
456 p1.x = r.xmin;p1.y = r.ymin;
457 p2.x = r.xmax;p2.y = r.ymin;
458 p3.x = r.xmin;p3.y = r.ymax;
459 p4.x = r.xmax;p4.y = r.ymax;
460 pp1 = swf_TurnPoint(p1, m);
461 pp2 = swf_TurnPoint(p2, m);
462 pp3 = swf_TurnPoint(p3, m);
463 pp4 = swf_TurnPoint(p4, m);
464 g.xmin = g.xmax = pp1.x;
465 g.ymin = g.ymax = pp1.y;
466 swf_ExpandRect(&g, pp2);
467 swf_ExpandRect(&g, pp3);
468 swf_ExpandRect(&g, pp4);
473 int swf_GetMatrix(TAG * t,MATRIX * m)
480 { m->sx = m->sy = 0x10000;
486 swf_ResetReadBits(t);
488 if (swf_GetBits(t,1))
489 { nbits = swf_GetBits(t,5);
490 m->sx = swf_GetSBits(t,nbits);
491 m->sy = swf_GetSBits(t,nbits);
493 else m->sx = m->sy = 0x10000;
495 if (swf_GetBits(t,1))
496 { nbits = swf_GetBits(t,5);
497 m->r0 = swf_GetSBits(t,nbits);
498 m->r1 = swf_GetSBits(t,nbits);
500 else m->r0 = m->r1 = 0x0;
502 nbits = swf_GetBits(t,5);
503 m->tx = swf_GetSBits(t,nbits);
504 m->ty = swf_GetSBits(t,nbits);
509 int swf_SetMatrix(TAG * t,MATRIX * m)
515 ma.sx = ma.sy = 0x10000;
520 swf_ResetWriteBits(t);
522 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
524 { swf_SetBits(t,1,1);
525 nbits = swf_CountBits(m->sx,0);
526 nbits = swf_CountBits(m->sy,nbits);
528 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
531 swf_SetBits(t,nbits,5);
532 swf_SetBits(t,m->sx,nbits);
533 swf_SetBits(t,m->sy,nbits);
536 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
538 { swf_SetBits(t,1,1);
539 nbits = swf_CountBits(m->r0,0);
540 nbits = swf_CountBits(m->r1,nbits);
542 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
545 swf_SetBits(t,nbits,5);
546 swf_SetBits(t,m->r0,nbits);
547 swf_SetBits(t,m->r1,nbits);
550 nbits = swf_CountBits(m->tx,0);
551 nbits = swf_CountBits(m->ty,nbits);
553 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
556 swf_SetBits(t,nbits,5);
557 swf_SetBits(t,m->tx,nbits);
558 swf_SetBits(t,m->ty,nbits);
563 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha) //FIXME: alpha should be type bool
571 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
572 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
576 swf_ResetReadBits(t);
577 hasadd = swf_GetBits(t,1);
578 hasmul = swf_GetBits(t,1);
579 nbits = swf_GetBits(t,4);
582 { cx->r0 = (S16)swf_GetSBits(t,nbits);
583 cx->g0 = (S16)swf_GetSBits(t,nbits);
584 cx->b0 = (S16)swf_GetSBits(t,nbits);
586 cx->a0 = (S16)swf_GetSBits(t,nbits);
590 { cx->r1 = (S16)swf_GetSBits(t,nbits);
591 cx->g1 = (S16)swf_GetSBits(t,nbits);
592 cx->b1 = (S16)swf_GetSBits(t,nbits);
594 cx->a1 = (S16)swf_GetSBits(t,nbits);
600 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
608 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
609 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
619 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
620 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
623 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
624 nbits = swf_CountBits((S32)cx->r0,nbits);
625 nbits = swf_CountBits((S32)cx->g0,nbits);
626 nbits = swf_CountBits((S32)cx->b0,nbits);
630 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
631 nbits = swf_CountBits((S32)cx->r1,nbits);
632 nbits = swf_CountBits((S32)cx->g1,nbits);
633 nbits = swf_CountBits((S32)cx->b1,nbits);
636 swf_ResetWriteBits(t);
637 swf_SetBits(t,hasadd?1:0,1);
638 swf_SetBits(t,hasmul?1:0,1);
639 swf_SetBits(t,nbits,4);
642 { swf_SetBits(t,cx->r0,nbits);
643 swf_SetBits(t,cx->g0,nbits);
644 swf_SetBits(t,cx->b0,nbits);
645 if (alpha) swf_SetBits(t,cx->a0,nbits);
649 { swf_SetBits(t,cx->r1,nbits);
650 swf_SetBits(t,cx->g1,nbits);
651 swf_SetBits(t,cx->b1,nbits);
652 if (alpha) swf_SetBits(t,cx->a1,nbits);
658 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
659 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
661 void swf_SetPassword(TAG * t, const char * password)
663 /* WARNING: crypt_md5 is not reentrant */
667 #if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) && defined(HAVE_TIME_H) && defined(HAVE_TIME)
669 salt[0] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
670 salt[1] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
674 fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
675 fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
678 md5string = crypt_md5(password, salt);
681 swf_SetString(t, md5string);
684 int swf_VerifyPassword(TAG * t, const char * password)
686 char*md5string1, *md5string2;
691 if(t->len >= 5 && t->pos==0 &&
696 printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
699 md5string1 = swf_GetString(t);
701 if(strncmp(md5string1, "$1$",3 )) {
702 fprintf(stderr, "rfxswf: no salt in pw string\n");
705 x = strchr(md5string1+3, '$');
707 fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
710 n = x-(md5string1+3);
711 salt = (char*)malloc(n+1);
712 memcpy(salt, md5string1+3, n);
715 md5string2 = crypt_md5(password, salt);
717 if(strcmp(md5string1, md5string2) != 0)
722 // Tag List Manipulating Functions
724 TAG * swf_InsertTag(TAG * after,U16 id)
727 t = (TAG *)malloc(sizeof(TAG));
729 { memset(t,0x00,sizeof(TAG));
735 t->next = after->next;
737 if (t->next) t->next->prev = t;
743 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
746 t = (TAG *)malloc(sizeof(TAG));
748 { memset(t,0x00,sizeof(TAG));
754 t->prev = before->prev;
756 if (t->prev) t->prev->next = t;
759 if(swf && swf->firstTag == before) {
765 void swf_ClearTag(TAG * t)
767 if (t->data) free(t->data);
776 void swf_ResetTag(TAG*tag, U16 id)
778 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
782 int swf_DeleteTag(TAG * t)
785 if (t->prev) t->prev->next = t->next;
786 if (t->next) t->next->prev = t->prev;
788 if (t->data) free(t->data);
793 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
799 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
807 if (reader->read(reader, &len, 4) != 4) return NULL;
811 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
812 // Sprite handling fix: Flatten sprite tree
814 t = (TAG *)malloc(sizeof(TAG));
819 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (2). (%d bytes)\n", sizeof(TAG));
824 memset(t,0x00,sizeof(TAG));
830 { t->data = (U8*)malloc(t->len);
834 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (3). (%d bytes)\n", t->len);
839 if (reader->read(reader, t->data, t->len) != t->len) return NULL;
851 int swf_DefineSprite_GetRealSize(TAG * t);
853 int swf_WriteTag2(struct writer_t*writer, TAG * t)
854 // returns tag length in bytes (incl. Header), -1 = Error
855 // writer = 0 -> no output
862 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
864 short_tag = len<0x3f;
868 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
869 if (writer->write(writer,raw,2)!=2)
872 fprintf(stderr,"WriteTag() failed: Short Header.\n");
879 raw[0] = SWAP16((t->id<<6)|0x3f);
880 if (writer->write(writer,raw,2)!=2)
883 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
889 if (writer->write(writer,&len,4)!=4)
892 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
899 { if (writer->write(writer,t->data,t->len)!=t->len)
902 fprintf(stderr,"WriteTag() failed: Data.\n");
908 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
912 return t->len+(short_tag?2:6);
915 int swf_WriteTag(int handle, TAG * t)
917 struct writer_t writer;
919 return swf_WriteTag2(0, t);
920 writer_init_filewriter(&writer, handle);
921 return swf_WriteTag2(&writer, t);
924 int swf_DefineSprite_GetRealSize(TAG * t)
925 // Sprite Handling: Helper function to pack DefineSprite-Tag
927 if(len>4) { // folded sprite
931 { t = swf_NextTag(t);
932 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
934 } while (t&&(t->id!=ST_END));
938 void swf_UnFoldSprite(TAG * t)
943 U16 spriteid,spriteframes;
945 if(t->id!=ST_DEFINESPRITE)
947 if(t->len<=4) // not folded
952 spriteid = swf_GetU16(t); //id
953 spriteframes = swf_GetU16(t); //frames
965 if(id == ST_DEFINESPRITE && len<=4)
970 it = swf_InsertTag(next, id);
975 { it->data = (U8*)malloc(it->len);
976 it->memsize = it->len;
977 swf_GetBlock(t, it->data, it->len);
984 free(t->data); t->data = 0;
985 t->memsize = t->len = t->pos = 0;
987 swf_SetU16(t, spriteid);
988 swf_SetU16(t, spriteframes);
991 void swf_FoldSprite(TAG * t)
996 if(t->id!=ST_DEFINESPRITE)
999 fprintf(stderr, "Error: Sprite has no ID!");
1003 /* sprite is already folded */
1010 t->len = t->pos = t->memsize = 0;
1015 t = swf_NextTag(sprtag);
1020 if(t->id==ST_SHOWFRAME) frames++;
1021 if(t->id == ST_DEFINESPRITE && t->len<=4)
1026 } while(t && level);
1028 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1030 swf_SetU16(sprtag, id);
1031 swf_SetU16(sprtag, frames);
1033 t = swf_NextTag(sprtag);
1039 swf_SetU16(sprtag,t->len|(t->id<<6));
1041 swf_SetU16(sprtag,0x3f|(t->id<<6));
1042 swf_SetU32(sprtag,t->len);
1045 swf_SetBlock(sprtag,t->data, t->len);
1047 if(t->id == ST_DEFINESPRITE && t->len<=4)
1056 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1058 // sprtag->next = t;
1059 // t->prev = sprtag;
1062 int swf_IsFolded(TAG * t)
1064 return (t->id == ST_DEFINESPRITE && t->len>4);
1067 void swf_FoldAll(SWF*swf)
1069 TAG*tag = swf->firstTag;
1070 //swf_DumpSWF(stdout, swf);
1072 if(tag->id == ST_DEFINESPRITE) {
1073 swf_FoldSprite(tag);
1074 //swf_DumpSWF(stdout, swf);
1076 tag = swf_NextTag(tag);
1080 void swf_FoldAllTags(TAG*tag)
1083 memset(&swf, 0, sizeof(swf));
1088 void swf_UnFoldAll(SWF*swf)
1090 TAG*tag = swf->firstTag;
1092 if(tag->id == ST_DEFINESPRITE)
1093 swf_UnFoldSprite(tag);
1098 void swf_OptimizeTagOrder(SWF*swf)
1105 /* at the moment, we don't actually do optimizing,
1106 only fixing of non-spec-conformant things like
1113 tag = swf->firstTag;
1116 if(tag->id == ST_DEFINESPRITE) {
1118 /* ??? all sprites are supposed to be unfolded */
1119 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1129 /* move non-sprite tags out of sprite */
1130 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1131 /* remove tag from current position */
1132 tag->prev->next = tag->next;
1134 tag->next->prev = tag->prev;
1136 /* insert before tag level0 */
1138 tag->prev = level0->prev;
1140 tag->prev->next = tag;
1144 if(tag->id == ST_END) {
1155 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1157 if (!swf) return -1;
1158 memset(swf,0x00,sizeof(SWF));
1160 { char b[32]; // read Header
1164 struct reader_t zreader;
1166 if ((len = reader->read(reader ,b,8))<8) return -1;
1168 if (b[0]!='F' && b[0]!='C') return -1;
1169 if (b[1]!='W') return -1;
1170 if (b[2]!='S') return -1;
1171 swf->fileVersion = b[3];
1172 swf->compressed = (b[0]=='C')?1:0;
1173 swf->fileSize = GET32(&b[4]);
1175 if(swf->compressed) {
1176 reader_init_zlibinflate(&zreader, reader);
1180 reader_GetRect(reader, &swf->movieSize);
1181 reader->read(reader, &swf->frameRate, 2);
1182 swf->frameRate = SWAP16(swf->frameRate);
1183 reader->read(reader, &swf->frameCount, 2);
1184 swf->frameCount = SWAP16(swf->frameCount);
1186 /* read tags and connect to list */
1188 while (t) t = swf_ReadTag(reader,t);
1189 swf->firstTag = t1.next;
1190 t1.next->prev = NULL;
1196 int swf_ReadSWF(int handle, SWF * swf)
1198 struct reader_t reader;
1199 reader_init_filereader(&reader, handle);
1200 return swf_ReadSWF2(&reader, swf);
1203 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1207 struct writer_t zwriter;
1210 if (!swf) return -1;
1212 // Insert REFLEX Tag
1214 #ifdef INSERT_RFX_TAG
1216 if (swf->firstTag && swf_NextTag(swf->firstTag))
1217 if (swf_GetTagID(swf_NextTag(swf->firstTag))!=ST_REFLEX)
1218 swf_SetBlock(swf_InsertTag(swf->firstTag,ST_REFLEX),"rfx",3);
1220 #endif // INSERT_RFX_TAG
1222 // Count Frames + File Size
1229 { len += swf_WriteTag(-1, t);
1230 if (t->id==ST_SHOWFRAME) frameCount++;
1238 memset(&t1,0x00,sizeof(TAG));
1242 { // measure header file size
1245 memset(&t2,0x00,sizeof(TAG));
1248 swf_SetRect(&t2, &swf->movieSize);
1249 swf_SetU16(&t2, swf->frameRate);
1250 swf_SetU16(&t2, swf->frameCount);
1251 l = swf_GetTagLen(&t2)+8;
1253 if(swf->compressed == 8) {
1258 if(len) {// don't touch headers without tags
1259 swf->fileSize = fileSize;
1260 swf->frameCount = frameCount;
1263 if(swf->compressed != 8) {
1264 /* compressed flag set to 8 means "skip first 8
1265 header bytes". This is necessary if the caller wants to
1266 create compressed SWFs himself */
1267 if(swf->compressed) {
1269 writer->write(writer, id, 3);
1273 writer->write(writer, id, 3);
1276 writer->write(writer, &swf->fileVersion, 1);
1277 PUT32(b4, swf->fileSize);
1278 writer->write(writer, b4, 4);
1280 if(swf->compressed) {
1281 writer_init_zlibdeflate(&zwriter, writer);
1286 swf_SetRect(&t1,&swf->movieSize);
1287 swf_SetU16(&t1,swf->frameRate);
1288 swf_SetU16(&t1,swf->frameCount);
1292 int ret = writer->write(writer,b,swf_GetTagLen(&t1));
1293 if (ret!=swf_GetTagLen(&t1))
1296 fprintf(stderr, "ret:%d\n",ret);
1298 fprintf(stderr,"WriteSWF() failed: Header.\n");
1305 { if (swf_WriteTag2(writer, t)<0) return -1;
1308 if(swf->compressed != 8)
1309 writer->finish(writer); // flush zlib buffers - only if _we_ initialized that writer.
1312 return (int)fileSize;
1315 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1317 struct writer_t writer;
1318 swf->compressed = 0;
1320 writer_init_nullwriter(&writer);
1321 return swf_WriteSWF2(&writer, swf);
1323 writer_init_filewriter(&writer, handle);
1324 return swf_WriteSWF2(&writer, swf);
1327 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1329 struct writer_t writer;
1330 swf->compressed = 1;
1332 writer_init_nullwriter(&writer);
1333 return swf_WriteSWF2(&writer, swf);
1335 writer_init_filewriter(&writer, handle);
1336 return swf_WriteSWF2(&writer, swf);
1339 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1342 memcpy(&myswf,swf,sizeof(SWF));
1344 return swf_WriteSWF2(writer, &myswf);
1347 int swf_WriteHeader(int handle,SWF * swf)
1350 memcpy(&myswf,swf,sizeof(SWF));
1352 return swf_WriteSWF(handle, &myswf);
1355 int swf_WriteCGI(SWF * swf)
1359 len = swf_WriteSWF(-1,swf);
1361 if (len<0) return -1;
1363 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1364 "Accept-Ranges: bytes\n"
1365 "Content-Length: %lu\n"
1366 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1369 write(fileno(stdout),s,strlen(s));
1370 return swf_WriteSWF(fileno(stdout),swf);
1373 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1374 { TAG * t = swf->firstTag;
1377 { TAG * tnew = t->next;
1378 if (t->data) free(t->data);
1384 // include advanced functions
1386 #include "modules/swfdump.c"
1387 #include "modules/swfshape.c"
1388 #include "modules/swftext.c"
1389 #include "modules/swffont.c"
1390 #include "modules/swfobject.c"
1391 #include "modules/swfbutton.c"
1392 #include "modules/swftools.c"
1393 #include "modules/swfcgi.c"
1394 #include "modules/swfbits.c"
1395 #include "modules/swfaction.c"
1396 #include "modules/swfsound.c"
1397 #include "modules/swfdraw.c"