added some more info to the "out of memory" messages.
[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 (while trying to claim %d bytes)\n", size);
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 (while trying to claim %d bytes)\n", size);
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 (while trying to claim %d bytes)\n", size);
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)
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     salt[2] = 0;
786     
787     md5string = crypt_md5(password, salt);
788
789     swf_SetU16(t,0);
790     swf_SetString(t, md5string);
791 }
792
793 int swf_VerifyPassword(TAG * t, const char * password)
794 {
795     char*md5string1, *md5string2;
796     char*x;
797     char*md5, *salt;
798     int n;
799
800     if(t->len >= 5 && t->pos==0 && 
801        t->data[0] == 0 &&
802        t->data[1] == 0) {
803       swf_GetU16(t);
804     } else {
805       printf("%d %d %d %d\n", t->len, t->pos, t->data[0], t->data[1]);
806     }
807
808     md5string1 = swf_GetString(t);
809
810     if(strncmp(md5string1, "$1$",3 )) {
811         fprintf(stderr, "rfxswf: no salt in pw string\n");
812         return 0;
813     }
814     x = strchr(md5string1+3, '$');
815     if(!x) {
816         fprintf(stderr, "rfxswf: invalid salt format in pw string\n");
817         return 0;
818     }
819     n = x-(md5string1+3);
820     salt = (char*)rfx_alloc(n+1);
821     memcpy(salt, md5string1+3, n);
822     salt[n] = 0;
823
824     md5string2 = crypt_md5(password, salt);
825     rfx_free(salt);
826     if(strcmp(md5string1, md5string2) != 0)
827         return 0;
828     return 1;
829 }
830
831 // Tag List Manipulating Functions
832
833 TAG * swf_InsertTag(TAG * after,U16 id)
834 { TAG * t;
835
836   t = (TAG *)rfx_calloc(sizeof(TAG));
837   t->id = id;
838   
839   if (after)
840   {
841     t->prev  = after;
842     t->next  = after->next;
843     after->next = t;
844     if (t->next) t->next->prev = t;
845   }
846   return t;
847 }
848
849 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
850 { TAG * t;
851
852   t = (TAG *)rfx_calloc(sizeof(TAG));
853   t->id = id;
854   
855   if (before)
856   {
857     t->next  = before;
858     t->prev  = before->prev;
859     before->prev = t;
860     if (t->prev) t->prev->next = t;
861   }
862   if(swf && swf->firstTag == before) {
863     swf->firstTag = t;
864   }
865   return t;
866 }
867
868 void swf_ClearTag(TAG * t)
869 {
870   if (t->data) rfx_free(t->data);
871   t->data = 0;
872   t->pos = 0;
873   t->len = 0;
874   t->readBit = 0;
875   t->writeBit = 0;
876   t->memsize = 0;
877 }
878
879 void swf_ResetTag(TAG*tag, U16 id)
880 {
881     tag->len = tag->pos = tag->readBit = tag->writeBit = 0;
882     tag->id = id;
883 }
884
885 TAG* swf_CopyTag(TAG*tag, TAG*to_copy)
886 {
887     tag = swf_InsertTag(tag, to_copy->id);
888     swf_SetBlock(tag, to_copy->data, to_copy->len);
889     return tag;
890 }
891
892 int swf_DeleteTag(TAG * t)
893 { if (!t) return -1;
894
895   if (t->prev) t->prev->next = t->next;
896   if (t->next) t->next->prev = t->prev;
897
898   if (t->data) rfx_free(t->data);
899   rfx_free(t);
900   return 0;
901 }
902
903 TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
904 { TAG * t;
905   U16 raw;
906   U32 len;
907   int id;
908
909   if (reader->read(reader, &raw, 2) !=2 ) return NULL;
910   raw = SWAP16(raw);
911
912   len = raw&0x3f;
913   id  = raw>>6;
914
915   if (len==0x3f)
916   {
917       if (reader->read(reader, &len, 4) != 4) return NULL;
918       len = SWAP32(len);
919   }
920
921   if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
922   // Sprite handling fix: Flatten sprite tree
923
924   t = (TAG *)rfx_calloc(sizeof(TAG));
925   
926   t->len = len;
927   t->id  = id;
928
929   if (t->len)
930   { t->data = (U8*)rfx_alloc(t->len);
931     t->memsize = t->len;
932     if (reader->read(reader, t->data, t->len) != t->len) {
933       fprintf(stderr, "rfxswf: Warning: Short read (tagid %d). File truncated?\n", t->id);
934       free(t->data);t->data=0;
935       free(t);
936       return NULL;
937     }
938   }
939
940   if (prev)
941   {
942     t->prev  = prev;
943     prev->next = t;
944   }
945
946   return t;
947 }
948
949 int swf_DefineSprite_GetRealSize(TAG * t);
950
951 int swf_WriteTag2(struct writer_t*writer, TAG * t)
952 // returns tag length in bytes (incl. Header), -1 = Error
953 // writer = 0 -> no output
954 { U16 raw[3];
955   U32 len;
956   int short_tag;
957
958   if (!t) return -1;
959
960   len = (t->id==ST_DEFINESPRITE)?swf_DefineSprite_GetRealSize(t):t->len;
961
962   short_tag = len<0x3f&&(t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2);
963
964   if (writer)
965   { if (short_tag)
966     { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
967       if (writer->write(writer,raw,2)!=2)
968       {
969         #ifdef DEBUG_RFXSWF
970           fprintf(stderr,"WriteTag() failed: Short Header.\n");
971         #endif
972         return -1;
973       }
974     }
975     else
976     {
977       raw[0] = SWAP16((t->id<<6)|0x3f);
978       if (writer->write(writer,raw,2)!=2)
979       {
980 #ifdef DEBUG_RFXSWF
981           fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
982 #endif
983           return -1;
984       }
985       
986       len = SWAP32(len);
987       if (writer->write(writer,&len,4)!=4)
988       {
989         #ifdef DEBUG_RFXSWF
990           fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
991         #endif
992         return -1;
993       }
994     }
995     
996     if (t->data)
997     { if (writer->write(writer,t->data,t->len)!=t->len)
998       {
999         #ifdef DEBUG_RFXSWF
1000           fprintf(stderr,"WriteTag() failed: Data.\n");
1001         #endif
1002         return -1;
1003       }
1004     }
1005     #ifdef DEBUG_RFXSWF
1006       else if (t->len) fprintf(stderr,"WriteTag(): Tag Data Error, id=%i\n",t->id);
1007     #endif
1008   }
1009
1010   return t->len+(short_tag?2:6);
1011 }
1012
1013 int swf_WriteTag(int handle, TAG * t)
1014 {
1015   struct writer_t writer;
1016   int len = 0;
1017   if(handle<0)
1018     return swf_WriteTag2(0, t);
1019   writer_init_filewriter(&writer, handle);
1020   len = swf_WriteTag2(&writer, t);
1021   writer.finish(&writer);
1022   return len;
1023 }
1024
1025 int swf_DefineSprite_GetRealSize(TAG * t)
1026 // Sprite Handling: Helper function to pack DefineSprite-Tag
1027 { U32 len = t->len;
1028   if(len>4) { // folded sprite
1029       return t->len;
1030   }
1031   do
1032   { t = swf_NextTag(t);
1033     if (t && t->id!=ST_DEFINESPRITE) len += swf_WriteTag(-1, t);
1034     else t = NULL;
1035   } while (t&&(t->id!=ST_END));
1036   return len;
1037 }
1038
1039 void swf_UnFoldSprite(TAG * t)
1040 {
1041   U16 id,tmp;
1042   U32 len;
1043   TAG*next = t;
1044   U16 spriteid,spriteframes;
1045   int level;
1046   if(t->id!=ST_DEFINESPRITE)
1047     return;
1048   if(t->len<=4) // not folded
1049     return;
1050
1051   swf_SetTagPos(t,0);
1052
1053   spriteid = swf_GetU16(t); //id
1054   spriteframes = swf_GetU16(t); //frames
1055
1056   level = 1;
1057
1058   while(1)
1059   {
1060     TAG*it = 0;
1061     tmp = swf_GetU16(t);
1062     len = tmp&0x3f;
1063     id  = tmp>>6;
1064     if(id == ST_END)
1065         level--;
1066     if(id == ST_DEFINESPRITE && len<=4)
1067         level++;
1068
1069     if (len==0x3f)
1070         len = swf_GetU32(t);
1071     it = swf_InsertTag(next, id);
1072     next = it;
1073     it->len = len;
1074     it->id  = id;
1075     if (it->len)
1076     { it->data = (U8*)rfx_alloc(it->len);
1077       it->memsize = it->len;
1078       swf_GetBlock(t, it->data, it->len);
1079     }
1080
1081     if(!level)
1082         break;
1083   }
1084   
1085   rfx_free(t->data); t->data = 0;
1086   t->memsize = t->len = t->pos = 0;
1087
1088   swf_SetU16(t, spriteid);
1089   swf_SetU16(t, spriteframes);
1090 }
1091
1092 void swf_FoldSprite(TAG * t)
1093 {
1094   TAG*sprtag=t,*tmp;
1095   U16 id,frames,tmpid;
1096   int level;
1097   if(t->id!=ST_DEFINESPRITE)
1098       return;
1099   if(!t->len) {
1100       fprintf(stderr, "Error: Sprite has no ID!");
1101       return;
1102   }
1103   if(t->len>4) {
1104     /* sprite is already folded */
1105       return;
1106   }
1107
1108   t->pos = 0;
1109   id = swf_GetU16(t);
1110   rfx_free(t->data);
1111   t->len = t->pos = t->memsize = 0;
1112   t->data = 0;
1113
1114   frames = 0;
1115
1116   t = swf_NextTag(sprtag);
1117   level = 1;
1118
1119   do 
1120   { 
1121     if(t->id==ST_SHOWFRAME) frames++;
1122     if(t->id == ST_DEFINESPRITE && t->len<=4)
1123         level++;
1124     if(t->id == ST_END)
1125         level--;
1126     t = swf_NextTag(t);
1127   } while(t && level);
1128   if(level)
1129     fprintf(stderr, "rfxswf error: sprite doesn't end(1)\n");
1130
1131   swf_SetU16(sprtag, id);
1132   swf_SetU16(sprtag, frames);
1133
1134   t = swf_NextTag(sprtag);
1135   level = 1;
1136
1137   do
1138   { 
1139     if(t->len<0x3f&&t->id!=ST_DEFINEBITSLOSSLESS&&t->id!=ST_DEFINEBITSLOSSLESS2) {
1140         swf_SetU16(sprtag,t->len|(t->id<<6));
1141     } else {
1142         swf_SetU16(sprtag,0x3f|(t->id<<6));
1143         swf_SetU32(sprtag,t->len);
1144     }
1145     if(t->len)
1146         swf_SetBlock(sprtag,t->data, t->len);
1147     tmp = t;
1148     if(t->id == ST_DEFINESPRITE && t->len<=4)
1149         level++;
1150     if(t->id == ST_END)
1151         level--;
1152     t = swf_NextTag(t);
1153     swf_DeleteTag(tmp);
1154   } 
1155   while (t && level);
1156   if(level)
1157     fprintf(stderr, "rfxswf error: sprite doesn't end(2)\n");
1158
1159 //  sprtag->next = t;
1160 //  t->prev = sprtag;
1161 }
1162
1163 int swf_IsFolded(TAG * t)
1164 {
1165     return (t->id == ST_DEFINESPRITE && t->len>4);
1166 }
1167
1168 void swf_FoldAll(SWF*swf)
1169 {
1170     TAG*tag = swf->firstTag;
1171     //swf_DumpSWF(stdout, swf);
1172     while(tag) {
1173         if(tag->id == ST_DEFINESPRITE) {
1174             swf_FoldSprite(tag);
1175             //swf_DumpSWF(stdout, swf);
1176         }
1177         tag = swf_NextTag(tag);
1178     }
1179 }
1180
1181 void swf_UnFoldAll(SWF*swf)
1182 {
1183     TAG*tag = swf->firstTag;
1184     while(tag) {
1185         if(tag->id == ST_DEFINESPRITE)
1186             swf_UnFoldSprite(tag);
1187         tag = tag->next;
1188     }
1189 }
1190
1191 void swf_OptimizeTagOrder(SWF*swf)
1192 {
1193   TAG*tag,*next;
1194   TAG*level0;
1195   int level;
1196   int changes;
1197   swf_UnFoldAll(swf);
1198   /* at the moment, we don't actually do optimizing,
1199      only fixing of non-spec-conformant things like
1200      sprite tags */
1201
1202   do {
1203     changes = 0;
1204     level = 0;
1205     level0 = 0;
1206     tag = swf->firstTag;
1207     while(tag) {
1208       next = tag->next;
1209       if(tag->id == ST_DEFINESPRITE) {
1210         if(tag->len>4) {
1211           /* ??? all sprites are supposed to be unfolded */
1212           fprintf(stderr, "librfxswf error - internal error in OptimizeTagOrder/UnfoldAll\n");
1213         }
1214         level++;
1215         if(level==1) {
1216           level0 = tag;
1217           tag = next;
1218           continue;
1219         }
1220       }
1221       if(level>=1) {
1222         /* move non-sprite tags out of sprite */
1223         if(!swf_isAllowedSpriteTag(tag) || level>=2) {
1224           /* remove tag from current position */
1225           tag->prev->next = tag->next;
1226           if(tag->next)
1227             tag->next->prev = tag->prev;
1228
1229           /* insert before tag level0 */
1230           tag->next = level0;
1231           tag->prev = level0->prev;
1232           level0->prev = tag;
1233           if(tag->prev)
1234             tag->prev->next = tag;
1235           else
1236             swf->firstTag = tag;
1237           changes = 1;
1238         }
1239       }
1240       if(tag->id == ST_END) {
1241         level--;
1242       }
1243
1244       tag = next;
1245     }
1246   } while(changes);
1247 }
1248
1249 // Movie Functions
1250
1251 int swf_ReadSWF2(struct reader_t*reader, SWF * swf)   // Reads SWF to memory (malloc'ed), returns length or <0 if fails
1252 {     
1253   if (!swf) return -1;
1254   memset(swf,0x00,sizeof(SWF));
1255
1256   { char b[32];                         // read Header
1257     int len;
1258     TAG * t;
1259     TAG t1;
1260     struct reader_t zreader;
1261     
1262     if ((len = reader->read(reader ,b,8))<8) return -1;
1263
1264     if (b[0]!='F' && b[0]!='C') return -1;
1265     if (b[1]!='W') return -1;
1266     if (b[2]!='S') return -1;
1267     swf->fileVersion = b[3];
1268     swf->compressed  = (b[0]=='C')?1:0;
1269     swf->fileSize    = GET32(&b[4]);
1270     
1271     if(swf->compressed) {
1272         reader_init_zlibinflate(&zreader, reader);
1273         reader = &zreader;
1274     }
1275
1276     reader_GetRect(reader, &swf->movieSize);
1277     reader->read(reader, &swf->frameRate, 2);
1278     swf->frameRate = SWAP16(swf->frameRate);
1279     reader->read(reader, &swf->frameCount, 2);
1280     swf->frameCount = SWAP16(swf->frameCount);
1281
1282     /* read tags and connect to list */
1283     t = &t1;
1284     while (t) t = swf_ReadTag(reader,t);
1285     swf->firstTag = t1.next;
1286     t1.next->prev = NULL;
1287   }
1288   
1289   return reader->pos;
1290 }
1291
1292 int swf_ReadSWF(int handle, SWF * swf)
1293 {
1294   struct reader_t reader;
1295   reader_init_filereader(&reader, handle);
1296   return swf_ReadSWF2(&reader, swf);
1297 }
1298
1299 int  swf_WriteSWF2(struct writer_t*writer, SWF * swf)     // Writes SWF to file, returns length or <0 if fails
1300 { U32 len;
1301   TAG * t;
1302   int frameCount=0;
1303   struct writer_t zwriter;
1304   int fileSize = 0;
1305   int inSprite = 0;
1306   int writer_lastpos = 0;
1307   int ret;
1308     
1309   if (!swf) return -1;
1310   if (!writer) return -1; // the caller should provide a nullwriter, not 0, for querying SWF size
1311
1312   if(writer) writer_lastpos = writer->pos;
1313
1314   // Insert REFLEX Tag
1315
1316 #ifdef INSERT_RFX_TAG
1317
1318   if (swf->firstTag && swf->firstTag->next &&
1319       (swf->firstTag->id != ST_REFLEX || swf->firstTag->next->id != ST_REFLEX)
1320      ) {
1321       swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),"rfx",3);
1322   }
1323
1324 #endif // INSERT_RFX_TAG
1325
1326   // Count Frames + File Size
1327
1328   len = 0;
1329   t = swf->firstTag;
1330   frameCount = 0;
1331
1332   while(t) {
1333       len += swf_WriteTag(-1,t);
1334       if(t->id == ST_DEFINESPRITE && !swf_IsFolded(t)) inSprite++;
1335       else if(t->id == ST_END && inSprite) inSprite--;
1336       else if(t->id == ST_END && !inSprite) {
1337         if(t->prev && t->prev->id!=ST_SHOWFRAME)
1338           frameCount++;
1339       }
1340       else if(t->id == ST_SHOWFRAME && !inSprite) frameCount++;
1341       t = swf_NextTag(t);
1342   }
1343   
1344   { TAG t1;
1345     char b[64],b4[4];
1346     U32 l;
1347
1348     memset(&t1,0x00,sizeof(TAG));
1349     t1.data    = (U8*)b;
1350     t1.memsize = 64;
1351     
1352     { // measure header file size
1353       TAG t2;
1354       char b2[64];
1355       memset(&t2,0x00,sizeof(TAG));
1356       t2.data    = (U8*)b2;
1357       t2.memsize = 64;
1358       swf_SetRect(&t2, &swf->movieSize);
1359       swf_SetU16(&t2, swf->frameRate);
1360       swf_SetU16(&t2, swf->frameCount);
1361       l = swf_GetTagLen(&t2)+8;
1362     }
1363     if(swf->compressed == 8) {
1364       l -= 8;
1365     }
1366
1367     fileSize = l+len;
1368     if(len) {// don't touch headers without tags
1369         swf->fileSize = fileSize;
1370         swf->frameCount = frameCount;
1371     }
1372
1373     if(swf->compressed != 8) {
1374     /* compressed flag set to 8 means "skip first 8 
1375        header bytes". This is necessary if the caller wants to
1376        create compressed SWFs himself .
1377        It also means that we don't initialize our own zlib
1378        writer, but assume the caller provided one.
1379      */
1380       if(swf->compressed) {
1381         char*id = "CWS";
1382         writer->write(writer, id, 3);
1383       }
1384       else {
1385         char*id = "FWS";
1386         writer->write(writer, id, 3);
1387       }
1388
1389       writer->write(writer, &swf->fileVersion, 1);
1390       PUT32(b4, swf->fileSize);
1391       writer->write(writer, b4, 4);
1392       
1393       if(swf->compressed) {
1394         writer_init_zlibdeflate(&zwriter, writer);
1395         writer = &zwriter;
1396       }
1397     }
1398
1399     swf_SetRect(&t1,&swf->movieSize);
1400     swf_SetU16(&t1,swf->frameRate);
1401     swf_SetU16(&t1,swf->frameCount);
1402
1403     ret = writer->write(writer,b,swf_GetTagLen(&t1));
1404     if (ret!=swf_GetTagLen(&t1))
1405     {
1406       #ifdef DEBUG_RFXSWF
1407         fprintf(stderr, "ret:%d\n",ret);
1408         perror("write:");
1409         fprintf(stderr,"WriteSWF() failed: Header.\n");
1410       #endif
1411       return -1;
1412     }
1413
1414     t = swf->firstTag;
1415     while (t)
1416     { if (swf_WriteTag2(writer, t)<0) return -1;
1417       t = swf_NextTag(t);
1418     }
1419     if(swf->compressed) {
1420       if(swf->compressed != 8) {
1421         zwriter.finish(&zwriter);
1422         return writer->pos - writer_lastpos;
1423       }
1424       return (int)fileSize;
1425     } else {
1426       return (int)fileSize;
1427     }
1428   }
1429 }
1430
1431 int  swf_WriteSWF(int handle, SWF * swf)     // Writes SWF to file, returns length or <0 if fails
1432 {
1433   struct writer_t writer;
1434   int len = 0;
1435   swf->compressed = 0;
1436   
1437   if(handle<0) {
1438     writer_init_nullwriter(&writer);
1439     len = swf_WriteSWF2(&writer, swf);
1440     return len;
1441   }
1442   writer_init_filewriter(&writer, handle);
1443   len = swf_WriteSWF2(&writer, swf);
1444   writer.finish(&writer);
1445   return len;
1446 }
1447
1448 int  swf_WriteSWC(int handle, SWF * swf)     // Writes SWF to file, returns length or <0 if fails
1449 {
1450   struct writer_t writer;
1451   int len = 0;
1452   swf->compressed = 1;
1453
1454   if(handle<0) {
1455     writer_init_nullwriter(&writer);
1456     len = swf_WriteSWF2(&writer, swf);
1457   }
1458   writer_init_filewriter(&writer, handle);
1459   len = swf_WriteSWF2(&writer, swf);
1460   writer.finish(&writer);
1461   return len;
1462 }
1463
1464 int swf_WriteHeader2(struct writer_t*writer,SWF * swf)
1465 {
1466   SWF myswf;
1467   memcpy(&myswf,swf,sizeof(SWF));
1468   myswf.firstTag = 0;
1469   return swf_WriteSWF2(writer, &myswf);
1470 }
1471
1472 int swf_WriteHeader(int handle,SWF * swf)
1473 {
1474   SWF myswf;
1475   memcpy(&myswf,swf,sizeof(SWF));
1476   myswf.firstTag = 0;
1477   return swf_WriteSWF(handle, &myswf);
1478 }
1479
1480 int swf_WriteCGI(SWF * swf)
1481 { int len;
1482   char s[1024];
1483     
1484   len = swf_WriteSWF(-1,swf);
1485
1486   if (len<0) return -1;
1487
1488   sprintf(s,"Content-type: application/x-shockwave-flash\n"
1489             "Accept-Ranges: bytes\n"
1490             "Content-Length: %lu\n"
1491             "Expires: Thu, 13 Apr 2000 23:59:59 GMT\n"
1492             "\n",len);
1493             
1494   write(fileno(stdout),s,strlen(s));
1495   return swf_WriteSWF(fileno(stdout),swf);
1496 }
1497
1498 SWF* swf_CopySWF(SWF*swf)
1499 {
1500     SWF*nswf = rfx_alloc(sizeof(SWF));
1501     TAG*tag, *ntag;
1502     memcpy(nswf, swf, sizeof(SWF));
1503     nswf->firstTag = 0;
1504     tag = swf->firstTag;
1505     ntag = 0;
1506     while(tag) {
1507         ntag = swf_CopyTag(ntag, tag);
1508         if(!nswf->firstTag)
1509             nswf->firstTag = ntag;
1510         tag = tag->next;
1511     }
1512     return nswf;
1513 }
1514
1515 void swf_FreeTags(SWF * swf)                 // Frees all malloc'ed memory for tags
1516 { TAG * t = swf->firstTag;
1517
1518   while (t)
1519   { TAG * tnew = t->next;
1520     if (t->data) rfx_free(t->data);
1521     rfx_free(t);
1522     t = tnew;
1523   }
1524   swf->firstTag = 0;
1525 }
1526
1527 // include advanced functions
1528
1529 #include "modules/swfdump.c"
1530 #include "modules/swfshape.c"
1531 #include "modules/swftext.c"
1532 #include "modules/swffont.c"
1533 #include "modules/swfobject.c"
1534 #include "modules/swfbutton.c"
1535 #include "modules/swftools.c"
1536 #include "modules/swfcgi.c"
1537 #include "modules/swfbits.c"
1538 #include "modules/swfaction.c"
1539 #include "modules/swfsound.c"
1540 #include "modules/swfdraw.c"
1541 #include "modules/swfrender.c"