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, c->exception_count);
328 sprintf(prefix2, "%s ", prefix);
330 dump_traits(fo, prefix, c->traits, file);
331 fprintf(fo, "%s{\n", prefix);
332 code_dump(c->code, 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);
637 multiname_t*param = multiname_clone(pool_lookup_multiname(pool, type_index));
638 /* type_index might be 0, which probably means "..." (varargs) */
639 list_append(m->parameters, param);
642 int namenr = swf_GetU30(tag);
644 m->name = strdup(pool_lookup_string(pool, namenr));
646 m->name = strdup("");
648 m->flags = swf_GetU8(tag);
650 DEBUG printf("method %d) %s flags=%02x\n", t, params_to_string(m->parameters), m->flags);
653 /* optional parameters */
654 int num = swf_GetU30(tag);
657 int val = swf_GetU30(tag);
658 U8 kind = swf_GetU8(tag); // specifies index type for "val"
662 /* debug information- not used by avm2 */
663 multiname_list_t*l = m->parameters;
665 char*name = pool_lookup_string(pool, swf_GetU30(tag));
669 array_append(file->methods, NO_KEY, m);
672 parse_metadata(tag, file, pool);
674 /* skip classes, and scripts for now, and do the real parsing later */
675 int num_classes = swf_GetU30(tag);
676 int classes_pos = tag->pos;
677 DEBUG printf("%d classes\n", num_classes);
678 for(t=0;t<num_classes;t++) {
679 abc_class_t*cls = malloc(sizeof(abc_class_t));
680 memset(cls, 0, sizeof(abc_class_t));
682 DEBUG printf("class %d\n", t);
683 swf_GetU30(tag); //classname
684 swf_GetU30(tag); //supername
686 array_append(file->classes, NO_KEY, cls);
688 cls->flags = swf_GetU8(tag);
690 swf_GetU30(tag); //protectedNS
692 int inum = swf_GetU30(tag); //interface count
694 for(s=0;s<inum;s++) {
695 int interface_index = swf_GetU30(tag);
696 multiname_t* m = multiname_clone(pool_lookup_multiname(pool, interface_index));
697 list_append(cls->interfaces, m);
698 DEBUG printf(" class %d interface: %s\n", t, m->name);
701 swf_GetU30(tag); //iinit
704 for(t=0;t<num_classes;t++) {
705 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
706 int cinit = swf_GetU30(tag);
707 cls->static_constructor = (abc_method_t*)array_getvalue(file->methods, cinit);
710 int num_scripts = swf_GetU30(tag);
711 DEBUG printf("%d scripts\n", num_scripts);
712 for(t=0;t<num_scripts;t++) {
713 int init = swf_GetU30(tag);
717 int num_method_bodies = swf_GetU30(tag);
718 DEBUG printf("%d method bodies\n", num_method_bodies);
719 for(t=0;t<num_method_bodies;t++) {
720 int methodnr = swf_GetU30(tag);
721 if(methodnr >= file->methods->num) {
722 printf("Invalid method number: %d\n", methodnr);
725 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, methodnr);
726 abc_method_body_t*c = malloc(sizeof(abc_method_body_t));
727 memset(c, 0, sizeof(abc_method_body_t));
728 c->max_stack = swf_GetU30(tag);
729 c->local_count = swf_GetU30(tag);
730 c->init_scope_depth = swf_GetU30(tag);
731 c->max_scope_depth = swf_GetU30(tag);
732 int code_length = swf_GetU30(tag);
737 int pos = tag->pos + code_length;
738 c->code = code_parse(tag, code_length, file, pool);
741 int exception_count = swf_GetU30(tag);
743 for(s=0;s<exception_count;s++) {
744 swf_GetU30(tag); //from
745 swf_GetU30(tag); //to
746 swf_GetU30(tag); //target
747 swf_GetU30(tag); //exc_type
748 swf_GetU30(tag); //var_name
750 c->traits = traits_parse(tag, pool, file);
752 DEBUG printf("method_body %d) (method %d), %d bytes of code", t, methodnr, code_length);
754 array_append(file->method_bodies, NO_KEY, c);
756 if(tag->len - tag->pos) {
757 fprintf(stderr, "%d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
761 swf_SetTagPos(tag, classes_pos);
762 for(t=0;t<num_classes;t++) {
763 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
765 int classname_index = swf_GetU30(tag);
766 int superclass_index = swf_GetU30(tag);
767 cls->classname = multiname_clone(pool_lookup_multiname(pool, classname_index));
768 cls->superclass = multiname_clone(pool_lookup_multiname(pool, superclass_index));
769 cls->flags = swf_GetU8(tag);
772 int ns_index = swf_GetU30(tag);
773 cls->protectedNS = namespace_clone(pool_lookup_namespace(pool, ns_index));
776 int num_interfaces = swf_GetU30(tag); //interface count
778 for(s=0;s<num_interfaces;s++) {
779 swf_GetU30(tag); // multiname index TODO
781 int iinit = swf_GetU30(tag);
782 cls->constructor = (abc_method_t*)array_getvalue(file->methods, iinit);
783 cls->traits = traits_parse(tag, pool, file);
785 for(t=0;t<num_classes;t++) {
786 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
788 swf_GetU30(tag); // cindex
789 cls->static_constructor_traits = traits_parse(tag, pool, file);
791 int num_scripts2 = swf_GetU30(tag);
792 for(t=0;t<num_scripts2;t++) {
793 int init = swf_GetU30(tag);
794 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, init);
796 abc_script_t*s = malloc(sizeof(abc_script_t));
797 memset(s, 0, sizeof(abc_script_t));
799 s->traits = traits_parse(tag, pool, file);
800 array_append(file->scripts, NO_KEY, s);
802 fprintf(stderr, "Can't parse script traits\n");
811 void swf_WriteABC(TAG*abctag, void*code)
813 abc_file_t*file = (abc_file_t*)code;
814 pool_t*pool = pool_new();
816 TAG*tmp = swf_InsertTag(0,0);
820 char need_null_method=0;
821 for(t=0;t<file->classes->num;t++) {
822 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
823 if(!c->constructor || !c->static_constructor) {
829 abc_method_t*nullmethod = 0;
830 if(need_null_method) {
831 nullmethod = malloc(sizeof(abc_method_t));
832 memset(nullmethod, 0, sizeof(abc_method_t));
833 /*TODO: might be more efficient to have this at the beginning */
834 array_append(file->methods, NO_KEY, nullmethod);
837 swf_SetU30(tag, file->methods->num);
838 /* enumerate classes, methods and method bodies */
839 for(t=0;t<file->methods->num;t++) {
840 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
843 for(t=0;t<file->classes->num;t++) {
844 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
847 for(t=0;t<file->method_bodies->num;t++) {
848 abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
852 for(t=0;t<file->methods->num;t++) {
853 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
855 multiname_list_t*l = m->parameters;
856 int num_params = list_length(m->parameters);
857 swf_SetU30(tag, num_params);
859 swf_SetU30(tag, pool_register_multiname(pool, m->return_type));
864 swf_SetU30(tag, pool_register_multiname(pool, l->multiname));
868 swf_SetU30(tag, pool_register_string(pool, m->name));
873 swf_SetU8(tag, 0); //flags
877 swf_SetU30(tag, file->metadata->num);
878 for(t=0;t<file->metadata->num;t++) {
879 const char*entry_name = array_getkey(file->metadata, t);
880 swf_SetU30(tag, pool_register_string(pool, entry_name));
881 array_t*items = (array_t*)array_getvalue(file->metadata, t);
882 swf_SetU30(tag, items->num);
884 for(s=0;s<items->num;s++) {
885 int i1 = pool_register_string(pool, array_getkey(items, s));
886 int i2 = pool_register_string(pool, array_getvalue(items, s));
892 swf_SetU30(tag, file->classes->num);
893 for(t=0;t<file->classes->num;t++) {
894 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
896 int classname_index = pool_register_multiname(pool, c->classname);
897 int superclass_index = pool_register_multiname(pool, c->superclass);
899 swf_SetU30(tag, classname_index);
900 swf_SetU30(tag, superclass_index);
902 swf_SetU8(tag, c->flags); // flags
904 int ns_index = pool_register_namespace(pool, c->protectedNS);
905 swf_SetU30(tag, ns_index);
908 swf_SetU30(tag, list_length(c->interfaces));
909 multiname_list_t*interface= c->interfaces;
911 swf_SetU30(tag, pool_register_multiname(pool, interface->multiname));
912 interface = interface->next;
915 if(!c->constructor) {
916 swf_SetU30(tag, nullmethod->index);
918 swf_SetU30(tag, c->constructor->index);
920 traits_write(pool, tag, c->traits);
922 for(t=0;t<file->classes->num;t++) {
923 abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
924 if(!c->static_constructor) {
925 swf_SetU30(tag, nullmethod->index);
927 swf_SetU30(tag, c->static_constructor->index);
929 traits_write(pool, tag, c->static_constructor_traits);
932 swf_SetU30(tag, file->scripts->num);
933 for(t=0;t<file->scripts->num;t++) {
934 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
935 swf_SetU30(tag, s->method->index); //!=t!
936 traits_write(pool, tag, s->traits);
939 swf_SetU30(tag, file->method_bodies->num);
940 for(t=0;t<file->method_bodies->num;t++) {
941 abc_method_body_t*c = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
942 abc_method_t*m = c->method;
943 swf_SetU30(tag, m->index);
944 swf_SetU30(tag, c->max_stack);
945 swf_SetU30(tag, c->local_count);
946 swf_SetU30(tag, c->init_scope_depth);
947 swf_SetU30(tag, c->max_scope_depth);
949 code_write(tag, c->code, pool, file);
951 swf_SetU30(tag, c->exception_count);
952 traits_write(pool, tag, c->traits);
955 // --- start to write real tag --
959 if(tag->id == ST_DOABC) {
960 swf_SetU32(tag, 1); // flags
961 swf_SetString(tag, file->name);
964 swf_SetU16(tag, 0x10); //version
965 swf_SetU16(tag, 0x2e);
967 pool_write(pool, tag);
969 swf_SetBlock(tag, tmp->data, tmp->len);
971 swf_DeleteTag(0, tmp);
975 void swf_FreeABC(void*code)
977 abc_file_t*file= (abc_file_t*)code;
980 for(t=0;t<file->metadata->num;t++) {
981 array_t*items = (array_t*)array_getvalue(file->metadata, t);
983 for(s=0;s<items->num;s++) {
984 free(array_getvalue(items, s));
988 array_free(file->metadata);
990 for(t=0;t<file->methods->num;t++) {
991 abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
993 multiname_list_t*param = m->parameters;
995 multiname_destroy(param->multiname);param->multiname=0;
998 list_free(m->parameters);m->parameters=0;
1001 free((void*)m->name);m->name=0;
1003 if(m->return_type) {
1004 multiname_destroy(m->return_type);
1008 array_free(file->methods);
1010 for(t=0;t<file->classes->num;t++) {
1011 abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1012 traits_free(cls->traits);cls->traits=0;
1013 traits_free(cls->static_constructor_traits);cls->static_constructor_traits=0;
1015 if(cls->classname) {
1016 multiname_destroy(cls->classname);
1018 if(cls->superclass) {
1019 multiname_destroy(cls->superclass);
1022 multiname_list_t*i = cls->interfaces;
1024 multiname_destroy(i->multiname);i->multiname=0;
1027 list_free(cls->interfaces);cls->interfaces=0;
1029 if(cls->protectedNS) {
1030 namespace_destroy(cls->protectedNS);
1034 array_free(file->classes);
1036 for(t=0;t<file->scripts->num;t++) {
1037 abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1038 traits_free(s->traits);s->traits=0;
1041 array_free(file->scripts);
1043 for(t=0;t<file->method_bodies->num;t++) {
1044 abc_method_body_t*body = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1045 code_free(body->code);body->code=0;
1046 traits_free(body->traits);body->traits=0;
1049 array_free(file->method_bodies);
1052 free((void*)file->name);file->name=0;
1058 void swf_AddButtonLinks(SWF*swf, char stop_each_frame, char events)
1061 int has_buttons = 0;
1062 TAG*tag=swf->firstTag;
1064 if(tag->id == ST_SHOWFRAME)
1066 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2)
1071 abc_file_t*file = abc_file_new();
1072 abc_method_body_t*c = 0;
1074 abc_class_t*cls = abc_class_new2(file, "rfx::MainTimeline", "flash.display::MovieClip");
1075 abc_class_protectedNS(cls, "rfx:MainTimeline");
1077 TAG*abctag = swf_InsertTagBefore(swf, swf->firstTag, ST_DOABC);
1079 tag = swf_InsertTag(abctag, ST_SYMBOLCLASS);
1082 swf_SetString(tag, "rfx.MainTimeline");
1084 c = abc_class_staticconstructor(cls, 0, 0);
1087 c->init_scope_depth = 9;
1088 c->max_scope_depth = 10;
1094 c = abc_class_constructor(cls, 0, 0);
1097 c->init_scope_depth = 10;
1098 c->max_scope_depth = 11;
1100 debugfile(c, "constructor.as");
1106 __ constructsuper(c,0);
1108 __ getlex(c, "[package]flash.system::Security");
1109 __ pushstring(c, "*");
1110 __ callpropvoid(c, "[package]::allowDomain", 1);
1112 if(stop_each_frame || has_buttons) {
1114 tag = swf->firstTag;
1115 abc_method_body_t*f = 0; //frame script
1116 while(tag && tag->id!=ST_END) {
1118 char needs_framescript=0;
1119 char buttonname[80];
1120 char functionname[80];
1121 sprintf(framename, "[packageinternal]rfx::frame%d", frame);
1123 if(!f && (tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2 || stop_each_frame)) {
1124 /* make the contructor add a frame script */
1125 __ findpropstrict(c,"[package]::addFrameScript");
1126 __ pushbyte(c,frame);
1127 __ getlex(c,framename);
1128 __ callpropvoid(c,"[package]::addFrameScript",2);
1130 f = abc_class_method(cls, 0, framename, 0);
1133 f->init_scope_depth = 10;
1134 f->max_scope_depth = 11;
1135 __ debugfile(f, "framescript.as");
1141 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1142 U16 id = swf_GetDefineID(tag);
1143 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1144 __ getlex(f,buttonname);
1145 __ getlex(f,"flash.events::MouseEvent");
1146 __ getproperty(f, "::CLICK");
1147 sprintf(functionname, "::clickbutton%d", swf_GetDefineID(tag));
1148 __ getlex(f,functionname);
1149 __ callpropvoid(f, "::addEventListener" ,2);
1151 if(stop_each_frame) {
1152 __ findpropstrict(f, "[package]::stop");
1153 __ callpropvoid(f, "[package]::stop", 0);
1155 needs_framescript = 1;
1157 abc_method_body_t*h =
1158 abc_class_method(cls, "::void", functionname, 1, "flash.events::MouseEvent");
1161 h->init_scope_depth = 10;
1162 h->max_scope_depth = 11;
1166 ActionTAG*oldaction = swf_ButtonGetAction(tag);
1167 if(oldaction && oldaction->op == ACTION__GOTOFRAME) {
1168 int framenr = GET16(oldaction->data);
1170 fprintf(stderr, "Warning: Couldn't translate jump to frame %d to flash 9 actionscript\n", framenr);
1173 __ findpropstrict(h,"[package]::gotoAndStop");
1174 __ pushbyte(h,framenr+1);
1175 __ callpropvoid(h,"[package]::gotoAndStop", 1);
1178 sprintf(framename, "frame%d", framenr);
1179 __ getlocal_0(h); //this
1180 __ findpropstrict(h, "[package]flash.events::TextEvent");
1181 __ pushstring(h, "link");
1184 __ pushstring(h, framename);
1185 __ constructprop(h,"[package]flash.events::TextEvent", 4);
1186 __ callpropvoid(h,"[package]::dispatchEvent", 1);
1188 } else if(oldaction && oldaction->op == ACTION__GETURL) {
1190 __ findpropstrict(h,"flash.net::navigateToURL");
1191 __ findpropstrict(h,"flash.net::URLRequest");
1192 // TODO: target _blank
1193 __ pushstring(h,oldaction->data); //url
1194 __ constructprop(h,"flash.net::URLRequest", 1);
1195 __ callpropvoid(h,"flash.net::navigateToURL", 1);
1197 __ getlocal_0(h); //this
1198 __ findpropstrict(h, "[package]flash.events::TextEvent");
1199 __ pushstring(h, "link");
1202 __ pushstring(h,oldaction->data); //url
1203 __ constructprop(h,"[package]flash.events::TextEvent", 4);
1204 __ callpropvoid(h,"[package]::dispatchEvent", 1);
1206 } else if(oldaction) {
1207 fprintf(stderr, "Warning: Couldn't translate button code of button %d to flash 9 abc action\n", id);
1210 swf_ActionFree(oldaction);
1212 if(tag->id == ST_SHOWFRAME) {
1227 tag = swf->firstTag;
1229 if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1230 char buttonname[80];
1231 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1232 abc_AddSlot(cls, buttonname, 0, "flash.display::SimpleButton");
1238 abc_script_t*s = abc_initscript(file, 0, 0);
1239 c = s->method->body;
1242 c->init_scope_depth = 1;
1243 c->max_scope_depth = 9;
1247 __ getscopeobject(c, 0);
1248 __ getlex(c,"::Object");
1250 __ getlex(c,"flash.events::EventDispatcher");
1252 __ getlex(c,"flash.display::DisplayObject");
1254 __ getlex(c,"flash.display::InteractiveObject");
1256 __ getlex(c,"flash.display::DisplayObjectContainer");
1258 __ getlex(c,"flash.display::Sprite");
1260 __ getlex(c,"flash.display::MovieClip");
1262 __ getlex(c,"flash.display::MovieClip");
1271 __ initproperty(c,"rfx::MainTimeline");
1274 //abc_method_body_addClassTrait(c, "rfx:MainTimeline", 1, cls);
1275 abc_initscript_addClassTrait(s, "rfx::MainTimeline", 1, cls);
1277 swf_WriteABC(abctag, file);