show DefineBits Infos.
[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 file is distributed under the GPL, see file COPYING for details */
9
10 #include "../config.h"
11
12 #ifdef HAVE_SYS_STAT_H
13 #include <sys/stat.h>
14 #else
15 #undef HAVE_STAT
16 #endif
17
18 #ifdef HAVE_SYS_TYPES_H
19 #include <sys/types.h>
20 #else
21 #undef HAVE_STAT
22 #endif
23
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <stdarg.h>
28 #include "../lib/rfxswf.h"
29 #include "../lib/args.h"
30
31 char * filename = 0;
32
33 /* idtab stores the ids which are defined in the file. This allows us
34    to detect errors in the file. (i.e. ids which are defined more than 
35    once */
36 char idtab[65536];
37 char * indent = "                ";
38
39 int action = 0;
40 int html = 0;
41 int xy = 0;
42 int showtext = 0;
43 int hex = 0;
44 int used = 0;
45
46 struct options_t options[] =
47 {
48  {"a","action"},
49  {"t","text"},
50  {"X","width"},
51  {"Y","height"},
52  {"r","rate"},
53  {"e","html"},
54  {"u","used"},
55  {"v","verbose"},
56  {"V","version"},
57  {"d","hex"},
58  {0,0}
59 };
60
61
62 int args_callback_option(char*name,char*val)
63 {
64     if(!strcmp(name, "V")) {
65         printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
66         exit(0);
67     } 
68     else if(name[0]=='a') {
69         action = 1;
70         return 0;
71     }
72     else if(name[0]=='t') {
73         showtext = 1;
74         return 0;
75     }
76     else if(name[0]=='e') {
77         html = 1;
78         return 0;
79     }
80     else if(name[0]=='X') {
81         xy |= 1;
82         return 0;
83     }
84     else if(name[0]=='Y') {
85         xy |= 2;
86         return 0;
87     }
88     else if(name[0]=='r') {
89         xy |= 4;
90         return 0;
91     }
92     else if(name[0]=='d') {
93         hex = 1;
94         return 0;
95     }
96     else if(name[0]=='u') {
97         used = 1;
98         return 0;
99     }
100     else {
101         printf("Unknown option: -%s\n", name);
102     }
103
104     return 0;
105 }
106 int args_callback_longoption(char*name,char*val)
107 {
108     return args_long2shortoption(options, name, val);
109 }
110 void args_callback_usage(char*name)
111 {    
112     printf("Usage: %s [-at] file.swf\n", name);
113     printf("\t-h , --help\t\t Print help and exit\n");
114     printf("\t-e , --html\t\t Create a html embedding the file (simple, but useful)\n");
115     printf("\t-X , --width\t\t Prints out a string of the form \"-X width\"\n");
116     printf("\t-Y , --height\t\t Prints out a string of the form \"-Y height\"\n");
117     printf("\t-r , --rate\t\t Prints out a string of the form \"-r rate\"\n");
118     printf("\t-a , --action\t\t Disassemble action tags\n");
119     printf("\t-t , --text\t\t Show text data\n");
120     printf("\t-d , --hex\t\t Print hex output of tag data, too\n");
121     printf("\t-u , --used\t\t Show referred IDs for each Tag\n");
122     printf("\t-V , --version\t\t Print program version and exit\n");
123 }
124 int args_callback_command(char*name,char*val)
125 {
126     if(filename) {
127         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
128                  filename, name);
129     }
130     filename = name;
131     return 0;
132 }
133
134 char* what;
135 char* testfunc(char*str)
136 {
137     printf("%s: %s\n", what, str);
138     return 0;
139 }
140
141 void dumpButton2Actions(TAG*tag, char*prefix)
142 {
143     U32 oldTagPos;
144     U32 offsetpos;
145     U32 condition;
146
147     oldTagPos = swf_GetTagPos(tag);
148
149     // scan DefineButton2 Record
150     
151     swf_GetU16(tag);          // Character ID
152     swf_GetU8(tag);           // Flags;
153
154     offsetpos = swf_GetTagPos(tag);  // first offset
155     swf_GetU16(tag);
156
157     while (swf_GetU8(tag))      // state  -> parse ButtonRecord
158     { swf_GetU16(tag);          // id
159       swf_GetU16(tag);          // layer
160       swf_GetMatrix(tag,NULL);  // matrix
161       swf_GetCXForm(tag,NULL,1);  // cxform
162     }
163
164     while(offsetpos)
165     { U8 a;
166       ActionTAG*actions;
167
168       if(tag->pos >= tag->len)
169           break;
170         
171       offsetpos = swf_GetU16(tag);
172       condition = swf_GetU16(tag);                // condition
173       
174       actions = swf_ActionGet(tag);
175       printf("%s condition %04x\n", prefix, condition);
176       swf_DumpActions(actions, prefix);
177     }
178     
179     swf_SetTagPos(tag,oldTagPos);
180     return;
181 }
182
183 void dumpButtonActions(TAG*tag, char*prefix)
184 {
185     ActionTAG*actions;
186     swf_GetU16(tag); // id
187     while (swf_GetU8(tag))      // state  -> parse ButtonRecord
188     { swf_GetU16(tag);          // id
189       swf_GetU16(tag);          // layer
190       swf_GetMatrix(tag,NULL);  // matrix
191     }
192     actions = swf_ActionGet(tag);
193     swf_DumpActions(actions, prefix);
194 }
195
196 #define ET_HASTEXT 32768
197 #define ET_WORDWRAP 16384
198 #define ET_MULTILINE 8192
199 #define ET_PASSWORD 4096
200 #define ET_READONLY 2048
201 #define ET_HASTEXTCOLOR 1024
202 #define ET_HASMAXLENGTH 512
203 #define ET_HASFONT 256
204 #define ET_X3 128
205 #define ET_X2 64
206 #define ET_HASLAYOUT 32
207 #define ET_NOSELECT 16
208 #define ET_BORDER 8
209 #define ET_X1 4
210 #define ET_X0 2
211 #define ET_USEOUTLINES 1
212
213 SWF swf;
214 int fontnum = 0;
215 SWFFONT**fonts;
216
217 void textcallback(int*glyphs, int nr, int fontid) 
218 {
219     int font=-1,t;
220     printf("                <%2d glyphs in font %2d> ",nr, fontid);
221     for(t=0;t<fontnum;t++)
222     {
223         if(fonts[t]->id == fontid) {
224             font = t;
225             break;
226         }
227     }
228
229     for(t=0;t<nr;t++)
230     {
231         unsigned char a; 
232         if(font>=0) {
233             if(glyphs[t] >= fonts[font]->numchars)
234                 continue;
235             a = fonts[font]->glyph2ascii[glyphs[t]];
236         } else {
237             a = glyphs[t];
238         }
239         if(a>=32)
240             printf("%c", a);
241         else
242             printf("\\x%x", (int)a);
243     }
244     printf("\n");
245 }
246
247 void handleText(TAG*tag) 
248 {
249   printf("\n");
250   swf_FontExtract_DefineTextCallback(-1,0,tag,4, textcallback);
251 }
252             
253 void handleDefineBits(TAG*tag)
254 {
255     U16 id;
256     U8 mode;
257     U16 width,height;
258     int bpp;
259     id = swf_GetU16(tag);
260     mode = swf_GetU8(tag);
261     width = swf_GetU16(tag);
262     height = swf_GetU16(tag);
263     printf(" image %dx%d",width,height);
264     if(mode == 3) printf(" (8 bpp)");
265     else if(mode == 4) printf(" (16 bpp)");
266     else if(mode == 5) printf(" (32 bpp)");
267     else printf(" (? bpp)");
268 }
269
270 void handleEditText(TAG*tag)
271 {
272     U16 id ;
273     U16 flags;
274     int t;
275     id = swf_GetU16(tag);
276     swf_GetRect(tag,0);
277     //swf_ResetReadBits(tag);
278     if (tag->readBit)  
279     { tag->pos++; 
280       tag->readBit = 0; 
281     }
282     flags = swf_GetBits(tag,16);
283     if(flags & ET_HASFONT) {
284         swf_GetU16(tag); //font
285         swf_GetU16(tag); //fontheight
286     }
287     if(flags & ET_HASTEXTCOLOR) {
288         swf_GetU8(tag); //rgba
289         swf_GetU8(tag);
290         swf_GetU8(tag);
291         swf_GetU8(tag);
292     }
293     if(flags & ET_HASMAXLENGTH) {
294         swf_GetU16(tag); //maxlength
295     }
296     if(flags & ET_HASLAYOUT) {
297         swf_GetU8(tag); //align
298         swf_GetU16(tag); //left margin
299         swf_GetU16(tag); //right margin
300         swf_GetU16(tag); //indent
301         swf_GetU16(tag); //leading
302     }
303     printf(" variable \"%s\"", &tag->data[tag->pos]);
304
305     if(flags & (ET_X1 | ET_X2 | ET_X3 | ET_X0))
306     {
307         printf(" undefined flags: %d%d%d%d", 
308                 (flags&ET_X0?1:0),
309                 (flags&ET_X1?1:0),
310                 (flags&ET_X2?1:0),
311                 (flags&ET_X3?1:0));
312     }
313     
314     while(tag->data[tag->pos++]);
315     if(flags & ET_HASTEXT)
316    //  printf(" text \"%s\"\n", &tag->data[tag->pos])
317         ;
318 }
319 void printhandlerflags(U16 handlerflags) 
320 {
321     if(handlerflags&1) printf("[on load]");
322     if(handlerflags&2) printf("[enter frame]");
323     if(handlerflags&4) printf("[unload]");
324     if(handlerflags&8) printf("[mouse move]");
325     if(handlerflags&16) printf("[mouse down]");
326     if(handlerflags&32) printf("[mouse up]");
327     if(handlerflags&64) printf("[key down]");
328     if(handlerflags&128) printf("[key up]");
329     if(handlerflags&256) printf("[data]");
330     if(handlerflags&0xfe00) printf("[???]");
331 }
332 void handlePlaceObject2(TAG*tag, char*prefix)
333 {
334     U8 flags = swf_GetU8(tag);
335     swf_GetU16(tag); //depth
336     //flags&1: move
337     if(flags&2) swf_GetU16(tag); //id
338     if(flags&4) swf_GetMatrix(tag,0);
339     if(flags&8) swf_GetCXForm(tag,0,1);
340     if(flags&16) swf_GetU16(tag); //ratio
341     if(flags&32) { while(swf_GetU8(tag)); }
342     if(flags&64) swf_GetU16(tag); //clip
343     if(flags&128) {
344       if (action) {
345         U16 unknown;
346         U32 globalflags;
347         U32 handlerflags;
348         char is32 = 0;
349         printf("\n");
350         unknown = swf_GetU16(tag);
351         globalflags = swf_GetU16(tag);
352         if(unknown) {
353             printf("Unknown parameter field not zero: %04x\n", unknown);
354             return;
355         }
356         printf("global flags: %04x\n", globalflags);
357         handlerflags = swf_GetU16(tag);
358         if(!handlerflags) {
359             handlerflags = swf_GetU32(tag);
360             is32 = 1;
361         }
362         while(handlerflags)  {
363             int length;
364             int t;
365             ActionTAG*a;
366
367             globalflags &= ~handlerflags;
368             printf("%s flags %08x ",prefix, handlerflags);
369             printhandlerflags(handlerflags);
370             length = swf_GetU32(tag);
371             printf(", %d bytes actioncode\n",length);
372             a = swf_ActionGet(tag);
373             swf_DumpActions(a,prefix);
374             swf_ActionFree(a);
375
376             handlerflags = is32?swf_GetU32(tag):swf_GetU16(tag);
377         }
378         if(globalflags) // should go to sterr.
379             printf("ERROR: unsatisfied handlerflags: %02x\n", globalflags);
380     } else {
381       printf(" has action code\n");
382     }
383     } else printf("\n");
384 }
385     
386 void fontcallback1(U16 id,U8 * name)
387 { fontnum++;
388 }
389
390 void fontcallback2(U16 id,U8 * name)
391
392   swf_FontExtract(&swf,id,&fonts[fontnum]);
393   fontnum++;
394 }
395
396 void dumperror(const char* format, ...)
397 {
398     char buf[1024];
399     va_list arglist;
400
401     va_start(arglist, format);
402     vsprintf(buf, format, arglist);
403     va_end(arglist);
404
405     printf("==== Error: %s ====\n", buf);
406 }
407
408 int main (int argc,char ** argv)
409
410     TAG*tag;
411 #ifdef HAVE_STAT
412     struct stat statbuf;
413 #endif
414     int f;
415     int xsize,ysize;
416     char issprite = 0; // are we inside a sprite definition?
417     int spriteframe = 0;
418     int mainframe=0;
419     char* spriteframelabel = 0;
420     char* framelabel = 0;
421     char prefix[128];
422     int filesize = 0;
423     prefix[0] = 0;
424     memset(idtab,0,65536);
425
426     processargs(argc, argv);
427
428     if(!filename)
429     {
430         fprintf(stderr, "You must supply a filename.\n");
431         return 1;
432     }
433
434     f = open(filename,O_RDONLY);
435
436     if (f<0)
437     { 
438         perror("Couldn't open file: ");
439         exit(1);
440     }
441     if FAILED(swf_ReadSWF(f,&swf))
442     { 
443         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
444         close(f);
445         exit(1);
446     }
447
448 #ifdef HAVE_STAT
449     fstat(f, &statbuf);
450     if(statbuf.st_size != swf.fileSize && !swf.compressed)
451         dumperror("Real Filesize (%d) doesn't match header Filesize (%d)",
452                 statbuf.st_size, swf.fileSize);
453     filesize = statbuf.st_size;
454 #endif
455
456     close(f);
457
458     xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
459     ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
460     if(xy)
461     {
462         if(xy&1)
463         printf("-X %d", xsize);
464
465         if((xy&1) && (xy&6))
466         printf(" ");
467
468         if(xy&2)
469         printf("-Y %d", ysize);
470         
471         if((xy&3) && (xy&4))
472         printf(" ");
473
474         if(xy&4)
475         printf("-r %d", swf.frameRate*100/256);
476         
477         printf("\n");
478         return 0;
479     }
480     if(html)
481     {
482         char*fileversions[] = {"","1,0,0,0", "2,0,0,0","3,0,0,0","4,0,0,0",
483                                "5,0,0,0","6,0,23,0","7,0,0,0","8,0,0,0"};
484         if(swf.fileVersion>8) {
485             fprintf(stderr, "Fileversion>8\n");
486             exit(1);
487         }
488         printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
489                " WIDTH=\"%d\"\n"
490                //" BGCOLOR=#ffffffff\n"?
491                " HEIGHT=\"%d\"\n"
492                //http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,23,0?
493                " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%s\">\n"
494                "  <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
495                "  <PARAM NAME=\"PLAY\" VALUE=\"true\">\n" 
496                "  <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
497                "  <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
498                "  <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n" //bgcolor=#ffffff?
499                "   PLAY=\"true\" ALIGN=\"\" LOOP=\"true\" QUALITY=\"high\"\n"
500                "   TYPE=\"application/x-shockwave-flash\"\n"
501                "   PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\">\n"
502                "  </EMBED>\n" 
503                "</OBJECT>\n", xsize, ysize, fileversions[swf.fileVersion], 
504                               filename, filename, xsize, ysize);
505         return 0;
506     } 
507     printf("[HEADER]        File version: %d\n", swf.fileVersion);
508     if(swf.compressed) {
509         printf("[HEADER]        File is zlib compressed.");
510         if(filesize && swf.fileSize)
511             printf(" Ratio: %02d%%\n", filesize*100/(swf.fileSize));
512         else
513             printf("\n");
514     }
515     printf("[HEADER]        File size: %ld%s\n", swf.fileSize, swf.compressed?" (Depacked)":"");
516     printf("[HEADER]        Frame rate: %f\n",swf.frameRate/256.0);
517     printf("[HEADER]        Frame count: %d\n",swf.frameCount);
518     printf("[HEADER]        Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
519     printf("[HEADER]        Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
520
521     tag = swf.firstTag;
522    
523     if(showtext) {
524         fontnum = 0;
525         swf_FontEnumerate(&swf,&fontcallback1);
526         fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
527         fontnum = 0;
528         swf_FontEnumerate(&swf,&fontcallback2);
529     }
530
531     while(tag) {
532         char*name = swf_TagGetName(tag);
533         char myprefix[128];
534         if(!name) {
535             dumperror("Unknown tag:0x%03x", tag->id);
536             tag = tag->next;
537             continue;
538         }
539         printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
540         
541         if(tag->id == ST_FREECHARACTER) {
542             U16 id = swf_GetU16(tag);
543             idtab[id] = 0;
544         }
545
546         if(swf_isDefiningTag(tag)) {
547             U16 id = swf_GetDefineID(tag);
548             printf(" defines id %04d", id);
549             if(idtab[id])
550                 dumperror("Id %04d is defined more than once.", id);
551             idtab[id] = 1;
552         }
553         else if(swf_isPseudoDefiningTag(tag)) {
554             U16 id = swf_GetDefineID(tag);
555             printf(" adds information to id %04d", id);
556             if(!idtab[id])
557                 dumperror("Id %04d is not yet defined.\n", id);
558         }
559         else if(tag->id == ST_PLACEOBJECT) {
560             printf(" places id %04d at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
561             if(swf_GetName(tag))
562                 printf(" name \"%s\"",swf_GetName(tag));
563         }
564         else if(tag->id == ST_PLACEOBJECT2) {
565             if(tag->data[0]&1)
566                 printf(" moves");
567             else
568                 printf(" places");
569             
570             if(tag->data[0]&2)
571                 printf(" id %04d",swf_GetPlaceID(tag));
572             else
573                 printf(" object");
574
575             printf(" at depth %04d", swf_GetDepth(tag));
576             if(swf_GetName(tag))
577                 printf(" name \"%s\"",swf_GetName(tag));
578         }
579         else if(tag->id == ST_REMOVEOBJECT) {
580             printf(" removes id %04d from depth %04d", swf_GetPlaceID(tag), swf_GetDepth(tag));
581         }
582         else if(tag->id == ST_REMOVEOBJECT2) {
583             printf(" removes object from depth %04d", swf_GetDepth(tag));
584         }
585         else if(tag->id == ST_STARTSOUND) {
586             printf(" starts id %04d", swf_GetPlaceID(tag));
587         }
588         else if(tag->id == ST_FRAMELABEL) {
589             int l = strlen(tag->data);
590             printf(" \"%s\"", tag->data);
591             if(l < tag->len-1) {
592                 printf(" has %d extra bytes", tag->len-1-l);
593                 if(tag ->len-1-l == 1 && tag->data[tag->len-1] == 1)
594                     printf(" (ANCHOR)");
595             }
596             if((framelabel && !issprite) ||
597                (spriteframelabel && issprite)) {
598                 dumperror("Frame %d has more than one label", 
599                         issprite?spriteframe:mainframe);
600             }
601             if(issprite) spriteframelabel = tag->data;
602             else framelabel = tag->data;
603         }
604         else if(tag->id == ST_SHOWFRAME) {
605             char*label = issprite?spriteframelabel:framelabel;
606             int frame = issprite?spriteframe:mainframe;
607             int nframe = frame;
608             if(!label) {
609                 while(tag->next && tag->next->id == ST_SHOWFRAME && tag->next->len == 0) {
610                     tag = tag->next;
611                     if(issprite) spriteframe++;
612                     else mainframe++;
613                     nframe++;
614                 }
615             }
616             if(nframe == frame)
617                 printf(" %d", frame);
618             else
619                 printf(" %d-%d", frame, nframe);
620             if(label)
621                 printf(" (label \"%s\")", label);
622             if(issprite) {spriteframe++; spriteframelabel = 0;}
623             if(!issprite) {mainframe++; framelabel = 0;}
624         }
625
626         if(tag->id == ST_DEFINEBITSLOSSLESS ||
627            tag->id == ST_DEFINEBITSLOSSLESS2) {
628             handleDefineBits(tag);
629             printf("\n");
630         }
631         else if(tag->id == ST_DEFINEEDITTEXT) {
632             handleEditText(tag);
633             printf("\n");
634         }
635         else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
636             if(showtext)
637                 handleText(tag);
638             else
639                 printf("\n");
640         }
641         else if(tag->id == ST_PLACEOBJECT2) {
642         }
643         else {
644             printf("\n");
645         }
646         
647         sprintf(myprefix, "                %s", prefix);
648
649         if(tag->id == ST_DEFINESPRITE) {
650             sprintf(prefix, "         ");
651             if(issprite) {
652                 dumperror("Sprite definition inside a sprite definition");
653             }
654             issprite = 1;
655             spriteframe = 0;
656             spriteframelabel = 0;
657         }
658         else if(tag->id == ST_END) {
659             *prefix = 0;
660             issprite = 0;
661             spriteframelabel = 0;
662             if(tag->len)
663                 dumperror("End Tag not empty");
664         }
665         else if(tag->id == ST_DOACTION && action) {
666             ActionTAG*actions;
667             actions = swf_ActionGet(tag);
668             swf_DumpActions(actions, myprefix);
669         }
670         else if(tag->id == ST_DEFINEBUTTON && action) {
671             dumpButtonActions(tag, myprefix);
672         }
673         else if(tag->id == ST_DEFINEBUTTON2 && action) {
674             dumpButton2Actions(tag, myprefix);
675         }
676         else if(tag->id == ST_PLACEOBJECT2) {
677             if((*(U8*)tag->data)&0x80)
678                 handlePlaceObject2(tag, myprefix);
679             else
680                 printf("\n");
681         }
682
683         if(tag->len && used) {
684             int num = swf_GetNumUsedIDs(tag);
685             int* used;
686             int t;
687             if(num) {
688                 used = (int*)malloc(sizeof(int)*num);
689                 swf_GetUsedIDs(tag, used);
690                 printf("%s%suses IDs: ", indent, prefix);
691                 for(t=0;t<num;t++) {
692                     tag->pos = used[t];
693                     printf("%d%s", swf_GetU16(tag), t<num-1?", ":"");
694                 }
695                 printf("\n");
696             }
697         }
698
699         if(tag->len && hex) {
700             int t;
701             printf("                %s-=> ",prefix);
702             for(t=0;t<tag->len;t++) {
703                 printf("%02x ", tag->data[t]);
704                 if((t && ((t&15)==15)) || (t==tag->len-1))
705                 {
706                     if(t==tag->len-1)
707                         printf("\n");
708                     else
709                         printf("\n                %s-=> ",prefix);
710                 }
711             }
712         }
713         tag = tag->next;
714         fflush(stdout);
715     }
716
717     swf_FreeTags(&swf);
718     return 0;
719 }
720
721