1 /* code.c
2 
3    Routines for handling Flash2 AVM2 ABC Actionscript
4 
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7 
8    Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
9 
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
23 
24 #include <assert.h>
25 #include "code.h"
26 #include "pool.h"
27 
28 #define OP_REGISTER 1
29 #define OP_STACK_ARGS 2
30 #define OP_STACK_NS 4
31 #define OP_SET_DXNS 8
32 #define OP_RETURN 16
33 #define OP_THROW 32
34 #define OP_BRANCH 64
35 #define OP_JUMP 128
36 #define OP_LABEL 256
37 #define OP_LOOKUPSWITCH 512
38 #define OP_NEED_ACTIVATION 1024
39 #define OP_STACK_ARGS2 2048
40 #define OP_INTERNAL 32768
41 
42 /* 2 = u30 index into multiname
43    m = u30 index into method
44    n = u30 number of params
45    i = u30 index into method info
46    b = byte
47    c = u30 index into class
48    N = u30 index into namespaces
49    s = string (u30 index into strings)
50    S = switch
51    u = u30
52    r = register
53 */
54 
55 opcode_t opcodes[]={
56 {0xa0, "add", "",              -2, 1, 0, 0},
57 {0xc5, "add_i", "",            -2, 1, 0, 0},
58 {0x86, "astype", "2",          -1, 1, 0, 0},
59 {0x87, "astypelate", "",       -2, 1, 0, 0},
60 {0xA8, "bitand", "",           -2, 1, 0, 0},
61 {0x97, "bitnot", "",           -1, 1, 0, 0},
62 {0xa9, "bitor", "",            -2, 1, 0, 0},
63 {0xaa, "bitxor", "",           -2, 1, 0, 0},
64 {0x41, "call", "n",            -2, 1, 0, OP_STACK_ARGS},
65 {0x43, "callmethod", "mn",     -1, 1, 0, OP_STACK_ARGS},
66 {0x4c, "callproplex", "2n",    -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
67 {0x46, "callproperty", "2n",   -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
68 {0x4f, "callpropvoid", "2n",   -1, 0, 0, OP_STACK_ARGS|OP_STACK_NS},
69 {0x44, "callstatic", "in",     -1, 1, 0, OP_STACK_ARGS},
70 {0x45, "callsuper", "2n",      -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
71 {0x4e, "callsupervoid", "2n",  -1, 0, 0, OP_STACK_ARGS|OP_STACK_NS},
72 {0x78, "checkfilter", "",      -1, 1, 0, 0},
73 {0x80, "coerce", "2",          -1, 1, 0, 0},
74 {0x82, "coerce_a", "",         -1, 1, 0, 0},
75 {0x85, "coerce_s", "",         -1, 1, 0, 0},
76 {0x42, "construct", "n",       -1, 1, 0, OP_STACK_ARGS},
77 {0x4a, "constructprop", "2n",  -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
78 {0x49, "constructsuper", "n",  -1, 0, 0, OP_STACK_ARGS},
79 {0x76, "convert_b", "",        -1, 1, 0, 0},
80 {0x73, "convert_i", "",        -1, 1, 0, 0},
81 {0x75, "convert_d", "",        -1, 1, 0, 0},
82 {0x77, "convert_o", "",        -1, 1, 0, 0},
83 {0x74, "convert_u", "",        -1, 1, 0, 0},
84 {0x70, "convert_s", "",        -1, 1, 0, 0},
85 {0xef, "debug", "D",            0, 0, 0, 0},
86 {0xf1, "debugfile", "s",        0, 0, 0, 0},
87 {0xf0, "debugline", "u",        0, 0, 0, 0},
88 {0x94, "declocal", "r",         0, 0, 0, OP_REGISTER},
89 {0xc3, "declocal_i", "r",       0, 0, 0, OP_REGISTER},
90 {0x93, "decrement", "",        -1, 1, 0, 0},
91 {0xc1, "decrement_i", "",      -1, 1, 0, 0},
92 {0x6a, "deleteproperty", "2",  -1, 1, 0, OP_STACK_NS},
93 {0xa3, "divide", "",           -2, 1, 0, 0},
94 {0x2a, "dup", "",              -1, 2, 0, 0},
95 {0x06, "dxns", "s",             0, 0, 0, OP_SET_DXNS},
96 {0x07, "dxnslate", "",         -1, 0, 0, OP_SET_DXNS},
97 {0xab, "equals", "",           -2, 1, 0, 0},
98 {0x72, "esc_xattr", "",        -1, 1, 0, 0},
99 {0x71, "esc_xelem", "",        -1, 1, 0, 0},
100 {0x5e, "findproperty", "2",     0, 1, 0, OP_STACK_NS},
101 {0x5d, "findpropstrict", "2",   0, 1, 0, OP_STACK_NS},
102 {0x59, "getdescendants", "2",  -1, 1, 0, OP_STACK_NS},
103 {0x64, "getglobalscope", "",    0, 1, 0, 0},
104 {0x6e, "getglobalslot", "u",    0, 1, 0, 0},
105 {0x60, "getlex", "2",           0, 1, 0, 0}, //multiname may not be runtime
106 {0x62, "getlocal", "r",         0, 1, 0, OP_REGISTER},
107 {0xd0, "getlocal_0", "",        0, 1, 0, OP_REGISTER},
108 {0xd1, "getlocal_1", "",        0, 1, 0, OP_REGISTER},
109 {0xd2, "getlocal_2", "",        0, 1, 0, OP_REGISTER},
110 {0xd3, "getlocal_3", "",        0, 1, 0, OP_REGISTER},
111 {0x66, "getproperty", "2",     -1, 1, 0, OP_STACK_NS},
112 {0x65, "getscopeobject", "u",   0, 1, 0, 0}, // u = index into scope stack
113 {0x6c, "getslot", "u",         -1, 1, 0, 0},
114 {0x04, "getsuper", "2",        -1, 1, 0, OP_STACK_NS},
115 {0xaf, "greaterthan", "",      -2, 1, 0, 0},
116 {0xb0, "greaterequals", "",    -2, 1, 0, 0},
117 {0x1f, "hasnext", "",          -2, 1, 0, 0},
118 {0x32, "hasnext2", "rr",        0, 1, 0, OP_REGISTER},
119 {0x13, "ifeq", "j",            -2, 0, 0, OP_BRANCH},
120 {0x12, "iffalse", "j",         -1, 0, 0, OP_BRANCH},
121 {0x18, "ifge", "j",            -2, 0, 0, OP_BRANCH},
122 {0x17, "ifgt", "j",            -2, 0, 0, OP_BRANCH},
123 {0x16, "ifle", "j",            -2, 0, 0, OP_BRANCH},
124 {0x15, "iflt", "j",            -2, 0, 0, OP_BRANCH},
125 {0x0f, "ifnge", "j",           -2, 0, 0, OP_BRANCH},
126 {0x0e, "ifngt", "j",           -2, 0, 0, OP_BRANCH},
127 {0x0d, "ifnle", "j",           -2, 0, 0, OP_BRANCH},
128 {0x0c, "ifnlt", "j",           -2, 0, 0, OP_BRANCH},
129 {0x14, "ifne", "j",            -2, 0, 0, OP_BRANCH},
130 {0x19, "ifstricteq", "j",      -2, 0, 0, OP_BRANCH},
131 {0x1a, "ifstrictne", "j",      -2, 0, 0, OP_BRANCH},
132 {0x11, "iftrue", "j",          -1, 0, 0, OP_BRANCH},
133 {0xb4, "in", "",               -2, 1, 0, 0},
134 {0x92, "inclocal", "r",         0, 0, 0, OP_REGISTER},
135 {0xc2, "inclocal_i", "r",       0, 0, 0, OP_REGISTER},
136 {0x91, "increment", "",        -1, 1, 0, 0},
137 {0xc0, "increment_i", "",      -1, 1, 0, 0},
138 {0x68, "initproperty", "2",    -2, 0, 0, OP_STACK_NS},
139 {0xb1, "instanceof", "",       -2, 1, 0, 0},
140 {0xb2, "istype", "2",          -1, 1, 0, 0}, // may not be a runtime multiname
141 {0xb3, "istypelate", "",       -2, 1, 0, 0},
142 {0x10, "jump", "j",             0, 0, 0, OP_JUMP},
143 {0x08, "kill", "r",             0, 0, 0, OP_REGISTER},
144 {0x09, "label", "",             0, 0, 0, OP_LABEL},
145 {0xae, "lessequals", "",       -2, 1, 0, 0},
146 {0xad, "lessthan", "",         -2, 1, 0, 0},
147 {0x1b, "lookupswitch", "S",    -1, 0, 0, OP_LOOKUPSWITCH},
148 {0xa5, "lshift", "",           -2, 1, 0, 0},
149 {0xa4, "modulo", "",           -2, 1, 0, 0},
150 {0xa2, "multiply", "",         -2, 1, 0, 0},
151 {0xc7, "multiply_i", "",       -2, 1, 0, 0},
152 {0x90, "negate", "",           -1, 1, 0, 0},
153 {0xc4, "negate_i", "",         -1, 1, 0, 0},
154 {0x57, "newactivation", "",     0, 1, 0, OP_NEED_ACTIVATION},
155 {0x56, "newarray", "n",         0, 1, 0, OP_STACK_ARGS},
156 {0x5a, "newcatch", "u",         0, 1, 0, 0}, //u = index into exception_info
157 {0x58, "newclass", "c",        -1, 1, 0, 0}, //c = index into class_info
158 {0x40, "newfunction", "m",      0, 1, 0, 0}, //i = index into method_info
159 {0x55, "newobject", "n",        0, 1, 0, OP_STACK_ARGS2},
160 {0x1e, "nextname", "",         -2, 1, 0, 0},
161 {0x23, "nextvalue", "",        -2, 1, 0, 0},
162 {0x02, "nop", "",               0, 0, 0, 0},
163 {0x96, "not", "",              -1, 1, 0 ,0},
164 {0x29, "pop", "",              -1, 0, 0, 0},
165 {0x1d, "popscope", "",          0, 0,-1, 0},
166 {0x24, "pushbyte", "b",         0, 1, 0, 0},
167 {0x2f, "pushdouble", "f",       0, 1, 0, 0}, //index into floats
168 {0x27, "pushfalse", "",         0, 1, 0, 0},
169 {0x2d, "pushint", "I",          0, 1, 0, 0}, //index into ints
170 {0x31, "pushnamespace", "N",    0, 1, 0, 0}, //index into namespace
171 {0x28, "pushnan", "",           0, 1, 0, 0},
172 {0x20, "pushnull", "",          0, 1, 0, 0},
173 {0x30, "pushscope", "",        -1, 0, 1, 0},
174 {0x25, "pushshort", "u",        0, 1, 0, 0},
175 {0x2c, "pushstring", "s",       0, 1, 0, 0},
176 {0x26, "pushtrue", "",          0, 1, 0, 0},
177 {0x2e, "pushuint", "U",         0, 1, 0, 0}, //index into uints
178 {0x21, "pushundefined", "",     0, 1, 0, 0},
179 {0x1c, "pushwith", "",         -1, 0, 1, 0},
180 {0x48, "returnvalue", "",      -1, 0, 0, OP_RETURN},
181 {0x47, "returnvoid", "",        0, 0, 0, OP_RETURN},
182 {0xa6, "rshift", "",           -2, 1, 0, 0},
183 {0x63, "setlocal", "r",        -1, 0, 0, OP_REGISTER},
184 {0xd4, "setlocal_0", "",       -1, 0, 0, OP_REGISTER},
185 {0xd5, "setlocal_1", "",       -1, 0, 0, OP_REGISTER},
186 {0xd6, "setlocal_2", "",       -1, 0, 0, OP_REGISTER},
187 {0xd7, "setlocal_3", "",       -1, 0, 0, OP_REGISTER},
188 {0x6f, "setglobalslot", "u",   -1, 0, 0, 0},
189 {0x61, "setproperty", "2",     -2, 0, 0, OP_STACK_NS},
190 {0x6d, "setslot", "u",         -2, 0, 0, 0},
191 {0x05, "setsuper", "2",        -2, 0, 0, OP_STACK_NS},
192 {0xac, "strictequals", "",     -2, 1, 0, 0},
193 {0xa1, "subtract", "",         -2, 1, 0, 0},
194 {0xc6, "subtract_i", "",       -2, 1, 0, 0},
195 {0x2b, "swap", "",             -2, 2, 0, 0},
196 {0x03, "throw", "",            -1, 0, 0, OP_THROW},
197 {0x95, "typeof", "",           -1, 1, 0, 0},
198 {0xa7, "urshift", "",          -2, 1, 0, 0},
199 
200 /* Alchemy opcodes */
201 {0x3a, "si8", "",                -2, 0, 0, 0},
202 {0x3b, "si16", "",               -2, 0, 0, 0},
203 {0x3c, "si32", "",               -2, 0, 0, 0},
204 {0x3d, "sf32", "",               -2, 0, 0, 0},
205 {0x3e, "sf64", "",               -2, 0, 0, 0},
206 {0x35, "li8", "",                -1, 1, 0, 0},
207 {0x36, "li16", "",               -1, 1, 0, 0},
208 {0x37, "li32", "",               -1, 1, 0, 0},
209 {0x38, "lf32", "",               -1, 1, 0, 0},
210 {0x39, "lf64", "",               -1, 1, 0, 0},
211 {0x50, "sxi1", "",               -1, 1, 0, 0},
212 {0x51, "sxi8", "",               -1, 1, 0, 0},
213 {0x52, "sxi16", "",              -1, 1, 0, 0},
214 
215 /* opcodes not documented, but seen in the wild */
216 {0x53, "applytype", "n",       -1, 1, 0, OP_STACK_ARGS}, //seen in builtin.abc
217 
218 /* dummy instructions. Warning: these are not actually supported by flash */
219 {0xfb, "__pushpackage__", "s",      0, 1, 0, OP_INTERNAL},
220 {0xfc, "__rethrow__", "",           0, 0, 0, OP_THROW|OP_INTERNAL},
221 {0xfd, "__fallthrough__", "s",      0, 0, 0, OP_INTERNAL},
222 {0xfe, "__continue__", "s",         0, 0, 0, OP_RETURN|OP_INTERNAL},
223 {0xff, "__break__", "s",            0, 0, 0, OP_RETURN|OP_INTERNAL},
224 };
225 
226 static opcode_t* op2op[256] = {0,0,0};
227 
opcode_get(U8 op)228 static inline opcode_t* opcode_get(U8 op)
229 {
230     int t;
231     if(!op2op[0x02]) {
232         memset(op2op, 0, sizeof(op2op));
233         for(t=0;t<sizeof(opcodes)/sizeof(opcodes[0]);t++) {
234             op2op[opcodes[t].opcode] = &opcodes[t];
235         }
236     }
237     return op2op[op];
238 }
239 
pos2code(code_t ** bytepos,code_t * c,int pos,int len)240 static code_t*pos2code(code_t**bytepos, code_t*c, int pos, int len)
241 {
242     if(c) {
243         pos+=c->pos;
244     }
245     if(pos < 0 ||
246        pos > len ||
247       (pos!=len && !bytepos[pos])) {
248         /* flex likes to generate these. yuck. */
249         if(c) {
250             opcode_t*op = opcode_get(c->opcode);
251             fprintf(stderr, "Warning: Invalid jump instruction \"%s\" from %d to %d (%d)\n", op->name, c->pos, pos, len);
252         } else {
253             fprintf(stderr, "Warning: Invalid jump to %d (%d)\n", pos, len);
254         }
255         return 0;
256     } else {
257         if(pos==len) {
258             //opcode_t*op = opcode_get(c->opcode);
259             //fprintf(stderr, "Warning: jump beyond end of code in instruction %s at position %d\n", op->name, c->pos);
260             return 0;
261         } else {
262             return bytepos[pos];
263         }
264     }
265 }
code_atposition(codelookup_t * l,int pos)266 code_t* code_atposition(codelookup_t*l, int pos)
267 {
268     return pos2code(l->bytepos, 0, pos, l->len);
269 }
270 
lookupswitch_print(lookupswitch_t * l)271 void lookupswitch_print(lookupswitch_t*l)
272 {
273     printf("default: %p\n", l->def);
274     code_list_t*t = l->targets;
275     while(t) {
276         printf("target: %p\n", t->code);
277         t = t->next;
278     }
279 }
280 
code_parse(TAG * tag,int len,abc_file_t * file,pool_t * pool,codelookup_t ** codelookup)281 code_t*code_parse(TAG*tag, int len, abc_file_t*file, pool_t*pool, codelookup_t**codelookup)
282 {
283     code_t*head=0;
284     code_t*code=0;
285     int start=tag->pos;
286     int end=tag->pos+len;
287     //printf("-->\n");fflush(stdout);
288 
289     code_t**bytepos = rfx_calloc(sizeof(code_t*)*len);
290 
291     while(tag->pos<end) {
292         int codepos = tag->pos-start;
293         U8 opcode = swf_GetU8(tag);
294         opcode_t*op = opcode_get(opcode);
295 	if(!op) {
296 	    fprintf(stderr, "Can't parse opcode %02x\n", opcode);
297 	    continue;
298         }
299         //printf("%s\n", op->name);fflush(stdout);
300         NEW(code_t,c);
301         c->pos = codepos;
302 
303         bytepos[codepos] = c;
304 
305         if(!head) {
306             head = code = c;
307         } else {
308             code->next = c;
309             c->prev = code;
310             code = c;
311         }
312 
313         c->opcode = opcode;
314 
315         char*p = op->params;
316         int pos = 0;
317         while(*p) {
318             void*data = 0;
319             if(*p == 'n') { // number
320                 data = (void*)(ptroff_t)swf_GetU30(tag);
321             } else if(*p == '2') { //multiname
322                 data = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
323             } else if(*p == 'N') { //namespace
324                 data = namespace_clone(pool_lookup_namespace(pool, swf_GetU30(tag)));
325             } else if(*p == 'U') { //uint
326                 data = (void*)(ptroff_t)pool_lookup_uint(pool, swf_GetU30(tag));
327             } else if(*p == 'I') { //int
328                 data = (void*)(ptroff_t)pool_lookup_int(pool, swf_GetU30(tag));
329             } else if(*p == 'f') { //int
330                 double*fp = malloc(sizeof(double));
331                 *fp = pool_lookup_float(pool, swf_GetU30(tag));
332                 data = fp;
333             } else if(*p == 'm') { //method
334                 data = array_getvalue(file->methods, swf_GetU30(tag));
335             } else if(*p == 'c') { //classinfo
336                 data = array_getvalue(file->classes, swf_GetU30(tag));
337             } else if(*p == 'i') {
338                 data = array_getvalue(file->method_bodies, swf_GetU30(tag));
339             } else if(*p == 'u') { // generic integer
340                 data = (void*)(ptroff_t)swf_GetU30(tag);
341             } else if(*p == 'r') { // local register
342                 data = (void*)(ptroff_t)swf_GetU30(tag);
343             } else if(*p == 'b') { // byte
344                 data = (void*)(ptroff_t)swf_GetU8(tag);
345             } else if(*p == 'j') { // jump
346                 int j = swf_GetS24(tag);
347                 data = (void*)(ptroff_t)j;
348             } else if(*p == 's') { // string
349                 string_t s = pool_lookup_string2(pool, swf_GetU30(tag));
350                 data = string_dup3(&s);
351             } else if(*p == 'D') { // debug
352                 /*type, usually 1*/
353                 U8 type = swf_GetU8(tag);
354                 if(type!=1)
355                     fprintf(stderr, "Unknown debug type: %02x\n", type);
356                 /*register name*/
357                 code->data[0] = strdup((char*)pool_lookup_string(pool, swf_GetU30(tag)));
358                 /*register index*/
359                 code->data[1] = (void*)(ptroff_t)swf_GetU8(tag);
360                 /*unused*/
361                 swf_GetU30(tag);
362             } else if(*p == 'S') { // switch statement
363                 lookupswitch_t*l = malloc(sizeof(lookupswitch_t));
364                 l->def = (code_t*)(ptroff_t)swf_GetS24(tag);
365                 l->targets = list_new();
366                 int num = swf_GetU30(tag)+1;
367                 int t;
368                 for(t=0;t<num;t++)
369                     list_append(l->targets, (code_t*)(ptroff_t)swf_GetS24(tag));
370                 data = l;
371             } else {
372                 printf("Can't parse opcode param type \"%c\" (for op %02x %s).\n", *p, code->opcode, op->name);
373                 return 0;
374             }
375             code->data[pos++] = data;
376             p++;
377         }
378     }
379 
380 //#define DEBUG_BYTES
381 #ifdef DEBUG_BYTES
382     int t;
383     for(t=0;t<len;t++) {
384         code_t*c = bytepos[t];
385         if(c) {
386             opcode_t*op = opcode_get(c->opcode);
387             if(op->flags & (OP_JUMP|OP_BRANCH)) {
388                 printf("%50d) %02x %s %d\n", t, tag->data[start+t], op->name, c->data[0]);
389             } else {
390                 printf("%50d) %02x %s\n", t, tag->data[start+t], op->name);
391             }
392         } else {
393             printf("%50d) %02x\n", t, tag->data[start+t]);
394         }
395     }
396     //printf("%05d) %02x\n", t, tag->data[start+t]);
397 #endif
398 
399     code_t*c = head;
400     while(c) {
401         opcode_t*op = opcode_get(c->opcode);
402         if(op->flags & (OP_JUMP|OP_BRANCH)) {
403             int j = ((int)(ptroff_t)c->data[0]);
404             c->branch = pos2code(bytepos,c,j+4,len);
405         } else if(op->flags & (OP_LOOKUPSWITCH)) {
406             lookupswitch_t*l = (lookupswitch_t*)c->data[0];
407             int offset = 0;
408             l->def = pos2code(bytepos,c,(ptroff_t)l->def+offset,len);
409             code_list_t*t=l->targets;
410             while(t) {
411                 t->code = pos2code(bytepos,c,(ptroff_t)t->code+offset,len);
412                 t = t->next;
413             }
414         }
415         c = c->next;
416     }
417 
418     if(codelookup) {
419         (*codelookup) = malloc(sizeof(codelookup_t));
420         (*codelookup)->bytepos = bytepos;
421         (*codelookup)->len = len;
422     } else {
423         free(bytepos);
424     }
425 
426     return head;
427 }
428 
codelookup_free(codelookup_t * codelookup)429 void codelookup_free(codelookup_t*codelookup)
430 {
431     free(codelookup->bytepos);codelookup->bytepos=0;
432     free(codelookup);
433 }
434 
code_free(code_t * c)435 void code_free(code_t*c)
436 {
437     c = code_start(c);
438     while(c) {
439         code_t*next = c->next;
440         opcode_t*op = opcode_get(c->opcode);
441         char*p = op?op->params:"";
442         int pos=0;
443         while(*p) {
444             void*data = c->data[pos];
445             if(*p == '2') { //multiname
446                 multiname_destroy(data);
447             } else if(*p == 'N') { //namespace
448                 namespace_destroy(data);
449             } else if(strchr("sDf", *p)) {
450                 free(data);
451             } else if(strchr("S", *p)) {
452                 lookupswitch_t*l = (lookupswitch_t*)data;
453                 list_free(l->targets);l->targets=0;
454                 free(l);
455             }
456             c->data[pos]=0;
457             p++;pos++;
458         }
459         memset(c, 0, sizeof(c));
460         free(c);
461         c = next;
462     }
463 }
464 
opcode_write(TAG * tag,code_t * c,pool_t * pool,abc_file_t * file,int length)465 static int opcode_write(TAG*tag, code_t*c, pool_t*pool, abc_file_t*file, int length)
466 {
467     opcode_t*op = opcode_get(c->opcode);
468     char*p = op->params;
469     int pos = 0;
470     int len = 0;
471 
472     if(tag)
473         swf_SetU8(tag, c->opcode);
474     len++;
475 
476     if(op->flags & OP_INTERNAL) {
477         if(c->opcode == OPCODE___BREAK__ ||
478            c->opcode == OPCODE___CONTINUE__) {
479             fprintf(stderr, "Unresolved %s\n", op->name);
480         } else {
481             fprintf(stderr, "Error: writing undefined internal opcode %s\n", op->name);
482         }
483     }
484 
485     while(*p) {
486         void*data = c->data[pos++];
487         assert(pos<=2);
488         switch(*p) {
489             case 'n': { // number
490                 len += swf_SetU30(tag, (ptroff_t)data);
491                 break;
492             }
493             case '2': { //multiname
494                 multiname_t*m = (multiname_t*)data;
495                 len += swf_SetU30(tag, pool_register_multiname(pool, m));
496                 break;
497             }
498             case 'N': { //namespace
499                 namespace_t*ns = (namespace_t*)data;
500                 len += swf_SetU30(tag, pool_register_namespace(pool, ns));
501                 break;
502             }
503             case 'm': { //method
504                 abc_method_t*m = (abc_method_t*)data;
505                 len += swf_SetU30(tag, m->index);
506                 break;
507             }
508             case 'c': { //classinfo
509                 abc_class_t*cls = (abc_class_t*)data;
510                 len += swf_SetU30(tag, cls->index);
511                 break;
512             }
513             case 'i': { //methodbody
514                 abc_method_body_t*m = (abc_method_body_t*)data;
515                 len += swf_SetU30(tag, m->index);
516                 break;
517             }
518             case 'I': { // int
519                 len += swf_SetU30(tag, pool_register_int(pool, (ptroff_t)data));
520                 break;
521             }
522             case 'U': { // uint
523                 len += swf_SetU30(tag, pool_register_uint(pool, (ptroff_t)data));
524                 break;
525             }
526             case 'f': { //  float
527                 len += swf_SetU30(tag, pool_register_float(pool, *(double*)data));
528                 break;
529             }
530             case 'u': { // integer
531                 len += swf_SetU30(tag, (ptroff_t)data);
532                 break;
533             }
534             case 'r': { // integer
535                 len += swf_SetU30(tag, (ptroff_t)data);
536                 break;
537             }
538             case 'b': { // byte
539                 if(tag)
540                     swf_SetU8(tag, (ptroff_t)data);
541                 len++;
542                 break;
543             }
544             case 'j': { // jump
545                 int skip = length-c->pos-4;
546                 if(c->branch)
547                     skip = (c->branch->pos) - c->pos - 4;
548                 len += swf_SetS24(tag, skip);
549                 break;
550             }
551             case 's': { // string
552                 int index = pool_register_string2(pool, (string_t*)data);
553                 len += swf_SetU30(tag, index);
554                 break;
555             }
556             case 'D': { // debug statement
557                 if(tag)
558                     swf_SetU8(tag, 1);
559                 len++;
560                 len+=swf_SetU30(tag, pool_register_string(pool,c->data[0]));
561                 if(tag)
562                     swf_SetU8(tag, (ptroff_t)c->data[1]);
563                 len++;
564                 len+=swf_SetU30(tag, 0);
565                 break;
566             }
567             case 'S': { // switch statement
568                 lookupswitch_t*l = (lookupswitch_t*)data;
569                 int offset = 0;
570                 len+=swf_SetS24(tag, l->def->pos-c->pos+offset); //default
571                 code_list_t*t = l->targets;
572                 if(list_length(t)) {
573                     len+=swf_SetU30(tag, list_length(t)-1); //nr-1
574                     code_list_t*t = l->targets;
575                     while(t) {
576                         len+=swf_SetS24(tag, t->code->pos - c->pos+offset);
577                         t = t->next;
578                     }
579                 } else {
580                     len+=swf_SetU30(tag, 0); //nr-1
581                     len+=swf_SetS24(tag, l->def->pos-c->pos+offset);
582                 }
583                 break;
584             }
585             default:
586                 printf("Can't parse opcode param type \"%c\"\n", *p);
587         }
588         p++;
589     }
590     return len;
591 }
592 
code_write(TAG * tag,code_t * code,pool_t * pool,abc_file_t * file)593 void code_write(TAG*tag, code_t*code, pool_t*pool, abc_file_t*file)
594 {
595     code = code_start(code);
596     int pos = 0;
597     int length = 0;
598     code_t*c = code;
599     while(c) {
600         c->pos = pos;
601         pos += opcode_write(0, c, pool, file, 0);
602         c = c->next;
603     }
604     length = pos;
605     swf_SetU30(tag, pos);
606     int start = tag->len;
607     c = code;
608     pos = 0;
609     while(c) {
610         opcode_t*op = opcode_get(code->opcode);
611         if(op->flags&(OP_BRANCH|OP_JUMP)) {
612             int skip = 0;
613         }
614         pos += opcode_write(tag, c, pool, file, length);
615         c = c->next;
616     }
617     assert(tag->len - start == pos);
618 }
619 
620 typedef struct {
621     int stackpos;
622     int scopepos;
623     code_t*code;
624     char flags;
625     char error;
626 } stackpos_t;
627 
628 typedef struct {
629     stackpos_t*stack;
630     int num;
631     int maxlocal;
632     int maxstack;
633     int maxscope;
634     int flags;
635 } currentstats_t;
636 
stack_minus(code_t * c)637 static int stack_minus(code_t*c)
638 {
639     opcode_t*op = opcode_get(c->opcode);
640     if(op->stack_minus>0) {
641         fprintf(stderr, "Invalid opcode entry %02x %s\n", c->opcode, op->name);
642     }
643     int stack = op->stack_minus;
644     if(op->flags&OP_STACK_NS) {
645         multiname_t*m = (multiname_t*)c->data[0];
646         if(multiname_late_namespace(m))
647             stack--;
648         if(multiname_late_name(m))
649             stack--;
650     }
651     if(op->flags&OP_STACK_ARGS || op->flags&OP_STACK_ARGS2) {
652         assert(strchr(op->params, 'n'));
653         int nr = (ptroff_t)(op->params[0]=='n'?c->data[0]:c->data[1]);
654         stack-=nr;
655         if(op->flags&OP_STACK_ARGS2)
656             stack-=nr;
657     }
658     return stack;
659 }
handleregister(currentstats_t * stats,int reg)660 static void handleregister(currentstats_t*stats, int reg)
661 {
662     if(reg+1 > stats->maxlocal)
663         stats->maxlocal = reg+1;
664 }
665 
666 #define FLAG_SEEN 1
667 #define FLAG_ERROR 2
668 
dumpstack(currentstats_t * stats)669 static void dumpstack(currentstats_t*stats)
670 {
671     int t;
672     for(t=0;t<stats->num;t++) {
673         code_t*c = stats->stack[t].code;
674         opcode_t*op = opcode_get(c->opcode);
675         printf("%05d) %c %d:%d %s", t, (stats->stack[t].flags&FLAG_SEEN)?'x':'|',
676                                stats->stack[t].stackpos,
677                                stats->stack[t].scopepos,
678                                op->name);
679 
680         if(op->flags&(OP_BRANCH|OP_JUMP)) {
681             if(c->branch)
682                 printf(" ->%d\n", c->branch->pos);
683             else
684                 printf(" 00000000\n");
685         }
686         if(op->params[0]=='2') {
687             printf(" %s", multiname_tostring(c->data[0]));
688         } else if(op->params[0]=='N') {
689             printf(" %s", namespace_tostring(c->data[0]));
690         }
691         printf("\n");
692     }
693 }
694 
callcode(currentstats_t * stats,int pos,int stack,int scope)695 static char callcode(currentstats_t*stats, int pos, int stack, int scope)
696 {
697     while(pos<stats->num) {
698         if(stats->stack[pos].flags&FLAG_SEEN) {
699             if(stats->stack[pos].stackpos != stack ||
700                stats->stack[pos].scopepos != scope) {
701                 //dumpstack(stats);
702                 stats->stack[pos].flags |= FLAG_ERROR;
703                 fprintf(stderr, "Stack mismatch at pos %d\n", pos);
704                 fprintf(stderr, "Should be: %d:%d, is: %d:%d\n", stack, scope,
705                     stats->stack[pos].stackpos, stats->stack[pos].scopepos);
706 
707                 /* return error here if we do verification */
708                 //return 0;
709             }
710             return 1;
711         }
712 
713         stats->stack[pos].flags |= FLAG_SEEN;
714         stats->stack[pos].stackpos = stack;
715         stats->stack[pos].scopepos = scope;
716 
717         code_t*c = stats->stack[pos].code;
718         opcode_t*op = opcode_get(c->opcode);
719 
720         //printf("Walking %s at position %d, stack=%d, scope=%d\n", op->name, pos, stack, scope);
721 
722         stack += stack_minus(c);
723 
724         if(stack<0) {
725             stats->stack[pos].flags |= FLAG_ERROR;
726             fprintf(stderr, "error: stack underflow at %d (%s)\n", pos, op->name);
727 
728             /* if we would do true verification (if we would be a vm), this is
729                where we would return the error
730                return 0;
731              */
732         }
733 
734         stack += op->stack_plus;
735         scope += op->scope_stack_plus;
736 
737         if(stack > stats->maxstack)
738             stats->maxstack = stack;
739         if(scope > stats->maxscope)
740             stats->maxscope = scope;
741 
742         if(op->flags & OP_SET_DXNS)
743             stats->flags |= FLAGS_SET_DXNS;
744         if(op->flags & OP_NEED_ACTIVATION)
745             stats->flags |= FLAGS_ACTIVATION;
746 
747         if(c->opcode == OPCODE_NEWCLASS) {
748             abc_class_t*cls = (abc_class_t*)(c->data[0]);
749             if(scope > cls->init_scope_depth)
750                 cls->init_scope_depth = scope;
751         }
752         if(c->opcode == OPCODE_NEWFUNCTION) {
753             abc_method_t*m = (abc_method_t*)(c->data[0]);
754             if(m->body && scope > m->body->init_scope_depth)
755                 m->body->init_scope_depth = scope;
756         }
757 
758         if(op->flags & OP_REGISTER) {
759             char*p = op->params;
760             int pos = 0;
761             char ok=0;
762             while(*p) {
763                 if(*p=='r') {
764                     handleregister(stats, (ptroff_t)c->data[pos]);
765                     ok = 1;
766                 }
767                 p++;
768             }
769             if(!ok) {
770                 handleregister(stats, c->opcode&3);
771             }
772         }
773         if(op->flags&OP_RETURN) {
774             if(OP_RETURN==0x48/*returnvalue*/) {
775                 if(stack!=1) {
776                     stats->stack[pos].flags |= FLAG_ERROR;
777                     fprintf(stderr, "return(value) with stackposition %d\n", stack);
778                 }
779             } else if(OP_RETURN==0x47) {
780                 if(stack!=0) {
781                     stats->stack[pos].flags |= FLAG_ERROR;
782                     fprintf(stderr, "return(void) with stackposition %d\n", stack);
783                 }
784             }
785         }
786         if(op->flags & (OP_THROW|OP_RETURN))
787             return 1;
788         if(op->flags & OP_JUMP) {
789             if(!c->branch) {
790                 stats->stack[pos].flags |= FLAG_ERROR;
791                 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d.\n", op->name, pos);
792                 return 0;
793             }
794             c = c->branch;
795             pos = c->pos;
796             continue;
797         }
798         if(op->flags & OP_BRANCH) {
799             if(!c->branch) {
800                 stats->stack[pos].flags |= FLAG_ERROR;
801                 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d\n", op->name, pos);
802                 return 0;
803             }
804             int newpos = c->branch->pos;
805             if(!callcode(stats, newpos, stack, scope))
806                 return 0;
807         }
808         if(op->flags & OP_LOOKUPSWITCH) {
809             lookupswitch_t*l = c->data[0];
810             if(!l->def) {
811                 stats->stack[pos].flags |= FLAG_ERROR;
812                 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d\n", op->name, pos);
813                 return 0;
814             }
815             if(!callcode(stats, l->def->pos, stack, scope))
816                 return 0;
817             code_list_t*t = l->targets;
818             while(t) {
819                 if(!t->code) {
820                     stats->stack[pos].flags |= FLAG_ERROR;
821                     fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d\n", op->name, pos);
822                     return 0;
823                 }
824                 if(!callcode(stats, t->code->pos, stack, scope))
825                     return 0;
826                 t = t->next;
827             }
828         }
829 
830         pos++;
831         if(pos<stats->num) {
832             assert(c->next == stats->stack[pos].code);
833         }
834     }
835     return 1;
836 }
837 
code_get_stats(code_t * code,abc_exception_list_t * exceptions)838 static currentstats_t* code_get_stats(code_t*code, abc_exception_list_t*exceptions)
839 {
840     code = code_start(code);
841     int num = 0;
842     code_t*c = code;
843     while(c) {
844         num++;
845         c = c->next;
846     }
847     currentstats_t* current = malloc(sizeof(currentstats_t));
848     current->stack = rfx_calloc(sizeof(stackpos_t)*num);
849     current->maxlocal = 0;
850     current->maxstack = 0;
851     current->maxscope = 0;
852     current->num = num;
853     current->flags = 0;
854 
855 //#define DEBUG_BYTES
856 #ifdef DEBUG_BYTES
857     int t;
858     c = code;
859     for(t=0;t<num;t++) {
860         opcode_t*op = opcode_get(c->opcode);
861         if(op->flags & (OP_JUMP|OP_BRANCH)) {
862             printf("%05d) %s %p\n", t, op->name, c->branch);
863         } else if(op->params[0]=='2') {
864             printf("%05d) %s %s\n", t, op->name, multiname_tostring(c->data[0]));
865         } else if(op->params[0]=='N') {
866             printf("%05d) %s %s\n", t, op->name, namespace_tostring(c->data[0]));
867         } else {
868             printf("%05d) %s\n", t, op->name);
869         }
870         c = c->next;
871     }
872     //printf("%05d) %02x\n", t, tag->data[start+t]);
873 #endif
874 
875     num = 0;
876     c = code;
877     while(c) {
878         //crosslink
879         current->stack[num].code = c;
880         c->pos = num;
881         num++;
882         c = c->next;
883     }
884 
885     if(!callcode(current, 0, 0, 0)) {
886         free(current);
887         return 0;
888     }
889     abc_exception_list_t*e = exceptions;
890     while(e) {
891         if(e->abc_exception->target)
892             callcode(current, e->abc_exception->target->pos, 1, 0);
893         e = e->next;
894     }
895 
896     return current;
897 }
898 
stats_free(currentstats_t * stats)899 void stats_free(currentstats_t*stats)
900 {
901     if(stats) {
902         free(stats->stack);stats->stack=0;
903         free(stats);
904     }
905 }
906 
code_dump(code_t * c)907 int code_dump(code_t*c)
908 {
909     code_t*cc = code_start(c);
910     while(cc) {
911         assert(!cc->next || cc->next->prev == cc);
912         cc = cc->next;
913     }
914 
915     return code_dump2(c, 0, 0, "", stdout);
916 }
code_dump2(code_t * c,abc_exception_list_t * exceptions,abc_file_t * file,char * prefix,FILE * fo)917 int code_dump2(code_t*c, abc_exception_list_t*exceptions, abc_file_t*file, char*prefix, FILE*fo)
918 {
919     abc_exception_list_t*e = exceptions;
920     c = code_start(c);
921     currentstats_t*stats =  code_get_stats(c, exceptions);
922 
923     int pos = 0;
924     while(c) {
925 	U8 opcode = c->opcode;
926 	char found = 0;
927         opcode_t*op = opcode_get(opcode);
928 
929         e = exceptions;
930         while(e) {
931             if(c==e->abc_exception->from)
932                 fprintf(fo, "%s   TRY {\n", prefix);
933             if(c==e->abc_exception->target) {
934                 char*s1 = multiname_tostring(e->abc_exception->exc_type);
935                 char*s2 = multiname_tostring(e->abc_exception->var_name);
936                 fprintf(fo, "%s   CATCH(%s %s)\n", prefix, s1, s2);
937                 free(s1);
938                 free(s2);
939             }
940             e = e->next;
941         }
942 
943 	if(!op) {
944 	    fprintf(stderr, "Can't parse opcode %02x.\n", opcode);
945 	    return 0;
946 	} else {
947             char*p = op->params;
948             char first = 1;
949             int i=0;
950 
951             if(stats) {
952                 int f = stats->stack[c->pos].flags;
953                 fprintf(fo, "%s%05d) %c %d:%d %s ", prefix, c->pos,
954                                        (f&FLAG_ERROR)?'E':((f&FLAG_SEEN)?'+':'|'),
955                                        stats->stack[c->pos].stackpos,
956                                        stats->stack[c->pos].scopepos,
957                                        op->name);
958             } else {
959                 fprintf(fo, "%s%05d) ? ?:? %s ", prefix, c->pos, op->name);
960             }
961 
962             while(*p) {
963                 void*data = c->data[i];
964                 if(i>0)
965                     printf(", ");
966 
967                 if(*p == 'n') {
968                     int n = (ptroff_t)data;
969                     fprintf(fo, "%d params", n);
970                 } else if(*p == '2') {
971                     multiname_t*n = (multiname_t*)data;
972                     char* m = multiname_tostring(n);
973                     fprintf(fo, "%s", m);
974                     free(m);
975                 } else if(*p == 'N') {
976                     namespace_t*ns = (namespace_t*)data;
977                     char* m = namespace_tostring(ns);
978                     fprintf(fo, "%s", m);
979                     free(m);
980                 } else if(*p == 'm') {
981                     abc_method_t*m = (abc_method_t*)data;
982                     fprintf(fo, "[method %08x %s]", m->index, m->name);
983                 } else if(*p == 'c') {
984                     abc_class_t*cls = (abc_class_t*)data;
985                     char*classname = multiname_tostring(cls->classname);
986                     fprintf(fo, "[classinfo %08x %s]", cls->index, classname);
987                     free(classname);
988                 } else if(*p == 'i') {
989                     abc_method_body_t*b = (abc_method_body_t*)data;
990                     fprintf(fo, "[methodbody]");
991                 } else if(*p == 'u' || *p == 'I' || *p == 'U') {
992                     int n = (ptroff_t)data;
993                     fprintf(fo, "%d", n);
994                 } else if(*p == 'f') {
995                     double f = *(double*)data;
996                     fprintf(fo, "%f", f);
997                 } else if(*p == 'r') {
998                     int n = (ptroff_t)data;
999                     fprintf(fo, "r%d", n);
1000                 } else if(*p == 'b') {
1001                     int b = (signed char)(ptroff_t)data;
1002                     fprintf(fo, "%d", b);
1003                 } else if(*p == 'j') {
1004                     if(c->branch)
1005                         fprintf(fo, "->%d", c->branch->pos);
1006                     else
1007                         fprintf(fo, "%p", c->branch);
1008                 } else if(*p == 's') {
1009                     char*s = string_escape((string_t*)data);
1010                     fprintf(fo, "\"%s\"", s);
1011                     free(s);
1012                 } else if(*p == 'D') {
1013                     fprintf(fo, "[register %02x=%s]", (int)(ptroff_t)c->data[1], (char*)c->data[0]);
1014                 } else if(*p == 'S') {
1015                     lookupswitch_t*l = c->data[0];
1016                     fprintf(fo, "[");
1017                     if(l->def)
1018                         fprintf(fo, "default->%d", l->def->pos);
1019                     else
1020                         fprintf(fo, "default->00000000");
1021                     code_list_t*t = l->targets;
1022                     while(t) {
1023                         if(t->code)
1024                             fprintf(fo, ",->%d", t->code->pos);
1025                         else
1026                             fprintf(fo, ",->00000000");
1027                         t = t->next;
1028                     }
1029                     fprintf(fo, "]");
1030                 } else {
1031                     fprintf(stderr, "Can't parse opcode param type \"%c\"\n", *p);
1032                     return 0;
1033                 }
1034                 p++;
1035                 i++;
1036                 first = 0;
1037             }
1038             fprintf(fo, "\n");
1039 	}
1040 
1041         e = exceptions;
1042         while(e) {
1043             if(c==e->abc_exception->to) {
1044                 if(e->abc_exception->target)
1045                     fprintf(fo, "%s   } // END TRY (HANDLER: %d)\n", prefix, e->abc_exception->target->pos);
1046                 else
1047                     fprintf(fo, "%s   } // END TRY (HANDLER: 00000000)\n", prefix);
1048             }
1049             e = e->next;
1050         }
1051 
1052         pos++;
1053         c = c->next;
1054     }
1055     stats_free(stats);
1056     return 1;
1057 }
1058 
code_get_statistics(code_t * code,abc_exception_list_t * exceptions)1059 codestats_t* code_get_statistics(code_t*code, abc_exception_list_t*exceptions)
1060 {
1061     currentstats_t*current = code_get_stats(code, exceptions);
1062     if(!current)
1063         return 0;
1064     codestats_t*stats = rfx_calloc(sizeof(codestats_t));
1065     stats->local_count = current->maxlocal;
1066     stats->max_stack = current->maxstack;
1067     stats->max_scope_depth = current->maxscope;
1068     stats->flags = current->flags;
1069 
1070     stats_free(current);current=0;
1071     return stats;
1072 }
1073 
codestats_free(codestats_t * s)1074 void codestats_free(codestats_t*s)
1075 {
1076     free(s);
1077 }
1078 
add_opcode(code_t * atag,U8 op)1079 code_t* add_opcode(code_t*atag, U8 op)
1080 {
1081     code_t*tmp = (code_t*)rfx_calloc(sizeof(code_t));
1082     tmp->opcode = op;
1083     if(atag) {
1084 	tmp->prev = atag;
1085         tmp->next = atag->next;
1086         if(tmp->next)
1087             tmp->next->prev = tmp;
1088 	atag->next = tmp;
1089     } else {
1090 	tmp->prev = 0;
1091         tmp->next = 0;
1092     }
1093     return tmp;
1094 }
1095 
codestats_print(codestats_t * stats)1096 void codestats_print(codestats_t*stats)
1097 {
1098     printf("max_stack: %d\n", stats->max_stack);
1099     printf("local_count: %d\n", stats->local_count);
1100     printf("scope_depth: %d\n", stats->max_scope_depth);
1101 }
1102 
code_end(code_t * code)1103 code_t* code_end(code_t*code)
1104 {
1105     if(!code)
1106         return 0;
1107     while(code->next)
1108         code = code->next;
1109     return code;
1110 }
code_start(code_t * code)1111 code_t* code_start(code_t*code)
1112 {
1113     if(!code)
1114         return 0;
1115     while(code->prev)
1116         code = code->prev;
1117     return code;
1118 }
1119 
code_append(code_t * code,code_t * toappend)1120 code_t* code_append(code_t*code, code_t*toappend)
1121 {
1122     if(!code)
1123         return code_end(toappend);
1124     if(!toappend)
1125         return code_end(code);
1126     //find end of first list
1127     while(code->next) {
1128         code = code->next;
1129     }
1130     code_t*start=toappend;
1131     //and start of second list
1132     while(start->prev) {
1133         start = start->prev;
1134     }
1135     code->next = start;
1136     start->prev = code;
1137     return code_end(toappend);
1138 }
1139 
lookupswitch_dup(lookupswitch_t * l)1140 lookupswitch_t*lookupswitch_dup(lookupswitch_t*l)
1141 {
1142     lookupswitch_t*n = malloc(sizeof(lookupswitch_t));
1143     fprintf(stderr, "Error: lookupswitch dupping not supported yet\n");
1144     n->targets = list_clone(l->targets);
1145     return 0;
1146 }
1147 
code_dup(code_t * c)1148 code_t*code_dup(code_t*c)
1149 {
1150     if(!c) return 0;
1151 
1152     dict_t*pos2pos = dict_new2(&ptr_type);
1153 
1154     code_t*last = 0;
1155     c = code_start(c);
1156     code_t*start = 0;
1157     char does_branch = 0;
1158     while(c) {
1159         NEW(code_t, n);
1160         memcpy(n, c, sizeof(code_t));
1161         if(!start)
1162             start=n;
1163 
1164         if(c->opcode == OPCODE_LABEL || c->opcode == OPCODE_NOP) {
1165             dict_put(pos2pos, c, n);
1166         }
1167         if(c->branch) {
1168             does_branch = 1;
1169         }
1170 
1171         opcode_t*op = opcode_get(c->opcode);
1172 
1173         char*p = op?op->params:"";
1174         int pos=0;
1175         while(*p) {
1176             if(*p == '2') { //multiname
1177                 c->data[pos] = multiname_clone(c->data[pos]);
1178             } else if(*p == 'N') { //multiname
1179                 c->data[pos] = namespace_clone(c->data[pos]);
1180             } else if(*p == 's') {
1181                 c->data[pos] = string_dup3(c->data[pos]);
1182             } else if(*p == 'D') {
1183                 c->data[pos] = strdup(c->data[pos]);
1184             } else if(*p == 'f') {
1185                 double old = *(double*)c->data[pos];
1186                 c->data[pos] = malloc(sizeof(double));
1187                 *(double*)c->data[pos] = old;
1188             } else if(strchr("S", *p)) {
1189                 c->data[pos] = lookupswitch_dup(c->data[pos]);
1190             }
1191             p++;pos++;
1192         }
1193 
1194         n->prev = last;
1195         if(last) {
1196             last->next = n;
1197         }
1198         last = n;
1199         c = c->next;
1200     }
1201 
1202     if(does_branch) {
1203         c = start;
1204         while(c) {
1205             if(c->branch) {
1206                 code_t*target = dict_lookup(pos2pos, c->branch);
1207                 if(!target) {
1208                     fprintf(stderr, "Error: Can't find branch target in code_dup\n");
1209                     return 0;
1210                 }
1211                 c->branch = target;
1212             }
1213             c = c->next;
1214         }
1215     }
1216     dict_destroy(pos2pos);
1217     return last;
1218 }
1219 
code_cut(code_t * c)1220 code_t*code_cut(code_t*c)
1221 {
1222     if(!c) return c;
1223     code_t*prev = c->prev;
1224     code_t*next = c->next;
1225     c->prev = 0;
1226     c->next = 0;
1227     if(prev) prev->next=next;
1228     if(next) next->prev=prev;
1229     code_free(c);
1230 
1231     if(next) return code_end(next);
1232     else     return prev;
1233 }
1234 
code_cutlast(code_t * c)1235 code_t*code_cutlast(code_t*c)
1236 {
1237     if(!c) return c;
1238     assert(!c->next);
1239     return code_cut(c);
1240 }
1241 
is_getlocal(code_t * c)1242 char is_getlocal(code_t*c)
1243 {
1244     if(!c) return 0;
1245     if(c->opcode == OPCODE_GETLOCAL ||
1246        c->opcode == OPCODE_GETLOCAL_0 ||
1247        c->opcode == OPCODE_GETLOCAL_1 ||
1248        c->opcode == OPCODE_GETLOCAL_2 ||
1249        c->opcode == OPCODE_GETLOCAL_3) {
1250 	return 1;
1251     }
1252     return 0;
1253 }
1254 
cut_last_push(code_t * c)1255 code_t* cut_last_push(code_t*c)
1256 {
1257     assert(!c->next);
1258     while(c) {
1259         if(!c) break;
1260         opcode_t*op = opcode_get(c->opcode);
1261         /* cut conversion type operations */
1262         if(op->stack_minus == -1 && op->stack_plus == 1 && !(op->flags)) {
1263             c = code_cutlast(c);
1264             continue;
1265         }
1266         /* cut any type of push */
1267         else if(op->stack_minus == 0 && op->stack_plus == 1 && !(op->flags)) {
1268             return code_cutlast(c);
1269         }
1270         /* cut register lookups */
1271         else if(c->opcode == OPCODE_GETLOCAL ||
1272            c->opcode == OPCODE_GETLOCAL_0 ||
1273            c->opcode == OPCODE_GETLOCAL_1 ||
1274            c->opcode == OPCODE_GETLOCAL_2 ||
1275            c->opcode == OPCODE_GETLOCAL_3) {
1276             return code_cutlast(c);
1277         }
1278         /* discard function call values */
1279         else if(c->opcode == OPCODE_CALLPROPERTY) {
1280             c->opcode = OPCODE_CALLPROPVOID;
1281             return c;
1282         } else if(c->opcode == OPCODE_CALLSUPER) {
1283             c->opcode = OPCODE_CALLSUPERVOID;
1284             return c;
1285         } else if((c->opcode == OPCODE_NEWOBJECT ||
1286                    c->opcode == OPCODE_NEWARRAY) &&
1287                    !c->data[0]) {
1288             // we can discard these if they're not eating up stack parameters
1289             return code_cutlast(c);
1290         } else if(op->stack_minus ==0 && op->stack_plus == 0 &&
1291                 !(op->flags&~(OP_REGISTER|OP_SET_DXNS)) && c->prev) {
1292             // trim code *before* the kill, inclocal, declocal, dxns
1293             code_t*p = c->prev;
1294             p->next = 0;
1295             c->prev = 0;
1296             return code_append(cut_last_push(p), c);
1297         } else
1298             break;
1299     }
1300     c = abc_pop(c);
1301     return c;
1302 }
1303 
1304 
1305