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
43 struct options_t options[] =
58 int args_callback_option(char*name,char*val)
60 if(!strcmp(name, "V")) {
61 printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
64 else if(name[0]=='a') {
68 else if(name[0]=='t') {
72 else if(name[0]=='e') {
76 else if(name[0]=='X') {
80 else if(name[0]=='Y') {
84 else if(name[0]=='r') {
88 else if(name[0]=='d') {
93 printf("Unknown option: -%s\n", name);
98 int args_callback_longoption(char*name,char*val)
100 return args_long2shortoption(options, name, val);
102 void args_callback_usage(char*name)
104 printf("Usage: %s [-at] file.swf\n", name);
105 printf("\t-h , --help\t\t Print help and exit\n");
106 printf("\t-e , --html\t\t Create a html embedding the file (simple, but useful)\n");
107 printf("\t-X , --width\t\t Prints out a string of the form \"-X width\"\n");
108 printf("\t-Y , --height\t\t Prints out a string of the form \"-Y height\"\n");
109 printf("\t-r , --rate\t\t Prints out a string of the form \"-r rate\"\n");
110 printf("\t-a , --action\t\t Disassemble action tags\n");
111 printf("\t-t , --text\t\t Show text data\n");
112 printf("\t-d , --hex\t\t Print hex output of tag data, too\n");
113 printf("\t-V , --version\t\t Print program version and exit\n");
115 int args_callback_command(char*name,char*val)
118 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
126 char* testfunc(char*str)
128 printf("%s: %s\n", what, str);
132 void dumpButton2Actions(TAG*tag, char*prefix)
138 oldTagPos = swf_GetTagPos(tag);
140 // scan DefineButton2 Record
142 swf_GetU16(tag); // Character ID
143 swf_GetU8(tag); // Flags;
145 offsetpos = swf_GetTagPos(tag); // first offset
148 while (swf_GetU8(tag)) // state -> parse ButtonRecord
149 { swf_GetU16(tag); // id
150 swf_GetU16(tag); // layer
151 swf_GetMatrix(tag,NULL); // matrix
152 swf_GetCXForm(tag,NULL,1); // cxform
159 if(tag->pos >= tag->len)
162 offsetpos = swf_GetU16(tag);
163 condition = swf_GetU16(tag); // condition
165 actions = swf_ActionGet(tag);
166 printf("%s condition %04x\n", prefix, condition);
167 swf_DumpActions(actions, prefix);
170 swf_SetTagPos(tag,oldTagPos);
174 void dumpButtonActions(TAG*tag, char*prefix)
177 swf_GetU16(tag); // id
178 while (swf_GetU8(tag)) // state -> parse ButtonRecord
179 { swf_GetU16(tag); // id
180 swf_GetU16(tag); // layer
181 swf_GetMatrix(tag,NULL); // matrix
183 actions = swf_ActionGet(tag);
184 swf_DumpActions(actions, prefix);
187 #define ET_HASTEXT 32768
188 #define ET_WORDWRAP 16384
189 #define ET_MULTILINE 8192
190 #define ET_PASSWORD 4096
191 #define ET_READONLY 2048
192 #define ET_HASTEXTCOLOR 1024
193 #define ET_HASMAXLENGTH 512
194 #define ET_HASFONT 256
197 #define ET_HASLAYOUT 32
198 #define ET_NOSELECT 16
202 #define ET_USEOUTLINES 1
208 void textcallback(int*glyphs, int nr, int fontid)
211 printf(" <%2d glyphs in font %2d> ",nr, fontid);
212 for(t=0;t<fontnum;t++)
214 if(fonts[t]->id == fontid) {
221 return; // todo: should we report this? (may only be that it's a definefont without fontinfo)
227 if(glyphs[t] >= fonts[font]->numchars)
229 a = fonts[font]->glyph2ascii[glyphs[t]];
233 printf("\\x%x", (int)a);
238 void handleText(TAG*tag)
241 swf_FontExtract_DefineTextCallback(-1,0,tag,4, textcallback);
244 void handleEditText(TAG*tag)
249 id = swf_GetU16(tag);
251 //swf_ResetReadBits(tag);
256 flags = swf_GetBits(tag,16);
257 if(flags & ET_HASFONT) {
258 swf_GetU16(tag); //font
259 swf_GetU16(tag); //fontheight
261 if(flags & ET_HASTEXTCOLOR) {
262 swf_GetU8(tag); //rgba
267 if(flags & ET_HASMAXLENGTH) {
268 swf_GetU16(tag); //maxlength
270 if(flags & ET_HASLAYOUT) {
271 swf_GetU8(tag); //align
272 swf_GetU16(tag); //left margin
273 swf_GetU16(tag); //right margin
274 swf_GetU16(tag); //indent
275 swf_GetU16(tag); //leading
277 printf(" variable \"%s\"", &tag->data[tag->pos]);
279 if(flags & (ET_X1 | ET_X2 | ET_X3 | ET_X0))
281 printf(" undefined flags: %d%d%d%d",
288 while(tag->data[tag->pos++]);
289 if(flags & ET_HASTEXT)
290 // printf(" text \"%s\"\n", &tag->data[tag->pos])
293 void printhandlerflags(U16 handlerflags)
295 if(handlerflags&1) printf("[on load]");
296 if(handlerflags&2) printf("[enter frame]");
297 if(handlerflags&4) printf("[unload]");
298 if(handlerflags&8) printf("[mouse move]");
299 if(handlerflags&16) printf("[mouse down]");
300 if(handlerflags&32) printf("[mouse up]");
301 if(handlerflags&64) printf("[key down]");
302 if(handlerflags&128) printf("[key up]");
303 if(handlerflags&256) printf("[data]");
304 if(handlerflags&0xfe00) printf("[???]");
306 void handlePlaceObject2(TAG*tag, char*prefix)
308 U8 flags = swf_GetU8(tag);
309 swf_GetU16(tag); //depth
311 if(flags&2) swf_GetU16(tag); //id
312 if(flags&4) swf_GetMatrix(tag,0);
313 if(flags&8) swf_GetCXForm(tag,0,0);
314 if(flags&16) swf_GetU16(tag); //ratio
316 while(swf_GetU8(tag));
318 if(flags&64) swf_GetU16(tag); //clip
324 unknown = swf_GetU16(tag);
325 globalflags = swf_GetU16(tag);
327 printf("Unknown parameter field not zero: %04x\n", unknown);
333 handlerflags = swf_GetU16(tag);
336 globalflags &= ~handlerflags;
337 printf("%s flags %04x ",prefix, handlerflags);
338 printhandlerflags(handlerflags);
340 length = swf_GetU32(tag);
341 printf(", %d bytes actioncode\n",length);
342 a = swf_ActionGet(tag);
343 swf_DumpActions(a,prefix);
346 if(globalflags) // should go to sterr.
347 printf("ERROR: unsatisfied handlerflags: %02x\n", globalflags);
349 printf(" has action code\n");
354 void fontcallback1(U16 id,U8 * name)
358 void fontcallback2(U16 id,U8 * name)
359 { swf_FontExtract(&swf,id,&fonts[fontnum]);
363 int main (int argc,char ** argv)
371 char issprite = 0; // are we inside a sprite definition?
374 char* spriteframelabel;
375 char* framelabel = 0;
378 memset(idtab,0,65536);
380 processargs(argc, argv);
384 fprintf(stderr, "You must supply a filename.\n");
388 f = open(filename,O_RDONLY);
392 perror("Couldn't open file: ");
395 if FAILED(swf_ReadSWF(f,&swf))
397 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
404 if(statbuf.st_size != swf.FileSize)
405 fprintf(stderr, "Error: Real Filesize (%d) doesn't match header Filesize (%d)",
406 statbuf.st_size, swf.FileSize);
411 xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
412 ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
416 printf("-X %d", xsize);
422 printf("-Y %d", ysize);
428 printf("-r %d", swf.frameRate*100/256);
435 printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
438 " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%d,0,0,0\">\n"
439 " <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
440 " <PARAM NAME=\"PLAY\" VALUE=\"true\">\n"
441 " <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
442 " <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
443 " <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n"
444 " PLAY=\"true\" LOOP=\"true\" QUALITY=\"high\"\n"
445 " TYPE=\"application/x-shockwave-flash\"\n"
446 " PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">\n"
448 "</OBJECT>\n", xsize, ysize, swf.fileVersion, filename, filename, xsize, ysize);
451 printf("[HEADER] File version: %d\n", swf.fileVersion);
452 printf("[HEADER] File size: %ld\n", swf.fileSize);
453 printf("[HEADER] Frame rate: %f\n",swf.frameRate/256.0);
454 printf("[HEADER] Frame count: %d\n",swf.frameCount);
455 printf("[HEADER] Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
456 printf("[HEADER] Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
462 swf_FontEnumerate(&swf,&fontcallback1);
463 fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
465 swf_FontEnumerate(&swf,&fontcallback2);
469 char*name = swf_TagGetName(tag);
472 fprintf(stderr, "Error: Unknown tag:0x%03x\n", tag->id);
476 printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
478 if(tag->id == ST_FREECHARACTER) {
479 U16 id = swf_GetU16(tag);
483 if(swf_isDefiningTag(tag)) {
484 U16 id = swf_GetDefineID(tag);
485 printf(" defines id %04x", id);
487 fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);
490 else if(swf_isPseudoDefiningTag(tag)) {
491 U16 id = swf_GetDefineID(tag);
492 printf(" adds information to id %04x", id);
494 fprintf(stderr, "Error: Id %04x is not yet defined.\n", id);
496 else if(tag->id == ST_PLACEOBJECT ||
497 tag->id == ST_PLACEOBJECT2) {
498 printf(" places id %04x at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
500 printf(" name \"%s\"",swf_GetName(tag));
502 else if(tag->id == ST_REMOVEOBJECT) {
503 printf(" removes id %04x from depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
505 else if(tag->id == ST_REMOVEOBJECT2) {
506 printf(" removes object from depth %04x", swf_GetDepth(tag));
508 else if(tag->id == ST_FRAMELABEL) {
509 printf(" \"%s\"", tag->data);
511 fprintf(stderr, "Error: Frame %d has more than one label\n",
512 issprite?spriteframe:mainframe);
514 if(issprite) spriteframelabel = tag->data;
515 else framelabel = tag->data;
517 else if(tag->id == ST_SHOWFRAME) {
518 char*label = issprite?spriteframelabel:framelabel;
519 int frame = issprite?spriteframe:mainframe;
522 while(tag->next && tag->next->id == ST_SHOWFRAME && tag->next->len == 0) {
524 if(issprite) spriteframe++;
530 printf(" %d", frame);
532 printf(" %d-%d", frame, nframe);
534 printf(" (label \"%s\")", label);
535 if(issprite) {spriteframe++; spriteframelabel = 0;}
536 if(!issprite) {mainframe++; framelabel = 0;}
539 if(tag->id == ST_DEFINEEDITTEXT) {
543 else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
549 else if(tag->id == ST_PLACEOBJECT2) {
555 sprintf(myprefix, " %s", prefix);
557 if(tag->id == ST_DEFINESPRITE) {
558 sprintf(prefix, " ");
560 fprintf(stderr, "Error: Sprite definition inside a sprite definition");
564 spriteframelabel = 0;
566 else if(tag->id == ST_END) {
570 fprintf(stderr, "Error: End Tag not empty");
572 else if(tag->id == ST_DOACTION && action) {
574 actions = swf_ActionGet(tag);
575 swf_DumpActions(actions, myprefix);
577 else if(tag->id == ST_DEFINEBUTTON && action) {
578 dumpButtonActions(tag, myprefix);
580 else if(tag->id == ST_DEFINEBUTTON2 && action) {
581 dumpButton2Actions(tag, myprefix);
583 else if(tag->id == ST_PLACEOBJECT2) {
584 if((*(U8*)tag->data)&0x80)
585 handlePlaceObject2(tag, myprefix);
589 if(tag->len && hex) {
591 printf(" %s-=> ",prefix);
592 for(t=0;t<tag->len;t++) {
593 printf("%02x ", tag->data[t]);
594 if((t && !(t&15)) || (t==tag->len-1))
599 printf("\n %s-=> ",prefix);