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* rfxalloc(int size)
63 fprintf(stderr, "FATAL: Out of memory\n");
64 /* TODO: we should send a signal, so that the debugger kicks in */
70 void rfxdealloc(void*ptr)
77 #define MALLOC_SIZE 128
78 #define INSERT_RFX_TAG
80 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
82 // inline wrapper functions
84 TAG * swf_NextTag(TAG * t) { return t->next; }
85 TAG * swf_PrevTag(TAG * t) { return t->prev; }
86 U16 swf_GetTagID(TAG * t) { return t->id; }
87 U32 swf_GetTagLen(TAG * t) { return t->len; }
88 U8* swf_GetTagLenPtr(TAG * t) { return &(t->data[t->len]); }
89 U32 swf_GetTagPos(TAG * t) { return t->pos; }
91 // for future purpose: avoid high level lib functions to change tagpos/bitpos
93 #define swf_SaveTagPos(tag)
94 #define swf_RestoreTagPos(tag)
96 void swf_SetTagPos(TAG * t,U32 pos)
97 { swf_ResetReadBits(t);
98 if (pos<=t->len) t->pos = pos;
101 fprintf(stderr,"SetTagPos(%d) out of bounds: TagID = %i\n",pos, t->id);
106 char* swf_GetString(TAG*t)
108 char* str = ((char*)(&(t)->data[(t)->pos]));
113 U8 swf_GetU8(TAG * t)
114 { swf_ResetReadBits(t);
117 { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
121 return t->data[t->pos++];
124 U16 swf_GetU16(TAG * t)
126 swf_ResetReadBits(t);
128 if (t->pos>(t->len-2))
129 { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
133 res = t->data[t->pos] | (t->data[t->pos+1]<<8);
138 U32 swf_GetU32(TAG * t)
140 swf_ResetReadBits(t);
142 if (t->pos>(t->len-4))
143 { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
147 res = t->data[t->pos] | (t->data[t->pos+1]<<8) |
148 (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
153 int swf_GetBlock(TAG * t,U8 * b,int l)
154 // returns number of bytes written (<=l)
155 // b = NULL -> skip data
156 { swf_ResetReadBits(t);
157 if ((t->len-t->pos)<l) l=t->len-t->pos;
158 if (b && l) memcpy(b,&t->data[t->pos],l);
163 int swf_SetBlock(TAG * t,U8 * b,int l)
164 // Appends Block to the end of Tagdata, returns size
165 { U32 newlen = t->len + l;
166 swf_ResetWriteBits(t);
167 if (newlen>t->memsize)
168 { U32 newmem = MEMSIZE(newlen);
169 U8 * newdata = (U8*)((t->data)?realloc(t->data,newmem):malloc(newmem));
173 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (1). (%d bytes)\n", newmem);
181 if (b) memcpy(&t->data[t->len],b,l);
182 else memset(&t->data[t->len],0x00,l);
187 int swf_SetU8(TAG * t,U8 v)
188 { swf_ResetWriteBits(t);
189 if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
190 t->data[t->len++] = v;
194 int swf_SetU16(TAG * t,U16 v)
199 swf_ResetWriteBits(t);
200 if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
201 t->data[t->len++] = a[0];
202 t->data[t->len++] = a[1];
205 void swf_SetS16(TAG * t,int v)
207 if(v>32767 || v<-32768) {
208 fprintf(stderr, "Warning: S16 overflow: %d\n", v);
210 swf_SetU16(t, (S16)v);
213 int swf_SetU32(TAG * t,U32 v)
215 a[0] = v&0xff; // to ensure correct handling of non-intel byteorder
220 swf_ResetWriteBits(t);
221 if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
222 t->data[t->len++] = a[0];
223 t->data[t->len++] = a[1];
224 t->data[t->len++] = a[2];
225 t->data[t->len++] = a[3];
229 U32 swf_GetBits(TAG * t,int nbits)
231 if (!nbits) return 0;
232 if (!t->readBit) t->readBit = 0x80;
235 if (t->data[t->pos]&t->readBit) res|=1;
239 { if (nbits) t->readBit = 0x80;
242 { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
252 S32 swf_GetSBits(TAG * t,int nbits)
253 { U32 res = swf_GetBits(t,nbits);
254 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
258 U32 reader_GetBits(struct reader_t*reader, int nbits)
259 { return reader_readbits(reader, nbits);
261 S32 reader_GetSBits(struct reader_t*reader, int nbits)
262 { U32 res = reader_readbits(reader, nbits);
263 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
267 int swf_SetBits(TAG * t,U32 v,int nbits)
268 { U32 bm = 1<<(nbits-1);
272 { if (FAILED(swf_SetU8(t,0))) return -1;
275 if (v&bm) t->data[t->len-1] |= t->writeBit;
283 // Advanced Data Access Functions
285 int swf_SetRGB(TAG * t,RGBA * col)
288 { swf_SetU8(t,col->r);
291 } else swf_SetBlock(t,NULL,3);
294 void swf_GetRGB(TAG * t, RGBA * col)
299 col->r = swf_GetU8(t);
300 col->g = swf_GetU8(t);
301 col->b = swf_GetU8(t);
305 int swf_SetRGBA(TAG * t,RGBA * col)
308 { swf_SetU8(t,col->r);
312 } else swf_SetBlock(t,NULL,4);
315 void swf_GetRGBA(TAG * t, RGBA * col)
320 col->r = swf_GetU8(t);
321 col->g = swf_GetU8(t);
322 col->b = swf_GetU8(t);
323 col->a = swf_GetU8(t);
326 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
331 memset(gradient, 0, sizeof(GRADIENT));
336 gradient->num = swf_GetU8(tag);
337 for(t=0;t<gradient->num;t++)
342 gradient->ratios[t] = swf_GetU8(tag);
344 swf_GetRGB(tag, &gradient->rgba[t]);
346 swf_GetRGBA(tag, &gradient->rgba[t]);
350 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
354 memset(gradient, 0, sizeof(GRADIENT));
357 swf_SetU8(tag, gradient->num);
358 for(t=0; t<8 && t<gradient->num; t++)
360 swf_SetU8(tag, gradient->ratios[t]);
362 swf_SetRGB(tag, &gradient->rgba[t]);
364 swf_SetRGBA(tag, &gradient->rgba[t]);
368 int swf_CountUBits(U32 v,int nbits)
371 if(v == 0x00000000) n = 0;
377 return (n>nbits)?n:nbits;
380 int swf_CountBits(U32 v,int nbits)
384 { if(v == 0xffffffff) n = 1;
392 { if(v == 0x00000000) n = 0;
399 return (n>nbits)?n:nbits;
402 int swf_GetRect(TAG * t,SRECT * r)
405 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
407 nbits = (int) swf_GetBits(t,5);
408 r->xmin = swf_GetSBits(t,nbits);
409 r->xmax = swf_GetSBits(t,nbits);
410 r->ymin = swf_GetSBits(t,nbits);
411 r->ymax = swf_GetSBits(t,nbits);
415 int reader_GetRect(struct reader_t*reader,SRECT * r)
419 nbits = (int) reader_GetBits(reader,5);
420 r->xmin = reader_GetSBits(reader,nbits);
421 r->xmax = reader_GetSBits(reader,nbits);
422 r->ymin = reader_GetSBits(reader,nbits);
423 r->ymax = reader_GetSBits(reader,nbits);
427 int swf_SetRect(TAG * t,SRECT * r)
430 nbits = swf_CountBits(r->xmin,0);
431 nbits = swf_CountBits(r->xmax,nbits);
432 nbits = swf_CountBits(r->ymin,nbits);
433 nbits = swf_CountBits(r->ymax,nbits);
435 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
439 swf_SetBits(t,nbits,5);
440 swf_SetBits(t,r->xmin,nbits);
441 swf_SetBits(t,r->xmax,nbits);
442 swf_SetBits(t,r->ymin,nbits);
443 swf_SetBits(t,r->ymax,nbits);
448 void swf_ExpandRect(SRECT*src, SPOINT add)
450 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
455 if((add.x|add.y) == 0) src->xmax++; //make sure the bbox is not NULL anymore
458 if(add.x < src->xmin)
460 if(add.x > src->xmax)
462 if(add.y < src->ymin)
464 if(add.y > src->ymax)
467 void swf_ExpandRect2(SRECT*src, SRECT*add)
469 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
471 if((src->xmin | src->ymin | src->xmax | src->ymax)==0)
473 if(add->xmin < src->xmin)
474 src->xmin = add->xmin;
475 if(add->ymin < src->ymin)
476 src->ymin = add->ymin;
477 if(add->xmax > src->xmax)
478 src->xmax = add->xmax;
479 if(add->ymax > src->ymax)
480 src->ymax = add->ymax;
482 void swf_ExpandRect3(SRECT*src, SPOINT center, int radius)
484 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
485 src->xmin = center.x-radius;
486 src->ymin = center.y-radius;
487 src->xmax = center.x+radius;
488 src->ymax = center.y+radius;
489 if((center.x|center.y|radius) == 0) src->xmax++; //make sure the bbox is not NULL anymore
492 if(center.x - radius < src->xmin)
493 src->xmin = center.x - radius;
494 if(center.x + radius > src->xmax)
495 src->xmax = center.x - radius;
496 if(center.y - radius < src->ymin)
497 src->ymin = center.y - radius;
498 if(center.y + radius > src->ymax)
499 src->ymax = center.y - radius;
501 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
504 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
505 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
508 SRECT swf_TurnRect(SRECT r, MATRIX* m)
511 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
512 p1.x = r.xmin;p1.y = r.ymin;
513 p2.x = r.xmax;p2.y = r.ymin;
514 p3.x = r.xmin;p3.y = r.ymax;
515 p4.x = r.xmax;p4.y = r.ymax;
516 pp1 = swf_TurnPoint(p1, m);
517 pp2 = swf_TurnPoint(p2, m);
518 pp3 = swf_TurnPoint(p3, m);
519 pp4 = swf_TurnPoint(p4, m);
520 g.xmin = g.xmax = pp1.x;
521 g.ymin = g.ymax = pp1.y;
522 swf_ExpandRect(&g, pp2);
523 swf_ExpandRect(&g, pp3);
524 swf_ExpandRect(&g, pp4);
529 int swf_GetMatrix(TAG * t,MATRIX * m)
536 { m->sx = m->sy = 0x10000;
542 swf_ResetReadBits(t);
544 if (swf_GetBits(t,1))
545 { nbits = swf_GetBits(t,5);
546 m->sx = swf_GetSBits(t,nbits);
547 m->sy = swf_GetSBits(t,nbits);
549 else m->sx = m->sy = 0x10000;
551 if (swf_GetBits(t,1))
552 { nbits = swf_GetBits(t,5);
553 m->r0 = swf_GetSBits(t,nbits);
554 m->r1 = swf_GetSBits(t,nbits);
556 else m->r0 = m->r1 = 0x0;
558 nbits = swf_GetBits(t,5);
559 m->tx = swf_GetSBits(t,nbits);
560 m->ty = swf_GetSBits(t,nbits);
565 int swf_SetMatrix(TAG * t,MATRIX * m)
571 ma.sx = ma.sy = 0x10000;
576 swf_ResetWriteBits(t);
578 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
580 { swf_SetBits(t,1,1);
581 nbits = swf_CountBits(m->sx,0);
582 nbits = swf_CountBits(m->sy,nbits);
584 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
587 swf_SetBits(t,nbits,5);
588 swf_SetBits(t,m->sx,nbits);
589 swf_SetBits(t,m->sy,nbits);
592 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
594 { swf_SetBits(t,1,1);
595 nbits = swf_CountBits(m->r0,0);
596 nbits = swf_CountBits(m->r1,nbits);
598 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
601 swf_SetBits(t,nbits,5);
602 swf_SetBits(t,m->r0,nbits);
603 swf_SetBits(t,m->r1,nbits);
606 nbits = swf_CountBits(m->tx,0);
607 nbits = swf_CountBits(m->ty,nbits);
609 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
612 swf_SetBits(t,nbits,5);
613 swf_SetBits(t,m->tx,nbits);
614 swf_SetBits(t,m->ty,nbits);
619 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha) //FIXME: alpha should be type bool
627 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
628 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
632 swf_ResetReadBits(t);
633 hasadd = swf_GetBits(t,1);
634 hasmul = swf_GetBits(t,1);
635 nbits = swf_GetBits(t,4);
638 { cx->r0 = (S16)swf_GetSBits(t,nbits);
639 cx->g0 = (S16)swf_GetSBits(t,nbits);
640 cx->b0 = (S16)swf_GetSBits(t,nbits);
642 cx->a0 = (S16)swf_GetSBits(t,nbits);
646 { cx->r1 = (S16)swf_GetSBits(t,nbits);
647 cx->g1 = (S16)swf_GetSBits(t,nbits);
648 cx->b1 = (S16)swf_GetSBits(t,nbits);
650 cx->a1 = (S16)swf_GetSBits(t,nbits);
656 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
664 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
665 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
675 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
676 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
679 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
680 nbits = swf_CountBits((S32)cx->r0,nbits);
681 nbits = swf_CountBits((S32)cx->g0,nbits);
682 nbits = swf_CountBits((S32)cx->b0,nbits);
686 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
687 nbits = swf_CountBits((S32)cx->r1,nbits);
688 nbits = swf_CountBits((S32)cx->g1,nbits);
689 nbits = swf_CountBits((S32)cx->b1,nbits);
692 swf_ResetWriteBits(t);
693 swf_SetBits(t,hasadd?1:0,1);
694 swf_SetBits(t,hasmul?1:0,1);
695 swf_SetBits(t,nbits,4);
698 { swf_SetBits(t,cx->r0,nbits);
699 swf_SetBits(t,cx->g0,nbits);
700 swf_SetBits(t,cx->b0,nbits);
701 if (alpha) swf_SetBits(t,cx->a0,nbits);
705 { swf_SetBits(t,cx->r1,nbits);
706 swf_SetBits(t,cx->g1,nbits);
707 swf_SetBits(t,cx->b1,nbits);
708 if (alpha) swf_SetBits(t,cx->a1,nbits);
714 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
715 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
717 void swf_SetPassword(TAG * t, const char * password)
719 /* WARNING: crypt_md5 is not reentrant */
723 #if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) && defined(HAVE_TIME_H) && defined(HAVE_TIME)
725 salt[0] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
726 salt[1] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
730 fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
731 fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
734 md5string = crypt_md5(password, salt);
737 swf_SetString(t, md5string);
740 int swf_VerifyPassword(TAG * t, const char * password)
742 char*md5string1, *md5string2;
747 if(t->len >= 5 && t->pos==0 &&
752 printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
755 md5string1 = swf_GetString(t);
757 if(strncmp(md5string1, "$1$",3 )) {
758 fprintf(stderr, "rfxswf: no salt in pw string\n");
761 x = strchr(md5string1+3, '$');
763 fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
766 n = x-(md5string1+3);
767 salt = (char*)malloc(n+1);
768 memcpy(salt, md5string1+3, n);
771 md5string2 = crypt_md5(password, salt);
773 if(strcmp(md5string1, md5string2) != 0)
778 // Tag List Manipulating Functions
780 TAG * swf_InsertTag(TAG * after,U16 id)
783 t = (TAG *)malloc(sizeof(TAG));
785 { memset(t,0x00,sizeof(TAG));
791 t->next = after->next;
793 if (t->next) t->next->prev = t;
799 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
802 t = (TAG *)malloc(sizeof(TAG));
804 { memset(t,0x00,sizeof(TAG));
810 t->prev = before->prev;
812 if (t->prev) t->prev->next = t;
815 if(swf && swf->firstTag == before) {
821 void swf_ClearTag(TAG * t)
823 if (t->data) free(t->data);
832 void swf_ResetTag(TAG*tag, U16 id)
834 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
838 int swf_DeleteTag(TAG * t)
841 if (t->prev) t->prev->next = t->next;
842 if (t->next) t->next->prev = t->prev;
844 if (t->data) free(t->data);
849 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
855 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
863 if (reader->read(reader, &len, 4) != 4) return NULL;
867 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
868 // Sprite handling fix: Flatten sprite tree
870 t = (TAG *)malloc(sizeof(TAG));
875 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (2). (%d bytes)\n", sizeof(TAG));
880 memset(t,0x00,sizeof(TAG));
886 { t->data = (U8*)malloc(t->len);
890 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (3). (%d bytes)\n", t->len);
895 if (reader->read(reader, t->data, t->len) != t->len) return NULL;
907 int swf_DefineSprite_GetRealSize(TAG * t);
909 int swf_WriteTag2(struct writer_t*writer, TAG * t)
910 // returns tag length in bytes (incl. Header), -1 = Error
911 // writer = 0 -> no output
918 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
920 short_tag = len<0x3f&&(t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2);
924 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
925 if (writer->write(writer,raw,2)!=2)
928 fprintf(stderr,"WriteTag() failed: Short Header.\n");
935 raw[0] = SWAP16((t->id<<6)|0x3f);
936 if (writer->write(writer,raw,2)!=2)
939 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
945 if (writer->write(writer,&len,4)!=4)
948 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
955 { if (writer->write(writer,t->data,t->len)!=t->len)
958 fprintf(stderr,"WriteTag() failed: Data.\n");
964 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
968 return t->len+(short_tag?2:6);
971 int swf_WriteTag(int handle, TAG * t)
973 struct writer_t writer;
976 return swf_WriteTag2(0, t);
977 writer_init_filewriter(&writer, handle);
978 len = swf_WriteTag2(&writer, t);
979 writer.finish(&writer);
983 int swf_DefineSprite_GetRealSize(TAG * t)
984 // Sprite Handling: Helper function to pack DefineSprite-Tag
986 if(len>4) { // folded sprite
990 { t = swf_NextTag(t);
991 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
993 } while (t&&(t->id!=ST_END));
997 void swf_UnFoldSprite(TAG * t)
1002 U16 spriteid,spriteframes;
1004 if(t->id!=ST_DEFINESPRITE)
1006 if(t->len<=4) // not folded
1011 spriteid = swf_GetU16(t); //id
1012 spriteframes = swf_GetU16(t); //frames
1019 tmp = swf_GetU16(t);
1024 if(id == ST_DEFINESPRITE && len<=4)
1028 len = swf_GetU32(t);
1029 it = swf_InsertTag(next, id);
1034 { it->data = (U8*)malloc(it->len);
1035 it->memsize = it->len;
1036 swf_GetBlock(t, it->data, it->len);
1043 free(t->data); t->data = 0;
1044 t->memsize = t->len = t->pos = 0;
1046 swf_SetU16(t, spriteid);
1047 swf_SetU16(t, spriteframes);
1050 void swf_FoldSprite(TAG * t)
1053 U16 id,frames,tmpid;
1055 if(t->id!=ST_DEFINESPRITE)
1058 fprintf(stderr, "Error: Sprite has no ID!");
1062 /* sprite is already folded */
1069 t->len = t->pos = t->memsize = 0;
1074 t = swf_NextTag(sprtag);
1079 if(t->id==ST_SHOWFRAME) frames++;
1080 if(t->id == ST_DEFINESPRITE && t->len<=4)
1085 } while(t && level);
1087 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1089 swf_SetU16(sprtag, id);
1090 swf_SetU16(sprtag, frames);
1092 t = swf_NextTag(sprtag);
1097 if(t->len<0x3f&&t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2) {
1098 swf_SetU16(sprtag,t->len|(t->id<<6));
1100 swf_SetU16(sprtag,0x3f|(t->id<<6));
1101 swf_SetU32(sprtag,t->len);
1104 swf_SetBlock(sprtag,t->data, t->len);
1106 if(t->id == ST_DEFINESPRITE && t->len<=4)
1115 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1117 // sprtag->next = t;
1118 // t->prev = sprtag;
1121 int swf_IsFolded(TAG * t)
1123 return (t->id == ST_DEFINESPRITE && t->len>4);
1126 void swf_FoldAll(SWF*swf)
1128 TAG*tag = swf->firstTag;
1129 //swf_DumpSWF(stdout, swf);
1131 if(tag->id == ST_DEFINESPRITE) {
1132 swf_FoldSprite(tag);
1133 //swf_DumpSWF(stdout, swf);
1135 tag = swf_NextTag(tag);
1139 void swf_UnFoldAll(SWF*swf)
1141 TAG*tag = swf->firstTag;
1143 if(tag->id == ST_DEFINESPRITE)
1144 swf_UnFoldSprite(tag);
1149 void swf_OptimizeTagOrder(SWF*swf)
1156 /* at the moment, we don't actually do optimizing,
1157 only fixing of non-spec-conformant things like
1164 tag = swf->firstTag;
1167 if(tag->id == ST_DEFINESPRITE) {
1169 /* ??? all sprites are supposed to be unfolded */
1170 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1180 /* move non-sprite tags out of sprite */
1181 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1182 /* remove tag from current position */
1183 tag->prev->next = tag->next;
1185 tag->next->prev = tag->prev;
1187 /* insert before tag level0 */
1189 tag->prev = level0->prev;
1191 tag->prev->next = tag;
1195 if(tag->id == ST_END) {
1206 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1208 if (!swf) return -1;
1209 memset(swf,0x00,sizeof(SWF));
1211 { char b[32]; // read Header
1215 struct reader_t zreader;
1217 if ((len = reader->read(reader ,b,8))<8) return -1;
1219 if (b[0]!='F' && b[0]!='C') return -1;
1220 if (b[1]!='W') return -1;
1221 if (b[2]!='S') return -1;
1222 swf->fileVersion = b[3];
1223 swf->compressed = (b[0]=='C')?1:0;
1224 swf->fileSize = GET32(&b[4]);
1226 if(swf->compressed) {
1227 reader_init_zlibinflate(&zreader, reader);
1231 reader_GetRect(reader, &swf->movieSize);
1232 reader->read(reader, &swf->frameRate, 2);
1233 swf->frameRate = SWAP16(swf->frameRate);
1234 reader->read(reader, &swf->frameCount, 2);
1235 swf->frameCount = SWAP16(swf->frameCount);
1237 /* read tags and connect to list */
1239 while (t) t = swf_ReadTag(reader,t);
1240 swf->firstTag = t1.next;
1241 t1.next->prev = NULL;
1247 int swf_ReadSWF(int handle, SWF * swf)
1249 struct reader_t reader;
1250 reader_init_filereader(&reader, handle);
1251 return swf_ReadSWF2(&reader, swf);
1254 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1258 struct writer_t zwriter;
1261 int writer_lastpos = 0;
1264 if (!swf) return -1;
1265 if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1267 if(writer) writer_lastpos = writer->pos;
1269 // Insert REFLEX Tag
1271 #ifdef INSERT_RFX_TAG
1273 if (swf->firstTag && swf_NextTag(swf->firstTag))
1274 if (swf_GetTagID(swf_NextTag(swf->firstTag))!=ST_REFLEX)
1275 swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),"rfx",3);
1277 #endif // INSERT_RFX_TAG
1279 // Count Frames + File Size
1286 len += swf_WriteTag(-1,t);
1287 if(t->id == ST_DEFINESPRITE && !swf_IsFolded(t)) inSprite++;
1288 else if(t->id == ST_END && inSprite) inSprite--;
1289 else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1297 memset(&t1,0x00,sizeof(TAG));
1301 { // measure header file size
1304 memset(&t2,0x00,sizeof(TAG));
1307 swf_SetRect(&t2, &swf->movieSize);
1308 swf_SetU16(&t2, swf->frameRate);
1309 swf_SetU16(&t2, swf->frameCount);
1310 l = swf_GetTagLen(&t2)+8;
1312 if(swf->compressed == 8) {
1317 if(len) {// don't touch headers without tags
1318 swf->fileSize = fileSize;
1319 swf->frameCount = frameCount;
1322 if(swf->compressed != 8) {
1323 /* compressed flag set to 8 means "skip first 8
1324 header bytes". This is necessary if the caller wants to
1325 create compressed SWFs himself .
1326 It also means that we don't initialize our own zlib
1327 writer, but assume the caller provided one.
1329 if(swf->compressed) {
1331 writer->write(writer, id, 3);
1335 writer->write(writer, id, 3);
1338 writer->write(writer, &swf->fileVersion, 1);
1339 PUT32(b4, swf->fileSize);
1340 writer->write(writer, b4, 4);
1342 if(swf->compressed) {
1343 writer_init_zlibdeflate(&zwriter, writer);
1348 swf_SetRect(&t1,&swf->movieSize);
1349 swf_SetU16(&t1,swf->frameRate);
1350 swf_SetU16(&t1,swf->frameCount);
1352 ret = writer->write(writer,b,swf_GetTagLen(&t1));
1353 if (ret!=swf_GetTagLen(&t1))
1356 fprintf(stderr, "ret:%d\n",ret);
1358 fprintf(stderr,"WriteSWF() failed: Header.\n");
1365 { if (swf_WriteTag2(writer, t)<0) return -1;
1368 if(swf->compressed) {
1369 if(swf->compressed != 8) {
1370 zwriter.finish(&zwriter);
1371 return writer->pos - writer_lastpos;
1373 return (int)fileSize;
1375 return (int)fileSize;
1380 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1382 struct writer_t writer;
1384 swf->compressed = 0;
1387 writer_init_nullwriter(&writer);
1388 len = swf_WriteSWF2(&writer, swf);
1390 writer_init_filewriter(&writer, handle);
1391 len = swf_WriteSWF2(&writer, swf);
1392 writer.finish(&writer);
1396 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1398 struct writer_t writer;
1400 swf->compressed = 1;
1403 writer_init_nullwriter(&writer);
1404 len = swf_WriteSWF2(&writer, swf);
1406 writer_init_filewriter(&writer, handle);
1407 len = swf_WriteSWF2(&writer, swf);
1408 writer.finish(&writer);
1412 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1415 memcpy(&myswf,swf,sizeof(SWF));
1417 return swf_WriteSWF2(writer, &myswf);
1420 int swf_WriteHeader(int handle,SWF * swf)
1423 memcpy(&myswf,swf,sizeof(SWF));
1425 return swf_WriteSWF(handle, &myswf);
1428 int swf_WriteCGI(SWF * swf)
1432 len = swf_WriteSWF(-1,swf);
1434 if (len<0) return -1;
1436 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1437 "Accept-Ranges: bytes\n"
1438 "Content-Length: %lu\n"
1439 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1442 write(fileno(stdout),s,strlen(s));
1443 return swf_WriteSWF(fileno(stdout),swf);
1446 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1447 { TAG * t = swf->firstTag;
1450 { TAG * tnew = t->next;
1451 if (t->data) free(t->data);
1458 // include advanced functions
1460 #include "modules/swfdump.c"
1461 #include "modules/swfshape.c"
1462 #include "modules/swftext.c"
1463 #include "modules/swffont.c"
1464 #include "modules/swfobject.c"
1465 #include "modules/swfbutton.c"
1466 #include "modules/swftools.c"
1467 #include "modules/swfcgi.c"
1468 #include "modules/swfbits.c"
1469 #include "modules/swfaction.c"
1470 #include "modules/swfsound.c"
1471 #include "modules/swfdraw.c"