shell script to make a shared librfxswf.
[swftools.git] / lib / rfxswf.c
1 /* vi: set sts=2 sw=2 :*/
2
3 /* rfxswf.c 
4
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.
8
9    Part of the swftools package.
10
11    Copyright (c) 2000, 2001 Rainer Böhme <rfxswf@reflex-studio.de>
12  
13    This file is distributed under the GPL, see file COPYING for details 
14
15 */
16
17 #include "rfxswf.h"
18
19 #ifdef HAVE_LIBJPEG
20 #ifdef HAVE_JPEGLIB_H
21 #define HAVE_BOOLEAN
22 #include <jpeglib.h>
23 #define _JPEGLIB_INCLUDED_
24 #endif // HAVE_JPEGLIB_H
25 #endif // HAVE_LIBJPEG
26
27 #ifdef HAVE_LIBZ
28 #ifdef HAVE_ZLIB_H
29 #include <zlib.h>
30 #define _ZLIB_INCLUDED_
31 #endif // HAVE_ZLIB_H
32 #endif // HAVE_LIBZ
33
34 #define LAME
35 #include "lame/lame.h"
36
37 #include "./bitio.h"
38
39 // internal constants
40
41 #define MALLOC_SIZE     128
42 #define INSERT_RFX_TAG
43
44 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
45
46
47 // inline wrapper functions
48
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; }
56
57 // Basic Data Access Functions
58
59 #define swf_ResetReadBits(tag)   if (tag->readBit)  { tag->pos++; tag->readBit = 0; }
60 #define swf_ResetWriteBits(tag)  if (tag->writeBit) { tag->writeBit = 0; }
61
62 // for future purpose: avoid high level lib functions to change tagpos/bitpos
63
64 #define swf_SaveTagPos(tag)
65 #define swf_RestoreTagPos(tag)
66
67 void swf_SetTagPos(TAG * t,U32 pos)
68 { swf_ResetReadBits(t);
69   if (pos<=t->len) t->pos = pos;
70   #ifdef DEBUG_RFXSWF
71   else fprintf(stderr,"SetTagPos() out of bounds: TagID = %i\n",t->id);
72   #endif
73 }
74
75 U8 swf_GetU8(TAG * t)
76 { swf_ResetReadBits(t);
77   #ifdef DEBUG_RFXSWF
78     if (t->pos>=t->len) 
79     { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
80       return 0;
81     }
82   #endif
83   return t->data[t->pos++];
84 }
85
86 U16 swf_GetU16(TAG * t)
87 { U16 res;
88   swf_ResetReadBits(t);
89   #ifdef DEBUG_RFXSWF
90     if (t->pos>(t->len-2)) 
91     { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
92       return 0;
93     }
94   #endif
95   res = t->data[t->pos] | (t->data[t->pos+1]<<8);
96   t->pos+=2;
97   return res;
98 }
99
100 U32 swf_GetU32(TAG * t)
101 { U32 res;
102   swf_ResetReadBits(t);
103   #ifdef DEBUG_RFXSWF
104     if (t->pos>(t->len-4)) 
105     { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
106       return 0;
107     }
108   #endif
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);
111   t->pos+=4;
112   return res;
113 }
114
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);
121   t->pos+=l;
122   return l;
123 }
124
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));
132     if (!newdata)
133     {
134       #ifdef DEBUG_RFXSWF
135         fprintf(stderr,"Fatal Error: malloc()/realloc() failed (1). (%d bytes)\n", newmem);
136         *(int*)0=0;
137       #endif
138       return 0;
139     }
140     t->memsize = newmem;
141     t->data    = newdata;
142   }
143   if (b) memcpy(&t->data[t->len],b,l);
144   else memset(&t->data[t->len],0x00,l);
145   t->len+=l;
146   return l;
147 }
148
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;
153   return 0;
154 }
155
156 int swf_SetU16(TAG * t,U16 v)
157 { U8 a[2];
158   a[0] = v&0xff;
159   a[1] = v>>8;
160   
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];
165   return 0;
166 }
167
168 int swf_SetU32(TAG * t,U32 v)
169 { U8 a[4];
170   a[0] = v&0xff;        // to ensure correct handling of non-intel byteorder
171   a[1] = (v>>8)&0xff;
172   a[2] = (v>>16)&0xff;
173   a[3] = (v>>24)&0xff;
174   
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];
181   return 0;
182 }
183
184 U32 swf_GetBits(TAG * t,int nbits)
185 { U32 res = 0;
186   if (!nbits) return 0;
187   if (!t->readBit) t->readBit = 0x80;
188   while (nbits)
189   { res<<=1;
190     if (t->data[t->pos]&t->readBit) res|=1;
191     t->readBit>>=1;
192     nbits--;
193     if (!t->readBit)
194     { if (nbits) t->readBit = 0x80;
195       #ifdef DEBUG_RFXSWF
196       if (t->pos>=t->len) 
197       { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
198         return res;
199       }
200       #endif
201       t->pos++;
202     }
203   }
204   return res;
205 }
206
207 S32 swf_GetSBits(TAG * t,int nbits)
208 { U32 res = swf_GetBits(t,nbits);
209   if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);  
210   return (S32)res;
211 }
212
213 U32 reader_GetBits(struct reader_t*reader, int nbits)
214 { return reader_readbits(reader, nbits);
215 }
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);  
219   return (S32)res;
220 }
221
222 int swf_SetBits(TAG * t,U32 v,int nbits)
223 { U32 bm = 1<<(nbits-1);
224
225   while (nbits)
226   { if (!t->writeBit)
227     { if (FAILED(swf_SetU8(t,0))) return -1;
228       t->writeBit = 0x80;
229     }
230     if (v&bm) t->data[t->len-1] |= t->writeBit;
231     bm>>=1;
232     t->writeBit>>=1;
233     nbits--;
234   }
235   return 0;
236 }
237
238 // Advanced Data Access Functions
239
240 int swf_SetRGB(TAG * t,RGBA * col)
241 { if (!t) return -1;
242   if (col)
243   { swf_SetU8(t,col->r);
244     swf_SetU8(t,col->g);
245     swf_SetU8(t,col->b);
246   } else swf_SetBlock(t,NULL,3);
247   return 0;
248 }
249 void swf_GetRGB(TAG * t, RGBA * col)
250 {
251     RGBA dummy;
252     if(!col);
253         col = &dummy;
254     col->r = swf_GetU8(t);
255     col->g = swf_GetU8(t);
256     col->b = swf_GetU8(t);
257     col->a = 255;
258 }
259
260 int swf_SetRGBA(TAG * t,RGBA * col)
261 { if (!t) return -1;
262   if (col)
263   { swf_SetU8(t,col->r);
264     swf_SetU8(t,col->g);
265     swf_SetU8(t,col->b);
266     swf_SetU8(t,col->a);
267   } else swf_SetBlock(t,NULL,4);
268   return 0;
269 }
270 void swf_GetRGBA(TAG * t, RGBA * col)
271 {
272     RGBA dummy;
273     if(!col);
274         col = &dummy;
275     col->r = swf_GetU8(t);
276     col->g = swf_GetU8(t);
277     col->b = swf_GetU8(t);
278     col->a = swf_GetU8(t);
279 }
280
281 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
282 {
283     GRADIENT dummy;
284     int t;
285     if(!gradient)
286         gradient = &dummy;
287     gradient->num = swf_GetU8(tag);
288     for(t=0;t<gradient->num;t++)
289     {
290         int s=t;
291         if(s>=8) //FIXME
292             s=7;
293         gradient->ratios[t] = swf_GetU8(tag);
294         if(!alpha)
295             swf_GetRGB(tag, &gradient->rgba[t]);
296         else
297             swf_GetRGBA(tag, &gradient->rgba[t]);
298     }
299 }
300
301 int swf_CountBits(U32 v,int nbits)
302 { int n = 33;
303   U32 m = 0x80000000;
304   if (!v) n = 0; else
305   if (v&m)
306   { while (v&m)
307     { n--;
308       m>>=1;
309       if (!m) break;
310     } 
311   }
312   else
313   { while (!(v&m))
314     { n--;
315       m>>=1;
316       if (!m) break;
317     } 
318   }
319   return (n>nbits)?n:nbits;
320 }
321
322 int swf_GetRect(TAG * t,SRECT * r)
323 { int nbits;
324   SRECT dummy;
325   if (!r) r = &dummy;
326   nbits = (int) swf_GetBits(t,5);
327   r->xmin = swf_GetSBits(t,nbits);
328   r->xmax = swf_GetSBits(t,nbits);
329   r->ymin = swf_GetSBits(t,nbits);
330   r->ymax = swf_GetSBits(t,nbits);
331   return 0;
332 }
333
334 int reader_GetRect(struct reader_t*reader,SRECT * r)
335 { int nbits;
336   SRECT dummy;
337   if (!r) r = &dummy;
338   nbits = (int) reader_GetBits(reader,5);
339   r->xmin = reader_GetSBits(reader,nbits);
340   r->xmax = reader_GetSBits(reader,nbits);
341   r->ymin = reader_GetSBits(reader,nbits);
342   r->ymax = reader_GetSBits(reader,nbits);
343   return 0;
344 }
345
346 int swf_SetRect(TAG * t,SRECT * r)
347 { int nbits;
348     
349   nbits = swf_CountBits(r->xmin,0);
350   nbits = swf_CountBits(r->xmax,nbits);
351   nbits = swf_CountBits(r->ymin,nbits);
352   nbits = swf_CountBits(r->ymax,nbits);
353
354   swf_SetBits(t,nbits,5);
355   swf_SetBits(t,r->xmin,nbits);
356   swf_SetBits(t,r->xmax,nbits);
357   swf_SetBits(t,r->ymin,nbits);
358   swf_SetBits(t,r->ymax,nbits);
359
360   return 0;
361 }
362
363 int swf_GetMatrix(TAG * t,MATRIX * m)
364 { MATRIX dummy;
365   int nbits;
366     
367   if (!m) m = &dummy;
368   
369   if (!t)
370   { m->sx = m->sy = 0x10000;
371     m->r0 = m->r1 = 0;
372     m->tx = m->ty = 0;
373     return -1;
374   }
375
376   swf_ResetReadBits(t);
377   
378   if (swf_GetBits(t,1))
379   { nbits = swf_GetBits(t,5);
380     m->sx = swf_GetSBits(t,nbits);
381     m->sy = swf_GetSBits(t,nbits);
382   }
383   else m->sx = m->sy = 0x10000;
384   
385   if (swf_GetBits(t,1))
386   { nbits = swf_GetBits(t,5);
387     m->r0 = swf_GetSBits(t,nbits);
388     m->r1 = swf_GetSBits(t,nbits);
389   }
390   else m->r0 = m->r1 = 0x0;
391
392   nbits = swf_GetBits(t,5);
393   m->tx = swf_GetSBits(t,nbits);
394   m->ty = swf_GetSBits(t,nbits);
395   
396   return 0;
397 }
398
399 int swf_SetMatrix(TAG * t,MATRIX * m)
400 { int nbits;
401   MATRIX ma;
402
403   if (!m)
404   { m = &ma;
405     ma.sx = ma.sy = 0x10000;
406     ma.r0 = ma.r1 = 0;
407     ma.tx = ma.ty = 0;
408   }
409
410   swf_ResetWriteBits(t);
411
412   if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
413   else
414   { swf_SetBits(t,1,1);
415     nbits = swf_CountBits(m->sx,0);
416     nbits = swf_CountBits(m->sy,nbits);
417     if(nbits>=32) {
418         fprintf(stderr,"rfxswf: Error: matrix values too large\n");
419         nbits = 31;
420     }
421     swf_SetBits(t,nbits,5);
422     swf_SetBits(t,m->sx,nbits);
423     swf_SetBits(t,m->sy,nbits);
424   }
425
426   if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
427   else
428   { swf_SetBits(t,1,1);
429     nbits = swf_CountBits(m->r0,0);
430     nbits = swf_CountBits(m->r1,nbits);
431     if(nbits>=32) {
432         fprintf(stderr,"rfxswf: Error: matrix values too large\n");
433         nbits = 31;
434     }
435     swf_SetBits(t,nbits,5);
436     swf_SetBits(t,m->r0,nbits);
437     swf_SetBits(t,m->r1,nbits);
438   }
439
440   nbits = swf_CountBits(m->tx,0);
441   nbits = swf_CountBits(m->ty,nbits);
442   if(nbits>=32) {
443       fprintf(stderr,"rfxswf: Error: matrix values too large\n");
444       nbits = 31;
445   }
446   swf_SetBits(t,nbits,5);
447   swf_SetBits(t,m->tx,nbits);
448   swf_SetBits(t,m->ty,nbits);
449
450   return 0;
451 }
452
453 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha) //FIXME: alpha should be type bool
454 { CXFORM cxf;
455   int hasadd;
456   int hasmul;
457   int nbits;
458     
459   if (!cx) cx = &cxf;
460   
461   cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
462   cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
463
464   if (!t) return 0;
465   
466   swf_ResetReadBits(t);
467   hasadd = swf_GetBits(t,1);
468   hasmul = swf_GetBits(t,1);
469   nbits  = swf_GetBits(t,4);
470
471   if (hasmul)
472   { cx->r0 = (S16)swf_GetSBits(t,nbits);
473     cx->g0 = (S16)swf_GetSBits(t,nbits);
474     cx->b0 = (S16)swf_GetSBits(t,nbits);
475     if (alpha)
476       cx->a0 = (S16)swf_GetSBits(t,nbits);
477   }
478
479   if (hasadd)
480   { cx->r1 = (S16)swf_GetSBits(t,nbits);
481     cx->g1 = (S16)swf_GetSBits(t,nbits);
482     cx->b1 = (S16)swf_GetSBits(t,nbits);
483     if (alpha)
484       cx->a1 = (S16)swf_GetSBits(t,nbits);
485   }
486   
487   return 0;
488 }
489
490 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
491 { CXFORM cxf;
492   int hasadd;
493   int hasmul;
494   int nbits;
495     
496   if (!cx)
497   { cx = &cxf;
498     cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
499     cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
500   }
501
502   if (!alpha)
503   { cx->a0 = 256;
504     cx->a1 = 0;
505   }
506
507   nbits = 0;
508
509   hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
510   hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
511
512   if (hasmul)
513   { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
514     nbits = swf_CountBits((S32)cx->r0,nbits);
515     nbits = swf_CountBits((S32)cx->g0,nbits);
516     nbits = swf_CountBits((S32)cx->b0,nbits);
517   }
518
519   if (hasadd)
520   { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
521     nbits = swf_CountBits((S32)cx->r1,nbits);
522     nbits = swf_CountBits((S32)cx->g1,nbits);
523     nbits = swf_CountBits((S32)cx->b1,nbits);
524   }
525   
526   swf_ResetWriteBits(t);
527   swf_SetBits(t,hasadd?1:0,1);
528   swf_SetBits(t,hasmul?1:0,1);
529   swf_SetBits(t,nbits,4);
530
531   if (hasmul)
532   { swf_SetBits(t,cx->r0,nbits);
533     swf_SetBits(t,cx->g0,nbits);
534     swf_SetBits(t,cx->b0,nbits);
535     if (alpha) swf_SetBits(t,cx->a0,nbits);
536   }
537
538   if (hasadd)
539   { swf_SetBits(t,cx->r1,nbits);
540     swf_SetBits(t,cx->g1,nbits);
541     swf_SetBits(t,cx->b1,nbits);
542     if (alpha) swf_SetBits(t,cx->a1,nbits);
543   }
544   
545   return 0;
546 }
547
548 int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
549 int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
550
551 // Tag List Manipulating Functions
552
553 int swf_UpdateFrame(TAG * t,S8 delta)
554 // returns number of frames
555 { int res = -1;
556   while (t)
557   { t->frame+=delta;
558     res = t->frame;
559     t = t->next;
560   }
561   return res;
562 }
563
564 TAG * swf_InsertTag(TAG * after,U16 id)     // updates frames, if nescessary
565 { TAG * t;
566
567   t = (TAG *)malloc(sizeof(TAG));
568   if (t)
569   { memset(t,0x00,sizeof(TAG));
570     t->id = id;
571     
572     if (after)
573     { t->frame = after->frame;
574       t->prev  = after;
575       t->next  = after->next;
576       after->next = t;
577       if (t->next) t->next->prev = t;
578       
579       if (id==ST_SHOWFRAME) swf_UpdateFrame(t->next,+1);
580     }
581   }
582   return t;
583 }
584
585 int swf_DeleteTag(TAG * t)
586 { if (!t) return -1;
587
588   if (t->id==ST_SHOWFRAME) swf_UpdateFrame(t->next,-1);
589     
590   if (t->prev) t->prev->next = t->next;
591   if (t->next) t->next->prev = t->prev;
592
593   if (t->data) free(t->data);
594   free(t);
595   return 0;
596 }
597
598 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
599 { TAG * t;
600   U16 raw;
601   U32 len;
602   int id;
603
604   if (reader->read(reader, &raw, 2) !=2 ) return NULL;
605   raw = SWAP16(raw);
606
607   len = raw&0x3f;
608   id  = raw>>6;
609
610   if (len==0x3f)
611   {
612       if (reader->read(reader, &len, 4) != 4) return NULL;
613       len = SWAP32(len);
614   }
615
616   if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
617   // Sprite handling fix: Flaten sprite tree
618
619   t = (TAG *)malloc(sizeof(TAG));
620   
621   if (!t)
622   {
623     #ifdef DEBUG_RFXSWF
624       fprintf(stderr,"Fatal Error: malloc()/realloc() failed (2). (%d bytes)\n", sizeof(TAG));
625     #endif
626     return NULL;
627   }
628
629   memset(t,0x00,sizeof(TAG));
630   
631   t->len = len;
632   t->id  = id;
633
634   if (t->len)
635   { t->data = (U8*)malloc(t->len);
636     if (!t->data)
637     {
638       #ifdef DEBUG_RFXSWF
639         fprintf(stderr,"Fatal Error: malloc()/realloc() failed (3). (%d bytes)\n", t->len);
640       #endif
641       return NULL;
642     }
643     t->memsize = t->len;
644     if (reader->read(reader, t->data, t->len) != t->len) return NULL;
645   }
646
647   if (prev)
648   { t->frame = prev->frame+((prev->id==ST_SHOWFRAME)?1:0);
649     t->prev  = prev;
650     prev->next = t;
651   }
652
653   return t;
654 }
655
656 int swf_DefineSprite_GetRealSize(TAG * t);
657
658 int swf_WriteTag2(struct writer_t*writer, TAG * t)
659 // returns tag length in bytes (incl. Header), -1 = Error
660 // writer = 0 -> no output
661 { U16 raw[3];
662   U32 len;
663   int short_tag;
664
665   if (!t) return -1;
666
667   len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
668
669   short_tag = len<0x3f;
670
671   if (writer)
672   { if (short_tag)
673     { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
674       if (writer->write(writer,raw,2)!=2)
675       {
676         #ifdef DEBUG_RFXSWF
677           fprintf(stderr,"WriteTag() failed: Short Header.\n");
678         #endif
679         return -1;
680       }
681     }
682     else
683     {
684       raw[0] = SWAP16((t->id<<6)|0x3f);
685       if (writer->write(writer,raw,2)!=2)
686       {
687 #ifdef DEBUG_RFXSWF
688           fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
689 #endif
690           return -1;
691       }
692       
693       len = SWAP32(len);
694       if (writer->write(writer,&len,4)!=4)
695       {
696         #ifdef DEBUG_RFXSWF
697           fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
698         #endif
699         return -1;
700       }
701     }
702     
703     if (t->data)
704     { if (writer->write(writer,t->data,t->len)!=t->len)
705       {
706         #ifdef DEBUG_RFXSWF
707           fprintf(stderr,"WriteTag() failed: Data.\n");
708         #endif
709         return -1;
710       }
711     }
712     #ifdef DEBUG_RFXSWF
713       else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
714     #endif
715   }
716
717   return t->len+(short_tag?2:6);
718 }
719
720 int swf_WriteTag(int handle, TAG * t)
721 {
722   struct writer_t writer;
723   if(handle<0)
724     return swf_WriteTag2(0, t);
725   writer_init_filewriter(&writer, handle);
726   return swf_WriteTag2(&writer, t);
727 }
728
729 int swf_DefineSprite_GetRealSize(TAG * t)
730 // Sprite Handling: Helper function to pack DefineSprite-Tag
731 { U32 len = t->len;
732   if(len>4) { // folded sprite
733       return t->len;
734   }
735   do
736   { t = swf_NextTag(t);
737     if (t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
738     else t = NULL;
739   } while (t&&(t->id!=ST_END));
740   return len;
741 }
742
743 void swf_FoldSprite(TAG * t)
744 {
745   TAG*sprtag=t,*tmp;
746   U16 id,frames,tmpid;
747   if(t->id!=ST_DEFINESPRITE)
748       return;
749   if(!t->len) {
750       fprintf(stderr, "Error: Sprite has no ID!");
751       return;
752   }
753   if(t->len>4) {
754     /* sprite is already folded */
755       return;
756   }
757
758   t->pos = 0;
759   id = swf_GetU16(t);
760   //frames = swf_GetU16(t);
761   free(t->data);
762   t->len = t->pos = t->memsize = 0;
763   t->data = 0;
764
765   frames = 0;
766
767   do 
768   { 
769     if(t->id==ST_SHOWFRAME) frames++;
770     t = swf_NextTag(t);
771   } while(t&&t!=ST_END);
772
773   t = swf_NextTag(sprtag);
774   swf_SetU16(sprtag, id);
775   swf_SetU16(sprtag, frames);
776
777   do
778   { 
779     tmpid= t->id;
780     if(t->len<0x3f) {
781         swf_SetU16(sprtag,t->len|(t->id<<6));
782     } else {
783         swf_SetU16(sprtag,0x3f|(t->id<<6));
784         swf_SetU32(sprtag,t->len);
785     }
786     if(t->len)
787         swf_SetBlock(sprtag,t->data, t->len);
788     tmp = t;
789     t = swf_NextTag(t);
790     swf_DeleteTag(tmp);
791   } 
792   while (t&&(tmpid!=ST_END));
793
794 //  sprtag->next = t;
795 //  t->prev = sprtag;
796 }
797
798 void swf_FoldAll(SWF*swf)
799 {
800     TAG*tag = swf->firstTag;
801     while(tag) {
802         if(tag->id == ST_DEFINESPRITE)
803             swf_FoldSprite(tag);
804         tag = swf_NextTag(tag);
805     }
806 }
807
808 // Movie Functions
809
810 int swf_ReadSWF2(struct reader_t*reader, SWF * swf)   // Reads SWF to memory (malloc'ed), returns length or <0 if fails
811 {     
812   if (!swf) return -1;
813   memset(swf,0x00,sizeof(SWF));
814
815   { char b[32];                         // read Header
816     int len;
817     TAG * t;
818     TAG t1;
819     struct reader_t zreader;
820     
821     if ((len = reader->read(reader ,b,8))<8) return -1;
822
823     if (b[0]!='F' && b[0]!='C') return -1;
824     if (b[1]!='W') return -1;
825     if (b[2]!='S') return -1;
826     swf->fileVersion = b[3];
827     swf->compressed  = (b[0]=='C')?1:0;
828     swf->fileSize    = GET32(&b[4]);
829     
830     if(swf->compressed) {
831         reader_init_zlibinflate(&zreader, reader);
832         reader = &zreader;
833     }
834
835     reader_GetRect(reader, &swf->movieSize);
836     reader->read(reader, &swf->frameRate, 2);
837     swf->frameRate = SWAP16(swf->frameRate);
838     reader->read(reader, &swf->frameCount, 2);
839     swf->frameCount = SWAP16(swf->frameCount);
840
841     /* read tags and connect to list */
842     t = &t1;
843     while (t) t = swf_ReadTag(reader,t);
844     swf->firstTag = t1.next;
845     t1.next->prev = NULL;
846   }
847   
848   return reader->pos;
849 }
850
851 int swf_ReadSWF(int handle, SWF * swf)
852 {
853   struct reader_t reader;
854   reader_init_filereader(&reader, handle);
855   return swf_ReadSWF2(&reader, swf);
856 }
857
858 int  swf_WriteSWF2(struct writer_t*writer, SWF * swf)     // Writes SWF to file, returns length or <0 if fails
859 { U32 len;
860   TAG * t;
861   int frameCount=0;
862   struct writer_t zwriter;
863     
864   if (!swf) return -1;
865
866   // Insert REFLEX Tag
867
868 #ifdef INSERT_RFX_TAG
869
870   if (swf->firstTag && swf_NextTag(swf->firstTag))
871     if (swf_GetTagID(swf_NextTag(swf->firstTag))!=ST_REFLEX)
872       swf_SetBlock(swf_InsertTag(swf->firstTag,ST_REFLEX),"rfx",3);
873
874 #endif // INSERT_RFX_TAG
875
876   // Count Frames + File Size
877
878   len = 0;
879   t = swf->firstTag;
880   frameCount = 0;
881
882   while(t)
883   { len += swf_WriteTag(-1, t);
884     if (t->id==ST_SHOWFRAME) frameCount++;
885     t = swf_NextTag(t);
886   }
887   
888   { TAG t1;
889     char b[64],b4[4];
890     U32 l;
891
892     memset(&t1,0x00,sizeof(TAG));
893     t1.data    = (U8*)b;
894     t1.memsize = 64;
895     
896     { // measure header file size
897       TAG t2;
898       char b2[64];
899       memset(&t2,0x00,sizeof(TAG));
900       t2.data    = (U8*)b2;
901       t2.memsize = 64;
902       swf_SetRect(&t2, &swf->movieSize);
903       swf_SetU16(&t2, swf->frameRate);
904       swf_SetU16(&t2, swf->frameCount);
905       l = swf_GetTagLen(&t2)+8;
906     }
907
908     if(len) {// don't touch headers without tags
909         swf->fileSize = l+len;
910         swf->frameCount = frameCount;
911     }
912    
913     if(swf->compressed) {
914       char*id = "CWS";
915       writer->write(writer, id, 3);
916     }
917     else {
918       char*id = "FWS";
919       writer->write(writer, id, 3);
920     }
921
922     writer->write(writer, &swf->fileVersion, 1);
923     PUT32(b4, swf->fileSize);
924     writer->write(writer, b4, 4);
925
926     if(swf->compressed) {
927       writer_init_zlibdeflate(&zwriter, writer);
928       writer = &zwriter;
929     }
930
931     swf_SetRect(&t1,&swf->movieSize);
932     swf_SetU16(&t1,swf->frameRate);
933     swf_SetU16(&t1,swf->frameCount);
934
935     if (writer)
936     { 
937       int ret = writer->write(writer,b,swf_GetTagLen(&t1));
938       if (ret!=swf_GetTagLen(&t1))
939       {
940         #ifdef DEBUG_RFXSWF
941           printf("ret:%d\n",ret);
942           perror("write:");
943           fprintf(stderr,"WriteSWF() failed: Header.\n");
944         #endif
945         return -1;
946       }
947
948       t = swf->firstTag;
949       while (t)
950       { if (swf_WriteTag2(writer, t)<0) return -1;
951         t = swf_NextTag(t);
952       }
953       writer->finish(writer); //e.g. flush zlib buffers
954     }
955   }
956   return (int)swf->fileSize;
957 }
958
959 int  swf_WriteSWF(int handle, SWF * swf)     // Writes SWF to file, returns length or <0 if fails
960 {
961   struct writer_t writer;
962   swf->compressed = 0;
963   if(handle<0)
964     return swf_WriteSWF2(&writer, swf);
965   writer_init_filewriter(&writer, handle);
966   return swf_WriteSWF2(&writer, swf);
967 }
968
969 int  swf_WriteSWC(int handle, SWF * swf)     // Writes SWF to file, returns length or <0 if fails
970 {
971   struct writer_t writer;
972   swf->compressed = 1;
973   if(handle<0)
974     return swf_WriteSWF2(&writer, swf);
975   writer_init_filewriter(&writer, handle);
976   return swf_WriteSWF2(&writer, swf);
977 }
978
979 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
980 {
981   SWF myswf;
982   memcpy(&myswf,swf,sizeof(SWF));
983   myswf.firstTag = 0;
984   return swf_WriteSWF2(writer, &myswf);
985 }
986
987 int swf_WriteHeader(int handle,SWF * swf)
988 {
989   SWF myswf;
990   memcpy(&myswf,swf,sizeof(SWF));
991   myswf.firstTag = 0;
992   return swf_WriteSWF(handle, &myswf);
993 }
994
995 int swf_WriteCGI(SWF * swf)
996 { int len;
997   char s[1024];
998     
999   len = swf_WriteSWF(-1,swf);
1000
1001   if (len<0) return -1;
1002
1003   sprintf(s,"Content-type: application/x-shockwave-flash\n"
1004             "Accept-Ranges: bytes\n"
1005             "Content-Length: %lu\n"
1006             "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1007             "\n",len);
1008             
1009   write(fileno(stdout),s,strlen(s));
1010   return swf_WriteSWF(fileno(stdout),swf);
1011 }
1012
1013 void swf_FreeTags(SWF * swf)                 // Frees all malloc'ed memory for tags
1014 { TAG * t = swf->firstTag;
1015
1016   while (t)
1017   { TAG * tnew = t->next;
1018     if (t->data) free(t->data);
1019     free(t);
1020     t = tnew;
1021   }
1022 }
1023
1024 // include advanced functions
1025
1026 #include "modules/swfdump.c"
1027 #include "modules/swfshape.c"
1028 #include "modules/swftext.c"
1029 #include "modules/swfobject.c"
1030 #include "modules/swfbutton.c"
1031 #include "modules/swftools.c"
1032 #include "modules/swfcgi.c"
1033 #include "modules/swfbits.c"
1034 #include "modules/swfaction.c"
1035 #include "modules/swfsound.c"
1036