+ int num_uints = swf_GetU30(tag);
+ DEBUG printf("%d uints\n", num_uints);
+ for(t=1;t<num_uints;t++) {
+ U32 v = swf_GetS30(tag);
+ DEBUG printf("uint %d) %d\n", t, v);
+ dict_append(pool->uints, 0, (void*)(ptroff_t)v);
+ }
+
+ int num_floats = swf_GetU30(tag);
+ DEBUG printf("%d floats\n", num_floats);
+ for(t=1;t<num_floats;t++) {
+ double d = swf_GetD64(tag);
+ DEBUG printf("float %d) %f\n", t, d);
+ dict_append(pool->floats, 0, 0);
+ }
+
+ int num_strings = swf_GetU30(tag);
+ DEBUG printf("%d strings\n", num_strings);
+ for(t=1;t<num_strings;t++) {
+ int len = swf_GetU30(tag);
+ char*s = malloc(len+1);
+ swf_GetBlock(tag, s, len);
+ s[len] = 0;
+ dict_append(pool->strings, s, 0);
+ DEBUG printf("%d) \"%s\"\n", t, pool->strings->d[t].name);
+ }
+ int num_namespaces = swf_GetU30(tag);
+ DEBUG printf("%d namespaces\n", num_namespaces);
+ for(t=1;t<num_namespaces;t++) {
+ U8 type = swf_GetU8(tag);
+ int namenr = swf_GetU30(tag);
+ const char*name = dict_getstr(pool->strings, namenr);
+ abc_namespace_t*ns = malloc(sizeof(abc_namespace_t));
+ memset(ns, 0, sizeof(abc_namespace_t));
+ ns->access = type;
+ ns->name = strdup(name);
+ dict_append(pool->namespaces, name, ns);
+ int w = 0;
+ DEBUG w=1;
+ if(w) {
+ if(type==0x08) printf("Namespace %s\n", name);
+ else if(type==0x16) printf("PackageNamespace %s\n", name);
+ else if(type==0x17) printf("PackageInternalNs %s\n", name);
+ else if(type==0x18) printf("ProtectedNamespace %s\n", name);
+ else if(type==0x19) printf("ExplicitNamespace %s\n", name);
+ else if(type==0x1A) printf("StaticProtectedNs %s\n", name);
+ else if(type==0x05) printf("PrivateNs %s\n", name);
+ else {
+ printf("Undefined namespace type\n");
+ return 0;
+ }
+ }
+ }
+ int num_sets = swf_GetU30(tag);
+ DEBUG printf("%d namespace sets\n", num_sets);
+ for(t=1;t<num_sets;t++) {
+ int count = swf_GetU30(tag);
+ int s;
+ const char**name = malloc(sizeof(const char*)*count);
+ int l = 0;
+ for(s=0;s<count;s++) {
+ int nsnr = swf_GetU30(tag);
+ name[s] = dict_getstr(pool->namespaces, nsnr);
+ l += strlen(name[s])+1;
+ }
+ char*desc = malloc(l+16);
+ strcpy(desc, "{");
+ for(s=0;s<count;s++) {
+ strcat(desc, name[s]);
+ strcat(desc, ",");
+ }
+ strcat(desc, "}");
+ dict_append(pool->namespace_sets, desc, 0);
+ DEBUG printf("set %d) %s\n", t, desc);
+ }
+
+ int num_multinames = swf_GetU30(tag);
+ DEBUG printf("%d multinames\n", num_multinames);
+ for(t=1;t<num_multinames;t++) {
+ abc_multiname_t*m = malloc(sizeof(abc_multiname_t));
+ memset(m, 0, sizeof(abc_multiname_t));
+
+ m->type = swf_GetU8(tag);
+
+ if(m->type==0x07 || m->type==0x0d) {
+ int nr1 = swf_GetU30(tag);
+ m->ns = (abc_namespace_t*)dict_getdata(pool->namespaces, nr1);
+ int name_index = swf_GetU30(tag);
+ m->name = dict_getstr(pool->strings, name_index);
+ } else if(m->type==0x0f || m->type==0x10) {
+ int name_index = swf_GetU30(tag);
+ m->name = dict_getstr(pool->strings, name_index);
+ } else if(m->type==0x11 || m->type==0x12) {
+ } else if(m->type==0x09 || m->type==0x0e) {
+ int name_index = swf_GetU30(tag);
+ int namespace_set_index = swf_GetU30(tag);
+ m->name = dict_getstr(pool->strings, name_index);
+ m->namespace_set_name = dict_getstr(pool->namespace_sets, namespace_set_index);
+ } else if(m->type==0x1b || m->type==0x1c) {
+ int namespace_set_index = swf_GetU30(tag);
+ m->namespace_set_name = dict_getstr(pool->namespace_sets, namespace_set_index);
+ } else {
+ printf("can't parse type %d multinames yet\n", m->type);
+ return 0;
+ }
+ char*mname = multiname_to_string(m);
+ DEBUG printf("multiname %d) %s\n", t, mname);
+ dict_append(pool->multinames, mname, m);
+ free(mname);
+ }
+
+ 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);
+