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_to_string(multiname_list_t*list)
46 int n = list_length(list);
47 char**names = (char**)malloc(sizeof(char*)*n);
53 names[n] = multiname_to_string(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();
131 #define CLASS_SEALED 1
132 #define CLASS_FINAL 2
133 #define CLASS_INTERFACE 4
134 #define CLASS_PROTECTED_NS 8
136 abc_class_t* abc_class_new(abc_file_t*file, multiname_t*classname, multiname_t*superclass) {
139 array_append(file->classes, NO_KEY, c);
142 c->classname = classname;
143 c->superclass = superclass;
146 c->static_constructor = 0;
147 c->traits = list_new();
150 abc_class_t* abc_class_new2(abc_file_t*pool, char*classname, char*superclass)
152 return abc_class_new(pool, multiname_fromstring(classname), multiname_fromstring(superclass));
155 void abc_class_sealed(abc_class_t*c)
157 c->flags |= CLASS_SEALED;
159 void abc_class_final(abc_class_t*c)
161 c->flags |= CLASS_FINAL;
163 void abc_class_interface(abc_class_t*c)
165 c->flags |= CLASS_INTERFACE;
167 void abc_class_protectedNS(abc_class_t*c, char*namespace)
169 c->protectedNS = namespace_new_protected(namespace);
170 c->flags |= CLASS_PROTECTED_NS;
172 void abc_class_add_interface(abc_class_t*c, multiname_t*interface)
174 list_append(c->interfaces, interface);
177 abc_method_body_t* add_method(abc_file_t*file, abc_class_t*cls, char*returntype, int num_params, va_list va)
179 /* construct code (method body) object */
180 NEW(abc_method_body_t,c);
181 array_append(file->method_bodies, NO_KEY, c);
183 c->traits = list_new();
186 /* construct method object */
188 array_append(file->methods, NO_KEY, m);
190 if(returntype && strcmp(returntype, "void")) {
191 m->return_type = multiname_fromstring(returntype);
196 for(t=0;t<num_params;t++) {
197 const char*param = va_arg(va, const char*);
198 list_append(m->parameters, multiname_fromstring(param));
201 /* crosslink the two objects */
208 abc_method_body_t* abc_class_constructor(abc_class_t*cls, char*returntype, int num_params, ...)
211 va_start(va, num_params);
212 abc_method_body_t* c = add_method(cls->file, cls, returntype, num_params, va);
214 cls->constructor = c->method;
218 abc_method_body_t* abc_class_staticconstructor(abc_class_t*cls, char*returntype, int num_params, ...)
221 va_start(va, num_params);
222 abc_method_body_t* c = add_method(cls->file, cls, returntype, num_params, va);
224 cls->static_constructor = c->method;
228 trait_t*trait_new(int type, multiname_t*name, int data1, int data2, constant_t*v)
230 trait_t*trait = malloc(sizeof(trait_t));
231 memset(trait, 0, sizeof(trait_t));
232 trait->kind = type&0x0f;
233 trait->attributes = type&0xf0;
235 trait->data1 = data1;
236 trait->data2 = data2;
240 trait_t*trait_new_member(multiname_t*type, multiname_t*name,constant_t*v)
242 int kind = TRAIT_SLOT;
243 trait_t*trait = malloc(sizeof(trait_t));
244 memset(trait, 0, sizeof(trait_t));
245 trait->kind = kind&0x0f;
246 trait->attributes = kind&0xf0;
248 trait->type_name = type;
251 trait_t*trait_new_method(multiname_t*name, abc_method_t*m)
253 int type = TRAIT_METHOD;
254 trait_t*trait = malloc(sizeof(trait_t));
255 memset(trait, 0, sizeof(trait_t));
256 trait->kind = type&0x0f;
257 trait->attributes = type&0xf0;
263 abc_method_body_t* abc_class_method(abc_class_t*cls, char*returntype, char*name, int num_params, ...)
265 abc_file_t*file = cls->file;
267 va_start(va, num_params);
268 abc_method_body_t* c = add_method(cls->file, cls, returntype, num_params, va);
270 list_append(cls->traits, trait_new_method(multiname_fromstring(name), c->method));
274 void abc_AddSlot(abc_class_t*cls, char*name, int slot, char*type)
276 abc_file_t*file = cls->file;
277 multiname_t*m_name = multiname_fromstring(name);
278 multiname_t*m_type = multiname_fromstring(type);
279 trait_t*t = trait_new_member(m_type, m_name, 0);
280 t->slot_id = list_length(cls->traits);
281 list_append(cls->traits, t);
284 void abc_method_body_addClassTrait(abc_method_body_t*code, char*multiname, int slotid, abc_class_t*cls)
286 abc_file_t*file = code->file;
287 multiname_t*m = multiname_fromstring(multiname);
288 trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0);
290 list_append(code->traits, trait);
293 /* notice: traits of a method (body) belonging to an init script
294 and traits of the init script are *not* the same thing */
295 int abc_initscript_addClassTrait(abc_script_t*script, multiname_t*multiname, abc_class_t*cls)
297 abc_file_t*file = script->file;
298 multiname_t*m = multiname_clone(multiname);
299 int slotid = list_length(script->traits)+1;
300 trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0);
302 list_append(script->traits, trait);
306 abc_script_t* abc_initscript(abc_file_t*file, char*returntype, int num_params, ...)
309 va_start(va, num_params);
310 abc_method_body_t* c = add_method(file, 0, returntype, num_params, va);
311 abc_script_t* s = malloc(sizeof(abc_script_t));
312 s->method = c->method;
313 s->traits = list_new();
315 array_append(file->scripts, NO_KEY, s);
320 static void traits_dump(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file);
322 static void dump_method(FILE*fo, const char*prefix, const char*type, const char*name, abc_method_t*m, abc_file_t*file)
324 char*return_type = 0;
326 return_type = multiname_to_string(m->return_type);
328 return_type = strdup("void");
329 char*paramstr = params_to_string(m->parameters);
330 fprintf(fo, "%s%s %s %s=%s %s (%d params)\n", prefix, type, return_type, name, m->name, paramstr, list_length(m->parameters));
331 free(paramstr);paramstr=0;
332 free(return_type);return_type=0;
334 abc_method_body_t*c = m->body;
339 fprintf(fo, "%s[stack:%d locals:%d scope:%d-%d flags:%02x]\n", prefix, c->max_stack, c->local_count, c->init_scope_depth, c->max_scope_depth, c->method->flags);
342 sprintf(prefix2, "%s ", prefix);
344 traits_dump(fo, prefix, c->traits, file);
345 fprintf(fo, "%s{\n", prefix);
346 code_dump(c->code, c->exceptions, file, prefix2, fo);
347 fprintf(fo, "%s}\n\n", prefix);
350 static void traits_free(trait_list_t*traits)
352 trait_list_t*t = traits;
355 multiname_destroy(t->trait->name);t->trait->name = 0;
357 if(t->trait->kind == TRAIT_SLOT || t->trait->kind == TRAIT_CONST) {
358 multiname_destroy(t->trait->type_name);
360 if(t->trait->value) {
361 constant_free(t->trait->value);t->trait->value = 0;
363 free(t->trait);t->trait = 0;
369 static trait_list_t* traits_parse(TAG*tag, pool_t*pool, abc_file_t*file)
371 int num_traits = swf_GetU30(tag);
372 trait_list_t*traits = list_new();
375 DEBUG printf("%d traits\n", num_traits);
378 for(t=0;t<num_traits;t++) {
380 list_append(traits, trait);
382 trait->name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag))); // always a QName (ns,name)
385 DEBUG name = multiname_to_string(trait->name);
386 U8 kind = swf_GetU8(tag);
387 U8 attributes = kind&0xf0;
390 trait->attributes = attributes;
391 DEBUG printf(" trait %d) %s type=%02x\n", t, name, kind);
392 if(kind == TRAIT_METHOD || kind == TRAIT_GETTER || kind == TRAIT_SETTER) { // method / getter / setter
393 trait->disp_id = swf_GetU30(tag);
394 trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
395 DEBUG printf(" method/getter/setter\n");
396 } else if(kind == TRAIT_FUNCTION) { // function
397 trait->slot_id = swf_GetU30(tag);
398 trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
399 } else if(kind == TRAIT_CLASS) { // class
400 trait->slot_id = swf_GetU30(tag);
401 trait->cls = (abc_class_t*)array_getvalue(file->classes, swf_GetU30(tag));
402 DEBUG printf(" class %s %d %d\n", name, trait->slot_id, trait->cls);
403 } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
404 /* a slot is a variable in a class that is shared amonst all instances
405 of the same type, but which has a unique location in each object
406 (in other words, slots are non-static, traits are static)
408 trait->slot_id = swf_GetU30(tag);
409 trait->type_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
410 int vindex = swf_GetU30(tag);
412 int vkind = swf_GetU8(tag);
413 trait->value = constant_fromindex(pool, vindex, vkind);
415 DEBUG printf(" slot %s %d %s (%s)\n", name, trait->slot_id, trait->type_name->name, constant_to_string(trait->value));
417 fprintf(stderr, "Can't parse trait type %d\n", kind);
419 if(attributes&0x40) {
420 int num = swf_GetU30(tag);
423 swf_GetU30(tag); //index into metadata array
430 void traits_skip(TAG*tag)
432 int num_traits = swf_GetU30(tag);
434 for(t=0;t<num_traits;t++) {
436 U8 kind = swf_GetU8(tag);
437 U8 attributes = kind&0xf0;
441 if(kind == TRAIT_SLOT || kind == TRAIT_CONST) {
442 if(swf_GetU30(tag)) swf_GetU8(tag);
443 } else if(kind>TRAIT_CONST) {
444 fprintf(stderr, "Can't parse trait type %d\n", kind);
446 if(attributes&0x40) {
447 int s, num = swf_GetU30(tag);
448 for(s=0;s<num;s++) swf_GetU30(tag);
454 static void traits_write(pool_t*pool, TAG*tag, trait_list_t*traits)
460 swf_SetU30(tag, list_length(traits));
464 trait_t*trait = traits->trait;
466 swf_SetU30(tag, pool_register_multiname(pool, trait->name));
467 swf_SetU8(tag, trait->kind|trait->attributes);
469 swf_SetU30(tag, trait->data1);
471 if(trait->kind == TRAIT_CLASS) {
472 swf_SetU30(tag, trait->cls->index);
473 } else if(trait->kind == TRAIT_GETTER ||
474 trait->kind == TRAIT_SETTER ||
475 trait->kind == TRAIT_METHOD) {
476 swf_SetU30(tag, trait->method->index);
477 } else if(trait->kind == TRAIT_SLOT ||
478 trait->kind == TRAIT_CONST) {
479 int index = pool_register_multiname(pool, trait->type_name);
480 swf_SetU30(tag, index);
482 swf_SetU30(tag, trait->data2);
485 if(trait->kind == TRAIT_SLOT || trait->kind == TRAIT_CONST) {
486 int vindex = constant_get_index(pool, trait->value);
487 swf_SetU30(tag, vindex);
489 swf_SetU8(tag, trait->value->type);
492 if(trait->attributes&0x40) {
496 traits = traits->next;
501 static void traits_dump(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file)
505 trait_t*trait = traits->trait;
506 char*name = multiname_to_string(trait->name);
507 U8 kind = trait->kind;
508 U8 attributes = trait->attributes;
509 if(kind == TRAIT_METHOD) {
510 abc_method_t*m = trait->method;
511 dump_method(fo, prefix, "method", name, m, file);
512 } else if(kind == TRAIT_GETTER) {
513 abc_method_t*m = trait->method;
514 dump_method(fo, prefix, "getter", name, m, file);
515 } else if(kind == TRAIT_SETTER) {
516 abc_method_t*m = trait->method;
517 dump_method(fo, prefix, "setter", name, m, file);
518 } else if(kind == TRAIT_FUNCTION) { // function
519 abc_method_t*m = trait->method;
520 dump_method(fo, prefix, "function", name, m, file);
521 } else if(kind == TRAIT_CLASS) { // class
522 abc_class_t*cls = trait->cls;
524 fprintf(fo, "%sslot %d: class %s=00000000\n", prefix, trait->slot_id, name);
526 fprintf(fo, "%sslot %d: class %s=%s\n", prefix, trait->slot_id, name, cls->classname->name);
528 } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
529 int slot_id = trait->slot_id;
530 char*type_name = multiname_to_string(trait->type_name);
531 char*value = constant_to_string(trait->value);
532 fprintf(fo, "%sslot %d: %s%s %s %s %s\n", prefix, trait->slot_id,
533 kind==TRAIT_CONST?"const ":"", type_name, name,
534 value?"=":"", value);
535 if(value) free(value);
538 fprintf(fo, "%s can't dump trait type %d\n", prefix, kind);
545 void* swf_DumpABC(FILE*fo, void*code, char*prefix)
547 abc_file_t* file = (abc_file_t*)code;
550 fprintf(fo, "%s#\n", prefix);
551 fprintf(fo, "%s#name: %s\n", prefix, file->name);
552 fprintf(fo, "%s#\n", prefix);
556 for(t=0;t<file->metadata->num;t++) {
557 const char*entry_name = array_getkey(file->metadata, t);
558 fprintf(fo, "%s#Metadata \"%s\":\n", prefix, entry_name);
560 array_t*items = (array_t*)array_getvalue(file->metadata, t);
561 for(s=0;s<items->num;s++) {
562 fprintf(fo, "%s# %s=%s\n", prefix, array_getkey(items, s), array_getvalue(items,s));
564 fprintf(fo, "%s#\n", prefix);
567 for(t=0;t<file->classes->num;t++) {
568 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
570 sprintf(prefix2, "%s ", prefix);
572 fprintf(fo, "%s", prefix);
573 if(cls->flags&1) fprintf(fo, "sealed ");
574 if(cls->flags&2) fprintf(fo, "final ");
575 if(cls->flags&4) fprintf(fo, "interface ");
577 char*s = namespace_to_string(cls->protectedNS);
578 fprintf(fo, "protectedNS(%s) ", s);
582 char*classname = multiname_to_string(cls->classname);
583 fprintf(fo, "class %s", classname);
585 if(cls->superclass) {
586 char*supername = multiname_to_string(cls->superclass);
587 fprintf(fo, " extends %s", supername);
589 multiname_list_t*ilist = cls->interfaces;
591 fprintf(fo, " implements");
593 char*s = multiname_to_string(ilist->multiname);
594 fprintf(fo, " %s", s);
601 fprintf(fo, "extra flags=%02x\n", cls->flags&0xf0);
602 fprintf(fo, "%s{\n", prefix);
604 if(cls->static_constructor)
605 dump_method(fo, prefix2,"staticconstructor", "", cls->static_constructor, file);
606 traits_dump(fo, prefix2, cls->static_constructor_traits, file);
608 char*n = multiname_to_string(cls->classname);
610 dump_method(fo, prefix2, "constructor", n, cls->constructor, file);
612 traits_dump(fo, prefix2,cls->traits, file);
613 fprintf(fo, "%s}\n", prefix);
615 fprintf(fo, "%s\n", prefix);
617 for(t=0;t<file->scripts->num;t++) {
618 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
619 dump_method(fo, prefix,"initmethod", "init", s->method, file);
620 traits_dump(fo, prefix, s->traits, file);
625 void* swf_ReadABC(TAG*tag)
627 abc_file_t* file = abc_file_new();
628 pool_t*pool = pool_new();
630 swf_SetTagPos(tag, 0);
632 if(tag->id == ST_DOABC) {
633 U32 abcflags = swf_GetU32(tag);
634 DEBUG printf("flags=%08x\n", abcflags);
635 char*name= swf_GetString(tag);
636 file->name = (name&&name[0])?strdup(name):0;
638 U32 version = swf_GetU32(tag);
639 if(version!=0x002e0010) {
640 fprintf(stderr, "Warning: unknown AVM2 version %08x\n", version);
643 pool_read(pool, tag);
645 int num_methods = swf_GetU30(tag);
646 DEBUG printf("%d methods\n", num_methods);
647 for(t=0;t<num_methods;t++) {
649 int param_count = swf_GetU30(tag);
650 int return_type_index = swf_GetU30(tag);
651 if(return_type_index)
652 m->return_type = multiname_clone(pool_lookup_multiname(pool, return_type_index));
657 for(s=0;s<param_count;s++) {
658 int type_index = swf_GetU30(tag);
660 /* type_index might be 0, which probably means "..." (varargs) */
661 multiname_t*param = type_index?multiname_clone(pool_lookup_multiname(pool, type_index)):0;
662 list_append(m->parameters, param);
665 int namenr = swf_GetU30(tag);
667 m->name = strdup(pool_lookup_string(pool, namenr));
669 m->name = strdup("");
671 m->flags = swf_GetU8(tag);
673 DEBUG printf("method %d) %s flags=%02x\n", t, params_to_string(m->parameters), m->flags);
676 /* TODO optional parameters */
677 m->optional_parameters = list_new();
678 int num = swf_GetU30(tag);
681 int vindex = swf_GetU30(tag);
682 U8 vkind = swf_GetU8(tag); // specifies index type for "val"
683 constant_t*c = constant_fromindex(pool, vindex, vkind);
684 list_append(m->optional_parameters, c);
688 /* debug information- not used by avm2 */
689 multiname_list_t*l = m->parameters;
691 char*name = pool_lookup_string(pool, swf_GetU30(tag));
695 array_append(file->methods, NO_KEY, m);
698 parse_metadata(tag, file, pool);
700 /* skip classes, and scripts for now, and do the real parsing later */
701 int num_classes = swf_GetU30(tag);
702 int classes_pos = tag->pos;
703 DEBUG printf("%d classes\n", num_classes);
704 for(t=0;t<num_classes;t++) {
705 abc_class_t*cls = malloc(sizeof(abc_class_t));
706 memset(cls, 0, sizeof(abc_class_t));
708 DEBUG printf("class %d\n", t);
709 swf_GetU30(tag); //classname
710 swf_GetU30(tag); //supername
712 array_append(file->classes, NO_KEY, cls);
714 cls->flags = swf_GetU8(tag);
716 swf_GetU30(tag); //protectedNS
718 int inum = swf_GetU30(tag); //interface count
720 for(s=0;s<inum;s++) {
721 int interface_index = swf_GetU30(tag);
722 multiname_t* m = multiname_clone(pool_lookup_multiname(pool, interface_index));
723 list_append(cls->interfaces, m);
724 DEBUG printf(" class %d interface: %s\n", t, m->name);
727 swf_GetU30(tag); //iinit
730 for(t=0;t<num_classes;t++) {
731 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
732 int cinit = swf_GetU30(tag);
733 cls->static_constructor = (abc_method_t*)array_getvalue(file->methods, cinit);
736 int num_scripts = swf_GetU30(tag);
737 DEBUG printf("%d scripts\n", num_scripts);
738 for(t=0;t<num_scripts;t++) {
739 int init = swf_GetU30(tag);
743 int num_method_bodies = swf_GetU30(tag);
744 DEBUG printf("%d method bodies\n", num_method_bodies);
745 for(t=0;t<num_method_bodies;t++) {
746 int methodnr = swf_GetU30(tag);
747 if(methodnr >= file->methods->num) {
748 printf("Invalid method number: %d\n", methodnr);
751 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, methodnr);
752 abc_method_body_t*c = malloc(sizeof(abc_method_body_t));
753 memset(c, 0, sizeof(abc_method_body_t));
754 c->max_stack = swf_GetU30(tag);
755 c->local_count = swf_GetU30(tag);
756 c->init_scope_depth = swf_GetU30(tag);
757 c->max_scope_depth = swf_GetU30(tag);
758 int code_length = swf_GetU30(tag);
763 int pos = tag->pos + code_length;
764 codelookup_t*codelookup = 0;
765 c->code = code_parse(tag, code_length, file, pool, &codelookup);
768 int exception_count = swf_GetU30(tag);
770 c->exceptions = list_new();
771 for(s=0;s<exception_count;s++) {
772 exception_t*e = malloc(sizeof(exception_t));
774 e->from = code_atposition(codelookup, swf_GetU30(tag));
775 e->to = code_atposition(codelookup, swf_GetU30(tag));
776 e->target = code_atposition(codelookup, swf_GetU30(tag));
778 e->exc_type = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
779 e->var_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
780 //e->var_name = pool_lookup_string(pool, swf_GetU30(tag));
781 //if(e->var_name) e->var_name = strdup(e->var_name);
782 list_append(c->exceptions, e);
784 codelookup_free(codelookup);
785 c->traits = traits_parse(tag, pool, file);
787 DEBUG printf("method_body %d) (method %d), %d bytes of code", t, methodnr, code_length);
789 array_append(file->method_bodies, NO_KEY, c);
791 if(tag->len - tag->pos) {
792 fprintf(stderr, "%d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
796 swf_SetTagPos(tag, classes_pos);
797 for(t=0;t<num_classes;t++) {
798 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
800 int classname_index = swf_GetU30(tag);
801 int superclass_index = swf_GetU30(tag);
802 cls->classname = multiname_clone(pool_lookup_multiname(pool, classname_index));
803 cls->superclass = multiname_clone(pool_lookup_multiname(pool, superclass_index));
804 cls->flags = swf_GetU8(tag);
807 int ns_index = swf_GetU30(tag);
808 cls->protectedNS = namespace_clone(pool_lookup_namespace(pool, ns_index));
811 int num_interfaces = swf_GetU30(tag); //interface count
813 for(s=0;s<num_interfaces;s++) {
814 swf_GetU30(tag); // multiname index TODO
816 int iinit = swf_GetU30(tag);
817 cls->constructor = (abc_method_t*)array_getvalue(file->methods, iinit);
818 cls->traits = traits_parse(tag, pool, file);
820 for(t=0;t<num_classes;t++) {
821 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
823 swf_GetU30(tag); // cindex
824 cls->static_constructor_traits = traits_parse(tag, pool, file);
826 int num_scripts2 = swf_GetU30(tag);
827 for(t=0;t<num_scripts2;t++) {
828 int init = swf_GetU30(tag);
829 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, init);
831 abc_script_t*s = malloc(sizeof(abc_script_t));
832 memset(s, 0, sizeof(abc_script_t));
834 s->traits = traits_parse(tag, pool, file);
835 array_append(file->scripts, NO_KEY, s);
837 fprintf(stderr, "Can't parse script traits\n");
846 void swf_WriteABC(TAG*abctag, void*code)
848 abc_file_t*file = (abc_file_t*)code;
849 pool_t*pool = pool_new();
851 TAG*tmp = swf_InsertTag(0,0);
855 char need_null_method=0;
856 for(t=0;t<file->classes->num;t++) {
857 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
858 if(!c->constructor || !c->static_constructor) {
864 abc_method_t*nullmethod = 0;
865 if(need_null_method) {
866 nullmethod = malloc(sizeof(abc_method_t));
867 memset(nullmethod, 0, sizeof(abc_method_t));
868 /*TODO: might be more efficient to have this at the beginning */
869 array_append(file->methods, NO_KEY, nullmethod);
872 swf_SetU30(tag, file->methods->num);
873 /* enumerate classes, methods and method bodies */
874 for(t=0;t<file->methods->num;t++) {
875 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
878 for(t=0;t<file->classes->num;t++) {
879 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
882 for(t=0;t<file->method_bodies->num;t++) {
883 abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
887 /* generate code statistics */
888 for(t=0;t<file->method_bodies->num;t++) {
889 abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
890 m->stats = code_get_statistics(m->code, m->exceptions);
893 for(t=0;t<file->methods->num;t++) {
894 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
896 multiname_list_t*l = m->parameters;
897 int num_params = list_length(m->parameters);
898 swf_SetU30(tag, num_params);
900 swf_SetU30(tag, pool_register_multiname(pool, m->return_type));
905 swf_SetU30(tag, pool_register_multiname(pool, l->multiname));
909 swf_SetU30(tag, pool_register_string(pool, m->name));
914 U8 flags = m->flags&(METHOD_NEED_REST|METHOD_NEED_ARGUMENTS);
915 if(m->optional_parameters)
916 flags |= METHOD_HAS_OPTIONAL;
918 flags |= m->body->stats->flags;
921 swf_SetU8(tag, flags);
922 if(flags&METHOD_HAS_OPTIONAL) {
923 swf_SetU30(tag, list_length(m->optional_parameters));
924 constant_list_t*l = m->optional_parameters;
926 swf_SetU30(tag, constant_get_index(pool, l->constant));
927 swf_SetU8(tag, l->constant->type);
934 swf_SetU30(tag, file->metadata->num);
935 for(t=0;t<file->metadata->num;t++) {
936 const char*entry_name = array_getkey(file->metadata, t);
937 swf_SetU30(tag, pool_register_string(pool, entry_name));
938 array_t*items = (array_t*)array_getvalue(file->metadata, t);
939 swf_SetU30(tag, items->num);
941 for(s=0;s<items->num;s++) {
942 int i1 = pool_register_string(pool, array_getkey(items, s));
943 int i2 = pool_register_string(pool, array_getvalue(items, s));
949 swf_SetU30(tag, file->classes->num);
950 for(t=0;t<file->classes->num;t++) {
951 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
953 int classname_index = pool_register_multiname(pool, c->classname);
954 int superclass_index = pool_register_multiname(pool, c->superclass);
956 swf_SetU30(tag, classname_index);
957 swf_SetU30(tag, superclass_index);
959 swf_SetU8(tag, c->flags); // flags
961 int ns_index = pool_register_namespace(pool, c->protectedNS);
962 swf_SetU30(tag, ns_index);
965 swf_SetU30(tag, list_length(c->interfaces));
966 multiname_list_t*interface= c->interfaces;
968 swf_SetU30(tag, pool_register_multiname(pool, interface->multiname));
969 interface = interface->next;
972 if(!c->constructor) {
973 swf_SetU30(tag, nullmethod->index);
975 swf_SetU30(tag, c->constructor->index);
977 traits_write(pool, tag, c->traits);
979 for(t=0;t<file->classes->num;t++) {
980 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
981 if(!c->static_constructor) {
982 swf_SetU30(tag, nullmethod->index);
984 swf_SetU30(tag, c->static_constructor->index);
986 traits_write(pool, tag, c->static_constructor_traits);
989 swf_SetU30(tag, file->scripts->num);
990 for(t=0;t<file->scripts->num;t++) {
991 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
992 swf_SetU30(tag, s->method->index); //!=t!
993 traits_write(pool, tag, s->traits);
996 swf_SetU30(tag, file->method_bodies->num);
997 for(t=0;t<file->method_bodies->num;t++) {
998 abc_method_body_t*c = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
999 abc_method_t*m = c->method;
1000 swf_SetU30(tag, m->index);
1002 //swf_SetU30(tag, c->max_stack);
1003 //swf_SetU30(tag, c->local_count);
1004 //swf_SetU30(tag, c->init_scope_depth);
1005 //swf_SetU30(tag, c->max_scope_depth);
1007 swf_SetU30(tag, c->stats->max_stack);
1008 if(list_length(c->method->parameters)+1 <= c->stats->local_count)
1009 swf_SetU30(tag, c->stats->local_count);
1011 swf_SetU30(tag, list_length(c->method->parameters)+1);
1012 swf_SetU30(tag, c->init_scope_depth);
1013 swf_SetU30(tag, c->stats->max_scope_depth+
1014 c->init_scope_depth);
1016 code_write(tag, c->code, pool, file);
1018 swf_SetU30(tag, list_length(c->exceptions));
1019 exception_list_t*l = c->exceptions;
1021 // warning: assumes "pos" in each code_t is up-to-date
1022 swf_SetU30(tag, l->exception->from->pos);
1023 swf_SetU30(tag, l->exception->to->pos);
1024 swf_SetU30(tag, l->exception->target->pos);
1025 swf_SetU30(tag, pool_register_multiname(pool, l->exception->exc_type));
1026 swf_SetU30(tag, pool_register_multiname(pool, l->exception->var_name));
1030 traits_write(pool, tag, c->traits);
1033 /* free temporary codestat data again. Notice: If we were to write this
1034 file multiple times, this can also be shifted to abc_file_free() */
1035 for(t=0;t<file->method_bodies->num;t++) {
1036 abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1037 codestats_free(m->stats);m->stats=0;
1040 // --- start to write real tag --
1044 if(tag->id == ST_DOABC) {
1045 swf_SetU32(tag, 1); // flags
1046 swf_SetString(tag, file->name);
1049 swf_SetU16(tag, 0x10); //version
1050 swf_SetU16(tag, 0x2e);
1052 pool_write(pool, tag);
1054 swf_SetBlock(tag, tmp->data, tmp->len);
1056 swf_DeleteTag(0, tmp);
1060 void abc_file_free(abc_file_t*file)
1063 for(t=0;t<file->metadata->num;t++) {
1064 array_t*items = (array_t*)array_getvalue(file->metadata, t);
1066 for(s=0;s<items->num;s++) {
1067 free(array_getvalue(items, s));
1071 array_free(file->metadata);
1073 for(t=0;t<file->methods->num;t++) {
1074 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1076 multiname_list_t*param = m->parameters;
1078 multiname_destroy(param->multiname);param->multiname=0;
1079 param = param->next;
1081 list_free(m->parameters);m->parameters=0;
1083 constant_list_t*opt = m->optional_parameters;
1085 constant_free(opt->constant);opt->constant=0;
1088 list_free(m->optional_parameters);m->optional_parameters=0;
1091 free((void*)m->name);m->name=0;
1093 if(m->return_type) {
1094 multiname_destroy(m->return_type);
1098 array_free(file->methods);
1100 for(t=0;t<file->classes->num;t++) {
1101 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1102 traits_free(cls->traits);cls->traits=0;
1103 traits_free(cls->static_constructor_traits);cls->static_constructor_traits=0;
1105 if(cls->classname) {
1106 multiname_destroy(cls->classname);
1108 if(cls->superclass) {
1109 multiname_destroy(cls->superclass);
1112 multiname_list_t*i = cls->interfaces;
1114 multiname_destroy(i->multiname);i->multiname=0;
1117 list_free(cls->interfaces);cls->interfaces=0;
1119 if(cls->protectedNS) {
1120 namespace_destroy(cls->protectedNS);
1124 array_free(file->classes);
1126 for(t=0;t<file->scripts->num;t++) {
1127 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1128 traits_free(s->traits);s->traits=0;
1131 array_free(file->scripts);
1133 for(t=0;t<file->method_bodies->num;t++) {
1134 abc_method_body_t*body = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1135 code_free(body->code);body->code=0;
1136 traits_free(body->traits);body->traits=0;
1138 exception_list_t*ee = body->exceptions;
1140 exception_t*e=ee->exception;ee->exception=0;
1141 e->from = e->to = e->target = 0;
1142 multiname_destroy(e->exc_type);e->exc_type=0;
1143 multiname_destroy(e->var_name);e->var_name=0;
1147 list_free(body->exceptions);body->exceptions=0;
1151 array_free(file->method_bodies);
1154 free((void*)file->name);file->name=0;
1160 void swf_FreeABC(void*code)
1162 abc_file_t*file= (abc_file_t*)code;
1163 abc_file_free(file);
1166 void swf_AddButtonLinks(SWF*swf, char stop_each_frame, char events)
1169 int has_buttons = 0;
1170 TAG*tag=swf->firstTag;
1172 if(tag->id == ST_SHOWFRAME)
1174 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2)
1179 abc_file_t*file = abc_file_new();
1180 abc_method_body_t*c = 0;
1182 abc_class_t*cls = abc_class_new2(file, "rfx::MainTimeline", "flash.display::MovieClip");
1183 abc_class_protectedNS(cls, "rfx:MainTimeline");
1185 TAG*abctag = swf_InsertTagBefore(swf, swf->firstTag, ST_DOABC);
1187 tag = swf_InsertTag(abctag, ST_SYMBOLCLASS);
1190 swf_SetString(tag, "rfx.MainTimeline");
1192 c = abc_class_staticconstructor(cls, 0, 0);
1195 c->init_scope_depth = 9;
1196 c->max_scope_depth = 10;
1202 c = abc_class_constructor(cls, 0, 0);
1205 c->init_scope_depth = 10;
1206 c->max_scope_depth = 11;
1208 debugfile(c, "constructor.as");
1214 __ constructsuper(c,0);
1216 __ getlex(c, "[package]flash.system::Security");
1217 __ pushstring(c, "*");
1218 __ callpropvoid(c, "[package]::allowDomain", 1);
1220 if(stop_each_frame || has_buttons) {
1222 tag = swf->firstTag;
1223 abc_method_body_t*f = 0; //frame script
1224 while(tag && tag->id!=ST_END) {
1226 char needs_framescript=0;
1227 char buttonname[80];
1228 char functionname[80];
1229 sprintf(framename, "[packageinternal]rfx::frame%d", frame);
1231 if(!f && (tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2 || stop_each_frame)) {
1232 /* make the contructor add a frame script */
1233 __ findpropstrict(c,"[package]::addFrameScript");
1234 __ pushbyte(c,frame);
1235 __ getlex(c,framename);
1236 __ callpropvoid(c,"[package]::addFrameScript",2);
1238 f = abc_class_method(cls, 0, framename, 0);
1241 f->init_scope_depth = 10;
1242 f->max_scope_depth = 11;
1243 __ debugfile(f, "framescript.as");
1249 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1250 U16 id = swf_GetDefineID(tag);
1251 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1252 __ getlex(f,buttonname);
1253 __ getlex(f,"flash.events::MouseEvent");
1254 __ getproperty(f, "::CLICK");
1255 sprintf(functionname, "::clickbutton%d", swf_GetDefineID(tag));
1256 __ getlex(f,functionname);
1257 __ callpropvoid(f, "::addEventListener" ,2);
1259 if(stop_each_frame) {
1260 __ findpropstrict(f, "[package]::stop");
1261 __ callpropvoid(f, "[package]::stop", 0);
1263 needs_framescript = 1;
1265 abc_method_body_t*h =
1266 abc_class_method(cls, "::void", functionname, 1, "flash.events::MouseEvent");
1269 h->init_scope_depth = 10;
1270 h->max_scope_depth = 11;
1274 ActionTAG*oldaction = swf_ButtonGetAction(tag);
1275 if(oldaction && oldaction->op == ACTION__GOTOFRAME) {
1276 int framenr = GET16(oldaction->data);
1278 fprintf(stderr, "Warning: Couldn't translate jump to frame %d to flash 9 actionscript\n", framenr);
1281 __ findpropstrict(h,"[package]::gotoAndStop");
1282 __ pushbyte(h,framenr+1);
1283 __ callpropvoid(h,"[package]::gotoAndStop", 1);
1286 sprintf(framename, "frame%d", framenr);
1287 __ getlocal_0(h); //this
1288 __ findpropstrict(h, "[package]flash.events::TextEvent");
1289 __ pushstring(h, "link");
1292 __ pushstring(h, framename);
1293 __ constructprop(h,"[package]flash.events::TextEvent", 4);
1294 __ callpropvoid(h,"[package]::dispatchEvent", 1);
1296 } else if(oldaction && oldaction->op == ACTION__GETURL) {
1298 __ findpropstrict(h,"flash.net::navigateToURL");
1299 __ findpropstrict(h,"flash.net::URLRequest");
1300 // TODO: target _blank
1301 __ pushstring(h,oldaction->data); //url
1302 __ constructprop(h,"flash.net::URLRequest", 1);
1303 __ callpropvoid(h,"flash.net::navigateToURL", 1);
1305 __ getlocal_0(h); //this
1306 __ findpropstrict(h, "[package]flash.events::TextEvent");
1307 __ pushstring(h, "link");
1310 __ pushstring(h,oldaction->data); //url
1311 __ constructprop(h,"[package]flash.events::TextEvent", 4);
1312 __ callpropvoid(h,"[package]::dispatchEvent", 1);
1314 } else if(oldaction) {
1315 fprintf(stderr, "Warning: Couldn't translate button code of button %d to flash 9 abc action\n", id);
1318 swf_ActionFree(oldaction);
1320 if(tag->id == ST_SHOWFRAME) {
1335 tag = swf->firstTag;
1337 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1338 char buttonname[80];
1339 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1340 abc_AddSlot(cls, buttonname, 0, "flash.display::SimpleButton");
1346 abc_script_t*s = abc_initscript(file, 0, 0);
1347 c = s->method->body;
1350 c->init_scope_depth = 1;
1351 c->max_scope_depth = 9;
1355 __ getscopeobject(c, 0);
1356 __ getlex(c,"::Object");
1358 __ getlex(c,"flash.events::EventDispatcher");
1360 __ getlex(c,"flash.display::DisplayObject");
1362 __ getlex(c,"flash.display::InteractiveObject");
1364 __ getlex(c,"flash.display::DisplayObjectContainer");
1366 __ getlex(c,"flash.display::Sprite");
1368 __ getlex(c,"flash.display::MovieClip");
1370 __ getlex(c,"flash.display::MovieClip");
1379 __ initproperty(c,"rfx::MainTimeline");
1382 //abc_method_body_addClassTrait(c, "rfx:MainTimeline", 1, cls);
1383 multiname_t*classname = multiname_fromstring("rfx::MainTimeline");
1384 abc_initscript_addClassTrait(s, classname, cls);
1385 multiname_destroy(classname);
1387 swf_WriteABC(abctag, file);