2 Shows the structure of a swf file
4 Part of the swftools package.
6 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
8 This file is distributed under the GPL, see file COPYING for details */
12 #ifdef HAVE_SYS_STAT_H
18 #ifdef HAVE_SYS_TYPES_H
19 #include <sys/types.h>
27 #include "../lib/rfxswf.h"
28 #include "../lib/args.h"
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
42 struct options_t options[] =
56 int args_callback_option(char*name,char*val)
58 if(!strcmp(name, "V")) {
59 printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
62 else if(name[0]=='a') {
66 else if(name[0]=='t') {
70 else if(name[0]=='e') {
74 else if(name[0]=='X') {
78 else if(name[0]=='Y') {
82 else if(name[0]=='r') {
87 printf("Unknown option: -%s\n", name);
92 int args_callback_longoption(char*name,char*val)
94 return args_long2shortoption(options, name, val);
96 void args_callback_usage(char*name)
98 printf("Usage: %s [-at] file.swf\n", name);
99 printf("\t-h , --help\t\t Print help and exit\n");
100 printf("\t-e , --html\t\t Create a html embedding the file (simple, but useful)\n");
101 printf("\t-X , --width\t\t Prints out a string of the form \"-X width\"\n");
102 printf("\t-Y , --height\t\t Prints out a string of the form \"-Y height\"\n");
103 printf("\t-r , --rate\t\t Prints out a string of the form \"-r rate\"\n");
104 printf("\t-a , --action\t\t Disassemble action tags\n");
105 printf("\t-t , --text\t\t Show text data\n");
106 printf("\t-V , --version\t\t Print program version and exit\n");
108 int args_callback_command(char*name,char*val)
111 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
119 char* testfunc(char*str)
121 printf("%s: %s\n", what, str);
125 void dumpButton2Actions(TAG*tag, char*prefix)
131 oldTagPos = swf_GetTagPos(tag);
133 // scan DefineButton2 Record
135 swf_GetU16(tag); // Character ID
136 swf_GetU8(tag); // Flags;
138 offsetpos = swf_GetTagPos(tag); // first offset
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
152 if(tag->pos >= tag->len)
155 offsetpos = swf_GetU16(tag);
156 condition = swf_GetU16(tag); // condition
158 actions = swf_ActionGet(tag);
159 printf("%s condition %04x\n", prefix, condition);
160 swf_DumpActions(actions, prefix);
163 swf_SetTagPos(tag,oldTagPos);
167 void dumpButtonActions(TAG*tag, char*prefix)
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
176 actions = swf_ActionGet(tag);
177 swf_DumpActions(actions, prefix);
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
190 #define ET_HASLAYOUT 32
191 #define ET_NOSELECT 16
195 #define ET_USEOUTLINES 1
201 void textcallback(int*glyphs, int nr, int fontid)
204 printf(" <%2d glyphs in font %2d> ",nr, fontid);
205 for(t=0;t<fontnum;t++)
207 if(fonts[t]->id == fontid) {
214 return; // todo: should we report this? (may only be that it's a definefont without fontinfo)
220 if(glyphs[t] >= fonts[font]->numchars)
222 a = fonts[font]->glyph2ascii[glyphs[t]];
226 printf("\\x%x", (int)a);
231 void handleText(TAG*tag)
234 swf_FontExtract_DefineTextCallback(-1,0,tag,4, textcallback);
237 void handleEditText(TAG*tag)
242 id = swf_GetU16(tag);
244 //swf_ResetReadBits(tag);
249 flags = swf_GetBits(tag,16);
250 if(flags & ET_HASFONT) {
251 swf_GetU16(tag); //font
252 swf_GetU16(tag); //fontheight
254 if(flags & ET_HASTEXTCOLOR) {
255 swf_GetU8(tag); //rgba
260 if(flags & ET_HASMAXLENGTH) {
261 swf_GetU16(tag); //maxlength
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
270 printf(" variable \"%s\"", &tag->data[tag->pos]);
272 if(flags & (ET_X1 | ET_X2 | ET_X3 | ET_X0))
274 printf(" undefined flags: %d%d%d%d",
281 while(tag->data[tag->pos++]);
282 if(flags & ET_HASTEXT)
283 // printf(" text \"%s\"\n", &tag->data[tag->pos])
286 void printhandlerflags(U16 handlerflags)
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("[???]");
299 void handlePlaceObject2(TAG*tag, char*prefix)
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
307 while(swf_GetU8(tag));
309 if(flags&64) swf_GetU16(tag); //clip
315 globalflags = swf_GetU16(tag);
316 // printf("%s global flags:%04x ",prefix, handlerflags);
317 // printhandlerflags(globalflags);
324 handlerflags = swf_GetU16(tag);
327 globalflags &= ~handlerflags;
328 printf("%s flags %04x ",prefix, handlerflags);
329 printhandlerflags(handlerflags);
331 length = swf_GetU32(tag);
332 printf(", %d bytes actioncode\n",length);
333 a = swf_ActionGet(tag);
334 swf_DumpActions(a,prefix);
337 if(globalflags) // should go to sterr.
338 printf("ERROR: unsatisfied handlerflags: %02x", globalflags);
340 printf(" has action code\n");
345 void fontcallback1(U16 id,U8 * name)
349 void fontcallback2(U16 id,U8 * name)
350 { swf_FontExtract(&swf,id,&fonts[fontnum]);
354 int main (int argc,char ** argv)
362 char issprite = 0; // are we inside a sprite definition?
365 char* spriteframelabel;
366 char* framelabel = 0;
369 memset(idtab,0,65536);
371 processargs(argc, argv);
375 fprintf(stderr, "You must supply a filename.\n");
379 f = open(filename,O_RDONLY);
383 perror("Couldn't open file: ");
386 if FAILED(swf_ReadSWF(f,&swf))
388 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
395 if(statbuf.st_size != swf.FileSize)
396 fprintf(stderr, "Error: Real Filesize (%d) doesn't match header Filesize (%d)",
397 statbuf.st_size, swf.FileSize);
402 xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
403 ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
407 printf("-X %d", xsize);
413 printf("-Y %d", ysize);
419 printf("-r %d", swf.frameRate*100/256);
426 printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
429 " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%d,0,0,0\">\n"
430 " <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
431 " <PARAM NAME=\"PLAY\" VALUE=\"true\">\n"
432 " <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
433 " <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
434 " <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n"
435 " PLAY=\"true\" LOOP=\"true\" QUALITY=\"high\"\n"
436 " TYPE=\"application/x-shockwave-flash\"\n"
437 " PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">\n"
439 "</OBJECT>\n", xsize, ysize, swf.fileVersion, filename, filename, xsize, ysize);
442 printf("[HEADER] File version: %d\n", swf.fileVersion);
443 printf("[HEADER] File size: %ld\n", swf.fileSize);
444 printf("[HEADER] Frame rate: %f\n",swf.frameRate/256.0);
445 printf("[HEADER] Frame count: %d\n",swf.frameCount);
446 printf("[HEADER] Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
447 printf("[HEADER] Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
453 swf_FontEnumerate(&swf,&fontcallback1);
454 fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
456 swf_FontEnumerate(&swf,&fontcallback2);
460 char*name = swf_TagGetName(tag);
463 fprintf(stderr, "Error: Unknown tag:0x%03x\n", tag->id);
467 printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
469 if(swf_isDefiningTag(tag)) {
470 U16 id = swf_GetDefineID(tag);
471 printf(" defines id %04x", id);
473 fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);
476 else if(swf_isPseudoDefiningTag(tag)) {
477 U16 id = swf_GetDefineID(tag);
478 printf(" adds information to id %04x", id);
480 fprintf(stderr, "Error: Id %04x is not yet defined.\n", id);
482 else if(tag->id == ST_PLACEOBJECT ||
483 tag->id == ST_PLACEOBJECT2) {
484 printf(" places id %04x at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
486 printf(" name \"%s\"",swf_GetName(tag));
488 else if(tag->id == ST_REMOVEOBJECT) {
489 printf(" removes id %04x from depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
491 else if(tag->id == ST_REMOVEOBJECT2) {
492 printf(" removes object from depth %04x", swf_GetDepth(tag));
494 else if(tag->id == ST_FRAMELABEL) {
495 printf(" \"%s\"", tag->data);
497 fprintf(stderr, "Error: Frame %d has more than one label\n",
498 issprite?spriteframe:mainframe);
500 if(issprite) spriteframelabel = tag->data;
501 else framelabel = tag->data;
503 else if(tag->id == ST_SHOWFRAME) {
504 char*label = issprite?spriteframelabel:framelabel;
505 int frame = issprite?spriteframe:mainframe;
508 while(tag->next && tag->next->id == ST_SHOWFRAME && tag->next->len == 0) {
510 if(issprite) spriteframe++;
516 printf(" %d", frame);
518 printf(" %d-%d", frame, nframe);
520 printf(" (label \"%s\")", label);
521 if(issprite) {spriteframe++; spriteframelabel = 0;}
522 if(!issprite) {mainframe++; framelabel = 0;}
525 if(tag->id == ST_DEFINEEDITTEXT) {
529 else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
535 else if(tag->id == ST_PLACEOBJECT2) {
541 sprintf(myprefix, " %s", prefix);
543 if(tag->id == ST_DEFINESPRITE) {
544 sprintf(prefix, " ");
546 fprintf(stderr, "Error: Sprite definition inside a sprite definition");
550 spriteframelabel = 0;
552 else if(tag->id == ST_END) {
556 fprintf(stderr, "Error: End Tag not empty");
558 else if(tag->id == ST_DOACTION && action) {
560 actions = swf_ActionGet(tag);
561 swf_DumpActions(actions, myprefix);
563 else if(tag->id == ST_DEFINEBUTTON && action) {
564 dumpButtonActions(tag, myprefix);
566 else if(tag->id == ST_DEFINEBUTTON2 && action) {
567 dumpButton2Actions(tag, myprefix);
569 else if(tag->id == ST_PLACEOBJECT2) {
570 if((*(U8*)tag->data)&0x80)
571 handlePlaceObject2(tag, myprefix);