* updated function names for the actionscript module
[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 int main (int argc,char ** argv)
171
172     SWF swf;
173     TAG*tag;
174 #ifdef HAVE_STAT
175     struct stat statbuf;
176 #endif
177     int f;
178     int xsize,ysize;
179     char prefix[128];
180     prefix[0] = 0;
181     memset(idtab,0,65536);
182
183     processargs(argc, argv);
184
185     if(!filename)
186     {
187         fprintf(stderr, "You must supply a filename.\n");
188         return 1;
189     }
190
191     f = open(filename,O_RDONLY);
192
193     if (f<0)
194     { 
195         perror("Couldn't open file: ");
196         exit(1);
197     }
198     if FAILED(swf_ReadSWF(f,&swf))
199     { 
200         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
201         close(f);
202         exit(1);
203     }
204
205 #ifdef HAVE_STAT
206     fstat(f, &statbuf);
207     if(statbuf.st_size != swf.FileSize)
208         fprintf(stderr, "Error: Real Filesize (%d) doesn't match header Filesize (%d)",
209                 statbuf.st_size, swf.FileSize);
210 #endif
211
212     close(f);
213
214     xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
215     ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
216     if(xy)
217     {
218         if(xy&1)
219         printf("-X %d", xsize);
220
221         if((xy&1) && (xy&6))
222         printf(" ");
223
224         if(xy&2)
225         printf("-Y %d", ysize);
226         
227         if((xy&3) && (xy&4))
228         printf(" ");
229
230         if(xy&4)
231         printf("-r %d", swf.frameRate*100/256);
232         
233         printf("\n");
234         return 0;
235     }
236     if(html)
237     {
238         printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
239                " WIDTH=\"%d\"\n"
240                " HEIGHT=\"%d\"\n"
241                " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%d,0,0,0\">\n"
242                "  <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
243                "  <PARAM NAME=\"PLAY\" VALUE=\"true\">\n" 
244                "  <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
245                "  <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
246                "  <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n"
247                "   PLAY=\"true\" LOOP=\"true\" QUALITY=\"high\"\n"
248                "   TYPE=\"application/x-shockwave-flash\"\n"
249                "   PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">\n"
250                "  </EMBED>\n" 
251                "</OBJECT>\n", xsize, ysize, swf.fileVersion, filename, filename, xsize, ysize);
252         return 0;
253     } 
254     printf("[HEADER]        File version: %d\n", swf.fileVersion);
255     printf("[HEADER]        File size: %ld\n", swf.fileSize);
256     printf("[HEADER]        Frame rate: %f\n",swf.frameRate/256.0);
257     printf("[HEADER]        Frame count: %d\n",swf.frameCount);
258     printf("[HEADER]        Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
259     printf("[HEADER]        Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
260
261     tag = swf.firstTag;
262
263     while(tag) {
264         char*name = swf_TagGetName(tag);
265         char myprefix[128];
266         if(!name) {
267             fprintf(stderr, "Error: Unknown tag:0x%03x\n", tag->id);
268             tag = tag->next;
269             continue;
270         }
271         printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
272
273         if(swf_isDefiningTag(tag)) {
274             U16 id = swf_GetDefineID(tag);
275             printf(" defines id %04x", id);
276             if(idtab[id])
277                 fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);
278             idtab[id] = 1;
279         }
280         else if(tag->id == ST_PLACEOBJECT || 
281                 tag->id == ST_PLACEOBJECT2) {
282             printf(" places id %04x at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
283             if(swf_GetName(tag))
284                 printf(" name \"%s\"",swf_GetName(tag));
285         }
286         else if(tag->id == ST_REMOVEOBJECT) {
287             printf(" removes id %04x from depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
288         }
289         else if(tag->id == ST_REMOVEOBJECT2) {
290             printf(" removes object from depth %04x", swf_GetDepth(tag));
291         }
292         
293         printf("\n");
294         sprintf(myprefix, "                %s", prefix);
295
296         if(tag->id == ST_DEFINESPRITE) {
297             sprintf(prefix, "         ");
298         }
299         else if(tag->id == ST_END) {
300             *prefix = 0;
301         }
302         else if(tag->id == ST_DOACTION && action) {
303             ActionTAG*actions;
304             actions = swf_ActionGet(tag);
305             swf_DumpActions(actions, myprefix);
306         }
307         else if(tag->id == ST_DEFINEBUTTON && action) {
308             dumpButtonActions(tag, myprefix);
309         }
310         else if(tag->id == ST_DEFINEBUTTON2 && action) {
311             dumpButton2Actions(tag, myprefix);
312         }
313         tag = tag->next;
314     }
315
316     swf_FreeTags(&swf);
317     return 0;
318 }
319