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 handleDefineSound(TAG*tag)
255 U16 id = swf_GetU16(tag);
256 U8 flags = swf_GetU8(tag);
257 int compression = (flags>>4)&3;
258 int rate = (flags>>2)&3;
259 int bits = flags&2?16:8;
260 int stereo = flags&1;
262 if(compression == 0) printf("Raw ");
263 else if(compression == 1) printf("ADPCM ");
264 else if(compression == 2) printf("MP3 ");
266 if(rate == 0) printf("5.5Khz ");
267 if(rate == 1) printf("11Khz ");
268 if(rate == 2) printf("22Khz ");
269 if(rate == 3) printf("44Khz ");
270 printf("%dBit ", bits);
271 if(stereo) printf("stereo");
276 void handleDefineBits(TAG*tag)
282 id = swf_GetU16(tag);
283 mode = swf_GetU8(tag);
284 width = swf_GetU16(tag);
285 height = swf_GetU16(tag);
286 printf(" image %dx%d",width,height);
287 if(mode == 3) printf(" (8 bpp)");
288 else if(mode == 4) printf(" (16 bpp)");
289 else if(mode == 5) printf(" (32 bpp)");
290 else printf(" (? bpp)");
293 void handleEditText(TAG*tag)
298 id = swf_GetU16(tag);
300 //swf_ResetReadBits(tag);
305 flags = swf_GetBits(tag,16);
306 if(flags & ET_HASFONT) {
307 swf_GetU16(tag); //font
308 swf_GetU16(tag); //fontheight
310 if(flags & ET_HASTEXTCOLOR) {
311 swf_GetU8(tag); //rgba
316 if(flags & ET_HASMAXLENGTH) {
317 swf_GetU16(tag); //maxlength
319 if(flags & ET_HASLAYOUT) {
320 swf_GetU8(tag); //align
321 swf_GetU16(tag); //left margin
322 swf_GetU16(tag); //right margin
323 swf_GetU16(tag); //indent
324 swf_GetU16(tag); //leading
326 printf(" variable \"%s\"", &tag->data[tag->pos]);
328 if(flags & (ET_X1 | ET_X2 | ET_X3 | ET_X0))
330 printf(" undefined flags: %d%d%d%d",
337 while(tag->data[tag->pos++]);
338 if(flags & ET_HASTEXT)
339 // printf(" text \"%s\"\n", &tag->data[tag->pos])
342 void printhandlerflags(U16 handlerflags)
344 if(handlerflags&1) printf("[on load]");
345 if(handlerflags&2) printf("[enter frame]");
346 if(handlerflags&4) printf("[unload]");
347 if(handlerflags&8) printf("[mouse move]");
348 if(handlerflags&16) printf("[mouse down]");
349 if(handlerflags&32) printf("[mouse up]");
350 if(handlerflags&64) printf("[key down]");
351 if(handlerflags&128) printf("[key up]");
352 if(handlerflags&256) printf("[data]");
353 if(handlerflags&0xfe00) printf("[???]");
355 void handlePlaceObject2(TAG*tag, char*prefix)
357 U8 flags = swf_GetU8(tag);
358 swf_GetU16(tag); //depth
360 if(flags&2) swf_GetU16(tag); //id
361 if(flags&4) swf_GetMatrix(tag,0);
362 if(flags&8) swf_GetCXForm(tag,0,1);
363 if(flags&16) swf_GetU16(tag); //ratio
364 if(flags&32) { while(swf_GetU8(tag)); }
365 if(flags&64) swf_GetU16(tag); //clip
373 unknown = swf_GetU16(tag);
374 globalflags = swf_GetU16(tag);
376 printf("Unknown parameter field not zero: %04x\n", unknown);
379 printf("global flags: %04x\n", globalflags);
380 handlerflags = swf_GetU16(tag);
382 handlerflags = swf_GetU32(tag);
385 while(handlerflags) {
390 globalflags &= ~handlerflags;
391 printf("%s flags %08x ",prefix, handlerflags);
392 printhandlerflags(handlerflags);
393 length = swf_GetU32(tag);
394 printf(", %d bytes actioncode\n",length);
395 a = swf_ActionGet(tag);
396 swf_DumpActions(a,prefix);
399 handlerflags = is32?swf_GetU32(tag):swf_GetU16(tag);
401 if(globalflags) // should go to sterr.
402 printf("ERROR: unsatisfied handlerflags: %02x\n", globalflags);
404 printf(" has action code\n");
409 void fontcallback1(U16 id,U8 * name)
413 void fontcallback2(U16 id,U8 * name)
415 swf_FontExtract(&swf,id,&fonts[fontnum]);
419 void hexdumpTag(TAG*tag, char* prefix)
422 printf(" %s-=> ",prefix);
423 for(t=0;t<tag->len;t++) {
424 printf("%02x ", tag->data[t]);
425 if((t && ((t&15)==15)) || (t==tag->len-1))
430 printf("\n %s-=> ",prefix);
435 void dumperror(const char* format, ...)
440 va_start(arglist, format);
441 vsprintf(buf, format, arglist);
445 printf("==== Error: %s ====\n", buf);
448 int main (int argc,char ** argv)
456 char issprite = 0; // are we inside a sprite definition?
459 char* spriteframelabel = 0;
460 char* framelabel = 0;
464 memset(idtab,0,65536);
466 processargs(argc, argv);
470 fprintf(stderr, "You must supply a filename.\n");
474 f = open(filename,O_RDONLY);
478 perror("Couldn't open file: ");
481 if FAILED(swf_ReadSWF(f,&swf))
483 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
490 if(statbuf.st_size != swf.fileSize && !swf.compressed)
491 dumperror("Real Filesize (%d) doesn't match header Filesize (%d)",
492 statbuf.st_size, swf.fileSize);
493 filesize = statbuf.st_size;
498 xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
499 ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
503 printf("-X %d", xsize);
509 printf("-Y %d", ysize);
515 printf("-r %d", swf.frameRate*100/256);
522 char*fileversions[] = {"","1,0,0,0", "2,0,0,0","3,0,0,0","4,0,0,0",
523 "5,0,0,0","6,0,23,0","7,0,0,0","8,0,0,0"};
524 if(swf.fileVersion>8) {
525 fprintf(stderr, "Fileversion>8\n");
528 printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
530 //" BGCOLOR=#ffffffff\n"?
532 //http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,23,0?
533 " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%s\">\n"
534 " <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
535 " <PARAM NAME=\"PLAY\" VALUE=\"true\">\n"
536 " <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
537 " <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
538 " <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n" //bgcolor=#ffffff?
539 " PLAY=\"true\" ALIGN=\"\" LOOP=\"true\" QUALITY=\"high\"\n"
540 " TYPE=\"application/x-shockwave-flash\"\n"
541 " PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\">\n"
543 "</OBJECT>\n", xsize, ysize, fileversions[swf.fileVersion],
544 filename, filename, xsize, ysize);
547 printf("[HEADER] File version: %d\n", swf.fileVersion);
549 printf("[HEADER] File is zlib compressed.");
550 if(filesize && swf.fileSize)
551 printf(" Ratio: %02d%%\n", filesize*100/(swf.fileSize));
555 printf("[HEADER] File size: %ld%s\n", swf.fileSize, swf.compressed?" (Depacked)":"");
556 printf("[HEADER] Frame rate: %f\n",swf.frameRate/256.0);
557 printf("[HEADER] Frame count: %d\n",swf.frameCount);
558 printf("[HEADER] Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
559 printf("[HEADER] Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
565 swf_FontEnumerate(&swf,&fontcallback1);
566 fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
568 swf_FontEnumerate(&swf,&fontcallback2);
572 char*name = swf_TagGetName(tag);
575 dumperror("Unknown tag:0x%03x", tag->id);
579 printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
581 if(tag->id == ST_FREECHARACTER) {
582 U16 id = swf_GetU16(tag);
586 if(swf_isDefiningTag(tag)) {
587 U16 id = swf_GetDefineID(tag);
588 printf(" defines id %04d", id);
590 dumperror("Id %04d is defined more than once.", id);
593 else if(swf_isPseudoDefiningTag(tag)) {
594 U16 id = swf_GetDefineID(tag);
595 printf(" adds information to id %04d", id);
597 dumperror("Id %04d is not yet defined.\n", id);
599 else if(tag->id == ST_PLACEOBJECT) {
600 printf(" places id %04d at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
602 printf(" name \"%s\"",swf_GetName(tag));
604 else if(tag->id == ST_PLACEOBJECT2) {
611 printf(" id %04d",swf_GetPlaceID(tag));
615 printf(" at depth %04d", swf_GetDepth(tag));
617 printf(" name \"%s\"",swf_GetName(tag));
619 else if(tag->id == ST_REMOVEOBJECT) {
620 printf(" removes id %04d from depth %04d", swf_GetPlaceID(tag), swf_GetDepth(tag));
622 else if(tag->id == ST_REMOVEOBJECT2) {
623 printf(" removes object from depth %04d", swf_GetDepth(tag));
625 else if(tag->id == ST_FREECHARACTER) {
626 printf(" frees object %04d", swf_GetPlaceID(tag));
628 else if(tag->id == ST_STARTSOUND) {
629 printf(" starts id %04d", swf_GetPlaceID(tag));
631 else if(tag->id == ST_FRAMELABEL) {
632 int l = strlen(tag->data);
633 printf(" \"%s\"", tag->data);
635 printf(" has %d extra bytes", tag->len-1-l);
636 if(tag ->len-1-l == 1 && tag->data[tag->len-1] == 1)
639 if((framelabel && !issprite) ||
640 (spriteframelabel && issprite)) {
641 dumperror("Frame %d has more than one label",
642 issprite?spriteframe:mainframe);
644 if(issprite) spriteframelabel = tag->data;
645 else framelabel = tag->data;
647 else if(tag->id == ST_SHOWFRAME) {
648 char*label = issprite?spriteframelabel:framelabel;
649 int frame = issprite?spriteframe:mainframe;
652 while(tag->next && tag->next->id == ST_SHOWFRAME && tag->next->len == 0) {
654 if(issprite) spriteframe++;
660 printf(" %d", frame);
662 printf(" %d-%d", frame, nframe);
664 printf(" (label \"%s\")", label);
665 if(issprite) {spriteframe++; spriteframelabel = 0;}
666 if(!issprite) {mainframe++; framelabel = 0;}
669 if(tag->id == ST_SETBACKGROUNDCOLOR) {
670 U8 r = swf_GetU8(tag);
671 U8 g = swf_GetU8(tag);
672 U8 b = swf_GetU8(tag);
673 printf(" (%02x/%02x/%02x)",r,g,b);
676 if(tag->id == ST_DEFINEBITSLOSSLESS ||
677 tag->id == ST_DEFINEBITSLOSSLESS2) {
678 handleDefineBits(tag);
681 else if(tag->id == ST_DEFINESOUND) {
682 handleDefineSound(tag);
685 else if(tag->id == ST_DEFINEEDITTEXT) {
689 else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
695 else if(tag->id == ST_PLACEOBJECT2) {
701 sprintf(myprefix, " %s", prefix);
703 if(tag->id == ST_DEFINESPRITE) {
704 sprintf(prefix, " ");
706 dumperror("Sprite definition inside a sprite definition");
710 spriteframelabel = 0;
712 else if(tag->id == ST_END) {
715 spriteframelabel = 0;
717 dumperror("End Tag not empty");
719 else if(tag->id == ST_DOACTION && action) {
721 actions = swf_ActionGet(tag);
722 swf_DumpActions(actions, myprefix);
724 else if(tag->id == ST_DEFINEBUTTON && action) {
725 dumpButtonActions(tag, myprefix);
727 else if(tag->id == ST_DEFINEBUTTON2 && action) {
728 dumpButton2Actions(tag, myprefix);
730 else if(tag->id == ST_PLACEOBJECT2) {
731 if((*(U8*)tag->data)&0x80)
732 handlePlaceObject2(tag, myprefix);
737 if(tag->len && used) {
738 int num = swf_GetNumUsedIDs(tag);
742 used = (int*)malloc(sizeof(int)*num);
743 swf_GetUsedIDs(tag, used);
744 printf("%s%suses IDs: ", indent, prefix);
746 swf_SetTagPos(tag, used[t]);
747 printf("%d%s", swf_GetU16(tag), t<num-1?", ":"");
753 if(tag->len && hex) {
754 hexdumpTag(tag, prefix);