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