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