Initial revision
[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 "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 int args_callback_option(char*name,char*val)\r
23 {\r
24 }\r
25 int args_callback_longoption(char*name,char*val)\r
26 {\r
27 }\r
28 void args_callback_usage(char*name)\r
29 {    \r
30     printf("Usage: %s file.swf\n", name);\r
31     printf("\n");\r
32 }\r
33 int args_callback_command(char*name,char*val)\r
34 {\r
35     if(filename) {\r
36         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",\r
37                  filename, name);\r
38     }\r
39     filename = name;\r
40     return 0;\r
41 }\r
42   \r
43 int main (int argc,char ** argv)\r
44\r
45     SWF swf;\r
46     TAG*tag;\r
47     int f;\r
48     char prefix[128];\r
49     prefix[0] = 0;\r
50     memset(idtab,0,65536);\r
51 \r
52     processargs(argc, argv);\r
53 \r
54     f = open(filename,O_RDONLY);\r
55 \r
56     if (f<0)\r
57     { \r
58         perror("Couldn't open file: ");\r
59         exit(1);\r
60     }\r
61     if FAILED(ReadSWF(f,&swf))\r
62     { \r
63         fprintf(stderr,"%s is not a valid SWF file or contains errors.\n",filename);\r
64         close(f);\r
65         exit(1);\r
66     }\r
67     close(f);\r
68 \r
69     tag = swf.FirstTag;\r
70 \r
71     while(tag) {\r
72         printf("[%02x] %s%s", tag->id, prefix, getTagName(tag));\r
73         if(isDefiningTag(tag)) {\r
74             U16 id = GetDefineID(tag);\r
75             printf(" defines id %04x", id);\r
76             if(idtab[id])\r
77                 fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);\r
78             idtab[id] = 1;\r
79         }\r
80         else if(tag->id == ST_PLACEOBJECT || \r
81                 tag->id == ST_PLACEOBJECT2) {\r
82             printf(" places id %04x at depth %04x", GetPlaceID(tag), GetDepth(tag));\r
83             if(GetName(tag))\r
84                 printf(" name \"%s\"",GetName(tag));\r
85         }\r
86         else if(tag->id == ST_REMOVEOBJECT) {\r
87             printf(" removes id %04x from depth %04x", GetPlaceID(tag), GetDepth(tag));\r
88         }\r
89         else if(tag->id == ST_REMOVEOBJECT2) {\r
90             printf(" removes object from depth %04x", GetDepth(tag));\r
91         }\r
92         \r
93         printf("\n");\r
94 \r
95         if(tag->id == ST_DEFINESPRITE)\r
96         {\r
97             sprintf(prefix, "         ");\r
98         }\r
99         if(tag->id == ST_END) \r
100         {\r
101             sprintf(prefix, "");\r
102         }\r
103         tag = tag->next;\r
104     }\r
105 \r
106     FreeTags(&swf);\r
107     return 0;\r
108 }\r
109 \r