resolved conflicts
[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 int action = 0;
37
38 struct options_t options[] =
39 {
40  {"a","action"},
41  {"v","verbose"},
42  {"V","version"},
43  {0,0}
44 };
45
46
47 int args_callback_option(char*name,char*val)
48 {
49     if(!strcmp(name, "V")) {
50         printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
51         exit(0);
52     } 
53     else if(name[0]=='a') {
54         action = 1;
55         return 0;
56     }
57     else {
58         printf("Unknown option: -%s\n", name);
59     }
60
61     return 0;
62 }
63 int args_callback_longoption(char*name,char*val)
64 {
65     return args_long2shortoption(options, name, val);
66 }
67 void args_callback_usage(char*name)
68 {    
69     printf("Usage: %s [-a] file.swf\n", name);
70     printf("-h , --help\t\t\t Print help and exit\n");
71     printf("-a , --action\t\t\t Disassemble action tags\n");
72     printf("-V , --version\t\t\t Print program version and exit\n");
73 }
74 int args_callback_command(char*name,char*val)
75 {
76     if(filename) {
77         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
78                  filename, name);
79     }
80     filename = name;
81     return 0;
82 }
83
84 char* what;
85 char* testfunc(char*str)
86 {
87     printf("%s: %s\n", what, str);
88     return 0;
89 }
90
91 int main (int argc,char ** argv)
92
93     SWF swf;
94     TAG*tag;
95 #ifdef HAVE_STAT
96     struct stat statbuf;
97 #endif
98     int f;
99     char prefix[128];
100     prefix[0] = 0;
101     memset(idtab,0,65536);
102
103     processargs(argc, argv);
104
105     if(!filename)
106     {
107         fprintf(stderr, "You must supply a filename.\n");
108         return 1;
109     }
110
111     f = open(filename,O_RDONLY);
112
113     if (f<0)
114     { 
115         perror("Couldn't open file: ");
116         exit(1);
117     }
118     if FAILED(swf_ReadSWF(f,&swf))
119     { 
120         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
121         close(f);
122         exit(1);
123     }
124
125 #ifdef HAVE_STAT
126     fstat(f, &statbuf);
127     if(statbuf.st_size != swf.FileSize)
128         fprintf(stderr, "Error: Real Filesize (%d) doesn't match header Filesize (%d)",
129                 statbuf.st_size, swf.FileSize);
130 #endif
131
132     close(f);
133
134     printf("[HEADER]        File version: %d\n", swf.fileVersion);
135     printf("[HEADER]        File size: %ld\n", swf.fileSize);
136     printf("[HEADER]        Frame rate: %f\n",swf.frameRate/256.0);
137     printf("[HEADER]        Frame count: %d\n",swf.frameCount);
138     printf("[HEADER]        Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
139     printf("[HEADER]        Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
140
141     tag = swf.firstTag;
142
143     while(tag) {
144         char*name = swf_GetTagName(tag);
145         if(!name) {
146             fprintf(stderr, "Error: Unknown tag:0x%03x\n", tag->id);
147             tag = tag->next;
148             continue;
149         }
150         printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_GetTagName(tag));
151
152         if(swf_isDefiningTag(tag)) {
153             U16 id = swf_GetDefineID(tag);
154             printf(" defines id %04x", id);
155             if(idtab[id])
156                 fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);
157             idtab[id] = 1;
158         }
159         else if(tag->id == ST_PLACEOBJECT || 
160                 tag->id == ST_PLACEOBJECT2) {
161             printf(" places id %04x at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
162             if(swf_TagGetName(tag))
163                 printf(" name \"%s\"",swf_TagGetName(tag));
164         }
165         else if(tag->id == ST_REMOVEOBJECT) {
166             printf(" removes id %04x from depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
167         }
168         else if(tag->id == ST_REMOVEOBJECT2) {
169             printf(" removes object from depth %04x", swf_GetDepth(tag));
170         }
171         
172         printf("\n");
173
174         if(tag->id == ST_DEFINESPRITE) {
175             sprintf(prefix, "         ");
176         }
177         else if(tag->id == ST_END) {
178             *prefix = 0;
179         }
180         else if(tag->id == ST_DOACTION && action) {
181             char myprefix[128];
182             ActionTAG*actions;
183             sprintf(myprefix, "                %s", prefix);
184             
185             actions = swf_GetActions(tag);
186
187             swf_DumpActions(actions, myprefix);
188
189 /*          what = "URL";
190             ActionEnumerateURLs(actions, testfunc);
191             what = "String";
192             ActionEnumerateStrings(actions, testfunc);
193             what = "Target";
194             ActionEnumerateTargets(actions, testfunc);*/
195         }
196         tag = tag->next;
197     }
198
199     swf_FreeTags(&swf);
200     return 0;
201 }
202