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]));
86 while(t->pos < t->len && swf_GetU8(t));
87 /* make sure we always have a trailing zero byte */
88 if(t->pos == t->len) {
89 if(t->len == t->memsize) {
90 swf_ResetWriteBits(t);
100 { swf_ResetReadBits(t);
103 { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
107 return t->data[t->pos++];
110 U16 swf_GetU16(TAG * t)
112 swf_ResetReadBits(t);
114 if (t->pos>(t->len-2))
115 { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
119 res = t->data[t->pos] | (t->data[t->pos+1]<<8);
124 U32 swf_GetU32(TAG * t)
126 swf_ResetReadBits(t);
128 if (t->pos>(t->len-4))
129 { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
133 res = t->data[t->pos] | (t->data[t->pos+1]<<8) |
134 (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
139 int swf_GetBlock(TAG * t,U8 * b,int l)
140 // returns number of bytes written (<=l)
141 // b = NULL -> skip data
142 { swf_ResetReadBits(t);
143 if ((t->len-t->pos)<l) l=t->len-t->pos;
144 if (b && l) memcpy(b,&t->data[t->pos],l);
149 int swf_SetBlock(TAG * t,U8 * b,int l)
150 // Appends Block to the end of Tagdata, returns size
151 { U32 newlen = t->len + l;
152 swf_ResetWriteBits(t);
153 if (newlen>t->memsize)
154 { U32 newmem = MEMSIZE(newlen);
155 U8 * newdata = (U8*)(rfx_realloc(t->data,newmem));
159 if (b) memcpy(&t->data[t->len],b,l);
160 else memset(&t->data[t->len],0x00,l);
165 int swf_SetU8(TAG * t,U8 v)
166 { swf_ResetWriteBits(t);
167 if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
168 t->data[t->len++] = v;
172 int swf_SetU16(TAG * t,U16 v)
177 swf_ResetWriteBits(t);
178 if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
179 t->data[t->len++] = a[0];
180 t->data[t->len++] = a[1];
183 void swf_SetS16(TAG * t,int v)
185 if(v>32767 || v<-32768) {
186 fprintf(stderr, "Warning: S16 overflow: %d\n", v);
188 swf_SetU16(t, (S16)v);
191 int swf_SetU32(TAG * t,U32 v)
193 a[0] = v&0xff; // to ensure correct handling of non-intel byteorder
198 swf_ResetWriteBits(t);
199 if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
200 t->data[t->len++] = a[0];
201 t->data[t->len++] = a[1];
202 t->data[t->len++] = a[2];
203 t->data[t->len++] = a[3];
207 U32 swf_GetBits(TAG * t,int nbits)
209 if (!nbits) return 0;
210 if (!t->readBit) t->readBit = 0x80;
213 if (t->data[t->pos]&t->readBit) res|=1;
217 { if (nbits) t->readBit = 0x80;
220 { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
230 S32 swf_GetSBits(TAG * t,int nbits)
231 { U32 res = swf_GetBits(t,nbits);
232 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
236 U32 reader_GetBits(reader_t*reader, int nbits)
237 { return reader_readbits(reader, nbits);
239 S32 reader_GetSBits(reader_t*reader, int nbits)
240 { U32 res = reader_readbits(reader, nbits);
241 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
245 int swf_SetBits(TAG * t,U32 v,int nbits)
246 { U32 bm = 1<<(nbits-1);
250 { if (FAILED(swf_SetU8(t,0))) return -1;
253 if (v&bm) t->data[t->len-1] |= t->writeBit;
261 // Advanced Data Access Functions
263 int swf_SetRGB(TAG * t,RGBA * col)
266 { swf_SetU8(t,col->r);
269 } else swf_SetBlock(t,NULL,3);
272 void swf_GetRGB(TAG * t, RGBA * col)
277 col->r = swf_GetU8(t);
278 col->g = swf_GetU8(t);
279 col->b = swf_GetU8(t);
283 int swf_SetRGBA(TAG * t,RGBA * col)
286 { swf_SetU8(t,col->r);
290 } else swf_SetBlock(t,NULL,4);
293 void swf_GetRGBA(TAG * t, RGBA * col)
298 col->r = swf_GetU8(t);
299 col->g = swf_GetU8(t);
300 col->b = swf_GetU8(t);
301 col->a = swf_GetU8(t);
304 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
309 memset(gradient, 0, sizeof(GRADIENT));
314 gradient->num = swf_GetU8(tag);
315 for(t=0;t<gradient->num;t++)
320 gradient->ratios[t] = swf_GetU8(tag);
322 swf_GetRGB(tag, &gradient->rgba[t]);
324 swf_GetRGBA(tag, &gradient->rgba[t]);
328 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
332 memset(gradient, 0, sizeof(GRADIENT));
335 swf_SetU8(tag, gradient->num);
336 for(t=0; t<8 && t<gradient->num; t++)
338 swf_SetU8(tag, gradient->ratios[t]);
340 swf_SetRGB(tag, &gradient->rgba[t]);
342 swf_SetRGBA(tag, &gradient->rgba[t]);
346 int swf_CountUBits(U32 v,int nbits)
349 if(v == 0x00000000) n = 0;
355 return (n>nbits)?n:nbits;
358 int swf_CountBits(U32 v,int nbits)
362 { if(v == 0xffffffff) n = 1;
370 { if(v == 0x00000000) n = 0;
377 return (n>nbits)?n:nbits;
380 int swf_GetRect(TAG * t,SRECT * r)
383 if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
385 nbits = (int) swf_GetBits(t,5);
386 r->xmin = swf_GetSBits(t,nbits);
387 r->xmax = swf_GetSBits(t,nbits);
388 r->ymin = swf_GetSBits(t,nbits);
389 r->ymax = swf_GetSBits(t,nbits);
393 int reader_GetRect(reader_t*reader,SRECT * r)
397 nbits = (int) reader_GetBits(reader,5);
398 r->xmin = reader_GetSBits(reader,nbits);
399 r->xmax = reader_GetSBits(reader,nbits);
400 r->ymin = reader_GetSBits(reader,nbits);
401 r->ymax = reader_GetSBits(reader,nbits);
405 int swf_SetRect(TAG * t,SRECT * r)
408 nbits = swf_CountBits(r->xmin,0);
409 nbits = swf_CountBits(r->xmax,nbits);
410 nbits = swf_CountBits(r->ymin,nbits);
411 nbits = swf_CountBits(r->ymax,nbits);
413 fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
417 swf_SetBits(t,nbits,5);
418 swf_SetBits(t,r->xmin,nbits);
419 swf_SetBits(t,r->xmax,nbits);
420 swf_SetBits(t,r->ymin,nbits);
421 swf_SetBits(t,r->ymax,nbits);
426 void swf_ExpandRect(SRECT*src, SPOINT add)
428 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
433 if((add.x|add.y) == 0) src->xmax++; //make sure the bbox is not NULL anymore
436 if(add.x < src->xmin)
438 if(add.x > src->xmax)
440 if(add.y < src->ymin)
442 if(add.y > src->ymax)
445 void swf_ExpandRect2(SRECT*src, SRECT*add)
447 if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
449 if((src->xmin | src->ymin | src->xmax | src->ymax)==0)
451 if(add->xmin < src->xmin)
452 src->xmin = add->xmin;
453 if(add->ymin < src->ymin)
454 src->ymin = add->ymin;
455 if(add->xmax > src->xmax)
456 src->xmax = add->xmax;
457 if(add->ymax > src->ymax)
458 src->ymax = add->ymax;
460 void swf_ExpandRect3(SRECT*src, SPOINT center, int radius)
462 if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
463 src->xmin = center.x-radius;
464 src->ymin = center.y-radius;
465 src->xmax = center.x+radius;
466 src->ymax = center.y+radius;
467 if((center.x|center.y|radius) == 0) src->xmax++; //make sure the bbox is not NULL anymore
470 if(center.x - radius < src->xmin)
471 src->xmin = center.x - radius;
472 if(center.x + radius > src->xmax)
473 src->xmax = center.x + radius;
474 if(center.y - radius < src->ymin)
475 src->ymin = center.y - radius;
476 if(center.y + radius > src->ymax)
477 src->ymax = center.y + radius;
479 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
482 r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
483 r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
486 SRECT swf_TurnRect(SRECT r, MATRIX* m)
489 SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
492 p1.x = r.xmin;p1.y = r.ymin;
493 p2.x = r.xmax;p2.y = r.ymin;
494 p3.x = r.xmin;p3.y = r.ymax;
495 p4.x = r.xmax;p4.y = r.ymax;
496 pp1 = swf_TurnPoint(p1, m);
497 pp2 = swf_TurnPoint(p2, m);
498 pp3 = swf_TurnPoint(p3, m);
499 pp4 = swf_TurnPoint(p4, m);
500 g.xmin = g.xmax = pp1.x;
501 g.ymin = g.ymax = pp1.y;
502 swf_ExpandRect(&g, pp2);
503 swf_ExpandRect(&g, pp3);
504 swf_ExpandRect(&g, pp4);
509 int swf_GetMatrix(TAG * t,MATRIX * m)
516 { m->sx = m->sy = 0x10000;
522 swf_ResetReadBits(t);
524 if (swf_GetBits(t,1))
525 { nbits = swf_GetBits(t,5);
526 m->sx = swf_GetSBits(t,nbits);
527 m->sy = swf_GetSBits(t,nbits);
529 else m->sx = m->sy = 0x10000;
531 if (swf_GetBits(t,1))
532 { nbits = swf_GetBits(t,5);
533 m->r0 = swf_GetSBits(t,nbits);
534 m->r1 = swf_GetSBits(t,nbits);
536 else m->r0 = m->r1 = 0x0;
538 nbits = swf_GetBits(t,5);
539 m->tx = swf_GetSBits(t,nbits);
540 m->ty = swf_GetSBits(t,nbits);
545 int swf_SetMatrix(TAG * t,MATRIX * m)
551 ma.sx = ma.sy = 0x10000;
556 swf_ResetWriteBits(t);
558 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
560 { swf_SetBits(t,1,1);
561 nbits = swf_CountBits(m->sx,0);
562 nbits = swf_CountBits(m->sy,nbits);
564 /* TODO: happens on AMD64 systems for normal values? */
565 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
568 swf_SetBits(t,nbits,5);
569 swf_SetBits(t,m->sx,nbits);
570 swf_SetBits(t,m->sy,nbits);
573 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
575 { swf_SetBits(t,1,1);
576 nbits = swf_CountBits(m->r0,0);
577 nbits = swf_CountBits(m->r1,nbits);
579 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
582 swf_SetBits(t,nbits,5);
583 swf_SetBits(t,m->r0,nbits);
584 swf_SetBits(t,m->r1,nbits);
587 nbits = swf_CountBits(m->tx,0);
588 nbits = swf_CountBits(m->ty,nbits);
590 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
593 swf_SetBits(t,nbits,5);
594 swf_SetBits(t,m->tx,nbits);
595 swf_SetBits(t,m->ty,nbits);
600 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha)
608 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
609 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
613 swf_ResetReadBits(t);
614 hasadd = swf_GetBits(t,1);
615 hasmul = swf_GetBits(t,1);
616 nbits = swf_GetBits(t,4);
619 { cx->r0 = (S16)swf_GetSBits(t,nbits);
620 cx->g0 = (S16)swf_GetSBits(t,nbits);
621 cx->b0 = (S16)swf_GetSBits(t,nbits);
623 cx->a0 = (S16)swf_GetSBits(t,nbits);
627 { cx->r1 = (S16)swf_GetSBits(t,nbits);
628 cx->g1 = (S16)swf_GetSBits(t,nbits);
629 cx->b1 = (S16)swf_GetSBits(t,nbits);
631 cx->a1 = (S16)swf_GetSBits(t,nbits);
637 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
645 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
646 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
656 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
657 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
660 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
661 nbits = swf_CountBits((S32)cx->r0,nbits);
662 nbits = swf_CountBits((S32)cx->g0,nbits);
663 nbits = swf_CountBits((S32)cx->b0,nbits);
667 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
668 nbits = swf_CountBits((S32)cx->r1,nbits);
669 nbits = swf_CountBits((S32)cx->g1,nbits);
670 nbits = swf_CountBits((S32)cx->b1,nbits);
673 swf_ResetWriteBits(t);
674 swf_SetBits(t,hasadd?1:0,1);
675 swf_SetBits(t,hasmul?1:0,1);
676 swf_SetBits(t,nbits,4);
679 { swf_SetBits(t,cx->r0,nbits);
680 swf_SetBits(t,cx->g0,nbits);
681 swf_SetBits(t,cx->b0,nbits);
682 if (alpha) swf_SetBits(t,cx->a0,nbits);
686 { swf_SetBits(t,cx->r1,nbits);
687 swf_SetBits(t,cx->g1,nbits);
688 swf_SetBits(t,cx->b1,nbits);
689 if (alpha) swf_SetBits(t,cx->a1,nbits);
695 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
696 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
698 void swf_SetPassword(TAG * t, const char * password)
700 /* WARNING: crypt_md5 is not reentrant */
704 #if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) && defined(HAVE_TIME_H) && defined(HAVE_TIME)
706 salt[0] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
707 salt[1] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
711 fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
712 fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
716 md5string = crypt_md5(password, salt);
719 swf_SetString(t, md5string);
722 int swf_VerifyPassword(TAG * t, const char * password)
724 char*md5string1, *md5string2;
729 if(t->len >= 5 && t->pos==0 &&
734 printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
737 md5string1 = swf_GetString(t);
739 if(strncmp(md5string1, "$1$",3 )) {
740 fprintf(stderr, "rfxswf: no salt in pw string\n");
743 x = strchr(md5string1+3, '$');
745 fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
748 n = x-(md5string1+3);
749 salt = (char*)rfx_alloc(n+1);
750 memcpy(salt, md5string1+3, n);
753 md5string2 = crypt_md5(password, salt);
755 if(strcmp(md5string1, md5string2) != 0)
760 // Tag List Manipulating Functions
762 TAG * swf_InsertTag(TAG * after,U16 id)
765 t = (TAG *)rfx_calloc(sizeof(TAG));
771 t->next = after->next;
773 if (t->next) t->next->prev = t;
778 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
781 t = (TAG *)rfx_calloc(sizeof(TAG));
787 t->prev = before->prev;
789 if (t->prev) t->prev->next = t;
791 if(swf && swf->firstTag == before) {
797 void swf_ClearTag(TAG * t)
799 if (t->data) rfx_free(t->data);
808 void swf_ResetTag(TAG*tag, U16 id)
810 tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
814 TAG* swf_CopyTag(TAG*tag, TAG*to_copy)
816 tag = swf_InsertTag(tag, to_copy->id);
817 swf_SetBlock(tag, to_copy->data, to_copy->len);
821 int swf_DeleteTag(TAG * t)
824 if (t->prev) t->prev->next = t->next;
825 if (t->next) t->next->prev = t->prev;
827 if (t->data) rfx_free(t->data);
832 TAG * swf_ReadTag(reader_t*reader, TAG * prev)
838 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
846 if (reader->read(reader, &len, 4) != 4) return NULL;
850 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
851 // Sprite handling fix: Flatten sprite tree
853 t = (TAG *)rfx_calloc(sizeof(TAG));
859 { t->data = (U8*)rfx_alloc(t->len);
861 if (reader->read(reader, t->data, t->len) != t->len) {
862 fprintf(stderr, "rfxswf: Warning: Short read (tagid %d). File truncated?\n", t->id);
863 free(t->data);t->data=0;
878 int swf_DefineSprite_GetRealSize(TAG * t);
880 int swf_WriteTag2(writer_t*writer, TAG * t)
881 // returns tag length in bytes (incl. Header), -1 = Error
882 // writer = 0 -> no output
889 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
891 short_tag = len<0x3f&&
892 (t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2&&t->id!=ST_SOUNDSTREAMBLOCK&&
893 t->id!=ST_DEFINEBITSJPEG&&t->id!=ST_DEFINEBITSJPEG2&&t->id!=ST_DEFINEBITSJPEG3);
897 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
898 if (writer->write(writer,raw,2)!=2)
901 fprintf(stderr,"WriteTag() failed: Short Header.\n");
908 raw[0] = SWAP16((t->id<<6)|0x3f);
909 if (writer->write(writer,raw,2)!=2)
912 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
918 if (writer->write(writer,&len,4)!=4)
921 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
928 { if (writer->write(writer,t->data,t->len)!=t->len)
931 fprintf(stderr,"WriteTag() failed: Data.\n");
937 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
941 return t->len+(short_tag?2:6);
944 int swf_WriteTag(int handle, TAG * t)
949 return swf_WriteTag2(0, t);
950 writer_init_filewriter(&writer, handle);
951 len = swf_WriteTag2(&writer, t);
952 writer.finish(&writer);
956 int swf_DefineSprite_GetRealSize(TAG * t)
957 // Sprite Handling: Helper function to pack DefineSprite-Tag
959 if(len>4) { // folded sprite
963 { t = swf_NextTag(t);
964 if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
966 } while (t&&(t->id!=ST_END));
970 void swf_UnFoldSprite(TAG * t)
975 U16 spriteid,spriteframes;
977 if(t->id!=ST_DEFINESPRITE)
979 if(t->len<=4) // not folded
984 spriteid = swf_GetU16(t); //id
985 spriteframes = swf_GetU16(t); //frames
997 if(id == ST_DEFINESPRITE && len<=4)
1001 len = swf_GetU32(t);
1002 it = swf_InsertTag(next, id);
1007 { it->data = (U8*)rfx_alloc(it->len);
1008 it->memsize = it->len;
1009 swf_GetBlock(t, it->data, it->len);
1016 rfx_free(t->data); t->data = 0;
1017 t->memsize = t->len = t->pos = 0;
1019 swf_SetU16(t, spriteid);
1020 swf_SetU16(t, spriteframes);
1023 void swf_FoldSprite(TAG * t)
1026 U16 id,frames,tmpid;
1028 if(t->id!=ST_DEFINESPRITE)
1031 fprintf(stderr, "Error: Sprite has no ID!");
1035 /* sprite is already folded */
1042 t->len = t->pos = t->memsize = 0;
1047 t = swf_NextTag(sprtag);
1052 if(t->id==ST_SHOWFRAME) frames++;
1053 if(t->id == ST_DEFINESPRITE && t->len<=4)
1058 } while(t && level);
1060 fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1062 swf_SetU16(sprtag, id);
1063 swf_SetU16(sprtag, frames);
1065 t = swf_NextTag(sprtag);
1071 (t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2&&t->id!=ST_SOUNDSTREAMBLOCK&&
1072 t->id!=ST_DEFINEBITSJPEG&&t->id!=ST_DEFINEBITSJPEG2&&t->id!=ST_DEFINEBITSJPEG3)
1074 swf_SetU16(sprtag,t->len|(t->id<<6));
1076 swf_SetU16(sprtag,0x3f|(t->id<<6));
1077 swf_SetU32(sprtag,t->len);
1080 swf_SetBlock(sprtag,t->data, t->len);
1082 if(t->id == ST_DEFINESPRITE && t->len<=4)
1091 fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1093 // sprtag->next = t;
1094 // t->prev = sprtag;
1097 int swf_IsFolded(TAG * t)
1099 return (t->id == ST_DEFINESPRITE && t->len>4);
1102 void swf_FoldAll(SWF*swf)
1104 TAG*tag = swf->firstTag;
1105 //swf_DumpSWF(stdout, swf);
1107 if(tag->id == ST_DEFINESPRITE) {
1108 swf_FoldSprite(tag);
1109 //swf_DumpSWF(stdout, swf);
1111 tag = swf_NextTag(tag);
1115 void swf_UnFoldAll(SWF*swf)
1117 TAG*tag = swf->firstTag;
1119 if(tag->id == ST_DEFINESPRITE)
1120 swf_UnFoldSprite(tag);
1125 void swf_OptimizeTagOrder(SWF*swf)
1132 /* at the moment, we don't actually do optimizing,
1133 only fixing of non-spec-conformant things like
1140 tag = swf->firstTag;
1143 if(tag->id == ST_DEFINESPRITE) {
1145 /* ??? all sprites are supposed to be unfolded */
1146 fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1156 /* move non-sprite tags out of sprite */
1157 if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1158 /* remove tag from current position */
1159 tag->prev->next = tag->next;
1161 tag->next->prev = tag->prev;
1163 /* insert before tag level0 */
1165 tag->prev = level0->prev;
1168 tag->prev->next = tag;
1170 swf->firstTag = tag;
1174 if(tag->id == ST_END) {
1185 int swf_ReadSWF2(reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1187 if (!swf) return -1;
1188 memset(swf,0x00,sizeof(SWF));
1190 { char b[32]; // read Header
1196 if ((len = reader->read(reader ,b,8))<8) return -1;
1198 if (b[0]!='F' && b[0]!='C') return -1;
1199 if (b[1]!='W') return -1;
1200 if (b[2]!='S') return -1;
1201 swf->fileVersion = b[3];
1202 swf->compressed = (b[0]=='C')?1:0;
1203 swf->fileSize = GET32(&b[4]);
1205 if(swf->compressed) {
1206 reader_init_zlibinflate(&zreader, reader);
1210 reader_GetRect(reader, &swf->movieSize);
1211 reader->read(reader, &swf->frameRate, 2);
1212 swf->frameRate = SWAP16(swf->frameRate);
1213 reader->read(reader, &swf->frameCount, 2);
1214 swf->frameCount = SWAP16(swf->frameCount);
1216 /* read tags and connect to list */
1218 while (t) t = swf_ReadTag(reader,t);
1219 swf->firstTag = t1.next;
1220 t1.next->prev = NULL;
1226 int swf_ReadSWF(int handle, SWF * swf)
1229 reader_init_filereader(&reader, handle);
1230 return swf_ReadSWF2(&reader, swf);
1233 int swf_WriteSWF2(writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1240 int writer_lastpos = 0;
1243 if (!swf) return -1;
1244 if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1246 if(writer) writer_lastpos = writer->pos;
1248 if(swf->fileVersion >= 8) {
1249 if ((swf->firstTag && swf->firstTag->id != ST_FILEATTRIBUTES) &&
1250 (!swf->firstTag->next || swf->firstTag->next->id != ST_FILEATTRIBUTES))
1252 U32 flags = 0; // | 128 = usenetwork, | 8 = hasmetadata
1253 swf_SetU32(swf_InsertTagBefore(swf, swf->firstTag,ST_FILEATTRIBUTES),flags);
1257 // Insert REFLEX Tag
1259 #ifdef INSERT_RFX_TAG
1261 if ((swf->firstTag && swf->firstTag->id != ST_REFLEX) &&
1262 (!swf->firstTag->next || swf->firstTag->next->id != ST_REFLEX))
1264 swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),"rfx",3);
1267 #endif // INSERT_RFX_TAG
1269 // Count Frames + File Size
1276 len += swf_WriteTag(-1,t);
1277 if(t->id == ST_DEFINESPRITE && !swf_IsFolded(t)) inSprite++;
1278 else if(t->id == ST_END && inSprite) inSprite--;
1279 else if(t->id == ST_END && !inSprite) {
1280 if(t->prev && t->prev->id!=ST_SHOWFRAME)
1283 else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1291 memset(&t1,0x00,sizeof(TAG));
1295 { // measure header file size
1298 memset(&t2,0x00,sizeof(TAG));
1301 swf_SetRect(&t2, &swf->movieSize);
1302 swf_SetU16(&t2, swf->frameRate);
1303 swf_SetU16(&t2, swf->frameCount);
1304 l = swf_GetTagLen(&t2)+8;
1306 if(swf->compressed == 8) {
1311 if(len) {// don't touch headers without tags
1312 swf->fileSize = fileSize;
1313 swf->frameCount = frameCount;
1316 if(swf->compressed != 8) {
1317 /* compressed flag set to 8 means "skip first 8
1318 header bytes". This is necessary if the caller wants to
1319 create compressed SWFs himself .
1320 It also means that we don't initialize our own zlib
1321 writer, but assume the caller provided one.
1323 if(swf->compressed) {
1325 writer->write(writer, id, 3);
1329 writer->write(writer, id, 3);
1332 writer->write(writer, &swf->fileVersion, 1);
1333 PUT32(b4, swf->fileSize);
1334 writer->write(writer, b4, 4);
1336 if(swf->compressed) {
1337 writer_init_zlibdeflate(&zwriter, writer);
1342 swf_SetRect(&t1,&swf->movieSize);
1343 swf_SetU16(&t1,swf->frameRate);
1344 swf_SetU16(&t1,swf->frameCount);
1346 ret = writer->write(writer,b,swf_GetTagLen(&t1));
1347 if (ret!=swf_GetTagLen(&t1))
1350 fprintf(stderr, "ret:%d\n",ret);
1352 fprintf(stderr,"WriteSWF() failed: Header.\n");
1359 { if (swf_WriteTag2(writer, t)<0) return -1;
1362 if(swf->compressed) {
1363 if(swf->compressed != 8) {
1364 zwriter.finish(&zwriter);
1365 return writer->pos - writer_lastpos;
1367 return (int)fileSize;
1369 return (int)fileSize;
1374 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1378 swf->compressed = 0;
1381 writer_init_nullwriter(&writer);
1382 len = swf_WriteSWF2(&writer, swf);
1385 writer_init_filewriter(&writer, handle);
1386 len = swf_WriteSWF2(&writer, swf);
1387 writer.finish(&writer);
1391 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
1395 swf->compressed = 1;
1398 writer_init_nullwriter(&writer);
1399 len = swf_WriteSWF2(&writer, swf);
1401 writer_init_filewriter(&writer, handle);
1402 len = swf_WriteSWF2(&writer, swf);
1403 writer.finish(&writer);
1407 int swf_WriteHeader2(writer_t*writer,SWF * swf)
1410 memcpy(&myswf,swf,sizeof(SWF));
1412 return swf_WriteSWF2(writer, &myswf);
1415 int swf_WriteHeader(int handle,SWF * swf)
1418 memcpy(&myswf,swf,sizeof(SWF));
1420 return swf_WriteSWF(handle, &myswf);
1423 int swf_WriteCGI(SWF * swf)
1427 len = swf_WriteSWF(-1,swf);
1429 if (len<0) return -1;
1431 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1432 "Accept-Ranges: bytes\n"
1433 "Content-Length: %lu\n"
1434 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1437 write(fileno(stdout),s,strlen(s));
1438 return swf_WriteSWF(fileno(stdout),swf);
1441 SWF* swf_CopySWF(SWF*swf)
1443 SWF*nswf = rfx_alloc(sizeof(SWF));
1445 memcpy(nswf, swf, sizeof(SWF));
1447 tag = swf->firstTag;
1450 ntag = swf_CopyTag(ntag, tag);
1452 nswf->firstTag = ntag;
1458 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1459 { TAG * t = swf->firstTag;
1462 { TAG * tnew = t->next;
1463 if (t->data) rfx_free(t->data);
1470 // include advanced functions
1472 #include "modules/swfdump.c"
1473 #include "modules/swfshape.c"
1474 #include "modules/swftext.c"
1475 #include "modules/swffont.c"
1476 #include "modules/swfobject.c"
1477 #include "modules/swfbutton.c"
1478 #include "modules/swftools.c"
1479 #include "modules/swfcgi.c"
1480 #include "modules/swfbits.c"
1481 #include "modules/swfaction.c"
1482 #include "modules/swfsound.c"
1483 #include "modules/swfdraw.c"
1484 #include "modules/swfrender.c"