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)
54 char**names = (char**)malloc(sizeof(char*)*n);
60 names[n] = multiname_to_string(list->multiname);
61 size += strlen(names[n]) + 2;
65 char* params = malloc(size+5);
74 strcat(params, names[n]);
88 static void parse_metadata(TAG*tag, abc_file_t*file, pool_t*pool)
91 int num_metadata = swf_GetU30(tag);
93 DEBUG printf("%d metadata\n");
94 for(t=0;t<num_metadata;t++) {
95 const char*entry_name = pool_lookup_string(pool, swf_GetU30(tag));
96 int num = swf_GetU30(tag);
98 DEBUG printf(" %s\n", entry_name);
99 array_t*items = array_new();
101 int i1 = swf_GetU30(tag);
102 int i2 = swf_GetU30(tag);
103 char*key = i1?pool_lookup_string(pool, i1):"";
104 char*value = i2?pool_lookup_string(pool, i2):"";
105 DEBUG printf(" %s=%s\n", key, value);
106 array_append(items, key, strdup(value));
108 array_append(file->metadata, entry_name, items);
112 void swf_CopyData(TAG*to, TAG*from, int len)
114 unsigned char*data = malloc(len);
115 swf_GetBlock(from, data, len);
116 swf_SetBlock(to, data, len);
120 abc_file_t*abc_file_new()
122 abc_file_t*f = malloc(sizeof(abc_file_t));
123 memset(f, 0, sizeof(abc_file_t));
124 f->metadata = array_new();
126 f->methods = array_new();
127 f->classes = array_new();
128 f->scripts = array_new();
129 f->method_bodies = array_new();
134 #define CLASS_SEALED 1
135 #define CLASS_FINAL 2
136 #define CLASS_INTERFACE 4
137 #define CLASS_PROTECTED_NS 8
139 abc_class_t* abc_class_new(abc_file_t*pool, multiname_t*classname, multiname_t*superclass) {
142 array_append(pool->classes, NO_KEY, c);
145 c->classname = classname;
146 c->superclass = superclass;
149 c->static_constructor = 0;
150 c->traits = list_new();
153 abc_class_t* abc_class_new2(abc_file_t*pool, char*classname, char*superclass)
155 return abc_class_new(pool, multiname_fromstring(classname), multiname_fromstring(superclass));
158 void abc_class_sealed(abc_class_t*c)
160 c->flags |= CLASS_SEALED;
162 void abc_class_final(abc_class_t*c)
164 c->flags |= CLASS_FINAL;
166 void abc_class_interface(abc_class_t*c)
168 c->flags |= CLASS_INTERFACE;
170 void abc_class_protectedNS(abc_class_t*c, char*namespace)
172 c->protectedNS = namespace_new_protected(namespace);
173 c->flags |= CLASS_PROTECTED_NS;
175 void abc_class_add_interface(abc_class_t*c, multiname_t*interface)
177 list_append(c->interfaces, interface);
180 abc_method_body_t* add_method(abc_file_t*pool, abc_class_t*cls, char*returntype, int num_params, va_list va)
182 /* construct code (method body) object */
183 NEW(abc_method_body_t,c);
184 array_append(pool->method_bodies, NO_KEY, c);
186 c->traits = list_new();
189 /* construct method object */
191 array_append(pool->methods, NO_KEY, m);
193 if(returntype && strcmp(returntype, "void")) {
194 m->return_type = multiname_fromstring(returntype);
199 for(t=0;t<num_params;t++) {
200 const char*param = va_arg(va, const char*);
201 list_append(m->parameters, multiname_fromstring(param));
204 /* crosslink the two objects */
211 abc_method_body_t* abc_class_constructor(abc_class_t*cls, char*returntype, int num_params, ...)
214 va_start(va, num_params);
215 abc_method_body_t* c = add_method(cls->pool, cls, returntype, num_params, va);
217 cls->constructor = c->method;
221 abc_method_body_t* abc_class_staticconstructor(abc_class_t*cls, char*returntype, int num_params, ...)
224 va_start(va, num_params);
225 abc_method_body_t* c = add_method(cls->pool, cls, returntype, num_params, va);
227 cls->static_constructor = c->method;
231 trait_t*trait_new(int type, multiname_t*name, int data1, int data2, int vindex, int vkind)
233 trait_t*trait = malloc(sizeof(trait_t));
234 memset(trait, 0, sizeof(trait_t));
235 trait->kind = type&0x0f;
236 trait->attributes = type&0xf0;
238 trait->data1 = data1;
239 trait->data2 = data2;
240 trait->vindex = vindex;
241 trait->vkind = vkind;
244 trait_t*trait_new_method(multiname_t*name, abc_method_t*m)
246 int type = TRAIT_METHOD;
247 trait_t*trait = malloc(sizeof(trait_t));
248 memset(trait, 0, sizeof(trait_t));
249 trait->kind = type&0x0f;
250 trait->attributes = type&0xf0;
256 abc_method_body_t* abc_class_method(abc_class_t*cls, char*returntype, char*name, int num_params, ...)
258 abc_file_t*pool = cls->pool;
260 va_start(va, num_params);
261 abc_method_body_t* c = add_method(cls->pool, cls, returntype, num_params, va);
263 list_append(cls->traits, trait_new_method(multiname_fromstring(name), c->method));
267 void abc_AddSlot(abc_class_t*cls, char*name, int slot, char*multiname)
269 abc_file_t*pool = cls->pool;
270 multiname_t*m = multiname_fromstring(multiname);
271 list_append(cls->traits, trait_new(TRAIT_SLOT, m, slot, 0, 0, 0));
274 void abc_method_body_addClassTrait(abc_method_body_t*code, char*multiname, int slotid, abc_class_t*cls)
276 abc_file_t*pool = code->pool;
277 multiname_t*m = multiname_fromstring(multiname);
278 trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0, 0);
280 list_append(code->traits, trait);
283 /* notice: traits of a method (body) belonging to an init script
284 and traits of the init script are *not* the same thing */
285 void abc_initscript_addClassTrait(abc_script_t*script, char*multiname, int slotid, abc_class_t*cls)
287 abc_file_t*pool = script->pool;
288 multiname_t*m = multiname_fromstring(multiname);
289 trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0, 0);
291 list_append(script->traits, trait);
294 abc_script_t* abc_initscript(abc_file_t*pool, char*returntype, int num_params, ...)
297 va_start(va, num_params);
298 abc_method_body_t* c = add_method(pool, 0, returntype, num_params, va);
299 abc_script_t* s = malloc(sizeof(abc_script_t));
300 s->method = c->method;
301 s->traits = list_new();
303 array_append(pool->scripts, NO_KEY, s);
308 static void dump_traits(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file);
310 static void dump_method(FILE*fo, const char*prefix, const char*type, const char*name, abc_method_t*m, abc_file_t*file)
312 const char*return_type = "void";
314 return_type = multiname_to_string(m->return_type);
316 char*paramstr = params_to_string(m->parameters);
318 fprintf(fo, "%s%s %s %s=%s %s\n", prefix, type, return_type, name, m->name, paramstr);
320 abc_method_body_t*c = m->body;
325 fprintf(fo, "%s[%d %d %d %d %d]\n", prefix, c->max_stack, c->local_count, c->init_scope_depth, c->max_scope_depth, list_length(c->exceptions));
328 sprintf(prefix2, "%s ", prefix);
330 dump_traits(fo, prefix, c->traits, file);
331 fprintf(fo, "%s{\n", prefix);
332 code_dump(c->code, c->exceptions, file, prefix2, fo);
333 fprintf(fo, "%s}\n\n", prefix);
336 static void traits_free(trait_list_t*traits)
338 trait_list_t*t = traits;
341 multiname_destroy(t->trait->name);t->trait->name = 0;
343 if(t->trait->kind == TRAIT_SLOT || t->trait->kind == TRAIT_CONST) {
344 multiname_destroy(t->trait->type_name);
346 free(t->trait);t->trait = 0;
352 static trait_list_t* traits_parse(TAG*tag, pool_t*pool, abc_file_t*file)
354 int num_traits = swf_GetU30(tag);
355 trait_list_t*traits = list_new();
358 DEBUG printf("%d traits\n", num_traits);
361 for(t=0;t<num_traits;t++) {
362 trait_t*trait = malloc(sizeof(trait_t));
363 memset(trait, 0, sizeof(trait_t));
364 list_append(traits, trait);
366 trait->name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag))); // always a QName (ns,name)
369 DEBUG name = multiname_to_string(trait->name);
370 U8 kind = swf_GetU8(tag);
371 U8 attributes = kind&0xf0;
374 trait->attributes = attributes;
375 DEBUG printf(" trait %d) %s type=%02x\n", t, name, kind);
376 if(kind == TRAIT_METHOD || kind == TRAIT_GETTER || kind == TRAIT_SETTER) { // method / getter / setter
377 trait->disp_id = swf_GetU30(tag);
378 trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
379 DEBUG printf(" method/getter/setter\n");
380 } else if(kind == TRAIT_FUNCTION) { // function
381 trait->slot_id = swf_GetU30(tag);
382 trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
383 } else if(kind == TRAIT_CLASS) { // class
384 trait->slot_id = swf_GetU30(tag);
385 trait->cls = (abc_class_t*)array_getvalue(file->classes, swf_GetU30(tag));
386 DEBUG printf(" class %s %d %d\n", name, trait->slot_id, trait->cls);
387 } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
388 /* a slot is a variable in a class that is shared amonst all instances
389 of the same type, but which has a unique location in each object
390 (in other words, slots are non-static, traits are static)
392 trait->slot_id = swf_GetU30(tag);
393 trait->type_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
394 trait->vindex = swf_GetU30(tag);
396 trait->vkind = swf_GetU8(tag);
398 DEBUG printf(" slot %s %d %s (vindex=%d)\n", name, trait->slot_id, trait->type_name->name, trait->vindex);
400 fprintf(stderr, "Can't parse trait type %d\n", kind);
402 if(attributes&0x40) {
403 int num = swf_GetU30(tag);
406 swf_GetU30(tag); //index into metadata array
413 void traits_skip(TAG*tag)
415 int num_traits = swf_GetU30(tag);
417 for(t=0;t<num_traits;t++) {
419 U8 kind = swf_GetU8(tag);
420 U8 attributes = kind&0xf0;
424 if(kind == TRAIT_SLOT || kind == TRAIT_CONST) {
425 if(swf_GetU30(tag)) swf_GetU8(tag);
426 } else if(kind>TRAIT_CONST) {
427 fprintf(stderr, "Can't parse trait type %d\n", kind);
429 if(attributes&0x40) {
430 int s, num = swf_GetU30(tag);
431 for(s=0;s<num;s++) swf_GetU30(tag);
437 static void traits_write(pool_t*pool, TAG*tag, trait_list_t*traits)
443 swf_SetU30(tag, list_length(traits));
447 trait_t*trait = traits->trait;
449 swf_SetU30(tag, pool_register_multiname(pool, trait->name));
450 swf_SetU8(tag, trait->kind|trait->attributes);
452 swf_SetU30(tag, trait->data1);
454 if(trait->kind == TRAIT_CLASS) {
455 swf_SetU30(tag, trait->cls->index);
456 } else if(trait->kind == TRAIT_GETTER ||
457 trait->kind == TRAIT_SETTER ||
458 trait->kind == TRAIT_METHOD) {
459 swf_SetU30(tag, trait->method->index);
460 } else if(trait->kind == TRAIT_SLOT ||
461 trait->kind == TRAIT_CONST) {
462 int index = pool_register_multiname(pool, trait->type_name);
463 swf_SetU30(tag, index);
465 swf_SetU30(tag, trait->data2);
468 if(trait->kind == TRAIT_SLOT || trait->kind == TRAIT_CONST) {
469 swf_SetU30(tag, trait->vindex);
471 swf_SetU8(tag, trait->vkind);
474 if(trait->attributes&0x40) {
478 traits = traits->next;
483 static void dump_traits(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file)
487 trait_t*trait = traits->trait;
488 char*name = multiname_to_string(trait->name);
489 U8 kind = trait->kind;
490 U8 attributes = trait->attributes;
491 if(kind == TRAIT_METHOD) {
492 abc_method_t*m = trait->method;
493 dump_method(fo, prefix, "method", name, m, file);
494 } else if(kind == TRAIT_GETTER) {
495 abc_method_t*m = trait->method;
496 dump_method(fo, prefix, "getter", name, m, file);
497 } else if(kind == TRAIT_SETTER) {
498 abc_method_t*m = trait->method;
499 dump_method(fo, prefix, "setter", name, m, file);
500 } else if(kind == TRAIT_FUNCTION) { // function
501 abc_method_t*m = trait->method;
502 dump_method(fo, prefix, "function", name, m, file);
503 } else if(kind == TRAIT_CLASS) { // class
504 abc_class_t*cls = trait->cls;
506 fprintf(fo, "%sslot %d: class %s=class%d\n", prefix, trait->slot_id, name);
508 fprintf(fo, "%sslot %d: class %s=%s\n", prefix, trait->slot_id, name, cls->classname->name);
510 } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
511 int slot_id = trait->slot_id;
512 char*type_name = multiname_to_string(trait->type_name);
513 fprintf(fo, "%sslot %s %d %s (vindex=%d)\n", prefix, name, trait->slot_id, type_name, trait->vindex);
516 fprintf(fo, "%s can't dump trait type %d\n", prefix, kind);
523 void* swf_DumpABC(FILE*fo, void*code, char*prefix)
525 abc_file_t* file = (abc_file_t*)code;
528 fprintf(fo, "%s#\n", prefix);
529 fprintf(fo, "%s#name: %s\n", prefix, file->name);
530 fprintf(fo, "%s#\n", prefix);
534 for(t=0;t<file->metadata->num;t++) {
535 const char*entry_name = array_getkey(file->metadata, t);
536 fprintf(fo, "%s#Metadata \"%s\":\n", prefix, entry_name);
538 array_t*items = (array_t*)array_getvalue(file->metadata, t);
539 for(s=0;s<items->num;s++) {
540 fprintf(fo, "%s# %s=%s\n", prefix, array_getkey(items, s), array_getvalue(items,s));
542 fprintf(fo, "%s#\n", prefix);
545 for(t=0;t<file->classes->num;t++) {
546 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
548 sprintf(prefix2, "%s ", prefix);
550 fprintf(fo, "%s", prefix);
551 if(cls->flags&1) fprintf(fo, "sealed ");
552 if(cls->flags&2) fprintf(fo, "final ");
553 if(cls->flags&4) fprintf(fo, "interface ");
555 char*s = namespace_to_string(cls->protectedNS);
556 fprintf(fo, "protectedNS(%s) ", s);
560 char*classname = multiname_to_string(cls->classname);
561 fprintf(fo, "class %s", classname);
563 if(cls->superclass) {
564 char*supername = multiname_to_string(cls->superclass);
565 fprintf(fo, " extends %s", supername);
567 multiname_list_t*ilist = cls->interfaces;
569 fprintf(fo, " implements");
571 char*s = multiname_to_string(ilist->multiname);
572 fprintf(fo, " %s", s);
579 fprintf(fo, "extra flags=%02x\n", cls->flags&0xf0);
580 fprintf(fo, "%s{\n", prefix);
582 if(cls->static_constructor)
583 dump_method(fo, prefix2,"staticconstructor", "", cls->static_constructor, file);
584 dump_traits(fo, prefix2, cls->static_constructor_traits, file);
586 char*n = multiname_to_string(cls->classname);
588 dump_method(fo, prefix2, "constructor", n, cls->constructor, file);
590 dump_traits(fo, prefix2,cls->traits, file);
591 fprintf(fo, "%s}\n", prefix);
593 fprintf(fo, "%s\n", prefix);
595 for(t=0;t<file->scripts->num;t++) {
596 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
597 dump_method(fo, prefix,"initmethod", "init", s->method, file);
598 dump_traits(fo, prefix, s->traits, file);
603 void* swf_ReadABC(TAG*tag)
605 abc_file_t* file = abc_file_new();
606 pool_t*pool = pool_new();
608 swf_SetTagPos(tag, 0);
610 if(tag->id == ST_DOABC) {
611 U32 abcflags = swf_GetU32(tag);
612 DEBUG printf("flags=%08x\n", abcflags);
613 char*name= swf_GetString(tag);
614 file->name = name?strdup(name):0;
616 U32 version = swf_GetU32(tag);
617 if(version!=0x002e0010) {
618 fprintf(stderr, "Warning: unknown AVM2 version %08x\n", version);
621 pool_read(pool, tag);
623 int num_methods = swf_GetU30(tag);
624 DEBUG printf("%d methods\n", num_methods);
625 for(t=0;t<num_methods;t++) {
627 int param_count = swf_GetU30(tag);
628 int return_type_index = swf_GetU30(tag);
629 if(return_type_index)
630 m->return_type = multiname_clone(pool_lookup_multiname(pool, return_type_index));
635 for(s=0;s<param_count;s++) {
636 int type_index = swf_GetU30(tag);
638 /* type_index might be 0, which probably means "..." (varargs) */
639 multiname_t*param = type_index?multiname_clone(pool_lookup_multiname(pool, type_index)):0;
640 list_append(m->parameters, param);
643 int namenr = swf_GetU30(tag);
645 m->name = strdup(pool_lookup_string(pool, namenr));
647 m->name = strdup("");
649 m->flags = swf_GetU8(tag);
651 DEBUG printf("method %d) %s flags=%02x\n", t, params_to_string(m->parameters), m->flags);
654 /* optional parameters */
655 int num = swf_GetU30(tag);
658 int val = swf_GetU30(tag);
659 U8 kind = swf_GetU8(tag); // specifies index type for "val"
663 /* debug information- not used by avm2 */
664 multiname_list_t*l = m->parameters;
666 char*name = pool_lookup_string(pool, swf_GetU30(tag));
670 array_append(file->methods, NO_KEY, m);
673 parse_metadata(tag, file, pool);
675 /* skip classes, and scripts for now, and do the real parsing later */
676 int num_classes = swf_GetU30(tag);
677 int classes_pos = tag->pos;
678 DEBUG printf("%d classes\n", num_classes);
679 for(t=0;t<num_classes;t++) {
680 abc_class_t*cls = malloc(sizeof(abc_class_t));
681 memset(cls, 0, sizeof(abc_class_t));
683 DEBUG printf("class %d\n", t);
684 swf_GetU30(tag); //classname
685 swf_GetU30(tag); //supername
687 array_append(file->classes, NO_KEY, cls);
689 cls->flags = swf_GetU8(tag);
691 swf_GetU30(tag); //protectedNS
693 int inum = swf_GetU30(tag); //interface count
695 for(s=0;s<inum;s++) {
696 int interface_index = swf_GetU30(tag);
697 multiname_t* m = multiname_clone(pool_lookup_multiname(pool, interface_index));
698 list_append(cls->interfaces, m);
699 DEBUG printf(" class %d interface: %s\n", t, m->name);
702 swf_GetU30(tag); //iinit
705 for(t=0;t<num_classes;t++) {
706 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
707 int cinit = swf_GetU30(tag);
708 cls->static_constructor = (abc_method_t*)array_getvalue(file->methods, cinit);
711 int num_scripts = swf_GetU30(tag);
712 DEBUG printf("%d scripts\n", num_scripts);
713 for(t=0;t<num_scripts;t++) {
714 int init = swf_GetU30(tag);
718 int num_method_bodies = swf_GetU30(tag);
719 DEBUG printf("%d method bodies\n", num_method_bodies);
720 for(t=0;t<num_method_bodies;t++) {
721 int methodnr = swf_GetU30(tag);
722 if(methodnr >= file->methods->num) {
723 printf("Invalid method number: %d\n", methodnr);
726 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, methodnr);
727 abc_method_body_t*c = malloc(sizeof(abc_method_body_t));
728 memset(c, 0, sizeof(abc_method_body_t));
729 c->max_stack = swf_GetU30(tag);
730 c->local_count = swf_GetU30(tag);
731 c->init_scope_depth = swf_GetU30(tag);
732 c->max_scope_depth = swf_GetU30(tag);
733 int code_length = swf_GetU30(tag);
738 int pos = tag->pos + code_length;
739 codelookup_t*codelookup = 0;
740 c->code = code_parse(tag, code_length, file, pool, &codelookup);
743 int exception_count = swf_GetU30(tag);
745 c->exceptions = list_new();
746 for(s=0;s<exception_count;s++) {
747 exception_t*e = malloc(sizeof(exception_t));
749 e->from = code_atposition(codelookup, swf_GetU30(tag));
750 e->to = code_atposition(codelookup, swf_GetU30(tag));
751 e->target = code_atposition(codelookup, swf_GetU30(tag));
753 e->exc_type = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
754 e->var_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
755 //e->var_name = pool_lookup_string(pool, swf_GetU30(tag));
756 //if(e->var_name) e->var_name = strdup(e->var_name);
757 list_append(c->exceptions, e);
759 codelookup_free(codelookup);
760 c->traits = traits_parse(tag, pool, file);
762 DEBUG printf("method_body %d) (method %d), %d bytes of code", t, methodnr, code_length);
764 array_append(file->method_bodies, NO_KEY, c);
766 if(tag->len - tag->pos) {
767 fprintf(stderr, "%d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
771 swf_SetTagPos(tag, classes_pos);
772 for(t=0;t<num_classes;t++) {
773 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
775 int classname_index = swf_GetU30(tag);
776 int superclass_index = swf_GetU30(tag);
777 cls->classname = multiname_clone(pool_lookup_multiname(pool, classname_index));
778 cls->superclass = multiname_clone(pool_lookup_multiname(pool, superclass_index));
779 cls->flags = swf_GetU8(tag);
782 int ns_index = swf_GetU30(tag);
783 cls->protectedNS = namespace_clone(pool_lookup_namespace(pool, ns_index));
786 int num_interfaces = swf_GetU30(tag); //interface count
788 for(s=0;s<num_interfaces;s++) {
789 swf_GetU30(tag); // multiname index TODO
791 int iinit = swf_GetU30(tag);
792 cls->constructor = (abc_method_t*)array_getvalue(file->methods, iinit);
793 cls->traits = traits_parse(tag, pool, file);
795 for(t=0;t<num_classes;t++) {
796 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
798 swf_GetU30(tag); // cindex
799 cls->static_constructor_traits = traits_parse(tag, pool, file);
801 int num_scripts2 = swf_GetU30(tag);
802 for(t=0;t<num_scripts2;t++) {
803 int init = swf_GetU30(tag);
804 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, init);
806 abc_script_t*s = malloc(sizeof(abc_script_t));
807 memset(s, 0, sizeof(abc_script_t));
809 s->traits = traits_parse(tag, pool, file);
810 array_append(file->scripts, NO_KEY, s);
812 fprintf(stderr, "Can't parse script traits\n");
821 void swf_WriteABC(TAG*abctag, void*code)
823 abc_file_t*file = (abc_file_t*)code;
824 pool_t*pool = pool_new();
826 TAG*tmp = swf_InsertTag(0,0);
830 char need_null_method=0;
831 for(t=0;t<file->classes->num;t++) {
832 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
833 if(!c->constructor || !c->static_constructor) {
839 abc_method_t*nullmethod = 0;
840 if(need_null_method) {
841 nullmethod = malloc(sizeof(abc_method_t));
842 memset(nullmethod, 0, sizeof(abc_method_t));
843 /*TODO: might be more efficient to have this at the beginning */
844 array_append(file->methods, NO_KEY, nullmethod);
847 swf_SetU30(tag, file->methods->num);
848 /* enumerate classes, methods and method bodies */
849 for(t=0;t<file->methods->num;t++) {
850 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
853 for(t=0;t<file->classes->num;t++) {
854 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
857 for(t=0;t<file->method_bodies->num;t++) {
858 abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
860 exception_list_t*ee = m->exceptions;
862 exception_t*e=ee->exception;ee->exception=0;
863 e->from = e->to = e->target = 0;
864 multiname_destroy(e->exc_type);e->exc_type=0;
865 multiname_destroy(e->var_name);e->var_name=0;
869 list_free(m->exceptions);m->exceptions=0;
872 for(t=0;t<file->methods->num;t++) {
873 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
875 multiname_list_t*l = m->parameters;
876 int num_params = list_length(m->parameters);
877 swf_SetU30(tag, num_params);
879 swf_SetU30(tag, pool_register_multiname(pool, m->return_type));
884 swf_SetU30(tag, pool_register_multiname(pool, l->multiname));
888 swf_SetU30(tag, pool_register_string(pool, m->name));
893 swf_SetU8(tag, 0); //flags
897 swf_SetU30(tag, file->metadata->num);
898 for(t=0;t<file->metadata->num;t++) {
899 const char*entry_name = array_getkey(file->metadata, t);
900 swf_SetU30(tag, pool_register_string(pool, entry_name));
901 array_t*items = (array_t*)array_getvalue(file->metadata, t);
902 swf_SetU30(tag, items->num);
904 for(s=0;s<items->num;s++) {
905 int i1 = pool_register_string(pool, array_getkey(items, s));
906 int i2 = pool_register_string(pool, array_getvalue(items, s));
912 swf_SetU30(tag, file->classes->num);
913 for(t=0;t<file->classes->num;t++) {
914 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
916 int classname_index = pool_register_multiname(pool, c->classname);
917 int superclass_index = pool_register_multiname(pool, c->superclass);
919 swf_SetU30(tag, classname_index);
920 swf_SetU30(tag, superclass_index);
922 swf_SetU8(tag, c->flags); // flags
924 int ns_index = pool_register_namespace(pool, c->protectedNS);
925 swf_SetU30(tag, ns_index);
928 swf_SetU30(tag, list_length(c->interfaces));
929 multiname_list_t*interface= c->interfaces;
931 swf_SetU30(tag, pool_register_multiname(pool, interface->multiname));
932 interface = interface->next;
935 if(!c->constructor) {
936 swf_SetU30(tag, nullmethod->index);
938 swf_SetU30(tag, c->constructor->index);
940 traits_write(pool, tag, c->traits);
942 for(t=0;t<file->classes->num;t++) {
943 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
944 if(!c->static_constructor) {
945 swf_SetU30(tag, nullmethod->index);
947 swf_SetU30(tag, c->static_constructor->index);
949 traits_write(pool, tag, c->static_constructor_traits);
952 swf_SetU30(tag, file->scripts->num);
953 for(t=0;t<file->scripts->num;t++) {
954 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
955 swf_SetU30(tag, s->method->index); //!=t!
956 traits_write(pool, tag, s->traits);
959 swf_SetU30(tag, file->method_bodies->num);
960 for(t=0;t<file->method_bodies->num;t++) {
961 abc_method_body_t*c = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
962 abc_method_t*m = c->method;
963 swf_SetU30(tag, m->index);
964 swf_SetU30(tag, c->max_stack);
965 swf_SetU30(tag, c->local_count);
966 swf_SetU30(tag, c->init_scope_depth);
967 swf_SetU30(tag, c->max_scope_depth);
969 code_write(tag, c->code, pool, file);
971 swf_SetU30(tag, list_length(c->exceptions));
972 exception_list_t*l = c->exceptions;
974 // warning: assumes "pos" in each code_t is up-to-date
975 swf_SetU30(tag, l->exception->from->pos);
976 swf_SetU30(tag, l->exception->to->pos);
977 swf_SetU30(tag, l->exception->target->pos);
978 swf_SetU30(tag, pool_register_multiname(pool, l->exception->exc_type));
979 swf_SetU30(tag, pool_register_multiname(pool, l->exception->var_name));
983 traits_write(pool, tag, c->traits);
986 // --- start to write real tag --
990 if(tag->id == ST_DOABC) {
991 swf_SetU32(tag, 1); // flags
992 swf_SetString(tag, file->name);
995 swf_SetU16(tag, 0x10); //version
996 swf_SetU16(tag, 0x2e);
998 pool_write(pool, tag);
1000 swf_SetBlock(tag, tmp->data, tmp->len);
1002 swf_DeleteTag(0, tmp);
1006 void abc_file_free(abc_file_t*file)
1010 for(t=0;t<file->metadata->num;t++) {
1011 array_t*items = (array_t*)array_getvalue(file->metadata, t);
1013 for(s=0;s<items->num;s++) {
1014 free(array_getvalue(items, s));
1018 array_free(file->metadata);
1020 for(t=0;t<file->methods->num;t++) {
1021 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1023 multiname_list_t*param = m->parameters;
1025 multiname_destroy(param->multiname);param->multiname=0;
1026 param = param->next;
1028 list_free(m->parameters);m->parameters=0;
1031 free((void*)m->name);m->name=0;
1033 if(m->return_type) {
1034 multiname_destroy(m->return_type);
1038 array_free(file->methods);
1040 for(t=0;t<file->classes->num;t++) {
1041 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1042 traits_free(cls->traits);cls->traits=0;
1043 traits_free(cls->static_constructor_traits);cls->static_constructor_traits=0;
1045 if(cls->classname) {
1046 multiname_destroy(cls->classname);
1048 if(cls->superclass) {
1049 multiname_destroy(cls->superclass);
1052 multiname_list_t*i = cls->interfaces;
1054 multiname_destroy(i->multiname);i->multiname=0;
1057 list_free(cls->interfaces);cls->interfaces=0;
1059 if(cls->protectedNS) {
1060 namespace_destroy(cls->protectedNS);
1064 array_free(file->classes);
1066 for(t=0;t<file->scripts->num;t++) {
1067 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1068 traits_free(s->traits);s->traits=0;
1071 array_free(file->scripts);
1073 for(t=0;t<file->method_bodies->num;t++) {
1074 abc_method_body_t*body = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1075 code_free(body->code);body->code=0;
1076 traits_free(body->traits);body->traits=0;
1079 array_free(file->method_bodies);
1082 free((void*)file->name);file->name=0;
1088 void swf_FreeABC(void*code)
1090 abc_file_t*file= (abc_file_t*)code;
1091 abc_file_free(file);
1094 void swf_AddButtonLinks(SWF*swf, char stop_each_frame, char events)
1097 int has_buttons = 0;
1098 TAG*tag=swf->firstTag;
1100 if(tag->id == ST_SHOWFRAME)
1102 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2)
1107 abc_file_t*file = abc_file_new();
1108 abc_method_body_t*c = 0;
1110 abc_class_t*cls = abc_class_new2(file, "rfx::MainTimeline", "flash.display::MovieClip");
1111 abc_class_protectedNS(cls, "rfx:MainTimeline");
1113 TAG*abctag = swf_InsertTagBefore(swf, swf->firstTag, ST_DOABC);
1115 tag = swf_InsertTag(abctag, ST_SYMBOLCLASS);
1118 swf_SetString(tag, "rfx.MainTimeline");
1120 c = abc_class_staticconstructor(cls, 0, 0);
1123 c->init_scope_depth = 9;
1124 c->max_scope_depth = 10;
1130 c = abc_class_constructor(cls, 0, 0);
1133 c->init_scope_depth = 10;
1134 c->max_scope_depth = 11;
1136 debugfile(c, "constructor.as");
1142 __ constructsuper(c,0);
1144 __ getlex(c, "[package]flash.system::Security");
1145 __ pushstring(c, "*");
1146 __ callpropvoid(c, "[package]::allowDomain", 1);
1148 if(stop_each_frame || has_buttons) {
1150 tag = swf->firstTag;
1151 abc_method_body_t*f = 0; //frame script
1152 while(tag && tag->id!=ST_END) {
1154 char needs_framescript=0;
1155 char buttonname[80];
1156 char functionname[80];
1157 sprintf(framename, "[packageinternal]rfx::frame%d", frame);
1159 if(!f && (tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2 || stop_each_frame)) {
1160 /* make the contructor add a frame script */
1161 __ findpropstrict(c,"[package]::addFrameScript");
1162 __ pushbyte(c,frame);
1163 __ getlex(c,framename);
1164 __ callpropvoid(c,"[package]::addFrameScript",2);
1166 f = abc_class_method(cls, 0, framename, 0);
1169 f->init_scope_depth = 10;
1170 f->max_scope_depth = 11;
1171 __ debugfile(f, "framescript.as");
1177 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1178 U16 id = swf_GetDefineID(tag);
1179 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1180 __ getlex(f,buttonname);
1181 __ getlex(f,"flash.events::MouseEvent");
1182 __ getproperty(f, "::CLICK");
1183 sprintf(functionname, "::clickbutton%d", swf_GetDefineID(tag));
1184 __ getlex(f,functionname);
1185 __ callpropvoid(f, "::addEventListener" ,2);
1187 if(stop_each_frame) {
1188 __ findpropstrict(f, "[package]::stop");
1189 __ callpropvoid(f, "[package]::stop", 0);
1191 needs_framescript = 1;
1193 abc_method_body_t*h =
1194 abc_class_method(cls, "::void", functionname, 1, "flash.events::MouseEvent");
1197 h->init_scope_depth = 10;
1198 h->max_scope_depth = 11;
1202 ActionTAG*oldaction = swf_ButtonGetAction(tag);
1203 if(oldaction && oldaction->op == ACTION__GOTOFRAME) {
1204 int framenr = GET16(oldaction->data);
1206 fprintf(stderr, "Warning: Couldn't translate jump to frame %d to flash 9 actionscript\n", framenr);
1209 __ findpropstrict(h,"[package]::gotoAndStop");
1210 __ pushbyte(h,framenr+1);
1211 __ callpropvoid(h,"[package]::gotoAndStop", 1);
1214 sprintf(framename, "frame%d", framenr);
1215 __ getlocal_0(h); //this
1216 __ findpropstrict(h, "[package]flash.events::TextEvent");
1217 __ pushstring(h, "link");
1220 __ pushstring(h, framename);
1221 __ constructprop(h,"[package]flash.events::TextEvent", 4);
1222 __ callpropvoid(h,"[package]::dispatchEvent", 1);
1224 } else if(oldaction && oldaction->op == ACTION__GETURL) {
1226 __ findpropstrict(h,"flash.net::navigateToURL");
1227 __ findpropstrict(h,"flash.net::URLRequest");
1228 // TODO: target _blank
1229 __ pushstring(h,oldaction->data); //url
1230 __ constructprop(h,"flash.net::URLRequest", 1);
1231 __ callpropvoid(h,"flash.net::navigateToURL", 1);
1233 __ getlocal_0(h); //this
1234 __ findpropstrict(h, "[package]flash.events::TextEvent");
1235 __ pushstring(h, "link");
1238 __ pushstring(h,oldaction->data); //url
1239 __ constructprop(h,"[package]flash.events::TextEvent", 4);
1240 __ callpropvoid(h,"[package]::dispatchEvent", 1);
1242 } else if(oldaction) {
1243 fprintf(stderr, "Warning: Couldn't translate button code of button %d to flash 9 abc action\n", id);
1246 swf_ActionFree(oldaction);
1248 if(tag->id == ST_SHOWFRAME) {
1263 tag = swf->firstTag;
1265 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1266 char buttonname[80];
1267 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1268 abc_AddSlot(cls, buttonname, 0, "flash.display::SimpleButton");
1274 abc_script_t*s = abc_initscript(file, 0, 0);
1275 c = s->method->body;
1278 c->init_scope_depth = 1;
1279 c->max_scope_depth = 9;
1283 __ getscopeobject(c, 0);
1284 __ getlex(c,"::Object");
1286 __ getlex(c,"flash.events::EventDispatcher");
1288 __ getlex(c,"flash.display::DisplayObject");
1290 __ getlex(c,"flash.display::InteractiveObject");
1292 __ getlex(c,"flash.display::DisplayObjectContainer");
1294 __ getlex(c,"flash.display::Sprite");
1296 __ getlex(c,"flash.display::MovieClip");
1298 __ getlex(c,"flash.display::MovieClip");
1307 __ initproperty(c,"rfx::MainTimeline");
1310 //abc_method_body_addClassTrait(c, "rfx:MainTimeline", 1, cls);
1311 abc_initscript_addClassTrait(s, "rfx::MainTimeline", 1, cls);
1313 swf_WriteABC(abctag, file);