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 */
33 #endif // HAVE_JPEGLIB
39 #ifndef RFXSWF_DISABLESOUND
41 #include "lame/lame.h"
54 #define MALLOC_SIZE 128
55 #define INSERT_RFX_TAG
57 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
59 // inline wrapper functions
61 TAG * swf_NextTag(TAG * t) { return t->next; }
62 TAG * swf_PrevTag(TAG * t) { return t->prev; }
63 U16 swf_GetTagID(TAG * t) { return t->id; }
64 U32 swf_GetTagLen(TAG * t) { return t->len; }
65 U8* swf_GetTagLenPtr(TAG * t) { return &(t->data[t->len]); }
66 U32 swf_GetTagPos(TAG * t) { return t->pos; }
68 // for future purpose: avoid high level lib functions to change tagpos/bitpos
70 #define swf_SaveTagPos(tag)
71 #define swf_RestoreTagPos(tag)
73 void swf_SetTagPos(TAG * t,U32 pos)
74 { swf_ResetReadBits(t);
75 if (pos<=t->len) t->pos = pos;
78 fprintf(stderr,"SetTagPos(%d) out of bounds: TagID = %i\n",pos, t->id);
83 char* swf_GetString(TAG*t)
85 char* str = ((char*)(&(t)->data[(t)->pos]));
91 { swf_ResetReadBits(t);
94 { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
98 return t->data[t->pos++];
101 U16 swf_GetU16(TAG * t)
103 swf_ResetReadBits(t);
105 if (t->pos>(t->len-2))
106 { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
110 res = t->data[t->pos] | (t->data[t->pos+1]<<8);
115 U32 swf_GetU32(TAG * t)
117 swf_ResetReadBits(t);
119 if (t->pos>(t->len-4))
120 { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
124 res = t->data[t->pos] | (t->data[t->pos+1]<<8) |
125 (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
130 int swf_GetBlock(TAG * t,U8 * b,int l)
131 // returns number of bytes written (<=l)
132 // b = NULL -> skip data
133 { swf_ResetReadBits(t);
134 if ((t->len-t->pos)<l) l=t->len-t->pos;
135 if (b && l) memcpy(b,&t->data[t->pos],l);
140 int swf_SetBlock(TAG * t,U8 * b,int l)
141 // Appends Block to the end of Tagdata, returns size
142 { U32 newlen = t->len + l;
143 swf_ResetWriteBits(t);
144 if (newlen>t->memsize)
145 { U32 newmem = MEMSIZE(newlen);
146 U8 * newdata = (U8*)(rfx_realloc(t->data,newmem));
150 if (b) memcpy(&t->data[t->len],b,l);
151 else memset(&t->data[t->len],0x00,l);
156 int swf_SetU8(TAG * t,U8 v)
157 { swf_ResetWriteBits(t);
158 if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
159 t->data[t->len++] = v;
163 int swf_SetU16(TAG * t,U16 v)
168 swf_ResetWriteBits(t);
169 if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
170 t->data[t->len++] = a[0];
171 t->data[t->len++] = a[1];
174 void swf_SetS16(TAG * t,int v)
176 if(v>32767 || v<-32768) {
177 fprintf(stderr, "Warning: S16 overflow: %d\n", v);
179 swf_SetU16(t, (S16)v);
182 int swf_SetU32(TAG * t,U32 v)
184 a[0] = v&0xff; // to ensure correct handling of non-intel byteorder
189 swf_ResetWriteBits(t);
190 if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
191 t->data[t->len++] = a[0];
192 t->data[t->len++] = a[1];
193 t->data[t->len++] = a[2];
194 t->data[t->len++] = a[3];
198 U32 swf_GetBits(TAG * t,int nbits)
200 if (!nbits) return 0;
201 if (!t->readBit) t->readBit = 0x80;
204 if (t->data[t->pos]&t->readBit) res|=1;
208 { if (nbits) t->readBit = 0x80;
211 { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
221 S32 swf_GetSBits(TAG * t,int nbits)
222 { U32 res = swf_GetBits(t,nbits);
223 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
227 U32 reader_GetBits(struct reader_t*reader, int nbits)
228 { return reader_readbits(reader, nbits);
230 S32 reader_GetSBits(struct reader_t*reader, int nbits)
231 { U32 res = reader_readbits(reader, nbits);
232 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
236 int swf_SetBits(TAG * t,U32 v,int nbits)
237 { U32 bm = 1<<(nbits-1);
241 { if (FAILED(swf_SetU8(t,0))) return -1;
244 if (v&bm) t->data[t->len-1] |= t->writeBit;
252 // Advanced Data Access Functions
254 int swf_SetRGB(TAG * t,RGBA * col)
257 { swf_SetU8(t,col->r);
260 } else swf_SetBlock(t,NULL,3);
263 void swf_GetRGB(TAG * t, RGBA * col)
268 col->r = swf_GetU8(t);
269 col->g = swf_GetU8(t);
270 col->b = swf_GetU8(t);
274 int swf_SetRGBA(TAG * t,RGBA * col)
277 { swf_SetU8(t,col->r);
281 } else swf_SetBlock(t,NULL,4);
284 void swf_GetRGBA(TAG * t, RGBA * col)
289 col->r = swf_GetU8(t);
290 col->g = swf_GetU8(t);
291 col->b = swf_GetU8(t);
292 col->a = swf_GetU8(t);
295 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
300 memset(gradient, 0, sizeof(GRADIENT));
305 gradient->num = swf_GetU8(tag);
306 for(t=0;t<gradient->num;t++)
311 gradient->ratios[t] = swf_GetU8(tag);
313 swf_GetRGB(tag, &gradient->rgba[t]);
315 swf_GetRGBA(tag, &gradient->rgba[t]);
319 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
323 memset(gradient, 0, sizeof(GRADIENT));
326 swf_SetU8(tag, gradient->num);
327 for(t=0; t<8 && t<gradient->num; t++)
329 swf_SetU8(tag, gradient->ratios[t]);
331 swf_SetRGB(tag, &gradient->rgba[t]);
333 swf_SetRGBA(tag, &gradient->rgba[t]);
337 int swf_CountUBits(U32 v,int nbits)
340 if(v == 0x00000000) n = 0;
346 return (n>nbits)?n:nbits;
349 int swf_CountBits(U32 v,int nbits)
353 { if(v == 0xffffffff) n = 1;
361 { if(v == 0x00000000) n = 0;
368 return (n>nbits)?n:nbits;
371 int swf_GetRect(TAG * t,SRECT * r)
374 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
376 nbits = (int) swf_GetBits(t,5);
377 r->xmin = swf_GetSBits(t,nbits);
378 r->xmax = swf_GetSBits(t,nbits);
379 r->ymin = swf_GetSBits(t,nbits);
380 r->ymax = swf_GetSBits(t,nbits);
384 int reader_GetRect(struct reader_t*reader,SRECT * r)
388 nbits = (int) reader_GetBits(reader,5);
389 r->xmin = reader_GetSBits(reader,nbits);
390 r->xmax = reader_GetSBits(reader,nbits);
391 r->ymin = reader_GetSBits(reader,nbits);
392 r->ymax = reader_GetSBits(reader,nbits);
396 int swf_SetRect(TAG * t,SRECT * r)
399 nbits = swf_CountBits(r->xmin,0);
400 nbits = swf_CountBits(r->xmax,nbits);
401 nbits = swf_CountBits(r->ymin,nbits);
402 nbits = swf_CountBits(r->ymax,nbits);
404 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
408 swf_SetBits(t,nbits,5);
409 swf_SetBits(t,r->xmin,nbits);
410 swf_SetBits(t,r->xmax,nbits);
411 swf_SetBits(t,r->ymin,nbits);
412 swf_SetBits(t,r->ymax,nbits);
417 void swf_ExpandRect(SRECT*src, SPOINT add)
419 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
424 if((add.x|add.y) == 0) src->xmax++; //make sure the bbox is not NULL anymore
427 if(add.x < src->xmin)
429 if(add.x > src->xmax)
431 if(add.y < src->ymin)
433 if(add.y > src->ymax)
436 void swf_ExpandRect2(SRECT*src, SRECT*add)
438 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
440 if((src->xmin | src->ymin | src->xmax | src->ymax)==0)
442 if(add->xmin < src->xmin)
443 src->xmin = add->xmin;
444 if(add->ymin < src->ymin)
445 src->ymin = add->ymin;
446 if(add->xmax > src->xmax)
447 src->xmax = add->xmax;
448 if(add->ymax > src->ymax)
449 src->ymax = add->ymax;
451 void swf_ExpandRect3(SRECT*src, SPOINT center, int radius)
453 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
454 src->xmin = center.x-radius;
455 src->ymin = center.y-radius;
456 src->xmax = center.x+radius;
457 src->ymax = center.y+radius;
458 if((center.x|center.y|radius) == 0) src->xmax++; //make sure the bbox is not NULL anymore
461 if(center.x - radius < src->xmin)
462 src->xmin = center.x - radius;
463 if(center.x + radius > src->xmax)
464 src->xmax = center.x + radius;
465 if(center.y - radius < src->ymin)
466 src->ymin = center.y - radius;
467 if(center.y + radius > src->ymax)
468 src->ymax = center.y + radius;
470 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
473 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
474 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
477 SRECT swf_TurnRect(SRECT r, MATRIX* m)
480 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
483 p1.x = r.xmin;p1.y = r.ymin;
484 p2.x = r.xmax;p2.y = r.ymin;
485 p3.x = r.xmin;p3.y = r.ymax;
486 p4.x = r.xmax;p4.y = r.ymax;
487 pp1 = swf_TurnPoint(p1, m);
488 pp2 = swf_TurnPoint(p2, m);
489 pp3 = swf_TurnPoint(p3, m);
490 pp4 = swf_TurnPoint(p4, m);
491 g.xmin = g.xmax = pp1.x;
492 g.ymin = g.ymax = pp1.y;
493 swf_ExpandRect(&g, pp2);
494 swf_ExpandRect(&g, pp3);
495 swf_ExpandRect(&g, pp4);
500 int swf_GetMatrix(TAG * t,MATRIX * m)
507 { m->sx = m->sy = 0x10000;
513 swf_ResetReadBits(t);
515 if (swf_GetBits(t,1))
516 { nbits = swf_GetBits(t,5);
517 m->sx = swf_GetSBits(t,nbits);
518 m->sy = swf_GetSBits(t,nbits);
520 else m->sx = m->sy = 0x10000;
522 if (swf_GetBits(t,1))
523 { nbits = swf_GetBits(t,5);
524 m->r0 = swf_GetSBits(t,nbits);
525 m->r1 = swf_GetSBits(t,nbits);
527 else m->r0 = m->r1 = 0x0;
529 nbits = swf_GetBits(t,5);
530 m->tx = swf_GetSBits(t,nbits);
531 m->ty = swf_GetSBits(t,nbits);
536 int swf_SetMatrix(TAG * t,MATRIX * m)
542 ma.sx = ma.sy = 0x10000;
547 swf_ResetWriteBits(t);
549 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
551 { swf_SetBits(t,1,1);
552 nbits = swf_CountBits(m->sx,0);
553 nbits = swf_CountBits(m->sy,nbits);
555 /* TODO: happens on AMD64 systems for normal values? */
556 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
559 swf_SetBits(t,nbits,5);
560 swf_SetBits(t,m->sx,nbits);
561 swf_SetBits(t,m->sy,nbits);
564 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
566 { swf_SetBits(t,1,1);
567 nbits = swf_CountBits(m->r0,0);
568 nbits = swf_CountBits(m->r1,nbits);
570 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
573 swf_SetBits(t,nbits,5);
574 swf_SetBits(t,m->r0,nbits);
575 swf_SetBits(t,m->r1,nbits);
578 nbits = swf_CountBits(m->tx,0);
579 nbits = swf_CountBits(m->ty,nbits);
581 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
584 swf_SetBits(t,nbits,5);
585 swf_SetBits(t,m->tx,nbits);
586 swf_SetBits(t,m->ty,nbits);
591 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha)
599 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
600 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
604 swf_ResetReadBits(t);
605 hasadd = swf_GetBits(t,1);
606 hasmul = swf_GetBits(t,1);
607 nbits = swf_GetBits(t,4);
610 { cx->r0 = (S16)swf_GetSBits(t,nbits);
611 cx->g0 = (S16)swf_GetSBits(t,nbits);
612 cx->b0 = (S16)swf_GetSBits(t,nbits);
614 cx->a0 = (S16)swf_GetSBits(t,nbits);
618 { cx->r1 = (S16)swf_GetSBits(t,nbits);
619 cx->g1 = (S16)swf_GetSBits(t,nbits);
620 cx->b1 = (S16)swf_GetSBits(t,nbits);
622 cx->a1 = (S16)swf_GetSBits(t,nbits);
628 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
636 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
637 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
647 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
648 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
651 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
652 nbits = swf_CountBits((S32)cx->r0,nbits);
653 nbits = swf_CountBits((S32)cx->g0,nbits);
654 nbits = swf_CountBits((S32)cx->b0,nbits);
658 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
659 nbits = swf_CountBits((S32)cx->r1,nbits);
660 nbits = swf_CountBits((S32)cx->g1,nbits);
661 nbits = swf_CountBits((S32)cx->b1,nbits);
664 swf_ResetWriteBits(t);
665 swf_SetBits(t,hasadd?1:0,1);
666 swf_SetBits(t,hasmul?1:0,1);
667 swf_SetBits(t,nbits,4);
670 { swf_SetBits(t,cx->r0,nbits);
671 swf_SetBits(t,cx->g0,nbits);
672 swf_SetBits(t,cx->b0,nbits);
673 if (alpha) swf_SetBits(t,cx->a0,nbits);
677 { swf_SetBits(t,cx->r1,nbits);
678 swf_SetBits(t,cx->g1,nbits);
679 swf_SetBits(t,cx->b1,nbits);
680 if (alpha) swf_SetBits(t,cx->a1,nbits);
686 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
687 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
689 void swf_SetPassword(TAG * t, const char * password)
691 /* WARNING: crypt_md5 is not reentrant */
695 #if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) && defined(HAVE_TIME_H) && defined(HAVE_TIME)
697 salt[0] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
698 salt[1] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
702 fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
703 fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
707 md5string = crypt_md5(password, salt);
710 swf_SetString(t, md5string);
713 int swf_VerifyPassword(TAG * t, const char * password)
715 char*md5string1, *md5string2;
720 if(t->len >= 5 && t->pos==0 &&
725 printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
728 md5string1 = swf_GetString(t);
730 if(strncmp(md5string1, "$1$",3 )) {
731 fprintf(stderr, "rfxswf: no salt in pw string\n");
734 x = strchr(md5string1+3, '$');
736 fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
739 n = x-(md5string1+3);
740 salt = (char*)rfx_alloc(n+1);
741 memcpy(salt, md5string1+3, n);
744 md5string2 = crypt_md5(password, salt);
746 if(strcmp(md5string1, md5string2) != 0)
751 // Tag List Manipulating Functions
753 TAG * swf_InsertTag(TAG * after,U16 id)
756 t = (TAG *)rfx_calloc(sizeof(TAG));
762 t->next = after->next;
764 if (t->next) t->next->prev = t;
769 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
772 t = (TAG *)rfx_calloc(sizeof(TAG));
778 t->prev = before->prev;
780 if (t->prev) t->prev->next = t;
782 if(swf && swf->firstTag == before) {
788 void swf_ClearTag(TAG * t)
790 if (t->data) rfx_free(t->data);
799 void swf_ResetTag(TAG*tag, U16 id)
801 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
805 TAG* swf_CopyTag(TAG*tag, TAG*to_copy)
807 tag = swf_InsertTag(tag, to_copy->id);
808 swf_SetBlock(tag, to_copy->data, to_copy->len);
812 int swf_DeleteTag(TAG * t)
815 if (t->prev) t->prev->next = t->next;
816 if (t->next) t->next->prev = t->prev;
818 if (t->data) rfx_free(t->data);
823 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
829 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
837 if (reader->read(reader, &len, 4) != 4) return NULL;
841 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
842 // Sprite handling fix: Flatten sprite tree
844 t = (TAG *)rfx_calloc(sizeof(TAG));
850 { t->data = (U8*)rfx_alloc(t->len);
852 if (reader->read(reader, t->data, t->len) != t->len) {
853 fprintf(stderr, "rfxswf: Warning: Short read (tagid %d). File truncated?\n", t->id);
854 free(t->data);t->data=0;
869 int swf_DefineSprite_GetRealSize(TAG * t);
871 int swf_WriteTag2(struct writer_t*writer, TAG * t)
872 // returns tag length in bytes (incl. Header), -1 = Error
873 // writer = 0 -> no output
880 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
882 short_tag = len<0x3f&&(t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2);
886 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
887 if (writer->write(writer,raw,2)!=2)
890 fprintf(stderr,"WriteTag() failed: Short Header.\n");
897 raw[0] = SWAP16((t->id<<6)|0x3f);
898 if (writer->write(writer,raw,2)!=2)
901 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
907 if (writer->write(writer,&len,4)!=4)
910 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
917 { if (writer->write(writer,t->data,t->len)!=t->len)
920 fprintf(stderr,"WriteTag() failed: Data.\n");
926 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
930 return t->len+(short_tag?2:6);
933 int swf_WriteTag(int handle, TAG * t)
935 struct writer_t writer;
938 return swf_WriteTag2(0, t);
939 writer_init_filewriter(&writer, handle);
940 len = swf_WriteTag2(&writer, t);
941 writer.finish(&writer);
945 int swf_DefineSprite_GetRealSize(TAG * t)
946 // Sprite Handling: Helper function to pack DefineSprite-Tag
948 if(len>4) { // folded sprite
952 { t = swf_NextTag(t);
953 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
955 } while (t&&(t->id!=ST_END));
959 void swf_UnFoldSprite(TAG * t)
964 U16 spriteid,spriteframes;
966 if(t->id!=ST_DEFINESPRITE)
968 if(t->len<=4) // not folded
973 spriteid = swf_GetU16(t); //id
974 spriteframes = swf_GetU16(t); //frames
986 if(id == ST_DEFINESPRITE && len<=4)
991 it = swf_InsertTag(next, id);
996 { it->data = (U8*)rfx_alloc(it->len);
997 it->memsize = it->len;
998 swf_GetBlock(t, it->data, it->len);
1005 rfx_free(t->data); t->data = 0;
1006 t->memsize = t->len = t->pos = 0;
1008 swf_SetU16(t, spriteid);
1009 swf_SetU16(t, spriteframes);
1012 void swf_FoldSprite(TAG * t)
1015 U16 id,frames,tmpid;
1017 if(t->id!=ST_DEFINESPRITE)
1020 fprintf(stderr, "Error: Sprite has no ID!");
1024 /* sprite is already folded */
1031 t->len = t->pos = t->memsize = 0;
1036 t = swf_NextTag(sprtag);
1041 if(t->id==ST_SHOWFRAME) frames++;
1042 if(t->id == ST_DEFINESPRITE && t->len<=4)
1047 } while(t && level);
1049 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1051 swf_SetU16(sprtag, id);
1052 swf_SetU16(sprtag, frames);
1054 t = swf_NextTag(sprtag);
1059 if(t->len<0x3f&&t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2) {
1060 swf_SetU16(sprtag,t->len|(t->id<<6));
1062 swf_SetU16(sprtag,0x3f|(t->id<<6));
1063 swf_SetU32(sprtag,t->len);
1066 swf_SetBlock(sprtag,t->data, t->len);
1068 if(t->id == ST_DEFINESPRITE && t->len<=4)
1077 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1079 // sprtag->next = t;
1080 // t->prev = sprtag;
1083 int swf_IsFolded(TAG * t)
1085 return (t->id == ST_DEFINESPRITE && t->len>4);
1088 void swf_FoldAll(SWF*swf)
1090 TAG*tag = swf->firstTag;
1091 //swf_DumpSWF(stdout, swf);
1093 if(tag->id == ST_DEFINESPRITE) {
1094 swf_FoldSprite(tag);
1095 //swf_DumpSWF(stdout, swf);
1097 tag = swf_NextTag(tag);
1101 void swf_UnFoldAll(SWF*swf)
1103 TAG*tag = swf->firstTag;
1105 if(tag->id == ST_DEFINESPRITE)
1106 swf_UnFoldSprite(tag);
1111 void swf_OptimizeTagOrder(SWF*swf)
1118 /* at the moment, we don't actually do optimizing,
1119 only fixing of non-spec-conformant things like
1126 tag = swf->firstTag;
1129 if(tag->id == ST_DEFINESPRITE) {
1131 /* ??? all sprites are supposed to be unfolded */
1132 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1142 /* move non-sprite tags out of sprite */
1143 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1144 /* remove tag from current position */
1145 tag->prev->next = tag->next;
1147 tag->next->prev = tag->prev;
1149 /* insert before tag level0 */
1151 tag->prev = level0->prev;
1154 tag->prev->next = tag;
1156 swf->firstTag = tag;
1160 if(tag->id == ST_END) {
1171 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1173 if (!swf) return -1;
1174 memset(swf,0x00,sizeof(SWF));
1176 { char b[32]; // read Header
1180 struct reader_t zreader;
1182 if ((len = reader->read(reader ,b,8))<8) return -1;
1184 if (b[0]!='F' && b[0]!='C') return -1;
1185 if (b[1]!='W') return -1;
1186 if (b[2]!='S') return -1;
1187 swf->fileVersion = b[3];
1188 swf->compressed = (b[0]=='C')?1:0;
1189 swf->fileSize = GET32(&b[4]);
1191 if(swf->compressed) {
1192 reader_init_zlibinflate(&zreader, reader);
1196 reader_GetRect(reader, &swf->movieSize);
1197 reader->read(reader, &swf->frameRate, 2);
1198 swf->frameRate = SWAP16(swf->frameRate);
1199 reader->read(reader, &swf->frameCount, 2);
1200 swf->frameCount = SWAP16(swf->frameCount);
1202 /* read tags and connect to list */
1204 while (t) t = swf_ReadTag(reader,t);
1205 swf->firstTag = t1.next;
1206 t1.next->prev = NULL;
1212 int swf_ReadSWF(int handle, SWF * swf)
1214 struct reader_t reader;
1215 reader_init_filereader(&reader, handle);
1216 return swf_ReadSWF2(&reader, swf);
1219 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1223 struct writer_t zwriter;
1226 int writer_lastpos = 0;
1229 if (!swf) return -1;
1230 if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1232 if(writer) writer_lastpos = writer->pos;
1234 // Insert REFLEX Tag
1236 #ifdef INSERT_RFX_TAG
1238 if ((swf->firstTag && swf->firstTag->id != ST_REFLEX) &&
1239 (!swf->firstTag->next || swf->firstTag->next->id != ST_REFLEX))
1241 swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),"rfx",3);
1244 #endif // INSERT_RFX_TAG
1246 // Count Frames + File Size
1253 len += swf_WriteTag(-1,t);
1254 if(t->id == ST_DEFINESPRITE && !swf_IsFolded(t)) inSprite++;
1255 else if(t->id == ST_END && inSprite) inSprite--;
1256 else if(t->id == ST_END && !inSprite) {
1257 if(t->prev && t->prev->id!=ST_SHOWFRAME)
1260 else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1268 memset(&t1,0x00,sizeof(TAG));
1272 { // measure header file size
1275 memset(&t2,0x00,sizeof(TAG));
1278 swf_SetRect(&t2, &swf->movieSize);
1279 swf_SetU16(&t2, swf->frameRate);
1280 swf_SetU16(&t2, swf->frameCount);
1281 l = swf_GetTagLen(&t2)+8;
1283 if(swf->compressed == 8) {
1288 if(len) {// don't touch headers without tags
1289 swf->fileSize = fileSize;
1290 swf->frameCount = frameCount;
1293 if(swf->compressed != 8) {
1294 /* compressed flag set to 8 means "skip first 8
1295 header bytes". This is necessary if the caller wants to
1296 create compressed SWFs himself .
1297 It also means that we don't initialize our own zlib
1298 writer, but assume the caller provided one.
1300 if(swf->compressed) {
1302 writer->write(writer, id, 3);
1306 writer->write(writer, id, 3);
1309 writer->write(writer, &swf->fileVersion, 1);
1310 PUT32(b4, swf->fileSize);
1311 writer->write(writer, b4, 4);
1313 if(swf->compressed) {
1314 writer_init_zlibdeflate(&zwriter, writer);
1319 swf_SetRect(&t1,&swf->movieSize);
1320 swf_SetU16(&t1,swf->frameRate);
1321 swf_SetU16(&t1,swf->frameCount);
1323 ret = writer->write(writer,b,swf_GetTagLen(&t1));
1324 if (ret!=swf_GetTagLen(&t1))
1327 fprintf(stderr, "ret:%d\n",ret);
1329 fprintf(stderr,"WriteSWF() failed: Header.\n");
1336 { if (swf_WriteTag2(writer, t)<0) return -1;
1339 if(swf->compressed) {
1340 if(swf->compressed != 8) {
1341 zwriter.finish(&zwriter);
1342 return writer->pos - writer_lastpos;
1344 return (int)fileSize;
1346 return (int)fileSize;
1351 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1353 struct writer_t writer;
1355 swf->compressed = 0;
1358 writer_init_nullwriter(&writer);
1359 len = swf_WriteSWF2(&writer, swf);
1362 writer_init_filewriter(&writer, handle);
1363 len = swf_WriteSWF2(&writer, swf);
1364 writer.finish(&writer);
1368 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1370 struct writer_t writer;
1372 swf->compressed = 1;
1375 writer_init_nullwriter(&writer);
1376 len = swf_WriteSWF2(&writer, swf);
1378 writer_init_filewriter(&writer, handle);
1379 len = swf_WriteSWF2(&writer, swf);
1380 writer.finish(&writer);
1384 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1387 memcpy(&myswf,swf,sizeof(SWF));
1389 return swf_WriteSWF2(writer, &myswf);
1392 int swf_WriteHeader(int handle,SWF * swf)
1395 memcpy(&myswf,swf,sizeof(SWF));
1397 return swf_WriteSWF(handle, &myswf);
1400 int swf_WriteCGI(SWF * swf)
1404 len = swf_WriteSWF(-1,swf);
1406 if (len<0) return -1;
1408 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1409 "Accept-Ranges: bytes\n"
1410 "Content-Length: %lu\n"
1411 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1414 write(fileno(stdout),s,strlen(s));
1415 return swf_WriteSWF(fileno(stdout),swf);
1418 SWF* swf_CopySWF(SWF*swf)
1420 SWF*nswf = rfx_alloc(sizeof(SWF));
1422 memcpy(nswf, swf, sizeof(SWF));
1424 tag = swf->firstTag;
1427 ntag = swf_CopyTag(ntag, tag);
1429 nswf->firstTag = ntag;
1435 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1436 { TAG * t = swf->firstTag;
1439 { TAG * tnew = t->next;
1440 if (t->data) rfx_free(t->data);
1447 // include advanced functions
1449 #include "modules/swfdump.c"
1450 #include "modules/swfshape.c"
1451 #include "modules/swftext.c"
1452 #include "modules/swffont.c"
1453 #include "modules/swfobject.c"
1454 #include "modules/swfbutton.c"
1455 #include "modules/swftools.c"
1456 #include "modules/swfcgi.c"
1457 #include "modules/swfbits.c"
1458 #include "modules/swfaction.c"
1459 #include "modules/swfsound.c"
1460 #include "modules/swfdraw.c"
1461 #include "modules/swfrender.c"