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 char*return_type = 0;
314 return_type = multiname_to_string(m->return_type);
316 return_type = strdup("void");
317 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);
319 free(paramstr);paramstr=0;
320 free(return_type);return_type=0;
322 abc_method_body_t*c = m->body;
327 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));
330 sprintf(prefix2, "%s ", prefix);
332 dump_traits(fo, prefix, c->traits, file);
333 fprintf(fo, "%s{\n", prefix);
334 code_dump(c->code, c->exceptions, file, prefix2, fo);
335 fprintf(fo, "%s}\n\n", prefix);
338 static void traits_free(trait_list_t*traits)
340 trait_list_t*t = traits;
343 multiname_destroy(t->trait->name);t->trait->name = 0;
345 if(t->trait->kind == TRAIT_SLOT || t->trait->kind == TRAIT_CONST) {
346 multiname_destroy(t->trait->type_name);
348 free(t->trait);t->trait = 0;
354 static trait_list_t* traits_parse(TAG*tag, pool_t*pool, abc_file_t*file)
356 int num_traits = swf_GetU30(tag);
357 trait_list_t*traits = list_new();
360 DEBUG printf("%d traits\n", num_traits);
363 for(t=0;t<num_traits;t++) {
364 trait_t*trait = malloc(sizeof(trait_t));
365 memset(trait, 0, sizeof(trait_t));
366 list_append(traits, trait);
368 trait->name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag))); // always a QName (ns,name)
371 DEBUG name = multiname_to_string(trait->name);
372 U8 kind = swf_GetU8(tag);
373 U8 attributes = kind&0xf0;
376 trait->attributes = attributes;
377 DEBUG printf(" trait %d) %s type=%02x\n", t, name, kind);
378 if(kind == TRAIT_METHOD || kind == TRAIT_GETTER || kind == TRAIT_SETTER) { // method / getter / setter
379 trait->disp_id = swf_GetU30(tag);
380 trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
381 DEBUG printf(" method/getter/setter\n");
382 } else if(kind == TRAIT_FUNCTION) { // function
383 trait->slot_id = swf_GetU30(tag);
384 trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
385 } else if(kind == TRAIT_CLASS) { // class
386 trait->slot_id = swf_GetU30(tag);
387 trait->cls = (abc_class_t*)array_getvalue(file->classes, swf_GetU30(tag));
388 DEBUG printf(" class %s %d %d\n", name, trait->slot_id, trait->cls);
389 } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
390 /* a slot is a variable in a class that is shared amonst all instances
391 of the same type, but which has a unique location in each object
392 (in other words, slots are non-static, traits are static)
394 trait->slot_id = swf_GetU30(tag);
395 trait->type_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
396 trait->vindex = swf_GetU30(tag);
398 trait->vkind = swf_GetU8(tag);
400 DEBUG printf(" slot %s %d %s (vindex=%d)\n", name, trait->slot_id, trait->type_name->name, trait->vindex);
402 fprintf(stderr, "Can't parse trait type %d\n", kind);
404 if(attributes&0x40) {
405 int num = swf_GetU30(tag);
408 swf_GetU30(tag); //index into metadata array
415 void traits_skip(TAG*tag)
417 int num_traits = swf_GetU30(tag);
419 for(t=0;t<num_traits;t++) {
421 U8 kind = swf_GetU8(tag);
422 U8 attributes = kind&0xf0;
426 if(kind == TRAIT_SLOT || kind == TRAIT_CONST) {
427 if(swf_GetU30(tag)) swf_GetU8(tag);
428 } else if(kind>TRAIT_CONST) {
429 fprintf(stderr, "Can't parse trait type %d\n", kind);
431 if(attributes&0x40) {
432 int s, num = swf_GetU30(tag);
433 for(s=0;s<num;s++) swf_GetU30(tag);
439 static void traits_write(pool_t*pool, TAG*tag, trait_list_t*traits)
445 swf_SetU30(tag, list_length(traits));
449 trait_t*trait = traits->trait;
451 swf_SetU30(tag, pool_register_multiname(pool, trait->name));
452 swf_SetU8(tag, trait->kind|trait->attributes);
454 swf_SetU30(tag, trait->data1);
456 if(trait->kind == TRAIT_CLASS) {
457 swf_SetU30(tag, trait->cls->index);
458 } else if(trait->kind == TRAIT_GETTER ||
459 trait->kind == TRAIT_SETTER ||
460 trait->kind == TRAIT_METHOD) {
461 swf_SetU30(tag, trait->method->index);
462 } else if(trait->kind == TRAIT_SLOT ||
463 trait->kind == TRAIT_CONST) {
464 int index = pool_register_multiname(pool, trait->type_name);
465 swf_SetU30(tag, index);
467 swf_SetU30(tag, trait->data2);
470 if(trait->kind == TRAIT_SLOT || trait->kind == TRAIT_CONST) {
471 swf_SetU30(tag, trait->vindex);
473 swf_SetU8(tag, trait->vkind);
476 if(trait->attributes&0x40) {
480 traits = traits->next;
485 static void dump_traits(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file)
489 trait_t*trait = traits->trait;
490 char*name = multiname_to_string(trait->name);
491 U8 kind = trait->kind;
492 U8 attributes = trait->attributes;
493 if(kind == TRAIT_METHOD) {
494 abc_method_t*m = trait->method;
495 dump_method(fo, prefix, "method", name, m, file);
496 } else if(kind == TRAIT_GETTER) {
497 abc_method_t*m = trait->method;
498 dump_method(fo, prefix, "getter", name, m, file);
499 } else if(kind == TRAIT_SETTER) {
500 abc_method_t*m = trait->method;
501 dump_method(fo, prefix, "setter", name, m, file);
502 } else if(kind == TRAIT_FUNCTION) { // function
503 abc_method_t*m = trait->method;
504 dump_method(fo, prefix, "function", name, m, file);
505 } else if(kind == TRAIT_CLASS) { // class
506 abc_class_t*cls = trait->cls;
508 fprintf(fo, "%sslot %d: class %s=class%d\n", prefix, trait->slot_id, name);
510 fprintf(fo, "%sslot %d: class %s=%s\n", prefix, trait->slot_id, name, cls->classname->name);
512 } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
513 int slot_id = trait->slot_id;
514 char*type_name = multiname_to_string(trait->type_name);
515 fprintf(fo, "%sslot %s %d %s (vindex=%d)\n", prefix, name, trait->slot_id, type_name, trait->vindex);
518 fprintf(fo, "%s can't dump trait type %d\n", prefix, kind);
525 void* swf_DumpABC(FILE*fo, void*code, char*prefix)
527 abc_file_t* file = (abc_file_t*)code;
530 fprintf(fo, "%s#\n", prefix);
531 fprintf(fo, "%s#name: %s\n", prefix, file->name);
532 fprintf(fo, "%s#\n", prefix);
536 for(t=0;t<file->metadata->num;t++) {
537 const char*entry_name = array_getkey(file->metadata, t);
538 fprintf(fo, "%s#Metadata \"%s\":\n", prefix, entry_name);
540 array_t*items = (array_t*)array_getvalue(file->metadata, t);
541 for(s=0;s<items->num;s++) {
542 fprintf(fo, "%s# %s=%s\n", prefix, array_getkey(items, s), array_getvalue(items,s));
544 fprintf(fo, "%s#\n", prefix);
547 for(t=0;t<file->classes->num;t++) {
548 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
550 sprintf(prefix2, "%s ", prefix);
552 fprintf(fo, "%s", prefix);
553 if(cls->flags&1) fprintf(fo, "sealed ");
554 if(cls->flags&2) fprintf(fo, "final ");
555 if(cls->flags&4) fprintf(fo, "interface ");
557 char*s = namespace_to_string(cls->protectedNS);
558 fprintf(fo, "protectedNS(%s) ", s);
562 char*classname = multiname_to_string(cls->classname);
563 fprintf(fo, "class %s", classname);
565 if(cls->superclass) {
566 char*supername = multiname_to_string(cls->superclass);
567 fprintf(fo, " extends %s", supername);
569 multiname_list_t*ilist = cls->interfaces;
571 fprintf(fo, " implements");
573 char*s = multiname_to_string(ilist->multiname);
574 fprintf(fo, " %s", s);
581 fprintf(fo, "extra flags=%02x\n", cls->flags&0xf0);
582 fprintf(fo, "%s{\n", prefix);
584 if(cls->static_constructor)
585 dump_method(fo, prefix2,"staticconstructor", "", cls->static_constructor, file);
586 dump_traits(fo, prefix2, cls->static_constructor_traits, file);
588 char*n = multiname_to_string(cls->classname);
590 dump_method(fo, prefix2, "constructor", n, cls->constructor, file);
592 dump_traits(fo, prefix2,cls->traits, file);
593 fprintf(fo, "%s}\n", prefix);
595 fprintf(fo, "%s\n", prefix);
597 for(t=0;t<file->scripts->num;t++) {
598 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
599 dump_method(fo, prefix,"initmethod", "init", s->method, file);
600 dump_traits(fo, prefix, s->traits, file);
605 void* swf_ReadABC(TAG*tag)
607 abc_file_t* file = abc_file_new();
608 pool_t*pool = pool_new();
610 swf_SetTagPos(tag, 0);
612 if(tag->id == ST_DOABC) {
613 U32 abcflags = swf_GetU32(tag);
614 DEBUG printf("flags=%08x\n", abcflags);
615 char*name= swf_GetString(tag);
616 file->name = name?strdup(name):0;
618 U32 version = swf_GetU32(tag);
619 if(version!=0x002e0010) {
620 fprintf(stderr, "Warning: unknown AVM2 version %08x\n", version);
623 pool_read(pool, tag);
625 int num_methods = swf_GetU30(tag);
626 DEBUG printf("%d methods\n", num_methods);
627 for(t=0;t<num_methods;t++) {
629 int param_count = swf_GetU30(tag);
630 int return_type_index = swf_GetU30(tag);
631 if(return_type_index)
632 m->return_type = multiname_clone(pool_lookup_multiname(pool, return_type_index));
637 for(s=0;s<param_count;s++) {
638 int type_index = swf_GetU30(tag);
640 /* type_index might be 0, which probably means "..." (varargs) */
641 multiname_t*param = type_index?multiname_clone(pool_lookup_multiname(pool, type_index)):0;
642 list_append(m->parameters, param);
645 int namenr = swf_GetU30(tag);
647 m->name = strdup(pool_lookup_string(pool, namenr));
649 m->name = strdup("");
651 m->flags = swf_GetU8(tag);
653 DEBUG printf("method %d) %s flags=%02x\n", t, params_to_string(m->parameters), m->flags);
656 /* TODO optional parameters */
657 int num = swf_GetU30(tag);
660 int val = swf_GetU30(tag);
661 U8 kind = swf_GetU8(tag); // specifies index type for "val"
665 /* debug information- not used by avm2 */
666 multiname_list_t*l = m->parameters;
668 char*name = pool_lookup_string(pool, swf_GetU30(tag));
672 array_append(file->methods, NO_KEY, m);
675 parse_metadata(tag, file, pool);
677 /* skip classes, and scripts for now, and do the real parsing later */
678 int num_classes = swf_GetU30(tag);
679 int classes_pos = tag->pos;
680 DEBUG printf("%d classes\n", num_classes);
681 for(t=0;t<num_classes;t++) {
682 abc_class_t*cls = malloc(sizeof(abc_class_t));
683 memset(cls, 0, sizeof(abc_class_t));
685 DEBUG printf("class %d\n", t);
686 swf_GetU30(tag); //classname
687 swf_GetU30(tag); //supername
689 array_append(file->classes, NO_KEY, cls);
691 cls->flags = swf_GetU8(tag);
693 swf_GetU30(tag); //protectedNS
695 int inum = swf_GetU30(tag); //interface count
697 for(s=0;s<inum;s++) {
698 int interface_index = swf_GetU30(tag);
699 multiname_t* m = multiname_clone(pool_lookup_multiname(pool, interface_index));
700 list_append(cls->interfaces, m);
701 DEBUG printf(" class %d interface: %s\n", t, m->name);
704 swf_GetU30(tag); //iinit
707 for(t=0;t<num_classes;t++) {
708 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
709 int cinit = swf_GetU30(tag);
710 cls->static_constructor = (abc_method_t*)array_getvalue(file->methods, cinit);
713 int num_scripts = swf_GetU30(tag);
714 DEBUG printf("%d scripts\n", num_scripts);
715 for(t=0;t<num_scripts;t++) {
716 int init = swf_GetU30(tag);
720 int num_method_bodies = swf_GetU30(tag);
721 DEBUG printf("%d method bodies\n", num_method_bodies);
722 for(t=0;t<num_method_bodies;t++) {
723 int methodnr = swf_GetU30(tag);
724 if(methodnr >= file->methods->num) {
725 printf("Invalid method number: %d\n", methodnr);
728 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, methodnr);
729 abc_method_body_t*c = malloc(sizeof(abc_method_body_t));
730 memset(c, 0, sizeof(abc_method_body_t));
731 c->max_stack = swf_GetU30(tag);
732 c->local_count = swf_GetU30(tag);
733 c->init_scope_depth = swf_GetU30(tag);
734 c->max_scope_depth = swf_GetU30(tag);
735 int code_length = swf_GetU30(tag);
740 int pos = tag->pos + code_length;
741 codelookup_t*codelookup = 0;
742 c->code = code_parse(tag, code_length, file, pool, &codelookup);
745 int exception_count = swf_GetU30(tag);
747 c->exceptions = list_new();
748 for(s=0;s<exception_count;s++) {
749 exception_t*e = malloc(sizeof(exception_t));
751 e->from = code_atposition(codelookup, swf_GetU30(tag));
752 e->to = code_atposition(codelookup, swf_GetU30(tag));
753 e->target = code_atposition(codelookup, swf_GetU30(tag));
755 e->exc_type = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
756 e->var_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
757 //e->var_name = pool_lookup_string(pool, swf_GetU30(tag));
758 //if(e->var_name) e->var_name = strdup(e->var_name);
759 list_append(c->exceptions, e);
761 codelookup_free(codelookup);
762 c->traits = traits_parse(tag, pool, file);
764 DEBUG printf("method_body %d) (method %d), %d bytes of code", t, methodnr, code_length);
766 array_append(file->method_bodies, NO_KEY, c);
768 if(tag->len - tag->pos) {
769 fprintf(stderr, "%d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
773 swf_SetTagPos(tag, classes_pos);
774 for(t=0;t<num_classes;t++) {
775 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
777 int classname_index = swf_GetU30(tag);
778 int superclass_index = swf_GetU30(tag);
779 cls->classname = multiname_clone(pool_lookup_multiname(pool, classname_index));
780 cls->superclass = multiname_clone(pool_lookup_multiname(pool, superclass_index));
781 cls->flags = swf_GetU8(tag);
784 int ns_index = swf_GetU30(tag);
785 cls->protectedNS = namespace_clone(pool_lookup_namespace(pool, ns_index));
788 int num_interfaces = swf_GetU30(tag); //interface count
790 for(s=0;s<num_interfaces;s++) {
791 swf_GetU30(tag); // multiname index TODO
793 int iinit = swf_GetU30(tag);
794 cls->constructor = (abc_method_t*)array_getvalue(file->methods, iinit);
795 cls->traits = traits_parse(tag, pool, file);
797 for(t=0;t<num_classes;t++) {
798 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
800 swf_GetU30(tag); // cindex
801 cls->static_constructor_traits = traits_parse(tag, pool, file);
803 int num_scripts2 = swf_GetU30(tag);
804 for(t=0;t<num_scripts2;t++) {
805 int init = swf_GetU30(tag);
806 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, init);
808 abc_script_t*s = malloc(sizeof(abc_script_t));
809 memset(s, 0, sizeof(abc_script_t));
811 s->traits = traits_parse(tag, pool, file);
812 array_append(file->scripts, NO_KEY, s);
814 fprintf(stderr, "Can't parse script traits\n");
823 void swf_WriteABC(TAG*abctag, void*code)
825 abc_file_t*file = (abc_file_t*)code;
826 pool_t*pool = pool_new();
828 TAG*tmp = swf_InsertTag(0,0);
832 char need_null_method=0;
833 for(t=0;t<file->classes->num;t++) {
834 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
835 if(!c->constructor || !c->static_constructor) {
841 abc_method_t*nullmethod = 0;
842 if(need_null_method) {
843 nullmethod = malloc(sizeof(abc_method_t));
844 memset(nullmethod, 0, sizeof(abc_method_t));
845 /*TODO: might be more efficient to have this at the beginning */
846 array_append(file->methods, NO_KEY, nullmethod);
849 swf_SetU30(tag, file->methods->num);
850 /* enumerate classes, methods and method bodies */
851 for(t=0;t<file->methods->num;t++) {
852 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
855 for(t=0;t<file->classes->num;t++) {
856 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
859 for(t=0;t<file->method_bodies->num;t++) {
860 abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
862 exception_list_t*ee = m->exceptions;
864 exception_t*e=ee->exception;ee->exception=0;
865 e->from = e->to = e->target = 0;
866 multiname_destroy(e->exc_type);e->exc_type=0;
867 multiname_destroy(e->var_name);e->var_name=0;
871 list_free(m->exceptions);m->exceptions=0;
874 for(t=0;t<file->methods->num;t++) {
875 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
877 multiname_list_t*l = m->parameters;
878 int num_params = list_length(m->parameters);
879 swf_SetU30(tag, num_params);
881 swf_SetU30(tag, pool_register_multiname(pool, m->return_type));
886 swf_SetU30(tag, pool_register_multiname(pool, l->multiname));
890 swf_SetU30(tag, pool_register_string(pool, m->name));
895 swf_SetU8(tag, 0); //flags
899 swf_SetU30(tag, file->metadata->num);
900 for(t=0;t<file->metadata->num;t++) {
901 const char*entry_name = array_getkey(file->metadata, t);
902 swf_SetU30(tag, pool_register_string(pool, entry_name));
903 array_t*items = (array_t*)array_getvalue(file->metadata, t);
904 swf_SetU30(tag, items->num);
906 for(s=0;s<items->num;s++) {
907 int i1 = pool_register_string(pool, array_getkey(items, s));
908 int i2 = pool_register_string(pool, array_getvalue(items, s));
914 swf_SetU30(tag, file->classes->num);
915 for(t=0;t<file->classes->num;t++) {
916 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
918 int classname_index = pool_register_multiname(pool, c->classname);
919 int superclass_index = pool_register_multiname(pool, c->superclass);
921 swf_SetU30(tag, classname_index);
922 swf_SetU30(tag, superclass_index);
924 swf_SetU8(tag, c->flags); // flags
926 int ns_index = pool_register_namespace(pool, c->protectedNS);
927 swf_SetU30(tag, ns_index);
930 swf_SetU30(tag, list_length(c->interfaces));
931 multiname_list_t*interface= c->interfaces;
933 swf_SetU30(tag, pool_register_multiname(pool, interface->multiname));
934 interface = interface->next;
937 if(!c->constructor) {
938 swf_SetU30(tag, nullmethod->index);
940 swf_SetU30(tag, c->constructor->index);
942 traits_write(pool, tag, c->traits);
944 for(t=0;t<file->classes->num;t++) {
945 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
946 if(!c->static_constructor) {
947 swf_SetU30(tag, nullmethod->index);
949 swf_SetU30(tag, c->static_constructor->index);
951 traits_write(pool, tag, c->static_constructor_traits);
954 swf_SetU30(tag, file->scripts->num);
955 for(t=0;t<file->scripts->num;t++) {
956 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
957 swf_SetU30(tag, s->method->index); //!=t!
958 traits_write(pool, tag, s->traits);
961 swf_SetU30(tag, file->method_bodies->num);
962 for(t=0;t<file->method_bodies->num;t++) {
963 abc_method_body_t*c = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
964 abc_method_t*m = c->method;
965 swf_SetU30(tag, m->index);
966 swf_SetU30(tag, c->max_stack);
967 swf_SetU30(tag, c->local_count);
968 swf_SetU30(tag, c->init_scope_depth);
969 swf_SetU30(tag, c->max_scope_depth);
971 code_write(tag, c->code, pool, file);
973 swf_SetU30(tag, list_length(c->exceptions));
974 exception_list_t*l = c->exceptions;
976 // warning: assumes "pos" in each code_t is up-to-date
977 swf_SetU30(tag, l->exception->from->pos);
978 swf_SetU30(tag, l->exception->to->pos);
979 swf_SetU30(tag, l->exception->target->pos);
980 swf_SetU30(tag, pool_register_multiname(pool, l->exception->exc_type));
981 swf_SetU30(tag, pool_register_multiname(pool, l->exception->var_name));
985 traits_write(pool, tag, c->traits);
988 // --- start to write real tag --
992 if(tag->id == ST_DOABC) {
993 swf_SetU32(tag, 1); // flags
994 swf_SetString(tag, file->name);
997 swf_SetU16(tag, 0x10); //version
998 swf_SetU16(tag, 0x2e);
1000 pool_write(pool, tag);
1002 swf_SetBlock(tag, tmp->data, tmp->len);
1004 swf_DeleteTag(0, tmp);
1008 void abc_file_free(abc_file_t*file)
1012 for(t=0;t<file->metadata->num;t++) {
1013 array_t*items = (array_t*)array_getvalue(file->metadata, t);
1015 for(s=0;s<items->num;s++) {
1016 free(array_getvalue(items, s));
1020 array_free(file->metadata);
1022 for(t=0;t<file->methods->num;t++) {
1023 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1025 multiname_list_t*param = m->parameters;
1027 multiname_destroy(param->multiname);param->multiname=0;
1028 param = param->next;
1030 list_free(m->parameters);m->parameters=0;
1033 free((void*)m->name);m->name=0;
1035 if(m->return_type) {
1036 multiname_destroy(m->return_type);
1040 array_free(file->methods);
1042 for(t=0;t<file->classes->num;t++) {
1043 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1044 traits_free(cls->traits);cls->traits=0;
1045 traits_free(cls->static_constructor_traits);cls->static_constructor_traits=0;
1047 if(cls->classname) {
1048 multiname_destroy(cls->classname);
1050 if(cls->superclass) {
1051 multiname_destroy(cls->superclass);
1054 multiname_list_t*i = cls->interfaces;
1056 multiname_destroy(i->multiname);i->multiname=0;
1059 list_free(cls->interfaces);cls->interfaces=0;
1061 if(cls->protectedNS) {
1062 namespace_destroy(cls->protectedNS);
1066 array_free(file->classes);
1068 for(t=0;t<file->scripts->num;t++) {
1069 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1070 traits_free(s->traits);s->traits=0;
1073 array_free(file->scripts);
1075 for(t=0;t<file->method_bodies->num;t++) {
1076 abc_method_body_t*body = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1077 code_free(body->code);body->code=0;
1078 traits_free(body->traits);body->traits=0;
1081 array_free(file->method_bodies);
1084 free((void*)file->name);file->name=0;
1090 void swf_FreeABC(void*code)
1092 abc_file_t*file= (abc_file_t*)code;
1093 abc_file_free(file);
1096 void swf_AddButtonLinks(SWF*swf, char stop_each_frame, char events)
1099 int has_buttons = 0;
1100 TAG*tag=swf->firstTag;
1102 if(tag->id == ST_SHOWFRAME)
1104 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2)
1109 abc_file_t*file = abc_file_new();
1110 abc_method_body_t*c = 0;
1112 abc_class_t*cls = abc_class_new2(file, "rfx::MainTimeline", "flash.display::MovieClip");
1113 abc_class_protectedNS(cls, "rfx:MainTimeline");
1115 TAG*abctag = swf_InsertTagBefore(swf, swf->firstTag, ST_DOABC);
1117 tag = swf_InsertTag(abctag, ST_SYMBOLCLASS);
1120 swf_SetString(tag, "rfx.MainTimeline");
1122 c = abc_class_staticconstructor(cls, 0, 0);
1125 c->init_scope_depth = 9;
1126 c->max_scope_depth = 10;
1132 c = abc_class_constructor(cls, 0, 0);
1135 c->init_scope_depth = 10;
1136 c->max_scope_depth = 11;
1138 debugfile(c, "constructor.as");
1144 __ constructsuper(c,0);
1146 __ getlex(c, "[package]flash.system::Security");
1147 __ pushstring(c, "*");
1148 __ callpropvoid(c, "[package]::allowDomain", 1);
1150 if(stop_each_frame || has_buttons) {
1152 tag = swf->firstTag;
1153 abc_method_body_t*f = 0; //frame script
1154 while(tag && tag->id!=ST_END) {
1156 char needs_framescript=0;
1157 char buttonname[80];
1158 char functionname[80];
1159 sprintf(framename, "[packageinternal]rfx::frame%d", frame);
1161 if(!f && (tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2 || stop_each_frame)) {
1162 /* make the contructor add a frame script */
1163 __ findpropstrict(c,"[package]::addFrameScript");
1164 __ pushbyte(c,frame);
1165 __ getlex(c,framename);
1166 __ callpropvoid(c,"[package]::addFrameScript",2);
1168 f = abc_class_method(cls, 0, framename, 0);
1171 f->init_scope_depth = 10;
1172 f->max_scope_depth = 11;
1173 __ debugfile(f, "framescript.as");
1179 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1180 U16 id = swf_GetDefineID(tag);
1181 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1182 __ getlex(f,buttonname);
1183 __ getlex(f,"flash.events::MouseEvent");
1184 __ getproperty(f, "::CLICK");
1185 sprintf(functionname, "::clickbutton%d", swf_GetDefineID(tag));
1186 __ getlex(f,functionname);
1187 __ callpropvoid(f, "::addEventListener" ,2);
1189 if(stop_each_frame) {
1190 __ findpropstrict(f, "[package]::stop");
1191 __ callpropvoid(f, "[package]::stop", 0);
1193 needs_framescript = 1;
1195 abc_method_body_t*h =
1196 abc_class_method(cls, "::void", functionname, 1, "flash.events::MouseEvent");
1199 h->init_scope_depth = 10;
1200 h->max_scope_depth = 11;
1204 ActionTAG*oldaction = swf_ButtonGetAction(tag);
1205 if(oldaction && oldaction->op == ACTION__GOTOFRAME) {
1206 int framenr = GET16(oldaction->data);
1208 fprintf(stderr, "Warning: Couldn't translate jump to frame %d to flash 9 actionscript\n", framenr);
1211 __ findpropstrict(h,"[package]::gotoAndStop");
1212 __ pushbyte(h,framenr+1);
1213 __ callpropvoid(h,"[package]::gotoAndStop", 1);
1216 sprintf(framename, "frame%d", framenr);
1217 __ getlocal_0(h); //this
1218 __ findpropstrict(h, "[package]flash.events::TextEvent");
1219 __ pushstring(h, "link");
1222 __ pushstring(h, framename);
1223 __ constructprop(h,"[package]flash.events::TextEvent", 4);
1224 __ callpropvoid(h,"[package]::dispatchEvent", 1);
1226 } else if(oldaction && oldaction->op == ACTION__GETURL) {
1228 __ findpropstrict(h,"flash.net::navigateToURL");
1229 __ findpropstrict(h,"flash.net::URLRequest");
1230 // TODO: target _blank
1231 __ pushstring(h,oldaction->data); //url
1232 __ constructprop(h,"flash.net::URLRequest", 1);
1233 __ callpropvoid(h,"flash.net::navigateToURL", 1);
1235 __ getlocal_0(h); //this
1236 __ findpropstrict(h, "[package]flash.events::TextEvent");
1237 __ pushstring(h, "link");
1240 __ pushstring(h,oldaction->data); //url
1241 __ constructprop(h,"[package]flash.events::TextEvent", 4);
1242 __ callpropvoid(h,"[package]::dispatchEvent", 1);
1244 } else if(oldaction) {
1245 fprintf(stderr, "Warning: Couldn't translate button code of button %d to flash 9 abc action\n", id);
1248 swf_ActionFree(oldaction);
1250 if(tag->id == ST_SHOWFRAME) {
1265 tag = swf->firstTag;
1267 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1268 char buttonname[80];
1269 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1270 abc_AddSlot(cls, buttonname, 0, "flash.display::SimpleButton");
1276 abc_script_t*s = abc_initscript(file, 0, 0);
1277 c = s->method->body;
1280 c->init_scope_depth = 1;
1281 c->max_scope_depth = 9;
1285 __ getscopeobject(c, 0);
1286 __ getlex(c,"::Object");
1288 __ getlex(c,"flash.events::EventDispatcher");
1290 __ getlex(c,"flash.display::DisplayObject");
1292 __ getlex(c,"flash.display::InteractiveObject");
1294 __ getlex(c,"flash.display::DisplayObjectContainer");
1296 __ getlex(c,"flash.display::Sprite");
1298 __ getlex(c,"flash.display::MovieClip");
1300 __ getlex(c,"flash.display::MovieClip");
1309 __ initproperty(c,"rfx::MainTimeline");
1312 //abc_method_body_addClassTrait(c, "rfx:MainTimeline", 1, cls);
1313 abc_initscript_addClassTrait(s, "rfx::MainTimeline", 1, cls);
1315 swf_WriteABC(abctag, file);