4 #include "swf_action.h"
6 swf_action_info_t swf_action_info_table[] = {
8 /* nothing 0x01 - 0x03 */
9 { 0x04, "Next Frame" },
10 { 0x05, "Previous Frame" },
13 { 0x08, "Toggle Quality" },
14 { 0x09, "Stop Sound" },
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" },
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" },
41 { 0x27, "Start Drag" },
42 { 0x28, "Stop Drag" },
43 { 0x29, "String Less Than"},
45 { 0x2B, "Cast Object" },
46 { 0x2C, "implements" },
47 { 0x2D, "FSCommand2" },
50 { 0x31, "String Length(multibyte)" },
53 { 0x34, "Get Timer" },
54 { 0x35, "SubString(multibyte)" },
55 { 0x36, "Ord(multibyte)" },
56 { 0x37, "Chr(multibyte)" },
59 { 0x3B, "Delete All" },
60 { 0x3C, "Set Local Variable" },
61 { 0x3D, "Call Function" },
65 { 0x41, "Declare Local Variable" },
66 { 0x42, "Declare Array" },
67 { 0x43, "Declare Object" },
69 { 0x45, "Get Targer" },
70 { 0x46, "Enumerate" },
71 { 0x47, "Add(typed)" },
72 { 0x48, "Less Than(typed)" },
73 { 0x49, "Equal(typed)" },
76 { 0x4C, "Duplicate" },
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 */
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)" },
97 /* nothing 0x6A - 0x80 */
98 { 0x81, "Goto Frame" },
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)"},
111 /* nothing 0x90 - 0x93 */
114 { 0x96, "Push Data" },
115 /* nothing 0x97 - 0x98 */
116 { 0x99, "Branch Always" },
117 { 0x9A, "Get URL2" },
118 { 0x9B, "Declare Function" },
120 { 0x9D, "Branch If True" },
121 { 0x9E, "Call Frame" },
122 { 0x9F, "Goto Expression" },
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]);
137 swf_action_parse(bitstream_t *bs, swf_action_t *act) {
138 unsigned long offset;
140 act->action_id = bitstream_getbyte(bs);
141 if (act->action_id & 0x80) {
142 act->action_has_length = 1;
144 act->action_has_length = 0;
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");
154 bitstream_getstring(bs, act->action_data, act->action_length);
160 swf_action_build(bitstream_t *bs, swf_action_t *act) {
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) {
168 bitstream_putbytesLE(bs, act->action_length, 2);
169 bitstream_putstring(bs, act->action_data, act->action_length);
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);
180 printf("0x%02x", act->action_id);
182 if (act->action_has_length) {
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);
191 case 0x88: // Declare Dictionary
192 d = act->action_data;
193 n = GetUShortLE(act->action_data); d += 2;
195 for (i=0 ; i < n ; i++) {
196 printf("\t\t[%d]'", i);
197 d += printf("%s", d) + 1;
201 case 0x96: // Push Data
202 swf_action_data_print(act->action_data, act->action_length);
205 printf(" len=%d", act->action_length);
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");
224 action = calloc(sizeof(*action), 1);
225 if (action == NULL) {
226 fprintf(stderr, "Can't alloc memory for action\n");
229 swf_action_parse(bs, action);
230 if (action_list->head == NULL) {
231 action_list->head = action_list->tail = action;
233 action_list->tail->next = action;
234 action_list->tail = action;
237 } while(action->action_id != 0); // End Action;
241 swf_action_list_output(swf_action_list_t *list, unsigned long *length) {
242 swf_action_t *action;
246 bs = bitstream_open();
247 for (action=list->head ; action ; action=action->next) {
248 swf_action_build(bs, action);
250 data = bitstream_steal(bs, length);
255 swf_action_list_destroy(swf_action_list_t *action_list) {
258 swf_action_t *action = action_list->head;
260 swf_action_t *action_next = action->next;
261 if (action->action_data) {
262 free(action->action_data);
265 action = action_next;
272 swf_action_list_print(swf_action_list_t *action_list) {
275 swf_action_t *action = action_list->head;
278 swf_action_print(action);
279 action = action->next;
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
291 printf("(String)%*s", data_len, data);
292 result += strlen((char*) data) + 1; // text + \0
295 printf("(Float)XXX");
300 case 0x03: // Undefined
301 printf("(Undefined)");
303 case 0x04: // Register
304 printf("(Register)%d", (data[0]&0xff));
306 case 0x05: // Boolean
307 printf("(Boolean)%s", (data[0]&0xff)?"true":"false");
311 printf("(Double)%f", GetDoubleIEEE(data));
313 case 0x07: // Integer
314 printf("(Integer)%ld", GetULongLE(data));
317 case 0x08: // Dictionary Lookup
318 printf("(Dictionary Lookup)%d", data[0] & 0xff);
321 case 0x09: // Large Dictionary Lookup
322 printf("(Large Dictionary Lookup)%d", GetUShortLE(data) & 0xffff);
326 printf("type=0x%02x len=%d", type, data_len);