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