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 handleDefineBits(TAG*tag)
259 id = swf_GetU16(tag);
260 mode = swf_GetU8(tag);
261 width = swf_GetU16(tag);
262 height = swf_GetU16(tag);
263 printf(" image %dx%d",width,height);
264 if(mode == 3) printf(" (8 bpp)");
265 else if(mode == 4) printf(" (16 bpp)");
266 else if(mode == 5) printf(" (32 bpp)");
267 else printf(" (? bpp)");
270 void handleEditText(TAG*tag)
275 id = swf_GetU16(tag);
277 //swf_ResetReadBits(tag);
282 flags = swf_GetBits(tag,16);
283 if(flags & ET_HASFONT) {
284 swf_GetU16(tag); //font
285 swf_GetU16(tag); //fontheight
287 if(flags & ET_HASTEXTCOLOR) {
288 swf_GetU8(tag); //rgba
293 if(flags & ET_HASMAXLENGTH) {
294 swf_GetU16(tag); //maxlength
296 if(flags & ET_HASLAYOUT) {
297 swf_GetU8(tag); //align
298 swf_GetU16(tag); //left margin
299 swf_GetU16(tag); //right margin
300 swf_GetU16(tag); //indent
301 swf_GetU16(tag); //leading
303 printf(" variable \"%s\"", &tag->data[tag->pos]);
305 if(flags & (ET_X1 | ET_X2 | ET_X3 | ET_X0))
307 printf(" undefined flags: %d%d%d%d",
314 while(tag->data[tag->pos++]);
315 if(flags & ET_HASTEXT)
316 // printf(" text \"%s\"\n", &tag->data[tag->pos])
319 void printhandlerflags(U16 handlerflags)
321 if(handlerflags&1) printf("[on load]");
322 if(handlerflags&2) printf("[enter frame]");
323 if(handlerflags&4) printf("[unload]");
324 if(handlerflags&8) printf("[mouse move]");
325 if(handlerflags&16) printf("[mouse down]");
326 if(handlerflags&32) printf("[mouse up]");
327 if(handlerflags&64) printf("[key down]");
328 if(handlerflags&128) printf("[key up]");
329 if(handlerflags&256) printf("[data]");
330 if(handlerflags&0xfe00) printf("[???]");
332 void handlePlaceObject2(TAG*tag, char*prefix)
334 U8 flags = swf_GetU8(tag);
335 swf_GetU16(tag); //depth
337 if(flags&2) swf_GetU16(tag); //id
338 if(flags&4) swf_GetMatrix(tag,0);
339 if(flags&8) swf_GetCXForm(tag,0,1);
340 if(flags&16) swf_GetU16(tag); //ratio
341 if(flags&32) { while(swf_GetU8(tag)); }
342 if(flags&64) swf_GetU16(tag); //clip
350 unknown = swf_GetU16(tag);
351 globalflags = swf_GetU16(tag);
353 printf("Unknown parameter field not zero: %04x\n", unknown);
356 printf("global flags: %04x\n", globalflags);
357 handlerflags = swf_GetU16(tag);
359 handlerflags = swf_GetU32(tag);
362 while(handlerflags) {
367 globalflags &= ~handlerflags;
368 printf("%s flags %08x ",prefix, handlerflags);
369 printhandlerflags(handlerflags);
370 length = swf_GetU32(tag);
371 printf(", %d bytes actioncode\n",length);
372 a = swf_ActionGet(tag);
373 swf_DumpActions(a,prefix);
376 handlerflags = is32?swf_GetU32(tag):swf_GetU16(tag);
378 if(globalflags) // should go to sterr.
379 printf("ERROR: unsatisfied handlerflags: %02x\n", globalflags);
381 printf(" has action code\n");
386 void fontcallback1(U16 id,U8 * name)
390 void fontcallback2(U16 id,U8 * name)
392 swf_FontExtract(&swf,id,&fonts[fontnum]);
396 void dumperror(const char* format, ...)
401 va_start(arglist, format);
402 vsprintf(buf, format, arglist);
406 printf("==== Error: %s ====\n", buf);
409 int main (int argc,char ** argv)
417 char issprite = 0; // are we inside a sprite definition?
420 char* spriteframelabel = 0;
421 char* framelabel = 0;
425 memset(idtab,0,65536);
427 processargs(argc, argv);
431 fprintf(stderr, "You must supply a filename.\n");
435 f = open(filename,O_RDONLY);
439 perror("Couldn't open file: ");
442 if FAILED(swf_ReadSWF(f,&swf))
444 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
451 if(statbuf.st_size != swf.fileSize && !swf.compressed)
452 dumperror("Real Filesize (%d) doesn't match header Filesize (%d)",
453 statbuf.st_size, swf.fileSize);
454 filesize = statbuf.st_size;
459 xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
460 ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
464 printf("-X %d", xsize);
470 printf("-Y %d", ysize);
476 printf("-r %d", swf.frameRate*100/256);
483 char*fileversions[] = {"","1,0,0,0", "2,0,0,0","3,0,0,0","4,0,0,0",
484 "5,0,0,0","6,0,23,0","7,0,0,0","8,0,0,0"};
485 if(swf.fileVersion>8) {
486 fprintf(stderr, "Fileversion>8\n");
489 printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
491 //" BGCOLOR=#ffffffff\n"?
493 //http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,23,0?
494 " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%s\">\n"
495 " <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
496 " <PARAM NAME=\"PLAY\" VALUE=\"true\">\n"
497 " <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
498 " <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
499 " <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n" //bgcolor=#ffffff?
500 " PLAY=\"true\" ALIGN=\"\" LOOP=\"true\" QUALITY=\"high\"\n"
501 " TYPE=\"application/x-shockwave-flash\"\n"
502 " PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\">\n"
504 "</OBJECT>\n", xsize, ysize, fileversions[swf.fileVersion],
505 filename, filename, xsize, ysize);
508 printf("[HEADER] File version: %d\n", swf.fileVersion);
510 printf("[HEADER] File is zlib compressed.");
511 if(filesize && swf.fileSize)
512 printf(" Ratio: %02d%%\n", filesize*100/(swf.fileSize));
516 printf("[HEADER] File size: %ld%s\n", swf.fileSize, swf.compressed?" (Depacked)":"");
517 printf("[HEADER] Frame rate: %f\n",swf.frameRate/256.0);
518 printf("[HEADER] Frame count: %d\n",swf.frameCount);
519 printf("[HEADER] Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
520 printf("[HEADER] Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
526 swf_FontEnumerate(&swf,&fontcallback1);
527 fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
529 swf_FontEnumerate(&swf,&fontcallback2);
533 char*name = swf_TagGetName(tag);
536 dumperror("Unknown tag:0x%03x", tag->id);
540 printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
542 if(tag->id == ST_FREECHARACTER) {
543 U16 id = swf_GetU16(tag);
547 if(swf_isDefiningTag(tag)) {
548 U16 id = swf_GetDefineID(tag);
549 printf(" defines id %04d", id);
551 dumperror("Id %04d is defined more than once.", id);
554 else if(swf_isPseudoDefiningTag(tag)) {
555 U16 id = swf_GetDefineID(tag);
556 printf(" adds information to id %04d", id);
558 dumperror("Id %04d is not yet defined.\n", id);
560 else if(tag->id == ST_PLACEOBJECT) {
561 printf(" places id %04d at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
563 printf(" name \"%s\"",swf_GetName(tag));
565 else if(tag->id == ST_PLACEOBJECT2) {
572 printf(" id %04d",swf_GetPlaceID(tag));
576 printf(" at depth %04d", swf_GetDepth(tag));
578 printf(" name \"%s\"",swf_GetName(tag));
580 else if(tag->id == ST_REMOVEOBJECT) {
581 printf(" removes id %04d from depth %04d", swf_GetPlaceID(tag), swf_GetDepth(tag));
583 else if(tag->id == ST_REMOVEOBJECT2) {
584 printf(" removes object from depth %04d", swf_GetDepth(tag));
586 else if(tag->id == ST_STARTSOUND) {
587 printf(" starts id %04d", swf_GetPlaceID(tag));
589 else if(tag->id == ST_FRAMELABEL) {
590 int l = strlen(tag->data);
591 printf(" \"%s\"", tag->data);
593 printf(" has %d extra bytes", tag->len-1-l);
594 if(tag ->len-1-l == 1 && tag->data[tag->len-1] == 1)
597 if((framelabel && !issprite) ||
598 (spriteframelabel && issprite)) {
599 dumperror("Frame %d has more than one label",
600 issprite?spriteframe:mainframe);
602 if(issprite) spriteframelabel = tag->data;
603 else framelabel = tag->data;
605 else if(tag->id == ST_SHOWFRAME) {
606 char*label = issprite?spriteframelabel:framelabel;
607 int frame = issprite?spriteframe:mainframe;
610 while(tag->next && tag->next->id == ST_SHOWFRAME && tag->next->len == 0) {
612 if(issprite) spriteframe++;
618 printf(" %d", frame);
620 printf(" %d-%d", frame, nframe);
622 printf(" (label \"%s\")", label);
623 if(issprite) {spriteframe++; spriteframelabel = 0;}
624 if(!issprite) {mainframe++; framelabel = 0;}
627 if(tag->id == ST_DEFINEBITSLOSSLESS ||
628 tag->id == ST_DEFINEBITSLOSSLESS2) {
629 handleDefineBits(tag);
632 else if(tag->id == ST_DEFINEEDITTEXT) {
636 else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
642 else if(tag->id == ST_PLACEOBJECT2) {
648 sprintf(myprefix, " %s", prefix);
650 if(tag->id == ST_DEFINESPRITE) {
651 sprintf(prefix, " ");
653 dumperror("Sprite definition inside a sprite definition");
657 spriteframelabel = 0;
659 else if(tag->id == ST_END) {
662 spriteframelabel = 0;
664 dumperror("End Tag not empty");
666 else if(tag->id == ST_DOACTION && action) {
668 actions = swf_ActionGet(tag);
669 swf_DumpActions(actions, myprefix);
671 else if(tag->id == ST_DEFINEBUTTON && action) {
672 dumpButtonActions(tag, myprefix);
674 else if(tag->id == ST_DEFINEBUTTON2 && action) {
675 dumpButton2Actions(tag, myprefix);
677 else if(tag->id == ST_PLACEOBJECT2) {
678 if((*(U8*)tag->data)&0x80)
679 handlePlaceObject2(tag, myprefix);
684 if(tag->len && used) {
685 int num = swf_GetNumUsedIDs(tag);
689 used = (int*)malloc(sizeof(int)*num);
690 swf_GetUsedIDs(tag, used);
691 printf("%s%suses IDs: ", indent, prefix);
694 printf("%d%s", swf_GetU16(tag), t<num-1?", ":"");
700 if(tag->len && hex) {
702 printf(" %s-=> ",prefix);
703 for(t=0;t<tag->len;t++) {
704 printf("%02x ", tag->data[t]);
705 if((t && ((t&15)==15)) || (t==tag->len-1))
710 printf("\n %s-=> ",prefix);