OSDN Git Service

for 文の整形 (スペースを見やすいように調節)
[swfed/swfed.git] / src / swf_action.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "bitstream.h"
4 #include "swf_action.h"
5
6 swf_action_info_t swf_action_info_table[] = {
7     { 0x00, "End" },
8     /* nothing 0x01 - 0x03 */
9     { 0x04, "Next Frame" },
10     { 0x05, "Previous Frame" },
11     { 0x06, "Play" },
12     { 0x07, "Stop" },
13     { 0x08, "Toggle Quality" },
14     { 0x09, "Stop Sound" },
15     { 0x0A, "Add" },
16     { 0x0B, "Subtract" },
17     { 0x0C, "Multiply" },
18     { 0x0D, "Divide" },
19     { 0x0E, "Equal" },
20     { 0x0F, "Less Than" },
21     { 0x10, "Logical And" },
22     { 0x11, "Logical Or " },
23     { 0x12, "Logical Not" },
24     { 0x13, "String Equal" },
25     { 0x14, "String Length" },
26     { 0x15, "SubString" },
27     /* nothing 0x16 */
28     { 0x17, "Pop"},
29     { 0x18, "Integral Part"},
30     /* nothing 0x19 - 0x1B */
31     { 0x1C, "Get Variable"},
32     { 0x1D, "Set Variable"},
33     /* nothing 0x1E - 0x1F */
34     { 0x20, "Set Target" },
35     { 0x21, "Concatenate Strings"},
36     { 0x22, "Get Property" },
37     { 0x23, "Set Property" },
38     { 0x24, "Duplicate Sprite" },
39     { 0x25, "Remove Sprite" },
40     { 0x26, "Trace" },
41     { 0x27, "Start Drag" },
42     { 0x28, "Stop Drag" },
43     { 0x29, "String Less Than"},
44     { 0x2A, "Throw" },
45     { 0x2B, "Cast Object" },
46     { 0x2C, "implements" },
47     { 0x2D, "FSCommand2" },
48     /* nothing 0x2E-2F */
49     { 0x30, "Random" },
50     { 0x31, "String Length(multibyte)" },
51     { 0x32, "Ord" },
52     { 0x33, "Chr"},
53     { 0x34, "Get Timer" },
54     { 0x35, "SubString(multibyte)" },
55     { 0x36, "Ord(multibyte)" },
56     { 0x37, "Chr(multibyte)" },
57     /* nothing 0x28-29 */
58     { 0x3A, "Delete" },
59     { 0x3B, "Delete All" },
60     { 0x3C, "Set Local Variable" },
61     { 0x3D, "Call Function" },
62     { 0x3E, "Return" },
63     { 0x3F, "Modulo" },
64     { 0x40, "New" },
65     { 0x41, "Declare Local Variable" },
66     { 0x42, "Declare Array" },
67     { 0x43, "Declare Object" },
68     { 0x44, "Type Of" },
69     { 0x45, "Get Targer" },
70     { 0x46, "Enumerate" },
71     { 0x47, "Add(typed)" },
72     { 0x48, "Less Than(typed)" },
73     { 0x49, "Equal(typed)" },
74     { 0x4A, "Number" },
75     { 0x4B, "String" },
76     { 0x4C, "Duplicate" },
77     { 0x4D, "Swap" },
78     { 0x4E, "Get Member" },
79     { 0x4F, "Set Member" },
80     { 0x50, "Increment" },
81     { 0x51, "Decrement" },
82     { 0x52, "Call Method" },
83     { 0x53, "New Method" },
84     { 0x54, "Instance Of" },
85     { 0x55, "Enumerate Object" },
86     /* nothing 0x56 - 0x5F */
87     { 0x60, "And" },
88     { 0x61, "Or" },
89     { 0x62, "XOr" },
90     { 0x63, "Shift Left" },
91     { 0x64, "Shift Right" },
92     { 0x65, "Shift Right Unsigned" },
93     { 0x66, "Strict Equal" },
94     { 0x67, "Greater Than(typed)" },
95     { 0x68, "String Greater Than(typed)" },
96     { 0x69, "Extends" },
97     /* nothing 0x6A - 0x80 */
98     { 0x81, "Goto Frame" },
99     /* nothing 0x82 */
100     { 0x83, "Get URL" },
101     /* nothing 0x84 - 0x86 */
102     { 0x87, "Store Register" },
103     { 0x88, "Declare Dictionary" },
104     { 0x89, "Strict Mode" },
105     { 0x8A, "Wait For Frame" },
106     { 0x8B, "Set Target" },
107     { 0x8C, "Goto Label" },
108     { 0x8D, "Wait For Frame(dynamic)" },
109     { 0x8E, "Declare Function (with 256 registers)"},
110     { 0x8F, "Try"},
111     /* nothing 0x90 - 0x93 */
112     { 0x94, "With"},
113     /* nothing 0x95 */
114     { 0x96, "Push Data" },
115      /* nothing 0x97 - 0x98 */
116     { 0x99, "Branch Always" },
117     { 0x9A, "Get URL2" },
118     { 0x9B, "Declare Function" },
119     /* nothing 0x9C */
120     { 0x9D, "Branch If True" },
121     { 0x9E, "Call Frame" },
122     { 0x9F, "Goto Expression" },
123 };
124
125 swf_action_info_t *
126 get_swf_action_info(int action_id) {
127     int i, action_info_num = NumOfTable(swf_action_info_table);
128     for (i=0 ; i < action_info_num; i++) {
129         if (action_id == swf_action_info_table[i].id) {
130             return &(swf_action_info_table[i]);
131         }
132     }
133     return NULL;
134 }
135
136 int
137 swf_action_parse(bitstream_t *bs, swf_action_t *act) {
138     unsigned long offset;
139     bitstream_align(bs);
140     act->action_id = bitstream_getbyte(bs);
141     if (act->action_id & 0x80) {
142         act->action_has_length = 1;
143     } else {
144         act->action_has_length = 0;
145     }
146     if (act->action_has_length) {
147         act->action_length = bitstream_getbytesLE(bs, 2);
148         offset = bitstream_getbytepos(bs);
149         act->action_data = malloc(act->action_length);
150         if (act->action_data == NULL) {
151             fprintf(stderr, "Can't alloc memory for act->action_data\n");
152             return 1;
153         }
154         bitstream_getstring(bs, act->action_data, act->action_length);
155     }
156     return 0;
157 }
158
159 int
160 swf_action_build(bitstream_t *bs, swf_action_t *act) {
161     bitstream_align(bs);
162     /* bitstream_putbits(bs, act->action_has_length, 1);  no need */
163     bitstream_putbyte(bs, act->action_id);
164     if (act->action_has_length) {
165         if (act->action_data == NULL) {
166             return 1; // error
167         }
168         bitstream_putbytesLE(bs, act->action_length, 2);
169         bitstream_putstring(bs, act->action_data, act->action_length);
170     }
171     return 0;
172 }
173
174 int
175 swf_action_print(swf_action_t *act) {
176     swf_action_info_t *act_info = get_swf_action_info(act->action_id);
177     if (act_info && act_info->name) {
178         printf("%s", act_info->name);
179     } else {
180         printf("0x%02x", act->action_id);
181     }
182     if (act->action_has_length) {
183         int i, n;
184         unsigned char *d;
185         switch(act->action_id) {
186         case 0x83: // Get URL
187             printf(" (String)%s", act->action_data);
188             printf(" (String)%s",
189                    act->action_data + strlen(act->action_data) + 1);
190             break;
191         case 0x88: // Declare Dictionary
192             d = act->action_data;
193             n = GetUShortLE(act->action_data);  d += 2;
194             printf(":\n");
195             for (i=0 ; i < n ; i++) {
196                 printf("\t\t[%d]'", i);
197                 d += printf("%s", d) + 1;
198                 printf("'\n");
199             }
200             break;
201         case 0x96: // Push Data
202             swf_action_data_print(act->action_data, act->action_length);
203             break;
204         default:
205             printf(" len=%d", act->action_length);
206             break;
207         }
208     }
209     printf("\n");
210     return 0;
211 }
212
213
214 swf_action_list_t *
215 swf_action_list_create(bitstream_t *bs) {
216     swf_action_list_t *action_list;
217     swf_action_t *action;
218     action_list = calloc(sizeof(*action_list), 1);
219     if (action_list == NULL) {
220         fprintf(stderr, "Can't alloc memory for action_list\n");
221         return NULL;
222     }
223     do {
224         action = calloc(sizeof(*action), 1);
225         if (action == NULL) {
226             fprintf(stderr, "Can't alloc memory for action\n");
227             break;
228         }
229         swf_action_parse(bs, action);
230         if (action_list->head == NULL) {
231             action_list->head = action_list->tail = action;
232         } else {
233             action_list->tail->next = action;
234             action_list->tail = action;
235         }
236         action->next = NULL;
237     } while(action->action_id != 0); // End Action;
238     return action_list;
239 }
240 unsigned char *
241 swf_action_list_output(swf_action_list_t *list, unsigned long *length) {
242     swf_action_t *action;
243     bitstream_t *bs;
244     unsigned char *data;
245     *length = 0;
246     bs = bitstream_open();
247     for (action=list->head ; action ; action=action->next) {
248         swf_action_build(bs, action);
249     }
250     data = bitstream_steal(bs, length);
251     bitstream_close(bs);
252     return data;
253 }
254 void
255 swf_action_list_destroy(swf_action_list_t *action_list) {
256
257     if (action_list) {
258         swf_action_t *action = action_list->head;
259         while (action) {
260             swf_action_t *action_next = action->next;
261             if (action->action_data) {
262                 free(action->action_data);
263             }
264             free(action);
265             action = action_next;
266         }
267         free(action_list);
268     }
269 }
270
271 void
272 swf_action_list_print(swf_action_list_t *action_list) {
273     
274     if (action_list) {
275         swf_action_t *action = action_list->head;
276         while(action) {
277             printf("\t");
278             swf_action_print(action);
279             action = action->next;
280         }
281     }
282 }
283
284 int swf_action_data_print(unsigned char *action_data, unsigned short action_data_len) {
285     unsigned char type = action_data[0] & 0xff;
286     unsigned char *data = action_data+1;
287     unsigned short data_len = action_data_len - 1;
288     int result = 1; // type field
289     switch (type) {
290     case 0x00: // String
291         printf("(String)%*s", data_len, data);
292         result += strlen((char*) data) + 1; // text + \0
293         break;
294     case 0x01: // Float
295         printf("(Float)XXX");
296         break;
297     case 0x02: // NULL
298         printf("(NULL)");
299         break;
300     case 0x03: // Undefined
301         printf("(Undefined)");
302         break;
303     case 0x04: // Register
304         printf("(Register)%d", (data[0]&0xff));
305         break;
306     case 0x05: // Boolean
307         printf("(Boolean)%s", (data[0]&0xff)?"true":"false");
308         result += 1;
309         break;
310     case 0x06: // Double
311         printf("(Double)%f", GetDoubleIEEE(data));
312         break;
313     case 0x07: // Integer
314         printf("(Integer)%ld", GetULongLE(data));
315         result += 4;
316         break;
317     case 0x08: // Dictionary Lookup
318         printf("(Dictionary Lookup)%d", data[0] & 0xff);
319         result += 1;
320         break;
321     case 0x09: // Large Dictionary Lookup
322         printf("(Large Dictionary Lookup)%d", GetUShortLE(data) & 0xffff);
323         result += 2;
324         break;
325     default:
326         printf("type=0x%02x len=%d", type, data_len);
327         break;
328     }
329     return result;
330 }