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();
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, multiname_t*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 m->return_type = returntype;
193 for(t=0;t<num_params;t++) {
194 const char*param = va_arg(va, const char*);
195 list_append(m->parameters, multiname_fromstring(param));
198 /* crosslink the two objects */
205 abc_method_body_t* abc_class_constructor(abc_class_t*cls, multiname_t*returntype, int num_params, ...)
208 va_start(va, num_params);
209 abc_method_body_t* c = add_method(cls->file, cls, returntype, num_params, va);
211 cls->constructor = c->method;
215 abc_method_body_t* abc_class_staticconstructor(abc_class_t*cls, multiname_t*returntype, int num_params, ...)
218 va_start(va, num_params);
219 abc_method_body_t* c = add_method(cls->file, cls, returntype, num_params, va);
221 cls->static_constructor = c->method;
225 trait_t*trait_new(int type, multiname_t*name, int data1, int data2, constant_t*v)
227 trait_t*trait = malloc(sizeof(trait_t));
228 memset(trait, 0, sizeof(trait_t));
229 trait->kind = type&0x0f;
230 trait->attributes = type&0xf0;
232 trait->data1 = data1;
233 trait->data2 = data2;
237 trait_t*trait_new_member(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;
248 trait_t*trait_new_method(multiname_t*name, abc_method_t*m)
250 int type = TRAIT_METHOD;
251 trait_t*trait = malloc(sizeof(trait_t));
252 memset(trait, 0, sizeof(trait_t));
253 trait->kind = type&0x0f;
254 trait->attributes = type&0xf0;
260 abc_method_body_t* abc_class_method(abc_class_t*cls, multiname_t*returntype, char*name, int num_params, ...)
262 abc_file_t*file = cls->file;
264 va_start(va, num_params);
265 abc_method_body_t* c = add_method(cls->file, cls, returntype, num_params, va);
267 list_append(cls->traits, trait_new_method(multiname_fromstring(name), c->method));
271 void abc_AddSlot(abc_class_t*cls, char*name, int slot, char*type)
273 abc_file_t*file = cls->file;
274 multiname_t*m_name = multiname_fromstring(name);
275 multiname_t*m_type = multiname_fromstring(type);
276 trait_t*t = trait_new_member(m_type, m_name, 0);
277 t->slot_id = list_length(cls->traits);
278 list_append(cls->traits, t);
281 void abc_method_body_addClassTrait(abc_method_body_t*code, char*multiname, int slotid, abc_class_t*cls)
283 abc_file_t*file = code->file;
284 multiname_t*m = multiname_fromstring(multiname);
285 trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0);
287 list_append(code->traits, trait);
290 /* notice: traits of a method (body) belonging to an init script
291 and traits of the init script are *not* the same thing */
292 int abc_initscript_addClassTrait(abc_script_t*script, multiname_t*multiname, abc_class_t*cls)
294 abc_file_t*file = script->file;
295 multiname_t*m = multiname_clone(multiname);
296 int slotid = list_length(script->traits)+1;
297 trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0);
299 list_append(script->traits, trait);
303 abc_script_t* abc_initscript(abc_file_t*file, multiname_t*returntype, int num_params, ...)
306 va_start(va, num_params);
307 abc_method_body_t* c = add_method(file, 0, returntype, num_params, va);
308 abc_script_t* s = malloc(sizeof(abc_script_t));
309 s->method = c->method;
310 s->traits = list_new();
312 array_append(file->scripts, NO_KEY, s);
317 static void traits_dump(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file);
319 static void dump_method(FILE*fo, const char*prefix, const char*type, const char*name, abc_method_t*m, abc_file_t*file)
321 char*return_type = 0;
323 return_type = multiname_tostring(m->return_type);
325 return_type = strdup("void");
326 char*paramstr = params_tostring(m->parameters);
327 fprintf(fo, "%s%s %s %s=%s %s (%d params)\n", prefix, type, return_type, name, m->name, paramstr, list_length(m->parameters));
328 free(paramstr);paramstr=0;
329 free(return_type);return_type=0;
331 abc_method_body_t*c = m->body;
336 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);
339 sprintf(prefix2, "%s ", prefix);
341 traits_dump(fo, prefix, c->traits, file);
342 fprintf(fo, "%s{\n", prefix);
343 code_dump(c->code, c->exceptions, file, prefix2, fo);
344 fprintf(fo, "%s}\n\n", prefix);
347 static void traits_free(trait_list_t*traits)
349 trait_list_t*t = traits;
352 multiname_destroy(t->trait->name);t->trait->name = 0;
354 if(t->trait->kind == TRAIT_SLOT || t->trait->kind == TRAIT_CONST) {
355 multiname_destroy(t->trait->type_name);
357 if(t->trait->value) {
358 constant_free(t->trait->value);t->trait->value = 0;
360 free(t->trait);t->trait = 0;
366 static trait_list_t* traits_parse(TAG*tag, pool_t*pool, abc_file_t*file)
368 int num_traits = swf_GetU30(tag);
369 trait_list_t*traits = list_new();
372 DEBUG printf("%d traits\n", num_traits);
375 for(t=0;t<num_traits;t++) {
377 list_append(traits, trait);
379 trait->name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag))); // always a QName (ns,name)
382 DEBUG name = multiname_tostring(trait->name);
383 U8 kind = swf_GetU8(tag);
384 U8 attributes = kind&0xf0;
387 trait->attributes = attributes;
388 DEBUG printf(" trait %d) %s type=%02x\n", t, name, kind);
389 if(kind == TRAIT_METHOD || kind == TRAIT_GETTER || kind == TRAIT_SETTER) { // method / getter / setter
390 trait->disp_id = swf_GetU30(tag);
391 trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
392 DEBUG printf(" method/getter/setter\n");
393 } else if(kind == TRAIT_FUNCTION) { // function
394 trait->slot_id = swf_GetU30(tag);
395 trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
396 } else if(kind == TRAIT_CLASS) { // class
397 trait->slot_id = swf_GetU30(tag);
398 trait->cls = (abc_class_t*)array_getvalue(file->classes, swf_GetU30(tag));
399 DEBUG printf(" class %s %d %d\n", name, trait->slot_id, trait->cls);
400 } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
401 /* a slot is a variable in a class that is shared amonst all instances
402 of the same type, but which has a unique location in each object
403 (in other words, slots are non-static, traits are static)
405 trait->slot_id = swf_GetU30(tag);
406 trait->type_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
407 int vindex = swf_GetU30(tag);
409 int vkind = swf_GetU8(tag);
410 trait->value = constant_fromindex(pool, vindex, vkind);
412 DEBUG printf(" slot %s %d %s (%s)\n", name, trait->slot_id, trait->type_name->name, constant_tostring(trait->value));
414 fprintf(stderr, "Can't parse trait type %d\n", kind);
416 if(attributes&0x40) {
417 int num = swf_GetU30(tag);
420 swf_GetU30(tag); //index into metadata array
427 void traits_skip(TAG*tag)
429 int num_traits = swf_GetU30(tag);
431 for(t=0;t<num_traits;t++) {
433 U8 kind = swf_GetU8(tag);
434 U8 attributes = kind&0xf0;
438 if(kind == TRAIT_SLOT || kind == TRAIT_CONST) {
439 if(swf_GetU30(tag)) swf_GetU8(tag);
440 } else if(kind>TRAIT_CONST) {
441 fprintf(stderr, "Can't parse trait type %d\n", kind);
443 if(attributes&0x40) {
444 int s, num = swf_GetU30(tag);
445 for(s=0;s<num;s++) swf_GetU30(tag);
451 static void traits_write(pool_t*pool, TAG*tag, trait_list_t*traits)
457 swf_SetU30(tag, list_length(traits));
461 trait_t*trait = traits->trait;
463 swf_SetU30(tag, pool_register_multiname(pool, trait->name));
464 swf_SetU8(tag, trait->kind|trait->attributes);
466 swf_SetU30(tag, trait->data1);
468 if(trait->kind == TRAIT_CLASS) {
469 swf_SetU30(tag, trait->cls->index);
470 } else if(trait->kind == TRAIT_GETTER ||
471 trait->kind == TRAIT_SETTER ||
472 trait->kind == TRAIT_METHOD) {
473 swf_SetU30(tag, trait->method->index);
474 } else if(trait->kind == TRAIT_SLOT ||
475 trait->kind == TRAIT_CONST) {
476 int index = pool_register_multiname(pool, trait->type_name);
477 swf_SetU30(tag, index);
479 swf_SetU30(tag, trait->data2);
482 if(trait->kind == TRAIT_SLOT || trait->kind == TRAIT_CONST) {
483 int vindex = constant_get_index(pool, trait->value);
484 swf_SetU30(tag, vindex);
486 swf_SetU8(tag, trait->value->type);
489 if(trait->attributes&0x40) {
493 traits = traits->next;
498 static void traits_dump(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file)
502 trait_t*trait = traits->trait;
503 char*name = multiname_tostring(trait->name);
504 U8 kind = trait->kind;
505 U8 attributes = trait->attributes;
506 if(kind == TRAIT_METHOD) {
507 abc_method_t*m = trait->method;
508 dump_method(fo, prefix, "method", name, m, file);
509 } else if(kind == TRAIT_GETTER) {
510 abc_method_t*m = trait->method;
511 dump_method(fo, prefix, "getter", name, m, file);
512 } else if(kind == TRAIT_SETTER) {
513 abc_method_t*m = trait->method;
514 dump_method(fo, prefix, "setter", name, m, file);
515 } else if(kind == TRAIT_FUNCTION) { // function
516 abc_method_t*m = trait->method;
517 dump_method(fo, prefix, "function", name, m, file);
518 } else if(kind == TRAIT_CLASS) { // class
519 abc_class_t*cls = trait->cls;
521 fprintf(fo, "%sslot %d: class %s=00000000\n", prefix, trait->slot_id, name);
523 fprintf(fo, "%sslot %d: class %s=%s\n", prefix, trait->slot_id, name, cls->classname->name);
525 } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
526 int slot_id = trait->slot_id;
527 char*type_name = multiname_tostring(trait->type_name);
528 char*value = constant_tostring(trait->value);
529 fprintf(fo, "%sslot %d: %s%s %s %s %s\n", prefix, trait->slot_id,
530 kind==TRAIT_CONST?"const ":"", type_name, name,
531 value?"=":"", value);
532 if(value) free(value);
535 fprintf(fo, "%s can't dump trait type %d\n", prefix, kind);
542 void* swf_DumpABC(FILE*fo, void*code, char*prefix)
544 abc_file_t* file = (abc_file_t*)code;
547 fprintf(fo, "%s#\n", prefix);
548 fprintf(fo, "%s#name: %s\n", prefix, file->name);
549 fprintf(fo, "%s#\n", prefix);
553 for(t=0;t<file->metadata->num;t++) {
554 const char*entry_name = array_getkey(file->metadata, t);
555 fprintf(fo, "%s#Metadata \"%s\":\n", prefix, entry_name);
557 array_t*items = (array_t*)array_getvalue(file->metadata, t);
558 for(s=0;s<items->num;s++) {
559 fprintf(fo, "%s# %s=%s\n", prefix, array_getkey(items, s), array_getvalue(items,s));
561 fprintf(fo, "%s#\n", prefix);
564 for(t=0;t<file->classes->num;t++) {
565 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
567 sprintf(prefix2, "%s ", prefix);
569 fprintf(fo, "%s", prefix);
570 if(cls->flags&1) fprintf(fo, "sealed ");
571 if(cls->flags&2) fprintf(fo, "final ");
572 if(cls->flags&4) fprintf(fo, "interface ");
574 char*s = namespace_tostring(cls->protectedNS);
575 fprintf(fo, "protectedNS(%s) ", s);
579 char*classname = multiname_tostring(cls->classname);
580 fprintf(fo, "class %s", classname);
582 if(cls->superclass) {
583 char*supername = multiname_tostring(cls->superclass);
584 fprintf(fo, " extends %s", supername);
586 multiname_list_t*ilist = cls->interfaces;
588 fprintf(fo, " implements");
590 char*s = multiname_tostring(ilist->multiname);
591 fprintf(fo, " %s", s);
598 fprintf(fo, "extra flags=%02x\n", cls->flags&0xf0);
599 fprintf(fo, "%s{\n", prefix);
601 if(cls->static_constructor)
602 dump_method(fo, prefix2,"staticconstructor", "", cls->static_constructor, file);
603 traits_dump(fo, prefix2, cls->static_constructor_traits, file);
605 char*n = multiname_tostring(cls->classname);
607 dump_method(fo, prefix2, "constructor", n, cls->constructor, file);
609 traits_dump(fo, prefix2,cls->traits, file);
610 fprintf(fo, "%s}\n", prefix);
612 fprintf(fo, "%s\n", prefix);
614 for(t=0;t<file->scripts->num;t++) {
615 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
616 dump_method(fo, prefix,"initmethod", "init", s->method, file);
617 traits_dump(fo, prefix, s->traits, file);
622 void* swf_ReadABC(TAG*tag)
624 abc_file_t* file = abc_file_new();
625 pool_t*pool = pool_new();
627 swf_SetTagPos(tag, 0);
629 if(tag->id == ST_DOABC) {
630 U32 abcflags = swf_GetU32(tag);
631 DEBUG printf("flags=%08x\n", abcflags);
632 char*name= swf_GetString(tag);
633 file->name = (name&&name[0])?strdup(name):0;
635 U32 version = swf_GetU32(tag);
636 if(version!=0x002e0010) {
637 fprintf(stderr, "Warning: unknown AVM2 version %08x\n", version);
640 pool_read(pool, tag);
642 int num_methods = swf_GetU30(tag);
643 DEBUG printf("%d methods\n", num_methods);
644 for(t=0;t<num_methods;t++) {
646 int param_count = swf_GetU30(tag);
647 int return_type_index = swf_GetU30(tag);
648 if(return_type_index)
649 m->return_type = multiname_clone(pool_lookup_multiname(pool, return_type_index));
654 for(s=0;s<param_count;s++) {
655 int type_index = swf_GetU30(tag);
657 /* type_index might be 0, which probably means "..." (varargs) */
658 multiname_t*param = type_index?multiname_clone(pool_lookup_multiname(pool, type_index)):0;
659 list_append(m->parameters, param);
662 int namenr = swf_GetU30(tag);
664 m->name = strdup(pool_lookup_string(pool, namenr));
666 m->name = strdup("");
668 m->flags = swf_GetU8(tag);
670 DEBUG printf("method %d) %s flags=%02x\n", t, params_tostring(m->parameters), m->flags);
673 /* TODO optional parameters */
674 m->optional_parameters = list_new();
675 int num = swf_GetU30(tag);
678 int vindex = swf_GetU30(tag);
679 U8 vkind = swf_GetU8(tag); // specifies index type for "val"
680 constant_t*c = constant_fromindex(pool, vindex, vkind);
681 list_append(m->optional_parameters, c);
685 /* debug information- not used by avm2 */
686 multiname_list_t*l = m->parameters;
688 char*name = pool_lookup_string(pool, swf_GetU30(tag));
692 array_append(file->methods, NO_KEY, m);
695 parse_metadata(tag, file, pool);
697 /* skip classes, and scripts for now, and do the real parsing later */
698 int num_classes = swf_GetU30(tag);
699 int classes_pos = tag->pos;
700 DEBUG printf("%d classes\n", num_classes);
701 for(t=0;t<num_classes;t++) {
702 abc_class_t*cls = malloc(sizeof(abc_class_t));
703 memset(cls, 0, sizeof(abc_class_t));
705 DEBUG printf("class %d\n", t);
706 swf_GetU30(tag); //classname
707 swf_GetU30(tag); //supername
709 array_append(file->classes, NO_KEY, cls);
711 cls->flags = swf_GetU8(tag);
713 swf_GetU30(tag); //protectedNS
715 int inum = swf_GetU30(tag); //interface count
717 for(s=0;s<inum;s++) {
718 int interface_index = swf_GetU30(tag);
719 multiname_t* m = multiname_clone(pool_lookup_multiname(pool, interface_index));
720 list_append(cls->interfaces, m);
721 DEBUG printf(" class %d interface: %s\n", t, m->name);
724 swf_GetU30(tag); //iinit
727 for(t=0;t<num_classes;t++) {
728 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
729 int cinit = swf_GetU30(tag);
730 cls->static_constructor = (abc_method_t*)array_getvalue(file->methods, cinit);
733 int num_scripts = swf_GetU30(tag);
734 DEBUG printf("%d scripts\n", num_scripts);
735 for(t=0;t<num_scripts;t++) {
736 int init = swf_GetU30(tag);
740 int num_method_bodies = swf_GetU30(tag);
741 DEBUG printf("%d method bodies\n", num_method_bodies);
742 for(t=0;t<num_method_bodies;t++) {
743 int methodnr = swf_GetU30(tag);
744 if(methodnr >= file->methods->num) {
745 printf("Invalid method number: %d\n", methodnr);
748 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, methodnr);
749 abc_method_body_t*c = malloc(sizeof(abc_method_body_t));
750 memset(c, 0, sizeof(abc_method_body_t));
751 c->max_stack = swf_GetU30(tag);
752 c->local_count = swf_GetU30(tag);
753 c->init_scope_depth = swf_GetU30(tag);
754 c->max_scope_depth = swf_GetU30(tag);
755 int code_length = swf_GetU30(tag);
760 int pos = tag->pos + code_length;
761 codelookup_t*codelookup = 0;
762 c->code = code_parse(tag, code_length, file, pool, &codelookup);
765 int exception_count = swf_GetU30(tag);
767 c->exceptions = list_new();
768 for(s=0;s<exception_count;s++) {
769 exception_t*e = malloc(sizeof(exception_t));
771 e->from = code_atposition(codelookup, swf_GetU30(tag));
772 e->to = code_atposition(codelookup, swf_GetU30(tag));
773 e->target = code_atposition(codelookup, swf_GetU30(tag));
775 e->exc_type = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
776 e->var_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
777 //e->var_name = pool_lookup_string(pool, swf_GetU30(tag));
778 //if(e->var_name) e->var_name = strdup(e->var_name);
779 list_append(c->exceptions, e);
781 codelookup_free(codelookup);
782 c->traits = traits_parse(tag, pool, file);
784 DEBUG printf("method_body %d) (method %d), %d bytes of code", t, methodnr, code_length);
786 array_append(file->method_bodies, NO_KEY, c);
788 if(tag->len - tag->pos) {
789 fprintf(stderr, "%d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
793 swf_SetTagPos(tag, classes_pos);
794 for(t=0;t<num_classes;t++) {
795 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
797 int classname_index = swf_GetU30(tag);
798 int superclass_index = swf_GetU30(tag);
799 cls->classname = multiname_clone(pool_lookup_multiname(pool, classname_index));
800 cls->superclass = multiname_clone(pool_lookup_multiname(pool, superclass_index));
801 cls->flags = swf_GetU8(tag);
804 int ns_index = swf_GetU30(tag);
805 cls->protectedNS = namespace_clone(pool_lookup_namespace(pool, ns_index));
808 int num_interfaces = swf_GetU30(tag); //interface count
810 for(s=0;s<num_interfaces;s++) {
811 swf_GetU30(tag); // multiname index TODO
813 int iinit = swf_GetU30(tag);
814 cls->constructor = (abc_method_t*)array_getvalue(file->methods, iinit);
815 cls->traits = traits_parse(tag, pool, file);
817 for(t=0;t<num_classes;t++) {
818 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
820 swf_GetU30(tag); // cindex
821 cls->static_constructor_traits = traits_parse(tag, pool, file);
823 int num_scripts2 = swf_GetU30(tag);
824 for(t=0;t<num_scripts2;t++) {
825 int init = swf_GetU30(tag);
826 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, init);
828 abc_script_t*s = malloc(sizeof(abc_script_t));
829 memset(s, 0, sizeof(abc_script_t));
831 s->traits = traits_parse(tag, pool, file);
832 array_append(file->scripts, NO_KEY, s);
834 fprintf(stderr, "Can't parse script traits\n");
843 void swf_WriteABC(TAG*abctag, void*code)
845 abc_file_t*file = (abc_file_t*)code;
846 pool_t*pool = pool_new();
848 TAG*tmp = swf_InsertTag(0,0);
852 char need_null_method=0;
853 for(t=0;t<file->classes->num;t++) {
854 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
855 if(!c->constructor || !c->static_constructor) {
861 abc_method_t*nullmethod = 0;
862 if(need_null_method) {
863 nullmethod = malloc(sizeof(abc_method_t));
864 memset(nullmethod, 0, sizeof(abc_method_t));
865 /*TODO: might be more efficient to have this at the beginning */
866 array_append(file->methods, NO_KEY, nullmethod);
869 swf_SetU30(tag, file->methods->num);
870 /* enumerate classes, methods and method bodies */
871 for(t=0;t<file->methods->num;t++) {
872 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
875 for(t=0;t<file->classes->num;t++) {
876 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
879 for(t=0;t<file->method_bodies->num;t++) {
880 abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
884 /* generate code statistics */
885 for(t=0;t<file->method_bodies->num;t++) {
886 abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
887 m->stats = code_get_statistics(m->code, m->exceptions);
890 for(t=0;t<file->methods->num;t++) {
891 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
893 multiname_list_t*l = m->parameters;
894 int num_params = list_length(m->parameters);
895 swf_SetU30(tag, num_params);
897 swf_SetU30(tag, pool_register_multiname(pool, m->return_type));
902 swf_SetU30(tag, pool_register_multiname(pool, l->multiname));
906 swf_SetU30(tag, pool_register_string(pool, m->name));
911 U8 flags = m->flags&(METHOD_NEED_REST|METHOD_NEED_ARGUMENTS);
912 if(m->optional_parameters)
913 flags |= METHOD_HAS_OPTIONAL;
915 flags |= m->body->stats->flags;
918 swf_SetU8(tag, flags);
919 if(flags&METHOD_HAS_OPTIONAL) {
920 swf_SetU30(tag, list_length(m->optional_parameters));
921 constant_list_t*l = m->optional_parameters;
923 swf_SetU30(tag, constant_get_index(pool, l->constant));
924 swf_SetU8(tag, l->constant->type);
931 swf_SetU30(tag, file->metadata->num);
932 for(t=0;t<file->metadata->num;t++) {
933 const char*entry_name = array_getkey(file->metadata, t);
934 swf_SetU30(tag, pool_register_string(pool, entry_name));
935 array_t*items = (array_t*)array_getvalue(file->metadata, t);
936 swf_SetU30(tag, items->num);
938 for(s=0;s<items->num;s++) {
939 int i1 = pool_register_string(pool, array_getkey(items, s));
940 int i2 = pool_register_string(pool, array_getvalue(items, s));
946 swf_SetU30(tag, file->classes->num);
947 for(t=0;t<file->classes->num;t++) {
948 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
950 int classname_index = pool_register_multiname(pool, c->classname);
951 int superclass_index = pool_register_multiname(pool, c->superclass);
953 swf_SetU30(tag, classname_index);
954 swf_SetU30(tag, superclass_index);
956 swf_SetU8(tag, c->flags); // flags
958 int ns_index = pool_register_namespace(pool, c->protectedNS);
959 swf_SetU30(tag, ns_index);
962 swf_SetU30(tag, list_length(c->interfaces));
963 multiname_list_t*interface= c->interfaces;
965 swf_SetU30(tag, pool_register_multiname(pool, interface->multiname));
966 interface = interface->next;
969 if(!c->constructor) {
970 swf_SetU30(tag, nullmethod->index);
972 swf_SetU30(tag, c->constructor->index);
974 traits_write(pool, tag, c->traits);
976 for(t=0;t<file->classes->num;t++) {
977 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
978 if(!c->static_constructor) {
979 swf_SetU30(tag, nullmethod->index);
981 swf_SetU30(tag, c->static_constructor->index);
983 traits_write(pool, tag, c->static_constructor_traits);
986 swf_SetU30(tag, file->scripts->num);
987 for(t=0;t<file->scripts->num;t++) {
988 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
989 swf_SetU30(tag, s->method->index); //!=t!
990 traits_write(pool, tag, s->traits);
993 swf_SetU30(tag, file->method_bodies->num);
994 for(t=0;t<file->method_bodies->num;t++) {
995 abc_method_body_t*c = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
996 abc_method_t*m = c->method;
997 swf_SetU30(tag, m->index);
999 //swf_SetU30(tag, c->max_stack);
1000 //swf_SetU30(tag, c->local_count);
1001 //swf_SetU30(tag, c->init_scope_depth);
1002 //swf_SetU30(tag, c->max_scope_depth);
1004 swf_SetU30(tag, c->stats->max_stack);
1005 if(list_length(c->method->parameters)+1 <= c->stats->local_count)
1006 swf_SetU30(tag, c->stats->local_count);
1008 swf_SetU30(tag, list_length(c->method->parameters)+1);
1009 swf_SetU30(tag, c->init_scope_depth);
1010 swf_SetU30(tag, c->stats->max_scope_depth+
1011 c->init_scope_depth);
1013 code_write(tag, c->code, pool, file);
1015 swf_SetU30(tag, list_length(c->exceptions));
1016 exception_list_t*l = c->exceptions;
1018 // warning: assumes "pos" in each code_t is up-to-date
1019 swf_SetU30(tag, l->exception->from->pos);
1020 swf_SetU30(tag, l->exception->to->pos);
1021 swf_SetU30(tag, l->exception->target->pos);
1022 swf_SetU30(tag, pool_register_multiname(pool, l->exception->exc_type));
1023 swf_SetU30(tag, pool_register_multiname(pool, l->exception->var_name));
1027 traits_write(pool, tag, c->traits);
1030 /* free temporary codestat data again. Notice: If we were to write this
1031 file multiple times, this can also be shifted to abc_file_free() */
1032 for(t=0;t<file->method_bodies->num;t++) {
1033 abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1034 codestats_free(m->stats);m->stats=0;
1037 // --- start to write real tag --
1041 if(tag->id == ST_DOABC) {
1042 swf_SetU32(tag, 1); // flags
1043 swf_SetString(tag, file->name);
1046 swf_SetU16(tag, 0x10); //version
1047 swf_SetU16(tag, 0x2e);
1049 pool_write(pool, tag);
1051 swf_SetBlock(tag, tmp->data, tmp->len);
1053 swf_DeleteTag(0, tmp);
1057 void abc_file_free(abc_file_t*file)
1060 for(t=0;t<file->metadata->num;t++) {
1061 array_t*items = (array_t*)array_getvalue(file->metadata, t);
1063 for(s=0;s<items->num;s++) {
1064 free(array_getvalue(items, s));
1068 array_free(file->metadata);
1070 for(t=0;t<file->methods->num;t++) {
1071 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1073 multiname_list_t*param = m->parameters;
1075 multiname_destroy(param->multiname);param->multiname=0;
1076 param = param->next;
1078 list_free(m->parameters);m->parameters=0;
1080 constant_list_t*opt = m->optional_parameters;
1082 constant_free(opt->constant);opt->constant=0;
1085 list_free(m->optional_parameters);m->optional_parameters=0;
1088 free((void*)m->name);m->name=0;
1090 if(m->return_type) {
1091 multiname_destroy(m->return_type);
1095 array_free(file->methods);
1097 for(t=0;t<file->classes->num;t++) {
1098 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1099 traits_free(cls->traits);cls->traits=0;
1100 traits_free(cls->static_constructor_traits);cls->static_constructor_traits=0;
1102 if(cls->classname) {
1103 multiname_destroy(cls->classname);
1105 if(cls->superclass) {
1106 multiname_destroy(cls->superclass);
1109 multiname_list_t*i = cls->interfaces;
1111 multiname_destroy(i->multiname);i->multiname=0;
1114 list_free(cls->interfaces);cls->interfaces=0;
1116 if(cls->protectedNS) {
1117 namespace_destroy(cls->protectedNS);
1121 array_free(file->classes);
1123 for(t=0;t<file->scripts->num;t++) {
1124 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1125 traits_free(s->traits);s->traits=0;
1128 array_free(file->scripts);
1130 for(t=0;t<file->method_bodies->num;t++) {
1131 abc_method_body_t*body = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1132 code_free(body->code);body->code=0;
1133 traits_free(body->traits);body->traits=0;
1135 exception_list_t*ee = body->exceptions;
1137 exception_t*e=ee->exception;ee->exception=0;
1138 e->from = e->to = e->target = 0;
1139 multiname_destroy(e->exc_type);e->exc_type=0;
1140 multiname_destroy(e->var_name);e->var_name=0;
1144 list_free(body->exceptions);body->exceptions=0;
1148 array_free(file->method_bodies);
1151 free((void*)file->name);file->name=0;
1157 void swf_FreeABC(void*code)
1159 abc_file_t*file= (abc_file_t*)code;
1160 abc_file_free(file);
1163 void swf_AddButtonLinks(SWF*swf, char stop_each_frame, char events)
1166 int has_buttons = 0;
1167 TAG*tag=swf->firstTag;
1169 if(tag->id == ST_SHOWFRAME)
1171 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2)
1176 abc_file_t*file = abc_file_new();
1177 abc_method_body_t*c = 0;
1179 abc_class_t*cls = abc_class_new2(file, "rfx::MainTimeline", "flash.display::MovieClip");
1180 abc_class_protectedNS(cls, "rfx:MainTimeline");
1182 TAG*abctag = swf_InsertTagBefore(swf, swf->firstTag, ST_DOABC);
1184 tag = swf_InsertTag(abctag, ST_SYMBOLCLASS);
1187 swf_SetString(tag, "rfx.MainTimeline");
1189 c = abc_class_staticconstructor(cls, 0, 0);
1192 c->init_scope_depth = 9;
1193 c->max_scope_depth = 10;
1199 c = abc_class_constructor(cls, 0, 0);
1202 c->init_scope_depth = 10;
1203 c->max_scope_depth = 11;
1205 debugfile(c, "constructor.as");
1211 __ constructsuper(c,0);
1213 __ getlex(c, "[package]flash.system::Security");
1214 __ pushstring(c, "*");
1215 __ callpropvoid(c, "[package]::allowDomain", 1);
1217 if(stop_each_frame || has_buttons) {
1219 tag = swf->firstTag;
1220 abc_method_body_t*f = 0; //frame script
1221 while(tag && tag->id!=ST_END) {
1223 char needs_framescript=0;
1224 char buttonname[80];
1225 char functionname[80];
1226 sprintf(framename, "[packageinternal]rfx::frame%d", frame);
1228 if(!f && (tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2 || stop_each_frame)) {
1229 /* make the contructor add a frame script */
1230 __ findpropstrict(c,"[package]::addFrameScript");
1231 __ pushbyte(c,frame);
1232 __ getlex(c,framename);
1233 __ callpropvoid(c,"[package]::addFrameScript",2);
1235 f = abc_class_method(cls, 0, framename, 0);
1238 f->init_scope_depth = 10;
1239 f->max_scope_depth = 11;
1240 __ debugfile(f, "framescript.as");
1246 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1247 U16 id = swf_GetDefineID(tag);
1248 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1249 __ getlex(f,buttonname);
1250 __ getlex(f,"flash.events::MouseEvent");
1251 __ getproperty(f, "::CLICK");
1252 sprintf(functionname, "::clickbutton%d", swf_GetDefineID(tag));
1253 __ getlex(f,functionname);
1254 __ callpropvoid(f, "::addEventListener" ,2);
1256 if(stop_each_frame) {
1257 __ findpropstrict(f, "[package]::stop");
1258 __ callpropvoid(f, "[package]::stop", 0);
1260 needs_framescript = 1;
1262 abc_method_body_t*h =
1263 abc_class_method(cls, 0, functionname, 1, "flash.events::MouseEvent");
1266 h->init_scope_depth = 10;
1267 h->max_scope_depth = 11;
1271 ActionTAG*oldaction = swf_ButtonGetAction(tag);
1272 if(oldaction && oldaction->op == ACTION__GOTOFRAME) {
1273 int framenr = GET16(oldaction->data);
1275 fprintf(stderr, "Warning: Couldn't translate jump to frame %d to flash 9 actionscript\n", framenr);
1278 __ findpropstrict(h,"[package]::gotoAndStop");
1279 __ pushbyte(h,framenr+1);
1280 __ callpropvoid(h,"[package]::gotoAndStop", 1);
1283 sprintf(framename, "frame%d", framenr);
1284 __ getlocal_0(h); //this
1285 __ findpropstrict(h, "[package]flash.events::TextEvent");
1286 __ pushstring(h, "link");
1289 __ pushstring(h, framename);
1290 __ constructprop(h,"[package]flash.events::TextEvent", 4);
1291 __ callpropvoid(h,"[package]::dispatchEvent", 1);
1293 } else if(oldaction && oldaction->op == ACTION__GETURL) {
1295 __ findpropstrict(h,"flash.net::navigateToURL");
1296 __ findpropstrict(h,"flash.net::URLRequest");
1297 // TODO: target _blank
1298 __ pushstring(h,oldaction->data); //url
1299 __ constructprop(h,"flash.net::URLRequest", 1);
1300 __ callpropvoid(h,"flash.net::navigateToURL", 1);
1302 __ getlocal_0(h); //this
1303 __ findpropstrict(h, "[package]flash.events::TextEvent");
1304 __ pushstring(h, "link");
1307 __ pushstring(h,oldaction->data); //url
1308 __ constructprop(h,"[package]flash.events::TextEvent", 4);
1309 __ callpropvoid(h,"[package]::dispatchEvent", 1);
1311 } else if(oldaction) {
1312 fprintf(stderr, "Warning: Couldn't translate button code of button %d to flash 9 abc action\n", id);
1315 swf_ActionFree(oldaction);
1317 if(tag->id == ST_SHOWFRAME) {
1332 tag = swf->firstTag;
1334 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1335 char buttonname[80];
1336 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1337 abc_AddSlot(cls, buttonname, 0, "flash.display::SimpleButton");
1343 abc_script_t*s = abc_initscript(file, 0, 0);
1344 c = s->method->body;
1347 c->init_scope_depth = 1;
1348 c->max_scope_depth = 9;
1352 __ getscopeobject(c, 0);
1353 __ getlex(c,"::Object");
1355 __ getlex(c,"flash.events::EventDispatcher");
1357 __ getlex(c,"flash.display::DisplayObject");
1359 __ getlex(c,"flash.display::InteractiveObject");
1361 __ getlex(c,"flash.display::DisplayObjectContainer");
1363 __ getlex(c,"flash.display::Sprite");
1365 __ getlex(c,"flash.display::MovieClip");
1367 __ getlex(c,"flash.display::MovieClip");
1376 __ initproperty(c,"rfx::MainTimeline");
1379 //abc_method_body_addClassTrait(c, "rfx:MainTimeline", 1, cls);
1380 multiname_t*classname = multiname_fromstring("rfx::MainTimeline");
1381 abc_initscript_addClassTrait(s, classname, cls);
1382 multiname_destroy(classname);
1384 swf_WriteABC(abctag, file);