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