3 Routines for handling Flash2 AVM2 ABC Actionscript
5 Extension module for the rfxswf library.
6 Part of the swftools package.
8 Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
26 #include "../rfxswf.h"
31 char stringbuffer[2048];
33 int abc_RegisterNameSpace(abc_file_t*file, const char*name);
34 int abc_RegisterPackageNameSpace(abc_file_t*file, const char*name);
35 int abc_RegisterPackageInternalNameSpace(abc_file_t*file, const char*name);
36 int abc_RegisterProtectedNameSpace(abc_file_t*file, const char*name);
37 int abc_RegisterExplicitNameSpace(abc_file_t*file, const char*name);
38 int abc_RegisterStaticProtectedNameSpace(abc_file_t*file, const char*name);
39 int abc_RegisterPrivateNameSpace(abc_file_t*file, const char*name);
41 /* TODO: switch to a datastructure with just values */
44 static void params_dump(FILE*fo, multiname_list_t*l, constant_list_t*o)
46 int n = list_length(l);
47 int no = list_length(o);
52 char*s = multiname_tostring(l->multiname);
56 s = constant_tostring(o->constant);
73 static void parse_metadata(TAG*tag, abc_file_t*file, pool_t*pool)
76 int num_metadata = swf_GetU30(tag);
78 DEBUG printf("%d metadata\n", num_metadata);
79 for(t=0;t<num_metadata;t++) {
80 const char*entry_name = pool_lookup_string(pool, swf_GetU30(tag));
81 int num = swf_GetU30(tag);
83 DEBUG printf(" %s\n", entry_name);
84 array_t*items = array_new();
86 int i1 = swf_GetU30(tag);
87 int i2 = swf_GetU30(tag);
88 const char*key = i1?pool_lookup_string(pool, i1):"";
89 const char*value = i2?pool_lookup_string(pool, i2):"";
90 DEBUG printf(" %s=%s\n", key, value);
91 array_append(items, key, strdup(value));
93 array_append(file->metadata, entry_name, items);
97 void swf_CopyData(TAG*to, TAG*from, int len)
99 unsigned char*data = malloc(len);
100 swf_GetBlock(from, data, len);
101 swf_SetBlock(to, data, len);
105 abc_file_t*abc_file_new()
107 abc_file_t*f = malloc(sizeof(abc_file_t));
108 memset(f, 0, sizeof(abc_file_t));
109 f->metadata = array_new();
111 f->methods = array_new();
112 f->classes = array_new();
113 f->scripts = array_new();
114 f->method_bodies = array_new();
115 f->flags = ABCFILE_LAZY;
120 abc_class_t* abc_class_new(abc_file_t*file, multiname_t*classname, multiname_t*superclass) {
124 array_append(file->classes, NO_KEY, c);
127 c->classname = multiname_clone(classname);
128 c->superclass = multiname_clone(superclass);
131 c->static_constructor = 0;
132 c->traits = list_new();
135 abc_class_t* abc_class_new2(abc_file_t*pool, char*classname, char*superclass)
137 return abc_class_new(pool, multiname_fromstring(classname), multiname_fromstring(superclass));
140 void abc_class_sealed(abc_class_t*c)
142 c->flags |= CLASS_SEALED;
144 void abc_class_final(abc_class_t*c)
146 c->flags |= CLASS_FINAL;
148 void abc_class_interface(abc_class_t*c)
150 c->flags |= CLASS_INTERFACE;
152 void abc_class_protectedNS(abc_class_t*c, char*namespace)
154 c->protectedNS = namespace_new_protected(namespace);
155 c->flags |= CLASS_PROTECTED_NS;
157 void abc_class_add_interface(abc_class_t*c, multiname_t*interface)
159 list_append(c->interfaces, multiname_clone(interface));
161 char*abc_class_fullname(abc_class_t*cls)
163 const char*package = cls->classname->ns->name;
164 const char*name = cls->classname->name;
165 int l1 = strlen(package);
166 int l2 = strlen(name);
167 char*fullname = malloc(l1+l2+2);
169 memcpy(fullname, package, l1);
172 memcpy(fullname+l1, name, l2+1);
176 void abc_method_init(abc_method_t*m, abc_file_t*file, multiname_t*returntype, char body)
178 /* construct method object */
179 m->index = array_length(file->methods);
180 array_append(file->methods, NO_KEY, m);
181 m->return_type = returntype;
184 /* construct code (method body) object */
185 NEW(abc_method_body_t,c);
186 array_append(file->method_bodies, NO_KEY, c);
187 c->index = array_length(file->method_bodies);
189 c->traits = list_new();
192 /* crosslink the two objects */
197 abc_method_t* abc_method_new(abc_file_t*file, multiname_t*returntype, char body)
200 abc_method_init(m, file, returntype, body);
204 abc_method_t* abc_class_getconstructor(abc_class_t*cls, multiname_t*returntype)
206 if(cls->constructor) {
207 return cls->constructor;
209 abc_method_t* m = abc_method_new(cls->file, returntype, 1);
210 cls->constructor = m;
214 abc_method_t* abc_class_getstaticconstructor(abc_class_t*cls, multiname_t*returntype)
216 if(cls->static_constructor) {
217 return cls->static_constructor;
219 abc_method_t* m = abc_method_new(cls->file, returntype, 1);
220 cls->static_constructor = m;
224 trait_t*trait_new(int type, multiname_t*name, int data1, int data2, constant_t*v)
226 trait_t*trait = malloc(sizeof(trait_t));
227 memset(trait, 0, sizeof(trait_t));
228 trait->kind = type&0x0f;
229 trait->attributes = type&0xf0;
231 trait->data1 = data1;
232 trait->data2 = data2;
237 trait_t*trait_new_member(trait_list_t**traits, multiname_t*type, multiname_t*name,constant_t*v)
239 int kind = TRAIT_SLOT;
240 trait_t*trait = malloc(sizeof(trait_t));
241 memset(trait, 0, sizeof(trait_t));
242 trait->kind = kind&0x0f;
243 trait->attributes = kind&0xf0;
245 trait->type_name = type;
247 trait->slot_id = list_length(*traits)+1;
248 trait_list_t*l = *traits;
249 list_append_(traits, trait);
252 trait_t*trait_new_method(trait_list_t**traits, multiname_t*name, abc_method_t*m)
254 int type = TRAIT_METHOD;
255 trait_t*trait = malloc(sizeof(trait_t));
256 memset(trait, 0, sizeof(trait_t));
257 trait->kind = type&0x0f;
258 trait->attributes = type&0xf0;
262 /* start assigning traits at position #1.
263 Weird things happen when assigning slot 0- slot 0 and 1 seem
265 trait->slot_id = list_length(*traits)+1;
266 list_append_(traits, trait);
270 abc_method_t* abc_class_method(abc_class_t*cls, multiname_t*returntype, multiname_t*name)
272 abc_file_t*file = cls->file;
273 abc_method_t* m = abc_method_new(cls->file, returntype, !(cls->flags&CLASS_INTERFACE));
274 m->trait = trait_new_method(&cls->traits, multiname_clone(name), m);
277 abc_method_t* abc_class_staticmethod(abc_class_t*cls, multiname_t*returntype, multiname_t*name)
279 abc_file_t*file = cls->file;
280 abc_method_t* m = abc_method_new(cls->file, returntype, !(cls->flags&CLASS_INTERFACE));
281 m->trait = trait_new_method(&cls->static_traits, multiname_clone(name), m);
285 trait_t* abc_class_slot(abc_class_t*cls, multiname_t*name, multiname_t*type)
287 abc_file_t*file = cls->file;
288 multiname_t*m_name = multiname_clone(name);
289 multiname_t*m_type = multiname_clone(type);
290 trait_t*t = trait_new_member(&cls->traits, m_type, m_name, 0);
293 trait_t* abc_class_staticslot(abc_class_t*cls, multiname_t*name, multiname_t*type)
295 abc_file_t*file = cls->file;
296 multiname_t*m_name = multiname_clone(name);
297 multiname_t*m_type = multiname_clone(type);
298 trait_t*t = trait_new_member(&cls->static_traits, m_type, m_name, 0);
303 trait_t* traits_find_slotid(trait_list_t*traits, int slotid)
307 for(l=traits;l;l=l->next) {
308 if(l->trait->slot_id==slotid) {
316 void abc_method_body_addClassTrait(abc_method_body_t*code, char*multiname, int slotid, abc_class_t*cls)
318 abc_file_t*file = code->file;
319 multiname_t*m = multiname_fromstring(multiname);
320 trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0);
322 list_append(code->traits, trait);
325 /* notice: traits of a method (body) belonging to an init script
326 and traits of the init script are *not* the same thing */
327 trait_t* abc_initscript_addClassTrait(abc_script_t*script, multiname_t*multiname, abc_class_t*cls)
329 abc_file_t*file = script->file;
330 multiname_t*m = multiname_clone(multiname);
331 int slotid = list_length(script->traits)+1;
332 trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0);
334 list_append(script->traits, trait);
338 abc_script_t* abc_initscript(abc_file_t*file)
340 abc_method_t*m = abc_method_new(file, 0, 1);
341 abc_script_t* s = malloc(sizeof(abc_script_t));
343 s->traits = list_new();
345 array_append(file->scripts, NO_KEY, s);
349 static void traits_dump(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file, dict_t*methods_seen);
351 static void dump_method(FILE*fo, const char*prefix,
355 abc_method_t*m, abc_file_t*file, dict_t*methods_seen)
358 dict_put(methods_seen, m, 0);
360 char*return_type = 0;
362 return_type = multiname_tostring(m->return_type);
364 return_type = strdup("*");
366 fprintf(fo, "%s", prefix);
367 fprintf(fo, "%s %s ", attr, type);
368 fprintf(fo, "%s %s=%s", return_type, name, m->name);
369 params_dump(fo, m->parameters, m->optional_parameters);
370 fprintf(fo, "(%d params, %d optional)\n", list_length(m->parameters), list_length(m->optional_parameters));
372 free(return_type);return_type=0;
374 abc_method_body_t*c = m->body;
379 fprintf(fo, "%s[stack:%d locals:%d scope:%d-%d flags:",
380 prefix, c->old.max_stack, c->old.local_count, c->old.init_scope_depth,
381 c->old.max_scope_depth);
384 int flags = c->method->flags;
385 if(flags&METHOD_NEED_ARGUMENTS) {fprintf(fo, " need_arguments");flags&=~METHOD_NEED_ARGUMENTS;}
386 if(flags&METHOD_NEED_ACTIVATION) {fprintf(fo, " need_activation");flags&=~METHOD_NEED_ACTIVATION;}
387 if(flags&METHOD_NEED_REST) {fprintf(fo, " need_rest");flags&=~METHOD_NEED_REST;}
388 if(flags&METHOD_HAS_OPTIONAL) {fprintf(fo, " has_optional");flags&=~METHOD_HAS_OPTIONAL;}
389 if(flags&METHOD_SET_DXNS) {fprintf(fo, " set_dxns");flags&=~METHOD_SET_DXNS;}
390 if(flags&METHOD_HAS_PARAM_NAMES) {fprintf(fo, " has_param_names");flags&=~METHOD_HAS_PARAM_NAMES;}
391 if(flags) fprintf(fo, " %02x", flags);
395 fprintf(fo, " slot:%d", m->trait->slot_id);
401 sprintf(prefix2, "%s ", prefix);
403 traits_dump(fo, prefix, c->traits, file, methods_seen);
404 fprintf(fo, "%s{\n", prefix);
405 code_dump2(c->code, c->exceptions, file, prefix2, fo);
406 fprintf(fo, "%s}\n\n", prefix);
409 static void traits_free(trait_list_t*traits)
411 trait_list_t*t = traits;
414 multiname_destroy(t->trait->name);t->trait->name = 0;
416 if(t->trait->kind == TRAIT_SLOT || t->trait->kind == TRAIT_CONST) {
417 multiname_destroy(t->trait->type_name);
419 if(t->trait->value) {
420 constant_free(t->trait->value);t->trait->value = 0;
422 free(t->trait);t->trait = 0;
428 static char trait_is_method(trait_t*trait)
430 return (trait->kind == TRAIT_METHOD || trait->kind == TRAIT_GETTER ||
431 trait->kind == TRAIT_SETTER || trait->kind == TRAIT_FUNCTION);
434 static trait_list_t* traits_parse(TAG*tag, pool_t*pool, abc_file_t*file)
436 int num_traits = swf_GetU30(tag);
437 trait_list_t*traits = list_new();
440 DEBUG printf("%d traits\n", num_traits);
443 for(t=0;t<num_traits;t++) {
445 list_append(traits, trait);
447 trait->name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag))); // always a QName (ns,name)
450 DEBUG name = multiname_tostring(trait->name);
451 U8 kind = swf_GetU8(tag);
452 U8 attributes = kind&0xf0;
455 trait->attributes = attributes;
456 DEBUG printf(" trait %d) %s type=%02x\n", t, name, kind);
457 if(kind == TRAIT_METHOD || kind == TRAIT_GETTER || kind == TRAIT_SETTER) { // method / getter / setter
458 trait->disp_id = swf_GetU30(tag);
459 trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
460 trait->method->trait = trait;
461 DEBUG printf(" method/getter/setter\n");
462 } else if(kind == TRAIT_FUNCTION) { // function
463 trait->slot_id = swf_GetU30(tag);
464 trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
465 trait->method->trait = trait;
466 } else if(kind == TRAIT_CLASS) { // class
467 trait->slot_id = swf_GetU30(tag);
468 trait->cls = (abc_class_t*)array_getvalue(file->classes, swf_GetU30(tag));
469 DEBUG printf(" class %s %d %08x\n", name, trait->slot_id, (int)trait->cls);
470 } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
471 trait->slot_id = swf_GetU30(tag);
472 trait->type_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
473 int vindex = swf_GetU30(tag);
475 int vkind = swf_GetU8(tag);
476 trait->value = constant_fromindex(pool, vindex, vkind);
478 DEBUG printf(" slot %s %d %s (%s)\n", name, trait->slot_id, trait->type_name->name, constant_tostring(trait->value));
480 fprintf(stderr, "Can't parse trait type %d\n", kind);
482 if(attributes&0x40) {
483 int num = swf_GetU30(tag);
486 swf_GetU30(tag); //index into metadata array
493 void traits_skip(TAG*tag)
495 int num_traits = swf_GetU30(tag);
497 for(t=0;t<num_traits;t++) {
499 U8 kind = swf_GetU8(tag);
500 U8 attributes = kind&0xf0;
504 if(kind == TRAIT_SLOT || kind == TRAIT_CONST) {
505 if(swf_GetU30(tag)) swf_GetU8(tag);
506 } else if(kind>TRAIT_CONST) {
507 fprintf(stderr, "Can't parse trait type %d\n", kind);
509 if(attributes&0x40) {
510 int s, num = swf_GetU30(tag);
511 for(s=0;s<num;s++) swf_GetU30(tag);
517 static void traits_write(pool_t*pool, TAG*tag, trait_list_t*traits)
523 swf_SetU30(tag, list_length(traits));
527 trait_t*trait = traits->trait;
529 swf_SetU30(tag, pool_register_multiname(pool, trait->name));
530 swf_SetU8(tag, trait->kind|trait->attributes);
532 swf_SetU30(tag, trait->data1);
534 if(trait->kind == TRAIT_CLASS) {
535 swf_SetU30(tag, trait->cls->index);
536 } else if(trait->kind == TRAIT_GETTER ||
537 trait->kind == TRAIT_SETTER ||
538 trait->kind == TRAIT_METHOD) {
539 swf_SetU30(tag, trait->method->index);
540 } else if(trait->kind == TRAIT_SLOT ||
541 trait->kind == TRAIT_CONST) {
542 int index = pool_register_multiname(pool, trait->type_name);
543 swf_SetU30(tag, index);
545 swf_SetU30(tag, trait->data2);
548 if(trait->kind == TRAIT_SLOT || trait->kind == TRAIT_CONST) {
549 int vindex = constant_get_index(pool, trait->value);
550 swf_SetU30(tag, vindex);
552 swf_SetU8(tag, trait->value->type);
555 if(trait->attributes&0x40) {
559 traits = traits->next;
564 static void traits_dump(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file, dict_t*methods_seen)
568 trait_t*trait = traits->trait;
569 char*name = multiname_tostring(trait->name);
570 U8 kind = trait->kind;
571 U8 attributes = trait->attributes;
573 char a = attributes & (TRAIT_ATTR_OVERRIDE|TRAIT_ATTR_FINAL);
575 if(a==TRAIT_ATTR_FINAL)
577 else if(a==TRAIT_ATTR_OVERRIDE)
579 else if(a==(TRAIT_ATTR_OVERRIDE|TRAIT_ATTR_FINAL))
580 type = "final override ";
582 if(attributes&TRAIT_ATTR_METADATA)
583 fprintf(fo, "<metadata>");
585 if(kind == TRAIT_METHOD) {
586 abc_method_t*m = trait->method;
587 dump_method(fo, prefix, type, "method", name, m, file, methods_seen);
588 } else if(kind == TRAIT_GETTER) {
589 abc_method_t*m = trait->method;
590 dump_method(fo, prefix, type, "getter", name, m, file, methods_seen);
591 } else if(kind == TRAIT_SETTER) {
592 abc_method_t*m = trait->method;
593 dump_method(fo, prefix, type, "setter", name, m, file, methods_seen);
594 } else if(kind == TRAIT_FUNCTION) { // function
595 abc_method_t*m = trait->method;
596 dump_method(fo, prefix, type, "function", name, m, file, methods_seen);
597 } else if(kind == TRAIT_CLASS) { // class
598 abc_class_t*cls = trait->cls;
600 fprintf(fo, "%sslot %d: class %s=00000000\n", prefix, trait->slot_id, name);
602 fprintf(fo, "%sslot %d: class %s=%s\n", prefix, trait->slot_id, name, cls->classname->name);
604 } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
605 int slot_id = trait->slot_id;
606 char*type_name = multiname_tostring(trait->type_name);
607 char*value = constant_tostring(trait->value);
608 fprintf(fo, "%sslot %d: %s %s:%s %s %s\n", prefix, trait->slot_id,
609 kind==TRAIT_CONST?"const":"var", name, type_name,
610 trait->value?"=":"", trait->value?value:"");
611 if(value) free(value);
614 fprintf(fo, "%s can't dump trait type %d\n", prefix, kind);
621 void* swf_DumpABC(FILE*fo, void*code, char*prefix)
623 abc_file_t* file = (abc_file_t*)code;
626 fprintf(fo, "%s#\n", prefix);
627 fprintf(fo, "%s#name: %s\n", prefix, file->name);
628 fprintf(fo, "%s#\n", prefix);
632 for(t=0;t<file->metadata->num;t++) {
633 const char*entry_name = array_getkey(file->metadata, t);
634 fprintf(fo, "%s#Metadata \"%s\":\n", prefix, entry_name);
636 array_t*items = (array_t*)array_getvalue(file->metadata, t);
637 for(s=0;s<items->num;s++) {
638 fprintf(fo, "%s# %s=%s\n", prefix, (char*)array_getkey(items, s), (char*)array_getvalue(items,s));
640 fprintf(fo, "%s#\n", prefix);
643 dict_t*methods_seen = dict_new2(&ptr_type);
644 for(t=0;t<file->classes->num;t++) {
645 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
647 sprintf(prefix2, "%s ", prefix);
649 fprintf(fo, "%s", prefix);
650 if(cls->flags&1) fprintf(fo, "sealed ");
651 if(cls->flags&2) fprintf(fo, "final ");
652 if(cls->flags&4) fprintf(fo, "interface ");
654 char*s = namespace_tostring(cls->protectedNS);
655 fprintf(fo, "protectedNS(%s) ", s);
659 char*classname = multiname_tostring(cls->classname);
660 fprintf(fo, "class %s", classname);
662 if(cls->superclass) {
663 char*supername = multiname_tostring(cls->superclass);
664 fprintf(fo, " extends %s", supername);
667 if(cls->interfaces) {
668 multiname_list_t*ilist = cls->interfaces;
670 fprintf(fo, " implements");
672 char*s = multiname_tostring(ilist->multiname);
673 fprintf(fo, " %s", s);
680 fprintf(fo, "extra flags=%02x\n", cls->flags&0xf0);
681 fprintf(fo, "%s{\n", prefix);
683 dict_put(methods_seen, cls->static_constructor, 0);
684 dict_put(methods_seen, cls->constructor, 0);
686 if(cls->static_constructor) {
687 dump_method(fo, prefix2, "", "staticconstructor", "", cls->static_constructor, file, methods_seen);
689 traits_dump(fo, prefix2, cls->static_traits, file, methods_seen);
691 char*n = multiname_tostring(cls->classname);
693 dump_method(fo, prefix2, "", "constructor", n, cls->constructor, file, methods_seen);
695 traits_dump(fo, prefix2,cls->traits, file, methods_seen);
698 swf_DumpAsset(fo, cls->asset, prefix2);
701 fprintf(fo, "%s}\n", prefix);
703 fprintf(fo, "%s\n", prefix);
705 for(t=0;t<file->scripts->num;t++) {
706 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
707 dump_method(fo, prefix, "", "initmethod", "init", s->method, file, methods_seen);
708 traits_dump(fo, prefix, s->traits, file, methods_seen);
712 for(t=0;t<file->methods->num;t++) {
713 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
714 if(!dict_contains(methods_seen, m)) {
718 fprintf(fo, "%s//internal (non-class non-script) methods:\n", prefix);
721 sprintf(name, "%08x ", m->index);
722 dump_method(fo, prefix, "", "internalmethod", name, m, file, methods_seen);
725 dict_destroy(methods_seen);
730 void* swf_ReadABC(TAG*tag)
732 abc_file_t* file = abc_file_new();
733 pool_t*pool = pool_new();
735 swf_SetTagPos(tag, 0);
737 if(tag->id == ST_DOABC) {
738 U32 abcflags = swf_GetU32(tag);
739 DEBUG printf("flags=%08x\n", abcflags);
740 char*name= swf_GetString(tag);
741 file->name = (name&&name[0])?strdup(name):0;
743 U32 version = swf_GetU32(tag);
744 if(version!=0x002e0010) {
745 fprintf(stderr, "Warning: unknown AVM2 version %08x\n", version);
748 pool_read(pool, tag);
750 int num_methods = swf_GetU30(tag);
751 DEBUG printf("%d methods\n", num_methods);
752 for(t=0;t<num_methods;t++) {
754 int param_count = swf_GetU30(tag);
755 int return_type_index = swf_GetU30(tag);
756 if(return_type_index)
757 m->return_type = multiname_clone(pool_lookup_multiname(pool, return_type_index));
762 for(s=0;s<param_count;s++) {
763 int type_index = swf_GetU30(tag);
765 /* type_index might be 0 ("*") */
766 multiname_t*param = type_index?multiname_clone(pool_lookup_multiname(pool, type_index)):0;
767 list_append(m->parameters, param);
770 int namenr = swf_GetU30(tag);
772 m->name = strdup(pool_lookup_string(pool, namenr));
774 m->name = strdup("");
776 m->flags = swf_GetU8(tag);
778 DEBUG printf("method %d) %s ", t, m->name);
779 DEBUG params_dump(stdout, m->parameters, m->optional_parameters);
780 DEBUG printf("flags=%02x\n", t, m->flags);
783 m->optional_parameters = list_new();
784 int num = swf_GetU30(tag);
787 int vindex = swf_GetU30(tag);
788 U8 vkind = swf_GetU8(tag); // specifies index type for "val"
789 constant_t*c = constant_fromindex(pool, vindex, vkind);
790 list_append(m->optional_parameters, c);
795 /* debug information- not used by avm2 */
796 multiname_list_t*l = m->parameters;
798 const char*name = pool_lookup_string(pool, swf_GetU30(tag));
802 m->index = array_length(file->methods);
803 array_append(file->methods, NO_KEY, m);
806 parse_metadata(tag, file, pool);
808 /* skip classes, and scripts for now, and do the real parsing later */
809 int num_classes = swf_GetU30(tag);
810 int classes_pos = tag->pos;
811 DEBUG printf("%d classes\n", num_classes);
812 for(t=0;t<num_classes;t++) {
813 abc_class_t*cls = malloc(sizeof(abc_class_t));
814 memset(cls, 0, sizeof(abc_class_t));
816 swf_GetU30(tag); //classname
817 swf_GetU30(tag); //supername
819 array_append(file->classes, NO_KEY, cls);
821 cls->flags = swf_GetU8(tag);
822 DEBUG printf("class %d %02x\n", t, cls->flags);
824 swf_GetU30(tag); //protectedNS
826 int inum = swf_GetU30(tag); //interface count
828 for(s=0;s<inum;s++) {
829 int interface_index = swf_GetU30(tag);
830 multiname_t* m = multiname_clone(pool_lookup_multiname(pool, interface_index));
831 list_append(cls->interfaces, m);
832 DEBUG printf(" class %d interface: %s\n", t, m->name);
835 int iinit = swf_GetU30(tag); //iinit
836 DEBUG printf("--iinit-->%d\n", iinit);
839 for(t=0;t<num_classes;t++) {
840 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
841 int cinit = swf_GetU30(tag);
842 DEBUG printf("--cinit(%d)-->%d\n", t, cinit);
843 cls->static_constructor = (abc_method_t*)array_getvalue(file->methods, cinit);
846 int num_scripts = swf_GetU30(tag);
847 DEBUG printf("%d scripts\n", num_scripts);
848 for(t=0;t<num_scripts;t++) {
849 int init = swf_GetU30(tag);
853 int num_method_bodies = swf_GetU30(tag);
854 DEBUG printf("%d method bodies\n", num_method_bodies);
855 for(t=0;t<num_method_bodies;t++) {
856 int methodnr = swf_GetU30(tag);
857 if(methodnr >= file->methods->num) {
858 printf("Invalid method number: %d\n", methodnr);
861 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, methodnr);
862 abc_method_body_t*c = malloc(sizeof(abc_method_body_t));
863 memset(c, 0, sizeof(abc_method_body_t));
864 c->old.max_stack = swf_GetU30(tag);
865 c->old.local_count = swf_GetU30(tag);
866 c->old.init_scope_depth = swf_GetU30(tag);
867 c->old.max_scope_depth = swf_GetU30(tag);
869 c->init_scope_depth = c->old.init_scope_depth;
870 int code_length = swf_GetU30(tag);
875 int pos = tag->pos + code_length;
876 codelookup_t*codelookup = 0;
877 c->code = code_parse(tag, code_length, file, pool, &codelookup);
880 int exception_count = swf_GetU30(tag);
882 c->exceptions = list_new();
883 for(s=0;s<exception_count;s++) {
884 abc_exception_t*e = malloc(sizeof(abc_exception_t));
886 e->from = code_atposition(codelookup, swf_GetU30(tag));
887 e->to = code_atposition(codelookup, swf_GetU30(tag));
888 e->target = code_atposition(codelookup, swf_GetU30(tag));
890 e->exc_type = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
891 e->var_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
892 //e->var_name = pool_lookup_string(pool, swf_GetU30(tag));
893 //if(e->var_name) e->var_name = strdup(e->var_name);
894 list_append(c->exceptions, e);
896 codelookup_free(codelookup);
897 c->traits = traits_parse(tag, pool, file);
899 DEBUG printf("method_body %d) (method %d), %d bytes of code\n", t, methodnr, code_length);
901 array_append(file->method_bodies, NO_KEY, c);
903 if(tag->len - tag->pos) {
904 fprintf(stderr, "ERROR: %d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
908 swf_SetTagPos(tag, classes_pos);
909 for(t=0;t<num_classes;t++) {
910 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
912 int classname_index = swf_GetU30(tag);
913 int superclass_index = swf_GetU30(tag);
914 cls->classname = multiname_clone(pool_lookup_multiname(pool, classname_index));
915 cls->superclass = multiname_clone(pool_lookup_multiname(pool, superclass_index));
916 cls->flags = swf_GetU8(tag);
919 int ns_index = swf_GetU30(tag);
920 cls->protectedNS = namespace_clone(pool_lookup_namespace(pool, ns_index));
923 int num_interfaces = swf_GetU30(tag); //interface count
925 for(s=0;s<num_interfaces;s++) {
928 int iinit = swf_GetU30(tag);
929 cls->constructor = (abc_method_t*)array_getvalue(file->methods, iinit);
930 cls->traits = traits_parse(tag, pool, file);
932 for(t=0;t<num_classes;t++) {
933 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
935 swf_GetU30(tag); // cindex
936 cls->static_traits = traits_parse(tag, pool, file);
938 int num_scripts2 = swf_GetU30(tag);
939 for(t=0;t<num_scripts2;t++) {
940 int init = swf_GetU30(tag);
941 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, init);
943 abc_script_t*s = malloc(sizeof(abc_script_t));
944 memset(s, 0, sizeof(abc_script_t));
946 s->traits = traits_parse(tag, pool, file);
947 array_append(file->scripts, NO_KEY, s);
954 static pool_t*writeABC(TAG*abctag, void*code, pool_t*pool)
956 abc_file_t*file = (abc_file_t*)code;
960 file = abc_file_new();
962 TAG*tmp = swf_InsertTag(0,0);
966 /* add method bodies where needed */
967 for(t=0;t<file->classes->num;t++) {
968 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
969 if(!c->constructor) {
970 if(!(c->flags&CLASS_INTERFACE)) {
971 NEW(abc_method_t,m);array_append(file->methods, NO_KEY, m);
972 NEW(abc_method_body_t,body);array_append(file->method_bodies, NO_KEY, body);
973 // don't bother to set m->index
974 body->method = m; m->body = body;
975 if(c->superclass && c->superclass->name && strcmp(c->superclass->name,"Object")) {
976 body->code = abc_getlocal_0(body->code);
977 body->code = abc_constructsuper(body->code, 0);
979 body->code = abc_returnvoid(body->code);
982 NEW(abc_method_t,m);array_append(file->methods, NO_KEY, m);
986 if(!c->static_constructor) {
987 NEW(abc_method_t,m);array_append(file->methods, NO_KEY, m);
988 NEW(abc_method_body_t,body);array_append(file->method_bodies, NO_KEY, body);
989 body->method = m; m->body = body;
990 body->code = abc_returnvoid(0);
991 c->static_constructor = m;
996 swf_SetU30(tag, file->methods->num);
997 /* enumerate classes, methods and method bodies */
998 for(t=0;t<file->methods->num;t++) {
999 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1002 for(t=0;t<file->classes->num;t++) {
1003 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1006 for(t=0;t<file->method_bodies->num;t++) {
1007 abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1011 /* generate code statistics */
1012 for(t=0;t<file->method_bodies->num;t++) {
1013 abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1014 m->stats = code_get_statistics(m->code, m->exceptions);
1017 /* level init scope depths: The init scope depth of a method is
1018 always as least as high as the init scope depth of it's surrounding
1020 A method has it's own init_scope_depth if it's an init method
1021 (then its init scope depth is zero), or if it's used as a closure.
1023 Not sure yet what to do with methods which are used at different
1024 locations- e.g. the nullmethod is used all over the place.
1025 EDIT: flashplayer doesn't allow this anyway- a method can only
1028 Also, I have the strong suspicion that flash player uses only
1029 the difference between max_scope_stack and init_scope_stack, anyway.
1031 for(t=0;t<file->classes->num;t++) {
1032 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1033 trait_list_t*traits = c->traits;
1034 if(c->constructor && c->constructor->body &&
1035 c->constructor->body->init_scope_depth < c->init_scope_depth) {
1036 c->constructor->body->init_scope_depth = c->init_scope_depth;
1038 if(c->static_constructor && c->static_constructor->body &&
1039 c->static_constructor->body->init_scope_depth < c->init_scope_depth) {
1040 c->static_constructor->body->init_scope_depth = c->init_scope_depth;
1043 trait_t*trait = traits->trait;
1044 if(trait_is_method(trait) && trait->method->body) {
1045 abc_method_body_t*body = trait->method->body;
1046 if(body->init_scope_depth < c->init_scope_depth) {
1047 body->init_scope_depth = c->init_scope_depth;
1050 traits = traits->next;
1054 for(t=0;t<file->methods->num;t++) {
1055 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1057 multiname_list_t*l = m->parameters;
1058 int num_params = list_length(m->parameters);
1059 swf_SetU30(tag, num_params);
1061 swf_SetU30(tag, pool_register_multiname(pool, m->return_type));
1066 swf_SetU30(tag, pool_register_multiname(pool, l->multiname));
1070 swf_SetU30(tag, pool_register_string(pool, m->name));
1075 U8 flags = m->flags&(METHOD_NEED_REST|METHOD_NEED_ARGUMENTS);
1076 if(m->optional_parameters)
1077 flags |= METHOD_HAS_OPTIONAL;
1079 flags |= m->body->stats->flags;
1082 swf_SetU8(tag, flags);
1083 if(flags&METHOD_HAS_OPTIONAL) {
1084 swf_SetU30(tag, list_length(m->optional_parameters));
1085 constant_list_t*l = m->optional_parameters;
1087 int i = constant_get_index(pool, l->constant);
1090 swf_SetU8(tag, CONSTANT_NULL);
1092 swf_SetU8(tag, l->constant->type);
1099 /* write metadata */
1100 swf_SetU30(tag, file->metadata->num);
1101 for(t=0;t<file->metadata->num;t++) {
1102 const char*entry_name = array_getkey(file->metadata, t);
1103 swf_SetU30(tag, pool_register_string(pool, entry_name));
1104 array_t*items = (array_t*)array_getvalue(file->metadata, t);
1105 swf_SetU30(tag, items->num);
1107 for(s=0;s<items->num;s++) {
1108 int i1 = pool_register_string(pool, array_getkey(items, s));
1109 int i2 = pool_register_string(pool, array_getvalue(items, s));
1110 swf_SetU30(tag, i1);
1111 swf_SetU30(tag, i2);
1115 swf_SetU30(tag, file->classes->num);
1116 for(t=0;t<file->classes->num;t++) {
1117 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1119 int classname_index = pool_register_multiname(pool, c->classname);
1120 int superclass_index = pool_register_multiname(pool, c->superclass);
1122 swf_SetU30(tag, classname_index);
1123 swf_SetU30(tag, superclass_index);
1125 swf_SetU8(tag, c->flags); // flags
1127 int ns_index = pool_register_namespace(pool, c->protectedNS);
1128 swf_SetU30(tag, ns_index);
1131 swf_SetU30(tag, list_length(c->interfaces));
1132 multiname_list_t*interface= c->interfaces;
1134 swf_SetU30(tag, pool_register_multiname(pool, interface->multiname));
1135 interface = interface->next;
1138 assert(c->constructor);
1139 swf_SetU30(tag, c->constructor->index);
1141 traits_write(pool, tag, c->traits);
1143 for(t=0;t<file->classes->num;t++) {
1144 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1145 assert(c->static_constructor);
1146 swf_SetU30(tag, c->static_constructor->index);
1148 traits_write(pool, tag, c->static_traits);
1151 swf_SetU30(tag, file->scripts->num);
1152 for(t=0;t<file->scripts->num;t++) {
1153 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1154 if(!s->method->body || !s->method->body->code) {
1155 fprintf(stderr, "Internal Error: initscript has no body\n");
1157 swf_SetU30(tag, s->method->index); //!=t!
1158 traits_write(pool, tag, s->traits);
1161 swf_SetU30(tag, file->method_bodies->num);
1162 for(t=0;t<file->method_bodies->num;t++) {
1163 abc_method_body_t*c = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1164 abc_method_t*m = c->method;
1165 swf_SetU30(tag, m->index);
1167 //swf_SetU30(tag, c->old.max_stack);
1168 //swf_SetU30(tag, c->old.local_count);
1169 //swf_SetU30(tag, c->old.init_scope_depth);
1170 //swf_SetU30(tag, c->old.max_scope_depth);
1172 swf_SetU30(tag, c->stats->max_stack);
1173 int param_num = list_length(c->method->parameters)+1;
1174 if(c->method->flags&METHOD_NEED_REST)
1176 if(param_num <= c->stats->local_count)
1177 swf_SetU30(tag, c->stats->local_count);
1179 swf_SetU30(tag, param_num);
1181 swf_SetU30(tag, c->init_scope_depth);
1182 swf_SetU30(tag, c->stats->max_scope_depth+
1183 c->init_scope_depth);
1185 code_write(tag, c->code, pool, file);
1187 swf_SetU30(tag, list_length(c->exceptions));
1188 abc_exception_list_t*l = c->exceptions;
1190 // warning: assumes "pos" in each code_t is up-to-date
1191 swf_SetU30(tag, l->abc_exception->from->pos);
1192 swf_SetU30(tag, l->abc_exception->to->pos);
1193 swf_SetU30(tag, l->abc_exception->target->pos);
1194 swf_SetU30(tag, pool_register_multiname(pool, l->abc_exception->exc_type));
1195 swf_SetU30(tag, pool_register_multiname(pool, l->abc_exception->var_name));
1199 traits_write(pool, tag, c->traits);
1202 /* free temporary codestat data again. Notice: If we were to write this
1203 file multiple times, this can also be shifted to abc_file_free() */
1204 for(t=0;t<file->method_bodies->num;t++) {
1205 abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1206 codestats_free(m->stats);m->stats=0;
1209 // --- start to write real tag --
1213 if(tag->id == ST_DOABC) {
1214 swf_SetU32(tag, file->flags); // flags
1215 swf_SetString(tag, file->name);
1218 swf_SetU16(tag, 0x10); //version
1219 swf_SetU16(tag, 0x2e);
1221 pool_write(pool, tag);
1223 swf_SetBlock(tag, tmp->data, tmp->len);
1225 swf_DeleteTag(0, tmp);
1229 void swf_WriteABC(TAG*abctag, void*code)
1231 pool_t*pool = writeABC(abctag, code, 0);
1232 pool_optimize(pool);
1233 swf_ResetTag(abctag, abctag->id);
1234 writeABC(abctag, code, pool);
1238 void abc_file_free(abc_file_t*file)
1243 if(file->metadata) {
1244 for(t=0;t<file->metadata->num;t++) {
1245 array_t*items = (array_t*)array_getvalue(file->metadata, t);
1247 for(s=0;s<items->num;s++) {
1248 free(array_getvalue(items, s));
1252 array_free(file->metadata);file->metadata=0;
1255 for(t=0;t<file->methods->num;t++) {
1256 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1258 multiname_list_t*param = m->parameters;
1260 multiname_destroy(param->multiname);param->multiname=0;
1261 param = param->next;
1263 list_free(m->parameters);m->parameters=0;
1265 constant_list_t*opt = m->optional_parameters;
1267 constant_free(opt->constant);opt->constant=0;
1270 list_free(m->optional_parameters);m->optional_parameters=0;
1273 free((void*)m->name);m->name=0;
1275 if(m->return_type) {
1276 multiname_destroy(m->return_type);
1280 array_free(file->methods);file->methods=0;
1282 for(t=0;t<file->classes->num;t++) {
1283 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1284 traits_free(cls->traits);cls->traits=0;
1285 traits_free(cls->static_traits);cls->static_traits=0;
1287 if(cls->classname) {
1288 multiname_destroy(cls->classname);
1290 if(cls->superclass) {
1291 multiname_destroy(cls->superclass);
1294 multiname_list_t*i = cls->interfaces;
1296 multiname_destroy(i->multiname);i->multiname=0;
1299 list_free(cls->interfaces);cls->interfaces=0;
1301 if(cls->protectedNS) {
1302 namespace_destroy(cls->protectedNS);
1306 array_free(file->classes);file->classes=0;
1308 for(t=0;t<file->scripts->num;t++) {
1309 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1310 traits_free(s->traits);s->traits=0;
1313 array_free(file->scripts);file->scripts=0;
1315 for(t=0;t<file->method_bodies->num;t++) {
1316 abc_method_body_t*body = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1317 code_free(body->code);body->code=0;
1318 traits_free(body->traits);body->traits=0;
1320 abc_exception_list_t*ee = body->exceptions;
1322 abc_exception_t*e=ee->abc_exception;ee->abc_exception=0;
1323 e->from = e->to = e->target = 0;
1324 multiname_destroy(e->exc_type);e->exc_type=0;
1325 multiname_destroy(e->var_name);e->var_name=0;
1329 list_free(body->exceptions);body->exceptions=0;
1333 array_free(file->method_bodies);file->method_bodies=0;
1336 free((void*)file->name);file->name=0;
1342 void swf_FreeABC(void*code)
1344 abc_file_t*file= (abc_file_t*)code;
1345 abc_file_free(file);