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