support for NAMECHARACTER, EXPORTASSETS and PLACEOBJECT2.
[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 -atp\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\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)
254                 continue;
255             a = fonts[font]->glyph2ascii[glyphs[t]];
256         } else {
257             a = glyphs[t];
258         }
259         if(a>=32)
260             printf("%c", a);
261         else
262             printf("\\x%x", (int)a);
263     }
264     printf("\n");
265 }
266
267 void handleText(TAG*tag) 
268 {
269   printf("\n");
270   swf_FontExtract_DefineTextCallback(-1,0,tag,4, textcallback);
271 }
272             
273 void handleDefineSound(TAG*tag)
274 {
275     U16 id = swf_GetU16(tag);
276     U8 flags = swf_GetU8(tag);
277     int compression = (flags>>4)&3;
278     int rate = (flags>>2)&3;
279     int bits = flags&2?16:8;
280     int stereo = flags&1;
281     printf(" (");
282     if(compression == 0) printf("Raw ");
283     else if(compression == 1) printf("ADPCM ");
284     else if(compression == 2) printf("MP3 ");
285     else printf("? ");
286     if(rate == 0) printf("5.5Khz ");
287     if(rate == 1) printf("11Khz ");
288     if(rate == 2) printf("22Khz ");
289     if(rate == 3) printf("44Khz ");
290     printf("%dBit ", bits);
291     if(stereo) printf("stereo");
292     else printf("mono");
293     printf(")");
294 }
295
296 void handleDefineBits(TAG*tag)
297 {
298     U16 id;
299     U8 mode;
300     U16 width,height;
301     int bpp;
302     id = swf_GetU16(tag);
303     mode = swf_GetU8(tag);
304     width = swf_GetU16(tag);
305     height = swf_GetU16(tag);
306     printf(" image %dx%d",width,height);
307     if(mode == 3) printf(" (8 bpp)");
308     else if(mode == 4) printf(" (16 bpp)");
309     else if(mode == 5) printf(" (32 bpp)");
310     else printf(" (? bpp)");
311 }
312
313 void handleEditText(TAG*tag)
314 {
315     U16 id ;
316     U16 flags;
317     int t;
318     id = swf_GetU16(tag);
319     swf_GetRect(tag,0);
320     //swf_ResetReadBits(tag);
321     if (tag->readBit)  
322     { tag->pos++; 
323       tag->readBit = 0; 
324     }
325     flags = swf_GetBits(tag,16);
326     if(flags & ET_HASFONT) {
327         swf_GetU16(tag); //font
328         swf_GetU16(tag); //fontheight
329     }
330     if(flags & ET_HASTEXTCOLOR) {
331         swf_GetU8(tag); //rgba
332         swf_GetU8(tag);
333         swf_GetU8(tag);
334         swf_GetU8(tag);
335     }
336     if(flags & ET_HASMAXLENGTH) {
337         swf_GetU16(tag); //maxlength
338     }
339     if(flags & ET_HASLAYOUT) {
340         swf_GetU8(tag); //align
341         swf_GetU16(tag); //left margin
342         swf_GetU16(tag); //right margin
343         swf_GetU16(tag); //indent
344         swf_GetU16(tag); //leading
345     }
346     printf(" variable \"%s\"", &tag->data[tag->pos]);
347
348     if(flags & (ET_X1 | ET_X2 | ET_X3 | ET_X0))
349     {
350         printf(" undefined flags: %d%d%d%d", 
351                 (flags&ET_X0?1:0),
352                 (flags&ET_X1?1:0),
353                 (flags&ET_X2?1:0),
354                 (flags&ET_X3?1:0));
355     }
356     
357     while(tag->data[tag->pos++]);
358     if(flags & ET_HASTEXT)
359    //  printf(" text \"%s\"\n", &tag->data[tag->pos])
360         ;
361 }
362 void printhandlerflags(U16 handlerflags) 
363 {
364     if(handlerflags&1) printf("[on load]");
365     if(handlerflags&2) printf("[enter frame]");
366     if(handlerflags&4) printf("[unload]");
367     if(handlerflags&8) printf("[mouse move]");
368     if(handlerflags&16) printf("[mouse down]");
369     if(handlerflags&32) printf("[mouse up]");
370     if(handlerflags&64) printf("[key down]");
371     if(handlerflags&128) printf("[key up]");
372     if(handlerflags&256) printf("[data]");
373     if(handlerflags&0xfe00) printf("[???]");
374 }
375 void handlePlaceObject2(TAG*tag, char*prefix)
376 {
377     U8 flags = swf_GetU8(tag);
378     MATRIX m;
379     CXFORM cx;
380     char pstr[3][160];
381     int ppos[3] = {0,0,0};
382     swf_GetU16(tag); //depth
383     //flags&1: move
384     if(flags&2) swf_GetU16(tag); //id
385     if(flags&4) {
386         swf_GetMatrix(tag,&m);
387         if(placements) {
388             ppos[0] += sprintf(pstr[0], "| Matrix             ");
389             ppos[1] += sprintf(pstr[1], "| %5.3f %5.3f %6.2f ", m.sx/65536.0, m.r1/65536.0, m.tx/20.0);
390             ppos[2] += sprintf(pstr[2], "| %5.3f %5.3f %6.2f ", m.r0/65536.0, m.sy/65536.0, m.ty/20.0);
391         }
392     }
393     if(flags&8) {
394         swf_GetCXForm(tag, &cx, 1);
395         if(placements) {
396             ppos[0] += sprintf(pstr[0]+ppos[0], "| CXForm    r    g    b    a ");
397             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);
398             ppos[2] += sprintf(pstr[2]+ppos[2], "| add    %4d %4d %4d %4d ", cx.r1, cx.g1, cx.b1, cx.a1);
399         }
400     }
401     if(flags&16) {
402         U16 ratio = swf_GetU16(tag); //ratio
403         if(placements) {
404             ppos[0] += sprintf(pstr[0]+ppos[0], "| Ratio ");
405             ppos[1] += sprintf(pstr[1]+ppos[1], "| %-5d ", ratio);
406             ppos[2] += sprintf(pstr[2]+ppos[2], "|       ");
407         }
408     }
409     if(flags&64) {
410         U16 clip = swf_GetU16(tag); //clip
411         if(placements) {
412             ppos[0] += sprintf(pstr[0]+ppos[0], "| Clip  ");
413             ppos[1] += sprintf(pstr[1]+ppos[1], "| %-5d ", clip);
414             ppos[2] += sprintf(pstr[2]+ppos[2], "|       ");
415         }
416     }
417     if(flags&32) { while(swf_GetU8(tag)); }
418     if(placements && ppos[0]) {
419         printf("\n");
420         printf("%s %s\n", prefix, pstr[0]);
421         printf("%s %s\n", prefix, pstr[1]);
422         printf("%s %s", prefix, pstr[2]);
423     }
424     if(flags&128) {
425       if (action) {
426         U16 unknown;
427         U32 globalflags;
428         U32 handlerflags;
429         char is32 = 0;
430         printf("\n");
431         unknown = swf_GetU16(tag);
432         globalflags = swf_GetU16(tag);
433         if(unknown) {
434             printf("Unknown parameter field not zero: %04x\n", unknown);
435             return;
436         }
437         printf("global flags: %04x\n", globalflags);
438         handlerflags = swf_GetU16(tag);
439         if(!handlerflags) {
440             handlerflags = swf_GetU32(tag);
441             is32 = 1;
442         }
443         while(handlerflags)  {
444             int length;
445             int t;
446             ActionTAG*a;
447
448             globalflags &= ~handlerflags;
449             printf("%s flags %08x ",prefix, handlerflags);
450             printhandlerflags(handlerflags);
451             length = swf_GetU32(tag);
452             printf(", %d bytes actioncode\n",length);
453             a = swf_ActionGet(tag);
454             swf_DumpActions(a,prefix);
455             swf_ActionFree(a);
456
457             handlerflags = is32?swf_GetU32(tag):swf_GetU16(tag);
458         }
459         if(globalflags) // should go to sterr.
460             printf("ERROR: unsatisfied handlerflags: %02x\n", globalflags);
461     } else {
462       printf(" has action code\n");
463     }
464     } else printf("\n");
465 }
466
467 void handlePlaceObject(TAG*tag, char*prefix)
468 {
469     /*TODO*/
470 }
471     
472 void fontcallback1(U16 id,U8 * name)
473 { fontnum++;
474 }
475
476 void fontcallback2(U16 id,U8 * name)
477
478   swf_FontExtract(&swf,id,&fonts[fontnum]);
479   fontnum++;
480 }
481
482 void hexdumpTag(TAG*tag, char* prefix)
483 {
484     int t;
485     printf("                %s-=> ",prefix);
486     for(t=0;t<tag->len;t++) {
487         printf("%02x ", tag->data[t]);
488         if((t && ((t&15)==15)) || (t==tag->len-1))
489         {
490             if(t==tag->len-1)
491                 printf("\n");
492             else
493                 printf("\n                %s-=> ",prefix);
494         }
495     }
496 }
497
498 void handleExportAssets(TAG*tag, char* prefix)
499 {
500     int num;
501     U16 id;
502     char* name;
503     int t;
504     num = swf_GetU16(tag);
505     for(t=0;t<num;t++)
506     {
507         id = swf_GetU16(tag);
508         name = swf_GetString(tag);
509         printf("%sexports %04d as \"%s\"\n", prefix, id, name);
510     }
511 }
512
513 void dumperror(const char* format, ...)
514 {
515     char buf[1024];
516     va_list arglist;
517
518     va_start(arglist, format);
519     vsprintf(buf, format, arglist);
520     va_end(arglist);
521
522     if(!html && !xy)
523         printf("==== Error: %s ====\n", buf);
524 }
525
526 static char strbuf[800];
527 static int bufpos=0;
528
529 char* timestring(double f)
530 {
531     int hours   = (int)(f/3600);
532     int minutes = (int)((f-hours*3600)/60);
533     int seconds = (int)((f-hours*3600-minutes*60));
534     int useconds = (int)((f-(int)f)*1000+0.5);
535     bufpos+=100;
536     bufpos%=800;
537     sprintf(&strbuf[bufpos], "%02d:%02d:%02d,%03d",hours,minutes,seconds,useconds);
538     return &strbuf[bufpos];
539 }
540
541 int main (int argc,char ** argv)
542
543     TAG*tag;
544 #ifdef HAVE_STAT
545     struct stat statbuf;
546 #endif
547     int f;
548     int xsize,ysize;
549     char issprite = 0; // are we inside a sprite definition?
550     int spriteframe = 0;
551     int mainframe=0;
552     char* spriteframelabel = 0;
553     char* framelabel = 0;
554     char prefix[128];
555     int filesize = 0;
556     prefix[0] = 0;
557     memset(idtab,0,65536);
558
559     processargs(argc, argv);
560
561     if(!filename)
562     {
563         fprintf(stderr, "You must supply a filename.\n");
564         return 1;
565     }
566
567     f = open(filename,O_RDONLY);
568
569     if (f<0)
570     { 
571         perror("Couldn't open file: ");
572         exit(1);
573     }
574     if FAILED(swf_ReadSWF(f,&swf))
575     { 
576         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
577         close(f);
578         exit(1);
579     }
580
581 #ifdef HAVE_STAT
582     fstat(f, &statbuf);
583     if(statbuf.st_size != swf.fileSize && !swf.compressed)
584         dumperror("Real Filesize (%d) doesn't match header Filesize (%d)",
585                 statbuf.st_size, swf.fileSize);
586     filesize = statbuf.st_size;
587 #endif
588
589     close(f);
590
591     xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
592     ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
593     if(xy)
594     {
595         if(xy&1)
596         printf("-X %d", xsize);
597
598         if((xy&1) && (xy&6))
599         printf(" ");
600
601         if(xy&2)
602         printf("-Y %d", ysize);
603         
604         if((xy&3) && (xy&4))
605         printf(" ");
606
607         if(xy&4)
608         printf("-r %d", swf.frameRate*100/256);
609         
610         if((xy&7) && (xy&8))
611         printf(" ");
612         
613         if(xy&8)
614         printf("-f %d", swf.frameCount);
615         
616         printf("\n");
617         return 0;
618     }
619     if(html)
620     {
621         char*fileversions[] = {"","1,0,0,0", "2,0,0,0","3,0,0,0","4,0,0,0",
622                                "5,0,0,0","6,0,23,0","7,0,0,0","8,0,0,0"};
623         if(swf.fileVersion>8) {
624             fprintf(stderr, "Fileversion>8\n");
625             exit(1);
626         }
627         printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
628                " WIDTH=\"%d\"\n"
629                //" BGCOLOR=#ffffffff\n"?
630                " HEIGHT=\"%d\"\n"
631                //http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,23,0?
632                " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%s\">\n"
633                "  <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
634                "  <PARAM NAME=\"PLAY\" VALUE=\"true\">\n" 
635                "  <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
636                "  <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
637                "  <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n" //bgcolor=#ffffff?
638                "   PLAY=\"true\" ALIGN=\"\" LOOP=\"true\" QUALITY=\"high\"\n"
639                "   TYPE=\"application/x-shockwave-flash\"\n"
640                "   PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\">\n"
641                "  </EMBED>\n" 
642                "</OBJECT>\n", xsize, ysize, fileversions[swf.fileVersion], 
643                               filename, filename, xsize, ysize);
644         return 0;
645     } 
646     printf("[HEADER]        File version: %d\n", swf.fileVersion);
647     if(swf.compressed) {
648         printf("[HEADER]        File is zlib compressed.");
649         if(filesize && swf.fileSize)
650             printf(" Ratio: %02d%%\n", filesize*100/(swf.fileSize));
651         else
652             printf("\n");
653     }
654     printf("[HEADER]        File size: %ld%s\n", swf.fileSize, swf.compressed?" (Depacked)":"");
655     printf("[HEADER]        Frame rate: %f\n",swf.frameRate/256.0);
656     printf("[HEADER]        Frame count: %d\n",swf.frameCount);
657     printf("[HEADER]        Movie width: %.2f",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
658     if(swf.movieSize.xmin)
659         printf(" (left offset: %.2f)\n", swf.movieSize.xmin/20.0);
660     else 
661         printf("\n");
662     printf("[HEADER]        Movie height: %.2f",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
663     if(swf.movieSize.ymin)
664         printf(" (top offset: %.2f)\n", swf.movieSize.ymin/20.0);
665     else 
666         printf("\n");
667
668     tag = swf.firstTag;
669    
670     if(showtext) {
671         fontnum = 0;
672         swf_FontEnumerate(&swf,&fontcallback1);
673         fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
674         fontnum = 0;
675         swf_FontEnumerate(&swf,&fontcallback2);
676     }
677
678     while(tag) {
679         char*name = swf_TagGetName(tag);
680         char myprefix[128];
681         if(!name) {
682             dumperror("Unknown tag:0x%03x", tag->id);
683             tag = tag->next;
684             continue;
685         }
686         printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
687         
688         if(tag->id == ST_FREECHARACTER) {
689             U16 id = swf_GetU16(tag);
690             idtab[id] = 0;
691         }
692
693         if(swf_isDefiningTag(tag)) {
694             U16 id = swf_GetDefineID(tag);
695             printf(" defines id %04d", id);
696             if(idtab[id])
697                 dumperror("Id %04d is defined more than once.", id);
698             idtab[id] = 1;
699         }
700         else if(swf_isPseudoDefiningTag(tag)) {
701             U16 id = swf_GetDefineID(tag);
702             printf(" adds information to id %04d", id);
703             if(!idtab[id])
704                 dumperror("Id %04d is not yet defined.\n", id);
705         }
706         else if(tag->id == ST_PLACEOBJECT) {
707             printf(" places id %04d at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
708             if(swf_GetName(tag))
709                 printf(" name \"%s\"",swf_GetName(tag));
710             handlePlaceObject(tag, myprefix);
711         }
712         else if(tag->id == ST_PLACEOBJECT2) {
713             if(tag->data[0]&1)
714                 printf(" moves");
715             else
716                 printf(" places");
717             
718             if(tag->data[0]&2)
719                 printf(" id %04d",swf_GetPlaceID(tag));
720             else
721                 printf(" object");
722
723             printf(" at depth %04d", swf_GetDepth(tag));
724             if(swf_GetName(tag))
725                 printf(" name \"%s\"",swf_GetName(tag));
726         }
727         else if(tag->id == ST_REMOVEOBJECT) {
728             printf(" removes id %04d from depth %04d", swf_GetPlaceID(tag), swf_GetDepth(tag));
729         }
730         else if(tag->id == ST_REMOVEOBJECT2) {
731             printf(" removes object from depth %04d", swf_GetDepth(tag));
732         }
733         else if(tag->id == ST_FREECHARACTER) {
734             printf(" frees object %04d", swf_GetPlaceID(tag));
735         }
736         else if(tag->id == ST_STARTSOUND) {
737             U8 flags;
738             U16 id;
739             id = swf_GetU16(tag);
740             flags = swf_GetU8(tag);
741             if(flags & 32)
742                 printf(" stops sound with id %04d", id);
743             else
744                 printf(" starts sound with id %04d", id);
745             if(flags & 16)
746                 printf(" (if not already playing)");
747             if(flags & 1)
748                 swf_GetU32(tag);
749             if(flags & 2)
750                 swf_GetU32(tag);
751             if(flags & 4) {
752                 printf(" looping %d times", swf_GetU16(tag));
753             }
754         }
755         else if(tag->id == ST_FRAMELABEL) {
756             int l = strlen(tag->data);
757             printf(" \"%s\"", tag->data);
758             if(l < tag->len-1) {
759                 printf(" has %d extra bytes", tag->len-1-l);
760                 if(tag ->len-1-l == 1 && tag->data[tag->len-1] == 1)
761                     printf(" (ANCHOR)");
762             }
763             if((framelabel && !issprite) ||
764                (spriteframelabel && issprite)) {
765                 dumperror("Frame %d has more than one label", 
766                         issprite?spriteframe:mainframe);
767             }
768             if(issprite) spriteframelabel = tag->data;
769             else framelabel = tag->data;
770         }
771         else if(tag->id == ST_SHOWFRAME) {
772             char*label = issprite?spriteframelabel:framelabel;
773             int frame = issprite?spriteframe:mainframe;
774             int nframe = frame;
775             if(!label) {
776                 while(tag->next && tag->next->id == ST_SHOWFRAME && tag->next->len == 0) {
777                     tag = tag->next;
778                     if(issprite) spriteframe++;
779                     else mainframe++;
780                     nframe++;
781                 }
782             }
783             if(nframe == frame)
784                 printf(" %d (%s)", frame, timestring(frame*(256.0/(swf.frameRate+0.1))));
785             else
786                 printf(" %d-%d (%s-%s)", frame, nframe,
787                         timestring(frame*(256.0/(swf.frameRate+0.1))),
788                         timestring(nframe*(256.0/(swf.frameRate+0.1)))
789                         );
790             if(label)
791                 printf(" (label \"%s\")", label);
792             if(issprite) {spriteframe++; spriteframelabel = 0;}
793             if(!issprite) {mainframe++; framelabel = 0;}
794         }
795
796         if(tag->id == ST_SETBACKGROUNDCOLOR) {
797             U8 r = swf_GetU8(tag);
798             U8 g = swf_GetU8(tag);
799             U8 b = swf_GetU8(tag);
800             printf(" (%02x/%02x/%02x)\n",r,g,b);
801         }
802         else if(tag->id == ST_DEFINEBITSLOSSLESS ||
803            tag->id == ST_DEFINEBITSLOSSLESS2) {
804             handleDefineBits(tag);
805             printf("\n");
806         }
807         else if(tag->id == ST_DEFINESOUND) {
808             handleDefineSound(tag);
809             printf("\n");
810         }
811         else if(tag->id == ST_DEFINEEDITTEXT) {
812             handleEditText(tag);
813             printf("\n");
814         }
815         else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
816             if(showtext)
817                 handleText(tag);
818             else
819                 printf("\n");
820         }
821         else if(tag->id == ST_PLACEOBJECT2) {
822         }
823         else if(tag->id == ST_NAMECHARACTER) {
824             swf_GetU16(tag);
825             printf(" \"%s\"\n", swf_GetString(tag));
826         }
827         else {
828             printf("\n");
829         }
830         
831         sprintf(myprefix, "                %s", prefix);
832
833         if(tag->id == ST_DEFINESPRITE) {
834             sprintf(prefix, "         ");
835             if(issprite) {
836                 dumperror("Sprite definition inside a sprite definition");
837             }
838             issprite = 1;
839             spriteframe = 0;
840             spriteframelabel = 0;
841         }
842         else if(tag->id == ST_END) {
843             *prefix = 0;
844             issprite = 0;
845             spriteframelabel = 0;
846             if(tag->len)
847                 dumperror("End Tag not empty");
848         }
849         else if(tag->id == ST_EXPORTASSETS) {
850             handleExportAssets(tag, myprefix);
851         }
852         else if(tag->id == ST_DOACTION && action) {
853             ActionTAG*actions;
854             actions = swf_ActionGet(tag);
855             swf_DumpActions(actions, myprefix);
856         }
857         else if(tag->id == ST_DEFINEBUTTON && action) {
858             dumpButtonActions(tag, myprefix);
859         }
860         else if(tag->id == ST_DEFINEBUTTON2 && action) {
861             dumpButton2Actions(tag, myprefix);
862         }
863         else if(tag->id == ST_PLACEOBJECT2) {
864             handlePlaceObject2(tag, myprefix);
865         }
866
867         if(tag->len && used) {
868             int num = swf_GetNumUsedIDs(tag);
869             int* used;
870             int t;
871             if(num) {
872                 used = (int*)malloc(sizeof(int)*num);
873                 swf_GetUsedIDs(tag, used);
874                 printf("%s%suses IDs: ", indent, prefix);
875                 for(t=0;t<num;t++) {
876                     swf_SetTagPos(tag, used[t]);
877                     printf("%d%s", swf_GetU16(tag), t<num-1?", ":"");
878                 }
879                 printf("\n");
880             }
881         }
882
883         if(tag->len && hex) {
884             hexdumpTag(tag, prefix);
885         }
886         tag = tag->next;
887         fflush(stdout);
888     }
889
890     swf_FreeTags(&swf);
891     return 0;
892 }
893
894