* Check filesize
[swftools.git] / src / swfdump.c
1 /* swfdump.c\r
2    Shows the structure of a swf file\r
3 \r
4    Part of the swftools package.\r
5    \r
6    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>\r
7 \r
8    This file is distributed under the GPL, see file COPYING for details */\r
9 \r
10 #include <sys/stat.h>\r
11 #include <sys/types.h>\r
12 #include <unistd.h>\r
13 #include <stdio.h>\r
14 #include <fcntl.h>\r
15 #include "../lib/rfxswf.h"\r
16 #include "../lib/args.h"\r
17 \r
18 char * filename = 0;\r
19 \r
20 /* idtab stores the ids which are defined in the file. This allows us\r
21    to detect errors in the file. (i.e. ids which are defined more than \r
22    once */\r
23 char idtab[65536];\r
24 \r
25 struct options_t options[] =\r
26 {\r
27  {"v","verbose"},\r
28  {"V","version"},\r
29  {0,0}\r
30 };\r
31 \r
32 \r
33 int args_callback_option(char*name,char*val)\r
34 {\r
35     if(!strcmp(name, "V")) {\r
36         printf("swfdump - part of %s %s\n", PACKAGE, VERSION);\r
37         exit(0);\r
38     }\r
39 }\r
40 int args_callback_longoption(char*name,char*val)\r
41 {\r
42     return args_long2shortoption(options, name, val);\r
43 }\r
44 void args_callback_usage(char*name)\r
45 {    \r
46     printf("Usage: %s file.swf\n", name);\r
47 }\r
48 int args_callback_command(char*name,char*val)\r
49 {\r
50     if(filename) {\r
51         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",\r
52                  filename, name);\r
53     }\r
54     filename = name;\r
55     return 0;\r
56 }\r
57   \r
58 int main (int argc,char ** argv)\r
59\r
60     SWF swf;\r
61     TAG*tag;\r
62     struct stat statbuf;\r
63     int f;\r
64     char prefix[128];\r
65     prefix[0] = 0;\r
66     memset(idtab,0,65536);\r
67 \r
68     processargs(argc, argv);\r
69 \r
70     f = open(filename,O_RDONLY);\r
71     fstat(f, &statbuf);\r
72 \r
73     if (f<0)\r
74     { \r
75         perror("Couldn't open file: ");\r
76         exit(1);\r
77     }\r
78     if FAILED(ReadSWF(f,&swf))\r
79     { \r
80         fprintf(stderr,"%s is not a valid SWF file or contains errors.\n",filename);\r
81         close(f);\r
82         exit(1);\r
83     }\r
84     close(f);\r
85 \r
86     printf("[HEADER]        File version: %d\n", swf.FileVersion);\r
87     printf("[HEADER]        File size: %d\n", swf.FileSize);\r
88     if(statbuf.st_size != swf.FileSize)\r
89         fprintf(stderr, "Error: Real Filesize (%d) doesn't match header Filesize (%d)",\r
90                 statbuf.st_size, swf.FileSize);\r
91     printf("[HEADER]        Frame rate: %f\n",swf.FrameRate/256.0);\r
92     printf("[HEADER]        Frame count: %d\n",swf.FrameCount);\r
93     printf("[HEADER]        Movie width: %.3f\n",(swf.MovieSize.xmax-swf.MovieSize.xmin)/20.0);\r
94     printf("[HEADER]        Movie height: %.3f\n",(swf.MovieSize.ymax-swf.MovieSize.ymin)/20.0);\r
95 \r
96     tag = swf.FirstTag;\r
97 \r
98     while(tag) {\r
99         printf("[%03x] %9d %s%s", tag->id, tag->len, prefix, getTagName(tag));\r
100         if(isDefiningTag(tag)) {\r
101             U16 id = GetDefineID(tag);\r
102             printf(" defines id %04x", id);\r
103             if(idtab[id])\r
104                 fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);\r
105             idtab[id] = 1;\r
106         }\r
107         else if(tag->id == ST_PLACEOBJECT || \r
108                 tag->id == ST_PLACEOBJECT2) {\r
109             printf(" places id %04x at depth %04x", GetPlaceID(tag), GetDepth(tag));\r
110             if(GetName(tag))\r
111                 printf(" name \"%s\"",GetName(tag));\r
112         }\r
113         else if(tag->id == ST_REMOVEOBJECT) {\r
114             printf(" removes id %04x from depth %04x", GetPlaceID(tag), GetDepth(tag));\r
115         }\r
116         else if(tag->id == ST_REMOVEOBJECT2) {\r
117             printf(" removes object from depth %04x", GetDepth(tag));\r
118         }\r
119         \r
120         printf("\n");\r
121 \r
122         if(tag->id == ST_DEFINESPRITE)\r
123         {\r
124             sprintf(prefix, "         ");\r
125         }\r
126         if(tag->id == ST_END) \r
127         {\r
128             sprintf(prefix, "");\r
129         }\r
130         tag = tag->next;\r
131     }\r
132 \r
133     FreeTags(&swf);\r
134     return 0;\r
135 }\r
136 \r