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 // for future purpose: avoid high level lib functions to change tagpos/bitpos
70 #define swf_SaveTagPos(tag)
71 #define swf_RestoreTagPos(tag)
73 void swf_SetTagPos(TAG * t,U32 pos)
74 { swf_ResetReadBits(t);
75 if (pos<=t->len) t->pos = pos;
78 fprintf(stderr,"SetTagPos(%d) out of bounds: TagID = %i\n",pos, t->id);
83 char* swf_GetString(TAG*t)
85 char* str = ((char*)(&(t)->data[(t)->pos]));
91 { swf_ResetReadBits(t);
94 { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
98 return t->data[t->pos++];
101 U16 swf_GetU16(TAG * t)
103 swf_ResetReadBits(t);
105 if (t->pos>(t->len-2))
106 { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
110 res = t->data[t->pos] | (t->data[t->pos+1]<<8);
115 U32 swf_GetU32(TAG * t)
117 swf_ResetReadBits(t);
119 if (t->pos>(t->len-4))
120 { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
124 res = t->data[t->pos] | (t->data[t->pos+1]<<8) |
125 (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
130 int swf_GetBlock(TAG * t,U8 * b,int l)
131 // returns number of bytes written (<=l)
132 // b = NULL -> skip data
133 { swf_ResetReadBits(t);
134 if ((t->len-t->pos)<l) l=t->len-t->pos;
135 if (b && l) memcpy(b,&t->data[t->pos],l);
140 int swf_SetBlock(TAG * t,U8 * b,int l)
141 // Appends Block to the end of Tagdata, returns size
142 { U32 newlen = t->len + l;
143 swf_ResetWriteBits(t);
144 if (newlen>t->memsize)
145 { U32 newmem = MEMSIZE(newlen);
146 U8 * newdata = (U8*)((t->data)?realloc(t->data,newmem):malloc(newmem));
150 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (1). (%d bytes)\n", newmem);
158 if (b) memcpy(&t->data[t->len],b,l);
159 else memset(&t->data[t->len],0x00,l);
164 int swf_SetU8(TAG * t,U8 v)
165 { swf_ResetWriteBits(t);
166 if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
167 t->data[t->len++] = v;
171 int swf_SetU16(TAG * t,U16 v)
176 swf_ResetWriteBits(t);
177 if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
178 t->data[t->len++] = a[0];
179 t->data[t->len++] = a[1];
183 int swf_SetU32(TAG * t,U32 v)
185 a[0] = v&0xff; // to ensure correct handling of non-intel byteorder
190 swf_ResetWriteBits(t);
191 if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
192 t->data[t->len++] = a[0];
193 t->data[t->len++] = a[1];
194 t->data[t->len++] = a[2];
195 t->data[t->len++] = a[3];
199 U32 swf_GetBits(TAG * t,int nbits)
201 if (!nbits) return 0;
202 if (!t->readBit) t->readBit = 0x80;
205 if (t->data[t->pos]&t->readBit) res|=1;
209 { if (nbits) t->readBit = 0x80;
212 { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
222 S32 swf_GetSBits(TAG * t,int nbits)
223 { U32 res = swf_GetBits(t,nbits);
224 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
228 U32 reader_GetBits(struct reader_t*reader, int nbits)
229 { return reader_readbits(reader, nbits);
231 S32 reader_GetSBits(struct reader_t*reader, int nbits)
232 { U32 res = reader_readbits(reader, nbits);
233 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
237 int swf_SetBits(TAG * t,U32 v,int nbits)
238 { U32 bm = 1<<(nbits-1);
242 { if (FAILED(swf_SetU8(t,0))) return -1;
245 if (v&bm) t->data[t->len-1] |= t->writeBit;
253 // Advanced Data Access Functions
255 int swf_SetRGB(TAG * t,RGBA * col)
258 { swf_SetU8(t,col->r);
261 } else swf_SetBlock(t,NULL,3);
264 void swf_GetRGB(TAG * t, RGBA * col)
269 col->r = swf_GetU8(t);
270 col->g = swf_GetU8(t);
271 col->b = swf_GetU8(t);
275 int swf_SetRGBA(TAG * t,RGBA * col)
278 { swf_SetU8(t,col->r);
282 } else swf_SetBlock(t,NULL,4);
285 void swf_GetRGBA(TAG * t, RGBA * col)
290 col->r = swf_GetU8(t);
291 col->g = swf_GetU8(t);
292 col->b = swf_GetU8(t);
293 col->a = swf_GetU8(t);
296 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
301 memset(gradient, 0, sizeof(GRADIENT));
306 gradient->num = swf_GetU8(tag);
307 for(t=0;t<gradient->num;t++)
312 gradient->ratios[t] = swf_GetU8(tag);
314 swf_GetRGB(tag, &gradient->rgba[t]);
316 swf_GetRGBA(tag, &gradient->rgba[t]);
320 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
324 memset(gradient, 0, sizeof(GRADIENT));
327 swf_SetU8(tag, gradient->num);
328 for(t=0; t<8 && t<gradient->num; t++)
330 swf_SetU8(tag, gradient->ratios[t]);
332 swf_SetRGB(tag, &gradient->rgba[t]);
334 swf_SetRGBA(tag, &gradient->rgba[t]);
338 int swf_CountUBits(U32 v,int nbits)
341 if(v == 0x00000000) n = 0;
347 return (n>nbits)?n:nbits;
350 int swf_CountBits(U32 v,int nbits)
354 { if(v == 0xffffffff) n = 1;
362 { if(v == 0x00000000) n = 0;
369 return (n>nbits)?n:nbits;
372 int swf_GetRect(TAG * t,SRECT * r)
375 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
377 nbits = (int) swf_GetBits(t,5);
378 r->xmin = swf_GetSBits(t,nbits);
379 r->xmax = swf_GetSBits(t,nbits);
380 r->ymin = swf_GetSBits(t,nbits);
381 r->ymax = swf_GetSBits(t,nbits);
385 int reader_GetRect(struct reader_t*reader,SRECT * r)
389 nbits = (int) reader_GetBits(reader,5);
390 r->xmin = reader_GetSBits(reader,nbits);
391 r->xmax = reader_GetSBits(reader,nbits);
392 r->ymin = reader_GetSBits(reader,nbits);
393 r->ymax = reader_GetSBits(reader,nbits);
397 int swf_SetRect(TAG * t,SRECT * r)
400 nbits = swf_CountBits(r->xmin,0);
401 nbits = swf_CountBits(r->xmax,nbits);
402 nbits = swf_CountBits(r->ymin,nbits);
403 nbits = swf_CountBits(r->ymax,nbits);
405 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
409 swf_SetBits(t,nbits,5);
410 swf_SetBits(t,r->xmin,nbits);
411 swf_SetBits(t,r->xmax,nbits);
412 swf_SetBits(t,r->ymin,nbits);
413 swf_SetBits(t,r->ymax,nbits);
418 void swf_ExpandRect(SRECT*src, SPOINT add)
420 if(add.x < src->xmin)
422 if(add.x > src->xmax)
424 if(add.y < src->ymin)
426 if(add.y > src->ymax)
429 void swf_ExpandRect2(SRECT*src, SRECT*add)
431 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
433 if((src->xmin | src->ymin | src->xmax | src->ymax)==0)
435 if(add->xmin < src->xmin)
436 src->xmin = add->xmin;
437 if(add->ymin < src->ymin)
438 src->ymin = add->ymin;
439 if(add->xmax > src->xmax)
440 src->xmax = add->xmax;
441 if(add->ymax > src->ymax)
442 src->ymax = add->ymax;
444 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
447 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
448 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
451 SRECT swf_TurnRect(SRECT r, MATRIX* m)
454 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
455 p1.x = r.xmin;p1.y = r.ymin;
456 p2.x = r.xmax;p2.y = r.ymin;
457 p3.x = r.xmin;p3.y = r.ymax;
458 p4.x = r.xmax;p4.y = r.ymax;
459 pp1 = swf_TurnPoint(p1, m);
460 pp2 = swf_TurnPoint(p2, m);
461 pp3 = swf_TurnPoint(p3, m);
462 pp4 = swf_TurnPoint(p4, m);
463 g.xmin = g.xmax = pp1.x;
464 g.ymin = g.ymax = pp1.y;
465 swf_ExpandRect(&g, pp2);
466 swf_ExpandRect(&g, pp3);
467 swf_ExpandRect(&g, pp4);
472 int swf_GetMatrix(TAG * t,MATRIX * m)
479 { m->sx = m->sy = 0x10000;
485 swf_ResetReadBits(t);
487 if (swf_GetBits(t,1))
488 { nbits = swf_GetBits(t,5);
489 m->sx = swf_GetSBits(t,nbits);
490 m->sy = swf_GetSBits(t,nbits);
492 else m->sx = m->sy = 0x10000;
494 if (swf_GetBits(t,1))
495 { nbits = swf_GetBits(t,5);
496 m->r0 = swf_GetSBits(t,nbits);
497 m->r1 = swf_GetSBits(t,nbits);
499 else m->r0 = m->r1 = 0x0;
501 nbits = swf_GetBits(t,5);
502 m->tx = swf_GetSBits(t,nbits);
503 m->ty = swf_GetSBits(t,nbits);
508 int swf_SetMatrix(TAG * t,MATRIX * m)
514 ma.sx = ma.sy = 0x10000;
519 swf_ResetWriteBits(t);
521 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
523 { swf_SetBits(t,1,1);
524 nbits = swf_CountBits(m->sx,0);
525 nbits = swf_CountBits(m->sy,nbits);
527 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
530 swf_SetBits(t,nbits,5);
531 swf_SetBits(t,m->sx,nbits);
532 swf_SetBits(t,m->sy,nbits);
535 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
537 { swf_SetBits(t,1,1);
538 nbits = swf_CountBits(m->r0,0);
539 nbits = swf_CountBits(m->r1,nbits);
541 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
544 swf_SetBits(t,nbits,5);
545 swf_SetBits(t,m->r0,nbits);
546 swf_SetBits(t,m->r1,nbits);
549 nbits = swf_CountBits(m->tx,0);
550 nbits = swf_CountBits(m->ty,nbits);
552 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
555 swf_SetBits(t,nbits,5);
556 swf_SetBits(t,m->tx,nbits);
557 swf_SetBits(t,m->ty,nbits);
562 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha) //FIXME: alpha should be type bool
570 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
571 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
575 swf_ResetReadBits(t);
576 hasadd = swf_GetBits(t,1);
577 hasmul = swf_GetBits(t,1);
578 nbits = swf_GetBits(t,4);
581 { cx->r0 = (S16)swf_GetSBits(t,nbits);
582 cx->g0 = (S16)swf_GetSBits(t,nbits);
583 cx->b0 = (S16)swf_GetSBits(t,nbits);
585 cx->a0 = (S16)swf_GetSBits(t,nbits);
589 { cx->r1 = (S16)swf_GetSBits(t,nbits);
590 cx->g1 = (S16)swf_GetSBits(t,nbits);
591 cx->b1 = (S16)swf_GetSBits(t,nbits);
593 cx->a1 = (S16)swf_GetSBits(t,nbits);
599 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
607 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
608 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
618 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
619 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
622 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
623 nbits = swf_CountBits((S32)cx->r0,nbits);
624 nbits = swf_CountBits((S32)cx->g0,nbits);
625 nbits = swf_CountBits((S32)cx->b0,nbits);
629 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
630 nbits = swf_CountBits((S32)cx->r1,nbits);
631 nbits = swf_CountBits((S32)cx->g1,nbits);
632 nbits = swf_CountBits((S32)cx->b1,nbits);
635 swf_ResetWriteBits(t);
636 swf_SetBits(t,hasadd?1:0,1);
637 swf_SetBits(t,hasmul?1:0,1);
638 swf_SetBits(t,nbits,4);
641 { swf_SetBits(t,cx->r0,nbits);
642 swf_SetBits(t,cx->g0,nbits);
643 swf_SetBits(t,cx->b0,nbits);
644 if (alpha) swf_SetBits(t,cx->a0,nbits);
648 { swf_SetBits(t,cx->r1,nbits);
649 swf_SetBits(t,cx->g1,nbits);
650 swf_SetBits(t,cx->b1,nbits);
651 if (alpha) swf_SetBits(t,cx->a1,nbits);
657 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
658 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
660 void swf_SetPassword(TAG * t, const char * password)
662 /* WARNING: crypt_md5 is not reentrant */
666 #if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) && defined(HAVE_TIME_H) && defined(HAVE_TIME)
668 salt[0] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
669 salt[1] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
673 fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
674 fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
677 md5string = crypt_md5(password, salt);
680 swf_SetString(t, md5string);
683 int swf_VerifyPassword(TAG * t, const char * password)
685 char*md5string1, *md5string2;
690 if(t->len >= 5 && t->pos==0 &&
695 printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
698 md5string1 = swf_GetString(t);
700 if(strncmp(md5string1, "$1$",3 )) {
701 fprintf(stderr, "rfxswf: no salt in pw string\n");
704 x = strchr(md5string1+3, '$');
706 fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
709 n = x-(md5string1+3);
710 salt = (char*)malloc(n+1);
711 memcpy(salt, md5string1+3, n);
714 md5string2 = crypt_md5(password, salt);
716 if(strcmp(md5string1, md5string2) != 0)
721 // Tag List Manipulating Functions
723 TAG * swf_InsertTag(TAG * after,U16 id)
726 t = (TAG *)malloc(sizeof(TAG));
728 { memset(t,0x00,sizeof(TAG));
734 t->next = after->next;
736 if (t->next) t->next->prev = t;
742 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
745 t = (TAG *)malloc(sizeof(TAG));
747 { memset(t,0x00,sizeof(TAG));
753 t->prev = before->prev;
755 if (t->prev) t->prev->next = t;
758 if(swf && swf->firstTag == before) {
764 void swf_ClearTag(TAG * t)
766 if (t->data) free(t->data);
775 void swf_ResetTag(TAG*tag, U16 id)
777 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
781 int swf_DeleteTag(TAG * t)
784 if (t->prev) t->prev->next = t->next;
785 if (t->next) t->next->prev = t->prev;
787 if (t->data) free(t->data);
792 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
798 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
806 if (reader->read(reader, &len, 4) != 4) return NULL;
810 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
811 // Sprite handling fix: Flatten sprite tree
813 t = (TAG *)malloc(sizeof(TAG));
818 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (2). (%d bytes)\n", sizeof(TAG));
823 memset(t,0x00,sizeof(TAG));
829 { t->data = (U8*)malloc(t->len);
833 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (3). (%d bytes)\n", t->len);
838 if (reader->read(reader, t->data, t->len) != t->len) return NULL;
850 int swf_DefineSprite_GetRealSize(TAG * t);
852 int swf_WriteTag2(struct writer_t*writer, TAG * t)
853 // returns tag length in bytes (incl. Header), -1 = Error
854 // writer = 0 -> no output
861 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
863 short_tag = len<0x3f&&(t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2);
867 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
868 if (writer->write(writer,raw,2)!=2)
871 fprintf(stderr,"WriteTag() failed: Short Header.\n");
878 raw[0] = SWAP16((t->id<<6)|0x3f);
879 if (writer->write(writer,raw,2)!=2)
882 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
888 if (writer->write(writer,&len,4)!=4)
891 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
898 { if (writer->write(writer,t->data,t->len)!=t->len)
901 fprintf(stderr,"WriteTag() failed: Data.\n");
907 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
911 return t->len+(short_tag?2:6);
914 int swf_WriteTag(int handle, TAG * t)
916 struct writer_t writer;
919 return swf_WriteTag2(0, t);
920 writer_init_filewriter(&writer, handle);
921 len = swf_WriteTag2(&writer, t);
922 writer.finish(&writer);
926 int swf_DefineSprite_GetRealSize(TAG * t)
927 // Sprite Handling: Helper function to pack DefineSprite-Tag
929 if(len>4) { // folded sprite
933 { t = swf_NextTag(t);
934 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
936 } while (t&&(t->id!=ST_END));
940 void swf_UnFoldSprite(TAG * t)
945 U16 spriteid,spriteframes;
947 if(t->id!=ST_DEFINESPRITE)
949 if(t->len<=4) // not folded
954 spriteid = swf_GetU16(t); //id
955 spriteframes = swf_GetU16(t); //frames
967 if(id == ST_DEFINESPRITE && len<=4)
972 it = swf_InsertTag(next, id);
977 { it->data = (U8*)malloc(it->len);
978 it->memsize = it->len;
979 swf_GetBlock(t, it->data, it->len);
986 free(t->data); t->data = 0;
987 t->memsize = t->len = t->pos = 0;
989 swf_SetU16(t, spriteid);
990 swf_SetU16(t, spriteframes);
993 void swf_FoldSprite(TAG * t)
998 if(t->id!=ST_DEFINESPRITE)
1001 fprintf(stderr, "Error: Sprite has no ID!");
1005 /* sprite is already folded */
1012 t->len = t->pos = t->memsize = 0;
1017 t = swf_NextTag(sprtag);
1022 if(t->id==ST_SHOWFRAME) frames++;
1023 if(t->id == ST_DEFINESPRITE && t->len<=4)
1028 } while(t && level);
1030 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1032 swf_SetU16(sprtag, id);
1033 swf_SetU16(sprtag, frames);
1035 t = swf_NextTag(sprtag);
1040 if(t->len<0x3f&&t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2) {
1041 swf_SetU16(sprtag,t->len|(t->id<<6));
1043 swf_SetU16(sprtag,0x3f|(t->id<<6));
1044 swf_SetU32(sprtag,t->len);
1047 swf_SetBlock(sprtag,t->data, t->len);
1049 if(t->id == ST_DEFINESPRITE && t->len<=4)
1058 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1060 // sprtag->next = t;
1061 // t->prev = sprtag;
1064 int swf_IsFolded(TAG * t)
1066 return (t->id == ST_DEFINESPRITE && t->len>4);
1069 void swf_FoldAll(SWF*swf)
1071 TAG*tag = swf->firstTag;
1072 //swf_DumpSWF(stdout, swf);
1074 if(tag->id == ST_DEFINESPRITE) {
1075 swf_FoldSprite(tag);
1076 //swf_DumpSWF(stdout, swf);
1078 tag = swf_NextTag(tag);
1082 void swf_UnFoldAll(SWF*swf)
1084 TAG*tag = swf->firstTag;
1086 if(tag->id == ST_DEFINESPRITE)
1087 swf_UnFoldSprite(tag);
1092 void swf_OptimizeTagOrder(SWF*swf)
1099 /* at the moment, we don't actually do optimizing,
1100 only fixing of non-spec-conformant things like
1107 tag = swf->firstTag;
1110 if(tag->id == ST_DEFINESPRITE) {
1112 /* ??? all sprites are supposed to be unfolded */
1113 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1123 /* move non-sprite tags out of sprite */
1124 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1125 /* remove tag from current position */
1126 tag->prev->next = tag->next;
1128 tag->next->prev = tag->prev;
1130 /* insert before tag level0 */
1132 tag->prev = level0->prev;
1134 tag->prev->next = tag;
1138 if(tag->id == ST_END) {
1149 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1151 if (!swf) return -1;
1152 memset(swf,0x00,sizeof(SWF));
1154 { char b[32]; // read Header
1158 struct reader_t zreader;
1160 if ((len = reader->read(reader ,b,8))<8) return -1;
1162 if (b[0]!='F' && b[0]!='C') return -1;
1163 if (b[1]!='W') return -1;
1164 if (b[2]!='S') return -1;
1165 swf->fileVersion = b[3];
1166 swf->compressed = (b[0]=='C')?1:0;
1167 swf->fileSize = GET32(&b[4]);
1169 if(swf->compressed) {
1170 reader_init_zlibinflate(&zreader, reader);
1174 reader_GetRect(reader, &swf->movieSize);
1175 reader->read(reader, &swf->frameRate, 2);
1176 swf->frameRate = SWAP16(swf->frameRate);
1177 reader->read(reader, &swf->frameCount, 2);
1178 swf->frameCount = SWAP16(swf->frameCount);
1180 /* read tags and connect to list */
1182 while (t) t = swf_ReadTag(reader,t);
1183 swf->firstTag = t1.next;
1184 t1.next->prev = NULL;
1190 int swf_ReadSWF(int handle, SWF * swf)
1192 struct reader_t reader;
1193 reader_init_filereader(&reader, handle);
1194 return swf_ReadSWF2(&reader, swf);
1197 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1201 struct writer_t zwriter;
1204 int writer_lastpos = 0;
1207 if (!swf) return -1;
1208 if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1210 if(writer) writer_lastpos = writer->pos;
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_InsertTagBefore(swf, 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_DEFINESPRITE) inSprite++;
1231 else if(t->id == ST_END && inSprite) inSprite--;
1232 else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1240 memset(&t1,0x00,sizeof(TAG));
1244 { // measure header file size
1247 memset(&t2,0x00,sizeof(TAG));
1250 swf_SetRect(&t2, &swf->movieSize);
1251 swf_SetU16(&t2, swf->frameRate);
1252 swf_SetU16(&t2, swf->frameCount);
1253 l = swf_GetTagLen(&t2)+8;
1255 if(swf->compressed == 8) {
1260 if(len) {// don't touch headers without tags
1261 swf->fileSize = fileSize;
1262 swf->frameCount = frameCount;
1265 if(swf->compressed != 8) {
1266 /* compressed flag set to 8 means "skip first 8
1267 header bytes". This is necessary if the caller wants to
1268 create compressed SWFs himself .
1269 It also means that we don't initialize our own zlib
1270 writer, but assume the caller provided one.
1272 if(swf->compressed) {
1274 writer->write(writer, id, 3);
1278 writer->write(writer, id, 3);
1281 writer->write(writer, &swf->fileVersion, 1);
1282 PUT32(b4, swf->fileSize);
1283 writer->write(writer, b4, 4);
1285 if(swf->compressed) {
1286 writer_init_zlibdeflate(&zwriter, writer);
1291 swf_SetRect(&t1,&swf->movieSize);
1292 swf_SetU16(&t1,swf->frameRate);
1293 swf_SetU16(&t1,swf->frameCount);
1295 ret = writer->write(writer,b,swf_GetTagLen(&t1));
1296 if (ret!=swf_GetTagLen(&t1))
1299 fprintf(stderr, "ret:%d\n",ret);
1301 fprintf(stderr,"WriteSWF() failed: Header.\n");
1308 { if (swf_WriteTag2(writer, t)<0) return -1;
1311 if(swf->compressed) {
1312 if(swf->compressed != 8) {
1313 zwriter.finish(&zwriter);
1314 return writer->pos - writer_lastpos;
1316 return (int)fileSize;
1318 return (int)fileSize;
1323 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1325 struct writer_t writer;
1327 swf->compressed = 0;
1330 writer_init_nullwriter(&writer);
1331 len = swf_WriteSWF2(&writer, swf);
1333 writer_init_filewriter(&writer, handle);
1334 len = swf_WriteSWF2(&writer, swf);
1335 writer.finish(&writer);
1339 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1341 struct writer_t writer;
1343 swf->compressed = 1;
1346 writer_init_nullwriter(&writer);
1347 len = swf_WriteSWF2(&writer, swf);
1349 writer_init_filewriter(&writer, handle);
1350 len = swf_WriteSWF2(&writer, swf);
1351 writer.finish(&writer);
1355 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1358 memcpy(&myswf,swf,sizeof(SWF));
1360 return swf_WriteSWF2(writer, &myswf);
1363 int swf_WriteHeader(int handle,SWF * swf)
1366 memcpy(&myswf,swf,sizeof(SWF));
1368 return swf_WriteSWF(handle, &myswf);
1371 int swf_WriteCGI(SWF * swf)
1375 len = swf_WriteSWF(-1,swf);
1377 if (len<0) return -1;
1379 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1380 "Accept-Ranges: bytes\n"
1381 "Content-Length: %lu\n"
1382 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1385 write(fileno(stdout),s,strlen(s));
1386 return swf_WriteSWF(fileno(stdout),swf);
1389 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1390 { TAG * t = swf->firstTag;
1393 { TAG * tnew = t->next;
1394 if (t->data) free(t->data);
1401 // include advanced functions
1403 #include "modules/swfdump.c"
1404 #include "modules/swfshape.c"
1405 #include "modules/swftext.c"
1406 #include "modules/swffont.c"
1407 #include "modules/swfobject.c"
1408 #include "modules/swfbutton.c"
1409 #include "modules/swftools.c"
1410 #include "modules/swfcgi.c"
1411 #include "modules/swfbits.c"
1412 #include "modules/swfaction.c"
1413 #include "modules/swfsound.c"
1414 #include "modules/swfdraw.c"