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\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");
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
313 handlerflags = swf_GetU16(tag);
314 printf("%s global flags:%04x ",prefix, handlerflags);
315 printhandlerflags(handlerflags);
321 handlerflags = swf_GetU16(tag);
324 printf("%s flags:%04x ",prefix, handlerflags);
325 printhandlerflags(handlerflags);
328 length = swf_GetU32(tag);
329 printf("%s %d bytes actioncode\n",prefix);
330 a = swf_ActionGet(tag);
331 swf_DumpActions(a,prefix);
337 void fontcallback1(U16 id,U8 * name)
341 void fontcallback2(U16 id,U8 * name)
342 { swf_FontExtract(&swf,id,&fonts[fontnum]);
346 int main (int argc,char ** argv)
354 char issprite = 0; // are we inside a sprite definition?
357 char* spriteframelabel;
358 char* framelabel = 0;
361 memset(idtab,0,65536);
363 processargs(argc, argv);
367 fprintf(stderr, "You must supply a filename.\n");
371 f = open(filename,O_RDONLY);
375 perror("Couldn't open file: ");
378 if FAILED(swf_ReadSWF(f,&swf))
380 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
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);
394 xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
395 ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
399 printf("-X %d", xsize);
405 printf("-Y %d", ysize);
411 printf("-r %d", swf.frameRate*100/256);
418 printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\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"
431 "</OBJECT>\n", xsize, ysize, swf.fileVersion, filename, filename, xsize, ysize);
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);
445 swf_FontEnumerate(&swf,&fontcallback1);
446 fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
448 swf_FontEnumerate(&swf,&fontcallback2);
452 char*name = swf_TagGetName(tag);
455 fprintf(stderr, "Error: Unknown tag:0x%03x\n", tag->id);
459 printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
461 if(swf_isDefiningTag(tag)) {
462 U16 id = swf_GetDefineID(tag);
463 printf(" defines id %04x", id);
465 fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);
468 else if(swf_isPseudoDefiningTag(tag)) {
469 U16 id = swf_GetDefineID(tag);
470 printf(" adds information to id %04x", id);
472 fprintf(stderr, "Error: Id %04x is not yet defined.\n", id);
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));
478 printf(" name \"%s\"",swf_GetName(tag));
480 else if(tag->id == ST_REMOVEOBJECT) {
481 printf(" removes id %04x from depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
483 else if(tag->id == ST_REMOVEOBJECT2) {
484 printf(" removes object from depth %04x", swf_GetDepth(tag));
486 else if(tag->id == ST_FRAMELABEL) {
487 printf(" \"%s\"", tag->data);
489 fprintf(stderr, "Error: Frame %d has more than one label\n",
490 issprite?spriteframe:mainframe);
492 if(issprite) spriteframelabel = tag->data;
493 else framelabel = tag->data;
495 else if(tag->id == ST_SHOWFRAME) {
496 char*label = issprite?spriteframelabel:framelabel;
497 printf(" %d", issprite?spriteframe:mainframe);
499 printf(" (label \"%s\")", label);
500 if(issprite) {spriteframe++; spriteframelabel = 0;}
501 if(!issprite) {mainframe++; framelabel = 0;}
504 if(tag->id == ST_DEFINEEDITTEXT) {
508 else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
518 sprintf(myprefix, " %s", prefix);
520 if(tag->id == ST_DEFINESPRITE) {
521 sprintf(prefix, " ");
523 fprintf(stderr, "Error: Sprite definition inside a sprite definition");
527 spriteframelabel = 0;
529 else if(tag->id == ST_END) {
533 fprintf(stderr, "Error: End Tag not empty");
535 else if(tag->id == ST_DOACTION && action) {
537 actions = swf_ActionGet(tag);
538 swf_DumpActions(actions, myprefix);
540 else if(tag->id == ST_DEFINEBUTTON && action) {
541 dumpButtonActions(tag, myprefix);
543 else if(tag->id == ST_DEFINEBUTTON2 && action) {
544 dumpButton2Actions(tag, myprefix);
546 else if(tag->id == ST_PLACEOBJECT2) {
547 if((*(U8*)tag->data)&0x80)
548 handlePlaceObject2(tag, myprefix);