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