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