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_UnFoldAll(SWF*swf)
1082 TAG*tag = swf->firstTag;
1084 if(tag->id == ST_DEFINESPRITE)
1085 swf_UnFoldSprite(tag);
1090 void swf_OptimizeTagOrder(SWF*swf)
1097 /* at the moment, we don't actually do optimizing,
1098 only fixing of non-spec-conformant things like
1105 tag = swf->firstTag;
1108 if(tag->id == ST_DEFINESPRITE) {
1110 /* ??? all sprites are supposed to be unfolded */
1111 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1121 /* move non-sprite tags out of sprite */
1122 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1123 /* remove tag from current position */
1124 tag->prev->next = tag->next;
1126 tag->next->prev = tag->prev;
1128 /* insert before tag level0 */
1130 tag->prev = level0->prev;
1132 tag->prev->next = tag;
1136 if(tag->id == ST_END) {
1147 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1149 if (!swf) return -1;
1150 memset(swf,0x00,sizeof(SWF));
1152 { char b[32]; // read Header
1156 struct reader_t zreader;
1158 if ((len = reader->read(reader ,b,8))<8) return -1;
1160 if (b[0]!='F' && b[0]!='C') return -1;
1161 if (b[1]!='W') return -1;
1162 if (b[2]!='S') return -1;
1163 swf->fileVersion = b[3];
1164 swf->compressed = (b[0]=='C')?1:0;
1165 swf->fileSize = GET32(&b[4]);
1167 if(swf->compressed) {
1168 reader_init_zlibinflate(&zreader, reader);
1172 reader_GetRect(reader, &swf->movieSize);
1173 reader->read(reader, &swf->frameRate, 2);
1174 swf->frameRate = SWAP16(swf->frameRate);
1175 reader->read(reader, &swf->frameCount, 2);
1176 swf->frameCount = SWAP16(swf->frameCount);
1178 /* read tags and connect to list */
1180 while (t) t = swf_ReadTag(reader,t);
1181 swf->firstTag = t1.next;
1182 t1.next->prev = NULL;
1188 int swf_ReadSWF(int handle, SWF * swf)
1190 struct reader_t reader;
1191 reader_init_filereader(&reader, handle);
1192 return swf_ReadSWF2(&reader, swf);
1195 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1199 struct writer_t zwriter;
1202 if (!swf) return -1;
1204 // Insert REFLEX Tag
1206 #ifdef INSERT_RFX_TAG
1208 if (swf->firstTag && swf_NextTag(swf->firstTag))
1209 if (swf_GetTagID(swf_NextTag(swf->firstTag))!=ST_REFLEX)
1210 swf_SetBlock(swf_InsertTag(swf->firstTag,ST_REFLEX),"rfx",3);
1212 #endif // INSERT_RFX_TAG
1214 // Count Frames + File Size
1221 { len += swf_WriteTag(-1, t);
1222 if (t->id==ST_SHOWFRAME) frameCount++;
1230 memset(&t1,0x00,sizeof(TAG));
1234 { // measure header file size
1237 memset(&t2,0x00,sizeof(TAG));
1240 swf_SetRect(&t2, &swf->movieSize);
1241 swf_SetU16(&t2, swf->frameRate);
1242 swf_SetU16(&t2, swf->frameCount);
1243 l = swf_GetTagLen(&t2)+8;
1245 if(swf->compressed == 8) {
1250 if(len) {// don't touch headers without tags
1251 swf->fileSize = fileSize;
1252 swf->frameCount = frameCount;
1255 if(swf->compressed != 8) {
1256 /* compressed flag set to 8 means "skip first 8
1257 header bytes". This is necessary if the caller wants to
1258 create compressed SWFs himself */
1259 if(swf->compressed) {
1261 writer->write(writer, id, 3);
1265 writer->write(writer, id, 3);
1268 writer->write(writer, &swf->fileVersion, 1);
1269 PUT32(b4, swf->fileSize);
1270 writer->write(writer, b4, 4);
1272 if(swf->compressed) {
1273 writer_init_zlibdeflate(&zwriter, writer);
1278 swf_SetRect(&t1,&swf->movieSize);
1279 swf_SetU16(&t1,swf->frameRate);
1280 swf_SetU16(&t1,swf->frameCount);
1284 int ret = writer->write(writer,b,swf_GetTagLen(&t1));
1285 if (ret!=swf_GetTagLen(&t1))
1288 fprintf(stderr, "ret:%d\n",ret);
1290 fprintf(stderr,"WriteSWF() failed: Header.\n");
1297 { if (swf_WriteTag2(writer, t)<0) return -1;
1300 if(swf->compressed != 8)
1301 writer->finish(writer); // flush zlib buffers - only if _we_ initialized that writer.
1304 return (int)fileSize;
1307 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1309 struct writer_t writer;
1310 swf->compressed = 0;
1312 writer_init_nullwriter(&writer);
1313 return swf_WriteSWF2(&writer, swf);
1315 writer_init_filewriter(&writer, handle);
1316 return swf_WriteSWF2(&writer, swf);
1319 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1321 struct writer_t writer;
1322 swf->compressed = 1;
1324 writer_init_nullwriter(&writer);
1325 return swf_WriteSWF2(&writer, swf);
1327 writer_init_filewriter(&writer, handle);
1328 return swf_WriteSWF2(&writer, swf);
1331 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1334 memcpy(&myswf,swf,sizeof(SWF));
1336 return swf_WriteSWF2(writer, &myswf);
1339 int swf_WriteHeader(int handle,SWF * swf)
1342 memcpy(&myswf,swf,sizeof(SWF));
1344 return swf_WriteSWF(handle, &myswf);
1347 int swf_WriteCGI(SWF * swf)
1351 len = swf_WriteSWF(-1,swf);
1353 if (len<0) return -1;
1355 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1356 "Accept-Ranges: bytes\n"
1357 "Content-Length: %lu\n"
1358 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1361 write(fileno(stdout),s,strlen(s));
1362 return swf_WriteSWF(fileno(stdout),swf);
1365 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1366 { TAG * t = swf->firstTag;
1369 { TAG * tnew = t->next;
1370 if (t->data) free(t->data);
1376 // include advanced functions
1378 #include "modules/swfdump.c"
1379 #include "modules/swfshape.c"
1380 #include "modules/swftext.c"
1381 #include "modules/swffont.c"
1382 #include "modules/swfobject.c"
1383 #include "modules/swfbutton.c"
1384 #include "modules/swftools.c"
1385 #include "modules/swfcgi.c"
1386 #include "modules/swfbits.c"
1387 #include "modules/swfaction.c"
1388 #include "modules/swfsound.c"
1389 #include "modules/swfdraw.c"