1 /* vi: set sts=2 sw=2 :*/
5 Library for creating and reading SWF files or parts of it.
6 There's a module directory which provides some extended functionality.
7 Most modules are included at the bottom of this file.
9 Part of the swftools package.
11 Copyright (c) 2000, 2001 Rainer Böhme <rfxswf@reflex-studio.de>
13 This file is distributed under the GPL, see file COPYING for details
23 #define _JPEGLIB_INCLUDED_
24 #endif // HAVE_JPEGLIB_H
25 #endif // HAVE_LIBJPEG
30 #define _ZLIB_INCLUDED_
35 #include "lame/lame.h"
41 #define MALLOC_SIZE 128
42 #define INSERT_RFX_TAG
44 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
47 // inline wrapper functions
49 TAG * swf_NextTag(TAG * t) { return t->next; }
50 TAG * swf_PrevTag(TAG * t) { return t->prev; }
51 int swf_GetFrameNo(TAG * t) { return t->frame; }
52 U16 swf_GetTagID(TAG * t) { return t->id; }
53 U32 swf_GetTagLen(TAG * t) { return t->len; }
54 U8* swf_GetTagLenPtr(TAG * t) { return &(t->data[t->len]); }
55 U32 swf_GetTagPos(TAG * t) { return t->pos; }
57 // Basic Data Access Functions
59 #define swf_ResetReadBits(tag) if (tag->readBit) { tag->pos++; tag->readBit = 0; }
60 #define swf_ResetWriteBits(tag) if (tag->writeBit) { tag->writeBit = 0; }
62 // for future purpose: avoid high level lib functions to change tagpos/bitpos
64 #define swf_SaveTagPos(tag)
65 #define swf_RestoreTagPos(tag)
67 void swf_SetTagPos(TAG * t,U32 pos)
68 { swf_ResetReadBits(t);
69 if (pos<=t->len) t->pos = pos;
71 else fprintf(stderr,"SetTagPos() out of bounds: TagID = %i\n",t->id);
76 { swf_ResetReadBits(t);
79 { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
83 return t->data[t->pos++];
86 U16 swf_GetU16(TAG * t)
90 if (t->pos>(t->len-2))
91 { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
95 res = t->data[t->pos] | (t->data[t->pos+1]<<8);
100 U32 swf_GetU32(TAG * t)
102 swf_ResetReadBits(t);
104 if (t->pos>(t->len-4))
105 { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
109 res = t->data[t->pos] | (t->data[t->pos+1]<<8) |
110 (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
115 int swf_GetBlock(TAG * t,U8 * b,int l)
116 // returns number of bytes written (<=l)
117 // b = NULL -> skip data
118 { swf_ResetReadBits(t);
119 if ((t->len-t->pos)<l) l=t->len-t->pos;
120 if (b && l) memcpy(b,&t->data[t->pos],l);
125 int swf_SetBlock(TAG * t,U8 * b,int l)
126 // Appends Block to the end of Tagdata, returns size
127 { U32 newlen = t->len + l;
128 swf_ResetWriteBits(t);
129 if (newlen>t->memsize)
130 { U32 newmem = MEMSIZE(newlen);
131 U8 * newdata = (U8*)((t->data)?realloc(t->data,newmem):malloc(newmem));
135 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (1). (%d bytes)\n", newmem);
143 if (b) memcpy(&t->data[t->len],b,l);
144 else memset(&t->data[t->len],0x00,l);
149 int swf_SetU8(TAG * t,U8 v)
150 { swf_ResetWriteBits(t);
151 if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
152 t->data[t->len++] = v;
156 int swf_SetU16(TAG * t,U16 v)
161 swf_ResetWriteBits(t);
162 if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
163 t->data[t->len++] = a[0];
164 t->data[t->len++] = a[1];
168 int swf_SetU32(TAG * t,U32 v)
170 a[0] = v&0xff; // to ensure correct handling of non-intel byteorder
175 swf_ResetWriteBits(t);
176 if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
177 t->data[t->len++] = a[0];
178 t->data[t->len++] = a[1];
179 t->data[t->len++] = a[2];
180 t->data[t->len++] = a[3];
184 U32 swf_GetBits(TAG * t,int nbits)
186 if (!nbits) return 0;
187 if (!t->readBit) t->readBit = 0x80;
190 if (t->data[t->pos]&t->readBit) res|=1;
194 { if (nbits) t->readBit = 0x80;
197 { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
207 S32 swf_GetSBits(TAG * t,int nbits)
208 { U32 res = swf_GetBits(t,nbits);
209 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
213 U32 reader_GetBits(struct reader_t*reader, int nbits)
214 { return reader_readbits(reader, nbits);
216 S32 reader_GetSBits(struct reader_t*reader, int nbits)
217 { U32 res = reader_readbits(reader, nbits);
218 if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);
222 int swf_SetBits(TAG * t,U32 v,int nbits)
223 { U32 bm = 1<<(nbits-1);
227 { if (FAILED(swf_SetU8(t,0))) return -1;
230 if (v&bm) t->data[t->len-1] |= t->writeBit;
238 // Advanced Data Access Functions
240 int swf_SetRGB(TAG * t,RGBA * col)
243 { swf_SetU8(t,col->r);
246 } else swf_SetBlock(t,NULL,3);
249 void swf_GetRGB(TAG * t, RGBA * col)
254 col->r = swf_GetU8(t);
255 col->g = swf_GetU8(t);
256 col->b = swf_GetU8(t);
260 int swf_SetRGBA(TAG * t,RGBA * col)
263 { swf_SetU8(t,col->r);
267 } else swf_SetBlock(t,NULL,4);
270 void swf_GetRGBA(TAG * t, RGBA * col)
275 col->r = swf_GetU8(t);
276 col->g = swf_GetU8(t);
277 col->b = swf_GetU8(t);
278 col->a = swf_GetU8(t);
281 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
287 gradient->num = swf_GetU8(tag);
288 for(t=0;t<gradient->num;t++)
293 gradient->ratios[t] = swf_GetU8(tag);
295 swf_GetRGB(tag, &gradient->rgba[t]);
297 swf_GetRGBA(tag, &gradient->rgba[t]);
301 void swf_GetMorphGradient(TAG * tag, GRADIENT * gradient1, GRADIENT * gradient2)
311 gradient2->num = swf_GetU8(tag);
312 for(t=0;t<gradient1->num;t++)
317 gradient1->ratios[t] = swf_GetU8(tag);
318 swf_GetRGBA(tag, &gradient1->rgba[t]);
319 gradient2->ratios[t] = swf_GetU8(tag);
320 swf_GetRGBA(tag, &gradient2->rgba[t]);
324 int swf_CountBits(U32 v,int nbits)
342 return (n>nbits)?n:nbits;
345 int swf_GetRect(TAG * t,SRECT * r)
349 nbits = (int) swf_GetBits(t,5);
350 r->xmin = swf_GetSBits(t,nbits);
351 r->xmax = swf_GetSBits(t,nbits);
352 r->ymin = swf_GetSBits(t,nbits);
353 r->ymax = swf_GetSBits(t,nbits);
357 int reader_GetRect(struct reader_t*reader,SRECT * r)
361 nbits = (int) reader_GetBits(reader,5);
362 r->xmin = reader_GetSBits(reader,nbits);
363 r->xmax = reader_GetSBits(reader,nbits);
364 r->ymin = reader_GetSBits(reader,nbits);
365 r->ymax = reader_GetSBits(reader,nbits);
369 int swf_SetRect(TAG * t,SRECT * r)
372 nbits = swf_CountBits(r->xmin,0);
373 nbits = swf_CountBits(r->xmax,nbits);
374 nbits = swf_CountBits(r->ymin,nbits);
375 nbits = swf_CountBits(r->ymax,nbits);
377 swf_SetBits(t,nbits,5);
378 swf_SetBits(t,r->xmin,nbits);
379 swf_SetBits(t,r->xmax,nbits);
380 swf_SetBits(t,r->ymin,nbits);
381 swf_SetBits(t,r->ymax,nbits);
386 int swf_GetMatrix(TAG * t,MATRIX * m)
393 { m->sx = m->sy = 0x10000;
399 swf_ResetReadBits(t);
401 if (swf_GetBits(t,1))
402 { nbits = swf_GetBits(t,5);
403 m->sx = swf_GetSBits(t,nbits);
404 m->sy = swf_GetSBits(t,nbits);
406 else m->sx = m->sy = 0x10000;
408 if (swf_GetBits(t,1))
409 { nbits = swf_GetBits(t,5);
410 m->r0 = swf_GetSBits(t,nbits);
411 m->r1 = swf_GetSBits(t,nbits);
413 else m->r0 = m->r1 = 0x0;
415 nbits = swf_GetBits(t,5);
416 m->tx = swf_GetSBits(t,nbits);
417 m->ty = swf_GetSBits(t,nbits);
422 int swf_SetMatrix(TAG * t,MATRIX * m)
428 ma.sx = ma.sy = 0x10000;
433 swf_ResetWriteBits(t);
435 if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
437 { swf_SetBits(t,1,1);
438 nbits = swf_CountBits(m->sx,0);
439 nbits = swf_CountBits(m->sy,nbits);
441 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
444 swf_SetBits(t,nbits,5);
445 swf_SetBits(t,m->sx,nbits);
446 swf_SetBits(t,m->sy,nbits);
449 if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
451 { swf_SetBits(t,1,1);
452 nbits = swf_CountBits(m->r0,0);
453 nbits = swf_CountBits(m->r1,nbits);
455 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
458 swf_SetBits(t,nbits,5);
459 swf_SetBits(t,m->r0,nbits);
460 swf_SetBits(t,m->r1,nbits);
463 nbits = swf_CountBits(m->tx,0);
464 nbits = swf_CountBits(m->ty,nbits);
466 fprintf(stderr,"rfxswf: Error: matrix values too large\n");
469 swf_SetBits(t,nbits,5);
470 swf_SetBits(t,m->tx,nbits);
471 swf_SetBits(t,m->ty,nbits);
476 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha) //FIXME: alpha should be type bool
484 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
485 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
489 swf_ResetReadBits(t);
490 hasadd = swf_GetBits(t,1);
491 hasmul = swf_GetBits(t,1);
492 nbits = swf_GetBits(t,4);
495 { cx->r0 = (S16)swf_GetSBits(t,nbits);
496 cx->g0 = (S16)swf_GetSBits(t,nbits);
497 cx->b0 = (S16)swf_GetSBits(t,nbits);
499 cx->a0 = (S16)swf_GetSBits(t,nbits);
503 { cx->r1 = (S16)swf_GetSBits(t,nbits);
504 cx->g1 = (S16)swf_GetSBits(t,nbits);
505 cx->b1 = (S16)swf_GetSBits(t,nbits);
507 cx->a1 = (S16)swf_GetSBits(t,nbits);
513 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
521 cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
522 cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
532 hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
533 hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
536 { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
537 nbits = swf_CountBits((S32)cx->r0,nbits);
538 nbits = swf_CountBits((S32)cx->g0,nbits);
539 nbits = swf_CountBits((S32)cx->b0,nbits);
543 { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
544 nbits = swf_CountBits((S32)cx->r1,nbits);
545 nbits = swf_CountBits((S32)cx->g1,nbits);
546 nbits = swf_CountBits((S32)cx->b1,nbits);
549 swf_ResetWriteBits(t);
550 swf_SetBits(t,hasadd?1:0,1);
551 swf_SetBits(t,hasmul?1:0,1);
552 swf_SetBits(t,nbits,4);
555 { swf_SetBits(t,cx->r0,nbits);
556 swf_SetBits(t,cx->g0,nbits);
557 swf_SetBits(t,cx->b0,nbits);
558 if (alpha) swf_SetBits(t,cx->a0,nbits);
562 { swf_SetBits(t,cx->r1,nbits);
563 swf_SetBits(t,cx->g1,nbits);
564 swf_SetBits(t,cx->b1,nbits);
565 if (alpha) swf_SetBits(t,cx->a1,nbits);
571 int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
572 int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
574 // Tag List Manipulating Functions
576 int swf_UpdateFrame(TAG * t,S8 delta)
577 // returns number of frames
587 TAG * swf_InsertTag(TAG * after,U16 id) // updates frames, if nescessary
590 t = (TAG *)malloc(sizeof(TAG));
592 { memset(t,0x00,sizeof(TAG));
596 { t->frame = after->frame;
598 t->next = after->next;
600 if (t->next) t->next->prev = t;
602 if (id==ST_SHOWFRAME) swf_UpdateFrame(t->next,+1);
608 int swf_DeleteTag(TAG * t)
611 if (t->id==ST_SHOWFRAME) swf_UpdateFrame(t->next,-1);
613 if (t->prev) t->prev->next = t->next;
614 if (t->next) t->next->prev = t->prev;
616 if (t->data) free(t->data);
621 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
627 if (reader->read(reader, &raw, 2) !=2 ) return NULL;
635 if (reader->read(reader, &len, 4) != 4) return NULL;
639 if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
640 // Sprite handling fix: Flaten sprite tree
642 t = (TAG *)malloc(sizeof(TAG));
647 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (2). (%d bytes)\n", sizeof(TAG));
652 memset(t,0x00,sizeof(TAG));
658 { t->data = (U8*)malloc(t->len);
662 fprintf(stderr,"Fatal Error: malloc()/realloc() failed (3). (%d bytes)\n", t->len);
667 if (reader->read(reader, t->data, t->len) != t->len) return NULL;
671 { t->frame = prev->frame+((prev->id==ST_SHOWFRAME)?1:0);
679 int swf_DefineSprite_GetRealSize(TAG * t);
681 int swf_WriteTag2(struct writer_t*writer, TAG * t)
682 // returns tag length in bytes (incl. Header), -1 = Error
683 // writer = 0 -> no output
690 len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
692 short_tag = len<0x3f;
696 { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
697 if (writer->write(writer,raw,2)!=2)
700 fprintf(stderr,"WriteTag() failed: Short Header.\n");
707 raw[0] = SWAP16((t->id<<6)|0x3f);
708 if (writer->write(writer,raw,2)!=2)
711 fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
717 if (writer->write(writer,&len,4)!=4)
720 fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
727 { if (writer->write(writer,t->data,t->len)!=t->len)
730 fprintf(stderr,"WriteTag() failed: Data.\n");
736 else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
740 return t->len+(short_tag?2:6);
743 int swf_WriteTag(int handle, TAG * t)
745 struct writer_t writer;
747 return swf_WriteTag2(0, t);
748 writer_init_filewriter(&writer, handle);
749 return swf_WriteTag2(&writer, t);
752 int swf_DefineSprite_GetRealSize(TAG * t)
753 // Sprite Handling: Helper function to pack DefineSprite-Tag
755 if(len>4) { // folded sprite
759 { t = swf_NextTag(t);
760 if (t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
762 } while (t&&(t->id!=ST_END));
766 void swf_FoldSprite(TAG * t)
770 if(t->id!=ST_DEFINESPRITE)
773 fprintf(stderr, "Error: Sprite has no ID!");
777 /* sprite is already folded */
783 //frames = swf_GetU16(t);
785 t->len = t->pos = t->memsize = 0;
792 if(t->id==ST_SHOWFRAME) frames++;
794 } while(t&&t!=ST_END);
796 t = swf_NextTag(sprtag);
797 swf_SetU16(sprtag, id);
798 swf_SetU16(sprtag, frames);
804 swf_SetU16(sprtag,t->len|(t->id<<6));
806 swf_SetU16(sprtag,0x3f|(t->id<<6));
807 swf_SetU32(sprtag,t->len);
810 swf_SetBlock(sprtag,t->data, t->len);
815 while (t&&(tmpid!=ST_END));
821 void swf_FoldAll(SWF*swf)
823 TAG*tag = swf->firstTag;
825 if(tag->id == ST_DEFINESPRITE)
827 tag = swf_NextTag(tag);
833 int swf_ReadSWF2(struct reader_t*reader, SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails
836 memset(swf,0x00,sizeof(SWF));
838 { char b[32]; // read Header
842 struct reader_t zreader;
844 if ((len = reader->read(reader ,b,8))<8) return -1;
846 if (b[0]!='F' && b[0]!='C') return -1;
847 if (b[1]!='W') return -1;
848 if (b[2]!='S') return -1;
849 swf->fileVersion = b[3];
850 swf->compressed = (b[0]=='C')?1:0;
851 swf->fileSize = GET32(&b[4]);
853 if(swf->compressed) {
854 reader_init_zlibinflate(&zreader, reader);
858 reader_GetRect(reader, &swf->movieSize);
859 reader->read(reader, &swf->frameRate, 2);
860 swf->frameRate = SWAP16(swf->frameRate);
861 reader->read(reader, &swf->frameCount, 2);
862 swf->frameCount = SWAP16(swf->frameCount);
864 /* read tags and connect to list */
866 while (t) t = swf_ReadTag(reader,t);
867 swf->firstTag = t1.next;
868 t1.next->prev = NULL;
874 int swf_ReadSWF(int handle, SWF * swf)
876 struct reader_t reader;
877 reader_init_filereader(&reader, handle);
878 return swf_ReadSWF2(&reader, swf);
881 int swf_WriteSWF2(struct writer_t*writer, SWF * swf) // Writes SWF to file, returns length or <0 if fails
885 struct writer_t zwriter;
891 #ifdef INSERT_RFX_TAG
893 if (swf->firstTag && swf_NextTag(swf->firstTag))
894 if (swf_GetTagID(swf_NextTag(swf->firstTag))!=ST_REFLEX)
895 swf_SetBlock(swf_InsertTag(swf->firstTag,ST_REFLEX),"rfx",3);
897 #endif // INSERT_RFX_TAG
899 // Count Frames + File Size
906 { len += swf_WriteTag(-1, t);
907 if (t->id==ST_SHOWFRAME) frameCount++;
915 memset(&t1,0x00,sizeof(TAG));
919 { // measure header file size
922 memset(&t2,0x00,sizeof(TAG));
925 swf_SetRect(&t2, &swf->movieSize);
926 swf_SetU16(&t2, swf->frameRate);
927 swf_SetU16(&t2, swf->frameCount);
928 l = swf_GetTagLen(&t2)+8;
931 if(len) {// don't touch headers without tags
932 swf->fileSize = l+len;
933 swf->frameCount = frameCount;
936 if(swf->compressed) {
938 writer->write(writer, id, 3);
942 writer->write(writer, id, 3);
945 writer->write(writer, &swf->fileVersion, 1);
946 PUT32(b4, swf->fileSize);
947 writer->write(writer, b4, 4);
949 if(swf->compressed) {
950 writer_init_zlibdeflate(&zwriter, writer);
954 swf_SetRect(&t1,&swf->movieSize);
955 swf_SetU16(&t1,swf->frameRate);
956 swf_SetU16(&t1,swf->frameCount);
960 int ret = writer->write(writer,b,swf_GetTagLen(&t1));
961 if (ret!=swf_GetTagLen(&t1))
964 printf("ret:%d\n",ret);
966 fprintf(stderr,"WriteSWF() failed: Header.\n");
973 { if (swf_WriteTag2(writer, t)<0) return -1;
976 writer->finish(writer); //e.g. flush zlib buffers
979 return (int)swf->fileSize;
982 int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
984 struct writer_t writer;
987 return swf_WriteSWF2(&writer, swf);
988 writer_init_filewriter(&writer, handle);
989 return swf_WriteSWF2(&writer, swf);
992 int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails
994 struct writer_t writer;
997 return swf_WriteSWF2(&writer, swf);
998 writer_init_filewriter(&writer, handle);
999 return swf_WriteSWF2(&writer, swf);
1002 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1005 memcpy(&myswf,swf,sizeof(SWF));
1007 return swf_WriteSWF2(writer, &myswf);
1010 int swf_WriteHeader(int handle,SWF * swf)
1013 memcpy(&myswf,swf,sizeof(SWF));
1015 return swf_WriteSWF(handle, &myswf);
1018 int swf_WriteCGI(SWF * swf)
1022 len = swf_WriteSWF(-1,swf);
1024 if (len<0) return -1;
1026 sprintf(s,"Content-type: application/x-shockwave-flash\n"
1027 "Accept-Ranges: bytes\n"
1028 "Content-Length: %lu\n"
1029 "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1032 write(fileno(stdout),s,strlen(s));
1033 return swf_WriteSWF(fileno(stdout),swf);
1036 void swf_FreeTags(SWF * swf) // Frees all malloc'ed memory for tags
1037 { TAG * t = swf->firstTag;
1040 { TAG * tnew = t->next;
1041 if (t->data) free(t->data);
1047 // include advanced functions
1049 #include "modules/swfdump.c"
1050 #include "modules/swfshape.c"
1051 #include "modules/swftext.c"
1052 #include "modules/swfobject.c"
1053 #include "modules/swfbutton.c"
1054 #include "modules/swftools.c"
1055 #include "modules/swfcgi.c"
1056 #include "modules/swfbits.c"
1057 #include "modules/swfaction.c"
1058 #include "modules/swfsound.c"