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