5 #define OP_STACK_ARGS 2
14 #define OP_NEED_ACTIVATION 512
15 #define OP_STACK_ARGS2 1024
17 /* 2 = u30 index into multiname
18 m = u30 index into method
19 n = u30 number of params
20 i = u30 index into method info
23 c = u30 index into class
24 s = string (u30 index into strings)
30 {0xa0, "add", "", -2, 1, 0, 0},
31 {0xc5, "add_i", "", -2, 1, 0, 0},
32 {0x86, "astype", "2", -1, 1, 0, 0},
33 {0x87, "astypelate", "", -2, 1, 0, 0},
34 {0xA8, "bitand", "", -2, 1, 0, 0},
35 {0x97, "bitnot", "" -1, 1, 0, 0},
36 {0xa9, "bitor", "", -2, 1, 0, 0},
37 {0xaa, "bitxor", "" -2, 1, 0, 0},
38 {0x41, "call", "n", -2, 1, 0, OP_STACK_ARGS},
39 {0x43, "callmethod", "mn", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
40 {0x4c, "callproplex", "2n", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
41 {0x46, "callproperty", "2n", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
42 {0x4f, "callpropvoid", "2n", -1, 0, 0, OP_STACK_ARGS|OP_STACK_NS},
43 {0x44, "callstatic", "in", -1, 1, 0, OP_STACK_ARGS},
44 {0x45, "callsuper", "2n", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
45 {0x4e, "callsupervoid", "2n", -1, 0, 0, OP_STACK_ARGS|OP_STACK_NS},
46 {0x78, "checkfilter", "", -1, 1, 0, 0},
47 {0x80, "coerce", "2", -1, 1, 0, 0},
48 {0x82, "coerce_a", "", -1, 1, 0, 0},
49 {0x85, "coerce_s", "", -1, 1, 0, 0},
50 {0x42, "construct", "n", -1, 1, 0, OP_STACK_ARGS},
51 {0x4a, "constructprop", "2n", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
52 {0x49, "constructsuper", "n", -1, 1, 0, OP_STACK_ARGS},
53 {0x76, "convert_b", "", -1, 1, 0, 0},
54 {0x73, "convert_i", "", -1, 1, 0, 0},
55 {0x75, "convert_d", "", -1, 1, 0, 0},
56 {0x77, "convert_o", "", -1, 1, 0, 0},
57 {0x74, "convert_u", "", -1, 1, 0, 0},
58 {0x70, "convert_s", "", -1, 1, 0, 0},
59 {0xef, "debug", "D", 0, 0, 0, 0},
60 {0xf1, "debugfile", "s", 0, 0, 0, 0},
61 {0xf0, "debugline", "u", 0, 0, 0, 0},
62 {0x94, "declocal", "r", 0, 0, 0, OP_REGISTER},
63 {0xc3, "declocal_i", "r", 0, 0, 0, OP_REGISTER},
64 {0x93, "decrement", "", -1, 1, 0, 0},
65 {0xc1, "decrement_i", "", -1, 1, 0, 0},
66 {0x6a, "deleteproperty", "2", -1, 1, 0, OP_STACK_NS},
67 {0xa3, "divide", "", -2, 1, 0, 0},
68 {0x2a, "dup", "", -1, 2, 0, 0},
69 {0x06, "dxns", "s", 0, 0, 0, OP_SET_DXNS},
70 {0x07, "dxnslate", "", -1, 0, 0, OP_SET_DXNS},
71 {0xab, "equals", "", -2, 1, 0, 0},
72 {0x72, "esc_xattr", "", -1, 1, 0, 0},
73 {0x71, "esc_xelem", "", -1, 1, 0, 0},
74 {0x5e, "findproperty", "2", 0, 1, 0, OP_STACK_NS},
75 {0x5d, "findpropstrict", "2", 0, 1, 0, OP_STACK_NS},
76 {0x59, "getdescendants", "2", -1, 1, 0, OP_STACK_NS},
77 {0x64, "getglobalscope", "", 0, 1, 0, 0},
78 {0x6e, "getglobalslot", "u", 0, 1, 0, 0},
79 {0x60, "getlex", "2", 0, 1, 0, 0}, //multiname may not be runtime
80 {0x62, "getlocal", "r", 0, 1, 0, OP_REGISTER},
81 {0xd0, "getlocal_0", "", 0, 1, 0, OP_REGISTER},
82 {0xd1, "getlocal_1", "", 0, 1, 0, OP_REGISTER},
83 {0xd2, "getlocal_2", "", 0, 1, 0, OP_REGISTER},
84 {0xd3, "getlocal_3", "", 0, 1, 0, OP_REGISTER},
85 {0x66, "getproperty", "2", -1, 1, 0, OP_STACK_NS},
86 {0x65, "getscopeobject", "u", 0, 1, 0, 0}, // u = index into scope stack
87 {0x6c, "getslot", "u", -1, 1, 0, 0},
88 {0x04, "getsuper", "2", -1, 1, 0, OP_STACK_NS},
89 {0xaf, "greaterequals", "", -2, 1, 0, 0},
90 {0x1f, "hasnext", "", -2, 1, 0, 0},
91 {0x32, "hasnext2", "rr", 0, 1, 0, OP_REGISTER},
92 {0x13, "ifeq", "j", -2, 0, 0, OP_BRANCH},
93 {0x12, "iffalse", "j", -1, 0, 0, OP_BRANCH},
94 {0x18, "ifge", "j", -2, 0, 0, OP_BRANCH},
95 {0x17, "ifgt", "j", -2, 0, 0, OP_BRANCH},
96 {0x16, "ifle", "j" -2, 0, 0, OP_BRANCH},
97 {0x15, "iflt", "j", -2, 0, 0, OP_BRANCH},
98 {0x0f, "ifnge", "j", -2, 0, 0, OP_BRANCH},
99 {0x0e, "ifngt", "j", -2, 0, 0, OP_BRANCH},
100 {0x0d, "ifnle", "j", -2, 0, 0, OP_BRANCH},
101 {0x0c, "ifnlt", "j", -2, 0, 0, OP_BRANCH},
102 {0x14, "ifne", "j", -2, 0, 0, OP_BRANCH},
103 {0x19, "ifstricteq", "j", -2, 0, 0, OP_BRANCH},
104 {0x1a, "ifstrictne", "j", -2, 0, 0, OP_BRANCH},
105 {0x11, "iftrue", "j", -1, 0, 0, OP_BRANCH},
106 {0xb4, "in", "", -2, 1, 0, 0},
107 {0x92, "inclocal", "r", 0, 0, 0, OP_REGISTER},
108 {0xc2, "inclocal_i", "r", 0, 0, 0, OP_REGISTER},
109 {0x91, "increment", "", -1, 1, 0, 0},
110 {0xc0, "increment_i", "", -1, 1, 0, 0},
111 {0x68, "initproperty", "2", -1, 0, 0, OP_STACK_NS},
112 {0xb1, "instanceof", "", -2, 1, 0, 0},
113 {0xb2, "istype", "2", -1, 1, 0, 0}, // may not be a runtime multiname
114 {0xb3, "istypelate", "", -2, 1, 0, 0},
115 {0x10, "jump", "j", 0, 0, 0, OP_JUMP},
116 {0x08, "kill", "r", 0, 0, 0, OP_REGISTER},
117 {0x09, "label", "", 0, 0, 0, OP_LABEL},
118 {0xae, "lessequals", "", -2, 1, 0, OP_BRANCH},
119 {0xad, "lessthan", "", -2, 1, 0, OP_BRANCH},
120 {0x1b, "lookupswitch", "S", -1, 0, 0, OP_SWITCH},
121 {0xa5, "lshift", "", -2, 1, 0, 0},
122 {0xa4, "modulo", "", -2, 1, 0, 0},
123 {0xa2, "multiply", "", -2, 1, 0, 0},
124 {0xc7, "multiply_i", "", -2, 1, 0, 0},
125 {0x90, "negate", "", -1, 1, 0, 0},
126 {0xc4, "negate_i", "", -1, 1, 0, 0},
127 {0x57, "newactivation", "", 0, 1, 0, OP_NEED_ACTIVATION},
128 {0x56, "newarray", "u", 0, 1, 0, OP_STACK_ARGS},
129 {0x5a, "newcatch", "u", 0, 1, 0, 0}, //u = index into exception_info
130 {0x58, "newclass", "c", -1, 1, 0, 0}, //c = index into class_info
131 {0x40, "newfunction", "m", 0, 1, 0, 0}, //i = index into method_info
132 {0x55, "newobject", "u", 0, 1, 0, OP_STACK_ARGS2},
133 {0x1e, "nextname", "", -2, 1, 0, 0},
134 {0x23, "nextvalue", "", -2, 1, 0, 0},
135 {0x02, "nop", "", 0, 0, 0, 0},
136 {0x96, "not", "", -1, 1, 0 ,0},
137 {0x29, "pop", "", -1, 0, 0, 0},
138 {0x1d, "popscope", "", 0, 0,-1, 0},
139 {0x24, "pushbyte", "b", 0, 1, 0, 0},
140 {0x2f, "pushdouble", "u", 0, 1, 0, 0}, //index into floats
141 {0x27, "pushfalse", "", 0, 1, 0, 0},
142 {0x2d, "pushint", "u", 0, 1, 0, 0}, //index into ints
143 {0x31, "pushnamespace", "u", 0, 1, 0, 0}, //index into namespace
144 {0x28, "pushnan", "", 0, 1, 0, 0},
145 {0x20, "pushnull", "", 0, 1, 0, 0},
146 {0x30, "pushscope", "", 0, 0, 1, 0},
147 {0x25, "pushshort", "u", 0, 1, 0, 0},
148 {0x2c, "pushstring", "s", 0, 1, 0, 0},
149 {0x26, "pushtrue", "", 0, 1, 0, 0},
150 {0x2e, "pushuint", "u", 0, 1, 0, 0}, //index into uints
151 {0x21, "pushundefined", "", 0, 1, 0, 0},
152 {0x1c, "pushwith", "", -1, 0, 1, 0},
153 {0x48, "returnvalue", "", -1, 0, 0, OP_RETURN},
154 {0x47, "returnvoid", "", 0, 0, 0, OP_RETURN},
155 {0xa6, "rshift", "", -2, 1, 0, 0},
156 {0x63, "setlocal", "r", -1, 0, 0, OP_REGISTER},
157 {0xd4, "setlocal_0", "", -1, 0, 0, OP_REGISTER},
158 {0xd5, "setlocal_1", "", -1, 0, 0, OP_REGISTER},
159 {0xd6, "setlocal_2", "", -1, 0, 0, OP_REGISTER},
160 {0xd7, "setlocal_3", "", -1, 0, 0, OP_REGISTER},
161 {0x6f, "setglobalslot", "u", -1, 0, 0, 0},
162 {0x61, "setproperty", "2", -2, 0, 0, OP_STACK_NS},
163 {0x6d, "setslot", "2", -2, 0, 0, 0},
164 {0x05, "setsuper", "2", -2, 0, 0, OP_STACK_NS},
165 {0xac, "strictequals", "", -2, 1, 0, 0},
166 {0xa1, "subtract", "", -2, 1, 0, 0},
167 {0xc6, "subtract_i", "", -2, 1, 0, 0},
168 {0x2b, "swap", "", -2, 2, 0, 0},
169 {0x03, "throw", "", -1, 0, 0, OP_THROW},
170 {0x95, "typeof", "", -1, 1, 0, 0},
171 {0xa7, "urshift", "", -2, 1, 0, 0},
172 {0xb0, "xxx", "", 0, 0, 0, 0},
175 static U8 op2index[256] = {254};
177 opcode_t* opcode_get(U8 op)
180 if(op2index[0]==254) {
181 memset(op2index, 255, sizeof(op2index));
182 for(t=0;t<sizeof(opcodes)/sizeof(opcodes[0]);t++) {
183 op2index[opcodes[t].opcode] = t;
186 if(op2index[op]!=255)
187 return &opcodes[op2index[op]];
191 abc_code_t*code_parse(TAG*tag, int len, abc_file_t*file, pool_t*pool)
195 int end=tag->pos+len;
196 //printf("-->\n");fflush(stdout);
197 while(tag->pos<end) {
198 U8 opcode = swf_GetU8(tag);
199 opcode_t*op = opcode_get(opcode);
201 fprintf(stderr, "Can't parse opcode %02x\n", opcode);
204 //printf("%s\n", op->name);fflush(stdout);
219 if(*p == 'n') { // number
220 data = (void*)(ptroff_t)swf_GetU30(tag);
221 } else if(*p == '2') { //multiname
222 data = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
223 } else if(*p == 'm') { //method
224 data = array_getvalue(file->methods, swf_GetU30(tag));
225 } else if(*p == 'c') { //classinfo
226 data = array_getvalue(file->classes, swf_GetU30(tag));
227 } else if(*p == 'i') {
228 data = array_getvalue(file->method_bodies, swf_GetU30(tag));
229 } else if(*p == 'u') { // generic integer
230 data = (void*)(ptroff_t)swf_GetU30(tag);
231 } else if(*p == 'r') { // local register
232 data = (void*)(ptroff_t)swf_GetU30(tag);
233 } else if(*p == 'b') { // byte
234 data = (void*)(ptroff_t)swf_GetU8(tag);
235 } else if(*p == 'j') { // jump TODO
236 data = (void*)(ptroff_t)swf_GetS24(tag);
237 } else if(*p == 's') { // string
238 data = strdup((char*)pool_lookup_string(pool, swf_GetU30(tag)));
239 } else if(*p == 'D') { // debug
241 U8 type = swf_GetU8(tag);
243 fprintf(stderr, "Unknown debug type: %02x\n", type);
245 code->params[0] = strdup((char*)pool_lookup_string(pool, swf_GetU30(tag)));
247 code->params[1] = (void*)(ptroff_t)swf_GetU8(tag);
250 } else if(*p == 'S') { // switch statement TODO
251 /* I hate these things */
252 swf_GetU24(tag); //default
253 int num = swf_GetU30(tag)+1;
259 printf("Can't parse opcode param type \"%c\"\n", *p);
263 code->params[pos++] = data;
270 void code_free(abc_code_t*c)
273 abc_code_t*next = c->next;
274 opcode_t*op = opcode_get(c->opcode);
275 char*p = op?op->params:"";
278 void*data = c->params[pos];
279 if(*p == '2') { //multiname
280 multiname_destroy(data);
281 } else if(strchr("sD", *p)) {
287 memset(c, 0, sizeof(c));
293 int code_dump(abc_code_t*c, abc_file_t*file, char*prefix, FILE*fo)
295 pool_t*pool = pool_new();
298 U8 opcode = c->opcode;
301 opcode_t*op = opcode_get(opcode);
303 fprintf(stderr, "Can't parse opcode %02x.\n", opcode);
306 fprintf(fo, "%s%s ", prefix, op->name);
311 void*data = c->params[pos];
316 int n = (ptroff_t)data;
317 fprintf(fo, "%d params", n);
318 } else if(*p == '2') {
319 multiname_t*n = (multiname_t*)data;
320 char* m = multiname_to_string(n);
321 fprintf(fo, "%s", m);
323 } else if(*p == 'm') {
324 abc_method_t*m = (abc_method_t*)data;
325 fprintf(fo, "[method %s]", m->name);
326 } else if(*p == 'c') {
327 abc_class_t*cls = (abc_class_t*)data;
328 char*classname = multiname_to_string(cls->classname);
329 fprintf(fo, "[classinfo %s]", classname);
331 } else if(*p == 'i') {
332 abc_method_body_t*b = (abc_method_body_t*)data;
333 fprintf(fo, "[methodbody]");
334 } else if(*p == 'u') {
335 int n = (ptroff_t)data;
336 fprintf(fo, "%d", n);
337 } else if(*p == 'r') {
338 int n = (ptroff_t)data;
339 fprintf(fo, "r%d", n);
340 } else if(*p == 'b') {
341 int b = (ptroff_t)data;
342 fprintf(fo, "%02x", b);
343 } else if(*p == 'j') {
344 int n = (ptroff_t)data;
345 fprintf(fo, "%d", n);
346 } else if(*p == 's') {
347 fprintf(fo, "\"%s\"", data);
348 } else if(*p == 'D') {
349 fprintf(fo, "[register %02x=%s]", (ptroff_t)c->params[1], (char*)c->params[0]);
350 } else if(*p == 'S') {
351 fprintf(fo, "[switch data]");
353 fprintf(stderr, "Can't parse opcode param type \"%c\"\n", *p);
367 int opcode_write(TAG*tag, abc_code_t*code, pool_t*pool, abc_file_t*file)
369 opcode_t*c = opcode_get(code->opcode);
375 swf_SetU8(tag, code->opcode);
379 void*data = code->params[pos++];
381 if(*p == 'n') { // number
382 len += swf_SetU30(tag, (ptroff_t)data);
383 } else if(*p == '2') { //multiname
384 multiname_t*m = (multiname_t*)data;
385 len += swf_SetU30(tag, pool_register_multiname(pool, m));
386 } else if(*p == 'm') { //method
387 abc_method_t*m = (abc_method_t*)data;
388 len += swf_SetU30(tag, m->index);
389 } else if(*p == 'c') { //classinfo
390 abc_class_t*cls = (abc_class_t*)data;
391 len += swf_SetU30(tag, cls->index);
392 } else if(*p == 'i') { //methodbody
393 abc_method_body_t*m = (abc_method_body_t*)data;
394 len += swf_SetU30(tag, m->index);
395 } else if(*p == 'u') { // integer
396 len += swf_SetU30(tag, (ptroff_t)data);
397 } else if(*p == 'r') { // integer
398 len += swf_SetU30(tag, (ptroff_t)data);
399 } else if(*p == 'b') { // byte
401 swf_SetU8(tag, (ptroff_t)data);
403 } else if(*p == 'j') { // jump
404 len += swf_SetS24(tag, (ptroff_t)data);
405 } else if(*p == 's') { // string
406 int index = pool_register_string(pool, data);
407 len += swf_SetU30(tag, index);
408 } else if(*p == 'D') { // debug statement
412 len+=swf_SetU30(tag, pool_register_string(pool,code->params[0]));
414 swf_SetU8(tag, (ptroff_t)code->params[1]);
416 len+=swf_SetU30(tag, 0);
417 } else if(*p == 'S') { // switch statement
418 len+=swf_SetU24(tag, 0); //default
419 len+=swf_SetU30(tag, 0); //nr-1
420 len+=swf_SetU24(tag, 0); //first
422 printf("Can't parse opcode param type \"%c\"\n", *p);
429 abc_code_t* add_opcode(abc_code_t*atag, U8 op)
431 abc_code_t*tmp = (abc_code_t*)malloc(sizeof(abc_code_t));
437 tmp->parent = atag->parent;