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