fix advance values.
[swftools.git] / lib / modules / swftools.c
1 /* swftools.c
2
3    Math and matrix functions, misc tools
4
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2000, 2001 Rainer Böhme <rfxswf@reflex-studio.de>
9  
10    This file is distributed under the GPL, see file COPYING for details 
11
12 */
13
14 // Matrix & Math tools for SWF files
15
16 #define S64 long long
17 SFIXED RFXSWF_SP(SFIXED a1,SFIXED a2,SFIXED b1,SFIXED b2)
18 { S64 a;
19   a = (S64)a1*(S64)b1+(S64)a2*(S64)b2;
20   return (SFIXED)(a>>16);
21 }
22 SFIXED RFXSWF_QFIX(int zaehler,int nenner) // bildet Quotient von zwei INTs in SFIXED
23 { S64 z = zaehler<<16;
24   S64 a = z/(S64)nenner;
25   return (SFIXED)a;
26 }
27 #undef S64
28
29 MATRIX * swf_MatrixJoin(MATRIX * d,MATRIX * s1,MATRIX * s2)
30 {        
31   if (!d) return NULL;
32   if (!s1) return (s2)?(MATRIX *)memcpy(d,s2,sizeof(MATRIX)):NULL;
33   if (!s2) return (MATRIX *)memcpy(d,s1,sizeof(MATRIX));
34   
35   d->tx = s1->tx + s2->tx;
36   d->ty = s1->ty + s2->ty;
37   
38   d->sx = RFXSWF_SP(s1->sx,s1->r1,s2->sx,s2->r0);
39   d->sy = RFXSWF_SP(s1->r0,s1->sy,s2->r1,s2->sy);
40   d->r0 = RFXSWF_SP(s1->r0,s1->sy,s2->sx,s2->r0);
41   d->r1 = RFXSWF_SP(s1->sx,s1->r1,s2->r1,s2->sy);
42
43   //DumpMatrix(NULL,d);
44   
45   return d;
46 }
47
48 MATRIX * swf_MatrixMapTriangle(MATRIX * m,int dx,int dy,int x0,int y0,
49                                int x1,int y1,int x2,int y2)
50 { int dx1 = x1 - x0;
51   int dy1 = y1 - y0;
52   int dx2 = x2 - x0;
53   int dy2 = y2 - y0;
54   
55   if (!m) return NULL;
56   if ((!dx)||(!dy)) return NULL; // check DIV by zero
57
58   m->tx = x0;
59   m->ty = y0;
60   m->sx = RFXSWF_QFIX(dx1,dx);
61   m->sy = RFXSWF_QFIX(dy2,dy);
62   m->r0 = RFXSWF_QFIX(dy1,dx);
63   m->r1 = RFXSWF_QFIX(dx2,dy);
64   
65   return m;
66 }
67
68 void swf_SetDefineID(TAG * tag, U16 newid)
69 {
70   int oldlen = tag->len;
71   tag->len = 0;
72   swf_SetU16(tag, newid); /* set defining ID */
73   tag->len = oldlen;
74 }
75
76 U16 swf_GetDefineID(TAG * t)
77 // up to SWF 4.0
78 { U32 oldTagPos;
79   U16 id = 0;
80
81   oldTagPos = swf_GetTagPos(t);
82   swf_SetTagPos(t,0);
83
84   switch (swf_GetTagID(t))
85   { case ST_DEFINESHAPE:
86     case ST_DEFINESHAPE2:
87     case ST_DEFINESHAPE3:
88     case ST_DEFINEMORPHSHAPE:
89     case ST_DEFINEEDITTEXT:
90     case ST_DEFINEBITS:
91     case ST_DEFINEBITSJPEG2:
92     case ST_DEFINEBITSJPEG3:
93     case ST_DEFINEBITSLOSSLESS:
94     case ST_DEFINEBITSLOSSLESS2:
95     case ST_DEFINEBUTTON:
96     case ST_DEFINEBUTTON2:
97     case ST_DEFINEBUTTONCXFORM: //pseudodefine
98     case ST_DEFINEBUTTONSOUND: //pseudodefine
99     case ST_DEFINEFONT:
100     case ST_DEFINEFONT2:
101     case ST_DEFINEFONTINFO: //pseudodefine
102     case ST_DEFINETEXT:
103     case ST_DEFINETEXT2:
104     case ST_DEFINESOUND:
105     case ST_DEFINESPRITE:
106     case ST_NAMECHARACTER: //pseudodefine
107       id = swf_GetU16(t);
108       break;
109   }
110
111   swf_SetTagPos(t,oldTagPos);
112
113   return id;
114 }
115
116 U16 swf_GetPlaceID(TAG * t)
117 // up to SWF 4.0
118 { U32 oldTagPos;
119   U16 id = 0;
120
121   oldTagPos = swf_GetTagPos(t);
122   swf_SetTagPos(t,0);
123
124   switch (swf_GetTagID(t))
125   { case ST_PLACEOBJECT:
126     case ST_REMOVEOBJECT:
127     case ST_FREECHARACTER:
128     case ST_STARTSOUND:
129       id = swf_GetU16(t);
130       break;
131
132     case ST_PLACEOBJECT2:
133     { U8 flags = swf_GetU8(t);
134       U16 d = swf_GetU16(t);
135       id = (flags&PF_CHAR)?swf_GetU16(t):id;
136     } break;
137
138   }
139
140   swf_SetTagPos(t,oldTagPos);
141
142   return id;
143 }
144
145 static int swf_definingtagids[] =
146 {ST_DEFINESHAPE,
147  ST_DEFINESHAPE2,
148  ST_DEFINESHAPE3,
149  ST_DEFINEMORPHSHAPE,
150  ST_DEFINEFONT,
151  ST_DEFINEFONT2,
152  ST_DEFINETEXT,
153  ST_DEFINETEXT2,
154  ST_DEFINEEDITTEXT,
155  ST_DEFINEBITS,
156  ST_DEFINEBITSJPEG2,
157  ST_DEFINEBITSJPEG3,
158  ST_DEFINEBITSLOSSLESS,
159  ST_DEFINEBITSLOSSLESS2,
160  ST_DEFINEMOVIE,
161  ST_DEFINESPRITE,
162  ST_DEFINEBUTTON,
163  ST_DEFINEBUTTON2,
164  ST_DEFINESOUND,
165  -1
166 };
167
168 // tags which may be used inside a sprite definition
169 static int swf_spritetagids[] =
170 {ST_SHOWFRAME,
171  ST_PLACEOBJECT,
172  ST_PLACEOBJECT2,
173  ST_REMOVEOBJECT,
174  ST_REMOVEOBJECT2, //?
175  ST_DOACTION,
176  ST_STARTSOUND,
177  ST_FRAMELABEL,
178  ST_SOUNDSTREAMHEAD,
179  ST_SOUNDSTREAMHEAD2,
180  ST_SOUNDSTREAMBLOCK,
181  ST_END,
182  -1
183 };
184
185 static int swf_pseudodefiningtagids[] = 
186 {
187  ST_DEFINEFONTINFO,
188  ST_DEFINEBUTTONCXFORM,
189  ST_DEFINEBUTTONSOUND,
190  ST_NAMECHARACTER,
191  -1
192 };
193
194 U8 swf_isAllowedSpriteTag(TAG * tag)
195 {
196     int id = tag->id;
197     int t=0;
198     while(swf_spritetagids[t]>=0)
199     {
200         if(swf_spritetagids[t] == id) 
201             return 1;
202         t++;
203     }
204     return 0; 
205 }
206
207 U8 swf_isDefiningTag(TAG * tag)
208 {
209     int id = tag->id;
210     int t=0;
211     while(swf_definingtagids[t]>=0)
212     {
213         if(swf_definingtagids[t] == id) 
214             return 1;
215         t++;
216     }
217     return 0; 
218 }
219
220 U8 swf_isPseudoDefiningTag(TAG * tag)
221 {
222     int id = tag->id;
223     int t=0;
224     while(swf_pseudodefiningtagids[t]>=0)
225     {
226         if(swf_pseudodefiningtagids[t] == id) 
227             return 1;
228         t++;
229     }
230     return 0; 
231 }
232
233 U16 swf_GetDepth(TAG * t)
234 // up to SWF 4.0
235
236   U16 depth = 0;
237   U32 oldTagPos;
238   oldTagPos = swf_GetTagPos(t);
239   swf_SetTagPos(t,0);
240
241   switch (swf_GetTagID(t))
242   { case ST_PLACEOBJECT:
243     case ST_REMOVEOBJECT:
244       swf_GetU16(t); //id
245       depth = swf_GetU16(t);
246       break;
247     case ST_REMOVEOBJECT2:
248       depth = swf_GetU16(t);
249       break;
250     case ST_PLACEOBJECT2:
251     { U8 flags = swf_GetU8(t);
252       depth = swf_GetU16(t);
253     } break;
254   }
255   swf_SetTagPos(t,oldTagPos);
256   return depth;
257 }
258
259 char* swf_GetName(TAG * t)
260 {
261     char* name = 0;
262     U32 oldTagPos;
263     MATRIX m;
264     CXFORM c;
265     oldTagPos = swf_GetTagPos(t);
266     swf_SetTagPos(t,0);
267     switch(swf_GetTagID(t))
268     {
269         case ST_FRAMELABEL:
270             name = &t->data[swf_GetTagPos(t)];
271         break;
272         case ST_PLACEOBJECT2: {   
273             U8 flags = swf_GetU8(t);
274             swf_GetU16(t); //depth;
275             if(flags&PF_CHAR) 
276               swf_GetU16(t); //id
277             if(flags&PF_MATRIX)
278               swf_GetMatrix(t, &m);
279             if(flags&PF_CXFORM)
280               swf_GetCXForm(t, &c, 1);
281             if(flags&PF_RATIO)
282               swf_GetU16(t);
283             if(flags&PF_NAME) {
284               swf_ResetReadBits(t);
285               name = &t->data[swf_GetTagPos(t)];
286             }
287         }
288         break;
289     }
290     swf_SetTagPos(t,oldTagPos);
291     return name;
292 }
293
294 /* used in enumerateUsedIDs */
295 void swf_GetMorphGradient(TAG * tag, GRADIENT * gradient1, GRADIENT * gradient2)
296 {
297     GRADIENT dummy1;
298     GRADIENT dummy2;
299     int t;
300     if(!gradient1)
301         gradient1 = &dummy1;
302     if(!gradient2)
303         gradient2 = &dummy2;
304     gradient1->num = 
305     gradient2->num = swf_GetU8(tag);
306     for(t=0;t<gradient1->num;t++)
307     {
308         int s=t;
309         if(s>=8) //FIXME
310             s=7;
311         gradient1->ratios[t] = swf_GetU8(tag);
312         swf_GetRGBA(tag, &gradient1->rgba[t]);
313         gradient2->ratios[t] = swf_GetU8(tag);
314         swf_GetRGBA(tag, &gradient2->rgba[t]);
315     }
316 }
317
318 #define DEBUG_ENUMERATE if(0)
319
320 static void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph)
321 {
322     U16 count;
323     int t;
324     count = swf_GetU8(tag);
325     if(count == 0xff && num>1) // defineshape2,3 only
326         count = swf_GetU16(tag);
327
328     for(t=0;t<count;t++)
329     {
330         int type;
331         U8*pos;
332         swf_ResetReadBits(tag);
333         type = swf_GetU8(tag); //type
334         if(type == 0) {
335             if(num == 3)
336                 {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
337             else 
338                 {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
339         }
340         else if(type == 0x10 || type == 0x12)
341         {
342             swf_ResetReadBits(tag);
343             swf_GetMatrix(tag, NULL);
344             if(morph)
345                 swf_GetMatrix(tag, NULL);
346             swf_ResetReadBits(tag);
347             if(morph)
348                 swf_GetMorphGradient(tag, NULL, NULL);
349             else
350                 swf_GetGradient(tag, NULL, /*alpha*/ num>=3?1:0);
351         }
352         else if(type == 0x40 || type == 0x41)
353         {
354             swf_ResetReadBits(tag);
355             // we made it.
356             if(tag->data[tag->pos] != 0xff ||
357                tag->data[tag->pos+1] != 0xff)
358             (callback)(tag, tag->pos, callback_data);
359
360             swf_GetU16(tag);
361             swf_ResetReadBits(tag);
362             swf_GetMatrix(tag, NULL);
363             if(morph)
364                 swf_GetMatrix(tag, NULL);
365         }
366         else {
367             fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
368         }
369     }
370     swf_ResetReadBits(tag);
371     count = swf_GetU8(tag); // line style array
372     if(count == 0xff)
373         count = swf_GetU16(tag);
374     for(t=0;t<count;t++) 
375     {
376         swf_GetU16(tag);
377         if(morph)
378             swf_GetU16(tag);
379         if(num == 3)
380             {swf_GetRGBA(tag, NULL);if(morph) swf_GetRGBA(tag, NULL);}
381         else
382             {swf_GetRGB(tag, NULL);if(morph) swf_GetRGB(tag, NULL);}
383     }
384 }
385
386 void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), void*callback_data)
387 {
388     int num = 1;
389     swf_ResetReadBits(tag);
390     tag->pos = 0;
391     switch(tag->id)
392     {
393         case ST_DEFINEBUTTONCXFORM: {
394             int t;
395             callback(tag, tag->pos + base, callback_data);
396             for(t=0;t<4;t++) {
397                 int flags;
398                 callback(tag, tag->pos + base, callback_data);
399                 swf_GetU16(tag); //sound id
400                 flags = swf_GetU8(tag);
401                 if(flags&1)
402                     swf_GetU32(tag); // in point
403                 if(flags&2)
404                     swf_GetU32(tag); // out points
405                 if(flags&4)
406                     swf_GetU16(tag); // loop count
407                 if(flags&8)
408                 {
409                     int npoints = swf_GetU8(tag);
410                     int s;
411                     for(s=0;s<npoints;s++)
412                     {
413                         swf_GetU32(tag);
414                         swf_GetU16(tag);
415                         swf_GetU16(tag);
416                     }
417                 }
418             }
419         } break;
420         case ST_DEFINEBUTTONSOUND:
421             callback(tag, tag->pos + base, callback_data); //button id
422         break;
423
424         case ST_EXPORTASSETS: {
425             int num =  swf_GetU16(tag);
426             int t;
427             for(t=0;t<num;t++) {
428                 callback(tag, tag->pos + base, callback_data); //button id
429                 swf_GetU16(tag); //id
430                 while(swf_GetU8(tag)); //name
431             }
432         } break;
433
434         case ST_FREECHARACTER: /* unusual tags, which all start with an ID */
435         case ST_NAMECHARACTER:
436         case ST_GENERATORTEXT:
437         case ST_MX3:
438             callback(tag, tag->pos + base, callback_data);
439         break;
440         case ST_PLACEOBJECT:
441             callback(tag, tag->pos + base, callback_data);
442         break;
443         case ST_PLACEOBJECT2:
444             // only if placeflaghascharacter
445             if(!(tag->data[0]&2))
446                 break;
447             callback(tag, 3 + base, callback_data);
448         break;
449         case ST_REMOVEOBJECT:
450             callback(tag, tag->pos + base, callback_data);
451         break;
452         case ST_STARTSOUND:
453             callback(tag, tag->pos + base, callback_data);
454         break;
455         case ST_DEFINESPRITE: {
456             if(tag->len <= 4)
457                 break; // sprite is expanded
458
459             swf_GetU16(tag); // id
460             swf_GetU16(tag); // framenum
461
462             while(1) {
463                 U16 flags = swf_GetU16(tag);
464                 U32 len;
465                 U16 id = flags>>6;
466                 TAG *tag2 = swf_InsertTag(NULL, id);
467                 len = flags&0x3f;
468                 if(len == 63)
469                     len = swf_GetU32(tag);
470                 if(id == ST_END)
471                     break;
472                 tag2->len = tag2->memsize = len;
473                 tag2->data = malloc(len);
474                 memcpy(tag2->data, &tag->data[tag->pos], len);
475                 /* I never saw recursive sprites, but they are (theoretically) 
476                    possible, so better add base here again */
477                 enumerateUsedIDs(tag2, tag->pos + base, callback, callback_data);
478                 swf_DeleteTag(tag2);
479                 swf_GetBlock(tag, NULL, len);
480             }
481         } 
482         break;
483         case ST_DEFINEBUTTON2: // has some font ids in the button records
484             num++; 
485         //fallthrough
486         case ST_DEFINEBUTTON: {
487             swf_GetU16(tag); //button id
488             if(num>1)
489             { 
490                 int offset;
491                 swf_GetU8(tag); //flag
492                 offset = swf_GetU16(tag); //offset
493             }
494             while(1)
495             {
496                 U16 charid;
497                 if(!swf_GetU8(tag)) //flags
498                     break; 
499                 callback(tag, tag->pos + base, callback_data);
500                 swf_GetU16(tag); //char
501                 swf_GetU16(tag); //layer
502                 swf_ResetReadBits(tag);
503                 swf_GetMatrix(tag, NULL);
504                 if(num>1) {
505                   swf_ResetReadBits(tag);
506                   swf_GetCXForm(tag, NULL, 1);
507                 }
508             }
509             // ...
510         }
511         break;
512         case ST_DEFINEEDITTEXT:  {
513             U8 flags1,flags2;
514             swf_GetU16(tag); //id
515             swf_GetRect(tag, NULL); //bounding box
516             swf_ResetReadBits(tag);
517             flags1 = swf_GetU8(tag);
518             flags2 = swf_GetU8(tag);
519             if(flags1 & 1)
520                 callback(tag, tag->pos + base, callback_data);
521         }
522         break;
523         case ST_DEFINETEXT2:
524             num ++;
525         case ST_DEFINETEXT: { 
526             int glyphbits, advancebits;
527             int id;
528             id = swf_GetU16(tag); //id
529             swf_GetRect(tag, NULL); //bounding box
530             swf_ResetReadBits(tag);
531             swf_GetMatrix(tag, NULL); //matrix
532             swf_ResetReadBits(tag);
533             glyphbits = swf_GetU8(tag); //glyphbits
534             advancebits = swf_GetU8(tag); //advancebits
535             while(1) {
536                 U16 flags;
537                 swf_ResetReadBits(tag);
538                 flags = swf_GetBits(tag, 8);
539                 if(!flags) break;
540                 if(flags & 128) // text style record
541                 {
542                     swf_ResetReadBits(tag);
543                     if(flags & 8) { // hasfont
544                         callback(tag, tag->pos + base, callback_data);
545                         id = swf_GetU16(tag);
546                     }
547                     if(flags & 4) { // hascolor
548                         if(num==1) swf_GetRGB(tag, NULL);
549                         else       swf_GetRGBA(tag, NULL);
550                     }
551                     if(flags & 2) { //has x offset
552                         swf_ResetReadBits(tag);
553                         swf_GetU16(tag);
554                     }
555                     if(flags & 1) { //has y offset
556                         swf_ResetReadBits(tag);
557                         swf_GetU16(tag);
558                     }
559                     if(flags & 8) { //has height
560                         swf_ResetReadBits(tag);
561                         swf_GetU16(tag);
562                     }
563                 } else { // glyph record
564                     int t;
565                     swf_ResetReadBits(tag);
566                     for(t=0;t<flags;t++) {
567                         swf_GetBits(tag, glyphbits);
568                         swf_GetBits(tag, advancebits);
569                     }
570                 }
571             }
572             break;
573         }
574         case ST_DEFINEFONTINFO:
575             callback(tag, tag->pos + base, callback_data);
576         break;
577
578         //case ST_DEFINEMORPHSHAPE: /* disabled for now (doesn't work) */
579
580         case ST_DEFINESHAPE3: // these thingies might have bitmap ids in their fillstyles
581         num++; //fallthrough
582         case ST_DEFINESHAPE2:
583         num++; //fallthrough
584         case ST_DEFINESHAPE: {
585             int fillbits;
586             int linebits;
587             int id; 
588             int morph = 0;
589             if(tag->id == ST_DEFINEMORPHSHAPE)
590                 morph = 1;
591
592             id = swf_GetU16(tag); // id;
593             swf_GetRect(tag, NULL); // bounds
594             if(morph) {
595                 swf_GetRect(tag, NULL); // bounds2
596                 swf_GetU32(tag); //offset to endedges
597             }
598     
599             DEBUG_ENUMERATE printf("Tag:%d Name:%s ID:%d\n", tag->id, swf_TagGetName(tag), id);
600
601             enumerateUsedIDs_styles(tag, callback, callback_data, num, morph);
602             DEBUG_ENUMERATE printf("-------\n");
603             while(--morph>=0) /* morph shapes define two shapes */
604             {
605                 fillbits = swf_GetBits(tag, 4);
606                 linebits = swf_GetBits(tag, 4);
607                 DEBUG_ENUMERATE printf("%d %d\n", fillbits, linebits);
608                 swf_ResetReadBits(tag);
609                 while(1) {
610                     int flags;
611                     flags = swf_GetBits(tag, 1);
612                     if(!flags) { //style change
613                         flags = swf_GetBits(tag, 5);
614                         if(!flags)
615                             break;
616                         if(flags&1) { //move
617                             int n = swf_GetBits(tag, 5); 
618                             int x,y;
619                             x = swf_GetBits(tag, n); //x
620                             y = swf_GetBits(tag, n); //y
621                             DEBUG_ENUMERATE printf("move %f %f\n",x/20.0,y/20.0);
622                         }
623                         if(flags&2) { //fill0
624                             int fill0;
625                             fill0 = swf_GetBits(tag, fillbits); 
626                             DEBUG_ENUMERATE printf("fill0 %d\n", fill0);
627                         }
628                         if(flags&4) { //fill1
629                             int fill1;
630                             fill1 = swf_GetBits(tag, fillbits); 
631                             DEBUG_ENUMERATE printf("fill1 %d\n", fill1);
632                         }
633                         if(flags&8) { //linestyle
634                             int line;
635                             line = swf_GetBits(tag, linebits); 
636                             DEBUG_ENUMERATE printf("linestyle %d\n",line);
637                         }
638                         if(flags&16) {
639                             DEBUG_ENUMERATE printf("more fillstyles\n");
640                             enumerateUsedIDs_styles(tag, callback, callback_data, num, 0);
641                             fillbits = swf_GetBits(tag, 4);
642                             linebits = swf_GetBits(tag, 4);
643                         }
644                     } else {
645                         flags = swf_GetBits(tag, 1);
646                         if(flags) { //straight edge
647                             int n = swf_GetBits(tag, 4) + 2;
648                             if(swf_GetBits(tag, 1)) { //line flag
649                                 int x,y;
650                                 x = swf_GetSBits(tag, n); //delta x
651                                 y = swf_GetSBits(tag, n); //delta y
652                                 DEBUG_ENUMERATE printf("line %f %f\n",x/20.0,y/20.0);
653                             } else {
654                                 int v=swf_GetBits(tag, 1);
655                                 int d;
656                                 d = swf_GetSBits(tag, n); //vert/horz
657                                 DEBUG_ENUMERATE printf("%s %f\n",v?"vertical":"horizontal", d/20.0);
658                             }
659                         } else { //curved edge
660                             int n = swf_GetBits(tag, 4) + 2;
661                             int x1,y1,x2,y2;
662                             x1 = swf_GetSBits(tag, n);
663                             y1 = swf_GetSBits(tag, n);
664                             x2 = swf_GetSBits(tag, n);
665                             y2 = swf_GetSBits(tag, n);
666                             DEBUG_ENUMERATE printf("curve %f %f %f %f\n", x1/20.0, y1/20.0, x2/20.0, y2/20.0);
667                         }
668                     }
669                 }
670             }
671         }
672         break;
673         default:
674         break;
675     }
676 }
677
678 void callbackCount(TAG * t,int pos, void*ptr)
679 {
680     (*(int*)ptr)++;
681     DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
682 }
683
684 void callbackFillin(TAG * t,int pos, void*ptr)
685 {
686     **(int**)ptr = pos;
687     (*(int**)ptr)++;
688     DEBUG_ENUMERATE printf("callback(%d) %d\n", pos, *(U16*)&t->data[pos]);
689 }
690
691 int swf_GetNumUsedIDs(TAG * t)
692 {
693     int num = 0;
694     enumerateUsedIDs(t, 0, callbackCount, &num);
695     return num;
696 }
697
698 void swf_GetUsedIDs(TAG * t, int * positions)
699 {
700     int * ptr = positions;
701     enumerateUsedIDs(t, 0, callbackFillin, &ptr);
702 }
703
704 void swf_Relocate (SWF*swf, char*bitmap)
705 {
706     TAG*tag;
707     int slaveids[65536];
708     memset(slaveids, -1, sizeof(slaveids));
709     tag = swf->firstTag;
710     while(tag)
711     {
712         int num; 
713         int *ptr;
714         int t;
715
716         if(swf_isDefiningTag(tag))
717         {
718             int newid;
719             int id;
720             
721             id = swf_GetDefineID(tag); //own id
722
723             if(!bitmap[id]) { //free
724                 newid = id;
725             }
726             else {
727                 newid = 0;
728                 for (t=1;t<65536;t++)
729                 {
730                     if(!bitmap[t])
731                     {
732                         newid = t;
733                         break;
734                     }
735                 }
736             }
737             bitmap[newid] = 1;
738             slaveids[id] = newid;
739
740             swf_SetDefineID(tag, newid);
741         } 
742         
743         num = swf_GetNumUsedIDs(tag);
744         ptr = malloc(sizeof(int)*num);
745         swf_GetUsedIDs(tag, ptr);
746
747         for(t=0;t<num;t++) {
748             int id = GET16(&tag->data[ptr[t]]);
749             if(slaveids[id]<0) {
750                 fprintf(stderr, "swf_Relocate: Mapping id never encountered before: %d\n", id);
751                 return ;
752             }
753             id = slaveids[id];
754             PUT16(&tag->data[ptr[t]], id);
755         }
756         tag=tag->next;
757     }
758 }
759