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 int main (int argc,char ** argv)
462 char issprite = 0; // are we inside a sprite definition?
465 char* spriteframelabel = 0;
466 char* framelabel = 0;
470 memset(idtab,0,65536);
472 processargs(argc, argv);
476 fprintf(stderr, "You must supply a filename.\n");
480 f = open(filename,O_RDONLY);
484 perror("Couldn't open file: ");
487 if FAILED(swf_ReadSWF(f,&swf))
489 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
496 if(statbuf.st_size != swf.fileSize && !swf.compressed)
497 dumperror("Real Filesize (%d) doesn't match header Filesize (%d)",
498 statbuf.st_size, swf.fileSize);
499 filesize = statbuf.st_size;
504 xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
505 ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
509 printf("-X %d", xsize);
515 printf("-Y %d", ysize);
521 printf("-r %d", swf.frameRate*100/256);
527 printf("-f %d", swf.frameCount);
534 char*fileversions[] = {"","1,0,0,0", "2,0,0,0","3,0,0,0","4,0,0,0",
535 "5,0,0,0","6,0,23,0","7,0,0,0","8,0,0,0"};
536 if(swf.fileVersion>8) {
537 fprintf(stderr, "Fileversion>8\n");
540 printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
542 //" BGCOLOR=#ffffffff\n"?
544 //http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,23,0?
545 " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%s\">\n"
546 " <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
547 " <PARAM NAME=\"PLAY\" VALUE=\"true\">\n"
548 " <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
549 " <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
550 " <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n" //bgcolor=#ffffff?
551 " PLAY=\"true\" ALIGN=\"\" LOOP=\"true\" QUALITY=\"high\"\n"
552 " TYPE=\"application/x-shockwave-flash\"\n"
553 " PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\">\n"
555 "</OBJECT>\n", xsize, ysize, fileversions[swf.fileVersion],
556 filename, filename, xsize, ysize);
559 printf("[HEADER] File version: %d\n", swf.fileVersion);
561 printf("[HEADER] File is zlib compressed.");
562 if(filesize && swf.fileSize)
563 printf(" Ratio: %02d%%\n", filesize*100/(swf.fileSize));
567 printf("[HEADER] File size: %ld%s\n", swf.fileSize, swf.compressed?" (Depacked)":"");
568 printf("[HEADER] Frame rate: %f\n",swf.frameRate/256.0);
569 printf("[HEADER] Frame count: %d\n",swf.frameCount);
570 printf("[HEADER] Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
571 printf("[HEADER] Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
577 swf_FontEnumerate(&swf,&fontcallback1);
578 fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
580 swf_FontEnumerate(&swf,&fontcallback2);
584 char*name = swf_TagGetName(tag);
587 dumperror("Unknown tag:0x%03x", tag->id);
591 printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
593 if(tag->id == ST_FREECHARACTER) {
594 U16 id = swf_GetU16(tag);
598 if(swf_isDefiningTag(tag)) {
599 U16 id = swf_GetDefineID(tag);
600 printf(" defines id %04d", id);
602 dumperror("Id %04d is defined more than once.", id);
605 else if(swf_isPseudoDefiningTag(tag)) {
606 U16 id = swf_GetDefineID(tag);
607 printf(" adds information to id %04d", id);
609 dumperror("Id %04d is not yet defined.\n", id);
611 else if(tag->id == ST_PLACEOBJECT) {
612 printf(" places id %04d at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
614 printf(" name \"%s\"",swf_GetName(tag));
616 else if(tag->id == ST_PLACEOBJECT2) {
623 printf(" id %04d",swf_GetPlaceID(tag));
627 printf(" at depth %04d", swf_GetDepth(tag));
629 printf(" name \"%s\"",swf_GetName(tag));
631 else if(tag->id == ST_REMOVEOBJECT) {
632 printf(" removes id %04d from depth %04d", swf_GetPlaceID(tag), swf_GetDepth(tag));
634 else if(tag->id == ST_REMOVEOBJECT2) {
635 printf(" removes object from depth %04d", swf_GetDepth(tag));
637 else if(tag->id == ST_FREECHARACTER) {
638 printf(" frees object %04d", swf_GetPlaceID(tag));
640 else if(tag->id == ST_STARTSOUND) {
641 printf(" starts id %04d", swf_GetPlaceID(tag));
643 else if(tag->id == ST_FRAMELABEL) {
644 int l = strlen(tag->data);
645 printf(" \"%s\"", tag->data);
647 printf(" has %d extra bytes", tag->len-1-l);
648 if(tag ->len-1-l == 1 && tag->data[tag->len-1] == 1)
651 if((framelabel && !issprite) ||
652 (spriteframelabel && issprite)) {
653 dumperror("Frame %d has more than one label",
654 issprite?spriteframe:mainframe);
656 if(issprite) spriteframelabel = tag->data;
657 else framelabel = tag->data;
659 else if(tag->id == ST_SHOWFRAME) {
660 char*label = issprite?spriteframelabel:framelabel;
661 int frame = issprite?spriteframe:mainframe;
664 while(tag->next && tag->next->id == ST_SHOWFRAME && tag->next->len == 0) {
666 if(issprite) spriteframe++;
672 printf(" %d", frame);
674 printf(" %d-%d", frame, nframe);
676 printf(" (label \"%s\")", label);
677 if(issprite) {spriteframe++; spriteframelabel = 0;}
678 if(!issprite) {mainframe++; framelabel = 0;}
681 if(tag->id == ST_SETBACKGROUNDCOLOR) {
682 U8 r = swf_GetU8(tag);
683 U8 g = swf_GetU8(tag);
684 U8 b = swf_GetU8(tag);
685 printf(" (%02x/%02x/%02x)",r,g,b);
688 if(tag->id == ST_DEFINEBITSLOSSLESS ||
689 tag->id == ST_DEFINEBITSLOSSLESS2) {
690 handleDefineBits(tag);
693 else if(tag->id == ST_DEFINESOUND) {
694 handleDefineSound(tag);
697 else if(tag->id == ST_DEFINEEDITTEXT) {
701 else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
707 else if(tag->id == ST_PLACEOBJECT2) {
713 sprintf(myprefix, " %s", prefix);
715 if(tag->id == ST_DEFINESPRITE) {
716 sprintf(prefix, " ");
718 dumperror("Sprite definition inside a sprite definition");
722 spriteframelabel = 0;
724 else if(tag->id == ST_END) {
727 spriteframelabel = 0;
729 dumperror("End Tag not empty");
731 else if(tag->id == ST_DOACTION && action) {
733 actions = swf_ActionGet(tag);
734 swf_DumpActions(actions, myprefix);
736 else if(tag->id == ST_DEFINEBUTTON && action) {
737 dumpButtonActions(tag, myprefix);
739 else if(tag->id == ST_DEFINEBUTTON2 && action) {
740 dumpButton2Actions(tag, myprefix);
742 else if(tag->id == ST_PLACEOBJECT2) {
743 if((*(U8*)tag->data)&0x80)
744 handlePlaceObject2(tag, myprefix);
749 if(tag->len && used) {
750 int num = swf_GetNumUsedIDs(tag);
754 used = (int*)malloc(sizeof(int)*num);
755 swf_GetUsedIDs(tag, used);
756 printf("%s%suses IDs: ", indent, prefix);
758 swf_SetTagPos(tag, used[t]);
759 printf("%d%s", swf_GetU16(tag), t<num-1?", ":"");
765 if(tag->len && hex) {
766 hexdumpTag(tag, prefix);