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 handleEditText(TAG*tag)
258 id = swf_GetU16(tag);
260 //swf_ResetReadBits(tag);
265 flags = swf_GetBits(tag,16);
266 if(flags & ET_HASFONT) {
267 swf_GetU16(tag); //font
268 swf_GetU16(tag); //fontheight
270 if(flags & ET_HASTEXTCOLOR) {
271 swf_GetU8(tag); //rgba
276 if(flags & ET_HASMAXLENGTH) {
277 swf_GetU16(tag); //maxlength
279 if(flags & ET_HASLAYOUT) {
280 swf_GetU8(tag); //align
281 swf_GetU16(tag); //left margin
282 swf_GetU16(tag); //right margin
283 swf_GetU16(tag); //indent
284 swf_GetU16(tag); //leading
286 printf(" variable \"%s\"", &tag->data[tag->pos]);
288 if(flags & (ET_X1 | ET_X2 | ET_X3 | ET_X0))
290 printf(" undefined flags: %d%d%d%d",
297 while(tag->data[tag->pos++]);
298 if(flags & ET_HASTEXT)
299 // printf(" text \"%s\"\n", &tag->data[tag->pos])
302 void printhandlerflags(U16 handlerflags)
304 if(handlerflags&1) printf("[on load]");
305 if(handlerflags&2) printf("[enter frame]");
306 if(handlerflags&4) printf("[unload]");
307 if(handlerflags&8) printf("[mouse move]");
308 if(handlerflags&16) printf("[mouse down]");
309 if(handlerflags&32) printf("[mouse up]");
310 if(handlerflags&64) printf("[key down]");
311 if(handlerflags&128) printf("[key up]");
312 if(handlerflags&256) printf("[data]");
313 if(handlerflags&0xfe00) printf("[???]");
315 void handlePlaceObject2(TAG*tag, char*prefix)
317 U8 flags = swf_GetU8(tag);
318 swf_GetU16(tag); //depth
320 if(flags&2) swf_GetU16(tag); //id
321 if(flags&4) swf_GetMatrix(tag,0);
322 if(flags&8) swf_GetCXForm(tag,0,0);
323 if(flags&16) swf_GetU16(tag); //ratio
325 while(swf_GetU8(tag));
327 if(flags&64) swf_GetU16(tag); //clip
333 unknown = swf_GetU16(tag);
334 globalflags = swf_GetU16(tag);
336 printf("Unknown parameter field not zero: %04x\n", unknown);
342 handlerflags = swf_GetU16(tag);
345 globalflags &= ~handlerflags;
346 printf("%s flags %04x ",prefix, handlerflags);
347 printhandlerflags(handlerflags);
349 length = swf_GetU32(tag);
350 printf(", %d bytes actioncode\n",length);
351 a = swf_ActionGet(tag);
352 swf_DumpActions(a,prefix);
355 if(globalflags) // should go to sterr.
356 printf("ERROR: unsatisfied handlerflags: %02x\n", globalflags);
358 printf(" has action code\n");
363 void fontcallback1(U16 id,U8 * name)
367 void fontcallback2(U16 id,U8 * name)
369 swf_FontExtract(&swf,id,&fonts[fontnum]);
373 void dumperror(const char* format, ...)
378 va_start(arglist, format);
379 vsprintf(buf, format, arglist);
382 printf("==== Error: %s ====\n", buf);
385 int main (int argc,char ** argv)
393 char issprite = 0; // are we inside a sprite definition?
396 char* spriteframelabel;
397 char* framelabel = 0;
400 memset(idtab,0,65536);
402 processargs(argc, argv);
406 fprintf(stderr, "You must supply a filename.\n");
410 f = open(filename,O_RDONLY);
414 perror("Couldn't open file: ");
417 if FAILED(swf_ReadSWF(f,&swf))
419 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
426 if(statbuf.st_size != swf.fileSize && !swf.compressed)
427 dumperror("Real Filesize (%d) doesn't match header Filesize (%d)",
428 statbuf.st_size, swf.fileSize);
433 xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
434 ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
438 printf("-X %d", xsize);
444 printf("-Y %d", ysize);
450 printf("-r %d", swf.frameRate*100/256);
457 printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
460 " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%d,0,0,0\">\n"
461 " <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
462 " <PARAM NAME=\"PLAY\" VALUE=\"true\">\n"
463 " <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
464 " <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
465 " <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n"
466 " PLAY=\"true\" LOOP=\"true\" QUALITY=\"high\"\n"
467 " TYPE=\"application/x-shockwave-flash\"\n"
468 " PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">\n"
470 "</OBJECT>\n", xsize, ysize, swf.fileVersion, filename, filename, xsize, ysize);
473 printf("[HEADER] File version: %d\n", swf.fileVersion);
475 printf("[HEADER] File is compressed.\n");
476 printf("[HEADER] File size: %ld\n", swf.fileSize);
477 printf("[HEADER] Frame rate: %f\n",swf.frameRate/256.0);
478 printf("[HEADER] Frame count: %d\n",swf.frameCount);
479 printf("[HEADER] Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
480 printf("[HEADER] Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
486 swf_FontEnumerate(&swf,&fontcallback1);
487 fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
489 swf_FontEnumerate(&swf,&fontcallback2);
493 char*name = swf_TagGetName(tag);
496 dumperror("Unknown tag:0x%03x", tag->id);
500 printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
502 if(tag->id == ST_FREECHARACTER) {
503 U16 id = swf_GetU16(tag);
507 if(swf_isDefiningTag(tag)) {
508 U16 id = swf_GetDefineID(tag);
509 printf(" defines id %04d", id);
511 dumperror("Id %04d is defined more than once.", id);
514 else if(swf_isPseudoDefiningTag(tag)) {
515 U16 id = swf_GetDefineID(tag);
516 printf(" adds information to id %04d", id);
518 dumperror("Id %04d is not yet defined.\n", id);
520 else if(tag->id == ST_PLACEOBJECT) {
521 printf(" places id %04d at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
523 printf(" name \"%s\"",swf_GetName(tag));
525 else if(tag->id == ST_PLACEOBJECT2) {
532 printf(" id %04d",swf_GetPlaceID(tag));
536 printf(" at depth %04d", swf_GetDepth(tag));
538 printf(" name \"%s\"",swf_GetName(tag));
540 else if(tag->id == ST_REMOVEOBJECT) {
541 printf(" removes id %04d from depth %04d", swf_GetPlaceID(tag), swf_GetDepth(tag));
543 else if(tag->id == ST_REMOVEOBJECT2) {
544 printf(" removes object from depth %04d", swf_GetDepth(tag));
546 else if(tag->id == ST_STARTSOUND) {
547 printf(" starts id %04d", swf_GetPlaceID(tag));
549 else if(tag->id == ST_FRAMELABEL) {
550 printf(" \"%s\"", tag->data);
551 if((framelabel && !issprite) ||
552 (spriteframelabel && issprite)) {
553 dumperror("Frame %d has more than one label",
554 issprite?spriteframe:mainframe);
556 if(issprite) spriteframelabel = tag->data;
557 else framelabel = tag->data;
559 else if(tag->id == ST_SHOWFRAME) {
560 char*label = issprite?spriteframelabel:framelabel;
561 int frame = issprite?spriteframe:mainframe;
564 while(tag->next && tag->next->id == ST_SHOWFRAME && tag->next->len == 0) {
566 if(issprite) spriteframe++;
572 printf(" %d", frame);
574 printf(" %d-%d", frame, nframe);
576 printf(" (label \"%s\")", label);
577 if(issprite) {spriteframe++; spriteframelabel = 0;}
578 if(!issprite) {mainframe++; framelabel = 0;}
581 if(tag->id == ST_DEFINEEDITTEXT) {
585 else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
591 else if(tag->id == ST_PLACEOBJECT2) {
597 sprintf(myprefix, " %s", prefix);
599 if(tag->id == ST_DEFINESPRITE) {
600 sprintf(prefix, " ");
602 dumperror("Sprite definition inside a sprite definition");
606 spriteframelabel = 0;
608 else if(tag->id == ST_END) {
611 spriteframelabel = 0;
613 dumperror("End Tag not empty");
615 else if(tag->id == ST_DOACTION && action) {
617 actions = swf_ActionGet(tag);
618 swf_DumpActions(actions, myprefix);
620 else if(tag->id == ST_DEFINEBUTTON && action) {
621 dumpButtonActions(tag, myprefix);
623 else if(tag->id == ST_DEFINEBUTTON2 && action) {
624 dumpButton2Actions(tag, myprefix);
626 else if(tag->id == ST_PLACEOBJECT2) {
627 if((*(U8*)tag->data)&0x80)
628 handlePlaceObject2(tag, myprefix);
633 if(tag->len && used) {
634 int num = swf_GetNumUsedIDs(tag);
638 used = (int*)malloc(sizeof(int)*num);
639 swf_GetUsedIDs(tag, used);
640 printf("%s%suses IDs: ", indent, prefix);
643 printf("%d%s", swf_GetU16(tag), t<num-1?", ":"");
649 if(tag->len && hex) {
651 printf(" %s-=> ",prefix);
652 for(t=0;t<tag->len;t++) {
653 printf("%02x ", tag->data[t]);
654 if((t && ((t&15)==15)) || (t==tag->len-1))
659 printf("\n %s-=> ",prefix);