* disassemble actionscript in placeobject2
[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 #define HAVE_STAT
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 "../lib/rfxswf.h"
28 #include "../lib/args.h"
29
30 char * filename = 0;
31
32 /* idtab stores the ids which are defined in the file. This allows us
33    to detect errors in the file. (i.e. ids which are defined more than 
34    once */
35 char idtab[65536];
36
37 int action = 0;
38 int html = 0;
39 int xy = 0;
40 int showtext = 0;
41
42 struct options_t options[] =
43 {
44  {"a","action"},
45  {"t","text"},
46  {"X","width"},
47  {"Y","height"},
48  {"r","rate"},
49  {"e","html"},
50  {"v","verbose"},
51  {"V","version"},
52  {0,0}
53 };
54
55
56 int args_callback_option(char*name,char*val)
57 {
58     if(!strcmp(name, "V")) {
59         printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
60         exit(0);
61     } 
62     else if(name[0]=='a') {
63         action = 1;
64         return 0;
65     }
66     else if(name[0]=='t') {
67         showtext = 1;
68         return 0;
69     }
70     else if(name[0]=='e') {
71         html = 1;
72         return 0;
73     }
74     else if(name[0]=='X') {
75         xy |= 1;
76         return 0;
77     }
78     else if(name[0]=='Y') {
79         xy |= 2;
80         return 0;
81     }
82     else if(name[0]=='r') {
83         xy |= 4;
84         return 0;
85     }
86     else {
87         printf("Unknown option: -%s\n", name);
88     }
89
90     return 0;
91 }
92 int args_callback_longoption(char*name,char*val)
93 {
94     return args_long2shortoption(options, name, val);
95 }
96 void args_callback_usage(char*name)
97 {    
98     printf("Usage: %s [-at] file.swf\n", name);
99     printf("\t-h , --help\t\t\t Print help and exit\n");
100     printf("\t-e , --html\t\t\t Create a html embedding the file (simple, but useful)\n");
101     printf("\t-X , --width\t\t\t Prints out a string of the form \"-X width\"\n");
102     printf("\t-Y , --height\t\t\t Prints out a string of the form \"-Y height\"\n");
103     printf("\t-r , --rate\t\t\t Prints out a string of the form \"-r rate\"\n");
104     printf("\t-a , --action\t\t\t Disassemble action tags\n");
105     printf("\t-t , --text\t\t\t Show text data\n");
106     printf("\t-V , --version\t\t\t Print program version and exit\n");
107 }
108 int args_callback_command(char*name,char*val)
109 {
110     if(filename) {
111         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
112                  filename, name);
113     }
114     filename = name;
115     return 0;
116 }
117
118 char* what;
119 char* testfunc(char*str)
120 {
121     printf("%s: %s\n", what, str);
122     return 0;
123 }
124
125 void dumpButton2Actions(TAG*tag, char*prefix)
126 {
127     U32 oldTagPos;
128     U32 offsetpos;
129     U32 condition;
130
131     oldTagPos = swf_GetTagPos(tag);
132
133     // scan DefineButton2 Record
134     
135     swf_GetU16(tag);          // Character ID
136     swf_GetU8(tag);           // Flags;
137
138     offsetpos = swf_GetTagPos(tag);  // first offset
139     swf_GetU16(tag);
140
141     while (swf_GetU8(tag))      // state  -> parse ButtonRecord
142     { swf_GetU16(tag);          // id
143       swf_GetU16(tag);          // layer
144       swf_GetMatrix(tag,NULL);  // matrix
145       swf_GetCXForm(tag,NULL,1);  // cxform
146     }
147
148     while(offsetpos)
149     { U8 a;
150       ActionTAG*actions;
151
152       if(tag->pos >= tag->len)
153           break;
154         
155       offsetpos = swf_GetU16(tag);
156       condition = swf_GetU16(tag);                // condition
157       
158       actions = swf_ActionGet(tag);
159       printf("%s condition %04x\n", prefix, condition);
160       swf_DumpActions(actions, prefix);
161     }
162     
163     swf_SetTagPos(tag,oldTagPos);
164     return;
165 }
166
167 void dumpButtonActions(TAG*tag, char*prefix)
168 {
169     ActionTAG*actions;
170     swf_GetU16(tag); // id
171     while (swf_GetU8(tag))      // state  -> parse ButtonRecord
172     { swf_GetU16(tag);          // id
173       swf_GetU16(tag);          // layer
174       swf_GetMatrix(tag,NULL);  // matrix
175     }
176     actions = swf_ActionGet(tag);
177     swf_DumpActions(actions, prefix);
178 }
179
180 #define ET_HASTEXT 32768
181 #define ET_WORDWRAP 16384
182 #define ET_MULTILINE 8192
183 #define ET_PASSWORD 4096
184 #define ET_READONLY 2048
185 #define ET_HASTEXTCOLOR 1024
186 #define ET_HASMAXLENGTH 512
187 #define ET_HASFONT 256
188 #define ET_X3 128
189 #define ET_X2 64
190 #define ET_HASLAYOUT 32
191 #define ET_NOSELECT 16
192 #define ET_BORDER 8
193 #define ET_X1 4
194 #define ET_X0 2
195 #define ET_USEOUTLINES 1
196
197 SWF swf;
198 int fontnum = 0;
199 SWFFONT**fonts;
200
201 void textcallback(int*glyphs, int nr, int fontid) 
202 {
203     int font=-1,t;
204     printf("                <%2d glyphs in font %2d> ",nr, fontid);
205     for(t=0;t<fontnum;t++)
206     {
207         if(fonts[t]->id == fontid) {
208             font = t;
209             break;
210         }
211     }
212     if(font<0) {
213         printf("\n");
214         return; // todo: should we report this? (may only be that it's a definefont without fontinfo)
215     }
216
217     for(t=0;t<nr;t++)
218     {
219         unsigned char a; 
220         if(glyphs[t] >= fonts[font]->numchars)
221             continue;
222         a = fonts[font]->glyph2ascii[glyphs[t]];
223         if(a>=32)
224             printf("%c", a);
225         else
226             printf("\\x%x", (int)a);
227     }
228     printf("\n");
229 }
230
231 void handleText(TAG*tag) 
232 {
233   printf("\n");
234   swf_FontExtract_DefineTextCallback(-1,0,tag,4, textcallback);
235 }
236
237 void handleEditText(TAG*tag)
238 {
239     U16 id ;
240     U16 flags;
241     int t;
242     id = swf_GetU16(tag);
243     swf_GetRect(tag,0);
244     //swf_ResetReadBits(tag);
245     if (tag->readBit)  
246     { tag->pos++; 
247       tag->readBit = 0; 
248     }
249     flags = swf_GetBits(tag,16);
250     if(flags & ET_HASFONT) {
251         swf_GetU16(tag); //font
252         swf_GetU16(tag); //fontheight
253     }
254     if(flags & ET_HASTEXTCOLOR) {
255         swf_GetU8(tag); //rgba
256         swf_GetU8(tag);
257         swf_GetU8(tag);
258         swf_GetU8(tag);
259     }
260     if(flags & ET_HASMAXLENGTH) {
261         swf_GetU16(tag); //maxlength
262     }
263     if(flags & ET_HASLAYOUT) {
264         swf_GetU8(tag); //align
265         swf_GetU16(tag); //left margin
266         swf_GetU16(tag); //right margin
267         swf_GetU16(tag); //indent
268         swf_GetU16(tag); //leading
269     }
270     printf(" variable \"%s\"", &tag->data[tag->pos]);
271
272     if(flags & (ET_X1 | ET_X2 | ET_X3 | ET_X0))
273     {
274         printf(" undefined flags: %d%d%d%d", 
275                 (flags&ET_X0?1:0),
276                 (flags&ET_X1?1:0),
277                 (flags&ET_X2?1:0),
278                 (flags&ET_X3?1:0));
279     }
280     
281     while(tag->data[tag->pos++]);
282     if(flags & ET_HASTEXT)
283    //  printf(" text \"%s\"\n", &tag->data[tag->pos])
284         ;
285 }
286 void printhandlerflags(U16 handlerflags) 
287 {
288     if(handlerflags&1) printf("[on load]");
289     if(handlerflags&2) printf("[enter frame]");
290     if(handlerflags&4) printf("[unload]");
291     if(handlerflags&8) printf("[mouse move]");
292     if(handlerflags&16) printf("[mouse down]");
293     if(handlerflags&32) printf("[mouse up]");
294     if(handlerflags&64) printf("[key down]");
295     if(handlerflags&128) printf("[key up]");
296     if(handlerflags&256) printf("[data]");
297     if(handlerflags&0xfe00) printf("[???]");
298 }
299 void handlePlaceObject2(TAG*tag, char*prefix)
300 {
301     U8 flags = swf_GetU8(tag);
302     if(flags&2) swf_GetU16(tag); //id
303     if(flags&4) swf_GetMatrix(tag,0);
304     if(flags&8) swf_GetCXForm(tag,0,0);
305     if(flags&16) swf_GetU16(tag); //ratio
306     if(flags&32) { 
307         while(swf_GetU8(tag));
308     }
309     if(flags&64) swf_GetU16(tag); //clip
310     if(flags&128) {
311         U8 handlerflags;
312         swf_GetU16(tag);
313         handlerflags = swf_GetU16(tag);
314         printf("%s global flags:%04x ",prefix, handlerflags);
315         printhandlerflags(handlerflags);
316         printf("\n");
317         while(1) {
318             int length;
319             int t;
320             ActionTAG*a;
321             handlerflags = swf_GetU16(tag);
322             if(!handlerflags)
323                 break;
324             printf("%s flags:%04x ",prefix, handlerflags);
325             printhandlerflags(handlerflags);
326
327             printf("\n");
328             length = swf_GetU32(tag);
329             printf("%s %d bytes actioncode\n",prefix);
330             a = swf_ActionGet(tag);
331             swf_DumpActions(a,prefix);
332             swf_ActionFree(a);
333         }
334     }
335 }
336     
337 void fontcallback1(U16 id,U8 * name)
338 { fontnum++;
339 }
340
341 void fontcallback2(U16 id,U8 * name)
342 { swf_FontExtract(&swf,id,&fonts[fontnum]);
343   fontnum++;
344 }
345
346 int main (int argc,char ** argv)
347
348     TAG*tag;
349 #ifdef HAVE_STAT
350     struct stat statbuf;
351 #endif
352     int f;
353     int xsize,ysize;
354     char issprite = 0; // are we inside a sprite definition?
355     int spriteframe;
356     int mainframe=0;
357     char* spriteframelabel;
358     char* framelabel = 0;
359     char prefix[128];
360     prefix[0] = 0;
361     memset(idtab,0,65536);
362
363     processargs(argc, argv);
364
365     if(!filename)
366     {
367         fprintf(stderr, "You must supply a filename.\n");
368         return 1;
369     }
370
371     f = open(filename,O_RDONLY);
372
373     if (f<0)
374     { 
375         perror("Couldn't open file: ");
376         exit(1);
377     }
378     if FAILED(swf_ReadSWF(f,&swf))
379     { 
380         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
381         close(f);
382         exit(1);
383     }
384
385 #ifdef HAVE_STAT
386     fstat(f, &statbuf);
387     if(statbuf.st_size != swf.FileSize)
388         fprintf(stderr, "Error: Real Filesize (%d) doesn't match header Filesize (%d)",
389                 statbuf.st_size, swf.FileSize);
390 #endif
391
392     close(f);
393
394     xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
395     ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
396     if(xy)
397     {
398         if(xy&1)
399         printf("-X %d", xsize);
400
401         if((xy&1) && (xy&6))
402         printf(" ");
403
404         if(xy&2)
405         printf("-Y %d", ysize);
406         
407         if((xy&3) && (xy&4))
408         printf(" ");
409
410         if(xy&4)
411         printf("-r %d", swf.frameRate*100/256);
412         
413         printf("\n");
414         return 0;
415     }
416     if(html)
417     {
418         printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
419                " WIDTH=\"%d\"\n"
420                " HEIGHT=\"%d\"\n"
421                " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%d,0,0,0\">\n"
422                "  <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
423                "  <PARAM NAME=\"PLAY\" VALUE=\"true\">\n" 
424                "  <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
425                "  <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
426                "  <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n"
427                "   PLAY=\"true\" LOOP=\"true\" QUALITY=\"high\"\n"
428                "   TYPE=\"application/x-shockwave-flash\"\n"
429                "   PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">\n"
430                "  </EMBED>\n" 
431                "</OBJECT>\n", xsize, ysize, swf.fileVersion, filename, filename, xsize, ysize);
432         return 0;
433     } 
434     printf("[HEADER]        File version: %d\n", swf.fileVersion);
435     printf("[HEADER]        File size: %ld\n", swf.fileSize);
436     printf("[HEADER]        Frame rate: %f\n",swf.frameRate/256.0);
437     printf("[HEADER]        Frame count: %d\n",swf.frameCount);
438     printf("[HEADER]        Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
439     printf("[HEADER]        Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
440
441     tag = swf.firstTag;
442    
443     if(showtext) {
444         fontnum = 0;
445         swf_FontEnumerate(&swf,&fontcallback1);
446         fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
447         fontnum = 0;
448         swf_FontEnumerate(&swf,&fontcallback2);
449     }
450
451     while(tag) {
452         char*name = swf_TagGetName(tag);
453         char myprefix[128];
454         if(!name) {
455             fprintf(stderr, "Error: Unknown tag:0x%03x\n", tag->id);
456             tag = tag->next;
457             continue;
458         }
459         printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
460
461         if(swf_isDefiningTag(tag)) {
462             U16 id = swf_GetDefineID(tag);
463             printf(" defines id %04x", id);
464             if(idtab[id])
465                 fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);
466             idtab[id] = 1;
467         }
468         else if(swf_isPseudoDefiningTag(tag)) {
469             U16 id = swf_GetDefineID(tag);
470             printf(" adds information to id %04x", id);
471             if(!idtab[id])
472                 fprintf(stderr, "Error: Id %04x is not yet defined.\n", id);
473         }
474         else if(tag->id == ST_PLACEOBJECT || 
475                 tag->id == ST_PLACEOBJECT2) {
476             printf(" places id %04x at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
477             if(swf_GetName(tag))
478                 printf(" name \"%s\"",swf_GetName(tag));
479         }
480         else if(tag->id == ST_REMOVEOBJECT) {
481             printf(" removes id %04x from depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
482         }
483         else if(tag->id == ST_REMOVEOBJECT2) {
484             printf(" removes object from depth %04x", swf_GetDepth(tag));
485         }
486         else if(tag->id == ST_FRAMELABEL) {
487             printf(" \"%s\"", tag->data);
488             if(framelabel) {
489                 fprintf(stderr, "Error: Frame %d has more than one label\n", 
490                         issprite?spriteframe:mainframe);
491             }
492             if(issprite) spriteframelabel = tag->data;
493             else framelabel = tag->data;
494         }
495         else if(tag->id == ST_SHOWFRAME) {
496             char*label = issprite?spriteframelabel:framelabel;
497             printf(" %d", issprite?spriteframe:mainframe);
498             if(label)
499                 printf(" (label \"%s\")", label);
500             if(issprite) {spriteframe++; spriteframelabel = 0;}
501             if(!issprite) {mainframe++; framelabel = 0;}
502         }
503
504         if(tag->id == ST_DEFINEEDITTEXT) {
505             handleEditText(tag);
506             printf("\n");
507         }
508         else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
509             if(showtext)
510                 handleText(tag);
511             else
512                 printf("\n");
513         }
514         else {
515             printf("\n");
516         }
517         
518         sprintf(myprefix, "                %s", prefix);
519
520         if(tag->id == ST_DEFINESPRITE) {
521             sprintf(prefix, "         ");
522             if(issprite) {
523                 fprintf(stderr, "Error: Sprite definition inside a sprite definition");
524             }
525             issprite = 1;
526             spriteframe = 0;
527             spriteframelabel = 0;
528         }
529         else if(tag->id == ST_END) {
530             *prefix = 0;
531             issprite = 0;
532             if(tag->len)
533                 fprintf(stderr, "Error: End Tag not empty");
534         }
535         else if(tag->id == ST_DOACTION && action) {
536             ActionTAG*actions;
537             actions = swf_ActionGet(tag);
538             swf_DumpActions(actions, myprefix);
539         }
540         else if(tag->id == ST_DEFINEBUTTON && action) {
541             dumpButtonActions(tag, myprefix);
542         }
543         else if(tag->id == ST_DEFINEBUTTON2 && action) {
544             dumpButton2Actions(tag, myprefix);
545         }
546         else if(tag->id == ST_PLACEOBJECT2) {
547             if((*(U8*)tag->data)&0x80) 
548             handlePlaceObject2(tag, myprefix);
549         }
550         tag = tag->next;
551     }
552
553     swf_FreeTags(&swf);
554     return 0;
555 }
556
557