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) {
920 fprintf(stderr, "rfxswf: Warning: Short read (tagid %d). File truncated?\n", t->id);
921 free(t->data);t->data=0;
936 int swf_DefineSprite_GetRealSize(TAG * t);
938 int swf_WriteTag2(struct writer_t*writer, TAG * t)
939 // returns tag length in bytes (incl. Header), -1 = Error
940 // writer = 0 -> no output
947 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
949 short_tag = len<0x3f&&(t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2);
953 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
954 if (writer->write(writer,raw,2)!=2)
957 fprintf(stderr,"WriteTag() failed: Short Header.\n");
964 raw[0] = SWAP16((t->id<<6)|0x3f);
965 if (writer->write(writer,raw,2)!=2)
968 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
974 if (writer->write(writer,&len,4)!=4)
977 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
984 { if (writer->write(writer,t->data,t->len)!=t->len)
987 fprintf(stderr,"WriteTag() failed: Data.\n");
993 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
997 return t->len+(short_tag?2:6);
1000 int swf_WriteTag(int handle, TAG * t)
1002 struct writer_t writer;
1005 return swf_WriteTag2(0, t);
1006 writer_init_filewriter(&writer, handle);
1007 len = swf_WriteTag2(&writer, t);
1008 writer.finish(&writer);
1012 int swf_DefineSprite_GetRealSize(TAG * t)
1013 // Sprite Handling: Helper function to pack DefineSprite-Tag
1015 if(len>4) { // folded sprite
1019 { t = swf_NextTag(t);
1020 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
1022 } while (t&&(t->id!=ST_END));
1026 void swf_UnFoldSprite(TAG * t)
1031 U16 spriteid,spriteframes;
1033 if(t->id!=ST_DEFINESPRITE)
1035 if(t->len<=4) // not folded
1040 spriteid = swf_GetU16(t); //id
1041 spriteframes = swf_GetU16(t); //frames
1048 tmp = swf_GetU16(t);
1053 if(id == ST_DEFINESPRITE && len<=4)
1057 len = swf_GetU32(t);
1058 it = swf_InsertTag(next, id);
1063 { it->data = (U8*)rfx_alloc(it->len);
1064 it->memsize = it->len;
1065 swf_GetBlock(t, it->data, it->len);
1072 rfx_free(t->data); t->data = 0;
1073 t->memsize = t->len = t->pos = 0;
1075 swf_SetU16(t, spriteid);
1076 swf_SetU16(t, spriteframes);
1079 void swf_FoldSprite(TAG * t)
1082 U16 id,frames,tmpid;
1084 if(t->id!=ST_DEFINESPRITE)
1087 fprintf(stderr, "Error: Sprite has no ID!");
1091 /* sprite is already folded */
1098 t->len = t->pos = t->memsize = 0;
1103 t = swf_NextTag(sprtag);
1108 if(t->id==ST_SHOWFRAME) frames++;
1109 if(t->id == ST_DEFINESPRITE && t->len<=4)
1114 } while(t && level);
1116 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1118 swf_SetU16(sprtag, id);
1119 swf_SetU16(sprtag, frames);
1121 t = swf_NextTag(sprtag);
1126 if(t->len<0x3f&&t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2) {
1127 swf_SetU16(sprtag,t->len|(t->id<<6));
1129 swf_SetU16(sprtag,0x3f|(t->id<<6));
1130 swf_SetU32(sprtag,t->len);
1133 swf_SetBlock(sprtag,t->data, t->len);
1135 if(t->id == ST_DEFINESPRITE && t->len<=4)
1144 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1146 // sprtag->next = t;
1147 // t->prev = sprtag;
1150 int swf_IsFolded(TAG * t)
1152 return (t->id == ST_DEFINESPRITE && t->len>4);
1155 void swf_FoldAll(SWF*swf)
1157 TAG*tag = swf->firstTag;
1158 //swf_DumpSWF(stdout, swf);
1160 if(tag->id == ST_DEFINESPRITE) {
1161 swf_FoldSprite(tag);
1162 //swf_DumpSWF(stdout, swf);
1164 tag = swf_NextTag(tag);
1168 void swf_UnFoldAll(SWF*swf)
1170 TAG*tag = swf->firstTag;
1172 if(tag->id == ST_DEFINESPRITE)
1173 swf_UnFoldSprite(tag);
1178 void swf_OptimizeTagOrder(SWF*swf)
1185 /* at the moment, we don't actually do optimizing,
1186 only fixing of non-spec-conformant things like
1193 tag = swf->firstTag;
1196 if(tag->id == ST_DEFINESPRITE) {
1198 /* ??? all sprites are supposed to be unfolded */
1199 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1209 /* move non-sprite tags out of sprite */
1210 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1211 /* remove tag from current position */
1212 tag->prev->next = tag->next;
1214 tag->next->prev = tag->prev;
1216 /* insert before tag level0 */
1218 tag->prev = level0->prev;
1220 tag->prev->next = tag;
1224 if(tag->id == ST_END) {
1235 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1237 if (!swf) return -1;
1238 memset(swf,0x00,sizeof(SWF));
1240 { char b[32]; // read Header
1244 struct reader_t zreader;
1246 if ((len = reader->read(reader ,b,8))<8) return -1;
1248 if (b[0]!='F' && b[0]!='C') return -1;
1249 if (b[1]!='W') return -1;
1250 if (b[2]!='S') return -1;
1251 swf->fileVersion = b[3];
1252 swf->compressed = (b[0]=='C')?1:0;
1253 swf->fileSize = GET32(&b[4]);
1255 if(swf->compressed) {
1256 reader_init_zlibinflate(&zreader, reader);
1260 reader_GetRect(reader, &swf->movieSize);
1261 reader->read(reader, &swf->frameRate, 2);
1262 swf->frameRate = SWAP16(swf->frameRate);
1263 reader->read(reader, &swf->frameCount, 2);
1264 swf->frameCount = SWAP16(swf->frameCount);
1266 /* read tags and connect to list */
1268 while (t) t = swf_ReadTag(reader,t);
1269 swf->firstTag = t1.next;
1270 t1.next->prev = NULL;
1276 int swf_ReadSWF(int handle, SWF * swf)
1278 struct reader_t reader;
1279 reader_init_filereader(&reader, handle);
1280 return swf_ReadSWF2(&reader, swf);
1283 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1287 struct writer_t zwriter;
1290 int writer_lastpos = 0;
1293 if (!swf) return -1;
1294 if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1296 if(writer) writer_lastpos = writer->pos;
1298 // Insert REFLEX Tag
1300 #ifdef INSERT_RFX_TAG
1302 if (swf->firstTag && swf_NextTag(swf->firstTag))
1303 if (swf_GetTagID(swf_NextTag(swf->firstTag))!=ST_REFLEX)
1304 swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),"rfx",3);
1306 #endif // INSERT_RFX_TAG
1308 // Count Frames + File Size
1315 len += swf_WriteTag(-1,t);
1316 if(t->id == ST_DEFINESPRITE && !swf_IsFolded(t)) inSprite++;
1317 else if(t->id == ST_END && inSprite) inSprite--;
1318 else if(t->id == ST_END && !inSprite) {
1319 if(t->prev && t->prev->id!=ST_SHOWFRAME)
1322 else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1330 memset(&t1,0x00,sizeof(TAG));
1334 { // measure header file size
1337 memset(&t2,0x00,sizeof(TAG));
1340 swf_SetRect(&t2, &swf->movieSize);
1341 swf_SetU16(&t2, swf->frameRate);
1342 swf_SetU16(&t2, swf->frameCount);
1343 l = swf_GetTagLen(&t2)+8;
1345 if(swf->compressed == 8) {
1350 if(len) {// don't touch headers without tags
1351 swf->fileSize = fileSize;
1352 swf->frameCount = frameCount;
1355 if(swf->compressed != 8) {
1356 /* compressed flag set to 8 means "skip first 8
1357 header bytes". This is necessary if the caller wants to
1358 create compressed SWFs himself .
1359 It also means that we don't initialize our own zlib
1360 writer, but assume the caller provided one.
1362 if(swf->compressed) {
1364 writer->write(writer, id, 3);
1368 writer->write(writer, id, 3);
1371 writer->write(writer, &swf->fileVersion, 1);
1372 PUT32(b4, swf->fileSize);
1373 writer->write(writer, b4, 4);
1375 if(swf->compressed) {
1376 writer_init_zlibdeflate(&zwriter, writer);
1381 swf_SetRect(&t1,&swf->movieSize);
1382 swf_SetU16(&t1,swf->frameRate);
1383 swf_SetU16(&t1,swf->frameCount);
1385 ret = writer->write(writer,b,swf_GetTagLen(&t1));
1386 if (ret!=swf_GetTagLen(&t1))
1389 fprintf(stderr, "ret:%d\n",ret);
1391 fprintf(stderr,"WriteSWF() failed: Header.\n");
1398 { if (swf_WriteTag2(writer, t)<0) return -1;
1401 if(swf->compressed) {
1402 if(swf->compressed != 8) {
1403 zwriter.finish(&zwriter);
1404 return writer->pos - writer_lastpos;
1406 return (int)fileSize;
1408 return (int)fileSize;
1413 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1415 struct writer_t writer;
1417 swf->compressed = 0;
1420 writer_init_nullwriter(&writer);
1421 len = swf_WriteSWF2(&writer, swf);
1423 writer_init_filewriter(&writer, handle);
1424 len = swf_WriteSWF2(&writer, swf);
1425 writer.finish(&writer);
1429 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1431 struct writer_t writer;
1433 swf->compressed = 1;
1436 writer_init_nullwriter(&writer);
1437 len = swf_WriteSWF2(&writer, swf);
1439 writer_init_filewriter(&writer, handle);
1440 len = swf_WriteSWF2(&writer, swf);
1441 writer.finish(&writer);
1445 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1448 memcpy(&myswf,swf,sizeof(SWF));
1450 return swf_WriteSWF2(writer, &myswf);
1453 int swf_WriteHeader(int handle,SWF * swf)
1456 memcpy(&myswf,swf,sizeof(SWF));
1458 return swf_WriteSWF(handle, &myswf);
1461 int swf_WriteCGI(SWF * swf)
1465 len = swf_WriteSWF(-1,swf);
1467 if (len<0) return -1;
1469 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1470 "Accept-Ranges: bytes\n"
1471 "Content-Length: %lu\n"
1472 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1475 write(fileno(stdout),s,strlen(s));
1476 return swf_WriteSWF(fileno(stdout),swf);
1479 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1480 { TAG * t = swf->firstTag;
1483 { TAG * tnew = t->next;
1484 if (t->data) rfx_free(t->data);
1491 // include advanced functions
1493 #include "modules/swfdump.c"
1494 #include "modules/swfshape.c"
1495 #include "modules/swftext.c"
1496 #include "modules/swffont.c"
1497 #include "modules/swfobject.c"
1498 #include "modules/swfbutton.c"
1499 #include "modules/swftools.c"
1500 #include "modules/swfcgi.c"
1501 #include "modules/swfbits.c"
1502 #include "modules/swfaction.c"
1503 #include "modules/swfsound.c"
1504 #include "modules/swfdraw.c"