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 void* rfx_alloc(int size)
58 //fprintf(stderr, "Warning: Zero alloc\n");
64 fprintf(stderr, "FATAL: Out of memory (while trying to claim %d bytes)\n", size);
65 /* TODO: we should send a signal, so that the debugger kicks in? */
70 void* rfx_realloc(void*data, int size)
75 //fprintf(stderr, "Warning: Zero realloc\n");
82 ptr = realloc(data, size);
86 fprintf(stderr, "FATAL: Out of memory (while trying to claim %d bytes)\n", size);
87 /* TODO: we should send a signal, so that the debugger kicks in? */
92 void* rfx_calloc(int size)
97 //fprintf(stderr, "Warning: Zero alloc\n");
106 fprintf(stderr, "FATAL: Out of memory (while trying to claim %d bytes)\n", size);
107 /* TODO: we should send a signal, so that the debugger kicks in? */
111 memset(ptr, 0, size);
116 void rfx_free(void*ptr)
124 long rfx_memory_used()
128 char* rfx_memory_used_str()
133 // internal constants
135 #define MALLOC_SIZE 128
136 #define INSERT_RFX_TAG
138 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
140 // inline wrapper functions
142 TAG * swf_NextTag(TAG * t) { return t->next; }
143 TAG * swf_PrevTag(TAG * t) { return t->prev; }
144 U16 swf_GetTagID(TAG * t) { return t->id; }
145 U32 swf_GetTagLen(TAG * t) { return t->len; }
146 U8* swf_GetTagLenPtr(TAG * t) { return &(t->data[t->len]); }
147 U32 swf_GetTagPos(TAG * t) { return t->pos; }
149 // for future purpose: avoid high level lib functions to change tagpos/bitpos
151 #define swf_SaveTagPos(tag)
152 #define swf_RestoreTagPos(tag)
154 void swf_SetTagPos(TAG * t,U32 pos)
155 { swf_ResetReadBits(t);
156 if (pos<=t->len) t->pos = pos;
159 fprintf(stderr,"SetTagPos(%d) out of bounds: TagID = %i\n",pos, t->id);
164 char* swf_GetString(TAG*t)
166 char* str = ((char*)(&(t)->data[(t)->pos]));
171 U8 swf_GetU8(TAG * t)
172 { swf_ResetReadBits(t);
175 { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
179 return t->data[t->pos++];
182 U16 swf_GetU16(TAG * t)
184 swf_ResetReadBits(t);
186 if (t->pos>(t->len-2))
187 { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
191 res = t->data[t->pos] | (t->data[t->pos+1]<<8);
196 U32 swf_GetU32(TAG * t)
198 swf_ResetReadBits(t);
200 if (t->pos>(t->len-4))
201 { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
205 res = t->data[t->pos] | (t->data[t->pos+1]<<8) |
206 (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
211 int swf_GetBlock(TAG * t,U8 * b,int l)
212 // returns number of bytes written (<=l)
213 // b = NULL -> skip data
214 { swf_ResetReadBits(t);
215 if ((t->len-t->pos)<l) l=t->len-t->pos;
216 if (b && l) memcpy(b,&t->data[t->pos],l);
221 int swf_SetBlock(TAG * t,U8 * b,int l)
222 // Appends Block to the end of Tagdata, returns size
223 { U32 newlen = t->len + l;
224 swf_ResetWriteBits(t);
225 if (newlen>t->memsize)
226 { U32 newmem = MEMSIZE(newlen);
227 U8 * newdata = (U8*)(rfx_realloc(t->data,newmem));
231 if (b) memcpy(&t->data[t->len],b,l);
232 else memset(&t->data[t->len],0x00,l);
237 int swf_SetU8(TAG * t,U8 v)
238 { swf_ResetWriteBits(t);
239 if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
240 t->data[t->len++] = v;
244 int swf_SetU16(TAG * t,U16 v)
249 swf_ResetWriteBits(t);
250 if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
251 t->data[t->len++] = a[0];
252 t->data[t->len++] = a[1];
255 void swf_SetS16(TAG * t,int v)
257 if(v>32767 || v<-32768) {
258 fprintf(stderr, "Warning: S16 overflow: %d\n", v);
260 swf_SetU16(t, (S16)v);
263 int swf_SetU32(TAG * t,U32 v)
265 a[0] = v&0xff; // to ensure correct handling of non-intel byteorder
270 swf_ResetWriteBits(t);
271 if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
272 t->data[t->len++] = a[0];
273 t->data[t->len++] = a[1];
274 t->data[t->len++] = a[2];
275 t->data[t->len++] = a[3];
279 U32 swf_GetBits(TAG * t,int nbits)
281 if (!nbits) return 0;
282 if (!t->readBit) t->readBit = 0x80;
285 if (t->data[t->pos]&t->readBit) res|=1;
289 { if (nbits) t->readBit = 0x80;
292 { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
302 S32 swf_GetSBits(TAG * t,int nbits)
303 { U32 res = swf_GetBits(t,nbits);
304 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
308 U32 reader_GetBits(struct reader_t*reader, int nbits)
309 { return reader_readbits(reader, nbits);
311 S32 reader_GetSBits(struct reader_t*reader, int nbits)
312 { U32 res = reader_readbits(reader, nbits);
313 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
317 int swf_SetBits(TAG * t,U32 v,int nbits)
318 { U32 bm = 1<<(nbits-1);
322 { if (FAILED(swf_SetU8(t,0))) return -1;
325 if (v&bm) t->data[t->len-1] |= t->writeBit;
333 // Advanced Data Access Functions
335 int swf_SetRGB(TAG * t,RGBA * col)
338 { swf_SetU8(t,col->r);
341 } else swf_SetBlock(t,NULL,3);
344 void swf_GetRGB(TAG * t, RGBA * col)
349 col->r = swf_GetU8(t);
350 col->g = swf_GetU8(t);
351 col->b = swf_GetU8(t);
355 int swf_SetRGBA(TAG * t,RGBA * col)
358 { swf_SetU8(t,col->r);
362 } else swf_SetBlock(t,NULL,4);
365 void swf_GetRGBA(TAG * t, RGBA * col)
370 col->r = swf_GetU8(t);
371 col->g = swf_GetU8(t);
372 col->b = swf_GetU8(t);
373 col->a = swf_GetU8(t);
376 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
381 memset(gradient, 0, sizeof(GRADIENT));
386 gradient->num = swf_GetU8(tag);
387 for(t=0;t<gradient->num;t++)
392 gradient->ratios[t] = swf_GetU8(tag);
394 swf_GetRGB(tag, &gradient->rgba[t]);
396 swf_GetRGBA(tag, &gradient->rgba[t]);
400 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
404 memset(gradient, 0, sizeof(GRADIENT));
407 swf_SetU8(tag, gradient->num);
408 for(t=0; t<8 && t<gradient->num; t++)
410 swf_SetU8(tag, gradient->ratios[t]);
412 swf_SetRGB(tag, &gradient->rgba[t]);
414 swf_SetRGBA(tag, &gradient->rgba[t]);
418 int swf_CountUBits(U32 v,int nbits)
421 if(v == 0x00000000) n = 0;
427 return (n>nbits)?n:nbits;
430 int swf_CountBits(U32 v,int nbits)
434 { if(v == 0xffffffff) n = 1;
442 { if(v == 0x00000000) n = 0;
449 return (n>nbits)?n:nbits;
452 int swf_GetRect(TAG * t,SRECT * r)
455 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
457 nbits = (int) swf_GetBits(t,5);
458 r->xmin = swf_GetSBits(t,nbits);
459 r->xmax = swf_GetSBits(t,nbits);
460 r->ymin = swf_GetSBits(t,nbits);
461 r->ymax = swf_GetSBits(t,nbits);
465 int reader_GetRect(struct reader_t*reader,SRECT * r)
469 nbits = (int) reader_GetBits(reader,5);
470 r->xmin = reader_GetSBits(reader,nbits);
471 r->xmax = reader_GetSBits(reader,nbits);
472 r->ymin = reader_GetSBits(reader,nbits);
473 r->ymax = reader_GetSBits(reader,nbits);
477 int swf_SetRect(TAG * t,SRECT * r)
480 nbits = swf_CountBits(r->xmin,0);
481 nbits = swf_CountBits(r->xmax,nbits);
482 nbits = swf_CountBits(r->ymin,nbits);
483 nbits = swf_CountBits(r->ymax,nbits);
485 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
489 swf_SetBits(t,nbits,5);
490 swf_SetBits(t,r->xmin,nbits);
491 swf_SetBits(t,r->xmax,nbits);
492 swf_SetBits(t,r->ymin,nbits);
493 swf_SetBits(t,r->ymax,nbits);
498 void swf_ExpandRect(SRECT*src, SPOINT add)
500 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
505 if((add.x|add.y) == 0) src->xmax++; //make sure the bbox is not NULL anymore
508 if(add.x < src->xmin)
510 if(add.x > src->xmax)
512 if(add.y < src->ymin)
514 if(add.y > src->ymax)
517 void swf_ExpandRect2(SRECT*src, SRECT*add)
519 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
521 if((src->xmin | src->ymin | src->xmax | src->ymax)==0)
523 if(add->xmin < src->xmin)
524 src->xmin = add->xmin;
525 if(add->ymin < src->ymin)
526 src->ymin = add->ymin;
527 if(add->xmax > src->xmax)
528 src->xmax = add->xmax;
529 if(add->ymax > src->ymax)
530 src->ymax = add->ymax;
532 void swf_ExpandRect3(SRECT*src, SPOINT center, int radius)
534 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
535 src->xmin = center.x-radius;
536 src->ymin = center.y-radius;
537 src->xmax = center.x+radius;
538 src->ymax = center.y+radius;
539 if((center.x|center.y|radius) == 0) src->xmax++; //make sure the bbox is not NULL anymore
542 if(center.x - radius < src->xmin)
543 src->xmin = center.x - radius;
544 if(center.x + radius > src->xmax)
545 src->xmax = center.x + radius;
546 if(center.y - radius < src->ymin)
547 src->ymin = center.y - radius;
548 if(center.y + radius > src->ymax)
549 src->ymax = center.y + radius;
551 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
554 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
555 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
558 SRECT swf_TurnRect(SRECT r, MATRIX* m)
561 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
564 p1.x = r.xmin;p1.y = r.ymin;
565 p2.x = r.xmax;p2.y = r.ymin;
566 p3.x = r.xmin;p3.y = r.ymax;
567 p4.x = r.xmax;p4.y = r.ymax;
568 pp1 = swf_TurnPoint(p1, m);
569 pp2 = swf_TurnPoint(p2, m);
570 pp3 = swf_TurnPoint(p3, m);
571 pp4 = swf_TurnPoint(p4, m);
572 g.xmin = g.xmax = pp1.x;
573 g.ymin = g.ymax = pp1.y;
574 swf_ExpandRect(&g, pp2);
575 swf_ExpandRect(&g, pp3);
576 swf_ExpandRect(&g, pp4);
581 int swf_GetMatrix(TAG * t,MATRIX * m)
588 { m->sx = m->sy = 0x10000;
594 swf_ResetReadBits(t);
596 if (swf_GetBits(t,1))
597 { nbits = swf_GetBits(t,5);
598 m->sx = swf_GetSBits(t,nbits);
599 m->sy = swf_GetSBits(t,nbits);
601 else m->sx = m->sy = 0x10000;
603 if (swf_GetBits(t,1))
604 { nbits = swf_GetBits(t,5);
605 m->r0 = swf_GetSBits(t,nbits);
606 m->r1 = swf_GetSBits(t,nbits);
608 else m->r0 = m->r1 = 0x0;
610 nbits = swf_GetBits(t,5);
611 m->tx = swf_GetSBits(t,nbits);
612 m->ty = swf_GetSBits(t,nbits);
617 int swf_SetMatrix(TAG * t,MATRIX * m)
623 ma.sx = ma.sy = 0x10000;
628 swf_ResetWriteBits(t);
630 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
632 { swf_SetBits(t,1,1);
633 nbits = swf_CountBits(m->sx,0);
634 nbits = swf_CountBits(m->sy,nbits);
636 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
639 swf_SetBits(t,nbits,5);
640 swf_SetBits(t,m->sx,nbits);
641 swf_SetBits(t,m->sy,nbits);
644 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
646 { swf_SetBits(t,1,1);
647 nbits = swf_CountBits(m->r0,0);
648 nbits = swf_CountBits(m->r1,nbits);
650 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
653 swf_SetBits(t,nbits,5);
654 swf_SetBits(t,m->r0,nbits);
655 swf_SetBits(t,m->r1,nbits);
658 nbits = swf_CountBits(m->tx,0);
659 nbits = swf_CountBits(m->ty,nbits);
661 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
664 swf_SetBits(t,nbits,5);
665 swf_SetBits(t,m->tx,nbits);
666 swf_SetBits(t,m->ty,nbits);
671 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha)
679 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
680 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
684 swf_ResetReadBits(t);
685 hasadd = swf_GetBits(t,1);
686 hasmul = swf_GetBits(t,1);
687 nbits = swf_GetBits(t,4);
690 { cx->r0 = (S16)swf_GetSBits(t,nbits);
691 cx->g0 = (S16)swf_GetSBits(t,nbits);
692 cx->b0 = (S16)swf_GetSBits(t,nbits);
694 cx->a0 = (S16)swf_GetSBits(t,nbits);
698 { cx->r1 = (S16)swf_GetSBits(t,nbits);
699 cx->g1 = (S16)swf_GetSBits(t,nbits);
700 cx->b1 = (S16)swf_GetSBits(t,nbits);
702 cx->a1 = (S16)swf_GetSBits(t,nbits);
708 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
716 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
717 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
727 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
728 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
731 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
732 nbits = swf_CountBits((S32)cx->r0,nbits);
733 nbits = swf_CountBits((S32)cx->g0,nbits);
734 nbits = swf_CountBits((S32)cx->b0,nbits);
738 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
739 nbits = swf_CountBits((S32)cx->r1,nbits);
740 nbits = swf_CountBits((S32)cx->g1,nbits);
741 nbits = swf_CountBits((S32)cx->b1,nbits);
744 swf_ResetWriteBits(t);
745 swf_SetBits(t,hasadd?1:0,1);
746 swf_SetBits(t,hasmul?1:0,1);
747 swf_SetBits(t,nbits,4);
750 { swf_SetBits(t,cx->r0,nbits);
751 swf_SetBits(t,cx->g0,nbits);
752 swf_SetBits(t,cx->b0,nbits);
753 if (alpha) swf_SetBits(t,cx->a0,nbits);
757 { swf_SetBits(t,cx->r1,nbits);
758 swf_SetBits(t,cx->g1,nbits);
759 swf_SetBits(t,cx->b1,nbits);
760 if (alpha) swf_SetBits(t,cx->a1,nbits);
766 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
767 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
769 void swf_SetPassword(TAG * t, const char * password)
771 /* WARNING: crypt_md5 is not reentrant */
775 #if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) && defined(HAVE_TIME_H) && defined(HAVE_TIME)
777 salt[0] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
778 salt[1] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
782 fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
783 fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
787 md5string = crypt_md5(password, salt);
790 swf_SetString(t, md5string);
793 int swf_VerifyPassword(TAG * t, const char * password)
795 char*md5string1, *md5string2;
800 if(t->len >= 5 && t->pos==0 &&
805 printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
808 md5string1 = swf_GetString(t);
810 if(strncmp(md5string1, "$1$",3 )) {
811 fprintf(stderr, "rfxswf: no salt in pw string\n");
814 x = strchr(md5string1+3, '$');
816 fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
819 n = x-(md5string1+3);
820 salt = (char*)rfx_alloc(n+1);
821 memcpy(salt, md5string1+3, n);
824 md5string2 = crypt_md5(password, salt);
826 if(strcmp(md5string1, md5string2) != 0)
831 // Tag List Manipulating Functions
833 TAG * swf_InsertTag(TAG * after,U16 id)
836 t = (TAG *)rfx_calloc(sizeof(TAG));
842 t->next = after->next;
844 if (t->next) t->next->prev = t;
849 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
852 t = (TAG *)rfx_calloc(sizeof(TAG));
858 t->prev = before->prev;
860 if (t->prev) t->prev->next = t;
862 if(swf && swf->firstTag == before) {
868 void swf_ClearTag(TAG * t)
870 if (t->data) rfx_free(t->data);
879 void swf_ResetTag(TAG*tag, U16 id)
881 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
885 TAG* swf_CopyTag(TAG*tag, TAG*to_copy)
887 tag = swf_InsertTag(tag, to_copy->id);
888 swf_SetBlock(tag, to_copy->data, to_copy->len);
892 int swf_DeleteTag(TAG * t)
895 if (t->prev) t->prev->next = t->next;
896 if (t->next) t->next->prev = t->prev;
898 if (t->data) rfx_free(t->data);
903 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
909 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
917 if (reader->read(reader, &len, 4) != 4) return NULL;
921 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
922 // Sprite handling fix: Flatten sprite tree
924 t = (TAG *)rfx_calloc(sizeof(TAG));
930 { t->data = (U8*)rfx_alloc(t->len);
932 if (reader->read(reader, t->data, t->len) != t->len) {
933 fprintf(stderr, "rfxswf: Warning: Short read (tagid %d). File truncated?\n", t->id);
934 free(t->data);t->data=0;
949 int swf_DefineSprite_GetRealSize(TAG * t);
951 int swf_WriteTag2(struct writer_t*writer, TAG * t)
952 // returns tag length in bytes (incl. Header), -1 = Error
953 // writer = 0 -> no output
960 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
962 short_tag = len<0x3f&&(t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2);
966 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
967 if (writer->write(writer,raw,2)!=2)
970 fprintf(stderr,"WriteTag() failed: Short Header.\n");
977 raw[0] = SWAP16((t->id<<6)|0x3f);
978 if (writer->write(writer,raw,2)!=2)
981 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
987 if (writer->write(writer,&len,4)!=4)
990 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
997 { if (writer->write(writer,t->data,t->len)!=t->len)
1000 fprintf(stderr,"WriteTag() failed: Data.\n");
1006 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
1010 return t->len+(short_tag?2:6);
1013 int swf_WriteTag(int handle, TAG * t)
1015 struct writer_t writer;
1018 return swf_WriteTag2(0, t);
1019 writer_init_filewriter(&writer, handle);
1020 len = swf_WriteTag2(&writer, t);
1021 writer.finish(&writer);
1025 int swf_DefineSprite_GetRealSize(TAG * t)
1026 // Sprite Handling: Helper function to pack DefineSprite-Tag
1028 if(len>4) { // folded sprite
1032 { t = swf_NextTag(t);
1033 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
1035 } while (t&&(t->id!=ST_END));
1039 void swf_UnFoldSprite(TAG * t)
1044 U16 spriteid,spriteframes;
1046 if(t->id!=ST_DEFINESPRITE)
1048 if(t->len<=4) // not folded
1053 spriteid = swf_GetU16(t); //id
1054 spriteframes = swf_GetU16(t); //frames
1061 tmp = swf_GetU16(t);
1066 if(id == ST_DEFINESPRITE && len<=4)
1070 len = swf_GetU32(t);
1071 it = swf_InsertTag(next, id);
1076 { it->data = (U8*)rfx_alloc(it->len);
1077 it->memsize = it->len;
1078 swf_GetBlock(t, it->data, it->len);
1085 rfx_free(t->data); t->data = 0;
1086 t->memsize = t->len = t->pos = 0;
1088 swf_SetU16(t, spriteid);
1089 swf_SetU16(t, spriteframes);
1092 void swf_FoldSprite(TAG * t)
1095 U16 id,frames,tmpid;
1097 if(t->id!=ST_DEFINESPRITE)
1100 fprintf(stderr, "Error: Sprite has no ID!");
1104 /* sprite is already folded */
1111 t->len = t->pos = t->memsize = 0;
1116 t = swf_NextTag(sprtag);
1121 if(t->id==ST_SHOWFRAME) frames++;
1122 if(t->id == ST_DEFINESPRITE && t->len<=4)
1127 } while(t && level);
1129 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1131 swf_SetU16(sprtag, id);
1132 swf_SetU16(sprtag, frames);
1134 t = swf_NextTag(sprtag);
1139 if(t->len<0x3f&&t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2) {
1140 swf_SetU16(sprtag,t->len|(t->id<<6));
1142 swf_SetU16(sprtag,0x3f|(t->id<<6));
1143 swf_SetU32(sprtag,t->len);
1146 swf_SetBlock(sprtag,t->data, t->len);
1148 if(t->id == ST_DEFINESPRITE && t->len<=4)
1157 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1159 // sprtag->next = t;
1160 // t->prev = sprtag;
1163 int swf_IsFolded(TAG * t)
1165 return (t->id == ST_DEFINESPRITE && t->len>4);
1168 void swf_FoldAll(SWF*swf)
1170 TAG*tag = swf->firstTag;
1171 //swf_DumpSWF(stdout, swf);
1173 if(tag->id == ST_DEFINESPRITE) {
1174 swf_FoldSprite(tag);
1175 //swf_DumpSWF(stdout, swf);
1177 tag = swf_NextTag(tag);
1181 void swf_UnFoldAll(SWF*swf)
1183 TAG*tag = swf->firstTag;
1185 if(tag->id == ST_DEFINESPRITE)
1186 swf_UnFoldSprite(tag);
1191 void swf_OptimizeTagOrder(SWF*swf)
1198 /* at the moment, we don't actually do optimizing,
1199 only fixing of non-spec-conformant things like
1206 tag = swf->firstTag;
1209 if(tag->id == ST_DEFINESPRITE) {
1211 /* ??? all sprites are supposed to be unfolded */
1212 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1222 /* move non-sprite tags out of sprite */
1223 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1224 /* remove tag from current position */
1225 tag->prev->next = tag->next;
1227 tag->next->prev = tag->prev;
1229 /* insert before tag level0 */
1231 tag->prev = level0->prev;
1234 tag->prev->next = tag;
1236 swf->firstTag = tag;
1240 if(tag->id == ST_END) {
1251 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1253 if (!swf) return -1;
1254 memset(swf,0x00,sizeof(SWF));
1256 { char b[32]; // read Header
1260 struct reader_t zreader;
1262 if ((len = reader->read(reader ,b,8))<8) return -1;
1264 if (b[0]!='F' && b[0]!='C') return -1;
1265 if (b[1]!='W') return -1;
1266 if (b[2]!='S') return -1;
1267 swf->fileVersion = b[3];
1268 swf->compressed = (b[0]=='C')?1:0;
1269 swf->fileSize = GET32(&b[4]);
1271 if(swf->compressed) {
1272 reader_init_zlibinflate(&zreader, reader);
1276 reader_GetRect(reader, &swf->movieSize);
1277 reader->read(reader, &swf->frameRate, 2);
1278 swf->frameRate = SWAP16(swf->frameRate);
1279 reader->read(reader, &swf->frameCount, 2);
1280 swf->frameCount = SWAP16(swf->frameCount);
1282 /* read tags and connect to list */
1284 while (t) t = swf_ReadTag(reader,t);
1285 swf->firstTag = t1.next;
1286 t1.next->prev = NULL;
1292 int swf_ReadSWF(int handle, SWF * swf)
1294 struct reader_t reader;
1295 reader_init_filereader(&reader, handle);
1296 return swf_ReadSWF2(&reader, swf);
1299 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1303 struct writer_t zwriter;
1306 int writer_lastpos = 0;
1309 if (!swf) return -1;
1310 if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1312 if(writer) writer_lastpos = writer->pos;
1314 // Insert REFLEX Tag
1316 #ifdef INSERT_RFX_TAG
1318 if ((swf->firstTag && swf->firstTag->id != ST_REFLEX) &&
1319 (!swf->firstTag->next || swf->firstTag->next->id != ST_REFLEX))
1321 swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),"rfx",3);
1324 #endif // INSERT_RFX_TAG
1326 // Count Frames + File Size
1333 len += swf_WriteTag(-1,t);
1334 if(t->id == ST_DEFINESPRITE && !swf_IsFolded(t)) inSprite++;
1335 else if(t->id == ST_END && inSprite) inSprite--;
1336 else if(t->id == ST_END && !inSprite) {
1337 if(t->prev && t->prev->id!=ST_SHOWFRAME)
1340 else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1348 memset(&t1,0x00,sizeof(TAG));
1352 { // measure header file size
1355 memset(&t2,0x00,sizeof(TAG));
1358 swf_SetRect(&t2, &swf->movieSize);
1359 swf_SetU16(&t2, swf->frameRate);
1360 swf_SetU16(&t2, swf->frameCount);
1361 l = swf_GetTagLen(&t2)+8;
1363 if(swf->compressed == 8) {
1368 if(len) {// don't touch headers without tags
1369 swf->fileSize = fileSize;
1370 swf->frameCount = frameCount;
1373 if(swf->compressed != 8) {
1374 /* compressed flag set to 8 means "skip first 8
1375 header bytes". This is necessary if the caller wants to
1376 create compressed SWFs himself .
1377 It also means that we don't initialize our own zlib
1378 writer, but assume the caller provided one.
1380 if(swf->compressed) {
1382 writer->write(writer, id, 3);
1386 writer->write(writer, id, 3);
1389 writer->write(writer, &swf->fileVersion, 1);
1390 PUT32(b4, swf->fileSize);
1391 writer->write(writer, b4, 4);
1393 if(swf->compressed) {
1394 writer_init_zlibdeflate(&zwriter, writer);
1399 swf_SetRect(&t1,&swf->movieSize);
1400 swf_SetU16(&t1,swf->frameRate);
1401 swf_SetU16(&t1,swf->frameCount);
1403 ret = writer->write(writer,b,swf_GetTagLen(&t1));
1404 if (ret!=swf_GetTagLen(&t1))
1407 fprintf(stderr, "ret:%d\n",ret);
1409 fprintf(stderr,"WriteSWF() failed: Header.\n");
1416 { if (swf_WriteTag2(writer, t)<0) return -1;
1419 if(swf->compressed) {
1420 if(swf->compressed != 8) {
1421 zwriter.finish(&zwriter);
1422 return writer->pos - writer_lastpos;
1424 return (int)fileSize;
1426 return (int)fileSize;
1431 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1433 struct writer_t writer;
1435 swf->compressed = 0;
1438 writer_init_nullwriter(&writer);
1439 len = swf_WriteSWF2(&writer, swf);
1442 writer_init_filewriter(&writer, handle);
1443 len = swf_WriteSWF2(&writer, swf);
1444 writer.finish(&writer);
1448 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1450 struct writer_t writer;
1452 swf->compressed = 1;
1455 writer_init_nullwriter(&writer);
1456 len = swf_WriteSWF2(&writer, swf);
1458 writer_init_filewriter(&writer, handle);
1459 len = swf_WriteSWF2(&writer, swf);
1460 writer.finish(&writer);
1464 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1467 memcpy(&myswf,swf,sizeof(SWF));
1469 return swf_WriteSWF2(writer, &myswf);
1472 int swf_WriteHeader(int handle,SWF * swf)
1475 memcpy(&myswf,swf,sizeof(SWF));
1477 return swf_WriteSWF(handle, &myswf);
1480 int swf_WriteCGI(SWF * swf)
1484 len = swf_WriteSWF(-1,swf);
1486 if (len<0) return -1;
1488 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1489 "Accept-Ranges: bytes\n"
1490 "Content-Length: %lu\n"
1491 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1494 write(fileno(stdout),s,strlen(s));
1495 return swf_WriteSWF(fileno(stdout),swf);
1498 SWF* swf_CopySWF(SWF*swf)
1500 SWF*nswf = rfx_alloc(sizeof(SWF));
1502 memcpy(nswf, swf, sizeof(SWF));
1504 tag = swf->firstTag;
1507 ntag = swf_CopyTag(ntag, tag);
1509 nswf->firstTag = ntag;
1515 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1516 { TAG * t = swf->firstTag;
1519 { TAG * tnew = t->next;
1520 if (t->data) rfx_free(t->data);
1527 // include advanced functions
1529 #include "modules/swfdump.c"
1530 #include "modules/swfshape.c"
1531 #include "modules/swftext.c"
1532 #include "modules/swffont.c"
1533 #include "modules/swfobject.c"
1534 #include "modules/swfbutton.c"
1535 #include "modules/swftools.c"
1536 #include "modules/swfcgi.c"
1537 #include "modules/swfbits.c"
1538 #include "modules/swfaction.c"
1539 #include "modules/swfsound.c"
1540 #include "modules/swfdraw.c"
1541 #include "modules/swfrender.c"