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