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)
123 // internal constants
125 #define MALLOC_SIZE 128
126 #define INSERT_RFX_TAG
128 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
130 // inline wrapper functions
132 TAG * swf_NextTag(TAG * t) { return t->next; }
133 TAG * swf_PrevTag(TAG * t) { return t->prev; }
134 U16 swf_GetTagID(TAG * t) { return t->id; }
135 U32 swf_GetTagLen(TAG * t) { return t->len; }
136 U8* swf_GetTagLenPtr(TAG * t) { return &(t->data[t->len]); }
137 U32 swf_GetTagPos(TAG * t) { return t->pos; }
139 // for future purpose: avoid high level lib functions to change tagpos/bitpos
141 #define swf_SaveTagPos(tag)
142 #define swf_RestoreTagPos(tag)
144 void swf_SetTagPos(TAG * t,U32 pos)
145 { swf_ResetReadBits(t);
146 if (pos<=t->len) t->pos = pos;
149 fprintf(stderr,"SetTagPos(%d) out of bounds: TagID = %i\n",pos, t->id);
154 char* swf_GetString(TAG*t)
156 char* str = ((char*)(&(t)->data[(t)->pos]));
161 U8 swf_GetU8(TAG * t)
162 { swf_ResetReadBits(t);
165 { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
169 return t->data[t->pos++];
172 U16 swf_GetU16(TAG * t)
174 swf_ResetReadBits(t);
176 if (t->pos>(t->len-2))
177 { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
181 res = t->data[t->pos] | (t->data[t->pos+1]<<8);
186 U32 swf_GetU32(TAG * t)
188 swf_ResetReadBits(t);
190 if (t->pos>(t->len-4))
191 { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
195 res = t->data[t->pos] | (t->data[t->pos+1]<<8) |
196 (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
201 int swf_GetBlock(TAG * t,U8 * b,int l)
202 // returns number of bytes written (<=l)
203 // b = NULL -> skip data
204 { swf_ResetReadBits(t);
205 if ((t->len-t->pos)<l) l=t->len-t->pos;
206 if (b && l) memcpy(b,&t->data[t->pos],l);
211 int swf_SetBlock(TAG * t,U8 * b,int l)
212 // Appends Block to the end of Tagdata, returns size
213 { U32 newlen = t->len + l;
214 swf_ResetWriteBits(t);
215 if (newlen>t->memsize)
216 { U32 newmem = MEMSIZE(newlen);
217 U8 * newdata = (U8*)(rfx_realloc(t->data,newmem));
221 if (b) memcpy(&t->data[t->len],b,l);
222 else memset(&t->data[t->len],0x00,l);
227 int swf_SetU8(TAG * t,U8 v)
228 { swf_ResetWriteBits(t);
229 if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
230 t->data[t->len++] = v;
234 int swf_SetU16(TAG * t,U16 v)
239 swf_ResetWriteBits(t);
240 if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
241 t->data[t->len++] = a[0];
242 t->data[t->len++] = a[1];
245 void swf_SetS16(TAG * t,int v)
247 if(v>32767 || v<-32768) {
248 fprintf(stderr, "Warning: S16 overflow: %d\n", v);
250 swf_SetU16(t, (S16)v);
253 int swf_SetU32(TAG * t,U32 v)
255 a[0] = v&0xff; // to ensure correct handling of non-intel byteorder
260 swf_ResetWriteBits(t);
261 if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
262 t->data[t->len++] = a[0];
263 t->data[t->len++] = a[1];
264 t->data[t->len++] = a[2];
265 t->data[t->len++] = a[3];
269 U32 swf_GetBits(TAG * t,int nbits)
271 if (!nbits) return 0;
272 if (!t->readBit) t->readBit = 0x80;
275 if (t->data[t->pos]&t->readBit) res|=1;
279 { if (nbits) t->readBit = 0x80;
282 { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
292 S32 swf_GetSBits(TAG * t,int nbits)
293 { U32 res = swf_GetBits(t,nbits);
294 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
298 U32 reader_GetBits(struct reader_t*reader, int nbits)
299 { return reader_readbits(reader, nbits);
301 S32 reader_GetSBits(struct reader_t*reader, int nbits)
302 { U32 res = reader_readbits(reader, nbits);
303 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
307 int swf_SetBits(TAG * t,U32 v,int nbits)
308 { U32 bm = 1<<(nbits-1);
312 { if (FAILED(swf_SetU8(t,0))) return -1;
315 if (v&bm) t->data[t->len-1] |= t->writeBit;
323 // Advanced Data Access Functions
325 int swf_SetRGB(TAG * t,RGBA * col)
328 { swf_SetU8(t,col->r);
331 } else swf_SetBlock(t,NULL,3);
334 void swf_GetRGB(TAG * t, RGBA * col)
339 col->r = swf_GetU8(t);
340 col->g = swf_GetU8(t);
341 col->b = swf_GetU8(t);
345 int swf_SetRGBA(TAG * t,RGBA * col)
348 { swf_SetU8(t,col->r);
352 } else swf_SetBlock(t,NULL,4);
355 void swf_GetRGBA(TAG * t, RGBA * col)
360 col->r = swf_GetU8(t);
361 col->g = swf_GetU8(t);
362 col->b = swf_GetU8(t);
363 col->a = swf_GetU8(t);
366 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
371 memset(gradient, 0, sizeof(GRADIENT));
376 gradient->num = swf_GetU8(tag);
377 for(t=0;t<gradient->num;t++)
382 gradient->ratios[t] = swf_GetU8(tag);
384 swf_GetRGB(tag, &gradient->rgba[t]);
386 swf_GetRGBA(tag, &gradient->rgba[t]);
390 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
394 memset(gradient, 0, sizeof(GRADIENT));
397 swf_SetU8(tag, gradient->num);
398 for(t=0; t<8 && t<gradient->num; t++)
400 swf_SetU8(tag, gradient->ratios[t]);
402 swf_SetRGB(tag, &gradient->rgba[t]);
404 swf_SetRGBA(tag, &gradient->rgba[t]);
408 int swf_CountUBits(U32 v,int nbits)
411 if(v == 0x00000000) n = 0;
417 return (n>nbits)?n:nbits;
420 int swf_CountBits(U32 v,int nbits)
424 { if(v == 0xffffffff) n = 1;
432 { if(v == 0x00000000) n = 0;
439 return (n>nbits)?n:nbits;
442 int swf_GetRect(TAG * t,SRECT * r)
445 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
447 nbits = (int) swf_GetBits(t,5);
448 r->xmin = swf_GetSBits(t,nbits);
449 r->xmax = swf_GetSBits(t,nbits);
450 r->ymin = swf_GetSBits(t,nbits);
451 r->ymax = swf_GetSBits(t,nbits);
455 int reader_GetRect(struct reader_t*reader,SRECT * r)
459 nbits = (int) reader_GetBits(reader,5);
460 r->xmin = reader_GetSBits(reader,nbits);
461 r->xmax = reader_GetSBits(reader,nbits);
462 r->ymin = reader_GetSBits(reader,nbits);
463 r->ymax = reader_GetSBits(reader,nbits);
467 int swf_SetRect(TAG * t,SRECT * r)
470 nbits = swf_CountBits(r->xmin,0);
471 nbits = swf_CountBits(r->xmax,nbits);
472 nbits = swf_CountBits(r->ymin,nbits);
473 nbits = swf_CountBits(r->ymax,nbits);
475 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
479 swf_SetBits(t,nbits,5);
480 swf_SetBits(t,r->xmin,nbits);
481 swf_SetBits(t,r->xmax,nbits);
482 swf_SetBits(t,r->ymin,nbits);
483 swf_SetBits(t,r->ymax,nbits);
488 void swf_ExpandRect(SRECT*src, SPOINT add)
490 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
495 if((add.x|add.y) == 0) src->xmax++; //make sure the bbox is not NULL anymore
498 if(add.x < src->xmin)
500 if(add.x > src->xmax)
502 if(add.y < src->ymin)
504 if(add.y > src->ymax)
507 void swf_ExpandRect2(SRECT*src, SRECT*add)
509 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
511 if((src->xmin | src->ymin | src->xmax | src->ymax)==0)
513 if(add->xmin < src->xmin)
514 src->xmin = add->xmin;
515 if(add->ymin < src->ymin)
516 src->ymin = add->ymin;
517 if(add->xmax > src->xmax)
518 src->xmax = add->xmax;
519 if(add->ymax > src->ymax)
520 src->ymax = add->ymax;
522 void swf_ExpandRect3(SRECT*src, SPOINT center, int radius)
524 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
525 src->xmin = center.x-radius;
526 src->ymin = center.y-radius;
527 src->xmax = center.x+radius;
528 src->ymax = center.y+radius;
529 if((center.x|center.y|radius) == 0) src->xmax++; //make sure the bbox is not NULL anymore
532 if(center.x - radius < src->xmin)
533 src->xmin = center.x - radius;
534 if(center.x + radius > src->xmax)
535 src->xmax = center.x - radius;
536 if(center.y - radius < src->ymin)
537 src->ymin = center.y - radius;
538 if(center.y + radius > src->ymax)
539 src->ymax = center.y - radius;
541 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
544 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
545 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
548 SRECT swf_TurnRect(SRECT r, MATRIX* m)
551 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
552 p1.x = r.xmin;p1.y = r.ymin;
553 p2.x = r.xmax;p2.y = r.ymin;
554 p3.x = r.xmin;p3.y = r.ymax;
555 p4.x = r.xmax;p4.y = r.ymax;
556 pp1 = swf_TurnPoint(p1, m);
557 pp2 = swf_TurnPoint(p2, m);
558 pp3 = swf_TurnPoint(p3, m);
559 pp4 = swf_TurnPoint(p4, m);
560 g.xmin = g.xmax = pp1.x;
561 g.ymin = g.ymax = pp1.y;
562 swf_ExpandRect(&g, pp2);
563 swf_ExpandRect(&g, pp3);
564 swf_ExpandRect(&g, pp4);
569 int swf_GetMatrix(TAG * t,MATRIX * m)
576 { m->sx = m->sy = 0x10000;
582 swf_ResetReadBits(t);
584 if (swf_GetBits(t,1))
585 { nbits = swf_GetBits(t,5);
586 m->sx = swf_GetSBits(t,nbits);
587 m->sy = swf_GetSBits(t,nbits);
589 else m->sx = m->sy = 0x10000;
591 if (swf_GetBits(t,1))
592 { nbits = swf_GetBits(t,5);
593 m->r0 = swf_GetSBits(t,nbits);
594 m->r1 = swf_GetSBits(t,nbits);
596 else m->r0 = m->r1 = 0x0;
598 nbits = swf_GetBits(t,5);
599 m->tx = swf_GetSBits(t,nbits);
600 m->ty = swf_GetSBits(t,nbits);
605 int swf_SetMatrix(TAG * t,MATRIX * m)
611 ma.sx = ma.sy = 0x10000;
616 swf_ResetWriteBits(t);
618 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
620 { swf_SetBits(t,1,1);
621 nbits = swf_CountBits(m->sx,0);
622 nbits = swf_CountBits(m->sy,nbits);
624 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
627 swf_SetBits(t,nbits,5);
628 swf_SetBits(t,m->sx,nbits);
629 swf_SetBits(t,m->sy,nbits);
632 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
634 { swf_SetBits(t,1,1);
635 nbits = swf_CountBits(m->r0,0);
636 nbits = swf_CountBits(m->r1,nbits);
638 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
641 swf_SetBits(t,nbits,5);
642 swf_SetBits(t,m->r0,nbits);
643 swf_SetBits(t,m->r1,nbits);
646 nbits = swf_CountBits(m->tx,0);
647 nbits = swf_CountBits(m->ty,nbits);
649 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
652 swf_SetBits(t,nbits,5);
653 swf_SetBits(t,m->tx,nbits);
654 swf_SetBits(t,m->ty,nbits);
659 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha) //FIXME: alpha should be type bool
667 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
668 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
672 swf_ResetReadBits(t);
673 hasadd = swf_GetBits(t,1);
674 hasmul = swf_GetBits(t,1);
675 nbits = swf_GetBits(t,4);
678 { cx->r0 = (S16)swf_GetSBits(t,nbits);
679 cx->g0 = (S16)swf_GetSBits(t,nbits);
680 cx->b0 = (S16)swf_GetSBits(t,nbits);
682 cx->a0 = (S16)swf_GetSBits(t,nbits);
686 { cx->r1 = (S16)swf_GetSBits(t,nbits);
687 cx->g1 = (S16)swf_GetSBits(t,nbits);
688 cx->b1 = (S16)swf_GetSBits(t,nbits);
690 cx->a1 = (S16)swf_GetSBits(t,nbits);
696 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
704 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
705 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
715 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
716 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
719 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
720 nbits = swf_CountBits((S32)cx->r0,nbits);
721 nbits = swf_CountBits((S32)cx->g0,nbits);
722 nbits = swf_CountBits((S32)cx->b0,nbits);
726 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
727 nbits = swf_CountBits((S32)cx->r1,nbits);
728 nbits = swf_CountBits((S32)cx->g1,nbits);
729 nbits = swf_CountBits((S32)cx->b1,nbits);
732 swf_ResetWriteBits(t);
733 swf_SetBits(t,hasadd?1:0,1);
734 swf_SetBits(t,hasmul?1:0,1);
735 swf_SetBits(t,nbits,4);
738 { swf_SetBits(t,cx->r0,nbits);
739 swf_SetBits(t,cx->g0,nbits);
740 swf_SetBits(t,cx->b0,nbits);
741 if (alpha) swf_SetBits(t,cx->a0,nbits);
745 { swf_SetBits(t,cx->r1,nbits);
746 swf_SetBits(t,cx->g1,nbits);
747 swf_SetBits(t,cx->b1,nbits);
748 if (alpha) swf_SetBits(t,cx->a1,nbits);
754 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
755 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
757 void swf_SetPassword(TAG * t, const char * password)
759 /* WARNING: crypt_md5 is not reentrant */
763 #if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) && defined(HAVE_TIME_H) && defined(HAVE_TIME)
765 salt[0] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
766 salt[1] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
770 fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
771 fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
774 md5string = crypt_md5(password, salt);
777 swf_SetString(t, md5string);
780 int swf_VerifyPassword(TAG * t, const char * password)
782 char*md5string1, *md5string2;
787 if(t->len >= 5 && t->pos==0 &&
792 printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
795 md5string1 = swf_GetString(t);
797 if(strncmp(md5string1, "$1$",3 )) {
798 fprintf(stderr, "rfxswf: no salt in pw string\n");
801 x = strchr(md5string1+3, '$');
803 fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
806 n = x-(md5string1+3);
807 salt = (char*)rfx_alloc(n+1);
808 memcpy(salt, md5string1+3, n);
811 md5string2 = crypt_md5(password, salt);
813 if(strcmp(md5string1, md5string2) != 0)
818 // Tag List Manipulating Functions
820 TAG * swf_InsertTag(TAG * after,U16 id)
823 t = (TAG *)rfx_calloc(sizeof(TAG));
829 t->next = after->next;
831 if (t->next) t->next->prev = t;
836 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
839 t = (TAG *)rfx_calloc(sizeof(TAG));
845 t->prev = before->prev;
847 if (t->prev) t->prev->next = t;
849 if(swf && swf->firstTag == before) {
855 void swf_ClearTag(TAG * t)
857 if (t->data) rfx_free(t->data);
866 void swf_ResetTag(TAG*tag, U16 id)
868 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
872 TAG* swf_CopyTag(TAG*tag, TAG*to_copy)
874 tag = swf_InsertTag(tag, to_copy->id);
875 swf_SetBlock(tag, to_copy->data, to_copy->len);
879 int swf_DeleteTag(TAG * t)
882 if (t->prev) t->prev->next = t->next;
883 if (t->next) t->next->prev = t->prev;
885 if (t->data) rfx_free(t->data);
890 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
896 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
904 if (reader->read(reader, &len, 4) != 4) return NULL;
908 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
909 // Sprite handling fix: Flatten sprite tree
911 t = (TAG *)rfx_calloc(sizeof(TAG));
917 { t->data = (U8*)rfx_alloc(t->len);
919 if (reader->read(reader, t->data, t->len) != t->len) return NULL;
931 int swf_DefineSprite_GetRealSize(TAG * t);
933 int swf_WriteTag2(struct writer_t*writer, TAG * t)
934 // returns tag length in bytes (incl. Header), -1 = Error
935 // writer = 0 -> no output
942 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
944 short_tag = len<0x3f&&(t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2);
948 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
949 if (writer->write(writer,raw,2)!=2)
952 fprintf(stderr,"WriteTag() failed: Short Header.\n");
959 raw[0] = SWAP16((t->id<<6)|0x3f);
960 if (writer->write(writer,raw,2)!=2)
963 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
969 if (writer->write(writer,&len,4)!=4)
972 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
979 { if (writer->write(writer,t->data,t->len)!=t->len)
982 fprintf(stderr,"WriteTag() failed: Data.\n");
988 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
992 return t->len+(short_tag?2:6);
995 int swf_WriteTag(int handle, TAG * t)
997 struct writer_t writer;
1000 return swf_WriteTag2(0, t);
1001 writer_init_filewriter(&writer, handle);
1002 len = swf_WriteTag2(&writer, t);
1003 writer.finish(&writer);
1007 int swf_DefineSprite_GetRealSize(TAG * t)
1008 // Sprite Handling: Helper function to pack DefineSprite-Tag
1010 if(len>4) { // folded sprite
1014 { t = swf_NextTag(t);
1015 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
1017 } while (t&&(t->id!=ST_END));
1021 void swf_UnFoldSprite(TAG * t)
1026 U16 spriteid,spriteframes;
1028 if(t->id!=ST_DEFINESPRITE)
1030 if(t->len<=4) // not folded
1035 spriteid = swf_GetU16(t); //id
1036 spriteframes = swf_GetU16(t); //frames
1043 tmp = swf_GetU16(t);
1048 if(id == ST_DEFINESPRITE && len<=4)
1052 len = swf_GetU32(t);
1053 it = swf_InsertTag(next, id);
1058 { it->data = (U8*)rfx_alloc(it->len);
1059 it->memsize = it->len;
1060 swf_GetBlock(t, it->data, it->len);
1067 rfx_free(t->data); t->data = 0;
1068 t->memsize = t->len = t->pos = 0;
1070 swf_SetU16(t, spriteid);
1071 swf_SetU16(t, spriteframes);
1074 void swf_FoldSprite(TAG * t)
1077 U16 id,frames,tmpid;
1079 if(t->id!=ST_DEFINESPRITE)
1082 fprintf(stderr, "Error: Sprite has no ID!");
1086 /* sprite is already folded */
1093 t->len = t->pos = t->memsize = 0;
1098 t = swf_NextTag(sprtag);
1103 if(t->id==ST_SHOWFRAME) frames++;
1104 if(t->id == ST_DEFINESPRITE && t->len<=4)
1109 } while(t && level);
1111 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1113 swf_SetU16(sprtag, id);
1114 swf_SetU16(sprtag, frames);
1116 t = swf_NextTag(sprtag);
1121 if(t->len<0x3f&&t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2) {
1122 swf_SetU16(sprtag,t->len|(t->id<<6));
1124 swf_SetU16(sprtag,0x3f|(t->id<<6));
1125 swf_SetU32(sprtag,t->len);
1128 swf_SetBlock(sprtag,t->data, t->len);
1130 if(t->id == ST_DEFINESPRITE && t->len<=4)
1139 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1141 // sprtag->next = t;
1142 // t->prev = sprtag;
1145 int swf_IsFolded(TAG * t)
1147 return (t->id == ST_DEFINESPRITE && t->len>4);
1150 void swf_FoldAll(SWF*swf)
1152 TAG*tag = swf->firstTag;
1153 //swf_DumpSWF(stdout, swf);
1155 if(tag->id == ST_DEFINESPRITE) {
1156 swf_FoldSprite(tag);
1157 //swf_DumpSWF(stdout, swf);
1159 tag = swf_NextTag(tag);
1163 void swf_UnFoldAll(SWF*swf)
1165 TAG*tag = swf->firstTag;
1167 if(tag->id == ST_DEFINESPRITE)
1168 swf_UnFoldSprite(tag);
1173 void swf_OptimizeTagOrder(SWF*swf)
1180 /* at the moment, we don't actually do optimizing,
1181 only fixing of non-spec-conformant things like
1188 tag = swf->firstTag;
1191 if(tag->id == ST_DEFINESPRITE) {
1193 /* ??? all sprites are supposed to be unfolded */
1194 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1204 /* move non-sprite tags out of sprite */
1205 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1206 /* remove tag from current position */
1207 tag->prev->next = tag->next;
1209 tag->next->prev = tag->prev;
1211 /* insert before tag level0 */
1213 tag->prev = level0->prev;
1215 tag->prev->next = tag;
1219 if(tag->id == ST_END) {
1230 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1232 if (!swf) return -1;
1233 memset(swf,0x00,sizeof(SWF));
1235 { char b[32]; // read Header
1239 struct reader_t zreader;
1241 if ((len = reader->read(reader ,b,8))<8) return -1;
1243 if (b[0]!='F' && b[0]!='C') return -1;
1244 if (b[1]!='W') return -1;
1245 if (b[2]!='S') return -1;
1246 swf->fileVersion = b[3];
1247 swf->compressed = (b[0]=='C')?1:0;
1248 swf->fileSize = GET32(&b[4]);
1250 if(swf->compressed) {
1251 reader_init_zlibinflate(&zreader, reader);
1255 reader_GetRect(reader, &swf->movieSize);
1256 reader->read(reader, &swf->frameRate, 2);
1257 swf->frameRate = SWAP16(swf->frameRate);
1258 reader->read(reader, &swf->frameCount, 2);
1259 swf->frameCount = SWAP16(swf->frameCount);
1261 /* read tags and connect to list */
1263 while (t) t = swf_ReadTag(reader,t);
1264 swf->firstTag = t1.next;
1265 t1.next->prev = NULL;
1271 int swf_ReadSWF(int handle, SWF * swf)
1273 struct reader_t reader;
1274 reader_init_filereader(&reader, handle);
1275 return swf_ReadSWF2(&reader, swf);
1278 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1282 struct writer_t zwriter;
1285 int writer_lastpos = 0;
1288 if (!swf) return -1;
1289 if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1291 if(writer) writer_lastpos = writer->pos;
1293 // Insert REFLEX Tag
1295 #ifdef INSERT_RFX_TAG
1297 if (swf->firstTag && swf_NextTag(swf->firstTag))
1298 if (swf_GetTagID(swf_NextTag(swf->firstTag))!=ST_REFLEX)
1299 swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),"rfx",3);
1301 #endif // INSERT_RFX_TAG
1303 // Count Frames + File Size
1310 len += swf_WriteTag(-1,t);
1311 if(t->id == ST_DEFINESPRITE && !swf_IsFolded(t)) inSprite++;
1312 else if(t->id == ST_END && inSprite) inSprite--;
1313 else if(t->id == ST_END && !inSprite) {
1314 if(t->prev && t->prev->id!=ST_SHOWFRAME)
1317 else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1325 memset(&t1,0x00,sizeof(TAG));
1329 { // measure header file size
1332 memset(&t2,0x00,sizeof(TAG));
1335 swf_SetRect(&t2, &swf->movieSize);
1336 swf_SetU16(&t2, swf->frameRate);
1337 swf_SetU16(&t2, swf->frameCount);
1338 l = swf_GetTagLen(&t2)+8;
1340 if(swf->compressed == 8) {
1345 if(len) {// don't touch headers without tags
1346 swf->fileSize = fileSize;
1347 swf->frameCount = frameCount;
1350 if(swf->compressed != 8) {
1351 /* compressed flag set to 8 means "skip first 8
1352 header bytes". This is necessary if the caller wants to
1353 create compressed SWFs himself .
1354 It also means that we don't initialize our own zlib
1355 writer, but assume the caller provided one.
1357 if(swf->compressed) {
1359 writer->write(writer, id, 3);
1363 writer->write(writer, id, 3);
1366 writer->write(writer, &swf->fileVersion, 1);
1367 PUT32(b4, swf->fileSize);
1368 writer->write(writer, b4, 4);
1370 if(swf->compressed) {
1371 writer_init_zlibdeflate(&zwriter, writer);
1376 swf_SetRect(&t1,&swf->movieSize);
1377 swf_SetU16(&t1,swf->frameRate);
1378 swf_SetU16(&t1,swf->frameCount);
1380 ret = writer->write(writer,b,swf_GetTagLen(&t1));
1381 if (ret!=swf_GetTagLen(&t1))
1384 fprintf(stderr, "ret:%d\n",ret);
1386 fprintf(stderr,"WriteSWF() failed: Header.\n");
1393 { if (swf_WriteTag2(writer, t)<0) return -1;
1396 if(swf->compressed) {
1397 if(swf->compressed != 8) {
1398 zwriter.finish(&zwriter);
1399 return writer->pos - writer_lastpos;
1401 return (int)fileSize;
1403 return (int)fileSize;
1408 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1410 struct writer_t writer;
1412 swf->compressed = 0;
1415 writer_init_nullwriter(&writer);
1416 len = swf_WriteSWF2(&writer, swf);
1418 writer_init_filewriter(&writer, handle);
1419 len = swf_WriteSWF2(&writer, swf);
1420 writer.finish(&writer);
1424 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1426 struct writer_t writer;
1428 swf->compressed = 1;
1431 writer_init_nullwriter(&writer);
1432 len = swf_WriteSWF2(&writer, swf);
1434 writer_init_filewriter(&writer, handle);
1435 len = swf_WriteSWF2(&writer, swf);
1436 writer.finish(&writer);
1440 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1443 memcpy(&myswf,swf,sizeof(SWF));
1445 return swf_WriteSWF2(writer, &myswf);
1448 int swf_WriteHeader(int handle,SWF * swf)
1451 memcpy(&myswf,swf,sizeof(SWF));
1453 return swf_WriteSWF(handle, &myswf);
1456 int swf_WriteCGI(SWF * swf)
1460 len = swf_WriteSWF(-1,swf);
1462 if (len<0) return -1;
1464 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1465 "Accept-Ranges: bytes\n"
1466 "Content-Length: %lu\n"
1467 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1470 write(fileno(stdout),s,strlen(s));
1471 return swf_WriteSWF(fileno(stdout),swf);
1474 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1475 { TAG * t = swf->firstTag;
1478 { TAG * tnew = t->next;
1479 if (t->data) rfx_free(t->data);
1486 // include advanced functions
1488 #include "modules/swfdump.c"
1489 #include "modules/swfshape.c"
1490 #include "modules/swftext.c"
1491 #include "modules/swffont.c"
1492 #include "modules/swfobject.c"
1493 #include "modules/swfbutton.c"
1494 #include "modules/swftools.c"
1495 #include "modules/swfcgi.c"
1496 #include "modules/swfbits.c"
1497 #include "modules/swfaction.c"
1498 #include "modules/swfsound.c"
1499 #include "modules/swfdraw.c"