support for DEFINEMOVIE added.
[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 file is distributed under the GPL, see file COPYING for details */
9
10 #include "../config.h"
11
12 #ifdef HAVE_SYS_STAT_H
13 #include <sys/stat.h>
14 #else
15 #undef HAVE_STAT
16 #endif
17
18 #ifdef HAVE_SYS_TYPES_H
19 #include <sys/types.h>
20 #else
21 #undef HAVE_STAT
22 #endif
23
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <stdarg.h>
28 #include "../lib/rfxswf.h"
29 #include "../lib/args.h"
30
31 static char * filename = 0;
32
33 /* idtab stores the ids which are defined in the file. This allows us
34    to detect errors in the file. (i.e. ids which are defined more than 
35    once */
36 static char idtab[65536];
37 static char * indent = "                ";
38
39 static int placements = 0;
40 static int action = 0;
41 static int html = 0;
42 static int xy = 0;
43 static int showtext = 0;
44 static int hex = 0;
45 static int used = 0;
46
47 struct options_t options[] =
48 {
49  {"D","full"},
50  {"a","action"},
51  {"t","text"},
52  {"X","width"},
53  {"Y","height"},
54  {"f","frames"},
55  {"r","rate"},
56  {"e","html"},
57  {"p","placements"},
58  {"u","used"},
59  {"v","verbose"},
60  {"V","version"},
61  {"d","hex"},
62  {0,0}
63 };
64
65
66 int args_callback_option(char*name,char*val)
67 {
68     if(!strcmp(name, "V")) {
69         printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
70         exit(0);
71     } 
72     else if(name[0]=='a') {
73         action = 1;
74         return 0;
75     }
76     else if(name[0]=='p') {
77         placements = 1;
78         return 0;
79     }
80     else if(name[0]=='t') {
81         showtext = 1;
82         return 0;
83     }
84     else if(name[0]=='e') {
85         html = 1;
86         return 0;
87     }
88     else if(name[0]=='X') {
89         xy |= 1;
90         return 0;
91     }
92     else if(name[0]=='Y') {
93         xy |= 2;
94         return 0;
95     }
96     else if(name[0]=='r') {
97         xy |= 4;
98         return 0;
99     }
100     else if(name[0]=='f') {
101         xy |= 8;
102         return 0;
103     }
104     else if(name[0]=='d') {
105         hex = 1;
106         return 0;
107     }
108     else if(name[0]=='u') {
109         used = 1;
110         return 0;
111     }
112     else if(name[0]=='D') {
113         action = placements = showtext = 1;
114         return 0;
115     }
116     else {
117         printf("Unknown option: -%s\n", name);
118         exit(1);
119     }
120
121     return 0;
122 }
123 int args_callback_longoption(char*name,char*val)
124 {
125     return args_long2shortoption(options, name, val);
126 }
127 void args_callback_usage(char*name)
128 {    
129     printf("Usage: %s [-at] file.swf\n", name);
130     printf("\t-h , --help\t\t Print help and exit\n");
131     printf("\t-D , --full\t\t Show everything. The same as -atMp\n");
132     printf("\t-e , --html\t\t Create html output embedding the file (simple, but useful)\n");
133     printf("\t-X , --width\t\t Prints out a string of the form \"-X width\"\n");
134     printf("\t-Y , --height\t\t Prints out a string of the form \"-Y height\"\n");
135     printf("\t-r , --rate\t\t Prints out a string of the form \"-r rate\"\n");
136     printf("\t-f , --frames\t\t Prints out a string of the form \"-f framenum\"\n");
137     printf("\t-a , --action\t\t Disassemble action tags\n");
138     printf("\t-p , --placements\t Show extra placement information\n");
139     printf("\t-t , --text\t\t Show text data\n");
140     printf("\t-d , --hex\t\t Print hex output of tag data, too\n");
141     printf("\t-u , --used\t\t Show referred IDs for each Tag\n");
142     printf("\t-V , --version\t\t Print program version and exit\n");
143 }
144 int args_callback_command(char*name,char*val)
145 {
146     if(filename) {
147         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
148                  filename, name);
149     }
150     filename = name;
151     return 0;
152 }
153
154 char* what;
155 char* testfunc(char*str)
156 {
157     printf("%s: %s\n", what, str);
158     return 0;
159 }
160
161 void dumpButton2Actions(TAG*tag, char*prefix)
162 {
163     U32 oldTagPos;
164     U32 offsetpos;
165     U32 condition;
166
167     oldTagPos = swf_GetTagPos(tag);
168
169     // scan DefineButton2 Record
170     
171     swf_GetU16(tag);          // Character ID
172     swf_GetU8(tag);           // Flags;
173
174     offsetpos = swf_GetTagPos(tag);  // first offset
175     swf_GetU16(tag);
176
177     while (swf_GetU8(tag))      // state  -> parse ButtonRecord
178     { swf_GetU16(tag);          // id
179       swf_GetU16(tag);          // layer
180       swf_GetMatrix(tag,NULL);  // matrix
181       swf_GetCXForm(tag,NULL,1);  // cxform
182     }
183
184     while(offsetpos)
185     { U8 a;
186       ActionTAG*actions;
187
188       if(tag->pos >= tag->len)
189           break;
190         
191       offsetpos = swf_GetU16(tag);
192       condition = swf_GetU16(tag);                // condition
193       
194       actions = swf_ActionGet(tag);
195       printf("%s condition %04x\n", prefix, condition);
196       swf_DumpActions(actions, prefix);
197     }
198     
199     swf_SetTagPos(tag,oldTagPos);
200     return;
201 }
202
203 void dumpButtonActions(TAG*tag, char*prefix)
204 {
205     ActionTAG*actions;
206     swf_GetU16(tag); // id
207     while (swf_GetU8(tag))      // state  -> parse ButtonRecord
208     { swf_GetU16(tag);          // id
209       swf_GetU16(tag);          // layer
210       swf_GetMatrix(tag,NULL);  // matrix
211     }
212     actions = swf_ActionGet(tag);
213     swf_DumpActions(actions, prefix);
214 }
215
216 #define ET_HASTEXT 32768
217 #define ET_WORDWRAP 16384
218 #define ET_MULTILINE 8192
219 #define ET_PASSWORD 4096
220 #define ET_READONLY 2048
221 #define ET_HASTEXTCOLOR 1024
222 #define ET_HASMAXLENGTH 512
223 #define ET_HASFONT 256
224 #define ET_X3 128
225 #define ET_X2 64
226 #define ET_HASLAYOUT 32
227 #define ET_NOSELECT 16
228 #define ET_BORDER 8
229 #define ET_X1 4
230 #define ET_X0 2
231 #define ET_USEOUTLINES 1
232
233 SWF swf;
234 int fontnum = 0;
235 SWFFONT**fonts;
236
237 void textcallback(int*glyphs, int nr, int fontid) 
238 {
239     int font=-1,t;
240     printf("                <%2d glyphs in font %2d> ",nr, fontid);
241     for(t=0;t<fontnum;t++)
242     {
243         if(fonts[t]->id == fontid) {
244             font = t;
245             break;
246         }
247     }
248
249     for(t=0;t<nr;t++)
250     {
251         unsigned char a; 
252         if(font>=0) {
253             if(glyphs[t] >= fonts[font]->numchars  /*glyph is in range*/
254                     || !fonts[font]->glyph2ascii /* font has ascii<->glyph mapping */
255               )
256                 continue;
257             a = fonts[font]->glyph2ascii[glyphs[t]];
258         } else {
259             a = glyphs[t];
260         }
261         if(a>=32)
262             printf("%c", a);
263         else
264             printf("\\x%x", (int)a);
265     }
266     printf("\n");
267 }
268
269 void handleText(TAG*tag) 
270 {
271   printf("\n");
272   swf_FontExtract_DefineTextCallback(-1,0,tag,4, textcallback);
273 }
274             
275 void handleDefineSound(TAG*tag)
276 {
277     U16 id = swf_GetU16(tag);
278     U8 flags = swf_GetU8(tag);
279     int compression = (flags>>4)&3;
280     int rate = (flags>>2)&3;
281     int bits = flags&2?16:8;
282     int stereo = flags&1;
283     printf(" (");
284     if(compression == 0) printf("Raw ");
285     else if(compression == 1) printf("ADPCM ");
286     else if(compression == 2) printf("MP3 ");
287     else printf("? ");
288     if(rate == 0) printf("5.5Khz ");
289     if(rate == 1) printf("11Khz ");
290     if(rate == 2) printf("22Khz ");
291     if(rate == 3) printf("44Khz ");
292     printf("%dBit ", bits);
293     if(stereo) printf("stereo");
294     else printf("mono");
295     printf(")");
296 }
297
298 void handleDefineBits(TAG*tag)
299 {
300     U16 id;
301     U8 mode;
302     U16 width,height;
303     int bpp;
304     id = swf_GetU16(tag);
305     mode = swf_GetU8(tag);
306     width = swf_GetU16(tag);
307     height = swf_GetU16(tag);
308     printf(" image %dx%d",width,height);
309     if(mode == 3) printf(" (8 bpp)");
310     else if(mode == 4) printf(" (16 bpp)");
311     else if(mode == 5) printf(" (32 bpp)");
312     else printf(" (? bpp)");
313 }
314
315 void handleEditText(TAG*tag)
316 {
317     U16 id ;
318     U16 flags;
319     int t;
320     id = swf_GetU16(tag);
321     swf_GetRect(tag,0);
322     //swf_ResetReadBits(tag);
323     if (tag->readBit)  
324     { tag->pos++; 
325       tag->readBit = 0; 
326     }
327     flags = swf_GetBits(tag,16);
328     if(flags & ET_HASFONT) {
329         swf_GetU16(tag); //font
330         swf_GetU16(tag); //fontheight
331     }
332     if(flags & ET_HASTEXTCOLOR) {
333         swf_GetU8(tag); //rgba
334         swf_GetU8(tag);
335         swf_GetU8(tag);
336         swf_GetU8(tag);
337     }
338     if(flags & ET_HASMAXLENGTH) {
339         swf_GetU16(tag); //maxlength
340     }
341     if(flags & ET_HASLAYOUT) {
342         swf_GetU8(tag); //align
343         swf_GetU16(tag); //left margin
344         swf_GetU16(tag); //right margin
345         swf_GetU16(tag); //indent
346         swf_GetU16(tag); //leading
347     }
348     printf(" variable \"%s\"", &tag->data[tag->pos]);
349
350     if(flags & (ET_X1 | ET_X2 | ET_X3 | ET_X0))
351     {
352         printf(" undefined flags: %d%d%d%d", 
353                 (flags&ET_X0?1:0),
354                 (flags&ET_X1?1:0),
355                 (flags&ET_X2?1:0),
356                 (flags&ET_X3?1:0));
357     }
358     
359     while(tag->data[tag->pos++]);
360     if(flags & ET_HASTEXT)
361    //  printf(" text \"%s\"\n", &tag->data[tag->pos])
362         ;
363 }
364 void printhandlerflags(U16 handlerflags) 
365 {
366     if(handlerflags&1) printf("[on load]");
367     if(handlerflags&2) printf("[enter frame]");
368     if(handlerflags&4) printf("[unload]");
369     if(handlerflags&8) printf("[mouse move]");
370     if(handlerflags&16) printf("[mouse down]");
371     if(handlerflags&32) printf("[mouse up]");
372     if(handlerflags&64) printf("[key down]");
373     if(handlerflags&128) printf("[key up]");
374     if(handlerflags&256) printf("[data]");
375     if(handlerflags&0xfe00) printf("[???]");
376 }
377 void handleVideoStream(TAG*tag, char*prefix)
378 {
379     U16 id = swf_GetU16(tag);
380     U16 frames = swf_GetU16(tag);
381     U16 width = swf_GetU16(tag);
382     U16 height = swf_GetU16(tag);
383     U8 flags = swf_GetU8(tag); //5-2(videopacket 01=off 10=on)-1(smoothing 1=on)
384     U8 codec = swf_GetU8(tag);
385     printf(" (%d frames, %dx%d", frames, width, height);
386     if(flags&1)
387         printf(" smoothed");
388     if(codec == 2)
389         printf(" sorenson h.263)");
390     else
391         printf(" codec 0x%02x)", codec);
392 }
393 void handleVideoFrame(TAG*tag, char*prefix)
394 {
395     U32 code, version, reference, sizeflags;
396     U32 width, height;
397     U8 type;
398     U16 id = swf_GetU16(tag);
399     U16 frame = swf_GetU16(tag);
400     U8 deblock,flags, tmp, bit;
401     U32 quantizer;
402     char*types[] = {"I-frame", "P-frame", "disposable P-frame", "<reserved>"};
403     printf(" (frame %d) ", frame);
404
405     /* video packet follows */
406     code = swf_GetBits(tag, 17);
407     version = swf_GetBits(tag, 5);
408     reference = swf_GetBits(tag, 8);
409
410     sizeflags = swf_GetBits(tag, 3);
411     switch(sizeflags)
412     {
413         case 0: width = swf_GetBits(tag, 8); height = swf_GetBits(tag, 8); break;
414         case 1: width = swf_GetBits(tag, 16); height = swf_GetBits(tag, 16); break;
415         case 2: width = 352; height = 288; break;
416         case 3: width = 176; height = 144; break;
417         case 4: width = 128; height = 96; break;
418         case 5: width = 320; height = 240; break;
419         case 6: width = 160; height = 120; break;
420         case 7: width = -1; height = -1;/*reserved*/ break;
421     }
422     printf("%dx%d ", width, height);
423     type = swf_GetBits(tag, 2);
424     printf("%s", types[type]);
425
426     deblock = swf_GetBits(tag, 1);
427     if(deblock)
428         printf(" deblock ", deblock);
429     quantizer = swf_GetBits(tag, 5);
430     printf(" quant: %d ", quantizer);
431 }
432
433 void handlePlaceObject2(TAG*tag, char*prefix)
434 {
435     U8 flags = swf_GetU8(tag);
436     MATRIX m;
437     CXFORM cx;
438     char pstr[3][160];
439     int ppos[3] = {0,0,0};
440     swf_GetU16(tag); //depth
441     //flags&1: move
442     if(flags&2) swf_GetU16(tag); //id
443     if(flags&4) {
444         swf_GetMatrix(tag,&m);
445         if(placements) {
446             ppos[0] += sprintf(pstr[0], "| Matrix             ");
447             ppos[1] += sprintf(pstr[1], "| %5.3f %5.3f %6.2f ", m.sx/65536.0, m.r1/65536.0, m.tx/20.0);
448             ppos[2] += sprintf(pstr[2], "| %5.3f %5.3f %6.2f ", m.r0/65536.0, m.sy/65536.0, m.ty/20.0);
449         }
450     }
451     if(flags&8) {
452         swf_GetCXForm(tag, &cx, 1);
453         if(placements) {
454             ppos[0] += sprintf(pstr[0]+ppos[0], "| CXForm    r    g    b    a ");
455             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);
456             ppos[2] += sprintf(pstr[2]+ppos[2], "| add    %4d %4d %4d %4d ", cx.r1, cx.g1, cx.b1, cx.a1);
457         }
458     }
459     if(flags&16) {
460         U16 ratio = swf_GetU16(tag); //ratio
461         if(placements) {
462             ppos[0] += sprintf(pstr[0]+ppos[0], "| Ratio ");
463             ppos[1] += sprintf(pstr[1]+ppos[1], "| %-5d ", ratio);
464             ppos[2] += sprintf(pstr[2]+ppos[2], "|       ");
465         }
466     }
467     if(flags&64) {
468         U16 clip = swf_GetU16(tag); //clip
469         if(placements) {
470             ppos[0] += sprintf(pstr[0]+ppos[0], "| Clip  ");
471             ppos[1] += sprintf(pstr[1]+ppos[1], "| %-5d ", clip);
472             ppos[2] += sprintf(pstr[2]+ppos[2], "|       ");
473         }
474     }
475     if(flags&32) { while(swf_GetU8(tag)); }
476     if(placements && ppos[0]) {
477         printf("\n");
478         printf("%s %s\n", prefix, pstr[0]);
479         printf("%s %s\n", prefix, pstr[1]);
480         printf("%s %s", prefix, pstr[2]);
481     }
482     if(flags&128) {
483       if (action) {
484         U16 unknown;
485         U32 globalflags;
486         U32 handlerflags;
487         char is32 = 0;
488         printf("\n");
489         unknown = swf_GetU16(tag);
490         globalflags = swf_GetU16(tag);
491         if(unknown) {
492             printf("Unknown parameter field not zero: %04x\n", unknown);
493             return;
494         }
495         printf("global flags: %04x\n", globalflags);
496         handlerflags = swf_GetU16(tag);
497         if(!handlerflags) {
498             handlerflags = swf_GetU32(tag);
499             is32 = 1;
500         }
501         while(handlerflags)  {
502             int length;
503             int t;
504             ActionTAG*a;
505
506             globalflags &= ~handlerflags;
507             printf("%s flags %08x ",prefix, handlerflags);
508             printhandlerflags(handlerflags);
509             length = swf_GetU32(tag);
510             printf(", %d bytes actioncode\n",length);
511             a = swf_ActionGet(tag);
512             swf_DumpActions(a,prefix);
513             swf_ActionFree(a);
514
515             handlerflags = is32?swf_GetU32(tag):swf_GetU16(tag);
516         }
517         if(globalflags) // should go to sterr.
518             printf("ERROR: unsatisfied handlerflags: %02x\n", globalflags);
519     } else {
520       printf(" has action code\n");
521     }
522     } else printf("\n");
523 }
524
525 void handlePlaceObject(TAG*tag, char*prefix)
526 {
527     /*TODO*/
528 }
529     
530 void fontcallback1(U16 id,U8 * name)
531 { fontnum++;
532 }
533
534 void fontcallback2(U16 id,U8 * name)
535
536   swf_FontExtract(&swf,id,&fonts[fontnum]);
537   fontnum++;
538 }
539
540 void hexdumpTag(TAG*tag, char* prefix)
541 {
542     int t;
543     printf("                %s-=> ",prefix);
544     for(t=0;t<tag->len;t++) {
545         printf("%02x ", tag->data[t]);
546         if((t && ((t&15)==15)) || (t==tag->len-1))
547         {
548             if(t==tag->len-1)
549                 printf("\n");
550             else
551                 printf("\n                %s-=> ",prefix);
552         }
553     }
554 }
555
556 void handleExportAssets(TAG*tag, char* prefix)
557 {
558     int num;
559     U16 id;
560     char* name;
561     int t;
562     num = swf_GetU16(tag);
563     for(t=0;t<num;t++)
564     {
565         id = swf_GetU16(tag);
566         name = swf_GetString(tag);
567         printf("%sexports %04d as \"%s\"\n", prefix, id, name);
568     }
569 }
570
571 void dumperror(const char* format, ...)
572 {
573     char buf[1024];
574     va_list arglist;
575
576     va_start(arglist, format);
577     vsprintf(buf, format, arglist);
578     va_end(arglist);
579
580     if(!html && !xy)
581         printf("==== Error: %s ====\n", buf);
582 }
583
584 static char strbuf[800];
585 static int bufpos=0;
586
587 char* timestring(double f)
588 {
589     int hours   = (int)(f/3600);
590     int minutes = (int)((f-hours*3600)/60);
591     int seconds = (int)((f-hours*3600-minutes*60));
592     int useconds = (int)((f-(int)f)*1000+0.5);
593     bufpos+=100;
594     bufpos%=800;
595     sprintf(&strbuf[bufpos], "%02d:%02d:%02d,%03d",hours,minutes,seconds,useconds);
596     return &strbuf[bufpos];
597 }
598
599 int main (int argc,char ** argv)
600
601     TAG*tag;
602 #ifdef HAVE_STAT
603     struct stat statbuf;
604 #endif
605     int f;
606     int xsize,ysize;
607     char issprite = 0; // are we inside a sprite definition?
608     int spriteframe = 0;
609     int mainframe=0;
610     char* spriteframelabel = 0;
611     char* framelabel = 0;
612     char prefix[128];
613     int filesize = 0;
614     prefix[0] = 0;
615     memset(idtab,0,65536);
616
617     processargs(argc, argv);
618
619     if(!filename)
620     {
621         fprintf(stderr, "You must supply a filename.\n");
622         return 1;
623     }
624
625     f = open(filename,O_RDONLY|O_BINARY);
626
627     if (f<0)
628     { 
629         perror("Couldn't open file: ");
630         exit(1);
631     }
632     if FAILED(swf_ReadSWF(f,&swf))
633     { 
634         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
635         close(f);
636         exit(1);
637     }
638
639 #ifdef HAVE_STAT
640     fstat(f, &statbuf);
641     if(statbuf.st_size != swf.fileSize && !swf.compressed)
642         dumperror("Real Filesize (%d) doesn't match header Filesize (%d)",
643                 statbuf.st_size, swf.fileSize);
644     filesize = statbuf.st_size;
645 #endif
646
647     close(f);
648
649     xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
650     ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
651     if(xy)
652     {
653         if(xy&1)
654         printf("-X %d", xsize);
655
656         if((xy&1) && (xy&6))
657         printf(" ");
658
659         if(xy&2)
660         printf("-Y %d", ysize);
661         
662         if((xy&3) && (xy&4))
663         printf(" ");
664
665         if(xy&4)
666         printf("-r %d", swf.frameRate*100/256);
667         
668         if((xy&7) && (xy&8))
669         printf(" ");
670         
671         if(xy&8)
672         printf("-f %d", swf.frameCount);
673         
674         printf("\n");
675         return 0;
676     }
677     if(html)
678     {
679         char*fileversions[] = {"","1,0,0,0", "2,0,0,0","3,0,0,0","4,0,0,0",
680                                "5,0,0,0","6,0,23,0","7,0,0,0","8,0,0,0"};
681         if(swf.fileVersion>8) {
682             fprintf(stderr, "Fileversion>8\n");
683             exit(1);
684         }
685         printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
686                " WIDTH=\"%d\"\n"
687                //" BGCOLOR=#ffffffff\n"?
688                " HEIGHT=\"%d\"\n"
689                //http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,23,0?
690                " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%s\">\n"
691                "  <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
692                "  <PARAM NAME=\"PLAY\" VALUE=\"true\">\n" 
693                "  <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
694                "  <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
695                "  <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n" //bgcolor=#ffffff?
696                "   PLAY=\"true\" ALIGN=\"\" LOOP=\"true\" QUALITY=\"high\"\n"
697                "   TYPE=\"application/x-shockwave-flash\"\n"
698                "   PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\">\n"
699                "  </EMBED>\n" 
700                "</OBJECT>\n", xsize, ysize, fileversions[swf.fileVersion], 
701                               filename, filename, xsize, ysize);
702         return 0;
703     } 
704     printf("[HEADER]        File version: %d\n", swf.fileVersion);
705     if(swf.compressed) {
706         printf("[HEADER]        File is zlib compressed.");
707         if(filesize && swf.fileSize)
708             printf(" Ratio: %02d%%\n", filesize*100/(swf.fileSize));
709         else
710             printf("\n");
711     }
712     printf("[HEADER]        File size: %ld%s\n", swf.fileSize, swf.compressed?" (Depacked)":"");
713     printf("[HEADER]        Frame rate: %f\n",swf.frameRate/256.0);
714     printf("[HEADER]        Frame count: %d\n",swf.frameCount);
715     printf("[HEADER]        Movie width: %.2f",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
716     if(swf.movieSize.xmin)
717         printf(" (left offset: %.2f)\n", swf.movieSize.xmin/20.0);
718     else 
719         printf("\n");
720     printf("[HEADER]        Movie height: %.2f",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
721     if(swf.movieSize.ymin)
722         printf(" (top offset: %.2f)\n", swf.movieSize.ymin/20.0);
723     else 
724         printf("\n");
725
726     tag = swf.firstTag;
727    
728     if(showtext) {
729         fontnum = 0;
730         swf_FontEnumerate(&swf,&fontcallback1);
731         fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
732         fontnum = 0;
733         swf_FontEnumerate(&swf,&fontcallback2);
734     }
735
736     while(tag) {
737         char*name = swf_TagGetName(tag);
738         char myprefix[128];
739         if(!name) {
740             dumperror("Unknown tag:0x%03x", tag->id);
741             tag = tag->next;
742             continue;
743         }
744         printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
745         
746         if(tag->id == ST_FREECHARACTER) {
747             U16 id = swf_GetU16(tag);
748             idtab[id] = 0;
749         }
750
751         if(swf_isDefiningTag(tag)) {
752             U16 id = swf_GetDefineID(tag);
753             printf(" defines id %04d", id);
754             if(idtab[id])
755                 dumperror("Id %04d is defined more than once.", id);
756             idtab[id] = 1;
757         }
758         else if(swf_isPseudoDefiningTag(tag)) {
759             U16 id = swf_GetDefineID(tag);
760             printf(" adds information to id %04d", id);
761             if(!idtab[id])
762                 dumperror("Id %04d is not yet defined.\n", id);
763         }
764         else if(tag->id == ST_PLACEOBJECT) {
765             printf(" places id %04d at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
766             if(swf_GetName(tag))
767                 printf(" name \"%s\"",swf_GetName(tag));
768             handlePlaceObject(tag, myprefix);
769         }
770         else if(tag->id == ST_PLACEOBJECT2) {
771             if(tag->data[0]&1)
772                 printf(" moves");
773             else
774                 printf(" places");
775             
776             if(tag->data[0]&2)
777                 printf(" id %04d",swf_GetPlaceID(tag));
778             else
779                 printf(" object");
780
781             printf(" at depth %04d", swf_GetDepth(tag));
782             if(swf_GetName(tag))
783                 printf(" name \"%s\"",swf_GetName(tag));
784         }
785         else if(tag->id == ST_REMOVEOBJECT) {
786             printf(" removes id %04d from depth %04d", swf_GetPlaceID(tag), swf_GetDepth(tag));
787         }
788         else if(tag->id == ST_REMOVEOBJECT2) {
789             printf(" removes object from depth %04d", swf_GetDepth(tag));
790         }
791         else if(tag->id == ST_FREECHARACTER) {
792             printf(" frees object %04d", swf_GetPlaceID(tag));
793         }
794         else if(tag->id == ST_STARTSOUND) {
795             U8 flags;
796             U16 id;
797             id = swf_GetU16(tag);
798             flags = swf_GetU8(tag);
799             if(flags & 32)
800                 printf(" stops sound with id %04d", id);
801             else
802                 printf(" starts sound with id %04d", id);
803             if(flags & 16)
804                 printf(" (if not already playing)");
805             if(flags & 1)
806                 swf_GetU32(tag);
807             if(flags & 2)
808                 swf_GetU32(tag);
809             if(flags & 4) {
810                 printf(" looping %d times", swf_GetU16(tag));
811             }
812         }
813         else if(tag->id == ST_FRAMELABEL) {
814             int l = strlen(tag->data);
815             printf(" \"%s\"", tag->data);
816             if(l < tag->len-1) {
817                 printf(" has %d extra bytes", tag->len-1-l);
818                 if(tag ->len-1-l == 1 && tag->data[tag->len-1] == 1)
819                     printf(" (ANCHOR)");
820             }
821             if((framelabel && !issprite) ||
822                (spriteframelabel && issprite)) {
823                 dumperror("Frame %d has more than one label", 
824                         issprite?spriteframe:mainframe);
825             }
826             if(issprite) spriteframelabel = tag->data;
827             else framelabel = tag->data;
828         }
829         else if(tag->id == ST_SHOWFRAME) {
830             char*label = issprite?spriteframelabel:framelabel;
831             int frame = issprite?spriteframe:mainframe;
832             int nframe = frame;
833             if(!label) {
834                 while(tag->next && tag->next->id == ST_SHOWFRAME && tag->next->len == 0) {
835                     tag = tag->next;
836                     if(issprite) spriteframe++;
837                     else mainframe++;
838                     nframe++;
839                 }
840             }
841             if(nframe == frame)
842                 printf(" %d (%s)", frame, timestring(frame*(256.0/(swf.frameRate+0.1))));
843             else
844                 printf(" %d-%d (%s-%s)", frame, nframe,
845                         timestring(frame*(256.0/(swf.frameRate+0.1))),
846                         timestring(nframe*(256.0/(swf.frameRate+0.1)))
847                         );
848             if(label)
849                 printf(" (label \"%s\")", label);
850             if(issprite) {spriteframe++; spriteframelabel = 0;}
851             if(!issprite) {mainframe++; framelabel = 0;}
852         }
853
854         if(tag->id == ST_SETBACKGROUNDCOLOR) {
855             U8 r = swf_GetU8(tag);
856             U8 g = swf_GetU8(tag);
857             U8 b = swf_GetU8(tag);
858             printf(" (%02x/%02x/%02x)\n",r,g,b);
859         }
860         else if(tag->id == ST_DEFINEBITSLOSSLESS ||
861            tag->id == ST_DEFINEBITSLOSSLESS2) {
862             handleDefineBits(tag);
863             printf("\n");
864         }
865         else if(tag->id == ST_DEFINESOUND) {
866             handleDefineSound(tag);
867             printf("\n");
868         }
869         else if(tag->id == ST_VIDEOFRAME) {
870             handleVideoFrame(tag, myprefix);
871             printf("\n");
872         }
873         else if(tag->id == ST_DEFINEVIDEOSTREAM) {
874             handleVideoStream(tag, myprefix);
875             printf("\n");
876         }
877         else if(tag->id == ST_DEFINEEDITTEXT) {
878             handleEditText(tag);
879             printf("\n");
880         }
881         else if(tag->id == ST_DEFINEMOVIE) {
882             U16 id = swf_GetU16(tag);
883             char*s = swf_GetString(tag);
884             printf(" URL: %s\n", s);
885         }
886         else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
887             if(showtext)
888                 handleText(tag);
889             else
890                 printf("\n");
891         }
892         else if(tag->id == ST_PLACEOBJECT2) {
893         }
894         else if(tag->id == ST_NAMECHARACTER) {
895             swf_GetU16(tag);
896             printf(" \"%s\"\n", swf_GetString(tag));
897         }
898         else {
899             printf("\n");
900         }
901         
902         sprintf(myprefix, "                %s", prefix);
903
904         if(tag->id == ST_DEFINESPRITE) {
905             sprintf(prefix, "         ");
906             if(issprite) {
907                 dumperror("Sprite definition inside a sprite definition");
908             }
909             issprite = 1;
910             spriteframe = 0;
911             spriteframelabel = 0;
912         }
913         else if(tag->id == ST_END) {
914             *prefix = 0;
915             issprite = 0;
916             spriteframelabel = 0;
917             if(tag->len)
918                 dumperror("End Tag not empty");
919         }
920         else if(tag->id == ST_EXPORTASSETS) {
921             handleExportAssets(tag, myprefix);
922         }
923         else if(tag->id == ST_DOACTION && action) {
924             ActionTAG*actions;
925             actions = swf_ActionGet(tag);
926             swf_DumpActions(actions, myprefix);
927         }
928         else if(tag->id == ST_DEFINEBUTTON && action) {
929             dumpButtonActions(tag, myprefix);
930         }
931         else if(tag->id == ST_DEFINEBUTTON2 && action) {
932             dumpButton2Actions(tag, myprefix);
933         }
934         else if(tag->id == ST_PLACEOBJECT2) {
935             handlePlaceObject2(tag, myprefix);
936         }
937
938         if(tag->len && used) {
939             int num = swf_GetNumUsedIDs(tag);
940             int* used;
941             int t;
942             if(num) {
943                 used = (int*)malloc(sizeof(int)*num);
944                 swf_GetUsedIDs(tag, used);
945                 printf("%s%suses IDs: ", indent, prefix);
946                 for(t=0;t<num;t++) {
947                     swf_SetTagPos(tag, used[t]);
948                     printf("%d%s", swf_GetU16(tag), t<num-1?", ":"");
949                 }
950                 printf("\n");
951             }
952         }
953
954         if(tag->len && hex) {
955             hexdumpTag(tag, prefix);
956         }
957         tag = tag->next;
958         fflush(stdout);
959     }
960
961     swf_FreeTags(&swf);
962     return 0;
963 }
964
965