added -r option
[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 int main (int argc,char ** argv)
119
120     SWF swf;
121     TAG*tag;
122 #ifdef HAVE_STAT
123     struct stat statbuf;
124 #endif
125     int f;
126     int xsize,ysize;
127     char prefix[128];
128     prefix[0] = 0;
129     memset(idtab,0,65536);
130
131     processargs(argc, argv);
132
133     if(!filename)
134     {
135         fprintf(stderr, "You must supply a filename.\n");
136         return 1;
137     }
138
139     f = open(filename,O_RDONLY);
140
141     if (f<0)
142     { 
143         perror("Couldn't open file: ");
144         exit(1);
145     }
146     if FAILED(swf_ReadSWF(f,&swf))
147     { 
148         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
149         close(f);
150         exit(1);
151     }
152
153 #ifdef HAVE_STAT
154     fstat(f, &statbuf);
155     if(statbuf.st_size != swf.FileSize)
156         fprintf(stderr, "Error: Real Filesize (%d) doesn't match header Filesize (%d)",
157                 statbuf.st_size, swf.FileSize);
158 #endif
159
160     close(f);
161
162     xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
163     ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
164     if(xy)
165     {
166         if(xy&1)
167         printf("-X %d", xsize);
168
169         if((xy&1) && (xy&6))
170         printf(" ");
171
172         if(xy&2)
173         printf("-Y %d", ysize);
174         
175         if((xy&3) && (xy&4))
176         printf(" ");
177
178         if(xy&4)
179         printf("-r %d", swf.frameRate*100/256);
180         
181         printf("\n");
182         return 0;
183     }
184     if(html)
185     {
186         printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
187                " WIDTH=\"%d\"\n"
188                " HEIGHT=\"%d\"\n"
189                " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=%d,0,0,0\">\n"
190                "  <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
191                "  <PARAM NAME=\"PLAY\" VALUE=\"true\">\n" 
192                "  <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
193                "  <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
194                "  <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n"
195                "   PLAY=\"true\" LOOP=\"true\" QUALITY=\"high\"\n"
196                "   PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">\n"
197                "  </EMBED>\n" 
198                "</OBJECT>\n", xsize, ysize, swf.fileVersion, filename, filename, xsize, ysize);
199         return 0;
200     } 
201     printf("[HEADER]        File version: %d\n", swf.fileVersion);
202     printf("[HEADER]        File size: %ld\n", swf.fileSize);
203     printf("[HEADER]        Frame rate: %f\n",swf.frameRate/256.0);
204     printf("[HEADER]        Frame count: %d\n",swf.frameCount);
205     printf("[HEADER]        Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
206     printf("[HEADER]        Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
207
208     tag = swf.firstTag;
209
210     while(tag) {
211         char*name = swf_TagGetName(tag);
212         if(!name) {
213             fprintf(stderr, "Error: Unknown tag:0x%03x\n", tag->id);
214             tag = tag->next;
215             continue;
216         }
217         printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
218
219         if(swf_isDefiningTag(tag)) {
220             U16 id = swf_GetDefineID(tag);
221             printf(" defines id %04x", id);
222             if(idtab[id])
223                 fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);
224             idtab[id] = 1;
225         }
226         else if(tag->id == ST_PLACEOBJECT || 
227                 tag->id == ST_PLACEOBJECT2) {
228             printf(" places id %04x at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
229             if(swf_GetName(tag))
230                 printf(" name \"%s\"",swf_GetName(tag));
231         }
232         else if(tag->id == ST_REMOVEOBJECT) {
233             printf(" removes id %04x from depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
234         }
235         else if(tag->id == ST_REMOVEOBJECT2) {
236             printf(" removes object from depth %04x", swf_GetDepth(tag));
237         }
238         
239         printf("\n");
240
241         if(tag->id == ST_DEFINESPRITE) {
242             sprintf(prefix, "         ");
243         }
244         else if(tag->id == ST_END) {
245             *prefix = 0;
246         }
247         else if(tag->id == ST_DOACTION && action) {
248             char myprefix[128];
249             ActionTAG*actions;
250             sprintf(myprefix, "                %s", prefix);
251             
252             actions = swf_GetActions(tag);
253
254             swf_DumpActions(actions, myprefix);
255
256 /*          what = "URL";
257             ActionEnumerateURLs(actions, testfunc);
258             what = "String";
259             ActionEnumerateStrings(actions, testfunc);
260             what = "Target";
261             ActionEnumerateTargets(actions, testfunc);*/
262         }
263         tag = tag->next;
264     }
265
266     swf_FreeTags(&swf);
267     return 0;
268 }
269