fixed bug in name display
[swftools.git] / lib / as3 / abc.c
1 /* abc.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 <stdarg.h>
25 #include <assert.h>
26 #include "../rfxswf.h"
27 #include "../q.h"
28 #include "abc.h"
29
30 char stringbuffer[2048];
31
32 typedef struct _opcode
33 {
34     unsigned char opcode;
35     char*name;
36     char*params;
37 } opcode_t;
38
39 /* 2 = multiname
40    m = method
41    n = number of params
42    i = method info
43    b = byte
44    s = short
45    c = class
46    s = string
47    S = switch
48 */
49
50 int abc_RegisterNameSpace(abc_file_t*file, const char*name);
51 int abc_RegisterPackageNameSpace(abc_file_t*file, const char*name);
52 int abc_RegisterPackageInternalNameSpace(abc_file_t*file, const char*name);
53 int abc_RegisterProtectedNameSpace(abc_file_t*file, const char*name);
54 int abc_RegisterExplicitNameSpace(abc_file_t*file, const char*name);
55 int abc_RegisterStaticProtectedNameSpace(abc_file_t*file, const char*name);
56 int abc_RegisterPrivateNameSpace(abc_file_t*file, const char*name);
57
58
59 opcode_t opcodes[]={
60 {0xa0, "add", ""},
61 {0xc5, "add_i", ""},
62 {0x86, "atype", "2"},
63 {0x87, "astypelate", ""},
64 {0xA8, "bitand", ""},
65 {0x97, "bitnot", ""},
66 {0xa9, "bitor", ""},
67 {0xaa, "bitxor", ""},
68 {0x41, "call", "n"},
69 {0x43, "callmethod", "mn"},
70 {0x4c, "callproplex", "2n"},
71 {0x46, "callproperty", "2n"},
72 {0x4f, "callpropvoid", "2n"},
73 {0x44, "callstatic", "in"},
74 {0x45, "callsuper", "2n"},
75 {0x4e, "callsupervoid", "2n"},
76 {0x78, "checkfilter", ""},
77 {0x80, "coerce", "2"},
78 {0x82, "coerce_a", ""},
79 {0x85, "coerce_s", ""},
80 {0x42, "construct", "n"},
81 {0x4a, "constructprop", "2n"},
82 {0x49, "constructsuper", "n"},
83 {0x76, "convert_b", ""},
84 {0x73, "convert_i", ""},
85 {0x75, "convert_d", ""},
86 {0x77, "convert_o", ""},
87 {0x74, "convert_u", ""},
88 {0x70, "convert_s", ""},
89 {0xef, "debug", "D"},
90 {0xf1, "debugfile", "s"},
91 {0xf0, "debugline", "u"},
92 {0x94, "declocal", "u"},
93 {0xc3, "declocal_i", "u"},
94 {0x93, "decrement", ""},
95 {0xc1, "decrement_i", ""},
96 {0x6a, "deleteproperty", "2"},
97 {0xa3, "divide", ""},
98 {0x2a, "dup", ""},
99 {0x06, "dxns", "s"},
100 {0x07, "dxnslate", ""},
101 {0xab, "equals", ""},
102 {0x72, "esc_xattr", ""},
103 {0x71, "esc_xelem", ""},
104 {0x5e, "findproperty", "2"},
105 {0x5d, "findpropstrict", "2"},
106 {0x59, "getdescendants", "2"},
107 {0x64, "getglobalscope", ""},
108 {0x6e, "getglobalslot", "u"},
109 {0x60, "getlex", "2"},
110 {0x62, "getlocal", "u"},
111 {0xd0, "getlocal_0", ""},
112 {0xd1, "getlocal_1", ""},
113 {0xd2, "getlocal_2", ""},
114 {0xd3, "getlocal_3", ""},
115 {0x66, "getproperty", "2"},
116 {0x65, "getscopeobject", "u"},
117 {0x6c, "getslot", "u"},
118 {0x04, "getsuper", "2"},
119 {0xaf, "greaterequals", ""},
120 {0x1f, "hasnext", ""},
121 {0x32, "hasnext2", "uu"},
122 {0x13, "ifeq", "j"},
123 {0x12, "iffalse", "j"},
124 {0x18, "ifge", "j"},
125 {0x17, "ifgt", "j"},
126 {0x16, "ifle", "j"},
127 {0x15, "iflt", "j"},
128 {0x0f, "ifnge", "j"},
129 {0x0e, "ifngt", "j"},
130 {0x0d, "ifnle", "j"},
131 {0x0c, "ifnlt", "j"},
132 {0x14, "ifne", "j"},
133 {0x19, "ifstricteq", "j"},
134 {0x1a, "ifstrictne", "j"},
135 {0x11, "iftrue", "j"},
136 {0xb4, "in", ""},
137 {0x92, "inclocal", "u"},
138 {0xc2, "inclocal_i", "u"},
139 {0x91, "increment", ""},
140 {0xc0, "increment_i", ""},
141 {0x68, "initproperty", "2"},
142 {0xb1, "instanceof", ""},
143 {0xb2, "istype", "2"},
144 {0xb3, "istypelate", ""},
145 {0x10, "jump", "j"},
146 {0x08, "kill", "u"},
147 {0x09, "label", ""},
148 {0xae, "lessequals", ""},
149 {0xad, "lessthan", ""},
150 {0x1b, "lookupswitch", "S"},
151 {0xa5, "lshift", ""},
152 {0xa4, "modulo", ""},
153 {0xa2, "multiply", ""},
154 {0xc7, "multiply_i", ""},
155 {0x90, "negate", ""},
156 {0xc4, "negate_i", ""},
157 {0x57, "newactivation", ""},
158 {0x56, "newarray", "u"},
159 {0x5a, "newcatch", "u"}, //index into exception_info
160 {0x58, "newclass", "c"}, //index into class_info
161 {0x40, "newfunction", "u"}, //index into method_info
162 {0x55, "newobject", "u"},
163 {0x1e, "nextname", ""},
164 {0x23, "nextvalue", ""},
165 {0x02, "nop", ""},
166 {0x96, "not", ""},
167 {0x29, "pop", ""},
168 {0x1d, "popscope", ""},
169 {0x24, "pushbyte", "b"},
170 {0x2f, "pushdouble", "u"}, //index into floats
171 {0x27, "pushfalse", ""},
172 {0x2d, "pushint", "u"}, //index into ints
173 {0x31, "pushnamespace", "u"}, //index into namespace
174 {0x28, "pushnan", ""},
175 {0x20, "pushnull", ""},
176 {0x30, "pushscope", ""},
177 {0x25, "pushshort", "u"},
178 {0x2c, "pushstring", "s"},
179 {0x26, "pushtrue", ""},
180 {0x2e, "pushuint", "u"}, //index into uints
181 {0x21, "pushundefined", ""},
182 {0x1c, "pushwith", ""},
183 {0x48, "returnvalue", ""},
184 {0x47, "returnvoid", ""},
185 {0xa6, "rshift", ""},
186 {0x63, "setlocal", "u"},
187 {0xd4, "setlocal_0", ""},
188 {0xd5, "setlocal_1", ""},
189 {0xd6, "setlocal_2", ""},
190 {0xd7, "setlocal_3", ""},
191 {0x6f, "setglobalshot", "u"},
192 {0x61, "setproperty", "2"},
193 {0x6d, "setslot", "u"},
194 {0x05, "setsuper", "2"},
195 {0xac, "strictequals", ""},
196 {0xa1, "subtract", ""},
197 {0xc6, "subtract_i", ""},
198 {0x2b, "swap", ""},
199 {0x03, "throw", ""},
200 {0x95, "typeof", ""},
201 {0xa7, "urshift", ""},
202 {0xb0, "xxx", ""},
203 };
204
205 static U8 op2index[256] = {254};
206
207 opcode_t* opcode_get(U8 op)
208 {
209     int t;
210     if(op2index[0]==254) {
211         memset(op2index, 255, sizeof(op2index));
212         for(t=0;t<sizeof(opcodes)/sizeof(opcodes[0]);t++) {
213             op2index[opcodes[t].opcode] = t;
214         }
215     }
216     if(op2index[op]!=255)
217         return &opcodes[op2index[op]];
218     return 0;
219 }
220
221 abc_code_t*code_parse(TAG*tag, int len, abc_file_t*file, pool_t*pool)
222 {
223     abc_code_t*head=0;
224     abc_code_t*code=0;
225     int end=tag->pos+len;
226     //printf("-->\n");fflush(stdout);
227     while(tag->pos<end) {
228         U8 opcode = swf_GetU8(tag);
229         opcode_t*op = opcode_get(opcode);
230         if(!op) {
231             fprintf(stderr, "Can't parse opcode %02x\n", opcode);
232             return head;
233         }
234         //printf("%s\n", op->name);fflush(stdout);
235         NEW(abc_code_t,c);
236         if(!head) {
237             head = code = c;
238         } else {
239             code->next = c;
240             code = c;
241         }
242
243         c->opcode = opcode;
244
245         char*p = op->params;
246         int pos = 0;
247         while(*p) {
248             void*data = 0;
249             if(*p == 'n') { // number
250                 data = (void*)(ptroff_t)swf_GetU30(tag);
251             } else if(*p == '2') { //multiname
252                 data = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
253             } else if(*p == 'm') { //method
254                 data = array_getvalue(file->methods, swf_GetU30(tag));
255             } else if(*p == 'c') { //classinfo
256                 data = array_getvalue(file->classes, swf_GetU30(tag));
257             } else if(*p == 'i') { //methodbody TODO
258                 data = array_getvalue(file->method_bodies, swf_GetU30(tag));
259             } else if(*p == 'u') { // lookoup TODO
260                 data = (void*)(ptroff_t)swf_GetU30(tag);
261             } else if(*p == 'b') { // byte
262                 data = (void*)(ptroff_t)swf_GetU8(tag);
263             } else if(*p == 'j') { // jump TODO
264                 data = (void*)(ptroff_t)swf_GetS24(tag);
265             } else if(*p == 's') { // string
266                 data = strdup((char*)pool_lookup_string(pool, swf_GetU30(tag)));
267             } else if(*p == 'D') { // debug
268                 /*type, usually 1*/
269                 U8 type = swf_GetU8(tag);
270                 if(type!=1) 
271                     fprintf(stderr, "Unknown debug type: %02x\n", type);
272                 /*register name*/
273                 code->params[0] = strdup((char*)pool_lookup_string(pool, swf_GetU30(tag)));
274                 /*register index*/
275                 code->params[1] = (void*)(ptroff_t)swf_GetU8(tag);
276                 /*unused*/
277                 swf_GetU30(tag);
278             } else if(*p == 'S') { // switch statement TODO
279                 /* I hate these things */
280                 swf_GetU24(tag); //default
281                 int num = swf_GetU30(tag)+1;
282                 int t;
283                 for(t=0;t<num;t++) 
284                     swf_GetU24(tag);
285                 data = 0;
286             } else {
287                 printf("Can't parse opcode param type \"%c\"\n", *p);
288                 return 0;
289             }
290             if(data)
291                 code->params[pos++] = data;
292             p++;
293         }
294     }
295     return head;
296 }
297
298 static void code_free(abc_code_t*c)
299 {
300     while(c) {
301         abc_code_t*next = c->next;
302         opcode_t*op = opcode_get(c->opcode);
303         char*p = op?op->params:"";
304         int pos=0;
305         while(*p) {
306             void*data = c->params[pos];
307             if(*p == '2') { //multiname
308                 multiname_destroy(data);
309             } else if(strchr("sD", *p)) {
310                 free(data);
311             }
312             c->params[pos]=0;
313             p++;pos++;
314         }
315         memset(c, 0, sizeof(c));
316         free(c);
317         c = next;
318     }
319 }
320
321 static int code_dump(abc_code_t*c, abc_file_t*file, char*prefix, FILE*fo)
322 {
323     pool_t*pool = pool_new();
324
325     while(c) {
326         U8 opcode = c->opcode;
327         int t;
328         char found = 0;
329         opcode_t*op = opcode_get(opcode);
330         if(!op) {
331             fprintf(stderr, "Can't parse opcode %02x.\n", opcode);
332             return 0;
333         } else {
334             fprintf(fo, "%s%s ", prefix, op->name);
335             char*p = op->params;
336             char first = 1;
337             int pos=0;
338             while(*p) {
339                 void*data = c->params[pos];
340                 if(pos>0)
341                     printf(", ");
342
343                 if(*p == 'n') {
344                     int n = (ptroff_t)data;
345                     fprintf(fo, "%d params", n);
346                 } else if(*p == '2') {
347                     multiname_t*n = (multiname_t*)data;
348                     char* m = multiname_to_string(n);
349                     fprintf(fo, "%s", m);
350                     free(m);
351                 } else if(*p == 'm') {
352                     abc_method_t*m = (abc_method_t*)data;
353                     fprintf(fo, "[method %s]", m->name);
354                 } else if(*p == 'c') {
355                     abc_class_t*cls = (abc_class_t*)data;
356                     char*classname = multiname_to_string(cls->classname);
357                     fprintf(fo, "[classinfo %s]", classname);
358                     free(classname);
359                 } else if(*p == 'i') {
360                     abc_method_body_t*b = (abc_method_body_t*)data;
361                     fprintf(fo, "[methodbody]");
362                 } else if(*p == 'u') {
363                     int n = (ptroff_t)data;
364                     fprintf(fo, "%d", n);
365                 } else if(*p == 'b') {
366                     int b = (ptroff_t)data;
367                     fprintf(fo, "%02x", b);
368                 } else if(*p == 'j') {
369                     int n = (ptroff_t)data;
370                     fprintf(fo, "%d", n);
371                 } else if(*p == 's') {
372                     fprintf(fo, "\"%s\"", data);
373                 } else if(*p == 'D') {
374                     fprintf(fo, "[register %02x=%s]", (ptroff_t)c->params[1], (char*)c->params[0]);
375                 } else if(*p == 'S') {
376                     fprintf(fo, "[switch data]");
377                 } else {
378                     fprintf(stderr, "Can't parse opcode param type \"%c\"\n", *p);
379                     return 0;
380                 }
381                 p++;
382                 pos++;
383                 first = 0;
384             }
385             fprintf(fo, "\n");
386         }
387         c = c->next;
388     }
389     return 1;
390 }
391
392 static int opcode_write(TAG*tag, abc_code_t*code, pool_t*pool, abc_file_t*file)
393 {
394     opcode_t*c = opcode_get(code->opcode);
395     char*p = c->params;
396     int pos = 0;
397     int len = 0;
398     
399     if(tag)
400         swf_SetU8(tag, code->opcode);
401     len++;
402
403     while(*p) {
404         void*data = code->params[pos++];
405         assert(pos<=2);
406         if(*p == 'n') { // number
407             len += swf_SetU30(tag, (ptroff_t)data);
408         } else if(*p == '2') { //multiname
409             multiname_t*m = (multiname_t*)data;
410             len += swf_SetU30(tag, pool_register_multiname(pool, m));
411         } else if(*p == 'm') { //method
412             abc_method_t*m = (abc_method_t*)data;
413             len += swf_SetU30(tag, m->index);
414         } else if(*p == 'c') { //classinfo 
415             abc_class_t*cls = (abc_class_t*)data;
416             len += swf_SetU30(tag, cls->index);
417         } else if(*p == 'i') { //methodbody
418             abc_method_body_t*m = (abc_method_body_t*)data;
419             len += swf_SetU30(tag, m->index);
420         } else if(*p == 'u') { // integer
421             len += swf_SetU30(tag, (ptroff_t)data);
422         } else if(*p == 'b') { // byte
423             if(tag)
424                 swf_SetU8(tag, (ptroff_t)data);
425             len++;
426         } else if(*p == 'j') { // jump
427             len += swf_SetS24(tag, (ptroff_t)data);
428         } else if(*p == 's') { // string
429             int index = pool_register_string(pool, data);
430             len += swf_SetU30(tag, index);
431         } else if(*p == 'D') { // debug statement
432             if(tag)
433                 swf_SetU8(tag, 1);
434             len++;
435             len+=swf_SetU30(tag, pool_register_string(pool,code->params[0]));
436             if(tag)
437                 swf_SetU8(tag, (ptroff_t)code->params[1]);
438             len++;
439             len+=swf_SetU30(tag, 0);
440         } else if(*p == 'S') { // switch statement
441             len+=swf_SetU24(tag, 0); //default
442             len+=swf_SetU30(tag, 0); //nr-1
443             len+=swf_SetU24(tag, 0); //first
444         } else {
445             printf("Can't parse opcode param type \"%c\"\n", *p);
446         }
447         p++;
448     }
449     return len;
450 }
451
452
453 static char* params_to_string(multiname_list_t*list)
454 {
455     multiname_list_t*l;
456     int n;
457
458     l = list;
459     n = 0;
460     while(list) {
461         n++;list=list->next;
462     }
463
464     char**names = (char**)malloc(sizeof(char*)*n);
465     
466     l = list;
467     n = 0;
468     int size = 0;
469     while(list) {
470         names[n] = multiname_to_string(list->multiname);
471         size += strlen(names[n]) + 2;
472         n++;list=list->next;
473     }
474
475     char* params = malloc(size+5);
476     params[0]='(';
477     params[1]=0;
478     l = list;
479     int s=0;
480     n = 0;
481     while(list) {
482         if(s)
483             strcat(params, ", ");
484         strcat(params, names[n]);
485         free(names[n]);
486         n++;
487         s=1;
488     }
489     free(names);
490     strcat(params, ")");
491     int t;
492     return params;
493 }
494
495 //#define DEBUG
496 #define DEBUG if(0)
497
498 static void parse_metadata(TAG*tag, abc_file_t*file, pool_t*pool)
499 {
500     int t;
501     int num_metadata = swf_GetU30(tag);
502
503     DEBUG printf("%d metadata\n");
504     for(t=0;t<num_metadata;t++) {
505         const char*entry_name = pool_lookup_string(pool, swf_GetU30(tag));
506         int num = swf_GetU30(tag);
507         int s;
508         DEBUG printf("  %s\n", entry_name);
509         array_t*items = array_new();
510         for(s=0;s<num;s++) {
511             int i1 = swf_GetU30(tag);
512             int i2 = swf_GetU30(tag);
513             char*key = i1?pool_lookup_string(pool, i1):"";
514             char*value = i2?pool_lookup_string(pool, i2):"";
515             DEBUG printf("    %s=%s\n", key, value);
516             array_append(items, key, strdup(value));
517         }
518         array_append(file->metadata, entry_name, items);
519     }
520 }
521
522 void swf_CopyData(TAG*to, TAG*from, int len)
523 {
524     unsigned char*data = malloc(len);
525     swf_GetBlock(from, data, len);
526     swf_SetBlock(to, data, len);
527     free(data);
528 }
529
530 abc_file_t*abc_file_new()
531 {
532     abc_file_t*f = malloc(sizeof(abc_file_t));
533     memset(f, 0, sizeof(abc_file_t));
534     f->metadata = array_new();
535
536     f->methods = array_new();
537     f->classes = array_new();
538     f->scripts = array_new();
539     f->method_bodies = array_new();
540
541     return f;
542 }
543
544 #define CLASS_SEALED 1
545 #define CLASS_FINAL 2
546 #define CLASS_INTERFACE 4
547 #define CLASS_PROTECTED_NS 8
548
549 abc_class_t* abc_class_new(abc_file_t*pool, multiname_t*classname, multiname_t*superclass) {
550     abc_class_t* c = malloc(sizeof(abc_class_t));
551     memset(c, 0, sizeof(abc_class_t));
552     c->index = array_append(pool->classes, classname->name, c);
553     c->pool = pool;
554     c->classname = classname;
555     c->superclass = superclass;
556     c->flags = 0;
557     c->constructor = 0;
558     c->static_constructor = 0;
559     c->traits = list_new();
560     return c;
561 }
562 abc_class_t* abc_class_new2(abc_file_t*pool, char*classname, char*superclass) 
563 {
564     return abc_class_new(pool, multiname_fromstring(classname), multiname_fromstring(superclass));
565 }
566
567 void abc_class_sealed(abc_class_t*c)
568 {
569     c->flags |= CLASS_SEALED;
570 }
571 void abc_class_final(abc_class_t*c)
572 {
573     c->flags |= CLASS_FINAL;
574 }
575 void abc_class_interface(abc_class_t*c)
576 {
577     c->flags |= CLASS_INTERFACE;
578 }
579 void abc_class_protectedNS(abc_class_t*c, char*namespace)
580 {
581     c->protectedNS = namespace_new_protected(namespace);
582     c->flags |= CLASS_PROTECTED_NS;
583 }
584 void abc_class_add_interface(abc_class_t*c, multiname_t*interface)
585 {
586     list_append(c->interfaces, interface);
587 }
588
589 abc_method_body_t* add_method(abc_file_t*pool, abc_class_t*cls, char*returntype, int num_params, va_list va)
590 {
591     /* construct code (method body) object */
592     abc_method_body_t* c = malloc(sizeof(abc_method_body_t));
593     memset(c, 0, sizeof(abc_method_body_t));
594     c->index = array_append(pool->method_bodies, 0, c);
595     c->pool = pool;
596     c->traits = list_new();
597     c->code = 0;
598
599     /* construct method object */
600     NEW(abc_method_t,m);
601     m->index = array_append(pool->methods, 0, m);
602     if(returntype && strcmp(returntype, "void")) {
603         m->return_type = multiname_fromstring(returntype);
604     } else {
605         m->return_type = 0;
606     }
607     int t;
608     for(t=0;t<num_params;t++) {
609         const char*param = va_arg(va, const char*);
610         list_append(m->parameters, multiname_fromstring(param));
611     }
612
613     /* crosslink the two objects */
614     m->body = c;
615     c->method = m;
616
617     return c;
618 }
619
620 abc_method_body_t* abc_class_constructor(abc_class_t*cls, char*returntype, int num_params, ...) 
621 {
622     va_list va;
623     va_start(va, num_params);
624     abc_method_body_t* c = add_method(cls->pool, cls, returntype, num_params, va);
625     va_end(va);
626     cls->constructor = c->method;
627     return c;
628 }
629
630 abc_method_body_t* abc_class_staticconstructor(abc_class_t*cls, char*returntype, int num_params, ...) 
631 {
632     va_list va;
633     va_start(va, num_params);
634     abc_method_body_t* c = add_method(cls->pool, cls, returntype, num_params, va);
635     va_end(va);
636     cls->static_constructor = c->method;
637     return c;
638 }
639
640 trait_t*trait_new(int type, multiname_t*name, int data1, int data2, int vindex, int vkind)
641 {
642     trait_t*trait = malloc(sizeof(trait_t));
643     memset(trait, 0, sizeof(trait_t));
644     trait->kind = type&0x0f;
645     trait->attributes = type&0xf0;
646     trait->name = name;
647     trait->data1 = data1;
648     trait->data2 = data2;
649     trait->vindex = vindex;
650     trait->vkind = vkind;
651     return trait;
652 }
653
654 abc_method_body_t* abc_class_method(abc_class_t*cls, char*returntype, char*name, int num_params, ...)
655 {
656     abc_file_t*pool = cls->pool;
657     va_list va;
658     va_start(va, num_params);
659     abc_method_body_t* c = add_method(cls->pool, cls, returntype, num_params, va);
660     va_end(va);
661     list_append(cls->traits, trait_new(TRAIT_METHOD, multiname_fromstring(name), 0, c->method->index, 0, 0));
662     return c;
663 }
664
665 void abc_AddSlot(abc_class_t*cls, char*name, int slot, char*multiname)
666 {
667     abc_file_t*pool = cls->pool;
668     multiname_t*m = multiname_fromstring(multiname);
669     list_append(cls->traits, trait_new(TRAIT_SLOT, m, slot, 0, 0, 0));
670 }
671
672 void abc_method_body_addClassTrait(abc_method_body_t*code, char*multiname, int slotid, abc_class_t*cls)
673 {
674     abc_file_t*pool = code->pool;
675     multiname_t*m = multiname_fromstring(multiname);
676     trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0, 0);
677     trait->cls = cls;
678     list_append(code->traits, trait);
679 }
680
681 /* notice: traits of a method (body) belonging to an init script
682    and traits of the init script are *not* the same thing */
683 void abc_initscript_addClassTrait(abc_script_t*script, char*multiname, int slotid, abc_class_t*cls)
684 {
685     abc_file_t*pool = script->pool;
686     multiname_t*m = multiname_fromstring(multiname);
687     trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0, 0);
688     trait->cls = cls;
689     list_append(script->traits, trait);
690 }
691
692 abc_script_t* abc_initscript(abc_file_t*pool, char*returntype, int num_params, ...) 
693 {
694     va_list va;
695     va_start(va, num_params);
696     abc_method_body_t* c = add_method(pool, 0, returntype, num_params, va);
697     abc_script_t* s = malloc(sizeof(abc_script_t));
698     s->method = c->method;
699     s->traits = list_new();
700     s->pool = pool;
701     array_append(pool->scripts, 0, s);
702     va_end(va);
703     return s;
704 }
705
706 static void dump_traits(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file);
707
708 static void dump_method(FILE*fo, const char*prefix, const char*type, const char*name, abc_method_t*m, abc_file_t*file)
709 {
710     const char*return_type = "";
711     if(m->return_type)
712         return_type = multiname_to_string(m->return_type);
713
714     char*paramstr = params_to_string(m->parameters);
715
716     fprintf(fo, "%s%s %s %s=%s %s\n", prefix, type, return_type, name, m->name, paramstr);
717
718     abc_method_body_t*c = m->body;
719     if(!c) {
720         return;
721     }
722     
723     fprintf(fo, "%s[%d %d %d %d %d]\n", prefix, c->max_stack, c->local_count, c->init_scope_depth, c->max_scope_depth, c->exception_count);
724
725     char prefix2[80];
726     sprintf(prefix2, "%s    ", prefix);
727     if(c->traits)
728         dump_traits(fo, prefix, c->traits, file);
729     fprintf(fo, "%s{\n", prefix);
730     code_dump(c->code, file, prefix2, fo);
731     fprintf(fo, "%s}\n\n", prefix);
732 }
733
734 static void traits_free(trait_list_t*traits) 
735 {
736     trait_list_t*t = traits;
737     while(t) {
738         if(t->trait->name) {
739             multiname_destroy(t->trait->name);t->trait->name = 0;
740         }
741         if(t->trait->kind == TRAIT_SLOT || t->trait->kind == TRAIT_CONST) {
742             multiname_destroy(t->trait->type_name);
743         }
744         free(t->trait);t->trait = 0;
745         t = t->next;
746     }
747     list_free(traits);
748 }
749
750 static trait_list_t* traits_parse(TAG*tag, pool_t*pool, abc_file_t*file)
751 {
752     int num_traits = swf_GetU30(tag);
753     trait_list_t*traits = list_new();
754     int t;
755     if(num_traits) {
756         DEBUG printf("%d traits\n", num_traits);
757     }
758     
759     for(t=0;t<num_traits;t++) {
760         trait_t*trait = malloc(sizeof(trait_t));
761         memset(trait, 0, sizeof(trait_t));
762         list_append(traits, trait);
763
764         trait->name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag))); // always a QName (ns,name)
765
766         const char*name = 0;
767         DEBUG name = multiname_to_string(trait->name);
768         U8 kind = swf_GetU8(tag);
769         U8 attributes = kind&0xf0;
770         kind&=0x0f;
771         trait->kind = kind;
772         trait->attributes = attributes;
773         DEBUG printf("  trait %d) %s type=%02x\n", t, name, kind);
774         if(kind == TRAIT_METHOD || kind == TRAIT_GETTER || kind == TRAIT_SETTER) { // method / getter / setter
775             trait->disp_id = swf_GetU30(tag);
776             trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
777             DEBUG printf("  method/getter/setter\n");
778         } else if(kind == TRAIT_FUNCTION) { // function
779             trait->slot_id =  swf_GetU30(tag);
780             trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
781         } else if(kind == TRAIT_CLASS) { // class
782             trait->slot_id = swf_GetU30(tag);
783             trait->cls = (abc_class_t*)array_getvalue(file->classes, swf_GetU30(tag));
784             DEBUG printf("  class %s %d %d\n", name, trait->slot_id, trait->cls);
785         } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
786             /* a slot is a variable in a class that is shared amonst all instances
787                of the same type, but which has a unique location in each object 
788                (in other words, slots are non-static, traits are static)
789              */
790             trait->slot_id = swf_GetU30(tag);
791             trait->type_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
792             trait->vindex = swf_GetU30(tag);
793             if(trait->vindex) {
794                 trait->vkind = swf_GetU8(tag);
795             }
796             DEBUG printf("  slot %s %d %s (vindex=%d)\n", name, trait->slot_id, trait->type_name->name, trait->vindex);
797         } else {
798             fprintf(stderr, "Can't parse trait type %d\n", kind);
799         }
800         if(attributes&0x40) {
801             int num = swf_GetU30(tag);
802             int s;
803             for(s=0;s<num;s++) {
804                 swf_GetU30(tag); //index into metadata array
805             }
806         }
807     }
808     return traits;
809 }
810
811 void traits_skip(TAG*tag)
812 {
813     int num_traits = swf_GetU30(tag);
814     int t;
815     for(t=0;t<num_traits;t++) {
816         swf_GetU30(tag);
817         U8 kind = swf_GetU8(tag);
818         U8 attributes = kind&0xf0;
819         kind&=0x0f;
820         swf_GetU30(tag);
821         swf_GetU30(tag);
822         if(kind == TRAIT_SLOT || kind == TRAIT_CONST) {
823             if(swf_GetU30(tag)) swf_GetU8(tag);
824         } else if(kind>TRAIT_CONST) {
825             fprintf(stderr, "Can't parse trait type %d\n", kind);
826         }
827         if(attributes&0x40) {
828             int s, num = swf_GetU30(tag);
829             for(s=0;s<num;s++) swf_GetU30(tag);
830         }
831     }
832 }
833
834
835 static void traits_write(pool_t*pool, TAG*tag, trait_list_t*traits)
836 {
837     if(!traits) {
838         swf_SetU30(tag, 0);
839         return;
840     }
841     swf_SetU30(tag, list_length(traits));
842     int s;
843
844     while(traits) {
845         trait_t*trait = traits->trait;
846
847         swf_SetU30(tag, pool_register_multiname(pool, trait->name));
848         swf_SetU8(tag, trait->kind|trait->attributes);
849
850         swf_SetU30(tag, trait->data1);
851
852         if(trait->kind == TRAIT_CLASS) {
853             swf_SetU30(tag, trait->cls->index);
854         } else if(trait->kind == TRAIT_GETTER ||
855                   trait->kind == TRAIT_SETTER ||
856                   trait->kind == TRAIT_METHOD) {
857             swf_SetU30(tag, trait->method->index);
858         } else if(trait->kind == TRAIT_SLOT ||
859                   trait->kind == TRAIT_CONST) {
860             int index = pool_register_multiname(pool, trait->type_name);
861             swf_SetU30(tag, index);
862         } else  {
863             swf_SetU30(tag, trait->data2);
864         }
865
866         if(trait->kind == TRAIT_SLOT || trait->kind == TRAIT_CONST) {
867             swf_SetU30(tag, trait->vindex);
868             if(trait->vindex) {
869                 swf_SetU8(tag, trait->vkind);
870             }
871         }
872         if(trait->attributes&0x40) {
873             // metadata
874             swf_SetU30(tag, 0);
875         }
876         traits = traits->next;
877     }
878 }
879
880
881 static void dump_traits(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file)
882 {
883     int t;
884     while(traits) {
885         trait_t*trait = traits->trait;
886         char*name = multiname_to_string(trait->name);
887         U8 kind = trait->kind;
888         U8 attributes = trait->attributes;
889         if(kind == TRAIT_METHOD) {
890             abc_method_t*m = trait->method;
891             dump_method(fo, prefix, "method", name, m, file);
892         } else if(kind == TRAIT_GETTER) {
893             abc_method_t*m = trait->method;
894             dump_method(fo, prefix, "getter", name, m, file);
895         } else if(kind == TRAIT_SETTER) {
896             abc_method_t*m = trait->method;
897             dump_method(fo, prefix, "setter", name, m, file);
898         } else if(kind == TRAIT_FUNCTION) { // function
899             abc_method_t*m = trait->method;
900             dump_method(fo, prefix, "function", name, m, file);
901         } else if(kind == TRAIT_CLASS) { // class
902             abc_class_t*cls = trait->cls;
903             if(!cls) {
904                 fprintf(fo, "%sslot %d: class %s=class%d\n", prefix, trait->slot_id, name);
905             } else {
906                 fprintf(fo, "%sslot %d: class %s=%s\n", prefix, trait->slot_id, name, cls->classname->name);
907             }
908         } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
909             int slot_id = trait->slot_id;
910             char*type_name = multiname_to_string(trait->type_name);
911             fprintf(fo, "%sslot %s %d %s (vindex=%d)\n", prefix, name, trait->slot_id, type_name, trait->vindex);
912             free(type_name);
913         } else {
914             fprintf(fo, "%s    can't dump trait type %d\n", prefix, kind);
915         }
916         free(name);
917         traits=traits->next;
918     }
919 }
920
921 void* swf_DumpABC(FILE*fo, void*code, char*prefix)
922 {
923     abc_file_t* file = (abc_file_t*)code;
924         
925     if(file->name) {
926         fprintf(fo, "%s#\n", prefix);
927         fprintf(fo, "%s#name: %s\n", prefix, file->name);
928         fprintf(fo, "%s#\n", prefix);
929     }
930
931     int t;
932     for(t=0;t<file->metadata->num;t++) {
933         const char*entry_name = array_getkey(file->metadata, t);
934         fprintf(fo, "%s#Metadata \"%s\":\n", prefix, entry_name);
935         int s;
936         array_t*items = (array_t*)array_getvalue(file->metadata, t);
937         for(s=0;s<items->num;s++) {
938             fprintf(fo, "%s#  %s=%s\n", prefix, array_getkey(items, s), array_getvalue(items,s));
939         }
940         fprintf(fo, "%s#\n", prefix);
941     }
942
943     for(t=0;t<file->classes->num;t++) {
944         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
945         char prefix2[80];
946         sprintf(prefix2, "%s    ", prefix);
947
948         fprintf(fo, "%s", prefix);
949         if(cls->flags&1) fprintf(fo, "sealed ");
950         if(cls->flags&2) fprintf(fo, "final ");
951         if(cls->flags&4) fprintf(fo, "interface ");
952         if(cls->flags&8) {
953             char*s = namespace_to_string(cls->protectedNS);
954             fprintf(fo, "protectedNS(%s) ", s);
955             free(s);
956         }
957
958         char*classname = multiname_to_string(cls->classname);
959         fprintf(fo, "class %s", classname);
960         free(classname);
961         if(cls->superclass) {
962             char*supername = multiname_to_string(cls->superclass);
963             fprintf(fo, " extends %s", supername);
964             free(supername);
965             multiname_list_t*ilist = cls->interfaces;
966             if(ilist)
967                 fprintf(fo, " implements");
968             while(ilist) {
969                 char*s = multiname_to_string(ilist->multiname);
970                 fprintf(fo, " %s", s);
971                 free(s);
972                 ilist = ilist->next;
973             }
974             ilist->next;
975         }
976         if(cls->flags&0xf0) 
977             fprintf(fo, "extra flags=%02x\n", cls->flags&0xf0);
978         fprintf(fo, "%s{\n", prefix);
979
980         if(cls->static_constructor)
981             dump_method(fo, prefix2,"staticconstructor", "", cls->static_constructor, file);
982         dump_traits(fo, prefix2, cls->static_constructor_traits, file);
983         
984         char*n = multiname_to_string(cls->classname);
985         if(cls->constructor)
986             dump_method(fo, prefix2, "constructor", n, cls->constructor, file);
987         free(n);
988         dump_traits(fo, prefix2,cls->traits, file);
989         fprintf(fo, "%s}\n", prefix);
990     }
991     fprintf(fo, "%s\n", prefix);
992
993     for(t=0;t<file->scripts->num;t++) {
994         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
995         dump_method(fo, prefix,"initmethod", "init", s->method, file);
996         dump_traits(fo, prefix, s->traits, file);
997     }
998     return file;
999 }
1000
1001 void* swf_ReadABC(TAG*tag)
1002 {
1003     abc_file_t* file = abc_file_new();
1004     pool_t*pool = pool_new();
1005
1006     swf_SetTagPos(tag, 0);
1007     int t;
1008     if(tag->id == ST_DOABC) {
1009         U32 abcflags = swf_GetU32(tag);
1010         DEBUG printf("flags=%08x\n", abcflags);
1011         char*name= swf_GetString(tag);
1012         file->name = name?strdup(name):0;
1013     }
1014     U32 version = swf_GetU32(tag);
1015     if(version!=0x002e0010) {
1016         fprintf(stderr, "Warning: unknown AVM2 version %08x\n", version);
1017     }
1018
1019     pool_read(pool, tag);
1020
1021     int num_methods = swf_GetU30(tag);
1022     DEBUG printf("%d methods\n", num_methods);
1023     for(t=0;t<num_methods;t++) {
1024         NEW(abc_method_t,m);
1025         int param_count = swf_GetU30(tag);
1026         int return_type_index = swf_GetU30(tag);
1027         m->return_type = multiname_clone(pool_lookup_multiname(pool, return_type_index));
1028
1029         int s;
1030         for(s=0;s<param_count;s++) {
1031             int type_index = swf_GetU30(tag);
1032             multiname_t*param = multiname_clone(pool_lookup_multiname(pool, type_index));
1033             /* type_index might be 0, which probably means "..." (varargs) */
1034             list_append(m->parameters, param);
1035         }
1036
1037         int namenr = swf_GetU30(tag);
1038         if(namenr)
1039             m->name = strdup(pool_lookup_string(pool, namenr));
1040         else
1041             m->name = strdup("");
1042
1043         m->flags = swf_GetU8(tag);
1044         
1045         DEBUG printf("method %d) %s flags=%02x\n", t, params_to_string(m->parameters), m->flags);
1046
1047         if(m->flags&0x08) {
1048             /* optional parameters */
1049             int num = swf_GetU30(tag);
1050             int s;
1051             for(s=0;s<num;s++) {
1052                 int val = swf_GetU30(tag);
1053                 U8 kind = swf_GetU8(tag); // specifies index type for "val"
1054             }
1055         }
1056         if(m->flags&0x80) {
1057             /* debug information- not used by avm2 */
1058             multiname_list_t*l = m->parameters;
1059             while(l) {
1060                 char*name = pool_lookup_string(pool, swf_GetU30(tag));
1061                 l = l->next;
1062             }
1063         }
1064         array_append(file->methods, m->name, m);
1065     }
1066             
1067     parse_metadata(tag, file, pool);
1068         
1069     /* skip classes, and scripts for now, and do the real parsing later */
1070     int num_classes = swf_GetU30(tag);
1071     int classes_pos = tag->pos;
1072     DEBUG printf("%d classes\n", num_classes);
1073     for(t=0;t<num_classes;t++) {
1074         abc_class_t*cls = malloc(sizeof(abc_class_t));
1075         memset(cls, 0, sizeof(abc_class_t));
1076         
1077         DEBUG printf("class %d\n", t);
1078         int name_index = swf_GetU30(tag); //classname
1079         char*name = pool_lookup_string(pool, name_index);
1080         
1081         array_append(file->classes, name, cls);
1082
1083         swf_GetU30(tag); //supername
1084         cls->flags = swf_GetU8(tag);
1085         if(cls->flags&8) 
1086             swf_GetU30(tag); //protectedNS
1087         int s;
1088         int inum = swf_GetU30(tag); //interface count
1089         cls->interfaces = 0;
1090         for(s=0;s<inum;s++) {
1091             int interface_index = swf_GetU30(tag);
1092             multiname_t* m = multiname_clone(pool_lookup_multiname(pool, interface_index));
1093             list_append(cls->interfaces, m);
1094             DEBUG printf("  class %d interface: %s\n", t, m->name);
1095         }
1096
1097         swf_GetU30(tag); //iinit
1098         traits_skip(tag);
1099     }
1100     for(t=0;t<num_classes;t++) {
1101         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1102         int cinit = swf_GetU30(tag);
1103         cls->static_constructor = (abc_method_t*)array_getvalue(file->methods, cinit);
1104         traits_skip(tag);
1105     }
1106     int num_scripts = swf_GetU30(tag);
1107     DEBUG printf("%d scripts\n", num_scripts);
1108     for(t=0;t<num_scripts;t++) {
1109         int init = swf_GetU30(tag);
1110         traits_skip(tag);
1111     }
1112
1113     int num_method_bodies = swf_GetU30(tag);
1114     DEBUG printf("%d method bodies\n", num_method_bodies);
1115     for(t=0;t<num_method_bodies;t++) {
1116         int methodnr = swf_GetU30(tag);
1117         if(methodnr >= file->methods->num) {
1118             printf("Invalid method number: %d\n", methodnr);
1119             return 0;
1120         }
1121         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, methodnr);
1122         abc_method_body_t*c = malloc(sizeof(abc_method_body_t));
1123         memset(c, 0, sizeof(abc_method_body_t));
1124         c->max_stack = swf_GetU30(tag);
1125         c->local_count = swf_GetU30(tag);
1126         c->init_scope_depth = swf_GetU30(tag);
1127         c->max_scope_depth = swf_GetU30(tag);
1128         int code_length = swf_GetU30(tag);
1129
1130         c->method = m;
1131         m->body = c;
1132
1133         int pos = tag->pos + code_length;
1134         c->code = code_parse(tag, code_length, file, pool);
1135         tag->pos = pos;
1136
1137         int exception_count = swf_GetU30(tag);
1138         int s;
1139         for(s=0;s<exception_count;s++) {
1140             swf_GetU30(tag); //from
1141             swf_GetU30(tag); //to
1142             swf_GetU30(tag); //target
1143             swf_GetU30(tag); //exc_type
1144             swf_GetU30(tag); //var_name
1145         }
1146         c->traits = traits_parse(tag, pool, file);
1147
1148         DEBUG printf("method_body %d) (method %d), %d bytes of code", t, methodnr, code_length);
1149
1150         array_append(file->method_bodies, m->name, c);
1151     }
1152     if(tag->len - tag->pos) {
1153         fprintf(stderr, "%d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
1154         return 0;
1155     }
1156
1157     swf_SetTagPos(tag, classes_pos);
1158     for(t=0;t<num_classes;t++) {
1159         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1160
1161         int classname_index = swf_GetU30(tag);
1162         int superclass_index = swf_GetU30(tag);
1163         cls->classname = multiname_clone(pool_lookup_multiname(pool, classname_index));
1164         cls->superclass = multiname_clone(pool_lookup_multiname(pool, superclass_index));
1165         cls->flags = swf_GetU8(tag);
1166         const char*ns = "";
1167         if(cls->flags&8) {
1168             int ns_index = swf_GetU30(tag);
1169             cls->protectedNS = namespace_clone(pool_lookup_namespace(pool, ns_index));
1170         }
1171         
1172         int num_interfaces = swf_GetU30(tag); //interface count
1173         int s;
1174         for(s=0;s<num_interfaces;s++) {
1175             swf_GetU30(tag); // multiname index TODO
1176         }
1177         int iinit = swf_GetU30(tag);
1178         cls->constructor = (abc_method_t*)array_getvalue(file->methods, iinit);
1179         cls->traits = traits_parse(tag, pool, file);
1180     }
1181     for(t=0;t<num_classes;t++) {
1182         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1183         /* SKIP */
1184         swf_GetU30(tag); // cindex
1185         cls->static_constructor_traits = traits_parse(tag, pool, file);
1186     }
1187     int num_scripts2 = swf_GetU30(tag);
1188     for(t=0;t<num_scripts2;t++) {
1189         int init = swf_GetU30(tag);
1190         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, init);
1191         
1192         abc_script_t*s = malloc(sizeof(abc_script_t));
1193         memset(s, 0, sizeof(abc_script_t));
1194         s->method = m;
1195         s->traits = traits_parse(tag, pool, file);
1196         array_append(file->scripts, "script", s);
1197         if(!s->traits) {
1198             fprintf(stderr, "Can't parse script traits\n");
1199             return 0;
1200         }
1201     }
1202
1203     pool_destroy(pool);
1204     return file;
1205 }
1206
1207 static void code_write(TAG*tag, abc_code_t*code, pool_t*pool, abc_file_t*file)
1208 {
1209     int len = 0;
1210     abc_code_t*c = code;
1211     while(c) {
1212         len += opcode_write(0, c, pool, file);
1213         c = c->next;
1214     }
1215     swf_SetU30(tag, len);
1216     int pos = tag->len;
1217     c = code;
1218     while(c) {
1219         opcode_write(tag, c, pool, file);
1220         c = c->next;
1221     }
1222     assert(tag->len - pos == len);
1223 }
1224
1225 void swf_WriteABC(TAG*abctag, void*code)
1226 {
1227     abc_file_t*file = (abc_file_t*)code;
1228     pool_t*pool = pool_new();
1229
1230     TAG*tmp = swf_InsertTag(0,0);
1231     TAG*tag = tmp;
1232     int t;
1233
1234     swf_SetU30(tag, file->methods->num);
1235    
1236     /* enumerate classes, methods and method bodies */
1237     for(t=0;t<file->methods->num;t++) {
1238         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1239         m->index = t;
1240     }
1241     for(t=0;t<file->classes->num;t++) {
1242         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1243         c->index = t;
1244     }
1245     for(t=0;t<file->method_bodies->num;t++) {
1246         abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1247         m->index = t;
1248     }
1249
1250     for(t=0;t<file->methods->num;t++) {
1251         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1252         int n = 0;
1253         multiname_list_t*l = m->parameters;
1254         int num_params = list_length(m->parameters);
1255         swf_SetU30(tag, num_params);
1256         if(m->return_type) 
1257             swf_SetU30(tag, pool_register_multiname(pool, m->return_type));
1258         else
1259             swf_SetU30(tag, 0);
1260         int s;
1261         while(l) {
1262             swf_SetU30(tag, pool_register_multiname(pool, l->multiname));
1263             l = l->next;
1264         }
1265         if(m->name) {
1266             swf_SetU30(tag, pool_register_string(pool, m->name));
1267         } else {
1268             swf_SetU30(tag, 0);
1269         }
1270
1271         swf_SetU8(tag, 0); //flags
1272     }
1273    
1274     /* write metadata */
1275     swf_SetU30(tag, file->metadata->num);
1276     for(t=0;t<file->metadata->num;t++) {
1277         const char*entry_name = array_getkey(file->metadata, t);
1278         swf_SetU30(tag, pool_register_string(pool, entry_name));
1279         array_t*items = (array_t*)array_getvalue(file->metadata, t);
1280         swf_SetU30(tag, items->num);
1281         int s;
1282         for(s=0;s<items->num;s++) {
1283             int i1 = pool_register_string(pool, array_getkey(items, s));
1284             int i2 = pool_register_string(pool, array_getvalue(items, s));
1285             swf_SetU30(tag, i1);
1286             swf_SetU30(tag, i2);
1287         }
1288     }
1289
1290     swf_SetU30(tag, file->classes->num);
1291     for(t=0;t<file->classes->num;t++) {
1292         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1293    
1294         int classname_index = pool_register_multiname(pool, c->classname);
1295         int superclass_index = pool_register_multiname(pool, c->superclass);
1296
1297         swf_SetU30(tag, classname_index);
1298         swf_SetU30(tag, superclass_index);
1299
1300         swf_SetU8(tag, c->flags); // flags
1301         if(c->flags&0x08) {
1302             int ns_index = pool_register_namespace(pool, c->protectedNS);
1303             swf_SetU30(tag, ns_index);
1304         }
1305
1306         swf_SetU30(tag, list_length(c->interfaces));
1307         multiname_list_t*interface= c->interfaces;
1308         while(interface) {
1309             swf_SetU30(tag, pool_register_multiname(pool, interface->multiname));
1310             interface = interface->next;
1311         }
1312
1313         if(!c->constructor) {
1314             fprintf(stderr, "Error: Class %s has no constructor\n", c->classname);
1315             return;
1316         }
1317         swf_SetU30(tag, c->constructor->index);
1318         traits_write(pool, tag, c->traits);
1319     }
1320     for(t=0;t<file->classes->num;t++) {
1321         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1322         if(!c->static_constructor) {
1323             fprintf(stderr, "Error: Class %s has no static constructor\n", c->classname);
1324             return;
1325         }
1326         swf_SetU30(tag, c->static_constructor->index);
1327         traits_write(pool, tag, c->static_constructor_traits);
1328     }
1329
1330     swf_SetU30(tag, file->scripts->num);
1331     for(t=0;t<file->scripts->num;t++) {
1332         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1333         swf_SetU30(tag, s->method->index); //!=t!
1334         traits_write(pool, tag, s->traits);
1335     }
1336
1337     swf_SetU30(tag, file->method_bodies->num);
1338     for(t=0;t<file->method_bodies->num;t++) {
1339         abc_method_body_t*c = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1340         abc_method_t*m = c->method;
1341         swf_SetU30(tag, m->index);
1342         swf_SetU30(tag, c->max_stack);
1343         swf_SetU30(tag, c->local_count);
1344         swf_SetU30(tag, c->init_scope_depth);
1345         swf_SetU30(tag, c->max_scope_depth);
1346
1347         code_write(tag, c->code, pool, file);
1348
1349         swf_SetU30(tag, c->exception_count);
1350         traits_write(pool, tag, c->traits);
1351     }
1352
1353     // --- start to write real tag --
1354     
1355     tag = abctag;
1356
1357     if(tag->id == ST_DOABC) {
1358         swf_SetU32(tag, 1); // flags
1359         swf_SetString(tag, file->name);
1360     }
1361
1362     swf_SetU16(tag, 0x10); //version
1363     swf_SetU16(tag, 0x2e);
1364     
1365     pool_write(pool, tag);
1366     
1367     swf_SetBlock(tag, tmp->data, tmp->len);
1368
1369     swf_DeleteTag(0, tmp);
1370     pool_destroy(pool);
1371 }
1372
1373 void swf_FreeABC(void*code)
1374 {
1375     abc_file_t*file= (abc_file_t*)code;
1376
1377     int t;
1378     for(t=0;t<file->metadata->num;t++) {
1379         array_t*items = (array_t*)array_getvalue(file->metadata, t);
1380         int s;
1381         for(s=0;s<items->num;s++) {
1382             free(array_getvalue(items, s));
1383         }
1384         array_free(items);
1385     }
1386     array_free(file->metadata);
1387
1388     for(t=0;t<file->methods->num;t++) {
1389         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1390
1391         multiname_list_t*param = m->parameters;
1392         while(param) {
1393             multiname_destroy(param->multiname);param->multiname=0;
1394             param = param->next;
1395         }
1396         list_free(m->parameters);m->parameters=0;
1397
1398         if(m->name) {
1399             free((void*)m->name);m->name=0;
1400         }
1401         if(m->return_type) {
1402             multiname_destroy(m->return_type);
1403         }
1404         free(m);
1405     }
1406     array_free(file->methods);
1407
1408     for(t=0;t<file->classes->num;t++) {
1409         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1410         traits_free(cls->traits);cls->traits=0;
1411         traits_free(cls->static_constructor_traits);cls->static_constructor_traits=0;
1412
1413         if(cls->classname) {
1414             multiname_destroy(cls->classname);
1415         }
1416         if(cls->superclass) {
1417             multiname_destroy(cls->superclass);
1418         }
1419
1420         multiname_list_t*i = cls->interfaces;
1421         while(i) {
1422             multiname_destroy(i->multiname);i->multiname=0;
1423             i = i->next;
1424         }
1425         list_free(cls->interfaces);cls->interfaces=0;
1426
1427         if(cls->protectedNS) {
1428             namespace_destroy(cls->protectedNS);
1429         }
1430         free(cls);
1431     }
1432     array_free(file->classes);
1433
1434     for(t=0;t<file->scripts->num;t++) {
1435         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1436         traits_free(s->traits);s->traits=0;
1437         free(s);
1438     }
1439     array_free(file->scripts);
1440
1441     for(t=0;t<file->method_bodies->num;t++) {
1442         abc_method_body_t*body = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1443         code_free(body->code);body->code=0;
1444         traits_free(body->traits);body->traits=0;
1445         free(body);
1446     }
1447     array_free(file->method_bodies);
1448
1449     if(file->name) {
1450         free((void*)file->name);file->name=0;
1451     }
1452
1453     free(file);
1454 }
1455
1456 abc_code_t* add_opcode(abc_code_t*atag, U8 op)
1457 {
1458     abc_code_t*tmp = (abc_code_t*)malloc(sizeof(abc_code_t));
1459     tmp->opcode = op;
1460     tmp->next = 0;
1461     if(atag) {
1462         tmp->prev = atag;
1463         atag->next = tmp;
1464         tmp->parent = atag->parent;
1465     } else {
1466         tmp->prev = 0;
1467         tmp->parent = tmp;
1468     }
1469     return tmp;
1470 }
1471
1472 #include "abc_ops.c"
1473
1474 void swf_AddButtonLinks(SWF*swf, char stop_each_frame, char events)
1475 {
1476     int num_frames = 0;
1477     int has_buttons = 0;
1478     TAG*tag=swf->firstTag;
1479     while(tag) {
1480         if(tag->id == ST_SHOWFRAME)
1481             num_frames++;
1482         if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2)
1483             has_buttons = 1;
1484         tag = tag->next;
1485     }
1486
1487     abc_file_t*file = abc_file_new();
1488     abc_method_body_t*c = 0;
1489    
1490     abc_class_t*cls = abc_class_new2(file, "rfx::MainTimeline", "flash.display::MovieClip");
1491     abc_class_protectedNS(cls, "rfx:MainTimeline");
1492   
1493     TAG*abctag = swf_InsertTagBefore(swf, swf->firstTag, ST_DOABC);
1494     
1495     tag = swf_InsertTag(abctag, ST_SYMBOLCLASS);
1496     swf_SetU16(tag, 1);
1497     swf_SetU16(tag, 0);
1498     swf_SetString(tag, "rfx.MainTimeline");
1499
1500     c = abc_class_staticconstructor(cls, 0, 0);
1501     c->max_stack = 1;
1502     c->local_count = 1;
1503     c->init_scope_depth = 9;
1504     c->max_scope_depth = 10;
1505
1506     __ getlocal_0(c);
1507     __ pushscope(c);
1508     __ returnvoid(c);
1509
1510     c = abc_class_constructor(cls, 0, 0);
1511     c->max_stack = 3;
1512     c->local_count = 1;
1513     c->init_scope_depth = 10;
1514     c->max_scope_depth = 11;
1515     
1516     debugfile(c, "constructor.as");
1517
1518     __ getlocal_0(c);
1519     __ pushscope(c);
1520
1521     __ getlocal_0(c);
1522     __ constructsuper(c,0);
1523
1524     __ getlex(c, "[package]flash.system::Security");
1525     __ pushstring(c, "*");
1526     __ callpropvoid(c, "[package]::allowDomain", 1);
1527     
1528     if(stop_each_frame || has_buttons) {
1529         int frame = 0;
1530         tag = swf->firstTag;
1531         abc_method_body_t*f = 0; //frame script
1532         while(tag && tag->id!=ST_END) {
1533             char framename[80];
1534             char needs_framescript=0;
1535             char buttonname[80];
1536             char functionname[80];
1537             sprintf(framename, "[packageinternal]rfx::frame%d", frame);
1538             
1539             if(!f && (tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2 || stop_each_frame)) {
1540                 /* make the contructor add a frame script */
1541                 __ findpropstrict(c,"[package]::addFrameScript");
1542                 __ pushbyte(c,frame);
1543                 __ getlex(c,framename);
1544                 __ callpropvoid(c,"[package]::addFrameScript",2);
1545
1546                 f = abc_class_method(cls, 0, framename, 0);
1547                 f->max_stack = 3;
1548                 f->local_count = 1;
1549                 f->init_scope_depth = 10;
1550                 f->max_scope_depth = 11;
1551                 __ debugfile(f, "framescript.as");
1552                 __ debugline(f, 1);
1553                 __ getlocal_0(f);
1554                 __ pushscope(f);
1555             }
1556
1557             if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1558                 U16 id = swf_GetDefineID(tag);
1559                 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1560                 __ getlex(f,buttonname);
1561                 __ getlex(f,"flash.events::MouseEvent");
1562                 __ getproperty(f, "::CLICK");
1563                 sprintf(functionname, "::clickbutton%d", swf_GetDefineID(tag));
1564                 __ getlex(f,functionname);
1565                 __ callpropvoid(f, "::addEventListener" ,2);
1566
1567                 if(stop_each_frame) {
1568                     __ findpropstrict(f, "[package]::stop");
1569                     __ callpropvoid(f, "[package]::stop", 0);
1570                 }
1571                 needs_framescript = 1;
1572
1573                 abc_method_body_t*h =
1574                     abc_class_method(cls, "::void", functionname, 1, "flash.events::MouseEvent");
1575                 h->max_stack = 6;
1576                 h->local_count = 2;
1577                 h->init_scope_depth = 10;
1578                 h->max_scope_depth = 11;
1579                 __ getlocal_0(h);
1580                 __ pushscope(h);
1581
1582                 ActionTAG*oldaction = swf_ButtonGetAction(tag);
1583                 if(oldaction && oldaction->op == ACTION__GOTOFRAME) {
1584                     int framenr = GET16(oldaction->data);
1585                     if(framenr>254) {
1586                         fprintf(stderr, "Warning: Couldn't translate jump to frame %d to flash 9 actionscript\n", framenr);
1587                     }
1588                     if(!events) {
1589                         __ findpropstrict(h,"[package]::gotoAndStop");
1590                         __ pushbyte(h,framenr+1);
1591                         __ callpropvoid(h,"[package]::gotoAndStop", 1);
1592                     } else {
1593                         char framename[80];
1594                         sprintf(framename, "frame%d", framenr);
1595                         __ getlocal_0(h); //this
1596                         __ findpropstrict(h, "[package]flash.events::TextEvent");
1597                         __ pushstring(h, "link");
1598                         __ pushtrue(h);
1599                         __ pushtrue(h);
1600                         __ pushstring(h, framename);
1601                         __ constructprop(h,"[package]flash.events::TextEvent", 4);
1602                         __ callpropvoid(h,"[package]::dispatchEvent", 1);
1603                     }
1604                 } else if(oldaction && oldaction->op == ACTION__GETURL) {
1605                     if(!events) {
1606                         __ findpropstrict(h,"flash.net::navigateToURL");
1607                         __ findpropstrict(h,"flash.net::URLRequest");
1608                         // TODO: target _blank
1609                         __ pushstring(h,oldaction->data); //url
1610                         __ constructprop(h,"flash.net::URLRequest", 1);
1611                         __ callpropvoid(h,"flash.net::navigateToURL", 1);
1612                     } else {
1613                         __ getlocal_0(h); //this
1614                         __ findpropstrict(h, "[package]flash.events::TextEvent");
1615                         __ pushstring(h, "link");
1616                         __ pushtrue(h);
1617                         __ pushtrue(h);
1618                         __ pushstring(h,oldaction->data); //url
1619                         __ constructprop(h,"[package]flash.events::TextEvent", 4);
1620                         __ callpropvoid(h,"[package]::dispatchEvent", 1);
1621                     }
1622                 } else if(oldaction) {
1623                     fprintf(stderr, "Warning: Couldn't translate button code of button %d to flash 9 abc action\n", id);
1624                 }
1625                 __ returnvoid(h);
1626                 swf_ActionFree(oldaction);
1627             }
1628             if(tag->id == ST_SHOWFRAME) {
1629                 if(f) {
1630                     __ returnvoid(f);
1631                     f = 0;
1632                 }
1633                 frame++;
1634             }
1635             tag = tag->next;
1636         }
1637         if(f) {
1638             __ returnvoid(f);
1639         }
1640     }
1641     __ returnvoid(c);
1642
1643     tag = swf->firstTag;
1644     while(tag) {
1645         if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1646             char buttonname[80];
1647             sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1648             abc_AddSlot(cls, buttonname, 0, "flash.display::SimpleButton");
1649         }
1650         tag = tag->next;
1651     }
1652
1653
1654     abc_script_t*s = abc_initscript(file, 0, 0);
1655     c = s->method->body;
1656     c->max_stack = 2;
1657     c->local_count = 1;
1658     c->init_scope_depth = 1;
1659     c->max_scope_depth = 9;
1660
1661     __ getlocal_0(c);
1662     __ pushscope(c);
1663     __ getscopeobject(c, 0);
1664     __ getlex(c,"::Object");
1665     __ pushscope(c);
1666     __ getlex(c,"flash.events::EventDispatcher");
1667     __ pushscope(c);
1668     __ getlex(c,"flash.display::DisplayObject");
1669     __ pushscope(c);
1670     __ getlex(c,"flash.display::InteractiveObject");
1671     __ pushscope(c);
1672     __ getlex(c,"flash.display::DisplayObjectContainer");
1673     __ pushscope(c);
1674     __ getlex(c,"flash.display::Sprite");
1675     __ pushscope(c);
1676     __ getlex(c,"flash.display::MovieClip");
1677     __ pushscope(c);
1678     __ getlex(c,"flash.display::MovieClip");
1679     __ newclass(c,cls);
1680     __ popscope(c);
1681     __ popscope(c);
1682     __ popscope(c);
1683     __ popscope(c);
1684     __ popscope(c);
1685     __ popscope(c);
1686     __ popscope(c);
1687     __ initproperty(c,"rfx::MainTimeline");
1688     __ returnvoid(c);
1689
1690     //abc_method_body_addClassTrait(c, "rfx:MainTimeline", 1, cls);
1691     abc_initscript_addClassTrait(s, "rfx::MainTimeline", 1, cls);
1692
1693     swf_WriteABC(abctag, file);
1694 }
1695