added scaling grid parsing
[swftools.git] / src / swfdump.c
1 /* swfdump.c
2    Shows the structure of a swf file
3
4    Part of the swftools package.
5    
6    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
7  
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
21
22 #include "../config.h"
23
24 #ifdef HAVE_SYS_STAT_H
25 #include <sys/stat.h>
26 #else
27 #undef HAVE_STAT
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #else
33 #undef HAVE_STAT
34 #endif
35
36 #include <unistd.h>
37 #include <stdio.h>
38 #include <fcntl.h>
39 #include <stdarg.h>
40 #include "../lib/rfxswf.h"
41 #include "../lib/args.h"
42
43 static char * filename = 0;
44
45 /* idtab stores the ids which are defined in the file. This allows us
46    to detect errors in the file. (i.e. ids which are defined more than 
47    once */
48 static char idtab[65536];
49 static char * indent = "                ";
50
51 static int placements = 0;
52 static int action = 0;
53 static int html = 0;
54 static int xhtml = 0;
55 static int xy = 0;
56 static int showtext = 0;
57 static int showshapes = 0;
58 static int hex = 0;
59 static int used = 0;
60 static int bbox = 0;
61 static int cumulative = 0;
62 static int showfonts = 0;
63
64 static struct options_t options[] = {
65 {"h", "help"},
66 {"D", "full"},
67 {"V", "version"},
68 {"e", "html"},
69 {"E", "xhtml"},
70 {"a", "action"},
71 {"t", "text"},
72 {"s", "shapes"},
73 {"F", "fonts"},
74 {"p", "placements"},
75 {"b", "bbox"},
76 {"X", "width"},
77 {"Y", "height"},
78 {"r", "rate"},
79 {"f", "frames"},
80 {"d", "hex"},
81 {"u", "used"},
82 {0,0}
83 };
84
85 int args_callback_option(char*name,char*val)
86 {
87     if(!strcmp(name, "V")) {
88         printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
89         exit(0);
90     } 
91     else if(name[0]=='a') {
92         action = 1;
93         return 0;
94     }
95     else if(name[0]=='p') {
96         placements = 1;
97         return 0;
98     }
99     else if(name[0]=='t') {
100         showtext = 1;
101         return 0;
102     }
103     else if(name[0]=='s') {
104         showshapes = 1;
105         return 0;
106     }
107     else if(name[0]=='e') {
108         html = 1;
109         return 0;
110     }
111     else if(name[0]=='c') {
112         cumulative = 1;
113         return 0;
114     }
115     else if(name[0]=='E') {
116         html = 1;
117         xhtml = 1;
118         return 0;
119     }
120     else if(name[0]=='X') {
121         xy |= 1;
122         return 0;
123     }
124     else if(name[0]=='Y') {
125         xy |= 2;
126         return 0;
127     }
128     else if(name[0]=='r') {
129         xy |= 4;
130         return 0;
131     }
132     else if(name[0]=='f') {
133         xy |= 8;
134         return 0;
135     }
136     else if(name[0]=='F') {
137         showfonts = 1;
138         return 0;
139     }
140     else if(name[0]=='d') {
141         hex = 1;
142         return 0;
143     }
144     else if(name[0]=='u') {
145         used = 1;
146         return 0;
147     }
148     else if(name[0]=='b') {
149         bbox = 1;
150         return 0;
151     }
152     else if(name[0]=='D') {
153         action = placements = showtext = showshapes = 1;
154         return 0;
155     }
156     else {
157         printf("Unknown option: -%s\n", name);
158         exit(1);
159     }
160
161     return 0;
162 }
163 int args_callback_longoption(char*name,char*val)
164 {
165     return args_long2shortoption(options, name, val);
166 }
167 void args_callback_usage(char *name)
168 {
169     printf("\n");
170     printf("Usage: %s [-atpdu] file.swf\n", name);
171     printf("\n");
172     printf("-h , --help                    Print short help message and exit\n");
173     printf("-D , --full                    Show everything. Same as -atp\n");
174     printf("-V , --version                 Print version info and exit\n");
175     printf("-e , --html                    Print out html code for embedding the file\n");
176     printf("-E , --xhtml                   Print out xhtml code for embedding the file\n");
177     printf("-a , --action                  Disassemble action tags\n");
178     printf("-t , --text                    Show text fields (like swfstrings).\n");
179     printf("-s , --shapes                  Show shape coordinates/styles\n");
180     printf("-F , --fonts                   Show font information\n");
181     printf("-p , --placements              Show placement information\n");
182     printf("-b , --bbox                    Print tag's bounding boxes\n");
183     printf("-X , --width                   Prints out a string of the form \"-X width\".\n");
184     printf("-Y , --height                  Prints out a string of the form \"-Y height\".\n");
185     printf("-r , --rate                    Prints out a string of the form \"-r rate\".\n");
186     printf("-f , --frames                  Prints out a string of the form \"-f framenum\".\n");
187     printf("-d , --hex                     Print hex output of tag data, too.\n");
188     printf("-u , --used                    Show referred IDs for each Tag.\n");
189     printf("\n");
190 }
191 int args_callback_command(char*name,char*val)
192 {
193     if(filename) {
194         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
195                  filename, name);
196     }
197     filename = name;
198     return 0;
199 }
200
201 char* what;
202 char* testfunc(char*str)
203 {
204     printf("%s: %s\n", what, str);
205     return 0;
206 }
207
208 void dumpButton2Actions(TAG*tag, char*prefix)
209 {
210     U32 oldTagPos;
211     U32 offsetpos;
212     U32 condition;
213
214     oldTagPos = swf_GetTagPos(tag);
215
216     // scan DefineButton2 Record
217     
218     swf_GetU16(tag);          // Character ID
219     swf_GetU8(tag);           // Flags;
220
221     offsetpos = swf_GetTagPos(tag);  // first offset
222     swf_GetU16(tag);
223
224     while (swf_GetU8(tag))      // state  -> parse ButtonRecord
225     { swf_GetU16(tag);          // id
226       swf_GetU16(tag);          // layer
227       swf_GetMatrix(tag,NULL);  // matrix
228       swf_GetCXForm(tag,NULL,1);  // cxform
229     }
230
231     while(offsetpos)
232     { U8 a;
233       ActionTAG*actions;
234
235       if(tag->pos >= tag->len)
236           break;
237         
238       offsetpos = swf_GetU16(tag);
239       condition = swf_GetU16(tag);                // condition
240       
241       actions = swf_ActionGet(tag);
242       printf("%s condition %04x\n", prefix, condition);
243       swf_DumpActions(actions, prefix);
244     }
245     
246     swf_SetTagPos(tag,oldTagPos);
247     return;
248 }
249
250 void dumpButtonActions(TAG*tag, char*prefix)
251 {
252     ActionTAG*actions;
253     swf_GetU16(tag); // id
254     while (swf_GetU8(tag))      // state  -> parse ButtonRecord
255     { swf_GetU16(tag);          // id
256       swf_GetU16(tag);          // layer
257       swf_GetMatrix(tag,NULL);  // matrix
258     }
259     actions = swf_ActionGet(tag);
260     swf_DumpActions(actions, prefix);
261 }
262
263 void dumpFont(TAG*tag, char*prefix)
264 {
265     SWFFONT* font = malloc(sizeof(SWFFONT));
266     memset(font, 0, sizeof(SWFFONT));
267     if(tag->id == ST_DEFINEFONT2) {
268         swf_FontExtract_DefineFont2(0, font, tag);
269     } else if(tag->id == ST_DEFINEFONT) {
270         swf_FontExtract_DefineFont(0, font, tag);
271     } else {
272         printf("%sCan't parse %s yet\n", prefix,swf_TagGetName(tag));
273     }
274     printf("%sID: %d\n", prefix,font->id);
275     printf("%sVersion: %d\n", prefix,font->version);
276     printf("%sname: %s\n", prefix,font->name);
277     printf("%scharacters: %d\n", prefix,font->numchars);
278     printf("%shightest mapped unicode value: %d\n", prefix,font->maxascii);
279     if(font->layout)
280     {
281         printf("%sascent:%.2f\n", prefix,font->layout->ascent / 20.0);
282         printf("%sdescent:%.2f\n", prefix,font->layout->descent / 20.0);
283         printf("%sleading:%.2f\n", prefix,font->layout->leading / 20.0);
284         printf("%skerning records:%d\n", prefix,font->layout->kerningcount);
285     }
286     printf("%sstyle: %d\n", prefix,font->style);
287     printf("%sencoding: %02x\n", prefix,font->encoding);
288     printf("%slanguage: %02x\n", prefix,font->language);
289     int t;
290     for(t=0;t<font->numchars;t++) {
291         int u = font->glyph2ascii?font->glyph2ascii[t]:-1;
292         printf("%s== Glyph %d: advance=%d encoding=%d ==\n", prefix, t, font->glyph[t].advance, u);
293         SHAPE2* shape = swf_ShapeToShape2(font->glyph[t].shape);
294         SHAPELINE*line = shape->lines;
295
296         while(line) {
297             if(line->type == moveTo) {
298                 printf("%smoveTo %.2f %.2f\n", prefix, line->x/20.0, line->y/20.0);
299             } else if(line->type == lineTo) {
300                 printf("%slineTo %.2f %.2f\n", prefix, line->x/20.0, line->y/20.0);
301             } else if(line->type == splineTo) {
302                 printf("%ssplineTo (%.2f %.2f) %.2f %.2f\n", prefix,
303                         line->sx/20.0, line->sy/20.0,
304                         line->x/20.0, line->y/20.0
305                         );
306             }
307             line = line->next;
308         }
309         swf_Shape2Free(shape);
310         free(shape);
311     }
312     swf_FontFree(font);
313 }
314
315 SWF swf;
316 int fontnum = 0;
317 SWFFONT**fonts;
318
319 void textcallback(void*self, int*glyphs, int*ypos, int nr, int fontid, int fontsize, int startx, int starty, RGBA*color) 
320 {
321     int font=-1,t;
322     printf("                <%2d glyphs in font %2d size %d, color #%02x%02x%02x%02x> ",nr, fontid, fontsize, color->r, color->g, color->b, color->a);
323     for(t=0;t<fontnum;t++)
324     {
325         if(fonts[t]->id == fontid) {
326             font = t;
327             break;
328         }
329     }
330
331     for(t=0;t<nr;t++)
332     {
333         unsigned char a; 
334         if(font>=0) {
335             if(glyphs[t] >= fonts[font]->numchars  /*glyph is in range*/
336                     || !fonts[font]->glyph2ascii /* font has ascii<->glyph mapping */
337               ) a = glyphs[t];
338             else {
339                 if(fonts[font]->glyph2ascii[glyphs[t]])
340                     a = fonts[font]->glyph2ascii[glyphs[t]];
341                 else
342                     a = glyphs[t];
343             }
344         } else {
345             a = glyphs[t];
346         }
347         if(a>=32)
348             printf("%c", a);
349         else
350             printf("\\x%x", (int)a);
351     }
352     printf("\n");
353 }
354
355 void handleText(TAG*tag) 
356 {
357   printf("\n");
358   swf_ParseDefineText(tag,textcallback, 0);
359 }
360             
361 void handleDefineSound(TAG*tag)
362 {
363     U16 id = swf_GetU16(tag);
364     U8 flags = swf_GetU8(tag);
365     int compression = (flags>>4)&7;
366     int rate = (flags>>2)&3;
367     int bits = flags&2?16:8;
368     int stereo = flags&1;
369     printf(" (");
370     if(compression == 0) printf("Raw ");
371     else if(compression == 1) printf("ADPCM ");
372     else if(compression == 2) printf("MP3 ");
373     else if(compression == 3) printf("Raw little-endian ");
374     else if(compression == 6) printf("ASAO ");
375     else printf("? ");
376     if(rate == 0) printf("5.5Khz ");
377     if(rate == 1) printf("11Khz ");
378     if(rate == 2) printf("22Khz ");
379     if(rate == 3) printf("44Khz ");
380     printf("%dBit ", bits);
381     if(stereo) printf("stereo");
382     else printf("mono");
383     printf(")");
384 }
385
386 void handleDefineBits(TAG*tag)
387 {
388     U16 id;
389     U8 mode;
390     U16 width,height;
391     int bpp;
392     id = swf_GetU16(tag);
393     mode = swf_GetU8(tag);
394     width = swf_GetU16(tag);
395     height = swf_GetU16(tag);
396     printf(" image %dx%d",width,height);
397     if(mode == 3) printf(" (8 bpp)");
398     else if(mode == 4) printf(" (16 bpp)");
399     else if(mode == 5) printf(" (32 bpp)");
400     else printf(" (? bpp)");
401 }
402
403 void handleEditText(TAG*tag)
404 {
405     U16 id ;
406     U16 flags;
407     int t;
408     id = swf_GetU16(tag);
409     swf_GetRect(tag,0);
410     
411     //swf_ResetReadBits(tag);
412
413     if (tag->readBit)  
414     { tag->pos++; 
415       tag->readBit = 0; 
416     }
417     flags = swf_GetBits(tag,16);
418     if(flags & ET_HASFONT) {
419         swf_GetU16(tag); //font
420         swf_GetU16(tag); //fontheight
421     }
422     if(flags & ET_HASTEXTCOLOR) {
423         swf_GetU8(tag); //rgba
424         swf_GetU8(tag);
425         swf_GetU8(tag);
426         swf_GetU8(tag);
427     }
428     if(flags & ET_HASMAXLENGTH) {
429         swf_GetU16(tag); //maxlength
430     }
431     if(flags & ET_HASLAYOUT) {
432         swf_GetU8(tag); //align
433         swf_GetU16(tag); //left margin
434         swf_GetU16(tag); //right margin
435         swf_GetU16(tag); //indent
436         swf_GetU16(tag); //leading
437     }
438     printf(" variable \"%s\" ", &tag->data[tag->pos]);
439     if(flags & ET_HTML) printf("(html)");
440     if(flags & ET_NOSELECT) printf("(noselect)");
441     if(flags & ET_PASSWORD) printf("(password)");
442     if(flags & ET_READONLY) printf("(readonly)");
443
444     if(flags & (ET_X1 | ET_X3 ))
445     {
446         printf(" undefined flags: %08x (%08x)", (flags&(ET_X1|ET_X3)), flags);
447     }
448     
449     while(tag->data[tag->pos++]);
450     if(flags & ET_HASTEXT)
451    //  printf(" text \"%s\"\n", &tag->data[tag->pos]) //TODO
452         ;
453 }
454 void printhandlerflags(U32 handlerflags) 
455 {
456     if(handlerflags&1) printf("[on load]");
457     if(handlerflags&2) printf("[enter frame]");
458     if(handlerflags&4) printf("[unload]");
459     if(handlerflags&8) printf("[mouse move]");
460     if(handlerflags&16) printf("[mouse down]");
461     if(handlerflags&32) printf("[mouse up]");
462     if(handlerflags&64) printf("[key down]");
463     if(handlerflags&128) printf("[key up]");
464
465     if(handlerflags&256) printf("[data]");
466     if(handlerflags&512) printf("[initialize]");
467     if(handlerflags&1024) printf("[mouse press]");
468     if(handlerflags&2048) printf("[mouse release]");
469     if(handlerflags&4096) printf("[mouse release outside]");
470     if(handlerflags&8192) printf("[mouse rollover]");
471     if(handlerflags&16384) printf("[mouse rollout]");
472     if(handlerflags&32768) printf("[mouse drag over]");
473
474     if(handlerflags&0x10000) printf("[mouse drag out]");
475     if(handlerflags&0x20000) printf("[key press]");
476     if(handlerflags&0x40000) printf("[construct even]");
477     if(handlerflags&0xfff80000) printf("[???]");
478 }
479 void handleVideoStream(TAG*tag, char*prefix)
480 {
481     U16 id = swf_GetU16(tag);
482     U16 frames = swf_GetU16(tag);
483     U16 width = swf_GetU16(tag);
484     U16 height = swf_GetU16(tag);
485     U8 flags = swf_GetU8(tag); //5-2(videopacket 01=off 10=on)-1(smoothing 1=on)
486     U8 codec = swf_GetU8(tag);
487     printf(" (%d frames, %dx%d", frames, width, height);
488     if(flags&1)
489         printf(" smoothed");
490     if(codec == 2)
491         printf(" sorenson h.263)");
492     else
493         printf(" codec 0x%02x)", codec);
494 }
495 void handleVideoFrame(TAG*tag, char*prefix)
496 {
497     U32 code, version, reference, sizeflags;
498     U32 width=0, height=0;
499     U8 type;
500     U16 id = swf_GetU16(tag);
501     U16 frame = swf_GetU16(tag);
502     U8 deblock,flags, tmp, bit;
503     U32 quantizer;
504     char*types[] = {"I-frame", "P-frame", "disposable P-frame", "<reserved>"};
505     printf(" (frame %d) ", frame);
506
507     /* video packet follows */
508     code = swf_GetBits(tag, 17);
509     version = swf_GetBits(tag, 5);
510     reference = swf_GetBits(tag, 8);
511
512     sizeflags = swf_GetBits(tag, 3);
513     switch(sizeflags)
514     {
515         case 0: width = swf_GetBits(tag, 8); height = swf_GetBits(tag, 8); break;
516         case 1: width = swf_GetBits(tag, 16); height = swf_GetBits(tag, 16); break;
517         case 2: width = 352; height = 288; break;
518         case 3: width = 176; height = 144; break;
519         case 4: width = 128; height = 96; break;
520         case 5: width = 320; height = 240; break;
521         case 6: width = 160; height = 120; break;
522         case 7: width = -1; height = -1;/*reserved*/ break;
523     }
524     printf("%dx%d ", width, height);
525     type = swf_GetBits(tag, 2);
526     printf("%s", types[type]);
527
528     deblock = swf_GetBits(tag, 1);
529     if(deblock)
530         printf(" deblock ", deblock);
531     quantizer = swf_GetBits(tag, 5);
532     printf(" quant: %d ", quantizer);
533 }
534
535 void dumpFilter(FILTER*filter)
536 {
537     if(filter->type == FILTERTYPE_BLUR) {
538         FILTER_BLUR*f = (FILTER_BLUR*)filter;
539         printf("blurx: %f blury: %f\n", f->blurx, f->blury);
540         printf("passes: %d\n", f->passes);
541     } if(filter->type == FILTERTYPE_GLOW) {
542         FILTER_GLOW*f = (FILTER_GLOW*)filter;
543         printf("color %02x%02x%02x%02x\n", f->rgba.r,f->rgba.g,f->rgba.b,f->rgba.a);
544         printf("blurx: %f blury: %f strength: %f\n", f->blurx, f->blury, f->strength);
545         printf("passes: %d\n", f->passes);
546         printf("flags: %s%s%s\n", 
547                 f->knockout?"knockout ":"",
548                 f->composite?"composite ":"",
549                 f->innerglow?"innerglow":"");
550     } if(filter->type == FILTERTYPE_DROPSHADOW) {
551         FILTER_DROPSHADOW*f = (FILTER_DROPSHADOW*)filter;
552         printf("blurx: %f blury: %f\n", f->blurx, f->blury);
553         printf("passes: %d\n", f->passes);
554         printf("angle: %f distance: %f\n", f->angle, f->distance);
555         printf("strength: %f passes: %d\n", f->strength, f->passes);
556         printf("flags: %s%s%s\n", 
557                 f->knockout?"knockout ":"",
558                 f->composite?"composite ":"",
559                 f->innershadow?"innershadow ":"");
560     } if(filter->type == FILTERTYPE_BEVEL) {
561         FILTER_BEVEL*f = (FILTER_BEVEL*)filter;
562         printf("blurx: %f blury: %f\n", f->blurx, f->blury);
563         printf("passes: %d\n", f->passes);
564         printf("angle: %f distance: %f\n", f->angle, f->distance);
565         printf("strength: %f passes: %d\n", f->strength, f->passes);
566         printf("flags: %s%s%s%s\n", 
567                 f->ontop?"ontop":"",
568                 f->knockout?"knockout ":"",
569                 f->composite?"composite ":"",
570                 f->innershadow?"innershadow ":"");
571     } if(filter->type == FILTERTYPE_GRADIENTGLOW) {
572         FILTER_GRADIENTGLOW*f = (FILTER_GRADIENTGLOW*)filter;
573         swf_DumpGradient(stdout, f->gradient);
574         printf("blurx: %f blury: %f\n", f->blurx, f->blury);
575         printf("angle: %f distance: %f\n", f->angle, f->distance);
576         printf("strength: %f passes: %d\n", f->strength, f->passes);
577         printf("flags: %s%s%s%s\n", 
578                 f->knockout?"knockout ":"",
579                 f->ontop?"ontop ":"",
580                 f->composite?"composite ":"",
581                 f->innershadow?"innershadow ":"");
582     }
583     rfx_free(filter);
584 }
585
586 void handlePlaceObject23(TAG*tag, char*prefix)
587 {
588     U8 flags,flags2=0;
589     MATRIX m;
590     CXFORM cx;
591     char pstr[3][256];
592     int ppos[3] = {0,0,0};
593     swf_SetTagPos(tag, 0);
594     flags = swf_GetU8(tag);
595     if(tag->id == ST_PLACEOBJECT3)
596         flags2 = swf_GetU8(tag);
597     swf_GetU16(tag); //depth
598
599     //flags&1: move
600     if(flags&2) swf_GetU16(tag); //id
601     if(flags&4) {
602         swf_GetMatrix(tag,&m);
603         if(placements) {
604             ppos[0] += sprintf(pstr[0], "| Matrix             ");
605             ppos[1] += sprintf(pstr[1], "| %5.3f %5.3f %6.2f ", m.sx/65536.0, m.r1/65536.0, m.tx/20.0);
606             ppos[2] += sprintf(pstr[2], "| %5.3f %5.3f %6.2f ", m.r0/65536.0, m.sy/65536.0, m.ty/20.0);
607         }
608     }
609     if(flags&8) {
610         swf_GetCXForm(tag, &cx, 1);
611         if(placements) {
612             ppos[0] += sprintf(pstr[0]+ppos[0], "| CXForm    r    g    b    a ");
613             ppos[1] += sprintf(pstr[1]+ppos[1], "| mul    %4.1f %4.1f %4.1f %4.1f ", cx.r0/256.0, cx.g0/256.0, cx.b0/256.0, cx.a0/256.0);
614             ppos[2] += sprintf(pstr[2]+ppos[2], "| add    %4d %4d %4d %4d ", cx.r1, cx.g1, cx.b1, cx.a1);
615         }
616     }
617     if(flags&16) {
618         U16 ratio = swf_GetU16(tag); //ratio
619         if(placements) {
620             ppos[0] += sprintf(pstr[0]+ppos[0], "| Ratio ");
621             ppos[1] += sprintf(pstr[1]+ppos[1], "| %-5d ", ratio);
622             ppos[2] += sprintf(pstr[2]+ppos[2], "|       ");
623         }
624     }
625     if(flags&64) {
626         U16 clip = swf_GetU16(tag); //clip
627         if(placements) {
628             ppos[0] += sprintf(pstr[0]+ppos[0], "| Clip  ");
629             ppos[1] += sprintf(pstr[1]+ppos[1], "| %-4d ", clip);
630             ppos[2] += sprintf(pstr[2]+ppos[2], "|       ");
631         }
632     }
633     if(flags&32) { while(swf_GetU8(tag)); }
634
635     if(flags2&1) { // filter list
636         U8 num = swf_GetU8(tag);
637         if(placements)
638             printf("\n%d filters\n", num);
639         char*filtername[] = {"dropshadow","blur","glow","bevel","gradientglow","convolution","colormatrix","gradientbevel"};
640         int t;
641         for(t=0;t<num;t++) {
642             FILTER*filter = swf_GetFilter(tag);
643             if(!filter) {
644                 printf("\n"); 
645                 return;
646             }
647             if(placements) {
648                 printf("filter %d: %02x (%s)\n", t, filter->type, (filter->type<sizeof(filtername)/sizeof(filtername[0]))?filtername[filter->type]:"?");
649                 dumpFilter(filter);
650             }
651         }
652     }
653     if(flags2&2) { // blend mode
654         U8 blendmode = swf_GetU8(tag);
655         if(placements) {
656             int t;
657             char name[80];
658             sprintf(name, "%-5d", blendmode);
659             for(t=0;blendModeNames[t];t++) {
660                 if(blendmode==t) {
661                     sprintf(name, "%-5s", blendModeNames[t]);
662                     break;
663                 }
664             }
665             ppos[0] += sprintf(pstr[0]+ppos[0], "| Blend ");
666             ppos[1] += sprintf(pstr[1]+ppos[1], "| %s ", name);
667             ppos[2] += sprintf(pstr[2]+ppos[2], "|       ");
668         }
669     }
670
671     if(placements && ppos[0]) {
672         printf("\n");
673         printf("%s %s\n", prefix, pstr[0]);
674         printf("%s %s\n", prefix, pstr[1]);
675         printf("%s %s", prefix, pstr[2]);
676     }
677     if(flags&128) {
678       if (action) {
679         U16 reserved;
680         U32 globalflags;
681         U32 handlerflags;
682         char is32 = 0;
683         printf("\n");
684         reserved = swf_GetU16(tag); // must be 0
685         globalflags = swf_GetU16(tag); //TODO: 32 if version>=6
686         if(reserved) {
687             printf("Unknown parameter field not zero: %04x\n", reserved);
688             return;
689         }
690         printf("global flags: %04x\n", globalflags);
691
692         handlerflags = swf_GetU16(tag); //TODO: 32 if version>=6
693         if(!handlerflags) {
694             handlerflags = swf_GetU32(tag);
695             is32 = 1;
696         }
697         while(handlerflags)  {
698             int length;
699             int t;
700             ActionTAG*a;
701
702             globalflags &= ~handlerflags;
703             printf("%s flags %08x ",prefix, handlerflags);
704             printhandlerflags(handlerflags);
705             length = swf_GetU32(tag);
706             printf(", %d bytes actioncode\n",length);
707             a = swf_ActionGet(tag);
708             swf_DumpActions(a,prefix);
709             swf_ActionFree(a);
710
711             handlerflags = is32?swf_GetU32(tag):swf_GetU16(tag); //TODO: 32 if version>=6
712         }
713         if(globalflags) // should go to sterr.
714             printf("ERROR: unsatisfied handlerflags: %02x\n", globalflags);
715     } else {
716       printf(" has action code\n");
717     }
718     } else printf("\n");
719 }
720
721 void handlePlaceObject(TAG*tag, char*prefix)
722 {
723     TAG*tag2 = swf_InsertTag(0, ST_PLACEOBJECT2);
724     U16 id, depth;
725     MATRIX matrix; 
726     CXFORM cxform;
727
728     swf_SetTagPos(tag, 0);
729     id = swf_GetU16(tag);
730     depth = swf_GetU16(tag);
731     swf_GetMatrix(tag, &matrix);
732     swf_GetCXForm(tag, &cxform, 0);
733
734     swf_SetU8(tag2, 14 /* char, matrix, cxform */);
735     swf_SetU16(tag2, depth);
736     swf_SetU16(tag2, id);
737     swf_SetMatrix(tag2, &matrix);
738     swf_SetCXForm(tag2, &cxform, 1);
739
740     handlePlaceObject23(tag2, prefix);
741 }
742 char stylebuf[256];
743 char* fillstyle2str(FILLSTYLE*style)
744 {
745     switch(style->type) {
746         case 0x00:
747             sprintf(stylebuf, "SOLID %02x%02x%02x%02x", style->color.r, style->color.g, style->color.b, style->color.a);
748             break;
749         case 0x10: case 0x11: case 0x12: case 0x13:
750             sprintf(stylebuf, "GRADIENT (%d steps)", style->gradient.num);
751             break;
752         case 0x40: case 0x42:
753             /* TODO: display information about that bitmap */
754             sprintf(stylebuf, "BITMAPt%s %d", (style->type&2)?"n":"", style->id_bitmap);
755             /* TODO: show matrix */
756             break;
757         case 0x41: case 0x43:
758             /* TODO: display information about that bitmap */
759             sprintf(stylebuf, "BITMAPc%s %d", (style->type&2)?"n":"", style->id_bitmap);
760             /* TODO: show matrix */
761             break;
762         default:
763             sprintf(stylebuf, "UNKNOWN[%02x]",style->type);
764     }
765     return stylebuf;
766 }
767 char* linestyle2str(LINESTYLE*style)
768 {
769     sprintf(stylebuf, "%.2f %02x%02x%02x%02x", style->width/20.0, style->color.r, style->color.g, style->color.b, style->color.a);
770     return stylebuf;
771 }
772
773 void handleShape(TAG*tag, char*prefix)
774 {
775     SHAPE2 shape;
776     SHAPELINE*line;
777     int t,max;
778
779     tag->pos = 0;
780     tag->readBit = 0;
781     swf_ParseDefineShape(tag, &shape);
782
783     max = shape.numlinestyles > shape.numfillstyles?shape.numlinestyles:shape.numfillstyles;
784
785     if(max) printf("%s | fillstyles(%02d)        linestyles(%02d)\n", 
786             prefix,
787             shape.numfillstyles,
788             shape.numlinestyles
789             );
790     else    printf("%s | (Neither line nor fill styles)\n", prefix);
791
792     for(t=0;t<max;t++) {
793         printf("%s", prefix);
794         if(t < shape.numfillstyles) {
795             printf(" | %-2d) %-18.18s", t+1, fillstyle2str(&shape.fillstyles[t]));
796         } else {
797             printf("                         ");
798         }
799         if(t < shape.numlinestyles) {
800             printf("%-2d) %s", t+1, linestyle2str(&shape.linestyles[t]));
801         }
802         printf("\n");
803     }
804
805     printf("%s |\n", prefix);
806
807     line = shape.lines;
808     while(line) {
809         printf("%s | fill: %02d/%02d line:%02d - ",
810                 prefix, 
811                 line->fillstyle0,
812                 line->fillstyle1,
813                 line->linestyle);
814         if(line->type == moveTo) {
815             printf("moveTo %.2f %.2f\n", line->x/20.0, line->y/20.0);
816         } else if(line->type == lineTo) {
817             printf("lineTo %.2f %.2f\n", line->x/20.0, line->y/20.0);
818         } else if(line->type == splineTo) {
819             printf("splineTo (%.2f %.2f) %.2f %.2f\n", 
820                     line->sx/20.0, line->sy/20.0,
821                     line->x/20.0, line->y/20.0
822                     );
823         }
824         line = line->next;
825     }
826     printf("%s |\n", prefix);
827 }
828     
829 void fontcallback1(void*self, U16 id,U8 * name)
830 { fontnum++;
831 }
832
833 void fontcallback2(void*self, U16 id,U8 * name)
834
835   swf_FontExtract(&swf,id,&fonts[fontnum]);
836   fontnum++;
837 }
838
839 static U8 printable(U8 a)
840 {
841     if(a<32 || a==127) return '.';
842     else return a;
843 }
844 void hexdumpTag(TAG*tag, char* prefix)
845 {
846     int t;
847     char ascii[32];
848     printf("                %s-=> ",prefix);
849     for(t=0;t<tag->len;t++) {
850         printf("%02x ", tag->data[t]);
851         ascii[t&15] = printable(tag->data[t]);
852         if((t && ((t&15)==15)) || (t==tag->len-1))
853         {
854             int s,p=((t)&15)+1;
855             ascii[p] = 0;
856             for(s=p-1;s<16;s++) {
857                 printf("   ");
858             }
859             if(t==tag->len-1)
860                 printf(" %s\n", ascii);
861             else
862                 printf(" %s\n                %s-=> ",ascii,prefix);
863         }
864     }
865 }
866
867 void handleExportAssets(TAG*tag, char* prefix)
868 {
869     int num;
870     U16 id;
871     char* name;
872     int t;
873     num = swf_GetU16(tag);
874     for(t=0;t<num;t++)
875     {
876         id = swf_GetU16(tag);
877         name = swf_GetString(tag);
878         printf("%sexports %04d as \"%s\"\n", prefix, id, name);
879     }
880 }
881
882 void dumperror(const char* format, ...)
883 {
884     char buf[1024];
885     va_list arglist;
886
887     va_start(arglist, format);
888     vsprintf(buf, format, arglist);
889     va_end(arglist);
890
891     if(!html && !xy)
892         printf("==== Error: %s ====\n", buf);
893 }
894
895 static char strbuf[800];
896 static int bufpos=0;
897
898 char* timestring(double f)
899 {
900     int hours   = (int)(f/3600);
901     int minutes = (int)((f-hours*3600)/60);
902     int seconds = (int)((f-hours*3600-minutes*60));
903     int useconds = (int)((f-(int)f)*1000+0.5);
904     bufpos+=100;
905     bufpos%=800;
906     sprintf(&strbuf[bufpos], "%02d:%02d:%02d,%03d",hours,minutes,seconds,useconds);
907     return &strbuf[bufpos];
908 }
909
910 int main (int argc,char ** argv)
911
912     TAG*tag;
913 #ifdef HAVE_STAT
914     struct stat statbuf;
915 #endif
916     int f;
917     int xsize,ysize;
918     char issprite = 0; // are we inside a sprite definition?
919     int spriteframe = 0;
920     int mainframe=0;
921     char* spriteframelabel = 0;
922     char* framelabel = 0;
923     char prefix[128];
924     int filesize = 0;
925     int filepos = 0;
926     prefix[0] = 0;
927     memset(idtab,0,65536);
928
929     processargs(argc, argv);
930
931     if(!filename)
932     {
933         fprintf(stderr, "You must supply a filename.\n");
934         return 1;
935     }
936
937     f = open(filename,O_RDONLY|O_BINARY);
938     if (f<0)
939     { 
940         char buffer[256];
941         sprintf(buffer, "Couldn't open %s", filename);
942         perror(buffer);
943         exit(1);
944     }
945     char header[3];
946     read(f, header, 3);
947     int compressed = (header[0]=='C');
948     close(f);
949     f = open(filename,O_RDONLY|O_BINARY);
950
951     if FAILED(swf_ReadSWF(f,&swf))
952     { 
953         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
954         close(f);
955         exit(1);
956     }
957
958 #ifdef HAVE_STAT
959     fstat(f, &statbuf);
960     if(statbuf.st_size != swf.fileSize && !compressed)
961         dumperror("Real Filesize (%d) doesn't match header Filesize (%d)",
962                 statbuf.st_size, swf.fileSize);
963     filesize = statbuf.st_size;
964 #endif
965
966     close(f);
967
968     //if(action && swf.fileVersion>=9) {
969     //    fprintf(stderr, "Actionscript parsing (-a) not yet supported for SWF versions>=9\n");
970     //    action = 0;
971     //}
972
973     xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
974     ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
975     if(xy)
976     {
977         if(xy&1)
978         printf("-X %d", xsize);
979
980         if((xy&1) && (xy&6))
981         printf(" ");
982
983         if(xy&2)
984         printf("-Y %d", ysize);
985         
986         if((xy&3) && (xy&4))
987         printf(" ");
988
989         if(xy&4)
990         printf("-r %.2f", swf.frameRate/256.0);
991         
992         if((xy&7) && (xy&8))
993         printf(" ");
994         
995         if(xy&8)
996         printf("-f %d", swf.frameCount);
997         
998         printf("\n");
999         return 0;
1000     }
1001     if(html)
1002     {
1003         char*fileversions[] = {"","1,0,0,0", "2,0,0,0","3,0,0,0","4,0,0,0",
1004                                "5,0,0,0","6,0,23,0","7,0,0,0","8,0,0,0","9,0,0,0"};
1005         if(swf.fileVersion>9) {
1006             fprintf(stderr, "Fileversion>9\n");
1007             exit(1);
1008         }
1009
1010         if(xhtml) {
1011             printf("<object type=\"application/x-shockwave-flash\" data=\"%s\" width=\"%d\" height=\"%d\">\n"
1012                             "<param name=\"movie\" value=\"%s\"/>\n"
1013                             "<param name=\"play\" value=\"true\"/>\n"
1014                             "<param name=\"loop\" value=\"false\"/>\n"
1015                             "<param name=\"quality\" value=\"high\"/>\n"
1016                             "<param name=\"loop\" value=\"false\"/>\n"
1017                             "</object>\n\n", filename, xsize, ysize, filename);
1018         } else {
1019             printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
1020                    " WIDTH=\"%d\"\n"
1021                    //" BGCOLOR=#ffffffff\n"?
1022                    " HEIGHT=\"%d\"\n"
1023                    //http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,23,0?
1024                    " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%s\">\n"
1025                    "  <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
1026                    "  <PARAM NAME=\"PLAY\" VALUE=\"true\">\n" 
1027                    "  <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
1028                    "  <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
1029                    "  <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n" //bgcolor=#ffffff?
1030                    "   PLAY=\"true\" ALIGN=\"\" LOOP=\"true\" QUALITY=\"high\"\n"
1031                    "   TYPE=\"application/x-shockwave-flash\"\n"
1032                    "   PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\">\n"
1033                    "  </EMBED>\n" 
1034                    "</OBJECT>\n", xsize, ysize, fileversions[swf.fileVersion], 
1035                                   filename, filename, xsize, ysize);
1036         }
1037         return 0;
1038     } 
1039     printf("[HEADER]        File version: %d\n", swf.fileVersion);
1040     if(compressed) {
1041         printf("[HEADER]        File is zlib compressed.");
1042         if(filesize && swf.fileSize)
1043             printf(" Ratio: %02d%%\n", filesize*100/(swf.fileSize));
1044         else
1045             printf("\n");
1046     }
1047     printf("[HEADER]        File size: %ld%s\n", swf.fileSize, swf.compressed?" (Depacked)":"");
1048     printf("[HEADER]        Frame rate: %f\n",swf.frameRate/256.0);
1049     printf("[HEADER]        Frame count: %d\n",swf.frameCount);
1050     printf("[HEADER]        Movie width: %.2f",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
1051     if(swf.movieSize.xmin)
1052         printf(" (left offset: %.2f)\n", swf.movieSize.xmin/20.0);
1053     else 
1054         printf("\n");
1055     printf("[HEADER]        Movie height: %.2f",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
1056     if(swf.movieSize.ymin)
1057         printf(" (top offset: %.2f)\n", swf.movieSize.ymin/20.0);
1058     else 
1059         printf("\n");
1060
1061     tag = swf.firstTag;
1062    
1063     if(showtext) {
1064         fontnum = 0;
1065         swf_FontEnumerate(&swf,&fontcallback1, 0);
1066         fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
1067         fontnum = 0;
1068         swf_FontEnumerate(&swf,&fontcallback2, 0);
1069     }
1070
1071     while(tag) {
1072         char*name = swf_TagGetName(tag);
1073         char myprefix[128];
1074         if(!name) {
1075             dumperror("Unknown tag:0x%03x", tag->id);
1076             //tag = tag->next;
1077             //continue;
1078         }
1079         if(!name) {
1080             name = "UNKNOWN TAG";
1081         }
1082         if(cumulative) {
1083             filepos += tag->len;
1084             printf("[%03x] %9ld %9ld %s%s", tag->id, tag->len, filepos, prefix, swf_TagGetName(tag));
1085         } else {
1086             printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
1087         }
1088         
1089         if(swf_isDefiningTag(tag)) {
1090             U16 id = swf_GetDefineID(tag);
1091             printf(" defines id %04d", id);
1092             if(idtab[id])
1093                 dumperror("Id %04d is defined more than once.", id);
1094             idtab[id] = 1;
1095         }
1096         else if(swf_isPseudoDefiningTag(tag)) {
1097             U16 id = swf_GetDefineID(tag);
1098             printf(" adds information to id %04d", id);
1099             if(!idtab[id])
1100                 dumperror("Id %04d is not yet defined.\n", id);
1101         }
1102         else if(tag->id == ST_PLACEOBJECT) {
1103             printf(" places id %04d at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
1104             if(swf_GetName(tag))
1105                 printf(" name \"%s\"",swf_GetName(tag));
1106         }
1107         else if(tag->id == ST_PLACEOBJECT2 || tag->id == ST_PLACEOBJECT3) {
1108             if(tag->data[0]&1)
1109                 printf(" moves");
1110             else
1111                 printf(" places");
1112             
1113             if(tag->data[0]&2)
1114                 printf(" id %04d",swf_GetPlaceID(tag));
1115             else
1116                 printf(" object");
1117
1118             printf(" at depth %04d", swf_GetDepth(tag));
1119
1120             if(tag->id == ST_PLACEOBJECT3 && tag->data[1]&4)
1121                 printf(" as bitmap");
1122            
1123             swf_SetTagPos(tag, 0);
1124             if(tag->data[0]&64) {
1125                 SWFPLACEOBJECT po;
1126                 swf_GetPlaceObject(tag, &po);
1127                 printf(" (clip to %04d)", po.clipdepth);
1128                 swf_PlaceObjectFree(&po);
1129             }
1130             if(swf_GetName(tag))
1131                 printf(" name \"%s\"",swf_GetName(tag));
1132
1133         }
1134         else if(tag->id == ST_REMOVEOBJECT) {
1135             printf(" removes id %04d from depth %04d", swf_GetPlaceID(tag), swf_GetDepth(tag));
1136         }
1137         else if(tag->id == ST_REMOVEOBJECT2) {
1138             printf(" removes object from depth %04d", swf_GetDepth(tag));
1139         }
1140         else if(tag->id == ST_FREECHARACTER) {
1141             printf(" frees object %04d", swf_GetPlaceID(tag));
1142         }
1143         else if(tag->id == ST_STARTSOUND) {
1144             U8 flags;
1145             U16 id;
1146             id = swf_GetU16(tag);
1147             flags = swf_GetU8(tag);
1148             if(flags & 32)
1149                 printf(" stops sound with id %04d", id);
1150             else
1151                 printf(" starts sound with id %04d", id);
1152             if(flags & 16)
1153                 printf(" (if not already playing)");
1154             if(flags & 1)
1155                 swf_GetU32(tag);
1156             if(flags & 2)
1157                 swf_GetU32(tag);
1158             if(flags & 4) {
1159                 printf(" looping %d times", swf_GetU16(tag));
1160             }
1161         }
1162         else if(tag->id == ST_FRAMELABEL) {
1163             int l = strlen(tag->data);
1164             printf(" \"%s\"", tag->data);
1165             if((l+1) < tag->len) {
1166                 printf(" has %d extra bytes", tag->len-1-l);
1167                 if(tag ->len - (l+1) == 1 && tag->data[tag->len-1] == 1)
1168                     printf(" (ANCHOR)");
1169             }
1170             if((framelabel && !issprite) ||
1171                (spriteframelabel && issprite)) {
1172                 dumperror("Frame %d has more than one label", 
1173                         issprite?spriteframe:mainframe);
1174             }
1175             if(issprite) spriteframelabel = tag->data;
1176             else framelabel = tag->data;
1177         }
1178         else if(tag->id == ST_SHOWFRAME) {
1179             char*label = issprite?spriteframelabel:framelabel;
1180             int frame = issprite?spriteframe:mainframe;
1181             int nframe = frame;
1182             if(!label) {
1183                 while(tag->next && tag->next->id == ST_SHOWFRAME && tag->next->len == 0) {
1184                     tag = tag->next;
1185                     if(issprite) spriteframe++;
1186                     else mainframe++;
1187                     nframe++;
1188                 }
1189             }
1190             if(nframe == frame)
1191                 printf(" %d (%s)", frame+1, timestring(frame*(256.0/(swf.frameRate+0.1))));
1192             else
1193                 printf(" %d-%d (%s-%s)", frame+1, nframe+1,
1194                         timestring(frame*(256.0/(swf.frameRate+0.1))),
1195                         timestring(nframe*(256.0/(swf.frameRate+0.1)))
1196                         );
1197             if(label)
1198                 printf(" (label \"%s\")", label);
1199             if(issprite) {spriteframe++; spriteframelabel = 0;}
1200             if(!issprite) {mainframe++; framelabel = 0;}
1201         }
1202
1203         if(tag->id == ST_SETBACKGROUNDCOLOR) {
1204             U8 r = swf_GetU8(tag);
1205             U8 g = swf_GetU8(tag);
1206             U8 b = swf_GetU8(tag);
1207             printf(" (%02x/%02x/%02x)\n",r,g,b);
1208         }
1209         else if(tag->id == ST_PROTECT) {
1210             if(tag->len>0) {
1211                 printf(" %s\n", swf_GetString(tag));
1212             } else {
1213                 printf("\n");
1214             }
1215         }
1216         else if(tag->id == ST_CSMTEXTSETTINGS) {
1217             U16 id = swf_GetU16(tag);
1218             U8 flags = swf_GetU8(tag);
1219             printf(" (");
1220             if(flags&0x40) {
1221                 printf("flashtype,");
1222             }
1223             switch(((flags>>3)&7)) {
1224                 case 0:printf("no grid,");break;
1225                 case 1:printf("pixel grid,");break;
1226                 case 2:printf("subpixel grid,");break;
1227                 case 3:printf("unknown grid,");break;
1228             }
1229             if(flags&0x87) 
1230                 printf("unknown[%08x],", flags);
1231             float thickness = swf_GetFixed(tag);
1232             float sharpness = swf_GetFixed(tag);
1233             printf("s=%.2f,t=%.2f)\n", thickness, sharpness);
1234             swf_GetU8(tag);
1235         }
1236         else if(tag->id == ST_DEFINEBITSLOSSLESS ||
1237            tag->id == ST_DEFINEBITSLOSSLESS2) {
1238             handleDefineBits(tag);
1239             printf("\n");
1240         }
1241         else if(tag->id == ST_DEFINESOUND) {
1242             handleDefineSound(tag);
1243             printf("\n");
1244         }
1245         else if(tag->id == ST_VIDEOFRAME) {
1246             handleVideoFrame(tag, myprefix);
1247             printf("\n");
1248         }
1249         else if(tag->id == ST_DEFINEVIDEOSTREAM) {
1250             handleVideoStream(tag, myprefix);
1251             printf("\n");
1252         }
1253         else if(tag->id == ST_DEFINEEDITTEXT) {
1254             handleEditText(tag);
1255             printf("\n");
1256         }
1257         else if(tag->id == ST_DEFINEMOVIE) {
1258             U16 id = swf_GetU16(tag);
1259             char*s = swf_GetString(tag);
1260             printf(" URL: %s\n", s);
1261         }
1262         else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
1263             if(showtext)
1264                 handleText(tag);
1265             else
1266                 printf("\n");
1267         }
1268         else if(tag->id == ST_DEFINESCALINGGRID) {
1269             U16 id = swf_GetU16(tag);
1270             SRECT r;
1271             swf_GetRect(tag, &r);
1272             printf(" (%.2f,%.2f)-(%.2f,%.2f)\n", r.xmin/20.0, r.ymin/20.0, r.xmax/20.0, r.ymax/20.0);
1273         }
1274         else if(tag->id == ST_PLACEOBJECT2 || tag->id == ST_PLACEOBJECT3) {
1275         }
1276         else if(tag->id == ST_NAMECHARACTER || tag->id==ST_DEFINEFONTNAME) {
1277             swf_GetU16(tag);
1278             printf(" \"%s\"\n", swf_GetString(tag));
1279         }
1280         else {
1281             printf("\n");
1282         }
1283
1284         if(bbox && swf_isDefiningTag(tag) && tag->id != ST_DEFINESPRITE) {
1285             SRECT r = swf_GetDefineBBox(tag);
1286             printf("                %s bbox [%.2f, %.2f, %.2f, %.2f]\n", prefix,
1287                     r.xmin/20.0,
1288                     r.ymin/20.0,
1289                     r.xmax/20.0,
1290                     r.ymax/20.0);
1291         }
1292         
1293         sprintf(myprefix, "                %s", prefix);
1294
1295         if(tag->id == ST_DEFINESPRITE) {
1296             sprintf(prefix, "         ");
1297             if(issprite) {
1298                 dumperror("Sprite definition inside a sprite definition");
1299             }
1300             issprite = 1;
1301             spriteframe = 0;
1302             spriteframelabel = 0;
1303         }
1304         else if(tag->id == ST_END) {
1305             *prefix = 0;
1306             issprite = 0;
1307             spriteframelabel = 0;
1308             if(tag->len)
1309                 dumperror("End Tag not empty");
1310         }
1311         else if(tag->id == ST_EXPORTASSETS || tag->id == ST_SYMBOLCLASS) {
1312             handleExportAssets(tag, myprefix);
1313         }
1314         else if(tag->id == ST_DOACTION && action) {
1315             ActionTAG*actions;
1316             actions = swf_ActionGet(tag);
1317             swf_DumpActions(actions, myprefix);
1318         }
1319         else if(tag->id == ST_DOABC && action) {
1320             swf_DissassembleABC(tag);
1321         }
1322         else if(tag->id == ST_DOINITACTION && action) {
1323             ActionTAG*actions;
1324             swf_GetU16(tag); // id
1325             actions = swf_ActionGet(tag);
1326             swf_DumpActions(actions, myprefix);
1327         }
1328         else if(tag->id == ST_DEFINEBUTTON && action) {
1329             dumpButtonActions(tag, myprefix);
1330         }
1331         else if(swf_isFontTag(tag) && showfonts) {
1332             dumpFont(tag, myprefix);
1333         }
1334         else if(tag->id == ST_DEFINEBUTTON2 && action) {
1335             dumpButton2Actions(tag, myprefix);
1336         }
1337         else if(tag->id == ST_PLACEOBJECT) {
1338             handlePlaceObject(tag, myprefix);
1339         }
1340         else if(tag->id == ST_PLACEOBJECT2 || tag->id == ST_PLACEOBJECT3) {
1341             handlePlaceObject23(tag, myprefix);
1342         }
1343         else if(tag->id == ST_DEFINEFONTNAME) {
1344             swf_SetTagPos(tag, 0);
1345             swf_GetU16(tag); //id
1346             swf_GetString(tag); //name
1347             char* copyright = swf_GetString(tag);
1348             printf("%s%s\n", myprefix, copyright);
1349         }
1350         else if(tag->id == ST_DEFINESHAPE ||
1351                 tag->id == ST_DEFINESHAPE2 ||
1352                 tag->id == ST_DEFINESHAPE3 ||
1353                 tag->id == ST_DEFINESHAPE4) {
1354             if(showshapes)
1355                 handleShape(tag, myprefix);
1356         }
1357
1358         if(tag->len && used) {
1359             int num = swf_GetNumUsedIDs(tag);
1360             int* used;
1361             int t;
1362             if(num) {
1363                 used = (int*)malloc(sizeof(int)*num);
1364                 swf_GetUsedIDs(tag, used);
1365                 printf("%s%suses IDs: ", indent, prefix);
1366                 for(t=0;t<num;t++) {
1367                     int id;
1368                     swf_SetTagPos(tag, used[t]);
1369                     id = swf_GetU16(tag);
1370                     printf("%d%s", id, t<num-1?", ":"");
1371                     if(!idtab[id]) {
1372                         dumperror("Id %04d is not yet defined.\n", id);
1373                     }
1374                 }
1375                 printf("\n");
1376             }
1377         }
1378         
1379         if(tag->id == ST_FREECHARACTER) {
1380             U16 id;
1381             swf_SetTagPos(tag, 0);
1382             id = swf_GetU16(tag);
1383             idtab[id] = 0;
1384         }
1385
1386         if(tag->len && hex) {
1387             hexdumpTag(tag, prefix);
1388         }
1389         tag = tag->next;
1390         fflush(stdout);
1391     }
1392
1393     swf_FreeTags(&swf);
1394     return 0;
1395 }
1396
1397