* bugfix in text handling
[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\t Print help and exit\n");
100     printf("\t-e , --html\t\t\t Create a html embedding the file (simple, but useful)\n");
101     printf("\t-X , --width\t\t\t Prints out a string of the form \"-X width\"\n");
102     printf("\t-Y , --height\t\t\t Prints out a string of the form \"-Y height\"\n");
103     printf("\t-r , --rate\t\t\t Prints out a string of the form \"-r rate\"\n");
104     printf("\t-a , --action\t\t\t Disassemble action tags\n");
105     printf("\t-t , --text\t\t\t Show text data\n");
106     printf("\t-V , --version\t\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);  // matrix
146     }
147
148     while(offsetpos)
149     { U8 a;
150       ActionTAG*actions;
151         
152       offsetpos = swf_GetU16(tag);
153       condition = swf_GetU16(tag);                // condition
154       
155       actions = swf_ActionGet(tag);
156       printf("%s condition %04x\n", prefix, condition);
157       swf_DumpActions(actions, prefix);
158     }
159     
160     swf_SetTagPos(tag,oldTagPos);
161     return;
162 }
163
164 void dumpButtonActions(TAG*tag, char*prefix)
165 {
166     ActionTAG*actions;
167     swf_GetU16(tag); // id
168     while (swf_GetU8(tag))      // state  -> parse ButtonRecord
169     { swf_GetU16(tag);          // id
170       swf_GetU16(tag);          // layer
171       swf_GetMatrix(tag,NULL);  // matrix
172     }
173     actions = swf_ActionGet(tag);
174     swf_DumpActions(actions, prefix);
175 }
176
177 #define ET_HASTEXT 32768
178 #define ET_WORDWRAP 16384
179 #define ET_MULTILINE 8192
180 #define ET_PASSWORD 4096
181 #define ET_READONLY 2048
182 #define ET_HASTEXTCOLOR 1024
183 #define ET_HASMAXLENGTH 512
184 #define ET_HASFONT 256
185 #define ET_X3 128
186 #define ET_X2 64
187 #define ET_HASLAYOUT 32
188 #define ET_NOSELECT 16
189 #define ET_BORDER 8
190 #define ET_X1 4
191 #define ET_X0 2
192 #define ET_USEOUTLINES 1
193
194 SWF swf;
195 int fontnum = 0;
196 SWFFONT**fonts;
197
198 void textcallback(int*glyphs, int nr, int fontid) 
199 {
200     int font=-1,t;
201     printf("                <%2d glyphs in font %2d> ",nr, fontid);
202     for(t=0;t<fontnum;t++)
203     {
204         if(fonts[t]->id == fontid) {
205             font = t;
206             break;
207         }
208     }
209     if(font<0) {
210         printf("\n");
211         return; // todo: should we report this? (may only be that it's a definefont without fontinfo)
212     }
213
214     for(t=0;t<nr;t++)
215     {
216         unsigned char a; 
217         if(glyphs[t] >= fonts[font]->numchars)
218             continue;
219         a = fonts[font]->glyph2ascii[glyphs[t]];
220         if(a>=32)
221             printf("%c", a);
222         else
223             printf("\\x%x", (int)a);
224     }
225     printf("\n");
226 }
227
228 void handleText(TAG*tag) 
229 {
230   printf("\n");
231   swf_FontExtract_DefineTextCallback(-1,0,tag,4, textcallback);
232 }
233
234 void handleEditText(TAG*tag)
235 {
236     U16 id ;
237     U16 flags;
238     int t;
239     id = swf_GetU16(tag);
240     swf_GetRect(tag,0);
241     //swf_ResetReadBits(tag);
242     if (tag->readBit)  
243     { tag->pos++; 
244       tag->readBit = 0; 
245     }
246     flags = swf_GetBits(tag,16);
247     if(flags & ET_HASFONT) {
248         swf_GetU16(tag); //font
249         swf_GetU16(tag); //fontheight
250     }
251     if(flags & ET_HASTEXTCOLOR) {
252         swf_GetU8(tag); //rgba
253         swf_GetU8(tag);
254         swf_GetU8(tag);
255         swf_GetU8(tag);
256     }
257     if(flags & ET_HASMAXLENGTH) {
258         swf_GetU16(tag); //maxlength
259     }
260     if(flags & ET_HASLAYOUT) {
261         swf_GetU8(tag); //align
262         swf_GetU16(tag); //left margin
263         swf_GetU16(tag); //right margin
264         swf_GetU16(tag); //indent
265         swf_GetU16(tag); //leading
266     }
267     printf(" variable \"%s\"", &tag->data[tag->pos]);
268
269     if(flags & (ET_X1 | ET_X2 | ET_X3 | ET_X0))
270     {
271         printf(" undefined flags: %d%d%d%d", 
272                 (flags&ET_X0?1:0),
273                 (flags&ET_X1?1:0),
274                 (flags&ET_X2?1:0),
275                 (flags&ET_X3?1:0));
276     }
277     
278     while(tag->data[tag->pos++]);
279     if(flags & ET_HASTEXT)
280    //  printf(" text \"%s\"\n", &tag->data[tag->pos])
281         ;
282 }
283     
284 void fontcallback1(U16 id,U8 * name)
285 { fontnum++;
286 }
287
288 void fontcallback2(U16 id,U8 * name)
289 { swf_FontExtract(&swf,id,&fonts[fontnum]);
290   fontnum++;
291 }
292
293 int main (int argc,char ** argv)
294
295     TAG*tag;
296 #ifdef HAVE_STAT
297     struct stat statbuf;
298 #endif
299     int f;
300     int xsize,ysize;
301     char issprite = 0; // are we inside a sprite definition?
302     int spriteframe;
303     int mainframe=0;
304     char* spriteframelabel;
305     char* framelabel = 0;
306     char prefix[128];
307     prefix[0] = 0;
308     memset(idtab,0,65536);
309
310     processargs(argc, argv);
311
312     if(!filename)
313     {
314         fprintf(stderr, "You must supply a filename.\n");
315         return 1;
316     }
317
318     f = open(filename,O_RDONLY);
319
320     if (f<0)
321     { 
322         perror("Couldn't open file: ");
323         exit(1);
324     }
325     if FAILED(swf_ReadSWF(f,&swf))
326     { 
327         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
328         close(f);
329         exit(1);
330     }
331
332 #ifdef HAVE_STAT
333     fstat(f, &statbuf);
334     if(statbuf.st_size != swf.FileSize)
335         fprintf(stderr, "Error: Real Filesize (%d) doesn't match header Filesize (%d)",
336                 statbuf.st_size, swf.FileSize);
337 #endif
338
339     close(f);
340
341     xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
342     ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
343     if(xy)
344     {
345         if(xy&1)
346         printf("-X %d", xsize);
347
348         if((xy&1) && (xy&6))
349         printf(" ");
350
351         if(xy&2)
352         printf("-Y %d", ysize);
353         
354         if((xy&3) && (xy&4))
355         printf(" ");
356
357         if(xy&4)
358         printf("-r %d", swf.frameRate*100/256);
359         
360         printf("\n");
361         return 0;
362     }
363     if(html)
364     {
365         printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
366                " WIDTH=\"%d\"\n"
367                " HEIGHT=\"%d\"\n"
368                " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%d,0,0,0\">\n"
369                "  <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
370                "  <PARAM NAME=\"PLAY\" VALUE=\"true\">\n" 
371                "  <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
372                "  <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
373                "  <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n"
374                "   PLAY=\"true\" LOOP=\"true\" QUALITY=\"high\"\n"
375                "   TYPE=\"application/x-shockwave-flash\"\n"
376                "   PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">\n"
377                "  </EMBED>\n" 
378                "</OBJECT>\n", xsize, ysize, swf.fileVersion, filename, filename, xsize, ysize);
379         return 0;
380     } 
381     printf("[HEADER]        File version: %d\n", swf.fileVersion);
382     printf("[HEADER]        File size: %ld\n", swf.fileSize);
383     printf("[HEADER]        Frame rate: %f\n",swf.frameRate/256.0);
384     printf("[HEADER]        Frame count: %d\n",swf.frameCount);
385     printf("[HEADER]        Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
386     printf("[HEADER]        Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
387
388     tag = swf.firstTag;
389    
390     if(showtext) {
391         fontnum = 0;
392         swf_FontEnumerate(&swf,&fontcallback1);
393         fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
394         fontnum = 0;
395         swf_FontEnumerate(&swf,&fontcallback2);
396     }
397
398     while(tag) {
399         char*name = swf_TagGetName(tag);
400         char myprefix[128];
401         if(!name) {
402             fprintf(stderr, "Error: Unknown tag:0x%03x\n", tag->id);
403             tag = tag->next;
404             continue;
405         }
406         printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
407
408         if(swf_isDefiningTag(tag)) {
409             U16 id = swf_GetDefineID(tag);
410             printf(" defines id %04x", id);
411             if(idtab[id])
412                 fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);
413             idtab[id] = 1;
414         }
415         else if(swf_isPseudoDefiningTag(tag)) {
416             U16 id = swf_GetDefineID(tag);
417             printf(" adds information to id %04x", id);
418             if(!idtab[id])
419                 fprintf(stderr, "Error: Id %04x is not yet defined.\n", id);
420         }
421         else if(tag->id == ST_PLACEOBJECT || 
422                 tag->id == ST_PLACEOBJECT2) {
423             printf(" places id %04x at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
424             if(swf_GetName(tag))
425                 printf(" name \"%s\"",swf_GetName(tag));
426         }
427         else if(tag->id == ST_REMOVEOBJECT) {
428             printf(" removes id %04x from depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
429         }
430         else if(tag->id == ST_REMOVEOBJECT2) {
431             printf(" removes object from depth %04x", swf_GetDepth(tag));
432         }
433         else if(tag->id == ST_FRAMELABEL) {
434             printf(" \"%s\"", tag->data);
435             if(framelabel) {
436                 fprintf(stderr, "Error: Frame %d has more than one label\n", 
437                         issprite?spriteframe:mainframe);
438             }
439             if(issprite) spriteframelabel = tag->data;
440             else framelabel = tag->data;
441         }
442         else if(tag->id == ST_SHOWFRAME) {
443             char*label = issprite?spriteframelabel:framelabel;
444             printf(" %d", issprite?spriteframe:mainframe);
445             if(label)
446                 printf(" (label \"%s\")", label);
447             if(issprite) {spriteframe++; spriteframelabel = 0;}
448             if(!issprite) {mainframe++; framelabel = 0;}
449         }
450
451         if(tag->id == ST_DEFINEEDITTEXT) {
452             handleEditText(tag);
453             printf("\n");
454         }
455         else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
456             if(showtext)
457                 handleText(tag);
458             else
459                 printf("\n");
460         }
461         else {
462             printf("\n");
463         }
464         
465         sprintf(myprefix, "                %s", prefix);
466
467         if(tag->id == ST_DEFINESPRITE) {
468             sprintf(prefix, "         ");
469             if(issprite) {
470                 fprintf(stderr, "Error: Sprite definition inside a sprite definition");
471             }
472             issprite = 1;
473             spriteframe = 0;
474             spriteframelabel = 0;
475         }
476         else if(tag->id == ST_END) {
477             *prefix = 0;
478             issprite = 0;
479             if(tag->len)
480                 fprintf(stderr, "Error: End Tag not empty");
481         }
482         else if(tag->id == ST_DOACTION && action) {
483             ActionTAG*actions;
484             actions = swf_ActionGet(tag);
485             swf_DumpActions(actions, myprefix);
486         }
487         else if(tag->id == ST_DEFINEBUTTON && action) {
488             dumpButtonActions(tag, myprefix);
489         }
490         else if(tag->id == ST_DEFINEBUTTON2 && action) {
491             dumpButton2Actions(tag, myprefix);
492         }
493         tag = tag->next;
494     }
495
496     swf_FreeTags(&swf);
497     return 0;
498 }
499