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
44 struct options_t options[] =
59 int args_callback_option(char*name,char*val)
61 if(!strcmp(name, "V")) {
62 printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
65 else if(name[0]=='a') {
69 else if(name[0]=='t') {
73 else if(name[0]=='e') {
77 else if(name[0]=='X') {
81 else if(name[0]=='Y') {
85 else if(name[0]=='r') {
89 else if(name[0]=='d') {
94 printf("Unknown option: -%s\n", name);
99 int args_callback_longoption(char*name,char*val)
101 return args_long2shortoption(options, name, val);
103 void args_callback_usage(char*name)
105 printf("Usage: %s [-at] file.swf\n", name);
106 printf("\t-h , --help\t\t Print help and exit\n");
107 printf("\t-e , --html\t\t Create a html embedding the file (simple, but useful)\n");
108 printf("\t-X , --width\t\t Prints out a string of the form \"-X width\"\n");
109 printf("\t-Y , --height\t\t Prints out a string of the form \"-Y height\"\n");
110 printf("\t-r , --rate\t\t Prints out a string of the form \"-r rate\"\n");
111 printf("\t-a , --action\t\t Disassemble action tags\n");
112 printf("\t-t , --text\t\t Show text data\n");
113 printf("\t-d , --hex\t\t Print hex output of tag data, too\n");
114 printf("\t-V , --version\t\t Print program version and exit\n");
116 int args_callback_command(char*name,char*val)
119 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
127 char* testfunc(char*str)
129 printf("%s: %s\n", what, str);
133 void dumpButton2Actions(TAG*tag, char*prefix)
139 oldTagPos = swf_GetTagPos(tag);
141 // scan DefineButton2 Record
143 swf_GetU16(tag); // Character ID
144 swf_GetU8(tag); // Flags;
146 offsetpos = swf_GetTagPos(tag); // first offset
149 while (swf_GetU8(tag)) // state -> parse ButtonRecord
150 { swf_GetU16(tag); // id
151 swf_GetU16(tag); // layer
152 swf_GetMatrix(tag,NULL); // matrix
153 swf_GetCXForm(tag,NULL,1); // cxform
160 if(tag->pos >= tag->len)
163 offsetpos = swf_GetU16(tag);
164 condition = swf_GetU16(tag); // condition
166 actions = swf_ActionGet(tag);
167 printf("%s condition %04x\n", prefix, condition);
168 swf_DumpActions(actions, prefix);
171 swf_SetTagPos(tag,oldTagPos);
175 void dumpButtonActions(TAG*tag, char*prefix)
178 swf_GetU16(tag); // id
179 while (swf_GetU8(tag)) // state -> parse ButtonRecord
180 { swf_GetU16(tag); // id
181 swf_GetU16(tag); // layer
182 swf_GetMatrix(tag,NULL); // matrix
184 actions = swf_ActionGet(tag);
185 swf_DumpActions(actions, prefix);
188 #define ET_HASTEXT 32768
189 #define ET_WORDWRAP 16384
190 #define ET_MULTILINE 8192
191 #define ET_PASSWORD 4096
192 #define ET_READONLY 2048
193 #define ET_HASTEXTCOLOR 1024
194 #define ET_HASMAXLENGTH 512
195 #define ET_HASFONT 256
198 #define ET_HASLAYOUT 32
199 #define ET_NOSELECT 16
203 #define ET_USEOUTLINES 1
209 void textcallback(int*glyphs, int nr, int fontid)
212 printf(" <%2d glyphs in font %2d> ",nr, fontid);
213 for(t=0;t<fontnum;t++)
215 if(fonts[t]->id == fontid) {
225 if(glyphs[t] >= fonts[font]->numchars)
227 a = fonts[font]->glyph2ascii[glyphs[t]];
234 printf("\\x%x", (int)a);
239 void handleText(TAG*tag)
242 swf_FontExtract_DefineTextCallback(-1,0,tag,4, textcallback);
245 void handleEditText(TAG*tag)
250 id = swf_GetU16(tag);
252 //swf_ResetReadBits(tag);
257 flags = swf_GetBits(tag,16);
258 if(flags & ET_HASFONT) {
259 swf_GetU16(tag); //font
260 swf_GetU16(tag); //fontheight
262 if(flags & ET_HASTEXTCOLOR) {
263 swf_GetU8(tag); //rgba
268 if(flags & ET_HASMAXLENGTH) {
269 swf_GetU16(tag); //maxlength
271 if(flags & ET_HASLAYOUT) {
272 swf_GetU8(tag); //align
273 swf_GetU16(tag); //left margin
274 swf_GetU16(tag); //right margin
275 swf_GetU16(tag); //indent
276 swf_GetU16(tag); //leading
278 printf(" variable \"%s\"", &tag->data[tag->pos]);
280 if(flags & (ET_X1 | ET_X2 | ET_X3 | ET_X0))
282 printf(" undefined flags: %d%d%d%d",
289 while(tag->data[tag->pos++]);
290 if(flags & ET_HASTEXT)
291 // printf(" text \"%s\"\n", &tag->data[tag->pos])
294 void printhandlerflags(U16 handlerflags)
296 if(handlerflags&1) printf("[on load]");
297 if(handlerflags&2) printf("[enter frame]");
298 if(handlerflags&4) printf("[unload]");
299 if(handlerflags&8) printf("[mouse move]");
300 if(handlerflags&16) printf("[mouse down]");
301 if(handlerflags&32) printf("[mouse up]");
302 if(handlerflags&64) printf("[key down]");
303 if(handlerflags&128) printf("[key up]");
304 if(handlerflags&256) printf("[data]");
305 if(handlerflags&0xfe00) printf("[???]");
307 void handlePlaceObject2(TAG*tag, char*prefix)
309 U8 flags = swf_GetU8(tag);
310 swf_GetU16(tag); //depth
312 if(flags&2) swf_GetU16(tag); //id
313 if(flags&4) swf_GetMatrix(tag,0);
314 if(flags&8) swf_GetCXForm(tag,0,0);
315 if(flags&16) swf_GetU16(tag); //ratio
317 while(swf_GetU8(tag));
319 if(flags&64) swf_GetU16(tag); //clip
325 unknown = swf_GetU16(tag);
326 globalflags = swf_GetU16(tag);
328 printf("Unknown parameter field not zero: %04x\n", unknown);
334 handlerflags = swf_GetU16(tag);
337 globalflags &= ~handlerflags;
338 printf("%s flags %04x ",prefix, handlerflags);
339 printhandlerflags(handlerflags);
341 length = swf_GetU32(tag);
342 printf(", %d bytes actioncode\n",length);
343 a = swf_ActionGet(tag);
344 swf_DumpActions(a,prefix);
347 if(globalflags) // should go to sterr.
348 printf("ERROR: unsatisfied handlerflags: %02x\n", globalflags);
350 printf(" has action code\n");
355 void fontcallback1(U16 id,U8 * name)
359 void fontcallback2(U16 id,U8 * name)
361 swf_FontExtract(&swf,id,&fonts[fontnum]);
365 void dumperror(const char* format, ...)
370 va_start(arglist, format);
371 vsprintf(buf, format, arglist);
374 printf("==== Error: %s ====\n", buf);
377 int main (int argc,char ** argv)
385 char issprite = 0; // are we inside a sprite definition?
388 char* spriteframelabel;
389 char* framelabel = 0;
392 memset(idtab,0,65536);
394 processargs(argc, argv);
398 fprintf(stderr, "You must supply a filename.\n");
402 f = open(filename,O_RDONLY);
406 perror("Couldn't open file: ");
409 if FAILED(swf_ReadSWF(f,&swf))
411 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
418 if(statbuf.st_size != swf.fileSize)
419 dumperror("Real Filesize (%d) doesn't match header Filesize (%d)",
420 statbuf.st_size, swf.fileSize);
425 xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
426 ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
430 printf("-X %d", xsize);
436 printf("-Y %d", ysize);
442 printf("-r %d", swf.frameRate*100/256);
449 printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
452 " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%d,0,0,0\">\n"
453 " <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
454 " <PARAM NAME=\"PLAY\" VALUE=\"true\">\n"
455 " <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
456 " <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
457 " <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n"
458 " PLAY=\"true\" LOOP=\"true\" QUALITY=\"high\"\n"
459 " TYPE=\"application/x-shockwave-flash\"\n"
460 " PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">\n"
462 "</OBJECT>\n", xsize, ysize, swf.fileVersion, filename, filename, xsize, ysize);
465 printf("[HEADER] File version: %d\n", swf.fileVersion);
466 printf("[HEADER] File size: %ld\n", swf.fileSize);
467 printf("[HEADER] Frame rate: %f\n",swf.frameRate/256.0);
468 printf("[HEADER] Frame count: %d\n",swf.frameCount);
469 printf("[HEADER] Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
470 printf("[HEADER] Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
476 swf_FontEnumerate(&swf,&fontcallback1);
477 fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
479 swf_FontEnumerate(&swf,&fontcallback2);
483 char*name = swf_TagGetName(tag);
486 dumperror("Unknown tag:0x%03x", tag->id);
490 printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
492 if(tag->id == ST_FREECHARACTER) {
493 U16 id = swf_GetU16(tag);
497 if(swf_isDefiningTag(tag)) {
498 U16 id = swf_GetDefineID(tag);
499 printf(" defines id %04d", id);
501 dumperror("Id %04d is defined more than once.", id);
504 else if(swf_isPseudoDefiningTag(tag)) {
505 U16 id = swf_GetDefineID(tag);
506 printf(" adds information to id %04d", id);
508 dumperror("Id %04d is not yet defined.\n", id);
510 else if(tag->id == ST_PLACEOBJECT) {
511 printf(" places id %04d at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
513 printf(" name \"%s\"",swf_GetName(tag));
515 else if(tag->id == ST_PLACEOBJECT2) {
522 printf(" id %04d",swf_GetPlaceID(tag));
526 printf(" at depth %04d", swf_GetDepth(tag));
528 printf(" name \"%s\"",swf_GetName(tag));
530 else if(tag->id == ST_REMOVEOBJECT) {
531 printf(" removes id %04d from depth %04d", swf_GetPlaceID(tag), swf_GetDepth(tag));
533 else if(tag->id == ST_REMOVEOBJECT2) {
534 printf(" removes object from depth %04d", swf_GetDepth(tag));
536 else if(tag->id == ST_STARTSOUND) {
537 printf(" starts id %04d", swf_GetPlaceID(tag));
539 else if(tag->id == ST_FRAMELABEL) {
540 printf(" \"%s\"", tag->data);
541 if((framelabel && !issprite) ||
542 (spriteframelabel && issprite)) {
543 dumperror("Frame %d has more than one label",
544 issprite?spriteframe:mainframe);
546 if(issprite) spriteframelabel = tag->data;
547 else framelabel = tag->data;
549 else if(tag->id == ST_SHOWFRAME) {
550 char*label = issprite?spriteframelabel:framelabel;
551 int frame = issprite?spriteframe:mainframe;
554 while(tag->next && tag->next->id == ST_SHOWFRAME && tag->next->len == 0) {
556 if(issprite) spriteframe++;
562 printf(" %d", frame);
564 printf(" %d-%d", frame, nframe);
566 printf(" (label \"%s\")", label);
567 if(issprite) {spriteframe++; spriteframelabel = 0;}
568 if(!issprite) {mainframe++; framelabel = 0;}
571 if(tag->id == ST_DEFINEEDITTEXT) {
575 else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
581 else if(tag->id == ST_PLACEOBJECT2) {
587 sprintf(myprefix, " %s", prefix);
589 if(tag->id == ST_DEFINESPRITE) {
590 sprintf(prefix, " ");
592 dumperror("Sprite definition inside a sprite definition");
596 spriteframelabel = 0;
598 else if(tag->id == ST_END) {
601 spriteframelabel = 0;
603 dumperror("End Tag not empty");
605 else if(tag->id == ST_DOACTION && action) {
607 actions = swf_ActionGet(tag);
608 swf_DumpActions(actions, myprefix);
610 else if(tag->id == ST_DEFINEBUTTON && action) {
611 dumpButtonActions(tag, myprefix);
613 else if(tag->id == ST_DEFINEBUTTON2 && action) {
614 dumpButton2Actions(tag, myprefix);
616 else if(tag->id == ST_PLACEOBJECT2) {
617 if((*(U8*)tag->data)&0x80)
618 handlePlaceObject2(tag, myprefix);
622 if(tag->len && hex) {
624 printf(" %s-=> ",prefix);
625 for(t=0;t<tag->len;t++) {
626 printf("%02x ", tag->data[t]);
627 if((t && !(t&15)) || (t==tag->len-1))
632 printf("\n %s-=> ",prefix);