added -X, -Y and -e options
[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  {"XY","size"},
45  {"e","html"},
46  {"v","verbose"},
47  {"V","version"},
48  {0,0}
49 };
50
51
52 int args_callback_option(char*name,char*val)
53 {
54     if(!strcmp(name, "V")) {
55         printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
56         exit(0);
57     } 
58     else if(name[0]=='a') {
59         action = 1;
60         return 0;
61     }
62     else if(name[0]=='e') {
63         html = 1;
64         return 0;
65     }
66     else if(name[0]=='X') {
67         xy |= 1;
68         return 0;
69     }
70     else if(name[0]=='Y') {
71         xy |= 2;
72         return 0;
73     }
74     else {
75         printf("Unknown option: -%s\n", name);
76     }
77
78     return 0;
79 }
80 int args_callback_longoption(char*name,char*val)
81 {
82     return args_long2shortoption(options, name, val);
83 }
84 void args_callback_usage(char*name)
85 {    
86     printf("Usage: %s [-a] file.swf\n", name);
87     printf("-h , --help\t\t\t Print help and exit\n");
88     printf("-e , --html\t\t\t Create a html embedding the file (simple, but useful)\n");
89     printf("-X , --width\t\t\t Prints out a string of the form \"-X width\"\n");
90     printf("-Y , --height\t\t\t Prints out a string of the form \"-Y height\"\n");
91     printf("-a , --action\t\t\t Disassemble action tags\n");
92     printf("-V , --version\t\t\t Print program version and exit\n");
93 }
94 int args_callback_command(char*name,char*val)
95 {
96     if(filename) {
97         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
98                  filename, name);
99     }
100     filename = name;
101     return 0;
102 }
103
104 char* what;
105 char* testfunc(char*str)
106 {
107     printf("%s: %s\n", what, str);
108     return 0;
109 }
110
111 int main (int argc,char ** argv)
112
113     SWF swf;
114     TAG*tag;
115 #ifdef HAVE_STAT
116     struct stat statbuf;
117 #endif
118     int f;
119     int xsize,ysize;
120     char prefix[128];
121     prefix[0] = 0;
122     memset(idtab,0,65536);
123
124     processargs(argc, argv);
125
126     if(!filename)
127     {
128         fprintf(stderr, "You must supply a filename.\n");
129         return 1;
130     }
131
132     f = open(filename,O_RDONLY);
133
134     if (f<0)
135     { 
136         perror("Couldn't open file: ");
137         exit(1);
138     }
139     if FAILED(swf_ReadSWF(f,&swf))
140     { 
141         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
142         close(f);
143         exit(1);
144     }
145
146 #ifdef HAVE_STAT
147     fstat(f, &statbuf);
148     if(statbuf.st_size != swf.FileSize)
149         fprintf(stderr, "Error: Real Filesize (%d) doesn't match header Filesize (%d)",
150                 statbuf.st_size, swf.FileSize);
151 #endif
152
153     close(f);
154
155     xsize = (swf.movieSize.xmax-swf.movieSize.xmin)/20;
156     ysize = (swf.movieSize.ymax-swf.movieSize.ymin)/20;
157     if(xy)
158     {
159         if(xy&1)
160         printf("-X %d", xsize);
161         if(xy==3)
162         printf(" ");
163         if(xy&2)
164         printf("-Y %d", ysize);
165         printf("\n");
166         return 0;
167     }
168     if(html)
169     {
170         printf("<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n"
171                " WIDTH=\"%d\"\n"
172                " HEIGHT=\"%d\"\n"
173                " CODEBASE=\"http://active.macromedia.com/flash5/cabs/swflash.cab#version=5,0,0,0\">\n"
174                "  <PARAM NAME=\"MOVIE\" VALUE=\"%s\">\n"
175                "  <PARAM NAME=\"PLAY\" VALUE=\"true\">\n" 
176                "  <PARAM NAME=\"LOOP\" VALUE=\"true\">\n"
177                "  <PARAM NAME=\"QUALITY\" VALUE=\"high\">\n"
178                "  <EMBED SRC=\"%s\" WIDTH=\"%d\" HEIGHT=\"%d\"\n"
179                "   PLAY=\"true\" LOOP=\"true\" QUALITY=\"high\"\n"
180                "   PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">\n"
181                "  </EMBED>\n" 
182                "</OBJECT>\n", xsize, ysize, filename, filename, xsize, ysize);
183         return 0;
184     } 
185     printf("[HEADER]        File version: %d\n", swf.fileVersion);
186     printf("[HEADER]        File size: %ld\n", swf.fileSize);
187     printf("[HEADER]        Frame rate: %f\n",swf.frameRate/256.0);
188     printf("[HEADER]        Frame count: %d\n",swf.frameCount);
189     printf("[HEADER]        Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
190     printf("[HEADER]        Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
191
192     tag = swf.firstTag;
193
194     while(tag) {
195         char*name = swf_TagGetName(tag);
196         if(!name) {
197             fprintf(stderr, "Error: Unknown tag:0x%03x\n", tag->id);
198             tag = tag->next;
199             continue;
200         }
201         printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
202
203         if(swf_isDefiningTag(tag)) {
204             U16 id = swf_GetDefineID(tag);
205             printf(" defines id %04x", id);
206             if(idtab[id])
207                 fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);
208             idtab[id] = 1;
209         }
210         else if(tag->id == ST_PLACEOBJECT || 
211                 tag->id == ST_PLACEOBJECT2) {
212             printf(" places id %04x at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
213             if(swf_GetName(tag))
214                 printf(" name \"%s\"",swf_GetName(tag));
215         }
216         else if(tag->id == ST_REMOVEOBJECT) {
217             printf(" removes id %04x from depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
218         }
219         else if(tag->id == ST_REMOVEOBJECT2) {
220             printf(" removes object from depth %04x", swf_GetDepth(tag));
221         }
222         
223         printf("\n");
224
225         if(tag->id == ST_DEFINESPRITE) {
226             sprintf(prefix, "         ");
227         }
228         else if(tag->id == ST_END) {
229             *prefix = 0;
230         }
231         else if(tag->id == ST_DOACTION && action) {
232             char myprefix[128];
233             ActionTAG*actions;
234             sprintf(myprefix, "                %s", prefix);
235             
236             actions = swf_GetActions(tag);
237
238             swf_DumpActions(actions, myprefix);
239
240 /*          what = "URL";
241             ActionEnumerateURLs(actions, testfunc);
242             what = "String";
243             ActionEnumerateStrings(actions, testfunc);
244             what = "Target";
245             ActionEnumerateTargets(actions, testfunc);*/
246         }
247         tag = tag->next;
248     }
249
250     swf_FreeTags(&swf);
251     return 0;
252 }
253