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