dump frame label and textfield variable names
[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
41 struct options_t options[] =
42 {
43  {"a","action"},
44  {"X","width"},
45  {"Y","height"},
46  {"r","rate"},
47  {"e","html"},
48  {"v","verbose"},
49  {"V","version"},
50  {0,0}
51 };
52
53
54 int args_callback_option(char*name,char*val)
55 {
56     if(!strcmp(name, "V")) {
57         printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
58         exit(0);
59     } 
60     else if(name[0]=='a') {
61         action = 1;
62         return 0;
63     }
64     else if(name[0]=='e') {
65         html = 1;
66         return 0;
67     }
68     else if(name[0]=='X') {
69         xy |= 1;
70         return 0;
71     }
72     else if(name[0]=='Y') {
73         xy |= 2;
74         return 0;
75     }
76     else if(name[0]=='r') {
77         xy |= 4;
78         return 0;
79     }
80     else {
81         printf("Unknown option: -%s\n", name);
82     }
83
84     return 0;
85 }
86 int args_callback_longoption(char*name,char*val)
87 {
88     return args_long2shortoption(options, name, val);
89 }
90 void args_callback_usage(char*name)
91 {    
92     printf("Usage: %s [-a] file.swf\n", name);
93     printf("-h , --help\t\t\t Print help and exit\n");
94     printf("-e , --html\t\t\t Create a html embedding the file (simple, but useful)\n");
95     printf("-X , --width\t\t\t Prints out a string of the form \"-X width\"\n");
96     printf("-Y , --height\t\t\t Prints out a string of the form \"-Y height\"\n");
97     printf("-r , --rate\t\t\t Prints out a string of the form \"-r rate\"\n");
98     printf("-a , --action\t\t\t Disassemble action tags\n");
99     printf("-V , --version\t\t\t Print program version and exit\n");
100 }
101 int args_callback_command(char*name,char*val)
102 {
103     if(filename) {
104         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
105                  filename, name);
106     }
107     filename = name;
108     return 0;
109 }
110
111 char* what;
112 char* testfunc(char*str)
113 {
114     printf("%s: %s\n", what, str);
115     return 0;
116 }
117
118 void dumpButton2Actions(TAG*tag, char*prefix)
119 {
120     U32 oldTagPos;
121     U32 offsetpos;
122     U32 condition;
123
124     oldTagPos = swf_GetTagPos(tag);
125
126     // scan DefineButton2 Record
127     
128     swf_GetU16(tag);          // Character ID
129     swf_GetU8(tag);           // Flags;
130
131     offsetpos = swf_GetTagPos(tag);  // first offset
132     swf_GetU16(tag);
133
134     while (swf_GetU8(tag))      // state  -> parse ButtonRecord
135     { swf_GetU16(tag);          // id
136       swf_GetU16(tag);          // layer
137       swf_GetMatrix(tag,NULL);  // matrix
138       swf_GetCXForm(tag,NULL,0);  // matrix
139     }
140
141     while(offsetpos)
142     { U8 a;
143       ActionTAG*actions;
144         
145       offsetpos = swf_GetU16(tag);
146       condition = swf_GetU16(tag);                // condition
147       
148       actions = swf_ActionGet(tag);
149       printf("%s condition %04x\n", prefix, condition);
150       swf_DumpActions(actions, prefix);
151     }
152     
153     swf_SetTagPos(tag,oldTagPos);
154     return;
155 }
156
157 void dumpButtonActions(TAG*tag, char*prefix)
158 {
159     ActionTAG*actions;
160     swf_GetU16(tag); // id
161     while (swf_GetU8(tag))      // state  -> parse ButtonRecord
162     { swf_GetU16(tag);          // id
163       swf_GetU16(tag);          // layer
164       swf_GetMatrix(tag,NULL);  // matrix
165     }
166     actions = swf_ActionGet(tag);
167     swf_DumpActions(actions, prefix);
168 }
169
170 #define ET_HASTEXT 32768
171 #define ET_WORDWRAP 16384
172 #define ET_MULTILINE 8192
173 #define ET_PASSWORD 4096
174 #define ET_READONLY 2048
175 #define ET_HASTEXTCOLOR 1024
176 #define ET_HASMAXLENGTH 512
177 #define ET_HASFONT 256
178 #define ET_X3 128
179 #define ET_X2 64
180 #define ET_HASLAYOUT 32
181 #define ET_NOSELECT 16
182 #define ET_BORDER 8
183 #define ET_X1 4
184 #define ET_X0 2
185 #define ET_USEOUTLINES 1
186
187 void handleEditText(TAG*tag)
188 {
189     U16 id ;
190     U16 flags;
191     int t;
192     id = swf_GetU16(tag);
193     swf_GetRect(tag,0);
194     //swf_ResetReadBits(tag);
195     if (tag->readBit)  
196     { tag->pos++; 
197       tag->readBit = 0; 
198     }
199     flags = swf_GetBits(tag,16);
200     if(flags & ET_HASFONT) {
201         swf_GetU16(tag); //font
202         swf_GetU16(tag); //fontheight
203     }
204     if(flags & ET_HASTEXTCOLOR) {
205         swf_GetU8(tag); //rgba
206         swf_GetU8(tag);
207         swf_GetU8(tag);
208         swf_GetU8(tag);
209     }
210     if(flags & ET_HASMAXLENGTH) {
211         swf_GetU16(tag); //maxlength
212     }
213     if(flags & ET_HASLAYOUT) {
214         swf_GetU8(tag); //align
215         swf_GetU16(tag); //left margin
216         swf_GetU16(tag); //right margin
217         swf_GetU16(tag); //indent
218         swf_GetU16(tag); //leading
219     }
220     printf(" variable \"%s\"", &tag->data[tag->pos]);
221
222     if(flags & (ET_X1 | ET_X2 | ET_X3 | ET_X0))
223     {
224         printf(" undefined flags: %d%d%d%d", 
225                 (flags&ET_X0?1:0),
226                 (flags&ET_X1?1:0),
227                 (flags&ET_X2?1:0),
228                 (flags&ET_X3?1:0));
229     }
230     
231     while(tag->data[tag->pos++]);
232     if(flags & ET_HASTEXT)
233    //  printf(" text \"%s\"\n", &tag->data[tag->pos])
234         ;
235 }
236
237 int main (int argc,char ** argv)
238
239     SWF swf;
240     TAG*tag;
241 #ifdef HAVE_STAT
242     struct stat statbuf;
243 #endif
244     int f;
245     int xsize,ysize;
246     char prefix[128];
247     prefix[0] = 0;
248     memset(idtab,0,65536);
249
250     processargs(argc, argv);
251
252     if(!filename)
253     {
254         fprintf(stderr, "You must supply a filename.\n");
255         return 1;
256     }
257
258     f = open(filename,O_RDONLY);
259
260     if (f<0)
261     { 
262         perror("Couldn't open file: ");
263         exit(1);
264     }
265     if FAILED(swf_ReadSWF(f,&swf))
266     { 
267         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
268         close(f);
269         exit(1);
270     }
271
272 #ifdef HAVE_STAT
273     fstat(f, &statbuf);
274     if(statbuf.st_size != swf.FileSize)
275         fprintf(stderr, "Error: Real Filesize (%d) doesn't match header Filesize (%d)",
276                 statbuf.st_size, swf.FileSize);
277 #endif
278
279     close(f);
280
281     xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
282     ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
283     if(xy)
284     {
285         if(xy&1)
286         printf("-X %d", xsize);
287
288         if((xy&1) && (xy&6))
289         printf(" ");
290
291         if(xy&2)
292         printf("-Y %d", ysize);
293         
294         if((xy&3) && (xy&4))
295         printf(" ");
296
297         if(xy&4)
298         printf("-r %d", swf.frameRate*100/256);
299         
300         printf("\n");
301         return 0;
302     }
303     if(html)
304     {
305         printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
306                " WIDTH=\"%d\"\n"
307                " HEIGHT=\"%d\"\n"
308                " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%d,0,0,0\">\n"
309                "  <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
310                "  <PARAM NAME=\"PLAY\" VALUE=\"true\">\n" 
311                "  <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
312                "  <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
313                "  <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n"
314                "   PLAY=\"true\" LOOP=\"true\" QUALITY=\"high\"\n"
315                "   TYPE=\"application/x-shockwave-flash\"\n"
316                "   PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">\n"
317                "  </EMBED>\n" 
318                "</OBJECT>\n", xsize, ysize, swf.fileVersion, filename, filename, xsize, ysize);
319         return 0;
320     } 
321     printf("[HEADER]        File version: %d\n", swf.fileVersion);
322     printf("[HEADER]        File size: %ld\n", swf.fileSize);
323     printf("[HEADER]        Frame rate: %f\n",swf.frameRate/256.0);
324     printf("[HEADER]        Frame count: %d\n",swf.frameCount);
325     printf("[HEADER]        Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
326     printf("[HEADER]        Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
327
328     tag = swf.firstTag;
329
330     while(tag) {
331         char*name = swf_TagGetName(tag);
332         char myprefix[128];
333         if(!name) {
334             fprintf(stderr, "Error: Unknown tag:0x%03x\n", tag->id);
335             tag = tag->next;
336             continue;
337         }
338         printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
339
340         if(swf_isDefiningTag(tag)) {
341             U16 id = swf_GetDefineID(tag);
342             printf(" defines id %04x", id);
343             if(idtab[id])
344                 fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);
345             idtab[id] = 1;
346         }
347         else if(tag->id == ST_PLACEOBJECT || 
348                 tag->id == ST_PLACEOBJECT2) {
349             printf(" places id %04x at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
350             if(swf_GetName(tag))
351                 printf(" name \"%s\"",swf_GetName(tag));
352         }
353         else if(tag->id == ST_REMOVEOBJECT) {
354             printf(" removes id %04x from depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
355         }
356         else if(tag->id == ST_REMOVEOBJECT2) {
357             printf(" removes object from depth %04x", swf_GetDepth(tag));
358         }
359         else if(tag->id == ST_FRAMELABEL) {
360             printf(" \"%s\"", tag->data);
361         }
362         if(tag->id == ST_DEFINEEDITTEXT) {
363             handleEditText(tag);
364         }
365         
366         printf("\n");
367         sprintf(myprefix, "                %s", prefix);
368
369         if(tag->id == ST_DEFINESPRITE) {
370             sprintf(prefix, "         ");
371         }
372         else if(tag->id == ST_END) {
373             *prefix = 0;
374             if(tag->len)
375                 fprintf(stderr, "Error: End Tag not empty");
376         }
377         else if(tag->id == ST_DOACTION && action) {
378             ActionTAG*actions;
379             actions = swf_ActionGet(tag);
380             swf_DumpActions(actions, myprefix);
381         }
382         else if(tag->id == ST_DEFINEBUTTON && action) {
383             dumpButtonActions(tag, myprefix);
384         }
385         else if(tag->id == ST_DEFINEBUTTON2 && action) {
386             dumpButton2Actions(tag, myprefix);
387         }
388         tag = tag->next;
389     }
390
391     swf_FreeTags(&swf);
392     return 0;
393 }
394