exit(0) after --version
[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 <stdio.h>\r
11 #include <fcntl.h>\r
12 #include "../lib/rfxswf.h"\r
13 #include "../lib/args.h"\r
14 \r
15 char * filename = 0;\r
16 \r
17 /* idtab stores the ids which are defined in the file. This allows us\r
18    to detect errors in the file. (i.e. ids which are defined more than \r
19    once */\r
20 char idtab[65536];\r
21 \r
22 struct options_t options[] =\r
23 {\r
24  {"v","verbose"},\r
25  {"V","version"},\r
26  {0,0}\r
27 };\r
28 \r
29 \r
30 int args_callback_option(char*name,char*val)\r
31 {\r
32     if(!strcmp(name, "V")) {\r
33         printf("swfdump - part of %s %s\n", PACKAGE, VERSION);\r
34         exit(0);\r
35     }\r
36 }\r
37 int args_callback_longoption(char*name,char*val)\r
38 {\r
39     return args_long2shortoption(options, name, val);\r
40 }\r
41 void args_callback_usage(char*name)\r
42 {    \r
43     printf("Usage: %s file.swf\n", name);\r
44 }\r
45 int args_callback_command(char*name,char*val)\r
46 {\r
47     if(filename) {\r
48         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",\r
49                  filename, name);\r
50     }\r
51     filename = name;\r
52     return 0;\r
53 }\r
54   \r
55 int main (int argc,char ** argv)\r
56\r
57     SWF swf;\r
58     TAG*tag;\r
59     int f;\r
60     char prefix[128];\r
61     prefix[0] = 0;\r
62     memset(idtab,0,65536);\r
63 \r
64     processargs(argc, argv);\r
65 \r
66     f = open(filename,O_RDONLY);\r
67 \r
68     if (f<0)\r
69     { \r
70         perror("Couldn't open file: ");\r
71         exit(1);\r
72     }\r
73     if FAILED(ReadSWF(f,&swf))\r
74     { \r
75         fprintf(stderr,"%s is not a valid SWF file or contains errors.\n",filename);\r
76         close(f);\r
77         exit(1);\r
78     }\r
79     close(f);\r
80 \r
81     tag = swf.FirstTag;\r
82 \r
83     while(tag) {\r
84         printf("[%02x] %s%s", tag->id, prefix, getTagName(tag));\r
85         if(isDefiningTag(tag)) {\r
86             U16 id = GetDefineID(tag);\r
87             printf(" defines id %04x", id);\r
88             if(idtab[id])\r
89                 fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);\r
90             idtab[id] = 1;\r
91         }\r
92         else if(tag->id == ST_PLACEOBJECT || \r
93                 tag->id == ST_PLACEOBJECT2) {\r
94             printf(" places id %04x at depth %04x", GetPlaceID(tag), GetDepth(tag));\r
95             if(GetName(tag))\r
96                 printf(" name \"%s\"",GetName(tag));\r
97         }\r
98         else if(tag->id == ST_REMOVEOBJECT) {\r
99             printf(" removes id %04x from depth %04x", GetPlaceID(tag), GetDepth(tag));\r
100         }\r
101         else if(tag->id == ST_REMOVEOBJECT2) {\r
102             printf(" removes object from depth %04x", GetDepth(tag));\r
103         }\r
104         \r
105         printf("\n");\r
106 \r
107         if(tag->id == ST_DEFINESPRITE)\r
108         {\r
109             sprintf(prefix, "         ");\r
110         }\r
111         if(tag->id == ST_END) \r
112         {\r
113             sprintf(prefix, "");\r
114         }\r
115         tag = tag->next;\r
116     }\r
117 \r
118     FreeTags(&swf);\r
119     return 0;\r
120 }\r
121 \r