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;
83 fprintf(stderr,"SetTagPos(%d) out of bounds: TagID = %i\n",pos, t->id);
88 char* swf_GetString(TAG*t)
90 char* str = ((char*)(&(t)->data[(t)->pos]));
96 { swf_ResetReadBits(t);
99 { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
103 return t->data[t->pos++];
106 U16 swf_GetU16(TAG * t)
108 swf_ResetReadBits(t);
110 if (t->pos>(t->len-2))
111 { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
115 res = t->data[t->pos] | (t->data[t->pos+1]<<8);
120 U32 swf_GetU32(TAG * t)
122 swf_ResetReadBits(t);
124 if (t->pos>(t->len-4))
125 { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
129 res = t->data[t->pos] | (t->data[t->pos+1]<<8) |
130 (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
135 int swf_GetBlock(TAG * t,U8 * b,int l)
136 // returns number of bytes written (<=l)
137 // b = NULL -> skip data
138 { swf_ResetReadBits(t);
139 if ((t->len-t->pos)<l) l=t->len-t->pos;
140 if (b && l) memcpy(b,&t->data[t->pos],l);
145 int swf_SetBlock(TAG * t,U8 * b,int l)
146 // Appends Block to the end of Tagdata, returns size
147 { U32 newlen = t->len + l;
148 swf_ResetWriteBits(t);
149 if (newlen>t->memsize)
150 { U32 newmem = MEMSIZE(newlen);
151 U8 * newdata = (U8*)((t->data)?realloc(t->data,newmem):malloc(newmem));
155 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (1). (%d bytes)\n", newmem);
163 if (b) memcpy(&t->data[t->len],b,l);
164 else memset(&t->data[t->len],0x00,l);
169 int swf_SetU8(TAG * t,U8 v)
170 { swf_ResetWriteBits(t);
171 if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
172 t->data[t->len++] = v;
176 int swf_SetU16(TAG * t,U16 v)
181 swf_ResetWriteBits(t);
182 if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
183 t->data[t->len++] = a[0];
184 t->data[t->len++] = a[1];
188 int swf_SetU32(TAG * t,U32 v)
190 a[0] = v&0xff; // to ensure correct handling of non-intel byteorder
195 swf_ResetWriteBits(t);
196 if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
197 t->data[t->len++] = a[0];
198 t->data[t->len++] = a[1];
199 t->data[t->len++] = a[2];
200 t->data[t->len++] = a[3];
204 U32 swf_GetBits(TAG * t,int nbits)
206 if (!nbits) return 0;
207 if (!t->readBit) t->readBit = 0x80;
210 if (t->data[t->pos]&t->readBit) res|=1;
214 { if (nbits) t->readBit = 0x80;
217 { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
227 S32 swf_GetSBits(TAG * t,int nbits)
228 { U32 res = swf_GetBits(t,nbits);
229 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
233 U32 reader_GetBits(struct reader_t*reader, int nbits)
234 { return reader_readbits(reader, nbits);
236 S32 reader_GetSBits(struct reader_t*reader, int nbits)
237 { U32 res = reader_readbits(reader, nbits);
238 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
242 int swf_SetBits(TAG * t,U32 v,int nbits)
243 { U32 bm = 1<<(nbits-1);
247 { if (FAILED(swf_SetU8(t,0))) return -1;
250 if (v&bm) t->data[t->len-1] |= t->writeBit;
258 // Advanced Data Access Functions
260 int swf_SetRGB(TAG * t,RGBA * col)
263 { swf_SetU8(t,col->r);
266 } else swf_SetBlock(t,NULL,3);
269 void swf_GetRGB(TAG * t, RGBA * col)
274 col->r = swf_GetU8(t);
275 col->g = swf_GetU8(t);
276 col->b = swf_GetU8(t);
280 int swf_SetRGBA(TAG * t,RGBA * col)
283 { swf_SetU8(t,col->r);
287 } else swf_SetBlock(t,NULL,4);
290 void swf_GetRGBA(TAG * t, RGBA * col)
295 col->r = swf_GetU8(t);
296 col->g = swf_GetU8(t);
297 col->b = swf_GetU8(t);
298 col->a = swf_GetU8(t);
301 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
306 memset(gradient, 0, sizeof(GRADIENT));
311 gradient->num = swf_GetU8(tag);
312 for(t=0;t<gradient->num;t++)
317 gradient->ratios[t] = swf_GetU8(tag);
319 swf_GetRGB(tag, &gradient->rgba[t]);
321 swf_GetRGBA(tag, &gradient->rgba[t]);
325 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
329 memset(gradient, 0, sizeof(GRADIENT));
332 swf_SetU8(tag, gradient->num);
333 for(t=0; t<8 && t<gradient->num; t++)
335 swf_SetU8(tag, gradient->ratios[t]);
337 swf_SetRGB(tag, &gradient->rgba[t]);
339 swf_SetRGBA(tag, &gradient->rgba[t]);
343 int swf_CountUBits(U32 v,int nbits)
346 if(v == 0x00000000) n = 0;
352 return (n>nbits)?n:nbits;
355 int swf_CountBits(U32 v,int nbits)
359 { if(v == 0xffffffff) n = 1;
367 { if(v == 0x00000000) n = 0;
374 return (n>nbits)?n:nbits;
377 int swf_GetRect(TAG * t,SRECT * r)
380 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
382 nbits = (int) swf_GetBits(t,5);
383 r->xmin = swf_GetSBits(t,nbits);
384 r->xmax = swf_GetSBits(t,nbits);
385 r->ymin = swf_GetSBits(t,nbits);
386 r->ymax = swf_GetSBits(t,nbits);
390 int reader_GetRect(struct reader_t*reader,SRECT * r)
394 nbits = (int) reader_GetBits(reader,5);
395 r->xmin = reader_GetSBits(reader,nbits);
396 r->xmax = reader_GetSBits(reader,nbits);
397 r->ymin = reader_GetSBits(reader,nbits);
398 r->ymax = reader_GetSBits(reader,nbits);
402 int swf_SetRect(TAG * t,SRECT * r)
405 nbits = swf_CountBits(r->xmin,0);
406 nbits = swf_CountBits(r->xmax,nbits);
407 nbits = swf_CountBits(r->ymin,nbits);
408 nbits = swf_CountBits(r->ymax,nbits);
410 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
414 swf_SetBits(t,nbits,5);
415 swf_SetBits(t,r->xmin,nbits);
416 swf_SetBits(t,r->xmax,nbits);
417 swf_SetBits(t,r->ymin,nbits);
418 swf_SetBits(t,r->ymax,nbits);
423 void swf_ExpandRect(SRECT*src, SPOINT add)
425 if(add.x < src->xmin)
427 if(add.x > src->xmax)
429 if(add.y < src->ymin)
431 if(add.y > src->ymax)
434 void swf_ExpandRect2(SRECT*src, SRECT*add)
436 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
438 if((src->xmin | src->ymin | src->xmax | src->ymax)==0)
440 if(add->xmin < src->xmin)
441 src->xmin = add->xmin;
442 if(add->ymin < src->ymin)
443 src->ymin = add->ymin;
444 if(add->xmax > src->xmax)
445 src->xmax = add->xmax;
446 if(add->ymax > src->ymax)
447 src->ymax = add->ymax;
449 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
452 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
453 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
456 SRECT swf_TurnRect(SRECT r, MATRIX* m)
459 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
460 p1.x = r.xmin;p1.y = r.ymin;
461 p2.x = r.xmax;p2.y = r.ymin;
462 p3.x = r.xmin;p3.y = r.ymax;
463 p4.x = r.xmax;p4.y = r.ymax;
464 pp1 = swf_TurnPoint(p1, m);
465 pp2 = swf_TurnPoint(p2, m);
466 pp3 = swf_TurnPoint(p3, m);
467 pp4 = swf_TurnPoint(p4, m);
468 g.xmin = g.xmax = pp1.x;
469 g.ymin = g.ymax = pp1.y;
470 swf_ExpandRect(&g, pp2);
471 swf_ExpandRect(&g, pp3);
472 swf_ExpandRect(&g, pp4);
477 int swf_GetMatrix(TAG * t,MATRIX * m)
484 { m->sx = m->sy = 0x10000;
490 swf_ResetReadBits(t);
492 if (swf_GetBits(t,1))
493 { nbits = swf_GetBits(t,5);
494 m->sx = swf_GetSBits(t,nbits);
495 m->sy = swf_GetSBits(t,nbits);
497 else m->sx = m->sy = 0x10000;
499 if (swf_GetBits(t,1))
500 { nbits = swf_GetBits(t,5);
501 m->r0 = swf_GetSBits(t,nbits);
502 m->r1 = swf_GetSBits(t,nbits);
504 else m->r0 = m->r1 = 0x0;
506 nbits = swf_GetBits(t,5);
507 m->tx = swf_GetSBits(t,nbits);
508 m->ty = swf_GetSBits(t,nbits);
513 int swf_SetMatrix(TAG * t,MATRIX * m)
519 ma.sx = ma.sy = 0x10000;
524 swf_ResetWriteBits(t);
526 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
528 { swf_SetBits(t,1,1);
529 nbits = swf_CountBits(m->sx,0);
530 nbits = swf_CountBits(m->sy,nbits);
532 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
535 swf_SetBits(t,nbits,5);
536 swf_SetBits(t,m->sx,nbits);
537 swf_SetBits(t,m->sy,nbits);
540 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
542 { swf_SetBits(t,1,1);
543 nbits = swf_CountBits(m->r0,0);
544 nbits = swf_CountBits(m->r1,nbits);
546 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
549 swf_SetBits(t,nbits,5);
550 swf_SetBits(t,m->r0,nbits);
551 swf_SetBits(t,m->r1,nbits);
554 nbits = swf_CountBits(m->tx,0);
555 nbits = swf_CountBits(m->ty,nbits);
557 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
560 swf_SetBits(t,nbits,5);
561 swf_SetBits(t,m->tx,nbits);
562 swf_SetBits(t,m->ty,nbits);
567 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha) //FIXME: alpha should be type bool
575 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
576 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
580 swf_ResetReadBits(t);
581 hasadd = swf_GetBits(t,1);
582 hasmul = swf_GetBits(t,1);
583 nbits = swf_GetBits(t,4);
586 { cx->r0 = (S16)swf_GetSBits(t,nbits);
587 cx->g0 = (S16)swf_GetSBits(t,nbits);
588 cx->b0 = (S16)swf_GetSBits(t,nbits);
590 cx->a0 = (S16)swf_GetSBits(t,nbits);
594 { cx->r1 = (S16)swf_GetSBits(t,nbits);
595 cx->g1 = (S16)swf_GetSBits(t,nbits);
596 cx->b1 = (S16)swf_GetSBits(t,nbits);
598 cx->a1 = (S16)swf_GetSBits(t,nbits);
604 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
612 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
613 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
623 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
624 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
627 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
628 nbits = swf_CountBits((S32)cx->r0,nbits);
629 nbits = swf_CountBits((S32)cx->g0,nbits);
630 nbits = swf_CountBits((S32)cx->b0,nbits);
634 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
635 nbits = swf_CountBits((S32)cx->r1,nbits);
636 nbits = swf_CountBits((S32)cx->g1,nbits);
637 nbits = swf_CountBits((S32)cx->b1,nbits);
640 swf_ResetWriteBits(t);
641 swf_SetBits(t,hasadd?1:0,1);
642 swf_SetBits(t,hasmul?1:0,1);
643 swf_SetBits(t,nbits,4);
646 { swf_SetBits(t,cx->r0,nbits);
647 swf_SetBits(t,cx->g0,nbits);
648 swf_SetBits(t,cx->b0,nbits);
649 if (alpha) swf_SetBits(t,cx->a0,nbits);
653 { swf_SetBits(t,cx->r1,nbits);
654 swf_SetBits(t,cx->g1,nbits);
655 swf_SetBits(t,cx->b1,nbits);
656 if (alpha) swf_SetBits(t,cx->a1,nbits);
662 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
663 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
665 void swf_SetPassword(TAG * t, const char * password)
667 /* WARNING: crypt_md5 is not reentrant */
671 #if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) && defined(HAVE_TIME_H) && defined(HAVE_TIME)
673 salt[0] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
674 salt[1] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
678 fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
679 fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
682 md5string = crypt_md5(password, salt);
685 swf_SetString(t, md5string);
688 int swf_VerifyPassword(TAG * t, const char * password)
690 char*md5string1, *md5string2;
695 if(t->len >= 5 && t->pos==0 &&
700 printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
703 md5string1 = swf_GetString(t);
705 if(strncmp(md5string1, "$1$",3 )) {
706 fprintf(stderr, "rfxswf: no salt in pw string\n");
709 x = strchr(md5string1+3, '$');
711 fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
714 n = x-(md5string1+3);
715 salt = (char*)malloc(n+1);
716 memcpy(salt, md5string1+3, n);
719 md5string2 = crypt_md5(password, salt);
721 if(strcmp(md5string1, md5string2) != 0)
726 // Tag List Manipulating Functions
728 TAG * swf_InsertTag(TAG * after,U16 id)
731 t = (TAG *)malloc(sizeof(TAG));
733 { memset(t,0x00,sizeof(TAG));
739 t->next = after->next;
741 if (t->next) t->next->prev = t;
747 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
750 t = (TAG *)malloc(sizeof(TAG));
752 { memset(t,0x00,sizeof(TAG));
758 t->prev = before->prev;
760 if (t->prev) t->prev->next = t;
763 if(swf && swf->firstTag == before) {
769 void swf_ClearTag(TAG * t)
771 if (t->data) free(t->data);
780 void swf_ResetTag(TAG*tag, U16 id)
782 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
786 int swf_DeleteTag(TAG * t)
789 if (t->prev) t->prev->next = t->next;
790 if (t->next) t->next->prev = t->prev;
792 if (t->data) free(t->data);
797 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
803 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
811 if (reader->read(reader, &len, 4) != 4) return NULL;
815 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
816 // Sprite handling fix: Flatten sprite tree
818 t = (TAG *)malloc(sizeof(TAG));
823 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (2). (%d bytes)\n", sizeof(TAG));
828 memset(t,0x00,sizeof(TAG));
834 { t->data = (U8*)malloc(t->len);
838 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (3). (%d bytes)\n", t->len);
843 if (reader->read(reader, t->data, t->len) != t->len) return NULL;
855 int swf_DefineSprite_GetRealSize(TAG * t);
857 int swf_WriteTag2(struct writer_t*writer, TAG * t)
858 // returns tag length in bytes (incl. Header), -1 = Error
859 // writer = 0 -> no output
866 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
868 short_tag = len<0x3f&&(t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2);
872 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
873 if (writer->write(writer,raw,2)!=2)
876 fprintf(stderr,"WriteTag() failed: Short Header.\n");
883 raw[0] = SWAP16((t->id<<6)|0x3f);
884 if (writer->write(writer,raw,2)!=2)
887 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
893 if (writer->write(writer,&len,4)!=4)
896 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
903 { if (writer->write(writer,t->data,t->len)!=t->len)
906 fprintf(stderr,"WriteTag() failed: Data.\n");
912 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
916 return t->len+(short_tag?2:6);
919 int swf_WriteTag(int handle, TAG * t)
921 struct writer_t writer;
924 return swf_WriteTag2(0, t);
925 writer_init_filewriter(&writer, handle);
926 len = swf_WriteTag2(&writer, t);
927 writer.finish(&writer);
931 int swf_DefineSprite_GetRealSize(TAG * t)
932 // Sprite Handling: Helper function to pack DefineSprite-Tag
934 if(len>4) { // folded sprite
938 { t = swf_NextTag(t);
939 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
941 } while (t&&(t->id!=ST_END));
945 void swf_UnFoldSprite(TAG * t)
950 U16 spriteid,spriteframes;
952 if(t->id!=ST_DEFINESPRITE)
954 if(t->len<=4) // not folded
959 spriteid = swf_GetU16(t); //id
960 spriteframes = swf_GetU16(t); //frames
972 if(id == ST_DEFINESPRITE && len<=4)
977 it = swf_InsertTag(next, id);
982 { it->data = (U8*)malloc(it->len);
983 it->memsize = it->len;
984 swf_GetBlock(t, it->data, it->len);
991 free(t->data); t->data = 0;
992 t->memsize = t->len = t->pos = 0;
994 swf_SetU16(t, spriteid);
995 swf_SetU16(t, spriteframes);
998 void swf_FoldSprite(TAG * t)
1001 U16 id,frames,tmpid;
1003 if(t->id!=ST_DEFINESPRITE)
1006 fprintf(stderr, "Error: Sprite has no ID!");
1010 /* sprite is already folded */
1017 t->len = t->pos = t->memsize = 0;
1022 t = swf_NextTag(sprtag);
1027 if(t->id==ST_SHOWFRAME) frames++;
1028 if(t->id == ST_DEFINESPRITE && t->len<=4)
1033 } while(t && level);
1035 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1037 swf_SetU16(sprtag, id);
1038 swf_SetU16(sprtag, frames);
1040 t = swf_NextTag(sprtag);
1045 if(t->len<0x3f&&t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2) {
1046 swf_SetU16(sprtag,t->len|(t->id<<6));
1048 swf_SetU16(sprtag,0x3f|(t->id<<6));
1049 swf_SetU32(sprtag,t->len);
1052 swf_SetBlock(sprtag,t->data, t->len);
1054 if(t->id == ST_DEFINESPRITE && t->len<=4)
1063 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1065 // sprtag->next = t;
1066 // t->prev = sprtag;
1069 int swf_IsFolded(TAG * t)
1071 return (t->id == ST_DEFINESPRITE && t->len>4);
1074 void swf_FoldAll(SWF*swf)
1076 TAG*tag = swf->firstTag;
1077 //swf_DumpSWF(stdout, swf);
1079 if(tag->id == ST_DEFINESPRITE) {
1080 swf_FoldSprite(tag);
1081 //swf_DumpSWF(stdout, swf);
1083 tag = swf_NextTag(tag);
1087 void swf_UnFoldAll(SWF*swf)
1089 TAG*tag = swf->firstTag;
1091 if(tag->id == ST_DEFINESPRITE)
1092 swf_UnFoldSprite(tag);
1097 void swf_OptimizeTagOrder(SWF*swf)
1104 /* at the moment, we don't actually do optimizing,
1105 only fixing of non-spec-conformant things like
1112 tag = swf->firstTag;
1115 if(tag->id == ST_DEFINESPRITE) {
1117 /* ??? all sprites are supposed to be unfolded */
1118 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1128 /* move non-sprite tags out of sprite */
1129 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1130 /* remove tag from current position */
1131 tag->prev->next = tag->next;
1133 tag->next->prev = tag->prev;
1135 /* insert before tag level0 */
1137 tag->prev = level0->prev;
1139 tag->prev->next = tag;
1143 if(tag->id == ST_END) {
1154 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1156 if (!swf) return -1;
1157 memset(swf,0x00,sizeof(SWF));
1159 { char b[32]; // read Header
1163 struct reader_t zreader;
1165 if ((len = reader->read(reader ,b,8))<8) return -1;
1167 if (b[0]!='F' && b[0]!='C') return -1;
1168 if (b[1]!='W') return -1;
1169 if (b[2]!='S') return -1;
1170 swf->fileVersion = b[3];
1171 swf->compressed = (b[0]=='C')?1:0;
1172 swf->fileSize = GET32(&b[4]);
1174 if(swf->compressed) {
1175 reader_init_zlibinflate(&zreader, reader);
1179 reader_GetRect(reader, &swf->movieSize);
1180 reader->read(reader, &swf->frameRate, 2);
1181 swf->frameRate = SWAP16(swf->frameRate);
1182 reader->read(reader, &swf->frameCount, 2);
1183 swf->frameCount = SWAP16(swf->frameCount);
1185 /* read tags and connect to list */
1187 while (t) t = swf_ReadTag(reader,t);
1188 swf->firstTag = t1.next;
1189 t1.next->prev = NULL;
1195 int swf_ReadSWF(int handle, SWF * swf)
1197 struct reader_t reader;
1198 reader_init_filereader(&reader, handle);
1199 return swf_ReadSWF2(&reader, swf);
1202 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1206 struct writer_t zwriter;
1209 int writer_lastpos = 0;
1212 if (!swf) return -1;
1213 if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1215 if(writer) writer_lastpos = writer->pos;
1217 // Insert REFLEX Tag
1219 #ifdef INSERT_RFX_TAG
1221 if (swf->firstTag && swf_NextTag(swf->firstTag))
1222 if (swf_GetTagID(swf_NextTag(swf->firstTag))!=ST_REFLEX)
1223 swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),"rfx",3);
1225 #endif // INSERT_RFX_TAG
1227 // Count Frames + File Size
1234 len += swf_WriteTag(-1,t);
1235 if(t->id == ST_DEFINESPRITE) inSprite++;
1236 else if(t->id == ST_END && inSprite) inSprite--;
1237 else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1245 memset(&t1,0x00,sizeof(TAG));
1249 { // measure header file size
1252 memset(&t2,0x00,sizeof(TAG));
1255 swf_SetRect(&t2, &swf->movieSize);
1256 swf_SetU16(&t2, swf->frameRate);
1257 swf_SetU16(&t2, swf->frameCount);
1258 l = swf_GetTagLen(&t2)+8;
1260 if(swf->compressed == 8) {
1265 if(len) {// don't touch headers without tags
1266 swf->fileSize = fileSize;
1267 swf->frameCount = frameCount;
1270 if(swf->compressed != 8) {
1271 /* compressed flag set to 8 means "skip first 8
1272 header bytes". This is necessary if the caller wants to
1273 create compressed SWFs himself .
1274 It also means that we don't initialize our own zlib
1275 writer, but assume the caller provided one.
1277 if(swf->compressed) {
1279 writer->write(writer, id, 3);
1283 writer->write(writer, id, 3);
1286 writer->write(writer, &swf->fileVersion, 1);
1287 PUT32(b4, swf->fileSize);
1288 writer->write(writer, b4, 4);
1290 if(swf->compressed) {
1291 writer_init_zlibdeflate(&zwriter, writer);
1296 swf_SetRect(&t1,&swf->movieSize);
1297 swf_SetU16(&t1,swf->frameRate);
1298 swf_SetU16(&t1,swf->frameCount);
1300 ret = writer->write(writer,b,swf_GetTagLen(&t1));
1301 if (ret!=swf_GetTagLen(&t1))
1304 fprintf(stderr, "ret:%d\n",ret);
1306 fprintf(stderr,"WriteSWF() failed: Header.\n");
1313 { if (swf_WriteTag2(writer, t)<0) return -1;
1316 if(swf->compressed) {
1317 if(swf->compressed != 8) {
1318 zwriter.finish(&zwriter);
1319 return writer->pos - writer_lastpos;
1321 return (int)fileSize;
1323 return (int)fileSize;
1328 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1330 struct writer_t writer;
1332 swf->compressed = 0;
1335 writer_init_nullwriter(&writer);
1336 len = swf_WriteSWF2(&writer, swf);
1338 writer_init_filewriter(&writer, handle);
1339 len = swf_WriteSWF2(&writer, swf);
1340 writer.finish(&writer);
1344 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1346 struct writer_t writer;
1348 swf->compressed = 1;
1351 writer_init_nullwriter(&writer);
1352 len = swf_WriteSWF2(&writer, swf);
1354 writer_init_filewriter(&writer, handle);
1355 len = swf_WriteSWF2(&writer, swf);
1356 writer.finish(&writer);
1360 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1363 memcpy(&myswf,swf,sizeof(SWF));
1365 return swf_WriteSWF2(writer, &myswf);
1368 int swf_WriteHeader(int handle,SWF * swf)
1371 memcpy(&myswf,swf,sizeof(SWF));
1373 return swf_WriteSWF(handle, &myswf);
1376 int swf_WriteCGI(SWF * swf)
1380 len = swf_WriteSWF(-1,swf);
1382 if (len<0) return -1;
1384 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1385 "Accept-Ranges: bytes\n"
1386 "Content-Length: %lu\n"
1387 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1390 write(fileno(stdout),s,strlen(s));
1391 return swf_WriteSWF(fileno(stdout),swf);
1394 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1395 { TAG * t = swf->firstTag;
1398 { TAG * tnew = t->next;
1399 if (t->data) free(t->data);
1406 // include advanced functions
1408 #include "modules/swfdump.c"
1409 #include "modules/swfshape.c"
1410 #include "modules/swftext.c"
1411 #include "modules/swffont.c"
1412 #include "modules/swfobject.c"
1413 #include "modules/swfbutton.c"
1414 #include "modules/swftools.c"
1415 #include "modules/swfcgi.c"
1416 #include "modules/swfbits.c"
1417 #include "modules/swfaction.c"
1418 #include "modules/swfsound.c"
1419 #include "modules/swfdraw.c"