+ int num_methods = swf_GetU30(tag);
+ DEBUG printf("%d methods\n", num_methods);
+ for(t=0;t<num_methods;t++) {
+ abc_method_t*m = malloc(sizeof(abc_method_t));
+ memset(m, 0, sizeof(*m));
+ int param_count = swf_GetU30(tag);
+ int return_type_index = swf_GetU30(tag);
+ m->return_type = multiname_lookup(pool, return_type_index);
+ m->index = t;
+
+ int s;
+ for(s=0;s<param_count;s++) {
+ int type_index = swf_GetU30(tag);
+ list_append(&m->parameters, multiname_lookup(pool, type_index));
+ }
+
+ int namenr = swf_GetU30(tag);
+ m->name = "";
+ if(namenr)
+ m->name = dict_getstr(pool->strings, namenr);
+
+ m->flags = swf_GetU8(tag);
+
+ DEBUG printf("method %d) %s flags=%02x\n", t, params_to_string(m->parameters), m->flags);
+
+ if(m->flags&0x08) {
+ /* optional parameters */
+ int num = swf_GetU30(tag);
+ int s;
+ for(s=0;s<num;s++) {
+ int val = swf_GetU30(tag);
+ U8 kind = swf_GetU8(tag); // specifies index type for "val"
+ }
+ }
+ if(m->flags&0x80) {
+ /* debug information- not used by avm2 */
+ abc_multiname_list_t*l = m->parameters;
+ while(l) {
+ m->name = dict_getstr(pool->strings, swf_GetU30(tag));
+ l = l->next;
+ }
+ }
+ dict_append(pool->methods, m->name, m);
+ }
+
+ parse_metadata(tag, pool);
+
+ /* skip classes, and scripts for now, and do the real parsing later */
+ int num_classes = swf_GetU30(tag);
+ int classes_pos = tag->pos;
+ DEBUG printf("%d classes\n", num_classes);
+ for(t=0;t<num_classes;t++) {
+ abc_class_t*cls = malloc(sizeof(abc_class_t));
+ memset(cls, 0, sizeof(abc_class_t));
+ dict_append(pool->classes, 0, cls);
+
+ DEBUG printf("class %d\n", t);
+ swf_GetU30(tag); //classname
+ swf_GetU30(tag); //supername
+ cls->flags = swf_GetU8(tag);
+ if(cls->flags&8)
+ swf_GetU30(tag); //protectedNS
+ int s;
+ int inum = swf_GetU30(tag); //interface count
+ abc_multiname_list_t*list = 0;
+ for(s=0;s<inum;s++) {
+ int interface_index = swf_GetU30(tag);
+ const char*interface = dict_getstr(pool->multinames, interface_index);
+ abc_multiname_t* m = (abc_multiname_t*)dict_getdata(pool->multinames, interface_index);
+ list_append(&list, m);
+ DEBUG printf(" class %d interface: %s\n", t, interface);
+ }
+
+ cls->iinit = swf_GetU30(tag);
+ cls->traits = traits_parse(tag, pool);
+ }
+ for(t=0;t<num_classes;t++) {
+ abc_class_t*cls = (abc_class_t*)dict_getdata(pool->classes, t);
+ cls->static_constructor_index = swf_GetU30(tag); // cinit
+ cls->static_constructor_traits = traits_parse(tag, pool);
+ }
+ int num_scripts = swf_GetU30(tag);
+ DEBUG printf("%d scripts\n", num_scripts);
+ for(t=0;t<num_scripts;t++) {
+ int init = swf_GetU30(tag);
+ dict_t*traits = traits_parse(tag, pool); //TODO: store
+ }
+
+ int num_method_bodies = swf_GetU30(tag);
+ DEBUG printf("%d method bodies\n", num_method_bodies);
+ for(t=0;t<num_method_bodies;t++) {
+ int methodnr = swf_GetU30(tag);
+ if(methodnr >= pool->methods->num) {
+ printf("Invalid method number: %d\n", methodnr);
+ return 0;
+ }
+ abc_method_t*m = (abc_method_t*)dict_getdata(pool->methods, methodnr);
+ abc_method_body_t*c = malloc(sizeof(abc_method_body_t));
+ memset(c, 0, sizeof(abc_method_body_t));
+ c->max_stack = swf_GetU30(tag);
+ c->local_count = swf_GetU30(tag);
+ c->init_scope_depth = swf_GetU30(tag);
+ c->max_scope_depth = swf_GetU30(tag);
+ int code_length = swf_GetU30(tag);
+ c->method = m;
+ m->method_body_index = t;
+
+ c->tag = swf_InsertTag(0,0);
+
+ swf_CopyData(c->tag, tag, code_length);
+
+ int exception_count = swf_GetU30(tag);
+ int s;
+ for(s=0;s<exception_count;s++) {
+ swf_GetU30(tag); //from
+ swf_GetU30(tag); //to
+ swf_GetU30(tag); //target
+ swf_GetU30(tag); //exc_type
+ swf_GetU30(tag); //var_name
+ }
+ c->traits = traits_parse(tag, pool);
+ if(!c->traits) {
+ fprintf(stderr, "Can't parse code traits\n");
+ return 0;
+ }
+ DEBUG printf("method_body %d) (method %d), %d bytes of code", t, methodnr, code_length);
+ int r,l = code_length>32?32:code_length;
+ for(r=0;r<l;r++) {
+ DEBUG printf("%02x ", c->tag->data[r]);
+ }
+ DEBUG printf("\n");
+
+ dict_append(pool->method_bodies, 0, c);
+ }
+ if(tag->len - tag->pos) {
+ fprintf(stderr, "%d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
+ return 0;
+ }
+
+ swf_SetTagPos(tag, classes_pos);
+ for(t=0;t<num_classes;t++) {
+ abc_class_t*cls = (abc_class_t*)dict_getdata(pool->classes, t);
+
+ int classname_index = swf_GetU30(tag);
+ int superclass_index = swf_GetU30(tag);
+ cls->classname = dict_getstr(pool->multinames, classname_index);
+ cls->superclass = dict_getstr(pool->multinames, superclass_index);
+ cls->flags = swf_GetU8(tag);
+ const char*ns = "";
+ if(cls->flags&8) {
+ int ns_index = swf_GetU30(tag);
+ cls->protectedNS = dict_getstr(pool->namespaces, ns_index);
+ }
+
+ if(cls->flags&1) printf("sealed ");
+ if(cls->flags&2) printf("final ");
+ if(cls->flags&4) printf("interface ");
+ if(cls->flags&8) {
+ printf("protectedNS<%s> ", cls->protectedNS);
+ }
+
+ printf("class %s", cls->classname);
+ if(cls->superclass && cls->superclass[0]) {
+ printf(" extends %s", cls->superclass);
+ abc_multiname_list_t*ilist = cls->interfaces;
+ if(ilist)
+ printf(" implements");
+ while(ilist) {
+ char*s = multiname_to_string(ilist->abc_multiname);
+ printf(" %d", s);
+ free(s);
+ ilist = ilist->next;
+ }
+ ilist->next;
+
+ }
+ if(cls->flags&0xf0)
+ printf("extra flags=%02x\n", cls->flags&0xf0);
+ printf("{\n");
+
+ dump_method(" ","staticconstructor", "", cls->static_constructor_index, pool);
+ dump_traits(" ", cls->static_constructor_traits, pool);
+
+ int num_interfaces = swf_GetU30(tag); //interface count
+ int s;
+ for(s=0;s<num_interfaces;s++) {
+ swf_GetU30(tag); // multiname index TODO
+ }
+ cls->iinit = swf_GetU30(tag);
+ dump_method(" ","constructor", cls->classname, cls->iinit, pool);
+ cls->traits = traits_parse(tag, pool);
+ if(!cls->traits) {
+ fprintf(stderr, "Can't parse class traits\n");
+ return 0;
+ }
+ dump_traits(" ",cls->traits, pool);
+
+ printf("}\n");
+ }
+ for(t=0;t<num_classes;t++) {
+ /* SKIP */
+ swf_GetU30(tag); // cindex
+ traits_parse(tag, pool); // TODO: free
+ }
+ int num_scripts2 = swf_GetU30(tag);
+ printf("\n");
+ for(t=0;t<num_scripts2;t++) {
+ int init = swf_GetU30(tag);
+ abc_method_t*m = (abc_method_t*)dict_getdata(pool->methods, init);
+
+ abc_script_t*s = malloc(sizeof(abc_script_t));
+ memset(s, 0, sizeof(abc_script_t));
+ s->method = m;
+ s->traits = traits_parse(tag, pool);
+ dict_append(pool->scripts, 0, s);
+ if(!s->traits) {
+ fprintf(stderr, "Can't parse script traits\n");
+ return 0;
+ }
+ dump_method("","initmethod", "init", init, pool);
+ dump_traits("", s->traits, pool);
+ }
+ return pool;
+}
+
+void swf_WriteABC(TAG*abctag, void*code)
+{
+ abc_file_t*pool = (abc_file_t*)code;
+
+ TAG*tmp = swf_InsertTag(0,0);
+ TAG*tag = tmp;
+ int t;
+
+ swf_SetU30(tag, pool->methods->num);
+ for(t=0;t<pool->methods->num;t++) {
+ abc_method_t*m = (abc_method_t*)dict_getdata(pool->methods, t);
+ int n = 0;
+ abc_multiname_list_t*l = m->parameters;
+ swf_SetU30(tag, list_length(m->parameters));
+ swf_SetU30(tag, register_multiname(pool, m->return_type));
+ int s;
+ while(l) {
+ swf_SetU30(tag, register_multiname(pool, l->abc_multiname));
+ l = l->next;
+ }
+ swf_SetU30(tag, 0); // name
+ swf_SetU8(tag, 0); //flags
+ }
+
+ swf_SetU30(tag, 0);//metadata
+
+ swf_SetU30(tag, pool->classes->num);
+
+ for(t=0;t<pool->classes->num;t++) {
+ abc_class_t*c = (abc_class_t*)dict_getdata(pool->classes, t);
+
+ int classname_index = multiname_index(pool, c->classname);
+ int superclass_index = multiname_index(pool, c->superclass);
+
+ swf_SetU30(tag, classname_index);
+ swf_SetU30(tag, superclass_index);
+
+ swf_SetU8(tag, c->flags); // flags
+ if(c->flags&0x08) {
+ abc_namespace_t*ns = abc_protectednamespace(pool, c->protectedNS);
+ int ns_index = register_namespace(pool, ns);
+ swf_SetU30(tag, ns_index);
+ }
+
+ swf_SetU30(tag, 0); // no interfaces
+ if(c->iinit<0) {
+ fprintf(stderr, "Error: Class %s has no constructor\n", c->classname);
+ return;
+ }
+ swf_SetU30(tag, c->iinit);
+ write_traits(pool, tag, c->traits);
+ }
+ for(t=0;t<pool->classes->num;t++) {
+ abc_class_t*c = (abc_class_t*)dict_getdata(pool->classes, t);
+ if(c->static_constructor_index<0) {
+ fprintf(stderr, "Error: Class %s has no static constructor\n", c->classname);
+ return;
+ }
+ swf_SetU30(tag, c->static_constructor_index);
+ write_traits(pool, tag, c->static_constructor_traits);
+ }
+
+ swf_SetU30(tag, pool->scripts->num);
+ for(t=0;t<pool->scripts->num;t++) {
+ abc_script_t*s = (abc_script_t*)dict_getdata(pool->scripts, t);
+ swf_SetU30(tag, s->method->index); //!=t!
+ write_traits(pool, tag, s->traits);
+ }
+
+ swf_SetU30(tag, pool->method_bodies->num);
+ for(t=0;t<pool->method_bodies->num;t++) {
+ abc_method_body_t*c = (abc_method_body_t*)dict_getdata(pool->method_bodies, t);
+ abc_method_t*m = c->method;
+ swf_SetU30(tag, m->index);
+ swf_SetU30(tag, c->max_stack);
+ swf_SetU30(tag, c->local_count);
+ swf_SetU30(tag, c->init_scope_depth);
+ swf_SetU30(tag, c->max_scope_depth);
+ swf_SetU30(tag, c->tag->len);
+ swf_SetBlock(tag, c->tag->data, c->tag->len);
+ swf_SetU30(tag, c->exception_count);
+ write_traits(pool, tag, c->traits);
+ }
+
+ // --- start to write real tag --
+
+ tag = abctag;
+
+ for(t=1;t<pool->multinames->num;t++) {
+ abc_multiname_t*m = (abc_multiname_t*)dict_getdata(pool->multinames, t);
+ if(m->ns) {
+ register_namespace(pool, m->ns);
+ }
+ if(m->namespace_set_name) {
+ // FIXME
+ }
+ if(m->name)
+ dict_append_if_new(pool->strings, m->name, 0);
+ }
+ for(t=1;t<pool->namespaces->num;t++) {
+ char*namespace_name = (char*)dict_getstr(pool->namespaces, t);
+ dict_append_if_new(pool->strings, namespace_name, 0);
+ }
+
+ swf_SetU32(tag, 1); // flags
+ swf_SetU8(tag, 0); //classname
+
+ swf_SetU16(tag, 0x10); //version
+ swf_SetU16(tag, 0x2e);
+
+ swf_SetU30(tag, pool->ints->num>1?pool->ints->num:0);
+ // ...
+ swf_SetU30(tag, pool->uints->num>1?pool->uints->num:0);
+ // ...
+ swf_SetU30(tag, pool->floats->num>1?pool->floats->num:0);
+ // ...
+ swf_SetU30(tag, pool->strings->num>1?pool->strings->num:0);
+ for(t=1;t<pool->strings->num;t++) {
+ swf_SetU30String(tag, dict_getstr(pool->strings, t));
+ }
+ swf_SetU30(tag, pool->namespaces->num>1?pool->namespaces->num:0);
+ for(t=1;t<pool->namespaces->num;t++) {
+ abc_namespace_t*ns= (abc_namespace_t*)dict_getdata(pool->namespaces, t);
+ const char*name = ns->name;
+ int i = dict_find(pool->strings, name);
+ if(i<0) {
+ fprintf(stderr, "Couldn't find namespace \"%s\" in constant pool\n", name);
+ return;
+ }
+ swf_SetU8(tag, ns->access);
+ swf_SetU30(tag, i);
+ }
+ swf_SetU30(tag, pool->sets->num>1?pool->sets->num:0);
+ // ...
+
+ swf_SetU30(tag, pool->multinames->num>1?pool->multinames->num:0);
+ // ...
+ for(t=1;t<pool->multinames->num;t++) {
+ abc_multiname_t*m = (abc_multiname_t*)dict_getdata(pool->multinames, t);
+ swf_SetU8(tag, m->type);
+
+ if(m->ns) {
+ assert(m->type==0x07 || m->type==0x0d);
+ /* fixme: might find wrong version */
+ int i = dict_find2(pool->namespaces, m->ns->name, (void*)(ptroff_t)m->ns);
+ if(i<0) fprintf(stderr, "internal error: unregistered namespace %02x %s %s\n", m->ns->access, access2str(m->ns->access), m->ns->name);
+ swf_SetU30(tag, i);
+ }
+ if(m->namespace_set_name) {
+ assert(m->type==0x09 || m->type==0x0e || m->type==0x1c || m->type==0x1b);
+ int i = dict_find(pool->namespace_sets, m->namespace_set_name);
+ if(i<0) fprintf(stderr, "internal error: unregistered namespace set\n");
+ swf_SetU30(tag, i);
+ }
+ if(m->name) {
+ assert(m->type==0x09 || m->type==0x0e || m->type==0x07 || m->type==0x0d || m->type==0x0f || m->type==0x10);
+ int i = dict_find(pool->strings, m->name);
+ if(i<0) fprintf(stderr, "internal error: unregistered name\n");
+ swf_SetU30(tag, i);
+ }
+ }
+
+ swf_SetBlock(tag, tmp->data, tmp->len);
+
+ swf_DeleteTag(0, tmp);
+}
+
+#include "swfabc_ops.c"
+
+void swf_AddButtonLinks(SWF*swf, char stop_each_frame)
+{
+ int num_frames = 0;
+ int has_buttons = 0;
+ TAG*tag=swf->firstTag;
+ while(tag) {
+ if(tag->id == ST_SHOWFRAME)
+ num_frames++;
+ if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2)
+ has_buttons = 1;
+ tag = tag->next;
+ }
+
+ abc_file_t*file = abc_file_new();
+ abc_method_body_t*c = 0;
+
+ abc_class_t*cls = abc_class_new(file, "rfx::MainTimeline", "flash.display::MovieClip");
+ abc_class_protectedNS(cls, "rfx:MainTimeline");
+
+ TAG*abctag = swf_InsertTagBefore(swf, swf->firstTag, ST_DOABC);
+
+ tag = swf_InsertTag(abctag, ST_SYMBOLCLASS);
+ swf_SetU16(tag, 1);
+ swf_SetU16(tag, 0);
+ swf_SetString(tag, "rfx.MainTimeline");
+
+ c = abc_class_staticconstructor(cls, 0, 0);
+ c->max_stack = 1;