fixed swf_AddButtonLinks()
[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 /* TODO: switch to a datastructure with just values */
222 #define NO_KEY ""
223
224 abc_code_t*code_parse(TAG*tag, int len, abc_file_t*file, pool_t*pool)
225 {
226     abc_code_t*head=0;
227     abc_code_t*code=0;
228     int end=tag->pos+len;
229     //printf("-->\n");fflush(stdout);
230     while(tag->pos<end) {
231         U8 opcode = swf_GetU8(tag);
232         opcode_t*op = opcode_get(opcode);
233         if(!op) {
234             fprintf(stderr, "Can't parse opcode %02x\n", opcode);
235             return head;
236         }
237         //printf("%s\n", op->name);fflush(stdout);
238         NEW(abc_code_t,c);
239         if(!head) {
240             head = code = c;
241         } else {
242             code->next = c;
243             code = c;
244         }
245
246         c->opcode = opcode;
247
248         char*p = op->params;
249         int pos = 0;
250         while(*p) {
251             void*data = 0;
252             if(*p == 'n') { // number
253                 data = (void*)(ptroff_t)swf_GetU30(tag);
254             } else if(*p == '2') { //multiname
255                 data = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
256             } else if(*p == 'm') { //method
257                 data = array_getvalue(file->methods, swf_GetU30(tag));
258             } else if(*p == 'c') { //classinfo
259                 data = array_getvalue(file->classes, swf_GetU30(tag));
260             } else if(*p == 'i') { //methodbody TODO
261                 data = array_getvalue(file->method_bodies, swf_GetU30(tag));
262             } else if(*p == 'u') { // lookoup TODO
263                 data = (void*)(ptroff_t)swf_GetU30(tag);
264             } else if(*p == 'b') { // byte
265                 data = (void*)(ptroff_t)swf_GetU8(tag);
266             } else if(*p == 'j') { // jump TODO
267                 data = (void*)(ptroff_t)swf_GetS24(tag);
268             } else if(*p == 's') { // string
269                 data = strdup((char*)pool_lookup_string(pool, swf_GetU30(tag)));
270             } else if(*p == 'D') { // debug
271                 /*type, usually 1*/
272                 U8 type = swf_GetU8(tag);
273                 if(type!=1) 
274                     fprintf(stderr, "Unknown debug type: %02x\n", type);
275                 /*register name*/
276                 code->params[0] = strdup((char*)pool_lookup_string(pool, swf_GetU30(tag)));
277                 /*register index*/
278                 code->params[1] = (void*)(ptroff_t)swf_GetU8(tag);
279                 /*unused*/
280                 swf_GetU30(tag);
281             } else if(*p == 'S') { // switch statement TODO
282                 /* I hate these things */
283                 swf_GetU24(tag); //default
284                 int num = swf_GetU30(tag)+1;
285                 int t;
286                 for(t=0;t<num;t++) 
287                     swf_GetU24(tag);
288                 data = 0;
289             } else {
290                 printf("Can't parse opcode param type \"%c\"\n", *p);
291                 return 0;
292             }
293             if(data)
294                 code->params[pos++] = data;
295             p++;
296         }
297     }
298     return head;
299 }
300
301 static void code_free(abc_code_t*c)
302 {
303     while(c) {
304         abc_code_t*next = c->next;
305         opcode_t*op = opcode_get(c->opcode);
306         char*p = op?op->params:"";
307         int pos=0;
308         while(*p) {
309             void*data = c->params[pos];
310             if(*p == '2') { //multiname
311                 multiname_destroy(data);
312             } else if(strchr("sD", *p)) {
313                 free(data);
314             }
315             c->params[pos]=0;
316             p++;pos++;
317         }
318         memset(c, 0, sizeof(c));
319         free(c);
320         c = next;
321     }
322 }
323
324 static int code_dump(abc_code_t*c, abc_file_t*file, char*prefix, FILE*fo)
325 {
326     pool_t*pool = pool_new();
327
328     while(c) {
329         U8 opcode = c->opcode;
330         int t;
331         char found = 0;
332         opcode_t*op = opcode_get(opcode);
333         if(!op) {
334             fprintf(stderr, "Can't parse opcode %02x.\n", opcode);
335             return 0;
336         } else {
337             fprintf(fo, "%s%s ", prefix, op->name);
338             char*p = op->params;
339             char first = 1;
340             int pos=0;
341             while(*p) {
342                 void*data = c->params[pos];
343                 if(pos>0)
344                     printf(", ");
345
346                 if(*p == 'n') {
347                     int n = (ptroff_t)data;
348                     fprintf(fo, "%d params", n);
349                 } else if(*p == '2') {
350                     multiname_t*n = (multiname_t*)data;
351                     char* m = multiname_to_string(n);
352                     fprintf(fo, "%s", m);
353                     free(m);
354                 } else if(*p == 'm') {
355                     abc_method_t*m = (abc_method_t*)data;
356                     fprintf(fo, "[method %s]", m->name);
357                 } else if(*p == 'c') {
358                     abc_class_t*cls = (abc_class_t*)data;
359                     char*classname = multiname_to_string(cls->classname);
360                     fprintf(fo, "[classinfo %s]", classname);
361                     free(classname);
362                 } else if(*p == 'i') {
363                     abc_method_body_t*b = (abc_method_body_t*)data;
364                     fprintf(fo, "[methodbody]");
365                 } else if(*p == 'u') {
366                     int n = (ptroff_t)data;
367                     fprintf(fo, "%d", n);
368                 } else if(*p == 'b') {
369                     int b = (ptroff_t)data;
370                     fprintf(fo, "%02x", b);
371                 } else if(*p == 'j') {
372                     int n = (ptroff_t)data;
373                     fprintf(fo, "%d", n);
374                 } else if(*p == 's') {
375                     fprintf(fo, "\"%s\"", data);
376                 } else if(*p == 'D') {
377                     fprintf(fo, "[register %02x=%s]", (ptroff_t)c->params[1], (char*)c->params[0]);
378                 } else if(*p == 'S') {
379                     fprintf(fo, "[switch data]");
380                 } else {
381                     fprintf(stderr, "Can't parse opcode param type \"%c\"\n", *p);
382                     return 0;
383                 }
384                 p++;
385                 pos++;
386                 first = 0;
387             }
388             fprintf(fo, "\n");
389         }
390         c = c->next;
391     }
392     return 1;
393 }
394
395 static int opcode_write(TAG*tag, abc_code_t*code, pool_t*pool, abc_file_t*file)
396 {
397     opcode_t*c = opcode_get(code->opcode);
398     char*p = c->params;
399     int pos = 0;
400     int len = 0;
401     
402     if(tag)
403         swf_SetU8(tag, code->opcode);
404     len++;
405
406     while(*p) {
407         void*data = code->params[pos++];
408         assert(pos<=2);
409         if(*p == 'n') { // number
410             len += swf_SetU30(tag, (ptroff_t)data);
411         } else if(*p == '2') { //multiname
412             multiname_t*m = (multiname_t*)data;
413             len += swf_SetU30(tag, pool_register_multiname(pool, m));
414         } else if(*p == 'm') { //method
415             abc_method_t*m = (abc_method_t*)data;
416             len += swf_SetU30(tag, m->index);
417         } else if(*p == 'c') { //classinfo 
418             abc_class_t*cls = (abc_class_t*)data;
419             len += swf_SetU30(tag, cls->index);
420         } else if(*p == 'i') { //methodbody
421             abc_method_body_t*m = (abc_method_body_t*)data;
422             len += swf_SetU30(tag, m->index);
423         } else if(*p == 'u') { // integer
424             len += swf_SetU30(tag, (ptroff_t)data);
425         } else if(*p == 'b') { // byte
426             if(tag)
427                 swf_SetU8(tag, (ptroff_t)data);
428             len++;
429         } else if(*p == 'j') { // jump
430             len += swf_SetS24(tag, (ptroff_t)data);
431         } else if(*p == 's') { // string
432             int index = pool_register_string(pool, data);
433             len += swf_SetU30(tag, index);
434         } else if(*p == 'D') { // debug statement
435             if(tag)
436                 swf_SetU8(tag, 1);
437             len++;
438             len+=swf_SetU30(tag, pool_register_string(pool,code->params[0]));
439             if(tag)
440                 swf_SetU8(tag, (ptroff_t)code->params[1]);
441             len++;
442             len+=swf_SetU30(tag, 0);
443         } else if(*p == 'S') { // switch statement
444             len+=swf_SetU24(tag, 0); //default
445             len+=swf_SetU30(tag, 0); //nr-1
446             len+=swf_SetU24(tag, 0); //first
447         } else {
448             printf("Can't parse opcode param type \"%c\"\n", *p);
449         }
450         p++;
451     }
452     return len;
453 }
454
455
456 static char* params_to_string(multiname_list_t*list)
457 {
458     multiname_list_t*l;
459     int n;
460
461     l = list;
462     n = 0;
463     while(list) {
464         n++;list=list->next;
465     }
466
467     char**names = (char**)malloc(sizeof(char*)*n);
468     
469     l = list;
470     n = 0;
471     int size = 0;
472     while(list) {
473         names[n] = multiname_to_string(list->multiname);
474         size += strlen(names[n]) + 2;
475         n++;list=list->next;
476     }
477
478     char* params = malloc(size+5);
479     params[0]='(';
480     params[1]=0;
481     l = list;
482     int s=0;
483     n = 0;
484     while(list) {
485         if(s)
486             strcat(params, ", ");
487         strcat(params, names[n]);
488         free(names[n]);
489         n++;
490         s=1;
491     }
492     free(names);
493     strcat(params, ")");
494     int t;
495     return params;
496 }
497
498 //#define DEBUG
499 #define DEBUG if(0)
500
501 static void parse_metadata(TAG*tag, abc_file_t*file, pool_t*pool)
502 {
503     int t;
504     int num_metadata = swf_GetU30(tag);
505
506     DEBUG printf("%d metadata\n");
507     for(t=0;t<num_metadata;t++) {
508         const char*entry_name = pool_lookup_string(pool, swf_GetU30(tag));
509         int num = swf_GetU30(tag);
510         int s;
511         DEBUG printf("  %s\n", entry_name);
512         array_t*items = array_new();
513         for(s=0;s<num;s++) {
514             int i1 = swf_GetU30(tag);
515             int i2 = swf_GetU30(tag);
516             char*key = i1?pool_lookup_string(pool, i1):"";
517             char*value = i2?pool_lookup_string(pool, i2):"";
518             DEBUG printf("    %s=%s\n", key, value);
519             array_append(items, key, strdup(value));
520         }
521         array_append(file->metadata, entry_name, items);
522     }
523 }
524
525 void swf_CopyData(TAG*to, TAG*from, int len)
526 {
527     unsigned char*data = malloc(len);
528     swf_GetBlock(from, data, len);
529     swf_SetBlock(to, data, len);
530     free(data);
531 }
532
533 abc_file_t*abc_file_new()
534 {
535     abc_file_t*f = malloc(sizeof(abc_file_t));
536     memset(f, 0, sizeof(abc_file_t));
537     f->metadata = array_new();
538
539     f->methods = array_new();
540     f->classes = array_new();
541     f->scripts = array_new();
542     f->method_bodies = array_new();
543
544     return f;
545 }
546
547 #define CLASS_SEALED 1
548 #define CLASS_FINAL 2
549 #define CLASS_INTERFACE 4
550 #define CLASS_PROTECTED_NS 8
551
552 abc_class_t* abc_class_new(abc_file_t*pool, multiname_t*classname, multiname_t*superclass) {
553     
554     NEW(abc_class_t,c);
555     array_append(pool->classes, NO_KEY, c);
556
557     c->pool = pool;
558     c->classname = classname;
559     c->superclass = superclass;
560     c->flags = 0;
561     c->constructor = 0;
562     c->static_constructor = 0;
563     c->traits = list_new();
564     return c;
565 }
566 abc_class_t* abc_class_new2(abc_file_t*pool, char*classname, char*superclass) 
567 {
568     return abc_class_new(pool, multiname_fromstring(classname), multiname_fromstring(superclass));
569 }
570
571 void abc_class_sealed(abc_class_t*c)
572 {
573     c->flags |= CLASS_SEALED;
574 }
575 void abc_class_final(abc_class_t*c)
576 {
577     c->flags |= CLASS_FINAL;
578 }
579 void abc_class_interface(abc_class_t*c)
580 {
581     c->flags |= CLASS_INTERFACE;
582 }
583 void abc_class_protectedNS(abc_class_t*c, char*namespace)
584 {
585     c->protectedNS = namespace_new_protected(namespace);
586     c->flags |= CLASS_PROTECTED_NS;
587 }
588 void abc_class_add_interface(abc_class_t*c, multiname_t*interface)
589 {
590     list_append(c->interfaces, interface);
591 }
592
593 abc_method_body_t* add_method(abc_file_t*pool, abc_class_t*cls, char*returntype, int num_params, va_list va)
594 {
595     /* construct code (method body) object */
596     NEW(abc_method_body_t,c);
597     array_append(pool->method_bodies, NO_KEY, c);
598     c->pool = pool;
599     c->traits = list_new();
600     c->code = 0;
601
602     /* construct method object */
603     NEW(abc_method_t,m);
604     array_append(pool->methods, NO_KEY, m);
605
606     if(returntype && strcmp(returntype, "void")) {
607         m->return_type = multiname_fromstring(returntype);
608     } else {
609         m->return_type = 0;
610     }
611     int t;
612     for(t=0;t<num_params;t++) {
613         const char*param = va_arg(va, const char*);
614         list_append(m->parameters, multiname_fromstring(param));
615     }
616
617     /* crosslink the two objects */
618     m->body = c;
619     c->method = m;
620
621     return c;
622 }
623
624 abc_method_body_t* abc_class_constructor(abc_class_t*cls, char*returntype, int num_params, ...) 
625 {
626     va_list va;
627     va_start(va, num_params);
628     abc_method_body_t* c = add_method(cls->pool, cls, returntype, num_params, va);
629     va_end(va);
630     cls->constructor = c->method;
631     return c;
632 }
633
634 abc_method_body_t* abc_class_staticconstructor(abc_class_t*cls, char*returntype, int num_params, ...) 
635 {
636     va_list va;
637     va_start(va, num_params);
638     abc_method_body_t* c = add_method(cls->pool, cls, returntype, num_params, va);
639     va_end(va);
640     cls->static_constructor = c->method;
641     return c;
642 }
643
644 trait_t*trait_new(int type, multiname_t*name, int data1, int data2, int vindex, int vkind)
645 {
646     trait_t*trait = malloc(sizeof(trait_t));
647     memset(trait, 0, sizeof(trait_t));
648     trait->kind = type&0x0f;
649     trait->attributes = type&0xf0;
650     trait->name = name;
651     trait->data1 = data1;
652     trait->data2 = data2;
653     trait->vindex = vindex;
654     trait->vkind = vkind;
655     return trait;
656 }
657 trait_t*trait_new_method(multiname_t*name, abc_method_t*m)
658 {
659     int type = TRAIT_METHOD;
660     trait_t*trait = malloc(sizeof(trait_t));
661     memset(trait, 0, sizeof(trait_t));
662     trait->kind = type&0x0f;
663     trait->attributes = type&0xf0;
664     trait->name = name;
665     trait->method = m;
666     return trait;
667 }
668
669 abc_method_body_t* abc_class_method(abc_class_t*cls, char*returntype, char*name, int num_params, ...)
670 {
671     abc_file_t*pool = cls->pool;
672     va_list va;
673     va_start(va, num_params);
674     abc_method_body_t* c = add_method(cls->pool, cls, returntype, num_params, va);
675     va_end(va);
676     list_append(cls->traits, trait_new_method(multiname_fromstring(name), c->method));
677     return c;
678 }
679
680 void abc_AddSlot(abc_class_t*cls, char*name, int slot, char*multiname)
681 {
682     abc_file_t*pool = cls->pool;
683     multiname_t*m = multiname_fromstring(multiname);
684     list_append(cls->traits, trait_new(TRAIT_SLOT, m, slot, 0, 0, 0));
685 }
686
687 void abc_method_body_addClassTrait(abc_method_body_t*code, char*multiname, int slotid, abc_class_t*cls)
688 {
689     abc_file_t*pool = code->pool;
690     multiname_t*m = multiname_fromstring(multiname);
691     trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0, 0);
692     trait->cls = cls;
693     list_append(code->traits, trait);
694 }
695
696 /* notice: traits of a method (body) belonging to an init script
697    and traits of the init script are *not* the same thing */
698 void abc_initscript_addClassTrait(abc_script_t*script, char*multiname, int slotid, abc_class_t*cls)
699 {
700     abc_file_t*pool = script->pool;
701     multiname_t*m = multiname_fromstring(multiname);
702     trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0, 0);
703     trait->cls = cls;
704     list_append(script->traits, trait);
705 }
706
707 abc_script_t* abc_initscript(abc_file_t*pool, char*returntype, int num_params, ...) 
708 {
709     va_list va;
710     va_start(va, num_params);
711     abc_method_body_t* c = add_method(pool, 0, returntype, num_params, va);
712     abc_script_t* s = malloc(sizeof(abc_script_t));
713     s->method = c->method;
714     s->traits = list_new();
715     s->pool = pool;
716     array_append(pool->scripts, NO_KEY, s);
717     va_end(va);
718     return s;
719 }
720
721 static void dump_traits(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file);
722
723 static void dump_method(FILE*fo, const char*prefix, const char*type, const char*name, abc_method_t*m, abc_file_t*file)
724 {
725     const char*return_type = "void";
726     if(m->return_type)
727         return_type = multiname_to_string(m->return_type);
728
729     char*paramstr = params_to_string(m->parameters);
730
731     fprintf(fo, "%s%s %s %s=%s %s\n", prefix, type, return_type, name, m->name, paramstr);
732
733     abc_method_body_t*c = m->body;
734     if(!c) {
735         return;
736     }
737     
738     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);
739
740     char prefix2[80];
741     sprintf(prefix2, "%s    ", prefix);
742     if(c->traits)
743         dump_traits(fo, prefix, c->traits, file);
744     fprintf(fo, "%s{\n", prefix);
745     code_dump(c->code, file, prefix2, fo);
746     fprintf(fo, "%s}\n\n", prefix);
747 }
748
749 static void traits_free(trait_list_t*traits) 
750 {
751     trait_list_t*t = traits;
752     while(t) {
753         if(t->trait->name) {
754             multiname_destroy(t->trait->name);t->trait->name = 0;
755         }
756         if(t->trait->kind == TRAIT_SLOT || t->trait->kind == TRAIT_CONST) {
757             multiname_destroy(t->trait->type_name);
758         }
759         free(t->trait);t->trait = 0;
760         t = t->next;
761     }
762     list_free(traits);
763 }
764
765 static trait_list_t* traits_parse(TAG*tag, pool_t*pool, abc_file_t*file)
766 {
767     int num_traits = swf_GetU30(tag);
768     trait_list_t*traits = list_new();
769     int t;
770     if(num_traits) {
771         DEBUG printf("%d traits\n", num_traits);
772     }
773     
774     for(t=0;t<num_traits;t++) {
775         trait_t*trait = malloc(sizeof(trait_t));
776         memset(trait, 0, sizeof(trait_t));
777         list_append(traits, trait);
778
779         trait->name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag))); // always a QName (ns,name)
780
781         const char*name = 0;
782         DEBUG name = multiname_to_string(trait->name);
783         U8 kind = swf_GetU8(tag);
784         U8 attributes = kind&0xf0;
785         kind&=0x0f;
786         trait->kind = kind;
787         trait->attributes = attributes;
788         DEBUG printf("  trait %d) %s type=%02x\n", t, name, kind);
789         if(kind == TRAIT_METHOD || kind == TRAIT_GETTER || kind == TRAIT_SETTER) { // method / getter / setter
790             trait->disp_id = swf_GetU30(tag);
791             trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
792             DEBUG printf("  method/getter/setter\n");
793         } else if(kind == TRAIT_FUNCTION) { // function
794             trait->slot_id =  swf_GetU30(tag);
795             trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
796         } else if(kind == TRAIT_CLASS) { // class
797             trait->slot_id = swf_GetU30(tag);
798             trait->cls = (abc_class_t*)array_getvalue(file->classes, swf_GetU30(tag));
799             DEBUG printf("  class %s %d %d\n", name, trait->slot_id, trait->cls);
800         } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
801             /* a slot is a variable in a class that is shared amonst all instances
802                of the same type, but which has a unique location in each object 
803                (in other words, slots are non-static, traits are static)
804              */
805             trait->slot_id = swf_GetU30(tag);
806             trait->type_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
807             trait->vindex = swf_GetU30(tag);
808             if(trait->vindex) {
809                 trait->vkind = swf_GetU8(tag);
810             }
811             DEBUG printf("  slot %s %d %s (vindex=%d)\n", name, trait->slot_id, trait->type_name->name, trait->vindex);
812         } else {
813             fprintf(stderr, "Can't parse trait type %d\n", kind);
814         }
815         if(attributes&0x40) {
816             int num = swf_GetU30(tag);
817             int s;
818             for(s=0;s<num;s++) {
819                 swf_GetU30(tag); //index into metadata array
820             }
821         }
822     }
823     return traits;
824 }
825
826 void traits_skip(TAG*tag)
827 {
828     int num_traits = swf_GetU30(tag);
829     int t;
830     for(t=0;t<num_traits;t++) {
831         swf_GetU30(tag);
832         U8 kind = swf_GetU8(tag);
833         U8 attributes = kind&0xf0;
834         kind&=0x0f;
835         swf_GetU30(tag);
836         swf_GetU30(tag);
837         if(kind == TRAIT_SLOT || kind == TRAIT_CONST) {
838             if(swf_GetU30(tag)) swf_GetU8(tag);
839         } else if(kind>TRAIT_CONST) {
840             fprintf(stderr, "Can't parse trait type %d\n", kind);
841         }
842         if(attributes&0x40) {
843             int s, num = swf_GetU30(tag);
844             for(s=0;s<num;s++) swf_GetU30(tag);
845         }
846     }
847 }
848
849
850 static void traits_write(pool_t*pool, TAG*tag, trait_list_t*traits)
851 {
852     if(!traits) {
853         swf_SetU30(tag, 0);
854         return;
855     }
856     swf_SetU30(tag, list_length(traits));
857     int s;
858
859     while(traits) {
860         trait_t*trait = traits->trait;
861
862         swf_SetU30(tag, pool_register_multiname(pool, trait->name));
863         swf_SetU8(tag, trait->kind|trait->attributes);
864
865         swf_SetU30(tag, trait->data1);
866
867         if(trait->kind == TRAIT_CLASS) {
868             swf_SetU30(tag, trait->cls->index);
869         } else if(trait->kind == TRAIT_GETTER ||
870                   trait->kind == TRAIT_SETTER ||
871                   trait->kind == TRAIT_METHOD) {
872             swf_SetU30(tag, trait->method->index);
873         } else if(trait->kind == TRAIT_SLOT ||
874                   trait->kind == TRAIT_CONST) {
875             int index = pool_register_multiname(pool, trait->type_name);
876             swf_SetU30(tag, index);
877         } else  {
878             swf_SetU30(tag, trait->data2);
879         }
880
881         if(trait->kind == TRAIT_SLOT || trait->kind == TRAIT_CONST) {
882             swf_SetU30(tag, trait->vindex);
883             if(trait->vindex) {
884                 swf_SetU8(tag, trait->vkind);
885             }
886         }
887         if(trait->attributes&0x40) {
888             // metadata
889             swf_SetU30(tag, 0);
890         }
891         traits = traits->next;
892     }
893 }
894
895
896 static void dump_traits(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file)
897 {
898     int t;
899     while(traits) {
900         trait_t*trait = traits->trait;
901         char*name = multiname_to_string(trait->name);
902         U8 kind = trait->kind;
903         U8 attributes = trait->attributes;
904         if(kind == TRAIT_METHOD) {
905             abc_method_t*m = trait->method;
906             dump_method(fo, prefix, "method", name, m, file);
907         } else if(kind == TRAIT_GETTER) {
908             abc_method_t*m = trait->method;
909             dump_method(fo, prefix, "getter", name, m, file);
910         } else if(kind == TRAIT_SETTER) {
911             abc_method_t*m = trait->method;
912             dump_method(fo, prefix, "setter", name, m, file);
913         } else if(kind == TRAIT_FUNCTION) { // function
914             abc_method_t*m = trait->method;
915             dump_method(fo, prefix, "function", name, m, file);
916         } else if(kind == TRAIT_CLASS) { // class
917             abc_class_t*cls = trait->cls;
918             if(!cls) {
919                 fprintf(fo, "%sslot %d: class %s=class%d\n", prefix, trait->slot_id, name);
920             } else {
921                 fprintf(fo, "%sslot %d: class %s=%s\n", prefix, trait->slot_id, name, cls->classname->name);
922             }
923         } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
924             int slot_id = trait->slot_id;
925             char*type_name = multiname_to_string(trait->type_name);
926             fprintf(fo, "%sslot %s %d %s (vindex=%d)\n", prefix, name, trait->slot_id, type_name, trait->vindex);
927             free(type_name);
928         } else {
929             fprintf(fo, "%s    can't dump trait type %d\n", prefix, kind);
930         }
931         free(name);
932         traits=traits->next;
933     }
934 }
935
936 void* swf_DumpABC(FILE*fo, void*code, char*prefix)
937 {
938     abc_file_t* file = (abc_file_t*)code;
939         
940     if(file->name) {
941         fprintf(fo, "%s#\n", prefix);
942         fprintf(fo, "%s#name: %s\n", prefix, file->name);
943         fprintf(fo, "%s#\n", prefix);
944     }
945
946     int t;
947     for(t=0;t<file->metadata->num;t++) {
948         const char*entry_name = array_getkey(file->metadata, t);
949         fprintf(fo, "%s#Metadata \"%s\":\n", prefix, entry_name);
950         int s;
951         array_t*items = (array_t*)array_getvalue(file->metadata, t);
952         for(s=0;s<items->num;s++) {
953             fprintf(fo, "%s#  %s=%s\n", prefix, array_getkey(items, s), array_getvalue(items,s));
954         }
955         fprintf(fo, "%s#\n", prefix);
956     }
957
958     for(t=0;t<file->classes->num;t++) {
959         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
960         char prefix2[80];
961         sprintf(prefix2, "%s    ", prefix);
962
963         fprintf(fo, "%s", prefix);
964         if(cls->flags&1) fprintf(fo, "sealed ");
965         if(cls->flags&2) fprintf(fo, "final ");
966         if(cls->flags&4) fprintf(fo, "interface ");
967         if(cls->flags&8) {
968             char*s = namespace_to_string(cls->protectedNS);
969             fprintf(fo, "protectedNS(%s) ", s);
970             free(s);
971         }
972
973         char*classname = multiname_to_string(cls->classname);
974         fprintf(fo, "class %s", classname);
975         free(classname);
976         if(cls->superclass) {
977             char*supername = multiname_to_string(cls->superclass);
978             fprintf(fo, " extends %s", supername);
979             free(supername);
980             multiname_list_t*ilist = cls->interfaces;
981             if(ilist)
982                 fprintf(fo, " implements");
983             while(ilist) {
984                 char*s = multiname_to_string(ilist->multiname);
985                 fprintf(fo, " %s", s);
986                 free(s);
987                 ilist = ilist->next;
988             }
989             ilist->next;
990         }
991         if(cls->flags&0xf0) 
992             fprintf(fo, "extra flags=%02x\n", cls->flags&0xf0);
993         fprintf(fo, "%s{\n", prefix);
994
995         if(cls->static_constructor)
996             dump_method(fo, prefix2,"staticconstructor", "", cls->static_constructor, file);
997         dump_traits(fo, prefix2, cls->static_constructor_traits, file);
998         
999         char*n = multiname_to_string(cls->classname);
1000         if(cls->constructor)
1001             dump_method(fo, prefix2, "constructor", n, cls->constructor, file);
1002         free(n);
1003         dump_traits(fo, prefix2,cls->traits, file);
1004         fprintf(fo, "%s}\n", prefix);
1005     }
1006     fprintf(fo, "%s\n", prefix);
1007
1008     for(t=0;t<file->scripts->num;t++) {
1009         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1010         dump_method(fo, prefix,"initmethod", "init", s->method, file);
1011         dump_traits(fo, prefix, s->traits, file);
1012     }
1013     return file;
1014 }
1015
1016 void* swf_ReadABC(TAG*tag)
1017 {
1018     abc_file_t* file = abc_file_new();
1019     pool_t*pool = pool_new();
1020
1021     swf_SetTagPos(tag, 0);
1022     int t;
1023     if(tag->id == ST_DOABC) {
1024         U32 abcflags = swf_GetU32(tag);
1025         DEBUG printf("flags=%08x\n", abcflags);
1026         char*name= swf_GetString(tag);
1027         file->name = name?strdup(name):0;
1028     }
1029     U32 version = swf_GetU32(tag);
1030     if(version!=0x002e0010) {
1031         fprintf(stderr, "Warning: unknown AVM2 version %08x\n", version);
1032     }
1033
1034     pool_read(pool, tag);
1035
1036     int num_methods = swf_GetU30(tag);
1037     DEBUG printf("%d methods\n", num_methods);
1038     for(t=0;t<num_methods;t++) {
1039         NEW(abc_method_t,m);
1040         int param_count = swf_GetU30(tag);
1041         int return_type_index = swf_GetU30(tag);
1042         if(return_type_index)
1043             m->return_type = multiname_clone(pool_lookup_multiname(pool, return_type_index));
1044         else
1045             m->return_type = 0;
1046
1047         int s;
1048         for(s=0;s<param_count;s++) {
1049             int type_index = swf_GetU30(tag);
1050             multiname_t*param = multiname_clone(pool_lookup_multiname(pool, type_index));
1051             /* type_index might be 0, which probably means "..." (varargs) */
1052             list_append(m->parameters, param);
1053         }
1054
1055         int namenr = swf_GetU30(tag);
1056         if(namenr)
1057             m->name = strdup(pool_lookup_string(pool, namenr));
1058         else
1059             m->name = strdup("");
1060
1061         m->flags = swf_GetU8(tag);
1062         
1063         DEBUG printf("method %d) %s flags=%02x\n", t, params_to_string(m->parameters), m->flags);
1064
1065         if(m->flags&0x08) {
1066             /* optional parameters */
1067             int num = swf_GetU30(tag);
1068             int s;
1069             for(s=0;s<num;s++) {
1070                 int val = swf_GetU30(tag);
1071                 U8 kind = swf_GetU8(tag); // specifies index type for "val"
1072             }
1073         }
1074         if(m->flags&0x80) {
1075             /* debug information- not used by avm2 */
1076             multiname_list_t*l = m->parameters;
1077             while(l) {
1078                 char*name = pool_lookup_string(pool, swf_GetU30(tag));
1079                 l = l->next;
1080             }
1081         }
1082         array_append(file->methods, NO_KEY, m);
1083     }
1084             
1085     parse_metadata(tag, file, pool);
1086         
1087     /* skip classes, and scripts for now, and do the real parsing later */
1088     int num_classes = swf_GetU30(tag);
1089     int classes_pos = tag->pos;
1090     DEBUG printf("%d classes\n", num_classes);
1091     for(t=0;t<num_classes;t++) {
1092         abc_class_t*cls = malloc(sizeof(abc_class_t));
1093         memset(cls, 0, sizeof(abc_class_t));
1094         
1095         DEBUG printf("class %d\n", t);
1096         swf_GetU30(tag); //classname
1097         swf_GetU30(tag); //supername
1098
1099         array_append(file->classes, NO_KEY, cls);
1100
1101         cls->flags = swf_GetU8(tag);
1102         if(cls->flags&8) 
1103             swf_GetU30(tag); //protectedNS
1104         int s;
1105         int inum = swf_GetU30(tag); //interface count
1106         cls->interfaces = 0;
1107         for(s=0;s<inum;s++) {
1108             int interface_index = swf_GetU30(tag);
1109             multiname_t* m = multiname_clone(pool_lookup_multiname(pool, interface_index));
1110             list_append(cls->interfaces, m);
1111             DEBUG printf("  class %d interface: %s\n", t, m->name);
1112         }
1113
1114         swf_GetU30(tag); //iinit
1115         traits_skip(tag);
1116     }
1117     for(t=0;t<num_classes;t++) {
1118         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1119         int cinit = swf_GetU30(tag);
1120         cls->static_constructor = (abc_method_t*)array_getvalue(file->methods, cinit);
1121         traits_skip(tag);
1122     }
1123     int num_scripts = swf_GetU30(tag);
1124     DEBUG printf("%d scripts\n", num_scripts);
1125     for(t=0;t<num_scripts;t++) {
1126         int init = swf_GetU30(tag);
1127         traits_skip(tag);
1128     }
1129
1130     int num_method_bodies = swf_GetU30(tag);
1131     DEBUG printf("%d method bodies\n", num_method_bodies);
1132     for(t=0;t<num_method_bodies;t++) {
1133         int methodnr = swf_GetU30(tag);
1134         if(methodnr >= file->methods->num) {
1135             printf("Invalid method number: %d\n", methodnr);
1136             return 0;
1137         }
1138         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, methodnr);
1139         abc_method_body_t*c = malloc(sizeof(abc_method_body_t));
1140         memset(c, 0, sizeof(abc_method_body_t));
1141         c->max_stack = swf_GetU30(tag);
1142         c->local_count = swf_GetU30(tag);
1143         c->init_scope_depth = swf_GetU30(tag);
1144         c->max_scope_depth = swf_GetU30(tag);
1145         int code_length = swf_GetU30(tag);
1146
1147         c->method = m;
1148         m->body = c;
1149
1150         int pos = tag->pos + code_length;
1151         c->code = code_parse(tag, code_length, file, pool);
1152         tag->pos = pos;
1153
1154         int exception_count = swf_GetU30(tag);
1155         int s;
1156         for(s=0;s<exception_count;s++) {
1157             swf_GetU30(tag); //from
1158             swf_GetU30(tag); //to
1159             swf_GetU30(tag); //target
1160             swf_GetU30(tag); //exc_type
1161             swf_GetU30(tag); //var_name
1162         }
1163         c->traits = traits_parse(tag, pool, file);
1164
1165         DEBUG printf("method_body %d) (method %d), %d bytes of code", t, methodnr, code_length);
1166
1167         array_append(file->method_bodies, NO_KEY, c);
1168     }
1169     if(tag->len - tag->pos) {
1170         fprintf(stderr, "%d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
1171         return 0;
1172     }
1173
1174     swf_SetTagPos(tag, classes_pos);
1175     for(t=0;t<num_classes;t++) {
1176         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1177
1178         int classname_index = swf_GetU30(tag);
1179         int superclass_index = swf_GetU30(tag);
1180         cls->classname = multiname_clone(pool_lookup_multiname(pool, classname_index));
1181         cls->superclass = multiname_clone(pool_lookup_multiname(pool, superclass_index));
1182         cls->flags = swf_GetU8(tag);
1183         const char*ns = "";
1184         if(cls->flags&8) {
1185             int ns_index = swf_GetU30(tag);
1186             cls->protectedNS = namespace_clone(pool_lookup_namespace(pool, ns_index));
1187         }
1188         
1189         int num_interfaces = swf_GetU30(tag); //interface count
1190         int s;
1191         for(s=0;s<num_interfaces;s++) {
1192             swf_GetU30(tag); // multiname index TODO
1193         }
1194         int iinit = swf_GetU30(tag);
1195         cls->constructor = (abc_method_t*)array_getvalue(file->methods, iinit);
1196         cls->traits = traits_parse(tag, pool, file);
1197     }
1198     for(t=0;t<num_classes;t++) {
1199         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1200         /* SKIP */
1201         swf_GetU30(tag); // cindex
1202         cls->static_constructor_traits = traits_parse(tag, pool, file);
1203     }
1204     int num_scripts2 = swf_GetU30(tag);
1205     for(t=0;t<num_scripts2;t++) {
1206         int init = swf_GetU30(tag);
1207         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, init);
1208         
1209         abc_script_t*s = malloc(sizeof(abc_script_t));
1210         memset(s, 0, sizeof(abc_script_t));
1211         s->method = m;
1212         s->traits = traits_parse(tag, pool, file);
1213         array_append(file->scripts, NO_KEY, s);
1214         if(!s->traits) {
1215             fprintf(stderr, "Can't parse script traits\n");
1216             return 0;
1217         }
1218     }
1219
1220     pool_destroy(pool);
1221     return file;
1222 }
1223
1224 static void code_write(TAG*tag, abc_code_t*code, pool_t*pool, abc_file_t*file)
1225 {
1226     int len = 0;
1227     abc_code_t*c = code;
1228     while(c) {
1229         len += opcode_write(0, c, pool, file);
1230         c = c->next;
1231     }
1232     swf_SetU30(tag, len);
1233     int pos = tag->len;
1234     c = code;
1235     while(c) {
1236         opcode_write(tag, c, pool, file);
1237         c = c->next;
1238     }
1239     assert(tag->len - pos == len);
1240 }
1241
1242 void swf_WriteABC(TAG*abctag, void*code)
1243 {
1244     abc_file_t*file = (abc_file_t*)code;
1245     pool_t*pool = pool_new();
1246
1247     TAG*tmp = swf_InsertTag(0,0);
1248     TAG*tag = tmp;
1249     int t;
1250
1251     swf_SetU30(tag, file->methods->num);
1252    
1253     /* enumerate classes, methods and method bodies */
1254     for(t=0;t<file->methods->num;t++) {
1255         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1256         m->index = t;
1257     }
1258     for(t=0;t<file->classes->num;t++) {
1259         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1260         c->index = t;
1261     }
1262     for(t=0;t<file->method_bodies->num;t++) {
1263         abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1264         m->index = t;
1265     }
1266
1267     for(t=0;t<file->methods->num;t++) {
1268         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1269         int n = 0;
1270         multiname_list_t*l = m->parameters;
1271         int num_params = list_length(m->parameters);
1272         swf_SetU30(tag, num_params);
1273         if(m->return_type) 
1274             swf_SetU30(tag, pool_register_multiname(pool, m->return_type));
1275         else
1276             swf_SetU30(tag, 0);
1277         int s;
1278         while(l) {
1279             swf_SetU30(tag, pool_register_multiname(pool, l->multiname));
1280             l = l->next;
1281         }
1282         if(m->name) {
1283             swf_SetU30(tag, pool_register_string(pool, m->name));
1284         } else {
1285             swf_SetU30(tag, 0);
1286         }
1287
1288         swf_SetU8(tag, 0); //flags
1289     }
1290    
1291     /* write metadata */
1292     swf_SetU30(tag, file->metadata->num);
1293     for(t=0;t<file->metadata->num;t++) {
1294         const char*entry_name = array_getkey(file->metadata, t);
1295         swf_SetU30(tag, pool_register_string(pool, entry_name));
1296         array_t*items = (array_t*)array_getvalue(file->metadata, t);
1297         swf_SetU30(tag, items->num);
1298         int s;
1299         for(s=0;s<items->num;s++) {
1300             int i1 = pool_register_string(pool, array_getkey(items, s));
1301             int i2 = pool_register_string(pool, array_getvalue(items, s));
1302             swf_SetU30(tag, i1);
1303             swf_SetU30(tag, i2);
1304         }
1305     }
1306
1307     swf_SetU30(tag, file->classes->num);
1308     for(t=0;t<file->classes->num;t++) {
1309         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1310    
1311         int classname_index = pool_register_multiname(pool, c->classname);
1312         int superclass_index = pool_register_multiname(pool, c->superclass);
1313
1314         swf_SetU30(tag, classname_index);
1315         swf_SetU30(tag, superclass_index);
1316
1317         swf_SetU8(tag, c->flags); // flags
1318         if(c->flags&0x08) {
1319             int ns_index = pool_register_namespace(pool, c->protectedNS);
1320             swf_SetU30(tag, ns_index);
1321         }
1322
1323         swf_SetU30(tag, list_length(c->interfaces));
1324         multiname_list_t*interface= c->interfaces;
1325         while(interface) {
1326             swf_SetU30(tag, pool_register_multiname(pool, interface->multiname));
1327             interface = interface->next;
1328         }
1329
1330         if(!c->constructor) {
1331             fprintf(stderr, "Error: Class %s has no constructor\n", c->classname);
1332             return;
1333         }
1334         swf_SetU30(tag, c->constructor->index);
1335         traits_write(pool, tag, c->traits);
1336     }
1337     for(t=0;t<file->classes->num;t++) {
1338         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1339         if(!c->static_constructor) {
1340             fprintf(stderr, "Error: Class %s has no static constructor\n", c->classname);
1341             return;
1342         }
1343         swf_SetU30(tag, c->static_constructor->index);
1344         traits_write(pool, tag, c->static_constructor_traits);
1345     }
1346
1347     swf_SetU30(tag, file->scripts->num);
1348     for(t=0;t<file->scripts->num;t++) {
1349         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1350         swf_SetU30(tag, s->method->index); //!=t!
1351         traits_write(pool, tag, s->traits);
1352     }
1353
1354     swf_SetU30(tag, file->method_bodies->num);
1355     for(t=0;t<file->method_bodies->num;t++) {
1356         abc_method_body_t*c = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1357         abc_method_t*m = c->method;
1358         swf_SetU30(tag, m->index);
1359         swf_SetU30(tag, c->max_stack);
1360         swf_SetU30(tag, c->local_count);
1361         swf_SetU30(tag, c->init_scope_depth);
1362         swf_SetU30(tag, c->max_scope_depth);
1363
1364         code_write(tag, c->code, pool, file);
1365
1366         swf_SetU30(tag, c->exception_count);
1367         traits_write(pool, tag, c->traits);
1368     }
1369
1370     // --- start to write real tag --
1371     
1372     tag = abctag;
1373
1374     if(tag->id == ST_DOABC) {
1375         swf_SetU32(tag, 1); // flags
1376         swf_SetString(tag, file->name);
1377     }
1378
1379     swf_SetU16(tag, 0x10); //version
1380     swf_SetU16(tag, 0x2e);
1381     
1382     pool_write(pool, tag);
1383     
1384     swf_SetBlock(tag, tmp->data, tmp->len);
1385
1386     swf_DeleteTag(0, tmp);
1387     pool_destroy(pool);
1388 }
1389
1390 void swf_FreeABC(void*code)
1391 {
1392     abc_file_t*file= (abc_file_t*)code;
1393
1394     int t;
1395     for(t=0;t<file->metadata->num;t++) {
1396         array_t*items = (array_t*)array_getvalue(file->metadata, t);
1397         int s;
1398         for(s=0;s<items->num;s++) {
1399             free(array_getvalue(items, s));
1400         }
1401         array_free(items);
1402     }
1403     array_free(file->metadata);
1404
1405     for(t=0;t<file->methods->num;t++) {
1406         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1407
1408         multiname_list_t*param = m->parameters;
1409         while(param) {
1410             multiname_destroy(param->multiname);param->multiname=0;
1411             param = param->next;
1412         }
1413         list_free(m->parameters);m->parameters=0;
1414
1415         if(m->name) {
1416             free((void*)m->name);m->name=0;
1417         }
1418         if(m->return_type) {
1419             multiname_destroy(m->return_type);
1420         }
1421         free(m);
1422     }
1423     array_free(file->methods);
1424
1425     for(t=0;t<file->classes->num;t++) {
1426         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1427         traits_free(cls->traits);cls->traits=0;
1428         traits_free(cls->static_constructor_traits);cls->static_constructor_traits=0;
1429
1430         if(cls->classname) {
1431             multiname_destroy(cls->classname);
1432         }
1433         if(cls->superclass) {
1434             multiname_destroy(cls->superclass);
1435         }
1436
1437         multiname_list_t*i = cls->interfaces;
1438         while(i) {
1439             multiname_destroy(i->multiname);i->multiname=0;
1440             i = i->next;
1441         }
1442         list_free(cls->interfaces);cls->interfaces=0;
1443
1444         if(cls->protectedNS) {
1445             namespace_destroy(cls->protectedNS);
1446         }
1447         free(cls);
1448     }
1449     array_free(file->classes);
1450
1451     for(t=0;t<file->scripts->num;t++) {
1452         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1453         traits_free(s->traits);s->traits=0;
1454         free(s);
1455     }
1456     array_free(file->scripts);
1457
1458     for(t=0;t<file->method_bodies->num;t++) {
1459         abc_method_body_t*body = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1460         code_free(body->code);body->code=0;
1461         traits_free(body->traits);body->traits=0;
1462         free(body);
1463     }
1464     array_free(file->method_bodies);
1465
1466     if(file->name) {
1467         free((void*)file->name);file->name=0;
1468     }
1469
1470     free(file);
1471 }
1472
1473 abc_code_t* add_opcode(abc_code_t*atag, U8 op)
1474 {
1475     abc_code_t*tmp = (abc_code_t*)malloc(sizeof(abc_code_t));
1476     tmp->opcode = op;
1477     tmp->next = 0;
1478     if(atag) {
1479         tmp->prev = atag;
1480         atag->next = tmp;
1481         tmp->parent = atag->parent;
1482     } else {
1483         tmp->prev = 0;
1484         tmp->parent = tmp;
1485     }
1486     return tmp;
1487 }
1488
1489 #include "abc_ops.c"
1490
1491 void swf_AddButtonLinks(SWF*swf, char stop_each_frame, char events)
1492 {
1493     int num_frames = 0;
1494     int has_buttons = 0;
1495     TAG*tag=swf->firstTag;
1496     while(tag) {
1497         if(tag->id == ST_SHOWFRAME)
1498             num_frames++;
1499         if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2)
1500             has_buttons = 1;
1501         tag = tag->next;
1502     }
1503
1504     abc_file_t*file = abc_file_new();
1505     abc_method_body_t*c = 0;
1506    
1507     abc_class_t*cls = abc_class_new2(file, "rfx::MainTimeline", "flash.display::MovieClip");
1508     abc_class_protectedNS(cls, "rfx:MainTimeline");
1509   
1510     TAG*abctag = swf_InsertTagBefore(swf, swf->firstTag, ST_DOABC);
1511     
1512     tag = swf_InsertTag(abctag, ST_SYMBOLCLASS);
1513     swf_SetU16(tag, 1);
1514     swf_SetU16(tag, 0);
1515     swf_SetString(tag, "rfx.MainTimeline");
1516
1517     c = abc_class_staticconstructor(cls, 0, 0);
1518     c->max_stack = 1;
1519     c->local_count = 1;
1520     c->init_scope_depth = 9;
1521     c->max_scope_depth = 10;
1522
1523     __ getlocal_0(c);
1524     __ pushscope(c);
1525     __ returnvoid(c);
1526
1527     c = abc_class_constructor(cls, 0, 0);
1528     c->max_stack = 3;
1529     c->local_count = 1;
1530     c->init_scope_depth = 10;
1531     c->max_scope_depth = 11;
1532     
1533     debugfile(c, "constructor.as");
1534
1535     __ getlocal_0(c);
1536     __ pushscope(c);
1537
1538     __ getlocal_0(c);
1539     __ constructsuper(c,0);
1540
1541     __ getlex(c, "[package]flash.system::Security");
1542     __ pushstring(c, "*");
1543     __ callpropvoid(c, "[package]::allowDomain", 1);
1544     
1545     if(stop_each_frame || has_buttons) {
1546         int frame = 0;
1547         tag = swf->firstTag;
1548         abc_method_body_t*f = 0; //frame script
1549         while(tag && tag->id!=ST_END) {
1550             char framename[80];
1551             char needs_framescript=0;
1552             char buttonname[80];
1553             char functionname[80];
1554             sprintf(framename, "[packageinternal]rfx::frame%d", frame);
1555             
1556             if(!f && (tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2 || stop_each_frame)) {
1557                 /* make the contructor add a frame script */
1558                 __ findpropstrict(c,"[package]::addFrameScript");
1559                 __ pushbyte(c,frame);
1560                 __ getlex(c,framename);
1561                 __ callpropvoid(c,"[package]::addFrameScript",2);
1562
1563                 f = abc_class_method(cls, 0, framename, 0);
1564                 f->max_stack = 3;
1565                 f->local_count = 1;
1566                 f->init_scope_depth = 10;
1567                 f->max_scope_depth = 11;
1568                 __ debugfile(f, "framescript.as");
1569                 __ debugline(f, 1);
1570                 __ getlocal_0(f);
1571                 __ pushscope(f);
1572             }
1573
1574             if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1575                 U16 id = swf_GetDefineID(tag);
1576                 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1577                 __ getlex(f,buttonname);
1578                 __ getlex(f,"flash.events::MouseEvent");
1579                 __ getproperty(f, "::CLICK");
1580                 sprintf(functionname, "::clickbutton%d", swf_GetDefineID(tag));
1581                 __ getlex(f,functionname);
1582                 __ callpropvoid(f, "::addEventListener" ,2);
1583
1584                 if(stop_each_frame) {
1585                     __ findpropstrict(f, "[package]::stop");
1586                     __ callpropvoid(f, "[package]::stop", 0);
1587                 }
1588                 needs_framescript = 1;
1589
1590                 abc_method_body_t*h =
1591                     abc_class_method(cls, "::void", functionname, 1, "flash.events::MouseEvent");
1592                 h->max_stack = 6;
1593                 h->local_count = 2;
1594                 h->init_scope_depth = 10;
1595                 h->max_scope_depth = 11;
1596                 __ getlocal_0(h);
1597                 __ pushscope(h);
1598
1599                 ActionTAG*oldaction = swf_ButtonGetAction(tag);
1600                 if(oldaction && oldaction->op == ACTION__GOTOFRAME) {
1601                     int framenr = GET16(oldaction->data);
1602                     if(framenr>254) {
1603                         fprintf(stderr, "Warning: Couldn't translate jump to frame %d to flash 9 actionscript\n", framenr);
1604                     }
1605                     if(!events) {
1606                         __ findpropstrict(h,"[package]::gotoAndStop");
1607                         __ pushbyte(h,framenr+1);
1608                         __ callpropvoid(h,"[package]::gotoAndStop", 1);
1609                     } else {
1610                         char framename[80];
1611                         sprintf(framename, "frame%d", framenr);
1612                         __ getlocal_0(h); //this
1613                         __ findpropstrict(h, "[package]flash.events::TextEvent");
1614                         __ pushstring(h, "link");
1615                         __ pushtrue(h);
1616                         __ pushtrue(h);
1617                         __ pushstring(h, framename);
1618                         __ constructprop(h,"[package]flash.events::TextEvent", 4);
1619                         __ callpropvoid(h,"[package]::dispatchEvent", 1);
1620                     }
1621                 } else if(oldaction && oldaction->op == ACTION__GETURL) {
1622                     if(!events) {
1623                         __ findpropstrict(h,"flash.net::navigateToURL");
1624                         __ findpropstrict(h,"flash.net::URLRequest");
1625                         // TODO: target _blank
1626                         __ pushstring(h,oldaction->data); //url
1627                         __ constructprop(h,"flash.net::URLRequest", 1);
1628                         __ callpropvoid(h,"flash.net::navigateToURL", 1);
1629                     } else {
1630                         __ getlocal_0(h); //this
1631                         __ findpropstrict(h, "[package]flash.events::TextEvent");
1632                         __ pushstring(h, "link");
1633                         __ pushtrue(h);
1634                         __ pushtrue(h);
1635                         __ pushstring(h,oldaction->data); //url
1636                         __ constructprop(h,"[package]flash.events::TextEvent", 4);
1637                         __ callpropvoid(h,"[package]::dispatchEvent", 1);
1638                     }
1639                 } else if(oldaction) {
1640                     fprintf(stderr, "Warning: Couldn't translate button code of button %d to flash 9 abc action\n", id);
1641                 }
1642                 __ returnvoid(h);
1643                 swf_ActionFree(oldaction);
1644             }
1645             if(tag->id == ST_SHOWFRAME) {
1646                 if(f) {
1647                     __ returnvoid(f);
1648                     f = 0;
1649                 }
1650                 frame++;
1651             }
1652             tag = tag->next;
1653         }
1654         if(f) {
1655             __ returnvoid(f);
1656         }
1657     }
1658     __ returnvoid(c);
1659
1660     tag = swf->firstTag;
1661     while(tag) {
1662         if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1663             char buttonname[80];
1664             sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1665             abc_AddSlot(cls, buttonname, 0, "flash.display::SimpleButton");
1666         }
1667         tag = tag->next;
1668     }
1669
1670
1671     abc_script_t*s = abc_initscript(file, 0, 0);
1672     c = s->method->body;
1673     c->max_stack = 2;
1674     c->local_count = 1;
1675     c->init_scope_depth = 1;
1676     c->max_scope_depth = 9;
1677
1678     __ getlocal_0(c);
1679     __ pushscope(c);
1680     __ getscopeobject(c, 0);
1681     __ getlex(c,"::Object");
1682     __ pushscope(c);
1683     __ getlex(c,"flash.events::EventDispatcher");
1684     __ pushscope(c);
1685     __ getlex(c,"flash.display::DisplayObject");
1686     __ pushscope(c);
1687     __ getlex(c,"flash.display::InteractiveObject");
1688     __ pushscope(c);
1689     __ getlex(c,"flash.display::DisplayObjectContainer");
1690     __ pushscope(c);
1691     __ getlex(c,"flash.display::Sprite");
1692     __ pushscope(c);
1693     __ getlex(c,"flash.display::MovieClip");
1694     __ pushscope(c);
1695     __ getlex(c,"flash.display::MovieClip");
1696     __ newclass(c,cls);
1697     __ popscope(c);
1698     __ popscope(c);
1699     __ popscope(c);
1700     __ popscope(c);
1701     __ popscope(c);
1702     __ popscope(c);
1703     __ popscope(c);
1704     __ initproperty(c,"rfx::MainTimeline");
1705     __ returnvoid(c);
1706
1707     //abc_method_body_addClassTrait(c, "rfx:MainTimeline", 1, cls);
1708     abc_initscript_addClassTrait(s, "rfx::MainTimeline", 1, cls);
1709
1710     swf_WriteABC(abctag, file);
1711 }
1712