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[] =
63 int args_callback_option(char*name,char*val)
65 if(!strcmp(name, "V")) {
66 printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
69 else if(name[0]=='a') {
73 else if(name[0]=='t') {
77 else if(name[0]=='e') {
81 else if(name[0]=='X') {
85 else if(name[0]=='Y') {
89 else if(name[0]=='r') {
93 else if(name[0]=='f') {
97 else if(name[0]=='d') {
101 else if(name[0]=='u') {
106 printf("Unknown option: -%s\n", name);
112 int args_callback_longoption(char*name,char*val)
114 return args_long2shortoption(options, name, val);
116 void args_callback_usage(char*name)
118 printf("Usage: %s [-at] file.swf\n", name);
119 printf("\t-h , --help\t\t Print help and exit\n");
120 printf("\t-e , --html\t\t Create html output embedding the file (simple, but useful)\n");
121 printf("\t-X , --width\t\t Prints out a string of the form \"-X width\"\n");
122 printf("\t-Y , --height\t\t Prints out a string of the form \"-Y height\"\n");
123 printf("\t-r , --rate\t\t Prints out a string of the form \"-r rate\"\n");
124 printf("\t-f , --frames\t\t Prints out a string of the form \"-f framenum\"\n");
125 printf("\t-a , --action\t\t Disassemble action tags\n");
126 printf("\t-t , --text\t\t Show text data\n");
127 printf("\t-d , --hex\t\t Print hex output of tag data, too\n");
128 printf("\t-u , --used\t\t Show referred IDs for each Tag\n");
129 printf("\t-V , --version\t\t Print program version and exit\n");
131 int args_callback_command(char*name,char*val)
134 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
142 char* testfunc(char*str)
144 printf("%s: %s\n", what, str);
148 void dumpButton2Actions(TAG*tag, char*prefix)
154 oldTagPos = swf_GetTagPos(tag);
156 // scan DefineButton2 Record
158 swf_GetU16(tag); // Character ID
159 swf_GetU8(tag); // Flags;
161 offsetpos = swf_GetTagPos(tag); // first offset
164 while (swf_GetU8(tag)) // state -> parse ButtonRecord
165 { swf_GetU16(tag); // id
166 swf_GetU16(tag); // layer
167 swf_GetMatrix(tag,NULL); // matrix
168 swf_GetCXForm(tag,NULL,1); // cxform
175 if(tag->pos >= tag->len)
178 offsetpos = swf_GetU16(tag);
179 condition = swf_GetU16(tag); // condition
181 actions = swf_ActionGet(tag);
182 printf("%s condition %04x\n", prefix, condition);
183 swf_DumpActions(actions, prefix);
186 swf_SetTagPos(tag,oldTagPos);
190 void dumpButtonActions(TAG*tag, char*prefix)
193 swf_GetU16(tag); // id
194 while (swf_GetU8(tag)) // state -> parse ButtonRecord
195 { swf_GetU16(tag); // id
196 swf_GetU16(tag); // layer
197 swf_GetMatrix(tag,NULL); // matrix
199 actions = swf_ActionGet(tag);
200 swf_DumpActions(actions, prefix);
203 #define ET_HASTEXT 32768
204 #define ET_WORDWRAP 16384
205 #define ET_MULTILINE 8192
206 #define ET_PASSWORD 4096
207 #define ET_READONLY 2048
208 #define ET_HASTEXTCOLOR 1024
209 #define ET_HASMAXLENGTH 512
210 #define ET_HASFONT 256
213 #define ET_HASLAYOUT 32
214 #define ET_NOSELECT 16
218 #define ET_USEOUTLINES 1
224 void textcallback(int*glyphs, int nr, int fontid)
227 printf(" <%2d glyphs in font %2d> ",nr, fontid);
228 for(t=0;t<fontnum;t++)
230 if(fonts[t]->id == fontid) {
240 if(glyphs[t] >= fonts[font]->numchars)
242 a = fonts[font]->glyph2ascii[glyphs[t]];
249 printf("\\x%x", (int)a);
254 void handleText(TAG*tag)
257 swf_FontExtract_DefineTextCallback(-1,0,tag,4, textcallback);
260 void handleDefineSound(TAG*tag)
262 U16 id = swf_GetU16(tag);
263 U8 flags = swf_GetU8(tag);
264 int compression = (flags>>4)&3;
265 int rate = (flags>>2)&3;
266 int bits = flags&2?16:8;
267 int stereo = flags&1;
269 if(compression == 0) printf("Raw ");
270 else if(compression == 1) printf("ADPCM ");
271 else if(compression == 2) printf("MP3 ");
273 if(rate == 0) printf("5.5Khz ");
274 if(rate == 1) printf("11Khz ");
275 if(rate == 2) printf("22Khz ");
276 if(rate == 3) printf("44Khz ");
277 printf("%dBit ", bits);
278 if(stereo) printf("stereo");
283 void handleDefineBits(TAG*tag)
289 id = swf_GetU16(tag);
290 mode = swf_GetU8(tag);
291 width = swf_GetU16(tag);
292 height = swf_GetU16(tag);
293 printf(" image %dx%d",width,height);
294 if(mode == 3) printf(" (8 bpp)");
295 else if(mode == 4) printf(" (16 bpp)");
296 else if(mode == 5) printf(" (32 bpp)");
297 else printf(" (? bpp)");
300 void handleEditText(TAG*tag)
305 id = swf_GetU16(tag);
307 //swf_ResetReadBits(tag);
312 flags = swf_GetBits(tag,16);
313 if(flags & ET_HASFONT) {
314 swf_GetU16(tag); //font
315 swf_GetU16(tag); //fontheight
317 if(flags & ET_HASTEXTCOLOR) {
318 swf_GetU8(tag); //rgba
323 if(flags & ET_HASMAXLENGTH) {
324 swf_GetU16(tag); //maxlength
326 if(flags & ET_HASLAYOUT) {
327 swf_GetU8(tag); //align
328 swf_GetU16(tag); //left margin
329 swf_GetU16(tag); //right margin
330 swf_GetU16(tag); //indent
331 swf_GetU16(tag); //leading
333 printf(" variable \"%s\"", &tag->data[tag->pos]);
335 if(flags & (ET_X1 | ET_X2 | ET_X3 | ET_X0))
337 printf(" undefined flags: %d%d%d%d",
344 while(tag->data[tag->pos++]);
345 if(flags & ET_HASTEXT)
346 // printf(" text \"%s\"\n", &tag->data[tag->pos])
349 void printhandlerflags(U16 handlerflags)
351 if(handlerflags&1) printf("[on load]");
352 if(handlerflags&2) printf("[enter frame]");
353 if(handlerflags&4) printf("[unload]");
354 if(handlerflags&8) printf("[mouse move]");
355 if(handlerflags&16) printf("[mouse down]");
356 if(handlerflags&32) printf("[mouse up]");
357 if(handlerflags&64) printf("[key down]");
358 if(handlerflags&128) printf("[key up]");
359 if(handlerflags&256) printf("[data]");
360 if(handlerflags&0xfe00) printf("[???]");
362 void handlePlaceObject2(TAG*tag, char*prefix)
364 U8 flags = swf_GetU8(tag);
365 swf_GetU16(tag); //depth
367 if(flags&2) swf_GetU16(tag); //id
368 if(flags&4) swf_GetMatrix(tag,0);
369 if(flags&8) swf_GetCXForm(tag,0,1);
370 if(flags&16) swf_GetU16(tag); //ratio
371 if(flags&32) { while(swf_GetU8(tag)); }
372 if(flags&64) swf_GetU16(tag); //clip
380 unknown = swf_GetU16(tag);
381 globalflags = swf_GetU16(tag);
383 printf("Unknown parameter field not zero: %04x\n", unknown);
386 printf("global flags: %04x\n", globalflags);
387 handlerflags = swf_GetU16(tag);
389 handlerflags = swf_GetU32(tag);
392 while(handlerflags) {
397 globalflags &= ~handlerflags;
398 printf("%s flags %08x ",prefix, handlerflags);
399 printhandlerflags(handlerflags);
400 length = swf_GetU32(tag);
401 printf(", %d bytes actioncode\n",length);
402 a = swf_ActionGet(tag);
403 swf_DumpActions(a,prefix);
406 handlerflags = is32?swf_GetU32(tag):swf_GetU16(tag);
408 if(globalflags) // should go to sterr.
409 printf("ERROR: unsatisfied handlerflags: %02x\n", globalflags);
411 printf(" has action code\n");
416 void fontcallback1(U16 id,U8 * name)
420 void fontcallback2(U16 id,U8 * name)
422 swf_FontExtract(&swf,id,&fonts[fontnum]);
426 void hexdumpTag(TAG*tag, char* prefix)
429 printf(" %s-=> ",prefix);
430 for(t=0;t<tag->len;t++) {
431 printf("%02x ", tag->data[t]);
432 if((t && ((t&15)==15)) || (t==tag->len-1))
437 printf("\n %s-=> ",prefix);
442 void dumperror(const char* format, ...)
447 va_start(arglist, format);
448 vsprintf(buf, format, arglist);
452 printf("==== Error: %s ====\n", buf);
455 static char strbuf[800];
458 char* timestring(double f)
460 int hours = (int)(f/3600);
461 int minutes = (int)((f-hours*3600)/60);
462 int seconds = (int)((f-hours*3600-minutes*60));
463 int useconds = (int)((f-(int)f)*1000+0.5);
466 sprintf(&strbuf[bufpos], "%02d:%02d:%02d,%03d",hours,minutes,seconds,useconds);
467 return &strbuf[bufpos];
470 int main (int argc,char ** argv)
478 char issprite = 0; // are we inside a sprite definition?
481 char* spriteframelabel = 0;
482 char* framelabel = 0;
486 memset(idtab,0,65536);
488 processargs(argc, argv);
492 fprintf(stderr, "You must supply a filename.\n");
496 f = open(filename,O_RDONLY);
500 perror("Couldn't open file: ");
503 if FAILED(swf_ReadSWF(f,&swf))
505 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
512 if(statbuf.st_size != swf.fileSize && !swf.compressed)
513 dumperror("Real Filesize (%d) doesn't match header Filesize (%d)",
514 statbuf.st_size, swf.fileSize);
515 filesize = statbuf.st_size;
520 xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
521 ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
525 printf("-X %d", xsize);
531 printf("-Y %d", ysize);
537 printf("-r %d", swf.frameRate*100/256);
543 printf("-f %d", swf.frameCount);
550 char*fileversions[] = {"","1,0,0,0", "2,0,0,0","3,0,0,0","4,0,0,0",
551 "5,0,0,0","6,0,23,0","7,0,0,0","8,0,0,0"};
552 if(swf.fileVersion>8) {
553 fprintf(stderr, "Fileversion>8\n");
556 printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
558 //" BGCOLOR=#ffffffff\n"?
560 //http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,23,0?
561 " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%s\">\n"
562 " <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
563 " <PARAM NAME=\"PLAY\" VALUE=\"true\">\n"
564 " <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
565 " <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
566 " <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n" //bgcolor=#ffffff?
567 " PLAY=\"true\" ALIGN=\"\" LOOP=\"true\" QUALITY=\"high\"\n"
568 " TYPE=\"application/x-shockwave-flash\"\n"
569 " PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\">\n"
571 "</OBJECT>\n", xsize, ysize, fileversions[swf.fileVersion],
572 filename, filename, xsize, ysize);
575 printf("[HEADER] File version: %d\n", swf.fileVersion);
577 printf("[HEADER] File is zlib compressed.");
578 if(filesize && swf.fileSize)
579 printf(" Ratio: %02d%%\n", filesize*100/(swf.fileSize));
583 printf("[HEADER] File size: %ld%s\n", swf.fileSize, swf.compressed?" (Depacked)":"");
584 printf("[HEADER] Frame rate: %f\n",swf.frameRate/256.0);
585 printf("[HEADER] Frame count: %d\n",swf.frameCount);
586 printf("[HEADER] Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
587 printf("[HEADER] Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
593 swf_FontEnumerate(&swf,&fontcallback1);
594 fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
596 swf_FontEnumerate(&swf,&fontcallback2);
600 char*name = swf_TagGetName(tag);
603 dumperror("Unknown tag:0x%03x", tag->id);
607 printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
609 if(tag->id == ST_FREECHARACTER) {
610 U16 id = swf_GetU16(tag);
614 if(swf_isDefiningTag(tag)) {
615 U16 id = swf_GetDefineID(tag);
616 printf(" defines id %04d", id);
618 dumperror("Id %04d is defined more than once.", id);
621 else if(swf_isPseudoDefiningTag(tag)) {
622 U16 id = swf_GetDefineID(tag);
623 printf(" adds information to id %04d", id);
625 dumperror("Id %04d is not yet defined.\n", id);
627 else if(tag->id == ST_PLACEOBJECT) {
628 printf(" places id %04d at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
630 printf(" name \"%s\"",swf_GetName(tag));
632 else if(tag->id == ST_PLACEOBJECT2) {
639 printf(" id %04d",swf_GetPlaceID(tag));
643 printf(" at depth %04d", swf_GetDepth(tag));
645 printf(" name \"%s\"",swf_GetName(tag));
647 else if(tag->id == ST_REMOVEOBJECT) {
648 printf(" removes id %04d from depth %04d", swf_GetPlaceID(tag), swf_GetDepth(tag));
650 else if(tag->id == ST_REMOVEOBJECT2) {
651 printf(" removes object from depth %04d", swf_GetDepth(tag));
653 else if(tag->id == ST_FREECHARACTER) {
654 printf(" frees object %04d", swf_GetPlaceID(tag));
656 else if(tag->id == ST_STARTSOUND) {
659 id = swf_GetU16(tag);
660 flags = swf_GetU8(tag);
662 printf(" stops sound with id %04d", id);
664 printf(" starts sound with id %04d", id);
666 printf(" (if not already playing)");
672 printf(" looping %d times", swf_GetU16(tag));
675 else if(tag->id == ST_FRAMELABEL) {
676 int l = strlen(tag->data);
677 printf(" \"%s\"", tag->data);
679 printf(" has %d extra bytes", tag->len-1-l);
680 if(tag ->len-1-l == 1 && tag->data[tag->len-1] == 1)
683 if((framelabel && !issprite) ||
684 (spriteframelabel && issprite)) {
685 dumperror("Frame %d has more than one label",
686 issprite?spriteframe:mainframe);
688 if(issprite) spriteframelabel = tag->data;
689 else framelabel = tag->data;
691 else if(tag->id == ST_SHOWFRAME) {
692 char*label = issprite?spriteframelabel:framelabel;
693 int frame = issprite?spriteframe:mainframe;
696 while(tag->next && tag->next->id == ST_SHOWFRAME && tag->next->len == 0) {
698 if(issprite) spriteframe++;
704 printf(" %d (%s)", frame, timestring(frame*(256.0/(swf.frameRate+0.1))));
706 printf(" %d-%d (%s-%s)", frame, nframe,
707 timestring(frame*(256.0/(swf.frameRate+0.1))),
708 timestring(nframe*(256.0/(swf.frameRate+0.1)))
711 printf(" (label \"%s\")", label);
712 if(issprite) {spriteframe++; spriteframelabel = 0;}
713 if(!issprite) {mainframe++; framelabel = 0;}
716 if(tag->id == ST_SETBACKGROUNDCOLOR) {
717 U8 r = swf_GetU8(tag);
718 U8 g = swf_GetU8(tag);
719 U8 b = swf_GetU8(tag);
720 printf(" (%02x/%02x/%02x)",r,g,b);
723 if(tag->id == ST_DEFINEBITSLOSSLESS ||
724 tag->id == ST_DEFINEBITSLOSSLESS2) {
725 handleDefineBits(tag);
728 else if(tag->id == ST_DEFINESOUND) {
729 handleDefineSound(tag);
732 else if(tag->id == ST_DEFINEEDITTEXT) {
736 else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
742 else if(tag->id == ST_PLACEOBJECT2) {
748 sprintf(myprefix, " %s", prefix);
750 if(tag->id == ST_DEFINESPRITE) {
751 sprintf(prefix, " ");
753 dumperror("Sprite definition inside a sprite definition");
757 spriteframelabel = 0;
759 else if(tag->id == ST_END) {
762 spriteframelabel = 0;
764 dumperror("End Tag not empty");
766 else if(tag->id == ST_DOACTION && action) {
768 actions = swf_ActionGet(tag);
769 swf_DumpActions(actions, myprefix);
771 else if(tag->id == ST_DEFINEBUTTON && action) {
772 dumpButtonActions(tag, myprefix);
774 else if(tag->id == ST_DEFINEBUTTON2 && action) {
775 dumpButton2Actions(tag, myprefix);
777 else if(tag->id == ST_PLACEOBJECT2) {
778 if((*(U8*)tag->data)&0x80)
779 handlePlaceObject2(tag, myprefix);
784 if(tag->len && used) {
785 int num = swf_GetNumUsedIDs(tag);
789 used = (int*)malloc(sizeof(int)*num);
790 swf_GetUsedIDs(tag, used);
791 printf("%s%suses IDs: ", indent, prefix);
793 swf_SetTagPos(tag, used[t]);
794 printf("%d%s", swf_GetU16(tag), t<num-1?", ":"");
800 if(tag->len && hex) {
801 hexdumpTag(tag, prefix);