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