3 Routines for handling Flash2 AVM2 ABC Actionscript
5 Extension module for the rfxswf library.
6 Part of the swftools package.
8 Copyright (c) 2007,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 */
25 #include "../rfxswf.h"
27 typedef struct _abc_method {
28 /* from method signature: */
31 int return_type_index;//index into multiname
33 int params[16]; // indexes into multiname
37 int method_body_index;
40 typedef struct _abc_multiname {
45 typedef struct _dict_entry {
50 typedef struct _dict {
56 typedef struct _abc_file_t {
65 dict_t*namespace_sets;
77 typedef struct _abc_trait {
96 typedef struct _abc_class {
101 int superclass_index;
105 int static_constructor_index;
106 dict_t*static_constructor_traits;
111 typedef struct _abc_code {
119 int init_scope_depth;
127 dict_t*d = malloc(sizeof(dict_t));
128 memset(d, 0, sizeof(dict_t));
132 void dict_free(dict_t*dict) {
134 free(dict->d);dict->d = 0;
138 const char*dict_getstr(dict_t*dict, int nr) {
139 if(nr > dict->num || nr<0) {
140 printf("error: reference to string %d in dict\n");
143 return dict->d[nr].name;
145 char*dict_getdata(dict_t*dict, int nr) {
146 if(nr > dict->num || nr<0) {
147 printf("error: reference to string %d in dict\n");
150 return dict->d[nr].data;
152 int dict_append(dict_t*dict, const char*name, void*data) {
153 while(dict->size <= dict->num) {
156 dict->d = malloc(sizeof(dict_entry_t)*dict->size);
158 dict->d = realloc(dict->d, sizeof(dict_entry_t)*dict->size);
162 dict->d[dict->num].name = strdup(name);
164 dict->d[dict->num].name = 0;
166 dict->d[dict->num].data = data;
169 int dict_find(dict_t*dict, const char*name)
174 for(t=0;t<dict->num;t++) {
175 if(dict->d[t].name && !strcmp(dict->d[t].name,name))
180 int dict_update(dict_t*dict, char*name, void*data) {
181 int pos = dict_find(dict, name);
183 dict->d[pos].data = data;
186 return dict_append(dict, name, data);
188 int dict_append_if_new(dict_t*dict, char*name, void*data) {
189 int pos = dict_find(dict, name);
192 return dict_append(dict, name, data);
195 int swf_GetU30(TAG*tag)
200 U8 b = swf_GetU8(tag);
209 int swf_GetS30(TAG*tag)
214 U8 b = swf_GetU8(tag);
219 s|=0xffffffff<<shift;
227 double swf_GetD64(TAG*tag)
229 double value = *(double*)&tag->data[tag->pos];
236 typedef struct _opcode
238 unsigned char opcode;
254 int abc_RegisterNameSpace(abc_file_t*file, char*name);
255 int abc_RegisterPackageNameSpace(abc_file_t*file, char*name);
256 int abc_RegisterPackageInternalNameSpace(abc_file_t*file, char*name);
257 int abc_RegisterProtectedNameSpace(abc_file_t*file, char*name);
258 int abc_RegisterExplicitNameSpace(abc_file_t*file, char*name);
259 int abc_RegisterStaticProtectedNameSpace(abc_file_t*file, char*name);
260 int abc_RegisterPrivateNameSpace(abc_file_t*file, char*name);
266 {0x86, "atype", "2"},
267 {0x87, "astypelate", ""},
268 {0xA8, "bitand", ""},
269 {0x97, "bitnot", ""},
271 {0xaa, "bitxor", ""},
273 {0x43, "callmethod", "mn"},
274 {0x4c, "callproplex", "2n"},
275 {0x46, "callproperty", "2n"},
276 {0x4f, "callpropvoid", "2n"},
277 {0x44, "callstatic", "in"},
278 {0x45, "callsuper", "2n"},
279 {0x4e, "callsupervoid", "2n"},
280 {0x78, "checkfilter", ""},
281 {0x80, "coerce", "m"},
282 {0x82, "coerce_a", ""},
283 {0x85, "coerce_s", ""},
284 {0x42, "construct", "n"},
285 {0x4a, "constructprop", "2n"},
286 {0x49, "constructsuper", "n"},
287 {0x76, "convert_b", ""},
288 {0x73, "convert_i", ""},
289 {0x75, "convert_d", ""},
290 {0x77, "convert_o", ""},
291 {0x74, "convert_u", ""},
292 {0x70, "convert_s", ""},
293 {0xef, "debug", "bsbu"},
294 {0xf1, "debugfile", "s"},
295 {0xf0, "debugline", "u"},
296 {0x94, "declocal", "u"},
297 {0xc3, "declocal_i", "u"},
298 {0x93, "decrement", ""},
299 {0xc1, "decrement_i", ""},
300 {0x6a, "deleteproperty", "2"},
301 {0xa3, "divide", ""},
304 {0x07, "dxnslate", ""},
305 {0xab, "equals", ""},
306 {0x72, "esc_xattr", ""},
307 {0x71, "esc_xelem", ""},
308 {0x5e, "findproperty", "2"},
309 {0x5d, "findpropstrict", "2"},
310 {0x59, "getdescendants", "2"},
311 {0x64, "getglobalscope", ""},
312 {0x6e, "getglobalslot", "u"},
313 {0x60, "getlex", "2"},
314 {0x62, "getlocal", "u"},
315 {0xd0, "getlocal_0", ""},
316 {0xd1, "getlocal_1", ""},
317 {0xd2, "getlocal_2", ""},
318 {0xd3, "getlocal_3", ""},
319 {0x66, "getproperty", "2"},
320 {0x65, "getscopeobject", "u"},
321 {0x6c, "getslot", "u"},
322 {0x04, "getsuper", "2"},
323 {0xaf, "greaterequals", ""},
324 {0x1f, "hasnext", ""},
325 {0x32, "hasnext2", "uu"},
327 {0x12, "iffalse", "j"},
332 {0x0f, "ifnge", "j"},
333 {0x0e, "ifngt", "j"},
334 {0x0d, "ifnle", "j"},
335 {0x0c, "ifnlt", "j"},
337 {0x19, "ifstricteq", "j"},
338 {0x1a, "ifstrictne", "j"},
339 {0x11, "iftrue", "j"},
341 {0x92, "inclocal", "u"},
342 {0xc2, "inclocal_i", "u"},
343 {0x91, "increment", ""},
344 {0xc0, "increment_i", ""},
345 {0x68, "initproperty", "2"},
346 {0xb1, "instanceof", ""},
347 {0xb2, "istype", "2"},
348 {0xb3, "istypelate", ""},
352 {0xae, "lessequals", ""},
353 {0xad, "lessthan", ""},
354 {0x1b, "lookupswitch", "S"},
355 {0xa5, "lshift", ""},
356 {0xa4, "modulo", ""},
357 {0xa2, "multiply", ""},
358 {0xc7, "multiply_i", ""},
359 {0x90, "negate", ""},
360 {0xc4, "negate_i", ""},
361 {0x57, "newactivation", ""},
362 {0x56, "newarray", "u"},
363 {0x5a, "newcatch", "u"}, //index into exception_info
364 {0x58, "newclass", "c"}, //index into class_info
365 {0x40, "newfunction", "u"}, //index into method_info
366 {0x55, "newobject", "u"},
367 {0x1e, "nextname", ""},
368 {0x23, "nextvalue", ""},
372 {0x1d, "popscope", ""},
373 {0x24, "pushbyte", "b"},
374 {0x2f, "pushdouble", "u"}, //index into floats
375 {0x27, "pushfalse", ""},
376 {0x2d, "pushint", "u"}, //index into ints
377 {0x31, "pushnamespace", "u"}, //index into namespace
378 {0x28, "pushnan", ""},
379 {0x20, "pushnull", ""},
380 {0x30, "pushscope", ""},
381 {0x25, "pushshort", "u"},
382 {0x2c, "pushstring", "s"},
383 {0x26, "pushtrue", ""},
384 {0x2e, "pushuint", "u"}, //index into uints
385 {0x21, "pushundefined", ""},
386 {0x1c, "pushwith", ""},
387 {0x48, "returnvalue", ""},
388 {0x47, "returnvoid", ""},
389 {0xa6, "rshift", ""},
390 {0x63, "setlocal", "u"},
391 {0xd4, "setlocal_0", ""},
392 {0xd5, "setlocal_1", ""},
393 {0xd6, "setlocal_2", ""},
394 {0xd7, "setlocal_3", ""},
395 {0x6f, "setglobalshot", "u"},
396 {0x61, "setproperty", "2"},
397 {0x6d, "setslot", "u"},
398 {0x05, "setsuper", "2"},
399 {0xac, "strictequals", ""},
400 {0xa1, "subtract", ""},
401 {0xc6, "subtract_i", ""},
404 {0x95, "typeof", ""},
405 {0xa7, "urshift", ""},
409 int swf_GetU24(TAG*tag)
411 int b1 = swf_GetU8(tag);
412 int b2 = swf_GetU8(tag);
413 int b3 = swf_GetU8(tag);
414 return b3<<16|b2<<8|b1;
416 int swf_GetS24(TAG*tag)
418 int b1 = swf_GetU8(tag);
419 int b2 = swf_GetU8(tag);
420 int b3 = swf_GetU8(tag);
422 return -1-((b3<<16|b2<<8|b1)^0xffffff);
424 return b3<<16|b2<<8|b1;
427 static int parse_code(TAG*tag, int len, abc_file_t*pool, char*prefix)
429 int end=tag->pos+len;
430 while(tag->pos<end) {
431 U8 opcode = swf_GetU8(tag);
434 for(t=0;t<sizeof(opcodes)/sizeof(opcodes[0]);t++) {
435 if(opcodes[t].opcode == opcode) {
436 printf("%s%s ", prefix, opcodes[t].name);
437 char*p = opcodes[t].params;
443 int n = swf_GetU30(tag);
444 printf("%d params", n);
445 } else if(*p == '2') {
446 const char* m = dict_getstr(pool->multinames, swf_GetU30(tag));
448 } else if(*p == 'm') {
449 int n = swf_GetU30(tag);
450 printf("[method%d]", n);
451 } else if(*p == 'c') {
452 int n = swf_GetU30(tag);
453 abc_class_t*cls = (abc_class_t*)dict_getdata(pool->classes, n);
454 printf("[classinfo%d %s]", n, cls->name);
455 } else if(*p == 'i') {
456 int n = swf_GetU30(tag);
457 printf("[methodbody%d]", n);
458 } else if(*p == 'u') {
459 int n = swf_GetU30(tag);
461 } else if(*p == 'b') {
462 int b = swf_GetU8(tag);
464 } else if(*p == 'j') {
465 printf("%d", swf_GetS24(tag));
466 } else if(*p == 's') {
467 const char*s = dict_getstr(pool->strings, swf_GetU30(tag));
469 } else if(*p == 'S') {
470 swf_GetU24(tag); //default
471 int num = swf_GetU30(tag)+1;
476 printf("Can't parse opcode param type \"%c\"\n", *p);
487 printf("Can't parse opcode %02x\n", opcode);
493 printf("Read beyond end of ABC Bytecode\n");
499 static void dump_method(const char*prefix, const char*type, const char*name, int nr, abc_file_t*pool)
501 if(nr >= pool->methods->num) {
502 printf("Invalid method number: %d\n", nr);
505 abc_method_t*m = (abc_method_t*)dict_getdata(pool->methods, nr);
506 printf("%s%s %s %s%s\n", prefix, type, dict_getstr(pool->multinames,m->return_type_index), name, m->paramstr);
508 abc_code_t*c = (abc_code_t*)dict_getdata(pool->method_bodies, m->method_body_index);
510 printf("%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);
512 swf_SetTagPos(c->tag, 0);
514 sprintf(prefix2, "%s ", prefix);
515 printf("%s{\n", prefix);
516 parse_code(c->tag, c->tag->len, pool,prefix2);
517 printf("%s}\n", prefix);
523 static void parse_metadata(TAG*tag, abc_file_t*pool)
526 int num_metadata = swf_GetU30(tag);
527 DEBUG printf("%d metadata\n");
528 for(t=0;t<num_metadata;t++) {
529 const char*name = dict_getstr(pool->strings, swf_GetU30(tag));
530 int num = swf_GetU30(tag);
532 DEBUG printf(" %s\n", name);
534 const char*key = dict_getstr(pool->strings, swf_GetU30(tag));
535 const char*value = dict_getstr(pool->strings, swf_GetU30(tag));
536 DEBUG printf(" %s=%s\n", key, value);
542 #define TRAIT_METHOD 1
543 #define TRAIT_GETTER 2
544 #define TRAIT_SETTER 3
545 #define TRAIT_CLASS 4
546 #define TRAIT_FUNCTION 5
547 #define TRAIT_CONST 6
549 static dict_t* traits_parse(TAG*tag, abc_file_t*pool)
551 int num_traits = swf_GetU30(tag);
552 dict_t*traits = dict_new();
555 DEBUG printf("%d traits\n", num_traits);
558 for(t=0;t<num_traits;t++) {
559 abc_trait_t*trait = malloc(sizeof(abc_trait_t));
560 memset(trait, 0, sizeof(abc_trait_t));
561 dict_append(traits, 0, trait);
562 trait->name_index = swf_GetU30(tag);
563 const char*name = dict_getstr(pool->multinames, trait->name_index);
564 U8 kind = trait->type = swf_GetU8(tag);
565 U8 attributes = kind&0xf0;
567 DEBUG printf(" trait %d) %s type=%02x\n", t, name, kind);
568 if(kind == 1 || kind == 2 || kind == 3) { // method / getter / setter
569 trait->disp_id = swf_GetU30(tag);
570 trait->nr = swf_GetU30(tag);
571 DEBUG printf(" method/getter/setter\n");
572 } else if(kind == 5) { // function
573 trait->slot_id = swf_GetU30(tag);
574 trait->nr = swf_GetU30(tag);
575 } else if(kind == 4) { // class
576 trait->slot_id = swf_GetU30(tag);
577 trait->cls = swf_GetU30(tag);
578 DEBUG printf(" class %s %d %d\n", name, trait->slot_id, trait->cls);
579 } else if(kind == 0 || kind == 6) { // slot, const
580 trait->slot_id = swf_GetU30(tag);
581 const char*type_name = dict_getstr(pool->multinames, swf_GetU30(tag));
582 trait->vindex = swf_GetU30(tag);
584 trait->vkind = swf_GetU8(tag);
586 DEBUG printf(" slot %s %d %s (vindex=%d)\n", name, trait->slot_id, type_name, trait->vindex);
588 printf(" can't parse trait type %d\n", kind);
591 if(attributes&0x40) {
592 int num = swf_GetU30(tag);
595 swf_GetU30(tag); //index into metadata array
602 static void dump_traits(char*prefix, dict_t*traits, abc_file_t*pool)
604 int num_traits = traits->num;
606 for(t=0;t<num_traits;t++) {
607 abc_trait_t*trait = (abc_trait_t*)dict_getdata(traits, t);
608 const char*name = dict_getstr(pool->multinames, trait->name_index);
609 U8 kind = trait->type;
610 U8 attributes = kind&0xf0;
612 if(kind == TRAIT_METHOD) {
613 printf("%s method %d %d %s\n", prefix, trait->nr, trait->disp_id, ((abc_method_t*)dict_getdata(pool->methods, trait->nr))->paramstr);
614 dump_method(prefix, "method", name, trait->nr, pool);
615 } else if(kind == TRAIT_GETTER) {
616 dump_method(prefix, "getter", name, trait->nr, pool);
617 } else if(kind == TRAIT_SETTER) {
618 dump_method(prefix, "setter", name, trait->nr, pool);
619 } else if(kind == TRAIT_FUNCTION) { // function
620 dump_method(prefix, "function", name, trait->nr, pool);
621 } else if(kind == TRAIT_CLASS) { // class
622 abc_class_t*cls = (abc_class_t*)dict_getdata(pool->classes, trait->cls);
624 printf("%sslot %d: class %s=class%d %d\n", prefix, trait->slot_id, name, trait->cls);
626 printf("%sslot %d: class %s=%s\n", prefix, trait->slot_id, name, cls->name);
628 } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
629 int slot_id = trait->slot_id;
630 const char*type_name = dict_getstr(pool->multinames, trait->type_index);
631 printf(" %sslot %s %d %s (vindex=%d)\n", prefix, name, trait->slot_id, type_name, trait->vindex);
633 printf(" can't dump trait type %d\n", kind);
638 void swf_CopyData(TAG*to, TAG*from, int len)
640 unsigned char*data = malloc(len);
641 swf_GetBlock(from, data, len);
642 swf_SetBlock(to, data, len);
646 abc_file_t*abc_file_new()
648 abc_file_t*f = malloc(sizeof(abc_file_t));
649 memset(f, 0, sizeof(abc_file_t));
651 f->ints = dict_new();
652 dict_append(f->ints, 0, (void*)(ptroff_t)0);
653 f->uints = dict_new();
654 dict_append(f->uints, 0, (void*)(ptroff_t)0);
655 f->floats = dict_new();
656 dict_append(f->floats, 0, 0);
657 f->strings = dict_new();
658 dict_append(f->strings, "--<UNDEFINED_STRING>--", 0);
659 f->namespaces = dict_new();
660 dict_append(f->namespaces, "--<UNDEFINED_NAMESPACE>--", 0);
661 f->namespace_sets = dict_new();
662 dict_append(f->namespace_sets, "--<UNDEFINED_NSSET>--", 0);
663 f->sets = dict_new();
664 dict_append(f->sets, "--<UNDEFINED_SET>--", 0);
665 f->multinames = dict_new();
666 dict_append(f->multinames, "--<UNDEFINED_MULTINAME>--", 0);
670 f->methods = dict_new();
671 f->classes = dict_new();
672 f->scripts = dict_new();
673 f->method_bodies = dict_new();
678 static char* access2str(int type)
680 if(type==0x08) return "";
681 else if(type==0x16) return "package";
682 else if(type==0x17) return "packageinternal";
683 else if(type==0x18) return "protected";
684 else if(type==0x19) return "explicit";
685 else if(type==0x1A) return "staticprotected";
686 else if(type==0x05) return "private";
687 else return "undefined";
690 void swf_DissassembleABC(TAG*tag)
692 abc_file_t* pool = abc_file_new();
694 swf_SetTagPos(tag, 0);
695 U32 flags = swf_GetU32(tag);
697 DEBUG printf("flags=%08x\n", flags);
698 char*classname = swf_GetString(tag);
699 U32 version = swf_GetU32(tag);
700 if(version!=0x002e0010) {
701 fprintf(stderr, "Warning: unknown AVM2 version %08x\n", version);
704 int num_ints = swf_GetU30(tag);
705 DEBUG printf("%d ints\n", num_ints);
706 for(t=1;t<num_ints;t++) {
707 S32 v = swf_GetU30(tag);
708 DEBUG printf("int %d) %d\n", t, v);
709 dict_append(pool->ints, 0, (void*)(ptroff_t)v);
712 int num_uints = swf_GetU30(tag);
713 DEBUG printf("%d uints\n", num_uints);
714 for(t=1;t<num_uints;t++) {
715 U32 v = swf_GetS30(tag);
716 DEBUG printf("uint %d) %d\n", t, v);
717 dict_append(pool->uints, 0, (void*)(ptroff_t)v);
720 int num_floats = swf_GetU30(tag);
721 DEBUG printf("%d floats\n", num_floats);
722 for(t=1;t<num_floats;t++) {
723 double d = swf_GetD64(tag);
724 DEBUG printf("float %d) %f\n", t, d);
725 dict_append(pool->floats, 0, 0);
728 int num_strings = swf_GetU30(tag);
729 DEBUG printf("%d strings\n", num_strings);
730 for(t=1;t<num_strings;t++) {
731 int len = swf_GetU30(tag);
732 char*s = malloc(len+1);
733 swf_GetBlock(tag, s, len);
735 dict_append(pool->strings, s, 0);
736 DEBUG printf("%d) \"%s\"\n", t, pool->strings->d[t].name);
738 int num_namespaces = swf_GetU30(tag);
739 DEBUG printf("%d namespaces\n", num_namespaces);
740 for(t=1;t<num_namespaces;t++) {
741 U8 type = swf_GetU8(tag);
742 int namenr = swf_GetU30(tag);
743 const char*name = dict_getstr(pool->strings, namenr);
744 dict_append(pool->namespaces, name, (void*)(ptroff_t)type);
748 if(type==0x08) printf("Namespace %s\n", name);
749 else if(type==0x16) printf("PackageNamespace %s\n", name);
750 else if(type==0x17) printf("PackageInternalNs %s\n", name);
751 else if(type==0x18) printf("ProtectedNamespace %s\n", name);
752 else if(type==0x19) printf("ExplicitNamespace %s\n", name);
753 else if(type==0x1A) printf("StaticProtectedNs %s\n", name);
754 else if(type==0x05) printf("PrivateNs %s\n", name);
756 printf("Undefined namespace type\n");
761 int num_sets = swf_GetU30(tag);
762 DEBUG printf("%d namespace sets\n", num_namespaces);
763 for(t=1;t<num_sets;t++) {
764 int count = swf_GetU30(tag);
766 const char**name = malloc(sizeof(const char*)*count);
768 for(s=0;s<count;s++) {
769 int nsnr = swf_GetU30(tag);
770 name[s] = dict_getstr(pool->namespaces, nsnr);
771 l += strlen(name[s])+1;
773 char*desc = malloc(l+16);
775 for(s=0;s<count;s++) {
776 strcat(desc, name[s]);
780 dict_append(pool->namespace_sets, desc, 0);
781 DEBUG printf("set %d) %s\n", t, desc);
784 int num_multinames = swf_GetU30(tag);
785 DEBUG printf("%d multinames\n", num_multinames);
786 for(t=1;t<num_multinames;t++) {
787 U8 type = swf_GetU8(tag);
789 if(type==0x07 || type==0x0d) {
790 int nr1 = swf_GetU30(tag);
791 const char*namespace = dict_getstr(pool->namespaces, nr1);
792 U8 access = (U8)(ptroff_t)dict_getdata(pool->namespaces, nr1);
793 const char*methodname = dict_getstr(pool->strings, swf_GetU30(tag));
794 DEBUG printf("multiname %d) <%s> %s:%s\n", t, access2str(access), namespace, methodname);
795 mname = malloc(strlen(namespace)+strlen(methodname)+300);
796 sprintf(mname, "[%s]\0", access2str(access));
797 strcat(mname, namespace);
799 strcat(mname, methodname);
800 } else if(type==0x0f || type==0x10) {
801 const char*methodname = dict_getstr(pool->strings, swf_GetU30(tag));
802 mname = strdup(methodname);
803 } else if(type==0x11 || type==0x12) {
805 } else if(type==0x09 || type==0x0e) {
806 const char*methodname = dict_getstr(pool->strings, swf_GetU30(tag));
807 const char*namespace = dict_getstr(pool->namespace_sets, swf_GetU30(tag));
808 DEBUG printf("multiname %d) %s:%s\n", t, namespace, methodname);
809 mname = malloc(strlen(namespace)+strlen(methodname)+16);
810 strcpy(mname, namespace);
812 strcat(mname, methodname);
813 } else if(type==0x1b || type==0x1c) {
814 const char*nsset = dict_getstr(pool->namespace_sets, swf_GetU30(tag));
815 mname = strdup(nsset);
817 printf("can't parse type %d multinames yet\n", type);
820 dict_append(pool->multinames, mname, 0);
824 int num_methods = swf_GetU30(tag);
825 DEBUG printf("%d methods\n", num_methods);
826 for(t=0;t<num_methods;t++) {
827 abc_method_t*m = malloc(sizeof(abc_method_t));
828 memset(m, 0, sizeof(*m));
829 m->param_count = swf_GetU30(tag);
830 m->return_type_index = swf_GetU30(tag);
833 int params_len = 256;
834 char* params = malloc(params_len);
837 for(s=0;s<m->param_count;s++) {
838 int typenr = swf_GetU30(tag);
839 if(s < sizeof(m->params)/sizeof(m->params[0]))
840 m->params[s] = typenr;
841 const char*type = dict_getstr(pool->multinames, typenr);
842 while(strlen(type)+strlen(params)>params_len-4) {
844 params = realloc(params, params_len);
847 strcat(params, ", ");
848 strcat(params, type);
851 int namenr = swf_GetU30(tag);
854 m->name = dict_getstr(pool->strings, namenr);
855 m->paramstr=strdup(params);
856 free(params);params = 0;
858 m->flags = swf_GetU8(tag);
860 DEBUG printf("method %d) %s flags=%02x\n", t, m->paramstr, m->flags);
863 /* optional parameters */
864 int num = swf_GetU30(tag);
867 int val = swf_GetU30(tag);
868 U8 kind = swf_GetU8(tag); // specifies index type for "val"
872 /* debug information- not used by avm2 */
873 for(s=0;s<m->param_count;s++) {
874 m->name = dict_getstr(pool->strings, swf_GetU30(tag));
877 dict_append(pool->methods, m->name, m);
880 parse_metadata(tag, pool);
882 /* skip classes, and scripts for now, and do the real parsing later */
883 int num_classes = swf_GetU30(tag);
884 int classes_pos = tag->pos;
885 DEBUG printf("%d classes\n", num_classes);
886 for(t=0;t<num_classes;t++) {
887 abc_class_t*cls = malloc(sizeof(abc_class_t));
888 memset(cls, 0, sizeof(abc_class_t));
889 dict_append(pool->classes, 0, cls);
891 DEBUG printf("class %d\n", t);
892 swf_GetU30(tag); //classname
893 swf_GetU30(tag); //supername
894 U8 flags = swf_GetU8(tag);
896 swf_GetU30(tag); //protectedNS
897 int inum = swf_GetU30(tag); //interface count
899 for(s=0;s<inum;s++) {
900 const char*interface = dict_getstr(pool->multinames, swf_GetU30(tag));
901 DEBUG printf(" class %d interface: %s\n", t, interface);
903 cls->iinit = swf_GetU30(tag);
904 cls->traits = traits_parse(tag, pool);
906 for(t=0;t<num_classes;t++) {
907 abc_class_t*cls = (abc_class_t*)dict_getdata(pool->classes, t);
908 cls->static_constructor_index = swf_GetU30(tag); // cinit
909 cls->static_constructor_traits = traits_parse(tag, pool);
911 int num_scripts = swf_GetU30(tag);
912 DEBUG printf("%d scripts\n", num_scripts);
913 for(t=0;t<num_scripts;t++) {
914 int init = swf_GetU30(tag);
915 dict_t*traits = traits_parse(tag, pool); //TODO: store
918 int num_method_bodies = swf_GetU30(tag);
919 DEBUG printf("%d method bodies\n", num_method_bodies);
920 for(t=0;t<num_method_bodies;t++) {
921 int methodnr = swf_GetU30(tag);
922 if(methodnr >= pool->methods->num) {
923 printf("Invalid method number: %d\n", methodnr);
926 abc_method_t*m = (abc_method_t*)dict_getdata(pool->methods, methodnr);
927 abc_code_t*c = malloc(sizeof(abc_code_t));
928 memset(c, 0, sizeof(abc_code_t));
929 c->max_stack = swf_GetU30(tag);
930 c->local_count = swf_GetU30(tag);
931 c->init_scope_depth = swf_GetU30(tag);
932 c->max_scope_depth = swf_GetU30(tag);
933 int code_length = swf_GetU30(tag);
935 m->method_body_index = t;
937 c->tag = swf_InsertTag(0,0);
939 swf_CopyData(c->tag, tag, code_length);
941 int exception_count = swf_GetU30(tag);
943 for(s=0;s<exception_count;s++) {
944 swf_GetU30(tag); //from
945 swf_GetU30(tag); //to
946 swf_GetU30(tag); //target
947 swf_GetU30(tag); //exc_type
948 swf_GetU30(tag); //var_name
950 c->traits = traits_parse(tag, pool);
954 dump_traits("<method body trait>", c->traits, pool);
956 DEBUG printf("method_body %d) (method %d), %d bytes of code", t, methodnr, code_length);
957 int r,l = code_length>32?32:code_length;
959 DEBUG printf("%02x ", c->tag->data[r]);
963 dict_append(pool->method_bodies, 0, c);
965 if(tag->len - tag->pos) {
966 printf("%d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
970 swf_SetTagPos(tag, classes_pos);
971 for(t=0;t<num_classes;t++) {
972 abc_class_t*cls = (abc_class_t*)dict_getdata(pool->classes, t);
974 const char* classname = dict_getstr(pool->multinames, swf_GetU30(tag));
975 const char* supername = dict_getstr(pool->multinames, swf_GetU30(tag));
976 cls->name = classname;
977 U8 flags = swf_GetU8(tag);
980 cls->ns_index = swf_GetU30(tag);
981 ns = dict_getstr(pool->namespaces, cls->ns_index);
983 printf("class %s extends %s, %s, flags=%02x\n", classname, supername, ns, flags);
986 int num_interfaces = swf_GetU30(tag); //interface count
988 for(s=0;s<num_interfaces;s++) {
989 swf_GetU30(tag); // multiname index TODO
991 cls->iinit = swf_GetU30(tag);
992 dump_method(" ","constructor", classname, cls->iinit, pool);
993 cls->traits = traits_parse(tag, pool);
997 dump_traits(" ",cls->traits, pool);
999 dump_method(" ","staticconstructor", "", cls->static_constructor_index, pool);
1000 dump_traits(" ", cls->static_constructor_traits, pool);
1004 for(t=0;t<num_classes;t++) {
1006 swf_GetU30(tag); // cindex
1007 traits_parse(tag, pool); // TODO: free
1009 int num_scripts2 = swf_GetU30(tag);
1011 for(t=0;t<num_scripts2;t++) {
1012 int init = swf_GetU30(tag);
1013 dump_method("","initmethod", "init", init, pool);
1014 dict_t*traits = traits_parse(tag, pool);
1017 dump_traits("", traits, pool);
1021 static int registerNameSpace(abc_file_t*file, U8 access, char*name) {
1022 if(access==0) { // autodetect access
1023 char*n = strdup(name);
1025 char*bracket = strchr(n, ']');
1029 name += (bracket-n)+1;
1030 if(!strcmp(a, "")) access=0x16;
1031 else if(!strcmp(a, "package")) access=0x16;
1032 else if(!strcmp(a, "packageinternal")) access=0x17;
1033 else if(!strcmp(a, "protected")) access=0x18;
1034 else if(!strcmp(a, "explicit")) access=0x19;
1035 else if(!strcmp(a, "staticprotected")) access=0x1a;
1036 else if(!strcmp(a, "private")) access=0x05;
1038 fprintf(stderr, "Undefined access level: [%s]\n", a);
1048 for(t=0;t<file->namespaces->num;t++) {
1049 const char*name2 = dict_getstr(file->namespaces, t);
1050 U8 access2 = (U8)(ptroff_t)dict_getdata(file->namespaces, t);
1051 if(access == access2 && !strcmp(name, name2)) {
1055 dict_update(file->strings, name, 0);
1056 return dict_append(file->namespaces, name, (void*)(ptroff_t)access);
1058 int abc_RegisterNameSpace(abc_file_t*file, char*name) {
1059 return registerNameSpace(file, 0x08, name);
1061 int abc_RegisterPackageNameSpace(abc_file_t*file, char*name) {
1062 return registerNameSpace(file, 0x16 , name);
1064 int abc_RegisterPackageInternalNameSpace(abc_file_t*file, char*name) {
1065 return registerNameSpace(file, 0x17, name);
1067 int abc_RegisterProtectedNameSpace(abc_file_t*file, char*name) {
1068 return registerNameSpace(file, 0x18, name);
1070 int abc_RegisterExplicitNameSpace(abc_file_t*file, char*name) {
1071 return registerNameSpace(file, 0x19, name);
1073 int abc_RegisterStaticProtectedNameSpace(abc_file_t*file, char*name) {
1074 return registerNameSpace(file, 0x1a, name);
1076 int abc_RegisterPrivateNameSpace(abc_file_t*file, char*name) {
1077 return registerNameSpace(file, 0x05, name);
1079 static int multiname_index(abc_file_t*abc, const char*name2)
1083 int pos = dict_find(abc->multinames, name2);
1088 char*n = strdup(name2);
1089 char*p = strchr(n, ':');
1090 char*namespace=0,*name=0;
1099 abc_multiname_t*m = malloc(sizeof(abc_multiname_t));
1100 m->namespace_index = registerNameSpace(abc, 0, namespace);
1101 m->name_index = dict_append_if_new(abc->strings, name, 0);
1102 return dict_append(abc->multinames, name2, m);
1105 abc_class_t* abc_NewClass(abc_file_t*abc, char*classname, char*superclass) {
1106 abc_class_t* c = malloc(sizeof(abc_class_t));
1107 memset(c, 0, sizeof(abc_class_t));
1108 c->index = dict_append(abc->classes, 0, c);
1110 c->name = strdup(classname);
1111 c->classname_index = multiname_index(abc, classname);
1112 c->superclass_index = multiname_index(abc, superclass);
1113 c->ns_index = abc_RegisterProtectedNameSpace(abc, classname);
1115 c->static_constructor_index = -1;
1117 c->traits = dict_new();
1121 abc_code_t* add_method(abc_file_t*abc, abc_class_t*cls, char*returntype, char*name, int num_params, va_list va)
1123 /* construct code (method body) object */
1124 abc_code_t* c = malloc(sizeof(abc_code_t));
1125 memset(c, 0, sizeof(abc_code_t));
1126 c->index = dict_append(abc->method_bodies, 0, c);
1127 c->tag = swf_InsertTag(0,0);
1129 c->traits = dict_new();
1131 /* construct method object */
1132 abc_method_t* m = malloc(sizeof(abc_method_t));
1133 memset(m, 0, sizeof(abc_method_t));
1134 m->param_count = num_params;
1135 m->index = dict_append(abc->methods, 0, m);
1137 m->return_type_index = multiname_index(abc, returntype);
1139 m->return_type_index = 0;
1140 if(num_params>sizeof(m->params)/sizeof(m->params[0])) {
1141 fprintf(stderr, "abc: Too many parameters\n");
1145 for(t=0;t<num_params;t++) {
1146 const char*param = va_arg(va, const char*);
1147 m->params[t] = multiname_index(abc, param);
1150 /* crosslink the two objects */
1151 m->method_body_index = c->index;
1157 abc_code_t* abc_AddConstructor(abc_class_t*cls, char*returntype, int num_params, ...)
1160 va_start(va, num_params);
1161 abc_code_t* c = add_method(cls->abc, cls, returntype, 0, num_params, va);
1163 cls->iinit = c->index;
1167 abc_code_t* abc_AddStaticConstructor(abc_class_t*cls, char*returntype, int num_params, ...)
1170 va_start(va, num_params);
1171 abc_code_t* c = add_method(cls->abc, cls, returntype, 0, num_params, va);
1173 cls->static_constructor_index = c->index;
1177 abc_trait_t*trait_new(int type, int name_index, int data1, int data2, int vindex, int vkind)
1179 abc_trait_t*trait = malloc(sizeof(abc_trait_t));
1180 memset(trait, 0, sizeof(abc_trait_t));
1182 trait->name_index = name_index;
1183 trait->data1 = data1;
1184 trait->data2 = data2;
1185 trait->vindex = vindex;
1186 trait->vkind = vkind;
1190 abc_code_t* abc_AddMethod(abc_class_t*cls, char*returntype, char*name, int num_params, ...)
1192 abc_file_t*abc = cls->abc;
1194 va_start(va, num_params);
1195 abc_code_t* c = add_method(cls->abc, cls, returntype, name, num_params, va);
1197 dict_append(cls->traits, 0, trait_new(TRAIT_METHOD, multiname_index(abc, name), 0, c->method->index, 0, 0));
1201 void abc_AddSlot(abc_class_t*cls, char*name, int slot, char*multiname)
1203 abc_file_t*abc = cls->abc;
1204 int i = multiname_index(abc, name);
1205 dict_append(cls->traits, 0, trait_new(TRAIT_SLOT, i, slot, multiname_index(abc, multiname), 0, 0));
1208 void abc_code_addClassTrait(abc_code_t*code, char*multiname, int slotid, abc_class_t*cls)
1210 abc_file_t*abc = code->abc;
1211 int i = multiname_index(abc, multiname);
1212 abc_trait_t*trait = trait_new(TRAIT_CLASS, i, slotid, cls->index, 0, 0);
1213 dict_append(code->traits, 0, trait);
1216 abc_code_t* abc_AddInitScript(abc_file_t*abc, char*returntype, int num_params, ...)
1219 va_start(va, num_params);
1220 abc_code_t* c = add_method(abc, 0, returntype, 0, num_params, va);
1221 dict_append(abc->scripts, 0, c);
1226 void swf_SetU30(TAG*tag, U32 u)
1229 swf_SetU8(tag, (u&~0x7f?0x80:0) | (u&0x7F));
1233 void swf_SetU30String(TAG*tag, const char*str)
1235 int l = strlen(str);
1237 swf_SetBlock(tag, (void*)str, l);
1240 static void write_traits(abc_file_t*abc, TAG*tag, dict_t*traits)
1242 swf_SetU30(tag, traits->num);
1245 for(s=0;s<traits->num;s++) {
1246 abc_trait_t*trait = (abc_trait_t*)dict_getdata(traits, s);
1247 swf_SetU30(tag, trait->name_index);
1248 swf_SetU8(tag, trait->type);
1249 swf_SetU30(tag, trait->data1);
1250 swf_SetU30(tag, trait->data2);
1251 if(trait->type == 0) { //slot
1252 swf_SetU30(tag, trait->vindex);
1254 swf_SetU8(tag, trait->vkind);
1260 void swf_WriteABC(TAG*tag, abc_file_t*abc)
1264 swf_SetU16(tag, 0x10);
1265 swf_SetU16(tag, 0x2e);
1266 swf_SetU30(tag, abc->ints->num>1?abc->ints->num:0);
1268 swf_SetU30(tag, abc->uints->num>1?abc->uints->num:0);
1270 swf_SetU30(tag, abc->floats->num>1?abc->floats->num:0);
1272 swf_SetU30(tag, abc->strings->num>1?abc->strings->num:0);
1274 for(t=1;t<abc->strings->num;t++) {
1275 swf_SetU30String(tag, dict_getstr(abc->strings, t));
1277 swf_SetU30(tag, abc->namespaces->num>1?abc->namespaces->num:0);
1278 for(t=1;t<abc->namespaces->num;t++) {
1279 U8 type = (U8)(ptroff_t)dict_getdata(abc->namespaces, t);
1280 const char*name = dict_getstr(abc->namespaces, t);
1281 int i = dict_find(abc->strings, name);
1283 fprintf(stderr, "Couldn't find namespace \"%s\" in constant pool\n", name);
1286 swf_SetU8(tag, type);
1289 swf_SetU30(tag, abc->sets->num>1?abc->sets->num:0);
1292 swf_SetU30(tag, abc->multinames->num>1?abc->multinames->num:0);
1294 for(t=1;t<abc->multinames->num;t++) {
1295 abc_multiname_t*m = (abc_multiname_t*)dict_getdata(abc->multinames, t);
1296 swf_SetU8(tag, 0x07);
1297 swf_SetU30(tag, m->namespace_index);
1298 swf_SetU30(tag, m->name_index);
1301 swf_SetU30(tag, abc->methods->num);
1302 for(t=0;t<abc->methods->num;t++) {
1303 abc_method_t*m = (abc_method_t*)dict_getdata(abc->methods, t);
1304 swf_SetU30(tag, m->param_count);
1305 swf_SetU30(tag, m->return_type_index);
1307 for(s=0;s<m->param_count;s++) {
1308 swf_SetU30(tag, m->params[s]);
1310 swf_SetU30(tag, 0); // name
1311 swf_SetU8(tag, 0); //flags
1314 swf_SetU30(tag, 0);//metadata
1316 swf_SetU30(tag, abc->classes->num);
1318 for(t=0;t<abc->classes->num;t++) {
1319 abc_class_t*c = (abc_class_t*)dict_getdata(abc->classes, t);
1320 swf_SetU30(tag, c->classname_index);
1321 swf_SetU30(tag, c->superclass_index);
1323 swf_SetU8(tag, 8); // flags
1324 swf_SetU30(tag, c->ns_index);
1326 swf_SetU30(tag, 0); // no interfaces
1328 fprintf(stderr, "Error: Class %s has no constructor\n", c->name);
1331 swf_SetU30(tag, c->iinit);
1332 write_traits(abc, tag, c->traits);
1334 for(t=0;t<abc->classes->num;t++) {
1335 abc_class_t*c = (abc_class_t*)dict_getdata(abc->classes, t);
1336 if(c->static_constructor_index<0) {
1337 fprintf(stderr, "Error: Class %s has no static constructor\n", c->name);
1340 swf_SetU30(tag, c->static_constructor_index);
1341 write_traits(abc, tag, c->static_constructor_traits);
1344 swf_SetU30(tag, abc->scripts->num);
1345 for(t=0;t<abc->scripts->num;t++) {
1346 abc_code_t*c = (abc_code_t*)dict_getdata(abc->scripts, t);
1347 swf_SetU30(tag, c->index); //!=t!
1348 write_traits(abc, tag, c->traits);
1351 swf_SetU30(tag, abc->method_bodies->num);
1352 for(t=0;t<abc->method_bodies->num;t++) {
1353 abc_code_t*c = (abc_code_t*)dict_getdata(abc->method_bodies, t);
1354 abc_method_t*m = c->method;
1355 swf_SetU30(tag, m->index);
1356 swf_SetU30(tag, c->max_stack);
1357 swf_SetU30(tag, c->local_count);
1358 swf_SetU30(tag, c->init_scope_depth);
1359 swf_SetU30(tag, c->max_scope_depth);
1360 swf_SetU30(tag, c->tag->len);
1361 swf_SetBlock(tag, c->tag->data, c->tag->len);
1362 swf_SetU30(tag, c->exception_count);
1363 swf_SetU8(tag, 0); // no traits
1367 #include "swfabc_ops.c"
1369 void swf_AddButtonLinks(TAG*tag)
1371 abc_file_t*abc = abc_file_new();
1374 abc_class_t*maintimeline = abc_NewClass(abc, "buttonmitlink_fla:MainTimeline", "flash.display:MovieClip");
1376 c = abc_AddStaticConstructor(maintimeline, 0, 0);
1379 c->init_scope_depth = 9;
1380 c->max_scope_depth = 10;
1386 c = abc_AddMethod(maintimeline, 0, "[packageinternal]buttonmitlink_fla:frame1", 0);
1389 c->init_scope_depth = 10;
1390 c->max_scope_depth = 11;
1395 abc_AddSlot(maintimeline, ":MyButton1", 0, "flash.display:SimpleButton");
1396 abc_AddSlot(maintimeline, ":MyButton2", 0, "flash.display:SimpleButton");
1398 c = abc_AddMethod(maintimeline, ":void", ":gotoPage2", 1, "flash.events:MouseEvent");
1401 c->init_scope_depth = 10;
1402 c->max_scope_depth = 11;
1405 abc_findpropstrict(c, "flash.net:navigateToURL");
1406 abc_findpropstrict(c, "flash.net:URLRequest");
1407 abc_pushstring(c, "http://www.quiss.org");
1408 abc_constructprop(c, "flash.net:URLRequest", 1);
1409 abc_callpropvoid(c, "flash.net:navigateToURL", 1);
1412 c = abc_AddMethod(maintimeline, ":void", ":gotoPage1", 1, "flash.events:MouseEvent");
1415 c->init_scope_depth = 10;
1416 c->max_scope_depth = 11;
1419 abc_findpropstrict(c,"flash.net:navigateToURL");
1420 abc_findpropstrict(c,"flash.net:URLRequest");
1421 abc_pushstring(c,"http://www.google.com/");
1422 abc_constructprop(c,"flash.net:URLRequest", 1);
1423 abc_callpropvoid(c,"flash.net:navigateToURL", 1);
1426 c = abc_AddConstructor(maintimeline, 0, 0);
1429 c->init_scope_depth = 10;
1430 c->max_scope_depth = 11;
1436 abc_constructsuper(c,0);
1437 abc_findpropstrict(c,":addFrameScript");
1438 abc_pushbyte(c,0x00);
1439 abc_getlex(c,"[packageinternal]buttonmitlink_fla:frame1");
1440 abc_callpropvoid(c,":addFrameScript",2);
1442 abc_getlex(c,":MyButton1");
1443 abc_getlex(c,"flash.events:MouseEvent");
1444 abc_getproperty(c, ":CLICK");
1445 abc_getlex(c, ":gotoPage1");
1446 abc_callpropvoid(c, ":addEventListener" ,2);
1448 abc_getlex(c,":MyButton2");
1449 abc_getlex(c,"flash.events:MouseEvent");
1450 abc_getproperty(c, ":CLICK");
1451 abc_getlex(c,":gotoPage2");
1452 abc_callpropvoid(c,":addEventListener",2);
1456 c = abc_AddInitScript(abc, 0, 0);
1459 c->init_scope_depth = 1;
1460 c->max_scope_depth = 9;
1463 abc_getscopeobject(c, 0);
1464 abc_getlex(c,":Object");
1466 abc_getlex(c,"flash.events:EventDispatcher");
1468 abc_getlex(c,"flash.display:DisplayObject");
1470 abc_getlex(c,"flash.display:InteractiveObject");
1472 abc_getlex(c,"flash.display:DisplayObjectContainer");
1474 abc_getlex(c,"flash.display:Sprite");
1476 abc_getlex(c,"flash.display:MovieClip");
1478 abc_getlex(c,"flash.display:MovieClip");
1479 abc_newclass(c,maintimeline);
1487 abc_initproperty(c,"buttonmitlink_fla:MainTimeline");
1490 abc_code_addClassTrait(c, "buttonmitlink_fla:MainTimeline", 1, maintimeline);
1492 swf_WriteABC(tag, abc);