make output shorter:
[swftools.git] / src / swfdump.c
1 /* swfdump.c
2    Shows the structure of a swf file
3
4    Part of the swftools package.
5    
6    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
7
8    This file is distributed under the GPL, see file COPYING for details */
9
10 #define HAVE_STAT
11
12 #ifdef HAVE_SYS_STAT_H
13 #include <sys/stat.h>
14 #else
15 #undef HAVE_STAT
16 #endif
17
18 #ifdef HAVE_SYS_TYPES_H
19 #include <sys/types.h>
20 #else
21 #undef HAVE_STAT
22 #endif
23
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include "../lib/rfxswf.h"
28 #include "../lib/args.h"
29
30 char * filename = 0;
31
32 /* idtab stores the ids which are defined in the file. This allows us
33    to detect errors in the file. (i.e. ids which are defined more than 
34    once */
35 char idtab[65536];
36
37 int action = 0;
38 int html = 0;
39 int xy = 0;
40 int showtext = 0;
41
42 struct options_t options[] =
43 {
44  {"a","action"},
45  {"t","text"},
46  {"X","width"},
47  {"Y","height"},
48  {"r","rate"},
49  {"e","html"},
50  {"v","verbose"},
51  {"V","version"},
52  {0,0}
53 };
54
55
56 int args_callback_option(char*name,char*val)
57 {
58     if(!strcmp(name, "V")) {
59         printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
60         exit(0);
61     } 
62     else if(name[0]=='a') {
63         action = 1;
64         return 0;
65     }
66     else if(name[0]=='t') {
67         showtext = 1;
68         return 0;
69     }
70     else if(name[0]=='e') {
71         html = 1;
72         return 0;
73     }
74     else if(name[0]=='X') {
75         xy |= 1;
76         return 0;
77     }
78     else if(name[0]=='Y') {
79         xy |= 2;
80         return 0;
81     }
82     else if(name[0]=='r') {
83         xy |= 4;
84         return 0;
85     }
86     else {
87         printf("Unknown option: -%s\n", name);
88     }
89
90     return 0;
91 }
92 int args_callback_longoption(char*name,char*val)
93 {
94     return args_long2shortoption(options, name, val);
95 }
96 void args_callback_usage(char*name)
97 {    
98     printf("Usage: %s [-at] file.swf\n", name);
99     printf("\t-h , --help\t\t Print help and exit\n");
100     printf("\t-e , --html\t\t Create a html embedding the file (simple, but useful)\n");
101     printf("\t-X , --width\t\t Prints out a string of the form \"-X width\"\n");
102     printf("\t-Y , --height\t\t Prints out a string of the form \"-Y height\"\n");
103     printf("\t-r , --rate\t\t Prints out a string of the form \"-r rate\"\n");
104     printf("\t-a , --action\t\t Disassemble action tags\n");
105     printf("\t-t , --text\t\t Show text data\n");
106     printf("\t-V , --version\t\t Print program version and exit\n");
107 }
108 int args_callback_command(char*name,char*val)
109 {
110     if(filename) {
111         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
112                  filename, name);
113     }
114     filename = name;
115     return 0;
116 }
117
118 char* what;
119 char* testfunc(char*str)
120 {
121     printf("%s: %s\n", what, str);
122     return 0;
123 }
124
125 void dumpButton2Actions(TAG*tag, char*prefix)
126 {
127     U32 oldTagPos;
128     U32 offsetpos;
129     U32 condition;
130
131     oldTagPos = swf_GetTagPos(tag);
132
133     // scan DefineButton2 Record
134     
135     swf_GetU16(tag);          // Character ID
136     swf_GetU8(tag);           // Flags;
137
138     offsetpos = swf_GetTagPos(tag);  // first offset
139     swf_GetU16(tag);
140
141     while (swf_GetU8(tag))      // state  -> parse ButtonRecord
142     { swf_GetU16(tag);          // id
143       swf_GetU16(tag);          // layer
144       swf_GetMatrix(tag,NULL);  // matrix
145       swf_GetCXForm(tag,NULL,1);  // cxform
146     }
147
148     while(offsetpos)
149     { U8 a;
150       ActionTAG*actions;
151
152       if(tag->pos >= tag->len)
153           break;
154         
155       offsetpos = swf_GetU16(tag);
156       condition = swf_GetU16(tag);                // condition
157       
158       actions = swf_ActionGet(tag);
159       printf("%s condition %04x\n", prefix, condition);
160       swf_DumpActions(actions, prefix);
161     }
162     
163     swf_SetTagPos(tag,oldTagPos);
164     return;
165 }
166
167 void dumpButtonActions(TAG*tag, char*prefix)
168 {
169     ActionTAG*actions;
170     swf_GetU16(tag); // id
171     while (swf_GetU8(tag))      // state  -> parse ButtonRecord
172     { swf_GetU16(tag);          // id
173       swf_GetU16(tag);          // layer
174       swf_GetMatrix(tag,NULL);  // matrix
175     }
176     actions = swf_ActionGet(tag);
177     swf_DumpActions(actions, prefix);
178 }
179
180 #define ET_HASTEXT 32768
181 #define ET_WORDWRAP 16384
182 #define ET_MULTILINE 8192
183 #define ET_PASSWORD 4096
184 #define ET_READONLY 2048
185 #define ET_HASTEXTCOLOR 1024
186 #define ET_HASMAXLENGTH 512
187 #define ET_HASFONT 256
188 #define ET_X3 128
189 #define ET_X2 64
190 #define ET_HASLAYOUT 32
191 #define ET_NOSELECT 16
192 #define ET_BORDER 8
193 #define ET_X1 4
194 #define ET_X0 2
195 #define ET_USEOUTLINES 1
196
197 SWF swf;
198 int fontnum = 0;
199 SWFFONT**fonts;
200
201 void textcallback(int*glyphs, int nr, int fontid) 
202 {
203     int font=-1,t;
204     printf("                <%2d glyphs in font %2d> ",nr, fontid);
205     for(t=0;t<fontnum;t++)
206     {
207         if(fonts[t]->id == fontid) {
208             font = t;
209             break;
210         }
211     }
212     if(font<0) {
213         printf("\n");
214         return; // todo: should we report this? (may only be that it's a definefont without fontinfo)
215     }
216
217     for(t=0;t<nr;t++)
218     {
219         unsigned char a; 
220         if(glyphs[t] >= fonts[font]->numchars)
221             continue;
222         a = fonts[font]->glyph2ascii[glyphs[t]];
223         if(a>=32)
224             printf("%c", a);
225         else
226             printf("\\x%x", (int)a);
227     }
228     printf("\n");
229 }
230
231 void handleText(TAG*tag) 
232 {
233   printf("\n");
234   swf_FontExtract_DefineTextCallback(-1,0,tag,4, textcallback);
235 }
236
237 void handleEditText(TAG*tag)
238 {
239     U16 id ;
240     U16 flags;
241     int t;
242     id = swf_GetU16(tag);
243     swf_GetRect(tag,0);
244     //swf_ResetReadBits(tag);
245     if (tag->readBit)  
246     { tag->pos++; 
247       tag->readBit = 0; 
248     }
249     flags = swf_GetBits(tag,16);
250     if(flags & ET_HASFONT) {
251         swf_GetU16(tag); //font
252         swf_GetU16(tag); //fontheight
253     }
254     if(flags & ET_HASTEXTCOLOR) {
255         swf_GetU8(tag); //rgba
256         swf_GetU8(tag);
257         swf_GetU8(tag);
258         swf_GetU8(tag);
259     }
260     if(flags & ET_HASMAXLENGTH) {
261         swf_GetU16(tag); //maxlength
262     }
263     if(flags & ET_HASLAYOUT) {
264         swf_GetU8(tag); //align
265         swf_GetU16(tag); //left margin
266         swf_GetU16(tag); //right margin
267         swf_GetU16(tag); //indent
268         swf_GetU16(tag); //leading
269     }
270     printf(" variable \"%s\"", &tag->data[tag->pos]);
271
272     if(flags & (ET_X1 | ET_X2 | ET_X3 | ET_X0))
273     {
274         printf(" undefined flags: %d%d%d%d", 
275                 (flags&ET_X0?1:0),
276                 (flags&ET_X1?1:0),
277                 (flags&ET_X2?1:0),
278                 (flags&ET_X3?1:0));
279     }
280     
281     while(tag->data[tag->pos++]);
282     if(flags & ET_HASTEXT)
283    //  printf(" text \"%s\"\n", &tag->data[tag->pos])
284         ;
285 }
286 void printhandlerflags(U16 handlerflags) 
287 {
288     if(handlerflags&1) printf("[on load]");
289     if(handlerflags&2) printf("[enter frame]");
290     if(handlerflags&4) printf("[unload]");
291     if(handlerflags&8) printf("[mouse move]");
292     if(handlerflags&16) printf("[mouse down]");
293     if(handlerflags&32) printf("[mouse up]");
294     if(handlerflags&64) printf("[key down]");
295     if(handlerflags&128) printf("[key up]");
296     if(handlerflags&256) printf("[data]");
297     if(handlerflags&0xfe00) printf("[???]");
298 }
299 void handlePlaceObject2(TAG*tag, char*prefix)
300 {
301     U8 flags = swf_GetU8(tag);
302     if(flags&2) swf_GetU16(tag); //id
303     if(flags&4) swf_GetMatrix(tag,0);
304     if(flags&8) swf_GetCXForm(tag,0,0);
305     if(flags&16) swf_GetU16(tag); //ratio
306     if(flags&32) { 
307         while(swf_GetU8(tag));
308     }
309     if(flags&64) swf_GetU16(tag); //clip
310     if(flags&128) {
311       if (action) {
312         U16 globalflags;
313         printf("\n");
314         swf_GetU16(tag);
315         globalflags = swf_GetU16(tag);
316 //      printf("%s global flags:%04x ",prefix, handlerflags);
317 //      printhandlerflags(globalflags);
318 //      printf("\n");
319         while(1)  {
320             int length;
321             int t;
322             U16 handlerflags;
323             ActionTAG*a;
324             handlerflags = swf_GetU16(tag);
325             if(!handlerflags)
326                 break;
327             globalflags &= ~handlerflags;
328             printf("%s flags %04x ",prefix, handlerflags);
329             printhandlerflags(handlerflags);
330
331             length = swf_GetU32(tag);
332             printf(", %d bytes actioncode\n",length);
333             a = swf_ActionGet(tag);
334             swf_DumpActions(a,prefix);
335             swf_ActionFree(a);
336         }
337         if(globalflags) // should go to sterr.
338             printf("%s ERROR: unsatisfied handlerflags: %02x", globalflags);
339     } else {
340       printf(" has action code\n");
341     }
342     } else printf("\n");
343 }
344     
345 void fontcallback1(U16 id,U8 * name)
346 { fontnum++;
347 }
348
349 void fontcallback2(U16 id,U8 * name)
350 { swf_FontExtract(&swf,id,&fonts[fontnum]);
351   fontnum++;
352 }
353
354 int main (int argc,char ** argv)
355
356     TAG*tag;
357 #ifdef HAVE_STAT
358     struct stat statbuf;
359 #endif
360     int f;
361     int xsize,ysize;
362     char issprite = 0; // are we inside a sprite definition?
363     int spriteframe;
364     int mainframe=0;
365     char* spriteframelabel;
366     char* framelabel = 0;
367     char prefix[128];
368     prefix[0] = 0;
369     memset(idtab,0,65536);
370
371     processargs(argc, argv);
372
373     if(!filename)
374     {
375         fprintf(stderr, "You must supply a filename.\n");
376         return 1;
377     }
378
379     f = open(filename,O_RDONLY);
380
381     if (f<0)
382     { 
383         perror("Couldn't open file: ");
384         exit(1);
385     }
386     if FAILED(swf_ReadSWF(f,&swf))
387     { 
388         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
389         close(f);
390         exit(1);
391     }
392
393 #ifdef HAVE_STAT
394     fstat(f, &statbuf);
395     if(statbuf.st_size != swf.FileSize)
396         fprintf(stderr, "Error: Real Filesize (%d) doesn't match header Filesize (%d)",
397                 statbuf.st_size, swf.FileSize);
398 #endif
399
400     close(f);
401
402     xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
403     ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
404     if(xy)
405     {
406         if(xy&1)
407         printf("-X %d", xsize);
408
409         if((xy&1) && (xy&6))
410         printf(" ");
411
412         if(xy&2)
413         printf("-Y %d", ysize);
414         
415         if((xy&3) && (xy&4))
416         printf(" ");
417
418         if(xy&4)
419         printf("-r %d", swf.frameRate*100/256);
420         
421         printf("\n");
422         return 0;
423     }
424     if(html)
425     {
426         printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
427                " WIDTH=\"%d\"\n"
428                " HEIGHT=\"%d\"\n"
429                " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%d,0,0,0\">\n"
430                "  <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
431                "  <PARAM NAME=\"PLAY\" VALUE=\"true\">\n" 
432                "  <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
433                "  <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
434                "  <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n"
435                "   PLAY=\"true\" LOOP=\"true\" QUALITY=\"high\"\n"
436                "   TYPE=\"application/x-shockwave-flash\"\n"
437                "   PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">\n"
438                "  </EMBED>\n" 
439                "</OBJECT>\n", xsize, ysize, swf.fileVersion, filename, filename, xsize, ysize);
440         return 0;
441     } 
442     printf("[HEADER]        File version: %d\n", swf.fileVersion);
443     printf("[HEADER]        File size: %ld\n", swf.fileSize);
444     printf("[HEADER]        Frame rate: %f\n",swf.frameRate/256.0);
445     printf("[HEADER]        Frame count: %d\n",swf.frameCount);
446     printf("[HEADER]        Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
447     printf("[HEADER]        Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
448
449     tag = swf.firstTag;
450    
451     if(showtext) {
452         fontnum = 0;
453         swf_FontEnumerate(&swf,&fontcallback1);
454         fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
455         fontnum = 0;
456         swf_FontEnumerate(&swf,&fontcallback2);
457     }
458
459     while(tag) {
460         char*name = swf_TagGetName(tag);
461         char myprefix[128];
462         if(!name) {
463             fprintf(stderr, "Error: Unknown tag:0x%03x\n", tag->id);
464             tag = tag->next;
465             continue;
466         }
467         printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
468
469         if(swf_isDefiningTag(tag)) {
470             U16 id = swf_GetDefineID(tag);
471             printf(" defines id %04x", id);
472             if(idtab[id])
473                 fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);
474             idtab[id] = 1;
475         }
476         else if(swf_isPseudoDefiningTag(tag)) {
477             U16 id = swf_GetDefineID(tag);
478             printf(" adds information to id %04x", id);
479             if(!idtab[id])
480                 fprintf(stderr, "Error: Id %04x is not yet defined.\n", id);
481         }
482         else if(tag->id == ST_PLACEOBJECT || 
483                 tag->id == ST_PLACEOBJECT2) {
484             printf(" places id %04x at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
485             if(swf_GetName(tag))
486                 printf(" name \"%s\"",swf_GetName(tag));
487         }
488         else if(tag->id == ST_REMOVEOBJECT) {
489             printf(" removes id %04x from depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
490         }
491         else if(tag->id == ST_REMOVEOBJECT2) {
492             printf(" removes object from depth %04x", swf_GetDepth(tag));
493         }
494         else if(tag->id == ST_FRAMELABEL) {
495             printf(" \"%s\"", tag->data);
496             if(framelabel) {
497                 fprintf(stderr, "Error: Frame %d has more than one label\n", 
498                         issprite?spriteframe:mainframe);
499             }
500             if(issprite) spriteframelabel = tag->data;
501             else framelabel = tag->data;
502         }
503         else if(tag->id == ST_SHOWFRAME) {
504             char*label = issprite?spriteframelabel:framelabel;
505             int frame = issprite?spriteframe:mainframe;
506             int nframe = frame;
507             if(!label) {
508                 while(tag->next && tag->next->id == ST_SHOWFRAME && tag->next->len == 0) {
509                     tag = tag->next;
510                     if(issprite) spriteframe++;
511                     else mainframe++;
512                     nframe++;
513                 }
514             }
515             if(nframe == frame)
516                 printf(" %d", frame);
517             else
518                 printf(" %d-%d", frame, nframe);
519             if(label)
520                 printf(" (label \"%s\")", label);
521             if(issprite) {spriteframe++; spriteframelabel = 0;}
522             if(!issprite) {mainframe++; framelabel = 0;}
523         }
524
525         if(tag->id == ST_DEFINEEDITTEXT) {
526             handleEditText(tag);
527             printf("\n");
528         }
529         else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
530             if(showtext)
531                 handleText(tag);
532             else
533                 printf("\n");
534         }
535         else if(tag->id == ST_PLACEOBJECT2) {
536         }
537         else {
538             printf("\n");
539         }
540         
541         sprintf(myprefix, "                %s", prefix);
542
543         if(tag->id == ST_DEFINESPRITE) {
544             sprintf(prefix, "         ");
545             if(issprite) {
546                 fprintf(stderr, "Error: Sprite definition inside a sprite definition");
547             }
548             issprite = 1;
549             spriteframe = 0;
550             spriteframelabel = 0;
551         }
552         else if(tag->id == ST_END) {
553             *prefix = 0;
554             issprite = 0;
555             if(tag->len)
556                 fprintf(stderr, "Error: End Tag not empty");
557         }
558         else if(tag->id == ST_DOACTION && action) {
559             ActionTAG*actions;
560             actions = swf_ActionGet(tag);
561             swf_DumpActions(actions, myprefix);
562         }
563         else if(tag->id == ST_DEFINEBUTTON && action) {
564             dumpButtonActions(tag, myprefix);
565         }
566         else if(tag->id == ST_DEFINEBUTTON2 && action) {
567             dumpButton2Actions(tag, myprefix);
568         }
569         else if(tag->id == ST_PLACEOBJECT2) {
570             if((*(U8*)tag->data)&0x80)
571                 handlePlaceObject2(tag, myprefix);
572             else
573                 printf("\n");
574         }
575         tag = tag->next;
576     }
577
578     swf_FreeTags(&swf);
579     return 0;
580 }
581
582