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);
111 int args_callback_longoption(char*name,char*val)
113 return args_long2shortoption(options, name, val);
115 void args_callback_usage(char*name)
117 printf("Usage: %s [-at] file.swf\n", name);
118 printf("\t-h , --help\t\t Print help and exit\n");
119 printf("\t-e , --html\t\t Create a html embedding the file (simple, but useful)\n");
120 printf("\t-X , --width\t\t Prints out a string of the form \"-X width\"\n");
121 printf("\t-Y , --height\t\t Prints out a string of the form \"-Y height\"\n");
122 printf("\t-r , --rate\t\t Prints out a string of the form \"-r rate\"\n");
123 printf("\t-f , --frames\t\t Prints out a string of the form \"-f framenum\"\n");
124 printf("\t-a , --action\t\t Disassemble action tags\n");
125 printf("\t-t , --text\t\t Show text data\n");
126 printf("\t-d , --hex\t\t Print hex output of tag data, too\n");
127 printf("\t-u , --used\t\t Show referred IDs for each Tag\n");
128 printf("\t-V , --version\t\t Print program version and exit\n");
130 int args_callback_command(char*name,char*val)
133 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
141 char* testfunc(char*str)
143 printf("%s: %s\n", what, str);
147 void dumpButton2Actions(TAG*tag, char*prefix)
153 oldTagPos = swf_GetTagPos(tag);
155 // scan DefineButton2 Record
157 swf_GetU16(tag); // Character ID
158 swf_GetU8(tag); // Flags;
160 offsetpos = swf_GetTagPos(tag); // first offset
163 while (swf_GetU8(tag)) // state -> parse ButtonRecord
164 { swf_GetU16(tag); // id
165 swf_GetU16(tag); // layer
166 swf_GetMatrix(tag,NULL); // matrix
167 swf_GetCXForm(tag,NULL,1); // cxform
174 if(tag->pos >= tag->len)
177 offsetpos = swf_GetU16(tag);
178 condition = swf_GetU16(tag); // condition
180 actions = swf_ActionGet(tag);
181 printf("%s condition %04x\n", prefix, condition);
182 swf_DumpActions(actions, prefix);
185 swf_SetTagPos(tag,oldTagPos);
189 void dumpButtonActions(TAG*tag, char*prefix)
192 swf_GetU16(tag); // id
193 while (swf_GetU8(tag)) // state -> parse ButtonRecord
194 { swf_GetU16(tag); // id
195 swf_GetU16(tag); // layer
196 swf_GetMatrix(tag,NULL); // matrix
198 actions = swf_ActionGet(tag);
199 swf_DumpActions(actions, prefix);
202 #define ET_HASTEXT 32768
203 #define ET_WORDWRAP 16384
204 #define ET_MULTILINE 8192
205 #define ET_PASSWORD 4096
206 #define ET_READONLY 2048
207 #define ET_HASTEXTCOLOR 1024
208 #define ET_HASMAXLENGTH 512
209 #define ET_HASFONT 256
212 #define ET_HASLAYOUT 32
213 #define ET_NOSELECT 16
217 #define ET_USEOUTLINES 1
223 void textcallback(int*glyphs, int nr, int fontid)
226 printf(" <%2d glyphs in font %2d> ",nr, fontid);
227 for(t=0;t<fontnum;t++)
229 if(fonts[t]->id == fontid) {
239 if(glyphs[t] >= fonts[font]->numchars)
241 a = fonts[font]->glyph2ascii[glyphs[t]];
248 printf("\\x%x", (int)a);
253 void handleText(TAG*tag)
256 swf_FontExtract_DefineTextCallback(-1,0,tag,4, textcallback);
259 void handleDefineSound(TAG*tag)
261 U16 id = swf_GetU16(tag);
262 U8 flags = swf_GetU8(tag);
263 int compression = (flags>>4)&3;
264 int rate = (flags>>2)&3;
265 int bits = flags&2?16:8;
266 int stereo = flags&1;
268 if(compression == 0) printf("Raw ");
269 else if(compression == 1) printf("ADPCM ");
270 else if(compression == 2) printf("MP3 ");
272 if(rate == 0) printf("5.5Khz ");
273 if(rate == 1) printf("11Khz ");
274 if(rate == 2) printf("22Khz ");
275 if(rate == 3) printf("44Khz ");
276 printf("%dBit ", bits);
277 if(stereo) printf("stereo");
282 void handleDefineBits(TAG*tag)
288 id = swf_GetU16(tag);
289 mode = swf_GetU8(tag);
290 width = swf_GetU16(tag);
291 height = swf_GetU16(tag);
292 printf(" image %dx%d",width,height);
293 if(mode == 3) printf(" (8 bpp)");
294 else if(mode == 4) printf(" (16 bpp)");
295 else if(mode == 5) printf(" (32 bpp)");
296 else printf(" (? bpp)");
299 void handleEditText(TAG*tag)
304 id = swf_GetU16(tag);
306 //swf_ResetReadBits(tag);
311 flags = swf_GetBits(tag,16);
312 if(flags & ET_HASFONT) {
313 swf_GetU16(tag); //font
314 swf_GetU16(tag); //fontheight
316 if(flags & ET_HASTEXTCOLOR) {
317 swf_GetU8(tag); //rgba
322 if(flags & ET_HASMAXLENGTH) {
323 swf_GetU16(tag); //maxlength
325 if(flags & ET_HASLAYOUT) {
326 swf_GetU8(tag); //align
327 swf_GetU16(tag); //left margin
328 swf_GetU16(tag); //right margin
329 swf_GetU16(tag); //indent
330 swf_GetU16(tag); //leading
332 printf(" variable \"%s\"", &tag->data[tag->pos]);
334 if(flags & (ET_X1 | ET_X2 | ET_X3 | ET_X0))
336 printf(" undefined flags: %d%d%d%d",
343 while(tag->data[tag->pos++]);
344 if(flags & ET_HASTEXT)
345 // printf(" text \"%s\"\n", &tag->data[tag->pos])
348 void printhandlerflags(U16 handlerflags)
350 if(handlerflags&1) printf("[on load]");
351 if(handlerflags&2) printf("[enter frame]");
352 if(handlerflags&4) printf("[unload]");
353 if(handlerflags&8) printf("[mouse move]");
354 if(handlerflags&16) printf("[mouse down]");
355 if(handlerflags&32) printf("[mouse up]");
356 if(handlerflags&64) printf("[key down]");
357 if(handlerflags&128) printf("[key up]");
358 if(handlerflags&256) printf("[data]");
359 if(handlerflags&0xfe00) printf("[???]");
361 void handlePlaceObject2(TAG*tag, char*prefix)
363 U8 flags = swf_GetU8(tag);
364 swf_GetU16(tag); //depth
366 if(flags&2) swf_GetU16(tag); //id
367 if(flags&4) swf_GetMatrix(tag,0);
368 if(flags&8) swf_GetCXForm(tag,0,1);
369 if(flags&16) swf_GetU16(tag); //ratio
370 if(flags&32) { while(swf_GetU8(tag)); }
371 if(flags&64) swf_GetU16(tag); //clip
379 unknown = swf_GetU16(tag);
380 globalflags = swf_GetU16(tag);
382 printf("Unknown parameter field not zero: %04x\n", unknown);
385 printf("global flags: %04x\n", globalflags);
386 handlerflags = swf_GetU16(tag);
388 handlerflags = swf_GetU32(tag);
391 while(handlerflags) {
396 globalflags &= ~handlerflags;
397 printf("%s flags %08x ",prefix, handlerflags);
398 printhandlerflags(handlerflags);
399 length = swf_GetU32(tag);
400 printf(", %d bytes actioncode\n",length);
401 a = swf_ActionGet(tag);
402 swf_DumpActions(a,prefix);
405 handlerflags = is32?swf_GetU32(tag):swf_GetU16(tag);
407 if(globalflags) // should go to sterr.
408 printf("ERROR: unsatisfied handlerflags: %02x\n", globalflags);
410 printf(" has action code\n");
415 void fontcallback1(U16 id,U8 * name)
419 void fontcallback2(U16 id,U8 * name)
421 swf_FontExtract(&swf,id,&fonts[fontnum]);
425 void hexdumpTag(TAG*tag, char* prefix)
428 printf(" %s-=> ",prefix);
429 for(t=0;t<tag->len;t++) {
430 printf("%02x ", tag->data[t]);
431 if((t && ((t&15)==15)) || (t==tag->len-1))
436 printf("\n %s-=> ",prefix);
441 void dumperror(const char* format, ...)
446 va_start(arglist, format);
447 vsprintf(buf, format, arglist);
451 printf("==== Error: %s ====\n", buf);
454 static char strbuf[800];
457 char* timestring(double f)
459 int hours = (int)(f/3600);
460 int minutes = (int)((f-hours*3600)/60);
461 int seconds = (int)((f-hours*3600-minutes*60));
462 int useconds = (int)((f-(int)f)*1000+0.5);
465 sprintf(&strbuf[bufpos], "%02d:%02d:%02d,%03d",hours,minutes,seconds,useconds);
466 return &strbuf[bufpos];
469 int main (int argc,char ** argv)
477 char issprite = 0; // are we inside a sprite definition?
480 char* spriteframelabel = 0;
481 char* framelabel = 0;
485 memset(idtab,0,65536);
487 processargs(argc, argv);
491 fprintf(stderr, "You must supply a filename.\n");
495 f = open(filename,O_RDONLY);
499 perror("Couldn't open file: ");
502 if FAILED(swf_ReadSWF(f,&swf))
504 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
511 if(statbuf.st_size != swf.fileSize && !swf.compressed)
512 dumperror("Real Filesize (%d) doesn't match header Filesize (%d)",
513 statbuf.st_size, swf.fileSize);
514 filesize = statbuf.st_size;
519 xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
520 ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
524 printf("-X %d", xsize);
530 printf("-Y %d", ysize);
536 printf("-r %d", swf.frameRate*100/256);
542 printf("-f %d", swf.frameCount);
549 char*fileversions[] = {"","1,0,0,0", "2,0,0,0","3,0,0,0","4,0,0,0",
550 "5,0,0,0","6,0,23,0","7,0,0,0","8,0,0,0"};
551 if(swf.fileVersion>8) {
552 fprintf(stderr, "Fileversion>8\n");
555 printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
557 //" BGCOLOR=#ffffffff\n"?
559 //http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,23,0?
560 " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%s\">\n"
561 " <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
562 " <PARAM NAME=\"PLAY\" VALUE=\"true\">\n"
563 " <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
564 " <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
565 " <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n" //bgcolor=#ffffff?
566 " PLAY=\"true\" ALIGN=\"\" LOOP=\"true\" QUALITY=\"high\"\n"
567 " TYPE=\"application/x-shockwave-flash\"\n"
568 " PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\">\n"
570 "</OBJECT>\n", xsize, ysize, fileversions[swf.fileVersion],
571 filename, filename, xsize, ysize);
574 printf("[HEADER] File version: %d\n", swf.fileVersion);
576 printf("[HEADER] File is zlib compressed.");
577 if(filesize && swf.fileSize)
578 printf(" Ratio: %02d%%\n", filesize*100/(swf.fileSize));
582 printf("[HEADER] File size: %ld%s\n", swf.fileSize, swf.compressed?" (Depacked)":"");
583 printf("[HEADER] Frame rate: %f\n",swf.frameRate/256.0);
584 printf("[HEADER] Frame count: %d\n",swf.frameCount);
585 printf("[HEADER] Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
586 printf("[HEADER] Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
592 swf_FontEnumerate(&swf,&fontcallback1);
593 fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
595 swf_FontEnumerate(&swf,&fontcallback2);
599 char*name = swf_TagGetName(tag);
602 dumperror("Unknown tag:0x%03x", tag->id);
606 printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
608 if(tag->id == ST_FREECHARACTER) {
609 U16 id = swf_GetU16(tag);
613 if(swf_isDefiningTag(tag)) {
614 U16 id = swf_GetDefineID(tag);
615 printf(" defines id %04d", id);
617 dumperror("Id %04d is defined more than once.", id);
620 else if(swf_isPseudoDefiningTag(tag)) {
621 U16 id = swf_GetDefineID(tag);
622 printf(" adds information to id %04d", id);
624 dumperror("Id %04d is not yet defined.\n", id);
626 else if(tag->id == ST_PLACEOBJECT) {
627 printf(" places id %04d at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
629 printf(" name \"%s\"",swf_GetName(tag));
631 else if(tag->id == ST_PLACEOBJECT2) {
638 printf(" id %04d",swf_GetPlaceID(tag));
642 printf(" at depth %04d", swf_GetDepth(tag));
644 printf(" name \"%s\"",swf_GetName(tag));
646 else if(tag->id == ST_REMOVEOBJECT) {
647 printf(" removes id %04d from depth %04d", swf_GetPlaceID(tag), swf_GetDepth(tag));
649 else if(tag->id == ST_REMOVEOBJECT2) {
650 printf(" removes object from depth %04d", swf_GetDepth(tag));
652 else if(tag->id == ST_FREECHARACTER) {
653 printf(" frees object %04d", swf_GetPlaceID(tag));
655 else if(tag->id == ST_STARTSOUND) {
658 id = swf_GetU16(tag);
659 flags = swf_GetU8(tag);
661 printf(" stops sound with id %04d", id);
663 printf(" starts sound with id %04d", id);
665 printf(" (if not already playing)");
671 printf(" looping %d times", swf_GetU16(tag));
674 else if(tag->id == ST_FRAMELABEL) {
675 int l = strlen(tag->data);
676 printf(" \"%s\"", tag->data);
678 printf(" has %d extra bytes", tag->len-1-l);
679 if(tag ->len-1-l == 1 && tag->data[tag->len-1] == 1)
682 if((framelabel && !issprite) ||
683 (spriteframelabel && issprite)) {
684 dumperror("Frame %d has more than one label",
685 issprite?spriteframe:mainframe);
687 if(issprite) spriteframelabel = tag->data;
688 else framelabel = tag->data;
690 else if(tag->id == ST_SHOWFRAME) {
691 char*label = issprite?spriteframelabel:framelabel;
692 int frame = issprite?spriteframe:mainframe;
695 while(tag->next && tag->next->id == ST_SHOWFRAME && tag->next->len == 0) {
697 if(issprite) spriteframe++;
703 printf(" %d (%s)", frame, timestring(frame*(256.0/(swf.frameRate+0.1))));
705 printf(" %d-%d (%s-%s)", frame, nframe,
706 timestring(frame*(256.0/(swf.frameRate+0.1))),
707 timestring(nframe*(256.0/(swf.frameRate+0.1)))
710 printf(" (label \"%s\")", label);
711 if(issprite) {spriteframe++; spriteframelabel = 0;}
712 if(!issprite) {mainframe++; framelabel = 0;}
715 if(tag->id == ST_SETBACKGROUNDCOLOR) {
716 U8 r = swf_GetU8(tag);
717 U8 g = swf_GetU8(tag);
718 U8 b = swf_GetU8(tag);
719 printf(" (%02x/%02x/%02x)",r,g,b);
722 if(tag->id == ST_DEFINEBITSLOSSLESS ||
723 tag->id == ST_DEFINEBITSLOSSLESS2) {
724 handleDefineBits(tag);
727 else if(tag->id == ST_DEFINESOUND) {
728 handleDefineSound(tag);
731 else if(tag->id == ST_DEFINEEDITTEXT) {
735 else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
741 else if(tag->id == ST_PLACEOBJECT2) {
747 sprintf(myprefix, " %s", prefix);
749 if(tag->id == ST_DEFINESPRITE) {
750 sprintf(prefix, " ");
752 dumperror("Sprite definition inside a sprite definition");
756 spriteframelabel = 0;
758 else if(tag->id == ST_END) {
761 spriteframelabel = 0;
763 dumperror("End Tag not empty");
765 else if(tag->id == ST_DOACTION && action) {
767 actions = swf_ActionGet(tag);
768 swf_DumpActions(actions, myprefix);
770 else if(tag->id == ST_DEFINEBUTTON && action) {
771 dumpButtonActions(tag, myprefix);
773 else if(tag->id == ST_DEFINEBUTTON2 && action) {
774 dumpButton2Actions(tag, myprefix);
776 else if(tag->id == ST_PLACEOBJECT2) {
777 if((*(U8*)tag->data)&0x80)
778 handlePlaceObject2(tag, myprefix);
783 if(tag->len && used) {
784 int num = swf_GetNumUsedIDs(tag);
788 used = (int*)malloc(sizeof(int)*num);
789 swf_GetUsedIDs(tag, used);
790 printf("%s%suses IDs: ", indent, prefix);
792 swf_SetTagPos(tag, used[t]);
793 printf("%d%s", swf_GetU16(tag), t<num-1?", ":"");
799 if(tag->len && hex) {
800 hexdumpTag(tag, prefix);