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"
30 char stringbuffer[2048];
32 int abc_RegisterNameSpace(abc_file_t*file, const char*name);
33 int abc_RegisterPackageNameSpace(abc_file_t*file, const char*name);
34 int abc_RegisterPackageInternalNameSpace(abc_file_t*file, const char*name);
35 int abc_RegisterProtectedNameSpace(abc_file_t*file, const char*name);
36 int abc_RegisterExplicitNameSpace(abc_file_t*file, const char*name);
37 int abc_RegisterStaticProtectedNameSpace(abc_file_t*file, const char*name);
38 int abc_RegisterPrivateNameSpace(abc_file_t*file, const char*name);
40 /* TODO: switch to a datastructure with just values */
43 static char* params_tostring(multiname_list_t*list)
46 int n = list_length(list);
47 char**names = (char**)malloc(sizeof(char*)*n);
53 names[n] = multiname_tostring(l->multiname);
54 size += strlen(names[n]) + 2;
58 char* params = malloc(size+15);
67 strcat(params, names[n]);
75 sprintf(num, "[%d params]", n);
76 strcat(params, num);*/
85 static void parse_metadata(TAG*tag, abc_file_t*file, pool_t*pool)
88 int num_metadata = swf_GetU30(tag);
90 DEBUG printf("%d metadata\n");
91 for(t=0;t<num_metadata;t++) {
92 const char*entry_name = pool_lookup_string(pool, swf_GetU30(tag));
93 int num = swf_GetU30(tag);
95 DEBUG printf(" %s\n", entry_name);
96 array_t*items = array_new();
98 int i1 = swf_GetU30(tag);
99 int i2 = swf_GetU30(tag);
100 char*key = i1?pool_lookup_string(pool, i1):"";
101 char*value = i2?pool_lookup_string(pool, i2):"";
102 DEBUG printf(" %s=%s\n", key, value);
103 array_append(items, key, strdup(value));
105 array_append(file->metadata, entry_name, items);
109 void swf_CopyData(TAG*to, TAG*from, int len)
111 unsigned char*data = malloc(len);
112 swf_GetBlock(from, data, len);
113 swf_SetBlock(to, data, len);
117 abc_file_t*abc_file_new()
119 abc_file_t*f = malloc(sizeof(abc_file_t));
120 memset(f, 0, sizeof(abc_file_t));
121 f->metadata = array_new();
123 f->methods = array_new();
124 f->classes = array_new();
125 f->scripts = array_new();
126 f->method_bodies = array_new();
127 f->flags = ABCFILE_LAZY;
132 #define CLASS_SEALED 1
133 #define CLASS_FINAL 2
134 #define CLASS_INTERFACE 4
135 #define CLASS_PROTECTED_NS 8
137 abc_class_t* abc_class_new(abc_file_t*file, multiname_t*classname, multiname_t*superclass) {
140 array_append(file->classes, NO_KEY, c);
143 c->classname = classname;
144 c->superclass = superclass;
147 c->static_constructor = 0;
148 c->traits = list_new();
151 abc_class_t* abc_class_new2(abc_file_t*pool, char*classname, char*superclass)
153 return abc_class_new(pool, multiname_fromstring(classname), multiname_fromstring(superclass));
156 void abc_class_sealed(abc_class_t*c)
158 c->flags |= CLASS_SEALED;
160 void abc_class_final(abc_class_t*c)
162 c->flags |= CLASS_FINAL;
164 void abc_class_interface(abc_class_t*c)
166 c->flags |= CLASS_INTERFACE;
168 void abc_class_protectedNS(abc_class_t*c, char*namespace)
170 c->protectedNS = namespace_new_protected(namespace);
171 c->flags |= CLASS_PROTECTED_NS;
173 void abc_class_add_interface(abc_class_t*c, multiname_t*interface)
175 list_append(c->interfaces, interface);
178 abc_method_body_t* add_method(abc_file_t*file, abc_class_t*cls, multiname_t*returntype, int num_params, va_list va)
180 /* construct code (method body) object */
181 NEW(abc_method_body_t,c);
182 array_append(file->method_bodies, NO_KEY, c);
184 c->traits = list_new();
187 /* construct method object */
189 array_append(file->methods, NO_KEY, m);
191 m->return_type = returntype;
194 for(t=0;t<num_params;t++) {
195 const char*param = va_arg(va, const char*);
196 list_append(m->parameters, multiname_fromstring(param));
199 /* crosslink the two objects */
206 abc_method_body_t* abc_class_constructor(abc_class_t*cls, multiname_t*returntype, int num_params, ...)
209 va_start(va, num_params);
210 abc_method_body_t* c = add_method(cls->file, cls, returntype, num_params, va);
212 cls->constructor = c->method;
216 abc_method_body_t* abc_class_staticconstructor(abc_class_t*cls, multiname_t*returntype, int num_params, ...)
219 va_start(va, num_params);
220 abc_method_body_t* c = add_method(cls->file, cls, returntype, num_params, va);
222 cls->static_constructor = c->method;
226 trait_t*trait_new(int type, multiname_t*name, int data1, int data2, constant_t*v)
228 trait_t*trait = malloc(sizeof(trait_t));
229 memset(trait, 0, sizeof(trait_t));
230 trait->kind = type&0x0f;
231 trait->attributes = type&0xf0;
233 trait->data1 = data1;
234 trait->data2 = data2;
238 trait_t*trait_new_member(multiname_t*type, multiname_t*name,constant_t*v)
240 int kind = TRAIT_SLOT;
241 trait_t*trait = malloc(sizeof(trait_t));
242 memset(trait, 0, sizeof(trait_t));
243 trait->kind = kind&0x0f;
244 trait->attributes = kind&0xf0;
246 trait->type_name = type;
249 trait_t*trait_new_method(multiname_t*name, abc_method_t*m)
251 int type = TRAIT_METHOD;
252 trait_t*trait = malloc(sizeof(trait_t));
253 memset(trait, 0, sizeof(trait_t));
254 trait->kind = type&0x0f;
255 trait->attributes = type&0xf0;
261 abc_method_body_t* abc_class_method(abc_class_t*cls, multiname_t*returntype, char*name, int num_params, ...)
263 abc_file_t*file = cls->file;
265 va_start(va, num_params);
266 abc_method_body_t* c = add_method(cls->file, cls, returntype, num_params, va);
268 list_append(cls->traits, trait_new_method(multiname_fromstring(name), c->method));
272 void abc_AddSlot(abc_class_t*cls, char*name, int slot, char*type)
274 abc_file_t*file = cls->file;
275 multiname_t*m_name = multiname_fromstring(name);
276 multiname_t*m_type = multiname_fromstring(type);
277 trait_t*t = trait_new_member(m_type, m_name, 0);
278 t->slot_id = list_length(cls->traits);
279 list_append(cls->traits, t);
282 void abc_method_body_addClassTrait(abc_method_body_t*code, char*multiname, int slotid, abc_class_t*cls)
284 abc_file_t*file = code->file;
285 multiname_t*m = multiname_fromstring(multiname);
286 trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0);
288 list_append(code->traits, trait);
291 /* notice: traits of a method (body) belonging to an init script
292 and traits of the init script are *not* the same thing */
293 int abc_initscript_addClassTrait(abc_script_t*script, multiname_t*multiname, abc_class_t*cls)
295 abc_file_t*file = script->file;
296 multiname_t*m = multiname_clone(multiname);
297 int slotid = list_length(script->traits)+1;
298 trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0);
300 list_append(script->traits, trait);
304 abc_script_t* abc_initscript(abc_file_t*file, multiname_t*returntype, int num_params, ...)
307 va_start(va, num_params);
308 abc_method_body_t* c = add_method(file, 0, returntype, num_params, va);
309 abc_script_t* s = malloc(sizeof(abc_script_t));
310 s->method = c->method;
311 s->traits = list_new();
313 array_append(file->scripts, NO_KEY, s);
318 static void traits_dump(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file);
320 static void dump_method(FILE*fo, const char*prefix, const char*type, const char*name, abc_method_t*m, abc_file_t*file)
322 char*return_type = 0;
324 return_type = multiname_tostring(m->return_type);
326 return_type = strdup("void");
327 char*paramstr = params_tostring(m->parameters);
328 fprintf(fo, "%s%s %s %s=%s %s (%d params)\n", prefix, type, return_type, name, m->name, paramstr, list_length(m->parameters));
329 free(paramstr);paramstr=0;
330 free(return_type);return_type=0;
332 abc_method_body_t*c = m->body;
337 fprintf(fo, "%s[stack:%d locals:%d scope:%d-%d flags:",
338 prefix, c->old.max_stack, c->old.local_count, c->old.init_scope_depth,
339 c->old.max_scope_depth);
342 int flags = c->method->flags;
343 if(flags&METHOD_NEED_ARGUMENTS) {fprintf(fo, " need_arguments");flags&=~METHOD_NEED_ARGUMENTS;}
344 if(flags&METHOD_NEED_ACTIVATION) {fprintf(fo, " need_activation");flags&=~METHOD_NEED_ACTIVATION;}
345 if(flags&METHOD_NEED_REST) {fprintf(fo, " need_rest");flags&=~METHOD_NEED_REST;}
346 if(flags&METHOD_HAS_OPTIONAL) {fprintf(fo, " has_optional");flags&=~METHOD_HAS_OPTIONAL;}
347 if(flags&METHOD_SET_DXNS) {fprintf(fo, " set_dxns");flags&=~METHOD_SET_DXNS;}
348 if(flags&METHOD_HAS_PARAM_NAMES) {fprintf(fo, " has_param_names");flags&=~METHOD_HAS_PARAM_NAMES;}
349 if(flags) fprintf(fo, " %02x", flags);
354 sprintf(prefix2, "%s ", prefix);
356 traits_dump(fo, prefix, c->traits, file);
357 fprintf(fo, "%s{\n", prefix);
358 code_dump(c->code, c->exceptions, file, prefix2, fo);
359 fprintf(fo, "%s}\n\n", prefix);
362 static void traits_free(trait_list_t*traits)
364 trait_list_t*t = traits;
367 multiname_destroy(t->trait->name);t->trait->name = 0;
369 if(t->trait->kind == TRAIT_SLOT || t->trait->kind == TRAIT_CONST) {
370 multiname_destroy(t->trait->type_name);
372 if(t->trait->value) {
373 constant_free(t->trait->value);t->trait->value = 0;
375 free(t->trait);t->trait = 0;
381 static char trait_is_method(trait_t*trait)
383 return (trait->kind == TRAIT_METHOD || trait->kind == TRAIT_GETTER ||
384 trait->kind == TRAIT_SETTER || trait->kind == TRAIT_FUNCTION);
387 static trait_list_t* traits_parse(TAG*tag, pool_t*pool, abc_file_t*file)
389 int num_traits = swf_GetU30(tag);
390 trait_list_t*traits = list_new();
393 DEBUG printf("%d traits\n", num_traits);
396 for(t=0;t<num_traits;t++) {
398 list_append(traits, trait);
400 trait->name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag))); // always a QName (ns,name)
403 DEBUG name = multiname_tostring(trait->name);
404 U8 kind = swf_GetU8(tag);
405 U8 attributes = kind&0xf0;
408 trait->attributes = attributes;
409 DEBUG printf(" trait %d) %s type=%02x\n", t, name, kind);
410 if(kind == TRAIT_METHOD || kind == TRAIT_GETTER || kind == TRAIT_SETTER) { // method / getter / setter
411 trait->disp_id = swf_GetU30(tag);
412 trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
413 DEBUG printf(" method/getter/setter\n");
414 } else if(kind == TRAIT_FUNCTION) { // function
415 trait->slot_id = swf_GetU30(tag);
416 trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
417 } else if(kind == TRAIT_CLASS) { // class
418 trait->slot_id = swf_GetU30(tag);
419 trait->cls = (abc_class_t*)array_getvalue(file->classes, swf_GetU30(tag));
420 DEBUG printf(" class %s %d %d\n", name, trait->slot_id, trait->cls);
421 } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
422 /* a slot is a variable in a class that is shared amonst all instances
423 of the same type, but which has a unique location in each object
424 (in other words, slots are non-static, traits are static)
426 trait->slot_id = swf_GetU30(tag);
427 trait->type_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
428 int vindex = swf_GetU30(tag);
430 int vkind = swf_GetU8(tag);
431 trait->value = constant_fromindex(pool, vindex, vkind);
433 DEBUG printf(" slot %s %d %s (%s)\n", name, trait->slot_id, trait->type_name->name, constant_tostring(trait->value));
435 fprintf(stderr, "Can't parse trait type %d\n", kind);
437 if(attributes&0x40) {
438 int num = swf_GetU30(tag);
441 swf_GetU30(tag); //index into metadata array
448 void traits_skip(TAG*tag)
450 int num_traits = swf_GetU30(tag);
452 for(t=0;t<num_traits;t++) {
454 U8 kind = swf_GetU8(tag);
455 U8 attributes = kind&0xf0;
459 if(kind == TRAIT_SLOT || kind == TRAIT_CONST) {
460 if(swf_GetU30(tag)) swf_GetU8(tag);
461 } else if(kind>TRAIT_CONST) {
462 fprintf(stderr, "Can't parse trait type %d\n", kind);
464 if(attributes&0x40) {
465 int s, num = swf_GetU30(tag);
466 for(s=0;s<num;s++) swf_GetU30(tag);
472 static void traits_write(pool_t*pool, TAG*tag, trait_list_t*traits)
478 swf_SetU30(tag, list_length(traits));
482 trait_t*trait = traits->trait;
484 swf_SetU30(tag, pool_register_multiname(pool, trait->name));
485 swf_SetU8(tag, trait->kind|trait->attributes);
487 swf_SetU30(tag, trait->data1);
489 if(trait->kind == TRAIT_CLASS) {
490 swf_SetU30(tag, trait->cls->index);
491 } else if(trait->kind == TRAIT_GETTER ||
492 trait->kind == TRAIT_SETTER ||
493 trait->kind == TRAIT_METHOD) {
494 swf_SetU30(tag, trait->method->index);
495 } else if(trait->kind == TRAIT_SLOT ||
496 trait->kind == TRAIT_CONST) {
497 int index = pool_register_multiname(pool, trait->type_name);
498 swf_SetU30(tag, index);
500 swf_SetU30(tag, trait->data2);
503 if(trait->kind == TRAIT_SLOT || trait->kind == TRAIT_CONST) {
504 int vindex = constant_get_index(pool, trait->value);
505 swf_SetU30(tag, vindex);
507 swf_SetU8(tag, trait->value->type);
510 if(trait->attributes&0x40) {
514 traits = traits->next;
519 static void traits_dump(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file)
523 trait_t*trait = traits->trait;
524 char*name = multiname_tostring(trait->name);
525 U8 kind = trait->kind;
526 U8 attributes = trait->attributes;
527 if(kind == TRAIT_METHOD) {
528 abc_method_t*m = trait->method;
529 dump_method(fo, prefix, "method", name, m, file);
530 } else if(kind == TRAIT_GETTER) {
531 abc_method_t*m = trait->method;
532 dump_method(fo, prefix, "getter", name, m, file);
533 } else if(kind == TRAIT_SETTER) {
534 abc_method_t*m = trait->method;
535 dump_method(fo, prefix, "setter", name, m, file);
536 } else if(kind == TRAIT_FUNCTION) { // function
537 abc_method_t*m = trait->method;
538 dump_method(fo, prefix, "function", name, m, file);
539 } else if(kind == TRAIT_CLASS) { // class
540 abc_class_t*cls = trait->cls;
542 fprintf(fo, "%sslot %d: class %s=00000000\n", prefix, trait->slot_id, name);
544 fprintf(fo, "%sslot %d: class %s=%s\n", prefix, trait->slot_id, name, cls->classname->name);
546 } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
547 int slot_id = trait->slot_id;
548 char*type_name = multiname_tostring(trait->type_name);
549 char*value = constant_tostring(trait->value);
550 fprintf(fo, "%sslot %d: %s%s %s %s %s\n", prefix, trait->slot_id,
551 kind==TRAIT_CONST?"const ":"", type_name, name,
552 value?"=":"", value);
553 if(value) free(value);
556 fprintf(fo, "%s can't dump trait type %d\n", prefix, kind);
563 void* swf_DumpABC(FILE*fo, void*code, char*prefix)
565 abc_file_t* file = (abc_file_t*)code;
568 fprintf(fo, "%s#\n", prefix);
569 fprintf(fo, "%s#name: %s\n", prefix, file->name);
570 fprintf(fo, "%s#\n", prefix);
574 for(t=0;t<file->metadata->num;t++) {
575 const char*entry_name = array_getkey(file->metadata, t);
576 fprintf(fo, "%s#Metadata \"%s\":\n", prefix, entry_name);
578 array_t*items = (array_t*)array_getvalue(file->metadata, t);
579 for(s=0;s<items->num;s++) {
580 fprintf(fo, "%s# %s=%s\n", prefix, array_getkey(items, s), array_getvalue(items,s));
582 fprintf(fo, "%s#\n", prefix);
585 for(t=0;t<file->classes->num;t++) {
586 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
588 sprintf(prefix2, "%s ", prefix);
590 fprintf(fo, "%s", prefix);
591 if(cls->flags&1) fprintf(fo, "sealed ");
592 if(cls->flags&2) fprintf(fo, "final ");
593 if(cls->flags&4) fprintf(fo, "interface ");
595 char*s = namespace_tostring(cls->protectedNS);
596 fprintf(fo, "protectedNS(%s) ", s);
600 char*classname = multiname_tostring(cls->classname);
601 fprintf(fo, "class %s", classname);
603 if(cls->superclass) {
604 char*supername = multiname_tostring(cls->superclass);
605 fprintf(fo, " extends %s", supername);
607 multiname_list_t*ilist = cls->interfaces;
609 fprintf(fo, " implements");
611 char*s = multiname_tostring(ilist->multiname);
612 fprintf(fo, " %s", s);
619 fprintf(fo, "extra flags=%02x\n", cls->flags&0xf0);
620 fprintf(fo, "%s{\n", prefix);
622 if(cls->static_constructor)
623 dump_method(fo, prefix2,"staticconstructor", "", cls->static_constructor, file);
624 traits_dump(fo, prefix2, cls->static_constructor_traits, file);
626 char*n = multiname_tostring(cls->classname);
628 dump_method(fo, prefix2, "constructor", n, cls->constructor, file);
630 traits_dump(fo, prefix2,cls->traits, file);
631 fprintf(fo, "%s}\n", prefix);
633 fprintf(fo, "%s\n", prefix);
635 for(t=0;t<file->scripts->num;t++) {
636 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
637 dump_method(fo, prefix,"initmethod", "init", s->method, file);
638 traits_dump(fo, prefix, s->traits, file);
643 void* swf_ReadABC(TAG*tag)
645 abc_file_t* file = abc_file_new();
646 pool_t*pool = pool_new();
648 swf_SetTagPos(tag, 0);
650 if(tag->id == ST_DOABC) {
651 U32 abcflags = swf_GetU32(tag);
652 DEBUG printf("flags=%08x\n", abcflags);
653 char*name= swf_GetString(tag);
654 file->name = (name&&name[0])?strdup(name):0;
656 U32 version = swf_GetU32(tag);
657 if(version!=0x002e0010) {
658 fprintf(stderr, "Warning: unknown AVM2 version %08x\n", version);
661 pool_read(pool, tag);
663 int num_methods = swf_GetU30(tag);
664 DEBUG printf("%d methods\n", num_methods);
665 for(t=0;t<num_methods;t++) {
667 int param_count = swf_GetU30(tag);
668 int return_type_index = swf_GetU30(tag);
669 if(return_type_index)
670 m->return_type = multiname_clone(pool_lookup_multiname(pool, return_type_index));
675 for(s=0;s<param_count;s++) {
676 int type_index = swf_GetU30(tag);
678 /* type_index might be 0, which probably means "..." (varargs) */
679 multiname_t*param = type_index?multiname_clone(pool_lookup_multiname(pool, type_index)):0;
680 list_append(m->parameters, param);
683 int namenr = swf_GetU30(tag);
685 m->name = strdup(pool_lookup_string(pool, namenr));
687 m->name = strdup("");
689 m->flags = swf_GetU8(tag);
691 DEBUG printf("method %d) %s flags=%02x\n", t, params_tostring(m->parameters), m->flags);
694 /* TODO optional parameters */
695 m->optional_parameters = list_new();
696 int num = swf_GetU30(tag);
699 int vindex = swf_GetU30(tag);
700 U8 vkind = swf_GetU8(tag); // specifies index type for "val"
701 constant_t*c = constant_fromindex(pool, vindex, vkind);
702 list_append(m->optional_parameters, c);
706 /* debug information- not used by avm2 */
707 multiname_list_t*l = m->parameters;
709 char*name = pool_lookup_string(pool, swf_GetU30(tag));
713 array_append(file->methods, NO_KEY, m);
716 parse_metadata(tag, file, pool);
718 /* skip classes, and scripts for now, and do the real parsing later */
719 int num_classes = swf_GetU30(tag);
720 int classes_pos = tag->pos;
721 DEBUG printf("%d classes\n", num_classes);
722 for(t=0;t<num_classes;t++) {
723 abc_class_t*cls = malloc(sizeof(abc_class_t));
724 memset(cls, 0, sizeof(abc_class_t));
726 DEBUG printf("class %d\n", t);
727 swf_GetU30(tag); //classname
728 swf_GetU30(tag); //supername
730 array_append(file->classes, NO_KEY, cls);
732 cls->flags = swf_GetU8(tag);
734 swf_GetU30(tag); //protectedNS
736 int inum = swf_GetU30(tag); //interface count
738 for(s=0;s<inum;s++) {
739 int interface_index = swf_GetU30(tag);
740 multiname_t* m = multiname_clone(pool_lookup_multiname(pool, interface_index));
741 list_append(cls->interfaces, m);
742 DEBUG printf(" class %d interface: %s\n", t, m->name);
745 swf_GetU30(tag); //iinit
748 for(t=0;t<num_classes;t++) {
749 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
750 int cinit = swf_GetU30(tag);
751 cls->static_constructor = (abc_method_t*)array_getvalue(file->methods, cinit);
754 int num_scripts = swf_GetU30(tag);
755 DEBUG printf("%d scripts\n", num_scripts);
756 for(t=0;t<num_scripts;t++) {
757 int init = swf_GetU30(tag);
761 int num_method_bodies = swf_GetU30(tag);
762 DEBUG printf("%d method bodies\n", num_method_bodies);
763 for(t=0;t<num_method_bodies;t++) {
764 int methodnr = swf_GetU30(tag);
765 if(methodnr >= file->methods->num) {
766 printf("Invalid method number: %d\n", methodnr);
769 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, methodnr);
770 abc_method_body_t*c = malloc(sizeof(abc_method_body_t));
771 memset(c, 0, sizeof(abc_method_body_t));
772 c->old.max_stack = swf_GetU30(tag);
773 c->old.local_count = swf_GetU30(tag);
774 c->old.init_scope_depth = swf_GetU30(tag);
775 c->old.max_scope_depth = swf_GetU30(tag);
777 c->init_scope_depth = c->old.init_scope_depth;
778 int code_length = swf_GetU30(tag);
783 int pos = tag->pos + code_length;
784 codelookup_t*codelookup = 0;
785 c->code = code_parse(tag, code_length, file, pool, &codelookup);
788 int exception_count = swf_GetU30(tag);
790 c->exceptions = list_new();
791 for(s=0;s<exception_count;s++) {
792 exception_t*e = malloc(sizeof(exception_t));
794 e->from = code_atposition(codelookup, swf_GetU30(tag));
795 e->to = code_atposition(codelookup, swf_GetU30(tag));
796 e->target = code_atposition(codelookup, swf_GetU30(tag));
798 e->exc_type = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
799 e->var_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
800 //e->var_name = pool_lookup_string(pool, swf_GetU30(tag));
801 //if(e->var_name) e->var_name = strdup(e->var_name);
802 list_append(c->exceptions, e);
804 codelookup_free(codelookup);
805 c->traits = traits_parse(tag, pool, file);
807 DEBUG printf("method_body %d) (method %d), %d bytes of code", t, methodnr, code_length);
809 array_append(file->method_bodies, NO_KEY, c);
811 if(tag->len - tag->pos) {
812 fprintf(stderr, "%d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
816 swf_SetTagPos(tag, classes_pos);
817 for(t=0;t<num_classes;t++) {
818 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
820 int classname_index = swf_GetU30(tag);
821 int superclass_index = swf_GetU30(tag);
822 cls->classname = multiname_clone(pool_lookup_multiname(pool, classname_index));
823 cls->superclass = multiname_clone(pool_lookup_multiname(pool, superclass_index));
824 cls->flags = swf_GetU8(tag);
827 int ns_index = swf_GetU30(tag);
828 cls->protectedNS = namespace_clone(pool_lookup_namespace(pool, ns_index));
831 int num_interfaces = swf_GetU30(tag); //interface count
833 for(s=0;s<num_interfaces;s++) {
834 swf_GetU30(tag); // multiname index TODO
836 int iinit = swf_GetU30(tag);
837 cls->constructor = (abc_method_t*)array_getvalue(file->methods, iinit);
838 cls->traits = traits_parse(tag, pool, file);
840 for(t=0;t<num_classes;t++) {
841 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
843 swf_GetU30(tag); // cindex
844 cls->static_constructor_traits = traits_parse(tag, pool, file);
846 int num_scripts2 = swf_GetU30(tag);
847 for(t=0;t<num_scripts2;t++) {
848 int init = swf_GetU30(tag);
849 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, init);
851 abc_script_t*s = malloc(sizeof(abc_script_t));
852 memset(s, 0, sizeof(abc_script_t));
854 s->traits = traits_parse(tag, pool, file);
855 array_append(file->scripts, NO_KEY, s);
862 void swf_WriteABC(TAG*abctag, void*code)
864 abc_file_t*file = (abc_file_t*)code;
865 pool_t*pool = pool_new();
867 TAG*tmp = swf_InsertTag(0,0);
871 char need_null_method=0;
872 for(t=0;t<file->classes->num;t++) {
873 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
874 if(!c->constructor || !c->static_constructor) {
880 abc_method_t*nullmethod = 0;
881 if(need_null_method) {
884 /*TODO: might be more efficient to have this at the beginning */
885 array_append(file->methods, NO_KEY, nullmethod);
887 NEW(abc_method_body_t,body);
891 array_append(file->method_bodies, NO_KEY, body);
893 for(t=0;t<file->classes->num;t++) {
894 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
896 c->constructor = nullmethod;
897 if(!c->static_constructor)
898 c->static_constructor = nullmethod;
901 swf_SetU30(tag, file->methods->num);
902 /* enumerate classes, methods and method bodies */
903 for(t=0;t<file->methods->num;t++) {
904 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
907 for(t=0;t<file->classes->num;t++) {
908 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
911 for(t=0;t<file->method_bodies->num;t++) {
912 abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
916 /* generate code statistics */
917 for(t=0;t<file->method_bodies->num;t++) {
918 abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
919 m->stats = code_get_statistics(m->code, m->exceptions);
922 /* level init scope depths: The init scope depth of a method is
923 always as least as high as the init scope depth of it's surrounding
925 A method has it's own init_scope_depth if it's an init method
926 (then its init scope depth is zero), or if it's used as a closure.
928 Not sure yet what to do with methods which are used at different
929 locations- e.g. the nullmethod is used all over the place.
931 Also, I have the strong suspicion that flash player uses only
932 the difference between max_scope_stack and init_scope_stack, anyway.
934 for(t=0;t<file->classes->num;t++) {
935 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
936 trait_list_t*traits = c->traits;
938 c->constructor->body->init_scope_depth < c->init_scope_depth) {
939 c->constructor->body->init_scope_depth = c->init_scope_depth;
941 if(c->static_constructor &&
942 c->static_constructor->body->init_scope_depth < c->init_scope_depth) {
943 c->static_constructor->body->init_scope_depth = c->init_scope_depth;
946 trait_t*trait = traits->trait;
947 if(trait_is_method(trait) && trait->method->body) {
948 abc_method_body_t*body = trait->method->body;
949 if(body->init_scope_depth < c->init_scope_depth) {
950 body->init_scope_depth = c->init_scope_depth;
953 traits = traits->next;
957 for(t=0;t<file->methods->num;t++) {
958 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
960 multiname_list_t*l = m->parameters;
961 int num_params = list_length(m->parameters);
962 swf_SetU30(tag, num_params);
964 swf_SetU30(tag, pool_register_multiname(pool, m->return_type));
969 swf_SetU30(tag, pool_register_multiname(pool, l->multiname));
973 swf_SetU30(tag, pool_register_string(pool, m->name));
978 U8 flags = m->flags&(METHOD_NEED_REST|METHOD_NEED_ARGUMENTS);
979 if(m->optional_parameters)
980 flags |= METHOD_HAS_OPTIONAL;
982 flags |= m->body->stats->flags;
985 swf_SetU8(tag, flags);
986 if(flags&METHOD_HAS_OPTIONAL) {
987 swf_SetU30(tag, list_length(m->optional_parameters));
988 constant_list_t*l = m->optional_parameters;
990 swf_SetU30(tag, constant_get_index(pool, l->constant));
991 swf_SetU8(tag, l->constant->type);
998 swf_SetU30(tag, file->metadata->num);
999 for(t=0;t<file->metadata->num;t++) {
1000 const char*entry_name = array_getkey(file->metadata, t);
1001 swf_SetU30(tag, pool_register_string(pool, entry_name));
1002 array_t*items = (array_t*)array_getvalue(file->metadata, t);
1003 swf_SetU30(tag, items->num);
1005 for(s=0;s<items->num;s++) {
1006 int i1 = pool_register_string(pool, array_getkey(items, s));
1007 int i2 = pool_register_string(pool, array_getvalue(items, s));
1008 swf_SetU30(tag, i1);
1009 swf_SetU30(tag, i2);
1013 swf_SetU30(tag, file->classes->num);
1014 for(t=0;t<file->classes->num;t++) {
1015 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1017 int classname_index = pool_register_multiname(pool, c->classname);
1018 int superclass_index = pool_register_multiname(pool, c->superclass);
1020 swf_SetU30(tag, classname_index);
1021 swf_SetU30(tag, superclass_index);
1023 swf_SetU8(tag, c->flags); // flags
1025 int ns_index = pool_register_namespace(pool, c->protectedNS);
1026 swf_SetU30(tag, ns_index);
1029 swf_SetU30(tag, list_length(c->interfaces));
1030 multiname_list_t*interface= c->interfaces;
1032 swf_SetU30(tag, pool_register_multiname(pool, interface->multiname));
1033 interface = interface->next;
1036 if(!c->constructor) {
1037 swf_SetU30(tag, nullmethod->index);
1039 swf_SetU30(tag, c->constructor->index);
1041 traits_write(pool, tag, c->traits);
1043 for(t=0;t<file->classes->num;t++) {
1044 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1045 if(!c->static_constructor) {
1046 swf_SetU30(tag, nullmethod->index);
1048 swf_SetU30(tag, c->static_constructor->index);
1050 traits_write(pool, tag, c->static_constructor_traits);
1053 swf_SetU30(tag, file->scripts->num);
1054 for(t=0;t<file->scripts->num;t++) {
1055 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1056 swf_SetU30(tag, s->method->index); //!=t!
1057 traits_write(pool, tag, s->traits);
1060 swf_SetU30(tag, file->method_bodies->num);
1061 for(t=0;t<file->method_bodies->num;t++) {
1062 abc_method_body_t*c = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1063 abc_method_t*m = c->method;
1064 swf_SetU30(tag, m->index);
1066 //swf_SetU30(tag, c->old.max_stack);
1067 //swf_SetU30(tag, c->old.local_count);
1068 //swf_SetU30(tag, c->old.init_scope_depth);
1069 //swf_SetU30(tag, c->old.max_scope_depth);
1071 swf_SetU30(tag, c->stats->max_stack);
1072 if(list_length(c->method->parameters)+1 <= c->stats->local_count)
1073 swf_SetU30(tag, c->stats->local_count);
1075 swf_SetU30(tag, list_length(c->method->parameters)+1);
1076 swf_SetU30(tag, c->init_scope_depth);
1077 swf_SetU30(tag, c->stats->max_scope_depth+
1078 c->init_scope_depth);
1080 code_write(tag, c->code, pool, file);
1082 swf_SetU30(tag, list_length(c->exceptions));
1083 exception_list_t*l = c->exceptions;
1085 // warning: assumes "pos" in each code_t is up-to-date
1086 swf_SetU30(tag, l->exception->from->pos);
1087 swf_SetU30(tag, l->exception->to->pos);
1088 swf_SetU30(tag, l->exception->target->pos);
1089 swf_SetU30(tag, pool_register_multiname(pool, l->exception->exc_type));
1090 swf_SetU30(tag, pool_register_multiname(pool, l->exception->var_name));
1094 traits_write(pool, tag, c->traits);
1097 /* free temporary codestat data again. Notice: If we were to write this
1098 file multiple times, this can also be shifted to abc_file_free() */
1099 for(t=0;t<file->method_bodies->num;t++) {
1100 abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1101 codestats_free(m->stats);m->stats=0;
1104 // --- start to write real tag --
1108 if(tag->id == ST_DOABC) {
1109 swf_SetU32(tag, file->flags); // flags
1110 swf_SetString(tag, file->name);
1113 swf_SetU16(tag, 0x10); //version
1114 swf_SetU16(tag, 0x2e);
1116 pool_write(pool, tag);
1118 swf_SetBlock(tag, tmp->data, tmp->len);
1120 swf_DeleteTag(0, tmp);
1124 void abc_file_free(abc_file_t*file)
1127 for(t=0;t<file->metadata->num;t++) {
1128 array_t*items = (array_t*)array_getvalue(file->metadata, t);
1130 for(s=0;s<items->num;s++) {
1131 free(array_getvalue(items, s));
1135 array_free(file->metadata);
1137 for(t=0;t<file->methods->num;t++) {
1138 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1140 multiname_list_t*param = m->parameters;
1142 multiname_destroy(param->multiname);param->multiname=0;
1143 param = param->next;
1145 list_free(m->parameters);m->parameters=0;
1147 constant_list_t*opt = m->optional_parameters;
1149 constant_free(opt->constant);opt->constant=0;
1152 list_free(m->optional_parameters);m->optional_parameters=0;
1155 free((void*)m->name);m->name=0;
1157 if(m->return_type) {
1158 multiname_destroy(m->return_type);
1162 array_free(file->methods);
1164 for(t=0;t<file->classes->num;t++) {
1165 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1166 traits_free(cls->traits);cls->traits=0;
1167 traits_free(cls->static_constructor_traits);cls->static_constructor_traits=0;
1169 if(cls->classname) {
1170 multiname_destroy(cls->classname);
1172 if(cls->superclass) {
1173 multiname_destroy(cls->superclass);
1176 multiname_list_t*i = cls->interfaces;
1178 multiname_destroy(i->multiname);i->multiname=0;
1181 list_free(cls->interfaces);cls->interfaces=0;
1183 if(cls->protectedNS) {
1184 namespace_destroy(cls->protectedNS);
1188 array_free(file->classes);
1190 for(t=0;t<file->scripts->num;t++) {
1191 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1192 traits_free(s->traits);s->traits=0;
1195 array_free(file->scripts);
1197 for(t=0;t<file->method_bodies->num;t++) {
1198 abc_method_body_t*body = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1199 code_free(body->code);body->code=0;
1200 traits_free(body->traits);body->traits=0;
1202 exception_list_t*ee = body->exceptions;
1204 exception_t*e=ee->exception;ee->exception=0;
1205 e->from = e->to = e->target = 0;
1206 multiname_destroy(e->exc_type);e->exc_type=0;
1207 multiname_destroy(e->var_name);e->var_name=0;
1211 list_free(body->exceptions);body->exceptions=0;
1215 array_free(file->method_bodies);
1218 free((void*)file->name);file->name=0;
1224 void swf_FreeABC(void*code)
1226 abc_file_t*file= (abc_file_t*)code;
1227 abc_file_free(file);
1230 void swf_AddButtonLinks(SWF*swf, char stop_each_frame, char events)
1233 int has_buttons = 0;
1234 TAG*tag=swf->firstTag;
1236 if(tag->id == ST_SHOWFRAME)
1238 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2)
1243 abc_file_t*file = abc_file_new();
1244 abc_method_body_t*c = 0;
1246 abc_class_t*cls = abc_class_new2(file, "rfx::MainTimeline", "flash.display::MovieClip");
1247 abc_class_protectedNS(cls, "rfx:MainTimeline");
1249 TAG*abctag = swf_InsertTagBefore(swf, swf->firstTag, ST_DOABC);
1251 tag = swf_InsertTag(abctag, ST_SYMBOLCLASS);
1254 swf_SetString(tag, "rfx.MainTimeline");
1256 c = abc_class_staticconstructor(cls, 0, 0);
1257 c->old.max_stack = 1;
1258 c->old.local_count = 1;
1259 c->old.init_scope_depth = 9;
1260 c->old.max_scope_depth = 10;
1266 c = abc_class_constructor(cls, 0, 0);
1267 c->old.max_stack = 3;
1268 c->old.local_count = 1;
1269 c->old.init_scope_depth = 10;
1270 c->old.max_scope_depth = 11;
1272 debugfile(c, "constructor.as");
1278 __ constructsuper(c,0);
1280 __ getlex(c, "[package]flash.system::Security");
1281 __ pushstring(c, "*");
1282 __ callpropvoid(c, "[package]::allowDomain", 1);
1284 if(stop_each_frame || has_buttons) {
1286 tag = swf->firstTag;
1287 abc_method_body_t*f = 0; //frame script
1288 while(tag && tag->id!=ST_END) {
1290 char needs_framescript=0;
1291 char buttonname[80];
1292 char functionname[80];
1293 sprintf(framename, "[packageinternal]rfx::frame%d", frame);
1295 if(!f && (tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2 || stop_each_frame)) {
1296 /* make the contructor add a frame script */
1297 __ findpropstrict(c,"[package]::addFrameScript");
1298 __ pushbyte(c,frame);
1299 __ getlex(c,framename);
1300 __ callpropvoid(c,"[package]::addFrameScript",2);
1302 f = abc_class_method(cls, 0, framename, 0);
1303 f->old.max_stack = 3;
1304 f->old.local_count = 1;
1305 f->old.init_scope_depth = 10;
1306 f->old.max_scope_depth = 11;
1307 __ debugfile(f, "framescript.as");
1313 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1314 U16 id = swf_GetDefineID(tag);
1315 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1316 __ getlex(f,buttonname);
1317 __ getlex(f,"flash.events::MouseEvent");
1318 __ getproperty(f, "::CLICK");
1319 sprintf(functionname, "::clickbutton%d", swf_GetDefineID(tag));
1320 __ getlex(f,functionname);
1321 __ callpropvoid(f, "::addEventListener" ,2);
1323 if(stop_each_frame) {
1324 __ findpropstrict(f, "[package]::stop");
1325 __ callpropvoid(f, "[package]::stop", 0);
1327 needs_framescript = 1;
1329 abc_method_body_t*h =
1330 abc_class_method(cls, 0, functionname, 1, "flash.events::MouseEvent");
1331 h->old.max_stack = 6;
1332 h->old.local_count = 2;
1333 h->old.init_scope_depth = 10;
1334 h->old.max_scope_depth = 11;
1338 ActionTAG*oldaction = swf_ButtonGetAction(tag);
1339 if(oldaction && oldaction->op == ACTION__GOTOFRAME) {
1340 int framenr = GET16(oldaction->data);
1342 fprintf(stderr, "Warning: Couldn't translate jump to frame %d to flash 9 actionscript\n", framenr);
1345 __ findpropstrict(h,"[package]::gotoAndStop");
1346 __ pushbyte(h,framenr+1);
1347 __ callpropvoid(h,"[package]::gotoAndStop", 1);
1350 sprintf(framename, "frame%d", framenr);
1351 __ getlocal_0(h); //this
1352 __ findpropstrict(h, "[package]flash.events::TextEvent");
1353 __ pushstring(h, "link");
1356 __ pushstring(h, framename);
1357 __ constructprop(h,"[package]flash.events::TextEvent", 4);
1358 __ callpropvoid(h,"[package]::dispatchEvent", 1);
1360 } else if(oldaction && oldaction->op == ACTION__GETURL) {
1362 __ findpropstrict(h,"flash.net::navigateToURL");
1363 __ findpropstrict(h,"flash.net::URLRequest");
1364 // TODO: target _blank
1365 __ pushstring(h,oldaction->data); //url
1366 __ constructprop(h,"flash.net::URLRequest", 1);
1367 __ callpropvoid(h,"flash.net::navigateToURL", 1);
1369 __ getlocal_0(h); //this
1370 __ findpropstrict(h, "[package]flash.events::TextEvent");
1371 __ pushstring(h, "link");
1374 __ pushstring(h,oldaction->data); //url
1375 __ constructprop(h,"[package]flash.events::TextEvent", 4);
1376 __ callpropvoid(h,"[package]::dispatchEvent", 1);
1378 } else if(oldaction) {
1379 fprintf(stderr, "Warning: Couldn't translate button code of button %d to flash 9 abc action\n", id);
1382 swf_ActionFree(oldaction);
1384 if(tag->id == ST_SHOWFRAME) {
1399 tag = swf->firstTag;
1401 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1402 char buttonname[80];
1403 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1404 abc_AddSlot(cls, buttonname, 0, "flash.display::SimpleButton");
1410 abc_script_t*s = abc_initscript(file, 0, 0);
1411 c = s->method->body;
1412 c->old.max_stack = 2;
1413 c->old.local_count = 1;
1414 c->old.init_scope_depth = 1;
1415 c->old.max_scope_depth = 9;
1419 __ getscopeobject(c, 0);
1420 __ getlex(c,"::Object");
1422 __ getlex(c,"flash.events::EventDispatcher");
1424 __ getlex(c,"flash.display::DisplayObject");
1426 __ getlex(c,"flash.display::InteractiveObject");
1428 __ getlex(c,"flash.display::DisplayObjectContainer");
1430 __ getlex(c,"flash.display::Sprite");
1432 __ getlex(c,"flash.display::MovieClip");
1434 __ getlex(c,"flash.display::MovieClip");
1443 __ initproperty(c,"rfx::MainTimeline");
1446 //abc_method_body_addClassTrait(c, "rfx:MainTimeline", 1, cls);
1447 multiname_t*classname = multiname_fromstring("rfx::MainTimeline");
1448 abc_initscript_addClassTrait(s, classname, cls);
1449 multiname_destroy(classname);
1451 swf_WriteABC(abctag, file);