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