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 */
10 #include "../config.h"
12 #ifdef HAVE_SYS_STAT_H
18 #ifdef HAVE_SYS_TYPES_H
19 #include <sys/types.h>
28 #include "../lib/rfxswf.h"
29 #include "../lib/args.h"
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
46 struct options_t options[] =
62 int args_callback_option(char*name,char*val)
64 if(!strcmp(name, "V")) {
65 printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
68 else if(name[0]=='a') {
72 else if(name[0]=='t') {
76 else if(name[0]=='e') {
80 else if(name[0]=='X') {
84 else if(name[0]=='Y') {
88 else if(name[0]=='r') {
92 else if(name[0]=='d') {
96 else if(name[0]=='u') {
101 printf("Unknown option: -%s\n", name);
106 int args_callback_longoption(char*name,char*val)
108 return args_long2shortoption(options, name, val);
110 void args_callback_usage(char*name)
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");
124 int args_callback_command(char*name,char*val)
127 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
135 char* testfunc(char*str)
137 printf("%s: %s\n", what, str);
141 void dumpButton2Actions(TAG*tag, char*prefix)
147 oldTagPos = swf_GetTagPos(tag);
149 // scan DefineButton2 Record
151 swf_GetU16(tag); // Character ID
152 swf_GetU8(tag); // Flags;
154 offsetpos = swf_GetTagPos(tag); // first offset
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
168 if(tag->pos >= tag->len)
171 offsetpos = swf_GetU16(tag);
172 condition = swf_GetU16(tag); // condition
174 actions = swf_ActionGet(tag);
175 printf("%s condition %04x\n", prefix, condition);
176 swf_DumpActions(actions, prefix);
179 swf_SetTagPos(tag,oldTagPos);
183 void dumpButtonActions(TAG*tag, char*prefix)
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
192 actions = swf_ActionGet(tag);
193 swf_DumpActions(actions, prefix);
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
206 #define ET_HASLAYOUT 32
207 #define ET_NOSELECT 16
211 #define ET_USEOUTLINES 1
217 void textcallback(int*glyphs, int nr, int fontid)
220 printf(" <%2d glyphs in font %2d> ",nr, fontid);
221 for(t=0;t<fontnum;t++)
223 if(fonts[t]->id == fontid) {
233 if(glyphs[t] >= fonts[font]->numchars)
235 a = fonts[font]->glyph2ascii[glyphs[t]];
242 printf("\\x%x", (int)a);
247 void handleText(TAG*tag)
250 swf_FontExtract_DefineTextCallback(-1,0,tag,4, textcallback);
253 void handleEditText(TAG*tag)
258 id = swf_GetU16(tag);
260 //swf_ResetReadBits(tag);
265 flags = swf_GetBits(tag,16);
266 if(flags & ET_HASFONT) {
267 swf_GetU16(tag); //font
268 swf_GetU16(tag); //fontheight
270 if(flags & ET_HASTEXTCOLOR) {
271 swf_GetU8(tag); //rgba
276 if(flags & ET_HASMAXLENGTH) {
277 swf_GetU16(tag); //maxlength
279 if(flags & ET_HASLAYOUT) {
280 swf_GetU8(tag); //align
281 swf_GetU16(tag); //left margin
282 swf_GetU16(tag); //right margin
283 swf_GetU16(tag); //indent
284 swf_GetU16(tag); //leading
286 printf(" variable \"%s\"", &tag->data[tag->pos]);
288 if(flags & (ET_X1 | ET_X2 | ET_X3 | ET_X0))
290 printf(" undefined flags: %d%d%d%d",
297 while(tag->data[tag->pos++]);
298 if(flags & ET_HASTEXT)
299 // printf(" text \"%s\"\n", &tag->data[tag->pos])
302 void printhandlerflags(U16 handlerflags)
304 if(handlerflags&1) printf("[on load]");
305 if(handlerflags&2) printf("[enter frame]");
306 if(handlerflags&4) printf("[unload]");
307 if(handlerflags&8) printf("[mouse move]");
308 if(handlerflags&16) printf("[mouse down]");
309 if(handlerflags&32) printf("[mouse up]");
310 if(handlerflags&64) printf("[key down]");
311 if(handlerflags&128) printf("[key up]");
312 if(handlerflags&256) printf("[data]");
313 if(handlerflags&0xfe00) printf("[???]");
315 void handlePlaceObject2(TAG*tag, char*prefix)
317 U8 flags = swf_GetU8(tag);
318 printf("flags: %02x", flags);
319 swf_GetU16(tag); //depth
321 if(flags&2) swf_GetU16(tag); //id
322 if(flags&4) swf_GetMatrix(tag,0);
323 if(flags&8) swf_GetCXForm(tag,0,1);
324 if(flags&16) swf_GetU16(tag); //ratio
325 if(flags&32) { while(swf_GetU8(tag)); }
326 if(flags&64) swf_GetU16(tag); //clip
334 unknown = swf_GetU16(tag);
335 globalflags = swf_GetU16(tag);
337 printf("Unknown parameter field not zero: %04x\n", unknown);
340 printf("global flags: %04x\n", globalflags);
341 handlerflags = swf_GetU16(tag);
343 handlerflags = swf_GetU32(tag);
346 while(handlerflags) {
351 globalflags &= ~handlerflags;
352 printf("%s flags %08x ",prefix, handlerflags);
353 printhandlerflags(handlerflags);
354 length = swf_GetU32(tag);
355 printf(", %d bytes actioncode\n",length);
356 a = swf_ActionGet(tag);
357 swf_DumpActions(a,prefix);
360 handlerflags = is32?swf_GetU32(tag):swf_GetU16(tag);
362 if(globalflags) // should go to sterr.
363 printf("ERROR: unsatisfied handlerflags: %02x\n", globalflags);
365 printf(" has action code\n");
370 void fontcallback1(U16 id,U8 * name)
374 void fontcallback2(U16 id,U8 * name)
376 swf_FontExtract(&swf,id,&fonts[fontnum]);
380 void dumperror(const char* format, ...)
385 va_start(arglist, format);
386 vsprintf(buf, format, arglist);
389 printf("==== Error: %s ====\n", buf);
392 int main (int argc,char ** argv)
400 char issprite = 0; // are we inside a sprite definition?
403 char* spriteframelabel;
404 char* framelabel = 0;
408 memset(idtab,0,65536);
410 processargs(argc, argv);
414 fprintf(stderr, "You must supply a filename.\n");
418 f = open(filename,O_RDONLY);
422 perror("Couldn't open file: ");
425 if FAILED(swf_ReadSWF(f,&swf))
427 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
434 if(statbuf.st_size != swf.fileSize && !swf.compressed)
435 dumperror("Real Filesize (%d) doesn't match header Filesize (%d)",
436 statbuf.st_size, swf.fileSize);
437 filesize = statbuf.st_size;
442 xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
443 ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
447 printf("-X %d", xsize);
453 printf("-Y %d", ysize);
459 printf("-r %d", swf.frameRate*100/256);
466 printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
469 " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%d,0,0,0\">\n"
470 " <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
471 " <PARAM NAME=\"PLAY\" VALUE=\"true\">\n"
472 " <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
473 " <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
474 " <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n"
475 " PLAY=\"true\" LOOP=\"true\" QUALITY=\"high\"\n"
476 " TYPE=\"application/x-shockwave-flash\"\n"
477 " PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">\n"
479 "</OBJECT>\n", xsize, ysize, swf.fileVersion, filename, filename, xsize, ysize);
482 printf("[HEADER] File version: %d\n", swf.fileVersion);
484 printf("[HEADER] File is zlib compressed.");
485 if(filesize && swf.fileSize)
486 printf(" Ratio: %02d%%\n", filesize*100/(swf.fileSize));
490 printf("[HEADER] File size: %ld%s\n", swf.fileSize, swf.compressed?" (Depacked)":"");
491 printf("[HEADER] Frame rate: %f\n",swf.frameRate/256.0);
492 printf("[HEADER] Frame count: %d\n",swf.frameCount);
493 printf("[HEADER] Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
494 printf("[HEADER] Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
500 swf_FontEnumerate(&swf,&fontcallback1);
501 fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
503 swf_FontEnumerate(&swf,&fontcallback2);
507 char*name = swf_TagGetName(tag);
510 dumperror("Unknown tag:0x%03x", tag->id);
514 printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
516 if(tag->id == ST_FREECHARACTER) {
517 U16 id = swf_GetU16(tag);
521 if(swf_isDefiningTag(tag)) {
522 U16 id = swf_GetDefineID(tag);
523 printf(" defines id %04d", id);
525 dumperror("Id %04d is defined more than once.", id);
528 else if(swf_isPseudoDefiningTag(tag)) {
529 U16 id = swf_GetDefineID(tag);
530 printf(" adds information to id %04d", id);
532 dumperror("Id %04d is not yet defined.\n", id);
534 else if(tag->id == ST_PLACEOBJECT) {
535 printf(" places id %04d at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
537 printf(" name \"%s\"",swf_GetName(tag));
539 else if(tag->id == ST_PLACEOBJECT2) {
546 printf(" id %04d",swf_GetPlaceID(tag));
550 printf(" at depth %04d", swf_GetDepth(tag));
552 printf(" name \"%s\"",swf_GetName(tag));
554 else if(tag->id == ST_REMOVEOBJECT) {
555 printf(" removes id %04d from depth %04d", swf_GetPlaceID(tag), swf_GetDepth(tag));
557 else if(tag->id == ST_REMOVEOBJECT2) {
558 printf(" removes object from depth %04d", swf_GetDepth(tag));
560 else if(tag->id == ST_STARTSOUND) {
561 printf(" starts id %04d", swf_GetPlaceID(tag));
563 else if(tag->id == ST_FRAMELABEL) {
564 int l = strlen(tag->data);
565 printf(" \"%s\"", tag->data);
567 printf(" has %d extra bytes", tag->len-1-l);
568 if(tag ->len-1-l == 1 && tag->data[tag->len-1] == 1)
571 if((framelabel && !issprite) ||
572 (spriteframelabel && issprite)) {
573 dumperror("Frame %d has more than one label",
574 issprite?spriteframe:mainframe);
576 if(issprite) spriteframelabel = tag->data;
577 else framelabel = tag->data;
579 else if(tag->id == ST_SHOWFRAME) {
580 char*label = issprite?spriteframelabel:framelabel;
581 int frame = issprite?spriteframe:mainframe;
584 while(tag->next && tag->next->id == ST_SHOWFRAME && tag->next->len == 0) {
586 if(issprite) spriteframe++;
592 printf(" %d", frame);
594 printf(" %d-%d", frame, nframe);
596 printf(" (label \"%s\")", label);
597 if(issprite) {spriteframe++; spriteframelabel = 0;}
598 if(!issprite) {mainframe++; framelabel = 0;}
601 if(tag->id == ST_DEFINEEDITTEXT) {
605 else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
611 else if(tag->id == ST_PLACEOBJECT2) {
617 sprintf(myprefix, " %s", prefix);
619 if(tag->id == ST_DEFINESPRITE) {
620 sprintf(prefix, " ");
622 dumperror("Sprite definition inside a sprite definition");
626 spriteframelabel = 0;
628 else if(tag->id == ST_END) {
631 spriteframelabel = 0;
633 dumperror("End Tag not empty");
635 else if(tag->id == ST_DOACTION && action) {
637 actions = swf_ActionGet(tag);
638 swf_DumpActions(actions, myprefix);
640 else if(tag->id == ST_DEFINEBUTTON && action) {
641 dumpButtonActions(tag, myprefix);
643 else if(tag->id == ST_DEFINEBUTTON2 && action) {
644 dumpButton2Actions(tag, myprefix);
646 else if(tag->id == ST_PLACEOBJECT2) {
647 if((*(U8*)tag->data)&0x80)
648 handlePlaceObject2(tag, myprefix);
653 if(tag->len && used) {
654 int num = swf_GetNumUsedIDs(tag);
658 used = (int*)malloc(sizeof(int)*num);
659 swf_GetUsedIDs(tag, used);
660 printf("%s%suses IDs: ", indent, prefix);
663 printf("%d%s", swf_GetU16(tag), t<num-1?", ":"");
669 if(tag->len && hex) {
671 printf(" %s-=> ",prefix);
672 for(t=0;t<tag->len;t++) {
673 printf("%02x ", tag->data[t]);
674 if((t && ((t&15)==15)) || (t==tag->len-1))
679 printf("\n %s-=> ",prefix);