rfxswf cleanups: added prefixes and altered structure name conventions
[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 int main (int argc,char ** argv)
85
86     SWF swf;
87     TAG*tag;
88 #ifdef HAVE_STAT
89     struct stat statbuf;
90 #endif
91     int f;
92     char prefix[128];
93     prefix[0] = 0;
94     memset(idtab,0,65536);
95
96     processargs(argc, argv);
97
98     if(!filename)
99     {
100         fprintf(stderr, "You must supply a filename.\n");
101         return 1;
102     }
103
104     f = open(filename,O_RDONLY);
105
106     if (f<0)
107     { 
108         perror("Couldn't open file: ");
109         exit(1);
110     }
111     if FAILED(swf_ReadSWF(f,&swf))
112     { 
113         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
114         close(f);
115         exit(1);
116     }
117
118 #ifdef HAVE_STAT
119     fstat(f, &statbuf);
120     if(statbuf.st_size != swf.fileSize)
121         fprintf(stderr, "Error: Real Filesize (%d) doesn't match header Filesize (%d)",
122                 statbuf.st_size, swf.fileSize);
123 #endif
124
125     close(f);
126
127     printf("[HEADER]        File version: %d\n", swf.fileVersion);
128     printf("[HEADER]        File size: %ld\n", swf.fileSize);
129     printf("[HEADER]        Frame rate: %f\n",swf.frameRate/256.0);
130     printf("[HEADER]        Frame count: %d\n",swf.frameCount);
131     printf("[HEADER]        Movie width: %.3f\n",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
132     printf("[HEADER]        Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
133
134     tag = swf.firstTag;
135
136     while(tag) {
137         char*name = swf_TagGetName(tag);
138         if(!name) {
139             fprintf(stderr, "Error: Unknown tag:0x%03x\n", tag->id);
140             tag = tag->next;
141             continue;
142         }
143         printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
144
145         if(swf_isDefiningTag(tag)) {
146             U16 id = swf_GetDefineID(tag);
147             printf(" defines id %04x", id);
148             if(idtab[id])
149                 fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);
150             idtab[id] = 1;
151         }
152         else if(tag->id == ST_PLACEOBJECT || 
153                 tag->id == ST_PLACEOBJECT2) {
154             printf(" places id %04x at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
155             if(swf_TagGetName(tag))
156                 printf(" name \"%s\"",swf_TagGetName(tag));
157         }
158         else if(tag->id == ST_REMOVEOBJECT) {
159             printf(" removes id %04x from depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
160         }
161         else if(tag->id == ST_REMOVEOBJECT2) {
162             printf(" removes object from depth %04x", swf_GetDepth(tag));
163         }
164         
165         printf("\n");
166
167         if(tag->id == ST_DEFINESPRITE) {
168             sprintf(prefix, "         ");
169         }
170         else if(tag->id == ST_END) {
171             *prefix = 0;
172         }
173         else if(tag->id == ST_DOACTION && action) {
174             char myprefix[128];
175             ActionTAG*actions;
176             sprintf(myprefix, "                %s", prefix);
177             
178             actions = swf_GetActions(tag);
179
180             swf_DumpActions(actions, myprefix);
181         }
182         tag = tag->next;
183     }
184
185     swf_FreeTags(&swf);
186     return 0;
187 }
188