added gcc 2.95.x support.
[swftools.git] / lib / rfxswf.c
1 /* vi: set sts=2 sw=2 :*/
2 /* rfxswf.c 
3
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.
7
8    Part of the swftools package.
9
10    Copyright (c) 2000-2003 Rainer Böhme <rfxswf@reflex-studio.de>
11    Copyright (c) 2003 Matthias Kramm <kramm@quiss.org> 
12
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.
17
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.
22
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 */
26
27 #include "rfxswf.h"
28
29 #ifdef HAVE_JPEGLIB
30 #define HAVE_BOOLEAN
31 #include <jpeglib.h>
32 #endif // HAVE_JPEGLIB
33
34 #ifdef HAVE_ZLIB
35 #include <zlib.h>
36 #endif // HAVE_ZLIB
37
38 #ifndef RFXSWF_DISABLESOUND
39 #ifdef HAVE_LAME
40 #include "lame/lame.h"
41 #endif
42 #endif
43
44 #ifdef HAVE_TIME_H
45 #include <time.h>
46 #endif
47
48 #include "./bitio.h"
49 #include "./MD5.h"
50
51 // memory allocation
52
53 void* rfx_alloc(int size)
54 {
55   void*ptr;
56   if(size == 0) {
57     //*(int*)0 = 0xdead;
58     //fprintf(stderr, "Warning: Zero alloc\n");
59     return 0;
60   }
61
62   ptr = malloc(size);
63   if(!ptr) {
64     fprintf(stderr, "FATAL: Out of memory\n");
65     /* TODO: we should send a signal, so that the debugger kicks in */
66     exit(1);
67   }
68   return ptr;
69 }
70 void* rfx_realloc(void*data, int size)
71 {
72   void*ptr;
73   if(size == 0) {
74     //*(int*)0 = 0xdead;
75     //fprintf(stderr, "Warning: Zero realloc\n");
76     rfx_free(data);
77     return 0;
78   }
79   if(!data) {
80     ptr = malloc(size);
81   } else {
82     ptr = realloc(data, size);
83   }
84
85   if(!ptr) {
86     fprintf(stderr, "FATAL: Out of memory\n");
87     /* TODO: we should send a signal, so that the debugger kicks in */
88     exit(1);
89   }
90   return ptr;
91 }
92 void* rfx_calloc(int size)
93 {
94   void*ptr;
95   if(size == 0) {
96     //*(int*)0 = 0xdead;
97     //fprintf(stderr, "Warning: Zero alloc\n");
98     return 0;
99   }
100 #ifdef HAVE_CALLOC
101   ptr = calloc(size);
102 #else
103   ptr = malloc(size);
104 #endif
105   if(!ptr) {
106     fprintf(stderr, "FATAL: Out of memory\n");
107     /* TODO: we should send a signal, so that the debugger kicks in */
108     exit(1);
109   }
110 #ifndef HAVE_CALLOC
111   memset(ptr, 0, size);
112 #endif
113   return ptr;
114 }
115
116 void rfx_free(void*ptr)
117 {
118   if(!ptr)
119     return;
120   free(ptr);
121 }
122
123 #ifdef MEMORY_INFO
124 long rfx_memory_used()
125 {
126 }
127
128 char* rfx_memory_used_str()
129 {
130 }
131 #endif
132
133 // internal constants
134
135 #define MALLOC_SIZE     128
136 #define INSERT_RFX_TAG
137
138 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
139
140 // inline wrapper functions
141
142 TAG * swf_NextTag(TAG * t) { return t->next; }
143 TAG * swf_PrevTag(TAG * t) { return t->prev; }
144 U16   swf_GetTagID(TAG * t)    { return t->id; }
145 U32   swf_GetTagLen(TAG * t) { return t->len; }
146 U8*   swf_GetTagLenPtr(TAG * t) { return &(t->data[t->len]); }
147 U32   swf_GetTagPos(TAG * t)   { return t->pos; }
148
149 // for future purpose: avoid high level lib functions to change tagpos/bitpos
150
151 #define swf_SaveTagPos(tag)
152 #define swf_RestoreTagPos(tag)
153
154 void swf_SetTagPos(TAG * t,U32 pos)
155 { swf_ResetReadBits(t);
156   if (pos<=t->len) t->pos = pos;
157   else { 
158 #ifdef DEBUG_RFXSWF
159     fprintf(stderr,"SetTagPos(%d) out of bounds: TagID = %i\n",pos, t->id);
160 #endif
161   }
162 }
163
164 char* swf_GetString(TAG*t)
165 {
166     char* str = ((char*)(&(t)->data[(t)->pos]));
167     while(swf_GetU8(t));
168     return str;
169 }
170
171 U8 swf_GetU8(TAG * t)
172 { swf_ResetReadBits(t);
173   #ifdef DEBUG_RFXSWF
174     if (t->pos>=t->len) 
175     { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
176       return 0;
177     }
178   #endif
179   return t->data[t->pos++];
180 }
181
182 U16 swf_GetU16(TAG * t)
183 { U16 res;
184   swf_ResetReadBits(t);
185   #ifdef DEBUG_RFXSWF
186     if (t->pos>(t->len-2)) 
187     { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
188       return 0;
189     }
190   #endif
191   res = t->data[t->pos] | (t->data[t->pos+1]<<8);
192   t->pos+=2;
193   return res;
194 }
195
196 U32 swf_GetU32(TAG * t)
197 { U32 res;
198   swf_ResetReadBits(t);
199   #ifdef DEBUG_RFXSWF
200     if (t->pos>(t->len-4)) 
201     { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
202       return 0;
203     }
204   #endif
205   res = t->data[t->pos]        | (t->data[t->pos+1]<<8) | 
206        (t->data[t->pos+2]<<16) | (t->data[t->pos+3]<<24);
207   t->pos+=4;
208   return res;
209 }
210
211 int swf_GetBlock(TAG * t,U8 * b,int l)
212 // returns number of bytes written (<=l)
213 // b = NULL -> skip data
214 { swf_ResetReadBits(t);
215   if ((t->len-t->pos)<l) l=t->len-t->pos;
216   if (b && l) memcpy(b,&t->data[t->pos],l);
217   t->pos+=l;
218   return l;
219 }
220
221 int swf_SetBlock(TAG * t,U8 * b,int l)
222 // Appends Block to the end of Tagdata, returns size
223 { U32 newlen = t->len + l;
224   swf_ResetWriteBits(t);
225   if (newlen>t->memsize)
226   { U32  newmem  = MEMSIZE(newlen);  
227     U8 * newdata = (U8*)(rfx_realloc(t->data,newmem));
228     t->memsize = newmem;
229     t->data    = newdata;
230   }
231   if (b) memcpy(&t->data[t->len],b,l);
232   else memset(&t->data[t->len],0x00,l);
233   t->len+=l;
234   return l;
235 }
236
237 int swf_SetU8(TAG * t,U8 v)
238 { swf_ResetWriteBits(t);
239   if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
240   t->data[t->len++] = v;
241   return 0;
242 }
243
244 int swf_SetU16(TAG * t,U16 v)
245 { U8 a[2];
246   a[0] = v&0xff;
247   a[1] = v>>8;
248   
249   swf_ResetWriteBits(t);
250   if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
251   t->data[t->len++] = a[0];
252   t->data[t->len++] = a[1];
253   return 0;
254 }
255 void swf_SetS16(TAG * t,int v)
256 {
257     if(v>32767 || v<-32768) {
258         fprintf(stderr, "Warning: S16 overflow: %d\n", v);
259     }
260     swf_SetU16(t, (S16)v);
261 }
262
263 int swf_SetU32(TAG * t,U32 v)
264 { U8 a[4];
265   a[0] = v&0xff;        // to ensure correct handling of non-intel byteorder
266   a[1] = (v>>8)&0xff;
267   a[2] = (v>>16)&0xff;
268   a[3] = (v>>24)&0xff;
269   
270   swf_ResetWriteBits(t);
271   if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
272   t->data[t->len++] = a[0];
273   t->data[t->len++] = a[1];
274   t->data[t->len++] = a[2];
275   t->data[t->len++] = a[3];
276   return 0;
277 }
278
279 U32 swf_GetBits(TAG * t,int nbits)
280 { U32 res = 0;
281   if (!nbits) return 0;
282   if (!t->readBit) t->readBit = 0x80;
283   while (nbits)
284   { res<<=1;
285     if (t->data[t->pos]&t->readBit) res|=1;
286     t->readBit>>=1;
287     nbits--;
288     if (!t->readBit)
289     { if (nbits) t->readBit = 0x80;
290       #ifdef DEBUG_RFXSWF
291       if (t->pos>=t->len) 
292       { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
293         return res;
294       }
295       #endif
296       t->pos++;
297     }
298   }
299   return res;
300 }
301
302 S32 swf_GetSBits(TAG * t,int nbits)
303 { U32 res = swf_GetBits(t,nbits);
304   if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);  
305   return (S32)res;
306 }
307
308 U32 reader_GetBits(struct reader_t*reader, int nbits)
309 { return reader_readbits(reader, nbits);
310 }
311 S32 reader_GetSBits(struct reader_t*reader, int nbits)
312 { U32 res = reader_readbits(reader, nbits);
313   if (res&(1<<(nbits-1))) res|=(0xffffffff<<nbits);  
314   return (S32)res;
315 }
316
317 int swf_SetBits(TAG * t,U32 v,int nbits)
318 { U32 bm = 1<<(nbits-1);
319
320   while (nbits)
321   { if (!t->writeBit)
322     { if (FAILED(swf_SetU8(t,0))) return -1;
323       t->writeBit = 0x80;
324     }
325     if (v&bm) t->data[t->len-1] |= t->writeBit;
326     bm>>=1;
327     t->writeBit>>=1;
328     nbits--;
329   }
330   return 0;
331 }
332
333 // Advanced Data Access Functions
334
335 int swf_SetRGB(TAG * t,RGBA * col)
336 { if (!t) return -1;
337   if (col)
338   { swf_SetU8(t,col->r);
339     swf_SetU8(t,col->g);
340     swf_SetU8(t,col->b);
341   } else swf_SetBlock(t,NULL,3);
342   return 0;
343 }
344 void swf_GetRGB(TAG * t, RGBA * col)
345 {
346     RGBA dummy;
347     if(!col)
348         col = &dummy;
349     col->r = swf_GetU8(t);
350     col->g = swf_GetU8(t);
351     col->b = swf_GetU8(t);
352     col->a = 255;
353 }
354
355 int swf_SetRGBA(TAG * t,RGBA * col)
356 { if (!t) return -1;
357   if (col)
358   { swf_SetU8(t,col->r);
359     swf_SetU8(t,col->g);
360     swf_SetU8(t,col->b);
361     swf_SetU8(t,col->a);
362   } else swf_SetBlock(t,NULL,4);
363   return 0;
364 }
365 void swf_GetRGBA(TAG * t, RGBA * col)
366 {
367     RGBA dummy;
368     if(!col)
369         col = &dummy;
370     col->r = swf_GetU8(t);
371     col->g = swf_GetU8(t);
372     col->b = swf_GetU8(t);
373     col->a = swf_GetU8(t);
374 }
375
376 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
377 {
378     GRADIENT dummy;
379     int t;
380     if(!tag) {
381       memset(gradient, 0, sizeof(GRADIENT));
382       return;
383     }
384     if(!gradient)
385         gradient = &dummy;
386     gradient->num = swf_GetU8(tag);
387     for(t=0;t<gradient->num;t++)
388     {
389         int s=t;
390         if(s>=8) //FIXME
391             s=7;
392         gradient->ratios[t] = swf_GetU8(tag);
393         if(!alpha)
394             swf_GetRGB(tag, &gradient->rgba[t]);
395         else
396             swf_GetRGBA(tag, &gradient->rgba[t]);
397     }
398 }
399
400 void swf_SetGradient(TAG * tag, GRADIENT * gradient, char alpha)
401 {
402     int t;
403     if(!tag) {
404       memset(gradient, 0, sizeof(GRADIENT));
405       return;
406     }
407     swf_SetU8(tag, gradient->num);
408     for(t=0; t<8 && t<gradient->num; t++)
409     {
410         swf_SetU8(tag, gradient->ratios[t]);
411         if(!alpha)
412             swf_SetRGB(tag, &gradient->rgba[t]);
413         else
414             swf_SetRGBA(tag, &gradient->rgba[t]);
415     }
416 }
417
418 int swf_CountUBits(U32 v,int nbits)
419 { int n = 32;
420   U32 m = 0x80000000;
421   if(v == 0x00000000) n = 0; 
422   else
423     while (!(v&m))
424     { n--;
425       m>>=1;
426     } 
427   return (n>nbits)?n:nbits;
428 }
429
430 int swf_CountBits(U32 v,int nbits)
431 { int n = 33;
432   U32 m = 0x80000000;
433   if (v&m)
434   { if(v == 0xffffffff) n = 1;
435     else 
436     while (v&m)
437     { n--;
438       m>>=1;
439     } 
440   }
441   else
442   { if(v == 0x00000000) n = 0; 
443     else
444     while (!(v&m))
445     { n--;
446       m>>=1;
447     } 
448   }
449   return (n>nbits)?n:nbits;
450 }
451
452 int swf_GetRect(TAG * t,SRECT * r)
453 { int nbits;
454   SRECT dummy;
455   if(!t) {r->xmin=r->xmax=r->ymin=r->ymax=0;return 0;}
456   if (!r) r = &dummy;
457   nbits = (int) swf_GetBits(t,5);
458   r->xmin = swf_GetSBits(t,nbits);
459   r->xmax = swf_GetSBits(t,nbits);
460   r->ymin = swf_GetSBits(t,nbits);
461   r->ymax = swf_GetSBits(t,nbits);
462   return 0;
463 }
464
465 int reader_GetRect(struct reader_t*reader,SRECT * r)
466 { int nbits;
467   SRECT dummy;
468   if (!r) r = &dummy;
469   nbits = (int) reader_GetBits(reader,5);
470   r->xmin = reader_GetSBits(reader,nbits);
471   r->xmax = reader_GetSBits(reader,nbits);
472   r->ymin = reader_GetSBits(reader,nbits);
473   r->ymax = reader_GetSBits(reader,nbits);
474   return 0;
475 }
476
477 int swf_SetRect(TAG * t,SRECT * r)
478 { int nbits;
479     
480   nbits = swf_CountBits(r->xmin,0);
481   nbits = swf_CountBits(r->xmax,nbits);
482   nbits = swf_CountBits(r->ymin,nbits);
483   nbits = swf_CountBits(r->ymax,nbits);
484   if(nbits>=32) {
485     fprintf(stderr, "rfxswf: Warning: num_bits overflow in swf_SetRect\n");
486     nbits=31;
487   }
488
489   swf_SetBits(t,nbits,5);
490   swf_SetBits(t,r->xmin,nbits);
491   swf_SetBits(t,r->xmax,nbits);
492   swf_SetBits(t,r->ymin,nbits);
493   swf_SetBits(t,r->ymax,nbits);
494
495   return 0;
496 }
497
498 void swf_ExpandRect(SRECT*src, SPOINT add)
499 {
500     if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
501         src->xmin = add.x;
502         src->ymin = add.y;
503         src->xmax = add.x;
504         src->ymax = add.y;
505         if((add.x|add.y) == 0) src->xmax++; //make sure the bbox is not NULL anymore
506         return;
507     }
508     if(add.x < src->xmin)
509         src->xmin = add.x;
510     if(add.x > src->xmax)
511         src->xmax = add.x;
512     if(add.y < src->ymin)
513         src->ymin = add.y;
514     if(add.y > src->ymax)
515         src->ymax = add.y;
516 }
517 void swf_ExpandRect2(SRECT*src, SRECT*add)
518 {
519     if((add->xmin | add->ymin | add->xmax | add->ymax)==0)
520         return;
521     if((src->xmin | src->ymin | src->xmax | src->ymax)==0)
522         *src = *add;
523     if(add->xmin < src->xmin)
524         src->xmin = add->xmin;
525     if(add->ymin < src->ymin)
526         src->ymin = add->ymin;
527     if(add->xmax > src->xmax)
528         src->xmax = add->xmax;
529     if(add->ymax > src->ymax)
530         src->ymax = add->ymax;
531 }
532 void swf_ExpandRect3(SRECT*src, SPOINT center, int radius)
533 {
534     if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
535         src->xmin = center.x-radius;
536         src->ymin = center.y-radius;
537         src->xmax = center.x+radius;
538         src->ymax = center.y+radius;
539         if((center.x|center.y|radius) == 0) src->xmax++; //make sure the bbox is not NULL anymore
540         return;
541     }
542     if(center.x - radius < src->xmin)
543         src->xmin = center.x - radius;
544     if(center.x + radius > src->xmax)
545         src->xmax = center.x + radius;
546     if(center.y - radius < src->ymin)
547         src->ymin = center.y - radius;
548     if(center.y + radius > src->ymax)
549         src->ymax = center.y + radius;
550 }
551 SPOINT swf_TurnPoint(SPOINT p, MATRIX* m)
552 {
553     SPOINT r;
554     r.x = (int)(m->sx*(1/65536.0)*p.x + m->r1*(1/65536.0)*p.y + 0.5) + m->tx;
555     r.y = (int)(m->r0*(1/65536.0)*p.x + m->sy*(1/65536.0)*p.y + 0.5) + m->ty;
556     return r;
557 }
558 SRECT swf_TurnRect(SRECT r, MATRIX* m)
559 {
560     SRECT g;
561     SPOINT p1,p2,p3,p4,pp1,pp2,pp3,pp4;
562     if(!m)
563       return r;
564     p1.x = r.xmin;p1.y = r.ymin;
565     p2.x = r.xmax;p2.y = r.ymin;
566     p3.x = r.xmin;p3.y = r.ymax;
567     p4.x = r.xmax;p4.y = r.ymax;
568     pp1 = swf_TurnPoint(p1, m);
569     pp2 = swf_TurnPoint(p2, m);
570     pp3 = swf_TurnPoint(p3, m);
571     pp4 = swf_TurnPoint(p4, m);
572     g.xmin = g.xmax = pp1.x;
573     g.ymin = g.ymax = pp1.y;
574     swf_ExpandRect(&g, pp2);
575     swf_ExpandRect(&g, pp3);
576     swf_ExpandRect(&g, pp4);
577     return g;
578 }
579         
580
581 int swf_GetMatrix(TAG * t,MATRIX * m)
582 { MATRIX dummy;
583   int nbits;
584     
585   if (!m) m = &dummy;
586   
587   if (!t)
588   { m->sx = m->sy = 0x10000;
589     m->r0 = m->r1 = 0;
590     m->tx = m->ty = 0;
591     return -1;
592   }
593
594   swf_ResetReadBits(t);
595   
596   if (swf_GetBits(t,1))
597   { nbits = swf_GetBits(t,5);
598     m->sx = swf_GetSBits(t,nbits);
599     m->sy = swf_GetSBits(t,nbits);
600   }
601   else m->sx = m->sy = 0x10000;
602   
603   if (swf_GetBits(t,1))
604   { nbits = swf_GetBits(t,5);
605     m->r0 = swf_GetSBits(t,nbits);
606     m->r1 = swf_GetSBits(t,nbits);
607   }
608   else m->r0 = m->r1 = 0x0;
609
610   nbits = swf_GetBits(t,5);
611   m->tx = swf_GetSBits(t,nbits);
612   m->ty = swf_GetSBits(t,nbits);
613   
614   return 0;
615 }
616
617 int swf_SetMatrix(TAG * t,MATRIX * m)
618 { int nbits;
619   MATRIX ma;
620
621   if (!m)
622   { m = &ma;
623     ma.sx = ma.sy = 0x10000;
624     ma.r0 = ma.r1 = 0;
625     ma.tx = ma.ty = 0;
626   }
627
628   swf_ResetWriteBits(t);
629
630   if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
631   else
632   { swf_SetBits(t,1,1);
633     nbits = swf_CountBits(m->sx,0);
634     nbits = swf_CountBits(m->sy,nbits);
635     if(nbits>=32) {
636         fprintf(stderr,"rfxswf: Error: matrix values too large\n");
637         nbits = 31;
638     }
639     swf_SetBits(t,nbits,5);
640     swf_SetBits(t,m->sx,nbits);
641     swf_SetBits(t,m->sy,nbits);
642   }
643
644   if ((!m->r0)&&(!m->r1)) swf_SetBits(t,0,1);
645   else
646   { swf_SetBits(t,1,1);
647     nbits = swf_CountBits(m->r0,0);
648     nbits = swf_CountBits(m->r1,nbits);
649     if(nbits>=32) {
650         fprintf(stderr,"rfxswf: Error: matrix values too large\n");
651         nbits = 31;
652     }
653     swf_SetBits(t,nbits,5);
654     swf_SetBits(t,m->r0,nbits);
655     swf_SetBits(t,m->r1,nbits);
656   }
657
658   nbits = swf_CountBits(m->tx,0);
659   nbits = swf_CountBits(m->ty,nbits);
660   if(nbits>=32) {
661       fprintf(stderr,"rfxswf: Error: matrix values too large\n");
662       nbits = 31;
663   }
664   swf_SetBits(t,nbits,5);
665   swf_SetBits(t,m->tx,nbits);
666   swf_SetBits(t,m->ty,nbits);
667
668   return 0;
669 }
670
671 int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha) //FIXME: alpha should be type bool
672 { CXFORM cxf;
673   int hasadd;
674   int hasmul;
675   int nbits;
676     
677   if (!cx) cx = &cxf;
678   
679   cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
680   cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
681
682   if (!t) return 0;
683   
684   swf_ResetReadBits(t);
685   hasadd = swf_GetBits(t,1);
686   hasmul = swf_GetBits(t,1);
687   nbits  = swf_GetBits(t,4);
688
689   if (hasmul)
690   { cx->r0 = (S16)swf_GetSBits(t,nbits);
691     cx->g0 = (S16)swf_GetSBits(t,nbits);
692     cx->b0 = (S16)swf_GetSBits(t,nbits);
693     if (alpha)
694       cx->a0 = (S16)swf_GetSBits(t,nbits);
695   }
696
697   if (hasadd)
698   { cx->r1 = (S16)swf_GetSBits(t,nbits);
699     cx->g1 = (S16)swf_GetSBits(t,nbits);
700     cx->b1 = (S16)swf_GetSBits(t,nbits);
701     if (alpha)
702       cx->a1 = (S16)swf_GetSBits(t,nbits);
703   }
704   
705   return 0;
706 }
707
708 int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
709 { CXFORM cxf;
710   int hasadd;
711   int hasmul;
712   int nbits;
713     
714   if (!cx)
715   { cx = &cxf;
716     cx->a0 = cx->r0 = cx->g0 = cx->b0 = 256;
717     cx->a1 = cx->r1 = cx->g1 = cx->b1 = 0;
718   }
719
720   if (!alpha)
721   { cx->a0 = 256;
722     cx->a1 = 0;
723   }
724
725   nbits = 0;
726
727   hasmul = (cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256);
728   hasadd = cx->a1|cx->r1|cx->g1|cx->b1;
729
730   if (hasmul)
731   { if (alpha) nbits = swf_CountBits((S32)cx->a0,nbits);
732     nbits = swf_CountBits((S32)cx->r0,nbits);
733     nbits = swf_CountBits((S32)cx->g0,nbits);
734     nbits = swf_CountBits((S32)cx->b0,nbits);
735   }
736
737   if (hasadd)
738   { if (alpha) nbits = swf_CountBits((S32)cx->a1,nbits);
739     nbits = swf_CountBits((S32)cx->r1,nbits);
740     nbits = swf_CountBits((S32)cx->g1,nbits);
741     nbits = swf_CountBits((S32)cx->b1,nbits);
742   }
743   
744   swf_ResetWriteBits(t);
745   swf_SetBits(t,hasadd?1:0,1);
746   swf_SetBits(t,hasmul?1:0,1);
747   swf_SetBits(t,nbits,4);
748
749   if (hasmul)
750   { swf_SetBits(t,cx->r0,nbits);
751     swf_SetBits(t,cx->g0,nbits);
752     swf_SetBits(t,cx->b0,nbits);
753     if (alpha) swf_SetBits(t,cx->a0,nbits);
754   }
755
756   if (hasadd)
757   { swf_SetBits(t,cx->r1,nbits);
758     swf_SetBits(t,cx->g1,nbits);
759     swf_SetBits(t,cx->b1,nbits);
760     if (alpha) swf_SetBits(t,cx->a1,nbits);
761   }
762   
763   return 0;
764 }
765
766 //int swf_GetPoint(TAG * t,SPOINT * p) { return 0; }
767 //int swf_SetPoint(TAG * t,SPOINT * p) { return 0; }
768
769 void  swf_SetPassword(TAG * t, const char * password)
770 {
771     /* WARNING: crypt_md5 is not reentrant */
772     char salt[3];
773     char* md5string;
774
775 #if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) && defined(HAVE_TIME_H) && defined(HAVE_TIME)
776     srand48(time(0));
777     salt[0] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
778     salt[1] = "abcdefghijklmnopqrstuvwxyz0123456789"[lrand48()%36];
779 #else
780     salt[0] = 'l';
781     salt[1] = '8';
782     fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
783     fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
784 #endif
785     
786     md5string = crypt_md5(password, salt);
787
788     swf_SetU16(t,0);
789     swf_SetString(t, md5string);
790 }
791
792 int swf_VerifyPassword(TAG * t, const char * password)
793 {
794     char*md5string1, *md5string2;
795     char*x;
796     char*md5, *salt;
797     int n;
798
799     if(t->len >= 5 && t->pos==0 && 
800        t->data[0] == 0 &&
801        t->data[1] == 0) {
802       swf_GetU16(t);
803     } else {
804       printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
805     }
806
807     md5string1 = swf_GetString(t);
808
809     if(strncmp(md5string1, "$1$",3 )) {
810         fprintf(stderr, "rfxswf: no salt in pw string\n");
811         return 0;
812     }
813     x = strchr(md5string1+3, '$');
814     if(!x) {
815         fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
816         return 0;
817     }
818     n = x-(md5string1+3);
819     salt = (char*)rfx_alloc(n+1);
820     memcpy(salt, md5string1+3, n);
821     salt[n] = 0;
822
823     md5string2 = crypt_md5(password, salt);
824     rfx_free(salt);
825     if(strcmp(md5string1, md5string2) != 0)
826         return 0;
827     return 1;
828 }
829
830 // Tag List Manipulating Functions
831
832 TAG * swf_InsertTag(TAG * after,U16 id)
833 { TAG * t;
834
835   t = (TAG *)rfx_calloc(sizeof(TAG));
836   t->id = id;
837   
838   if (after)
839   {
840     t->prev  = after;
841     t->next  = after->next;
842     after->next = t;
843     if (t->next) t->next->prev = t;
844   }
845   return t;
846 }
847
848 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
849 { TAG * t;
850
851   t = (TAG *)rfx_calloc(sizeof(TAG));
852   t->id = id;
853   
854   if (before)
855   {
856     t->next  = before;
857     t->prev  = before->prev;
858     before->prev = t;
859     if (t->prev) t->prev->next = t;
860   }
861   if(swf && swf->firstTag == before) {
862     swf->firstTag = t;
863   }
864   return t;
865 }
866
867 void swf_ClearTag(TAG * t)
868 {
869   if (t->data) rfx_free(t->data);
870   t->data = 0;
871   t->pos = 0;
872   t->len = 0;
873   t->readBit = 0;
874   t->writeBit = 0;
875   t->memsize = 0;
876 }
877
878 void swf_ResetTag(TAG*tag, U16 id)
879 {
880     tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
881     tag->id = id;
882 }
883
884 TAG* swf_CopyTag(TAG*tag, TAG*to_copy)
885 {
886     tag = swf_InsertTag(tag, to_copy->id);
887     swf_SetBlock(tag, to_copy->data, to_copy->len);
888     return tag;
889 }
890
891 int swf_DeleteTag(TAG * t)
892 { if (!t) return -1;
893
894   if (t->prev) t->prev->next = t->next;
895   if (t->next) t->next->prev = t->prev;
896
897   if (t->data) rfx_free(t->data);
898   rfx_free(t);
899   return 0;
900 }
901
902 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
903 { TAG * t;
904   U16 raw;
905   U32 len;
906   int id;
907
908   if (reader->read(reader, &raw, 2) !=2 ) return NULL;
909   raw = SWAP16(raw);
910
911   len = raw&0x3f;
912   id  = raw>>6;
913
914   if (len==0x3f)
915   {
916       if (reader->read(reader, &len, 4) != 4) return NULL;
917       len = SWAP32(len);
918   }
919
920   if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
921   // Sprite handling fix: Flatten sprite tree
922
923   t = (TAG *)rfx_calloc(sizeof(TAG));
924   
925   t->len = len;
926   t->id  = id;
927
928   if (t->len)
929   { t->data = (U8*)rfx_alloc(t->len);
930     t->memsize = t->len;
931     if (reader->read(reader, t->data, t->len) != t->len) {
932       fprintf(stderr, "rfxswf: Warning: Short read (tagid %d). File truncated?\n", t->id);
933       free(t->data);t->data=0;
934       free(t);
935       return NULL;
936     }
937   }
938
939   if (prev)
940   {
941     t->prev  = prev;
942     prev->next = t;
943   }
944
945   return t;
946 }
947
948 int swf_DefineSprite_GetRealSize(TAG * t);
949
950 int swf_WriteTag2(struct writer_t*writer, TAG * t)
951 // returns tag length in bytes (incl. Header), -1 = Error
952 // writer = 0 -> no output
953 { U16 raw[3];
954   U32 len;
955   int short_tag;
956
957   if (!t) return -1;
958
959   len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
960
961   short_tag = len<0x3f&&(t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2);
962
963   if (writer)
964   { if (short_tag)
965     { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
966       if (writer->write(writer,raw,2)!=2)
967       {
968         #ifdef DEBUG_RFXSWF
969           fprintf(stderr,"WriteTag() failed: Short Header.\n");
970         #endif
971         return -1;
972       }
973     }
974     else
975     {
976       raw[0] = SWAP16((t->id<<6)|0x3f);
977       if (writer->write(writer,raw,2)!=2)
978       {
979 #ifdef DEBUG_RFXSWF
980           fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
981 #endif
982           return -1;
983       }
984       
985       len = SWAP32(len);
986       if (writer->write(writer,&len,4)!=4)
987       {
988         #ifdef DEBUG_RFXSWF
989           fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
990         #endif
991         return -1;
992       }
993     }
994     
995     if (t->data)
996     { if (writer->write(writer,t->data,t->len)!=t->len)
997       {
998         #ifdef DEBUG_RFXSWF
999           fprintf(stderr,"WriteTag() failed: Data.\n");
1000         #endif
1001         return -1;
1002       }
1003     }
1004     #ifdef DEBUG_RFXSWF
1005       else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
1006     #endif
1007   }
1008
1009   return t->len+(short_tag?2:6);
1010 }
1011
1012 int swf_WriteTag(int handle, TAG * t)
1013 {
1014   struct writer_t writer;
1015   int len = 0;
1016   if(handle<0)
1017     return swf_WriteTag2(0, t);
1018   writer_init_filewriter(&writer, handle);
1019   len = swf_WriteTag2(&writer, t);
1020   writer.finish(&writer);
1021   return len;
1022 }
1023
1024 int swf_DefineSprite_GetRealSize(TAG * t)
1025 // Sprite Handling: Helper function to pack DefineSprite-Tag
1026 { U32 len = t->len;
1027   if(len>4) { // folded sprite
1028       return t->len;
1029   }
1030   do
1031   { t = swf_NextTag(t);
1032     if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
1033     else t = NULL;
1034   } while (t&&(t->id!=ST_END));
1035   return len;
1036 }
1037
1038 void swf_UnFoldSprite(TAG * t)
1039 {
1040   U16 id,tmp;
1041   U32 len;
1042   TAG*next = t;
1043   U16 spriteid,spriteframes;
1044   int level;
1045   if(t->id!=ST_DEFINESPRITE)
1046     return;
1047   if(t->len<=4) // not folded
1048     return;
1049
1050   swf_SetTagPos(t,0);
1051
1052   spriteid = swf_GetU16(t); //id
1053   spriteframes = swf_GetU16(t); //frames
1054
1055   level = 1;
1056
1057   while(1)
1058   {
1059     TAG*it = 0;
1060     tmp = swf_GetU16(t);
1061     len = tmp&0x3f;
1062     id  = tmp>>6;
1063     if(id == ST_END)
1064         level--;
1065     if(id == ST_DEFINESPRITE && len<=4)
1066         level++;
1067
1068     if (len==0x3f)
1069         len = swf_GetU32(t);
1070     it = swf_InsertTag(next, id);
1071     next = it;
1072     it->len = len;
1073     it->id  = id;
1074     if (it->len)
1075     { it->data = (U8*)rfx_alloc(it->len);
1076       it->memsize = it->len;
1077       swf_GetBlock(t, it->data, it->len);
1078     }
1079
1080     if(!level)
1081         break;
1082   }
1083   
1084   rfx_free(t->data); t->data = 0;
1085   t->memsize = t->len = t->pos = 0;
1086
1087   swf_SetU16(t, spriteid);
1088   swf_SetU16(t, spriteframes);
1089 }
1090
1091 void swf_FoldSprite(TAG * t)
1092 {
1093   TAG*sprtag=t,*tmp;
1094   U16 id,frames,tmpid;
1095   int level;
1096   if(t->id!=ST_DEFINESPRITE)
1097       return;
1098   if(!t->len) {
1099       fprintf(stderr, "Error: Sprite has no ID!");
1100       return;
1101   }
1102   if(t->len>4) {
1103     /* sprite is already folded */
1104       return;
1105   }
1106
1107   t->pos = 0;
1108   id = swf_GetU16(t);
1109   rfx_free(t->data);
1110   t->len = t->pos = t->memsize = 0;
1111   t->data = 0;
1112
1113   frames = 0;
1114
1115   t = swf_NextTag(sprtag);
1116   level = 1;
1117
1118   do 
1119   { 
1120     if(t->id==ST_SHOWFRAME) frames++;
1121     if(t->id == ST_DEFINESPRITE && t->len<=4)
1122         level++;
1123     if(t->id == ST_END)
1124         level--;
1125     t = swf_NextTag(t);
1126   } while(t && level);
1127   if(level)
1128     fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1129
1130   swf_SetU16(sprtag, id);
1131   swf_SetU16(sprtag, frames);
1132
1133   t = swf_NextTag(sprtag);
1134   level = 1;
1135
1136   do
1137   { 
1138     if(t->len<0x3f&&t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2) {
1139         swf_SetU16(sprtag,t->len|(t->id<<6));
1140     } else {
1141         swf_SetU16(sprtag,0x3f|(t->id<<6));
1142         swf_SetU32(sprtag,t->len);
1143     }
1144     if(t->len)
1145         swf_SetBlock(sprtag,t->data, t->len);
1146     tmp = t;
1147     if(t->id == ST_DEFINESPRITE && t->len<=4)
1148         level++;
1149     if(t->id == ST_END)
1150         level--;
1151     t = swf_NextTag(t);
1152     swf_DeleteTag(tmp);
1153   } 
1154   while (t && level);
1155   if(level)
1156     fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1157
1158 //  sprtag->next = t;
1159 //  t->prev = sprtag;
1160 }
1161
1162 int swf_IsFolded(TAG * t)
1163 {
1164     return (t->id == ST_DEFINESPRITE && t->len>4);
1165 }
1166
1167 void swf_FoldAll(SWF*swf)
1168 {
1169     TAG*tag = swf->firstTag;
1170     //swf_DumpSWF(stdout, swf);
1171     while(tag) {
1172         if(tag->id == ST_DEFINESPRITE) {
1173             swf_FoldSprite(tag);
1174             //swf_DumpSWF(stdout, swf);
1175         }
1176         tag = swf_NextTag(tag);
1177     }
1178 }
1179
1180 void swf_UnFoldAll(SWF*swf)
1181 {
1182     TAG*tag = swf->firstTag;
1183     while(tag) {
1184         if(tag->id == ST_DEFINESPRITE)
1185             swf_UnFoldSprite(tag);
1186         tag = tag->next;
1187     }
1188 }
1189
1190 void swf_OptimizeTagOrder(SWF*swf)
1191 {
1192   TAG*tag,*next;
1193   TAG*level0;
1194   int level;
1195   int changes;
1196   swf_UnFoldAll(swf);
1197   /* at the moment, we don't actually do optimizing,
1198      only fixing of non-spec-conformant things like
1199      sprite tags */
1200
1201   do {
1202     changes = 0;
1203     level = 0;
1204     level0 = 0;
1205     tag = swf->firstTag;
1206     while(tag) {
1207       next = tag->next;
1208       if(tag->id == ST_DEFINESPRITE) {
1209         if(tag->len>4) {
1210           /* ??? all sprites are supposed to be unfolded */
1211           fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1212         }
1213         level++;
1214         if(level==1) {
1215           level0 = tag;
1216           tag = next;
1217           continue;
1218         }
1219       }
1220       if(level>=1) {
1221         /* move non-sprite tags out of sprite */
1222         if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1223           /* remove tag from current position */
1224           tag->prev->next = tag->next;
1225           if(tag->next)
1226             tag->next->prev = tag->prev;
1227
1228           /* insert before tag level0 */
1229           tag->next = level0;
1230           tag->prev = level0->prev;
1231           level0->prev = tag;
1232           if(tag->prev)
1233             tag->prev->next = tag;
1234           else
1235             swf->firstTag = tag;
1236           changes = 1;
1237         }
1238       }
1239       if(tag->id == ST_END) {
1240         level--;
1241       }
1242
1243       tag = next;
1244     }
1245   } while(changes);
1246 }
1247
1248 // Movie Functions
1249
1250 int swf_ReadSWF2(struct reader_t*reader, SWF * swf)   // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1251 {     
1252   if (!swf) return -1;
1253   memset(swf,0x00,sizeof(SWF));
1254
1255   { char b[32];                         // read Header
1256     int len;
1257     TAG * t;
1258     TAG t1;
1259     struct reader_t zreader;
1260     
1261     if ((len = reader->read(reader ,b,8))<8) return -1;
1262
1263     if (b[0]!='F' && b[0]!='C') return -1;
1264     if (b[1]!='W') return -1;
1265     if (b[2]!='S') return -1;
1266     swf->fileVersion = b[3];
1267     swf->compressed  = (b[0]=='C')?1:0;
1268     swf->fileSize    = GET32(&b[4]);
1269     
1270     if(swf->compressed) {
1271         reader_init_zlibinflate(&zreader, reader);
1272         reader = &zreader;
1273     }
1274
1275     reader_GetRect(reader, &swf->movieSize);
1276     reader->read(reader, &swf->frameRate, 2);
1277     swf->frameRate = SWAP16(swf->frameRate);
1278     reader->read(reader, &swf->frameCount, 2);
1279     swf->frameCount = SWAP16(swf->frameCount);
1280
1281     /* read tags and connect to list */
1282     t = &t1;
1283     while (t) t = swf_ReadTag(reader,t);
1284     swf->firstTag = t1.next;
1285     t1.next->prev = NULL;
1286   }
1287   
1288   return reader->pos;
1289 }
1290
1291 int swf_ReadSWF(int handle, SWF * swf)
1292 {
1293   struct reader_t reader;
1294   reader_init_filereader(&reader, handle);
1295   return swf_ReadSWF2(&reader, swf);
1296 }
1297
1298 int  swf_WriteSWF2(struct writer_t*writer, SWF * swf)     // Writes SWF to file, returns length or <0 if fails
1299 { U32 len;
1300   TAG * t;
1301   int frameCount=0;
1302   struct writer_t zwriter;
1303   int fileSize = 0;
1304   int inSprite = 0;
1305   int writer_lastpos = 0;
1306   int ret;
1307     
1308   if (!swf) return -1;
1309   if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1310
1311   if(writer) writer_lastpos = writer->pos;
1312
1313   // Insert REFLEX Tag
1314
1315 #ifdef INSERT_RFX_TAG
1316
1317   if (swf->firstTag && swf->firstTag->next &&
1318       (swf->firstTag->id != ST_REFLEX || swf->firstTag->next->id != ST_REFLEX)
1319      ) {
1320       swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),"rfx",3);
1321   }
1322
1323 #endif // INSERT_RFX_TAG
1324
1325   // Count Frames + File Size
1326
1327   len = 0;
1328   t = swf->firstTag;
1329   frameCount = 0;
1330
1331   while(t) {
1332       len += swf_WriteTag(-1,t);
1333       if(t->id == ST_DEFINESPRITE && !swf_IsFolded(t)) inSprite++;
1334       else if(t->id == ST_END && inSprite) inSprite--;
1335       else if(t->id == ST_END && !inSprite) {
1336         if(t->prev && t->prev->id!=ST_SHOWFRAME)
1337           frameCount++;
1338       }
1339       else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1340       t = swf_NextTag(t);
1341   }
1342   
1343   { TAG t1;
1344     char b[64],b4[4];
1345     U32 l;
1346
1347     memset(&t1,0x00,sizeof(TAG));
1348     t1.data    = (U8*)b;
1349     t1.memsize = 64;
1350     
1351     { // measure header file size
1352       TAG t2;
1353       char b2[64];
1354       memset(&t2,0x00,sizeof(TAG));
1355       t2.data    = (U8*)b2;
1356       t2.memsize = 64;
1357       swf_SetRect(&t2, &swf->movieSize);
1358       swf_SetU16(&t2, swf->frameRate);
1359       swf_SetU16(&t2, swf->frameCount);
1360       l = swf_GetTagLen(&t2)+8;
1361     }
1362     if(swf->compressed == 8) {
1363       l -= 8;
1364     }
1365
1366     fileSize = l+len;
1367     if(len) {// don't touch headers without tags
1368         swf->fileSize = fileSize;
1369         swf->frameCount = frameCount;
1370     }
1371
1372     if(swf->compressed != 8) {
1373     /* compressed flag set to 8 means "skip first 8 
1374        header bytes". This is necessary if the caller wants to
1375        create compressed SWFs himself .
1376        It also means that we don't initialize our own zlib
1377        writer, but assume the caller provided one.
1378      */
1379       if(swf->compressed) {
1380         char*id = "CWS";
1381         writer->write(writer, id, 3);
1382       }
1383       else {
1384         char*id = "FWS";
1385         writer->write(writer, id, 3);
1386       }
1387
1388       writer->write(writer, &swf->fileVersion, 1);
1389       PUT32(b4, swf->fileSize);
1390       writer->write(writer, b4, 4);
1391       
1392       if(swf->compressed) {
1393         writer_init_zlibdeflate(&zwriter, writer);
1394         writer = &zwriter;
1395       }
1396     }
1397
1398     swf_SetRect(&t1,&swf->movieSize);
1399     swf_SetU16(&t1,swf->frameRate);
1400     swf_SetU16(&t1,swf->frameCount);
1401
1402     ret = writer->write(writer,b,swf_GetTagLen(&t1));
1403     if (ret!=swf_GetTagLen(&t1))
1404     {
1405       #ifdef DEBUG_RFXSWF
1406         fprintf(stderr, "ret:%d\n",ret);
1407         perror("write:");
1408         fprintf(stderr,"WriteSWF() failed: Header.\n");
1409       #endif
1410       return -1;
1411     }
1412
1413     t = swf->firstTag;
1414     while (t)
1415     { if (swf_WriteTag2(writer, t)<0) return -1;
1416       t = swf_NextTag(t);
1417     }
1418     if(swf->compressed) {
1419       if(swf->compressed != 8) {
1420         zwriter.finish(&zwriter);
1421         return writer->pos - writer_lastpos;
1422       }
1423       return (int)fileSize;
1424     } else {
1425       return (int)fileSize;
1426     }
1427   }
1428 }
1429
1430 int  swf_WriteSWF(int handle, SWF * swf)     // Writes SWF to file, returns length or <0 if fails
1431 {
1432   struct writer_t writer;
1433   int len = 0;
1434   swf->compressed = 0;
1435   
1436   if(handle<0) {
1437     writer_init_nullwriter(&writer);
1438     len = swf_WriteSWF2(&writer, swf);
1439   }
1440   writer_init_filewriter(&writer, handle);
1441   len = swf_WriteSWF2(&writer, swf);
1442   writer.finish(&writer);
1443   return len;
1444 }
1445
1446 int  swf_WriteSWC(int handle, SWF * swf)     // Writes SWF to file, returns length or <0 if fails
1447 {
1448   struct writer_t writer;
1449   int len = 0;
1450   swf->compressed = 1;
1451
1452   if(handle<0) {
1453     writer_init_nullwriter(&writer);
1454     len = swf_WriteSWF2(&writer, swf);
1455   }
1456   writer_init_filewriter(&writer, handle);
1457   len = swf_WriteSWF2(&writer, swf);
1458   writer.finish(&writer);
1459   return len;
1460 }
1461
1462 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1463 {
1464   SWF myswf;
1465   memcpy(&myswf,swf,sizeof(SWF));
1466   myswf.firstTag = 0;
1467   return swf_WriteSWF2(writer, &myswf);
1468 }
1469
1470 int swf_WriteHeader(int handle,SWF * swf)
1471 {
1472   SWF myswf;
1473   memcpy(&myswf,swf,sizeof(SWF));
1474   myswf.firstTag = 0;
1475   return swf_WriteSWF(handle, &myswf);
1476 }
1477
1478 int swf_WriteCGI(SWF * swf)
1479 { int len;
1480   char s[1024];
1481     
1482   len = swf_WriteSWF(-1,swf);
1483
1484   if (len<0) return -1;
1485
1486   sprintf(s,"Content-type: application/x-shockwave-flash\n"
1487             "Accept-Ranges: bytes\n"
1488             "Content-Length: %lu\n"
1489             "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1490             "\n",len);
1491             
1492   write(fileno(stdout),s,strlen(s));
1493   return swf_WriteSWF(fileno(stdout),swf);
1494 }
1495
1496 SWF* swf_CopySWF(SWF*swf)
1497 {
1498     SWF*nswf = rfx_alloc(sizeof(SWF));
1499     TAG*tag, *ntag;
1500     memcpy(nswf, swf, sizeof(SWF));
1501     nswf->firstTag = 0;
1502     tag = swf->firstTag;
1503     ntag = 0;
1504     while(tag) {
1505         ntag = swf_CopyTag(ntag, tag);
1506         if(!nswf->firstTag)
1507             nswf->firstTag = ntag;
1508         tag = tag->next;
1509     }
1510     return nswf;
1511 }
1512
1513 void swf_FreeTags(SWF * swf)                 // Frees all malloc'ed memory for tags
1514 { TAG * t = swf->firstTag;
1515
1516   while (t)
1517   { TAG * tnew = t->next;
1518     if (t->data) rfx_free(t->data);
1519     rfx_free(t);
1520     t = tnew;
1521   }
1522   swf->firstTag = 0;
1523 }
1524
1525 // include advanced functions
1526
1527 #include "modules/swfdump.c"
1528 #include "modules/swfshape.c"
1529 #include "modules/swftext.c"
1530 #include "modules/swffont.c"
1531 #include "modules/swfobject.c"
1532 #include "modules/swfbutton.c"
1533 #include "modules/swftools.c"
1534 #include "modules/swfcgi.c"
1535 #include "modules/swfbits.c"
1536 #include "modules/swfaction.c"
1537 #include "modules/swfsound.c"
1538 #include "modules/swfdraw.c"
1539 #include "modules/swfrender.c"