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\n");
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\n");
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\n");
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) //FIXME: alpha should be type bool
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");
786 md5string = crypt_md5(password, salt);
789 swf_SetString(t, md5string);
792 int swf_VerifyPassword(TAG * t, const char * password)
794 char*md5string1, *md5string2;
799 if(t->len >= 5 && t->pos==0 &&
804 printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
807 md5string1 = swf_GetString(t);
809 if(strncmp(md5string1, "$1$",3 )) {
810 fprintf(stderr, "rfxswf: no salt in pw string\n");
813 x = strchr(md5string1+3, '$');
815 fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
818 n = x-(md5string1+3);
819 salt = (char*)rfx_alloc(n+1);
820 memcpy(salt, md5string1+3, n);
823 md5string2 = crypt_md5(password, salt);
825 if(strcmp(md5string1, md5string2) != 0)
830 // Tag List Manipulating Functions
832 TAG * swf_InsertTag(TAG * after,U16 id)
835 t = (TAG *)rfx_calloc(sizeof(TAG));
841 t->next = after->next;
843 if (t->next) t->next->prev = t;
848 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
851 t = (TAG *)rfx_calloc(sizeof(TAG));
857 t->prev = before->prev;
859 if (t->prev) t->prev->next = t;
861 if(swf && swf->firstTag == before) {
867 void swf_ClearTag(TAG * t)
869 if (t->data) rfx_free(t->data);
878 void swf_ResetTag(TAG*tag, U16 id)
880 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
884 TAG* swf_CopyTag(TAG*tag, TAG*to_copy)
886 tag = swf_InsertTag(tag, to_copy->id);
887 swf_SetBlock(tag, to_copy->data, to_copy->len);
891 int swf_DeleteTag(TAG * t)
894 if (t->prev) t->prev->next = t->next;
895 if (t->next) t->next->prev = t->prev;
897 if (t->data) rfx_free(t->data);
902 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
908 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
916 if (reader->read(reader, &len, 4) != 4) return NULL;
920 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
921 // Sprite handling fix: Flatten sprite tree
923 t = (TAG *)rfx_calloc(sizeof(TAG));
929 { t->data = (U8*)rfx_alloc(t->len);
931 if (reader->read(reader, t->data, t->len) != t->len) {
932 fprintf(stderr, "rfxswf: Warning: Short read (tagid %d). File truncated?\n", t->id);
933 free(t->data);t->data=0;
948 int swf_DefineSprite_GetRealSize(TAG * t);
950 int swf_WriteTag2(struct writer_t*writer, TAG * t)
951 // returns tag length in bytes (incl. Header), -1 = Error
952 // writer = 0 -> no output
959 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
961 short_tag = len<0x3f&&(t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2);
965 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
966 if (writer->write(writer,raw,2)!=2)
969 fprintf(stderr,"WriteTag() failed: Short Header.\n");
976 raw[0] = SWAP16((t->id<<6)|0x3f);
977 if (writer->write(writer,raw,2)!=2)
980 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
986 if (writer->write(writer,&len,4)!=4)
989 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
996 { if (writer->write(writer,t->data,t->len)!=t->len)
999 fprintf(stderr,"WriteTag() failed: Data.\n");
1005 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
1009 return t->len+(short_tag?2:6);
1012 int swf_WriteTag(int handle, TAG * t)
1014 struct writer_t writer;
1017 return swf_WriteTag2(0, t);
1018 writer_init_filewriter(&writer, handle);
1019 len = swf_WriteTag2(&writer, t);
1020 writer.finish(&writer);
1024 int swf_DefineSprite_GetRealSize(TAG * t)
1025 // Sprite Handling: Helper function to pack DefineSprite-Tag
1027 if(len>4) { // folded sprite
1031 { t = swf_NextTag(t);
1032 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
1034 } while (t&&(t->id!=ST_END));
1038 void swf_UnFoldSprite(TAG * t)
1043 U16 spriteid,spriteframes;
1045 if(t->id!=ST_DEFINESPRITE)
1047 if(t->len<=4) // not folded
1052 spriteid = swf_GetU16(t); //id
1053 spriteframes = swf_GetU16(t); //frames
1060 tmp = swf_GetU16(t);
1065 if(id == ST_DEFINESPRITE && len<=4)
1069 len = swf_GetU32(t);
1070 it = swf_InsertTag(next, id);
1075 { it->data = (U8*)rfx_alloc(it->len);
1076 it->memsize = it->len;
1077 swf_GetBlock(t, it->data, it->len);
1084 rfx_free(t->data); t->data = 0;
1085 t->memsize = t->len = t->pos = 0;
1087 swf_SetU16(t, spriteid);
1088 swf_SetU16(t, spriteframes);
1091 void swf_FoldSprite(TAG * t)
1094 U16 id,frames,tmpid;
1096 if(t->id!=ST_DEFINESPRITE)
1099 fprintf(stderr, "Error: Sprite has no ID!");
1103 /* sprite is already folded */
1110 t->len = t->pos = t->memsize = 0;
1115 t = swf_NextTag(sprtag);
1120 if(t->id==ST_SHOWFRAME) frames++;
1121 if(t->id == ST_DEFINESPRITE && t->len<=4)
1126 } while(t && level);
1128 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1130 swf_SetU16(sprtag, id);
1131 swf_SetU16(sprtag, frames);
1133 t = swf_NextTag(sprtag);
1138 if(t->len<0x3f&&t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2) {
1139 swf_SetU16(sprtag,t->len|(t->id<<6));
1141 swf_SetU16(sprtag,0x3f|(t->id<<6));
1142 swf_SetU32(sprtag,t->len);
1145 swf_SetBlock(sprtag,t->data, t->len);
1147 if(t->id == ST_DEFINESPRITE && t->len<=4)
1156 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1158 // sprtag->next = t;
1159 // t->prev = sprtag;
1162 int swf_IsFolded(TAG * t)
1164 return (t->id == ST_DEFINESPRITE && t->len>4);
1167 void swf_FoldAll(SWF*swf)
1169 TAG*tag = swf->firstTag;
1170 //swf_DumpSWF(stdout, swf);
1172 if(tag->id == ST_DEFINESPRITE) {
1173 swf_FoldSprite(tag);
1174 //swf_DumpSWF(stdout, swf);
1176 tag = swf_NextTag(tag);
1180 void swf_UnFoldAll(SWF*swf)
1182 TAG*tag = swf->firstTag;
1184 if(tag->id == ST_DEFINESPRITE)
1185 swf_UnFoldSprite(tag);
1190 void swf_OptimizeTagOrder(SWF*swf)
1197 /* at the moment, we don't actually do optimizing,
1198 only fixing of non-spec-conformant things like
1205 tag = swf->firstTag;
1208 if(tag->id == ST_DEFINESPRITE) {
1210 /* ??? all sprites are supposed to be unfolded */
1211 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1221 /* move non-sprite tags out of sprite */
1222 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1223 /* remove tag from current position */
1224 tag->prev->next = tag->next;
1226 tag->next->prev = tag->prev;
1228 /* insert before tag level0 */
1230 tag->prev = level0->prev;
1233 tag->prev->next = tag;
1235 swf->firstTag = tag;
1239 if(tag->id == ST_END) {
1250 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1252 if (!swf) return -1;
1253 memset(swf,0x00,sizeof(SWF));
1255 { char b[32]; // read Header
1259 struct reader_t zreader;
1261 if ((len = reader->read(reader ,b,8))<8) return -1;
1263 if (b[0]!='F' && b[0]!='C') return -1;
1264 if (b[1]!='W') return -1;
1265 if (b[2]!='S') return -1;
1266 swf->fileVersion = b[3];
1267 swf->compressed = (b[0]=='C')?1:0;
1268 swf->fileSize = GET32(&b[4]);
1270 if(swf->compressed) {
1271 reader_init_zlibinflate(&zreader, reader);
1275 reader_GetRect(reader, &swf->movieSize);
1276 reader->read(reader, &swf->frameRate, 2);
1277 swf->frameRate = SWAP16(swf->frameRate);
1278 reader->read(reader, &swf->frameCount, 2);
1279 swf->frameCount = SWAP16(swf->frameCount);
1281 /* read tags and connect to list */
1283 while (t) t = swf_ReadTag(reader,t);
1284 swf->firstTag = t1.next;
1285 t1.next->prev = NULL;
1291 int swf_ReadSWF(int handle, SWF * swf)
1293 struct reader_t reader;
1294 reader_init_filereader(&reader, handle);
1295 return swf_ReadSWF2(&reader, swf);
1298 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1302 struct writer_t zwriter;
1305 int writer_lastpos = 0;
1308 if (!swf) return -1;
1309 if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1311 if(writer) writer_lastpos = writer->pos;
1313 // Insert REFLEX Tag
1315 #ifdef INSERT_RFX_TAG
1317 if (swf->firstTag && swf->firstTag->next &&
1318 (swf->firstTag->id != ST_REFLEX || swf->firstTag->next->id != ST_REFLEX)
1320 swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),"rfx",3);
1323 #endif // INSERT_RFX_TAG
1325 // Count Frames + File Size
1332 len += swf_WriteTag(-1,t);
1333 if(t->id == ST_DEFINESPRITE && !swf_IsFolded(t)) inSprite++;
1334 else if(t->id == ST_END && inSprite) inSprite--;
1335 else if(t->id == ST_END && !inSprite) {
1336 if(t->prev && t->prev->id!=ST_SHOWFRAME)
1339 else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1347 memset(&t1,0x00,sizeof(TAG));
1351 { // measure header file size
1354 memset(&t2,0x00,sizeof(TAG));
1357 swf_SetRect(&t2, &swf->movieSize);
1358 swf_SetU16(&t2, swf->frameRate);
1359 swf_SetU16(&t2, swf->frameCount);
1360 l = swf_GetTagLen(&t2)+8;
1362 if(swf->compressed == 8) {
1367 if(len) {// don't touch headers without tags
1368 swf->fileSize = fileSize;
1369 swf->frameCount = frameCount;
1372 if(swf->compressed != 8) {
1373 /* compressed flag set to 8 means "skip first 8
1374 header bytes". This is necessary if the caller wants to
1375 create compressed SWFs himself .
1376 It also means that we don't initialize our own zlib
1377 writer, but assume the caller provided one.
1379 if(swf->compressed) {
1381 writer->write(writer, id, 3);
1385 writer->write(writer, id, 3);
1388 writer->write(writer, &swf->fileVersion, 1);
1389 PUT32(b4, swf->fileSize);
1390 writer->write(writer, b4, 4);
1392 if(swf->compressed) {
1393 writer_init_zlibdeflate(&zwriter, writer);
1398 swf_SetRect(&t1,&swf->movieSize);
1399 swf_SetU16(&t1,swf->frameRate);
1400 swf_SetU16(&t1,swf->frameCount);
1402 ret = writer->write(writer,b,swf_GetTagLen(&t1));
1403 if (ret!=swf_GetTagLen(&t1))
1406 fprintf(stderr, "ret:%d\n",ret);
1408 fprintf(stderr,"WriteSWF() failed: Header.\n");
1415 { if (swf_WriteTag2(writer, t)<0) return -1;
1418 if(swf->compressed) {
1419 if(swf->compressed != 8) {
1420 zwriter.finish(&zwriter);
1421 return writer->pos - writer_lastpos;
1423 return (int)fileSize;
1425 return (int)fileSize;
1430 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1432 struct writer_t writer;
1434 swf->compressed = 0;
1437 writer_init_nullwriter(&writer);
1438 len = swf_WriteSWF2(&writer, swf);
1440 writer_init_filewriter(&writer, handle);
1441 len = swf_WriteSWF2(&writer, swf);
1442 writer.finish(&writer);
1446 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1448 struct writer_t writer;
1450 swf->compressed = 1;
1453 writer_init_nullwriter(&writer);
1454 len = swf_WriteSWF2(&writer, swf);
1456 writer_init_filewriter(&writer, handle);
1457 len = swf_WriteSWF2(&writer, swf);
1458 writer.finish(&writer);
1462 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1465 memcpy(&myswf,swf,sizeof(SWF));
1467 return swf_WriteSWF2(writer, &myswf);
1470 int swf_WriteHeader(int handle,SWF * swf)
1473 memcpy(&myswf,swf,sizeof(SWF));
1475 return swf_WriteSWF(handle, &myswf);
1478 int swf_WriteCGI(SWF * swf)
1482 len = swf_WriteSWF(-1,swf);
1484 if (len<0) return -1;
1486 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1487 "Accept-Ranges: bytes\n"
1488 "Content-Length: %lu\n"
1489 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1492 write(fileno(stdout),s,strlen(s));
1493 return swf_WriteSWF(fileno(stdout),swf);
1496 SWF* swf_CopySWF(SWF*swf)
1498 SWF*nswf = rfx_alloc(sizeof(SWF));
1499 memcpy(nswf, swf, sizeof(SWF));
1501 TAG*tag = swf->firstTag;
1504 ntag = swf_CopyTag(ntag, tag);
1506 nswf->firstTag = ntag;
1512 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1513 { TAG * t = swf->firstTag;
1516 { TAG * tnew = t->next;
1517 if (t->data) rfx_free(t->data);
1524 // include advanced functions
1526 #include "modules/swfdump.c"
1527 #include "modules/swfshape.c"
1528 #include "modules/swftext.c"
1529 #include "modules/swffont.c"
1530 #include "modules/swfobject.c"
1531 #include "modules/swfbutton.c"
1532 #include "modules/swftools.c"
1533 #include "modules/swfcgi.c"
1534 #include "modules/swfbits.c"
1535 #include "modules/swfaction.c"
1536 #include "modules/swfsound.c"
1537 #include "modules/swfdraw.c"
1538 #include "modules/swfrender.c"