fixed format warnings
[swftools.git] / lib / as3 / abc.c
index 4ab4555..7e11e1c 100644 (file)
@@ -26,6 +26,7 @@
 #include "../rfxswf.h"
 #include "../q.h"
 #include "abc.h"
+#include "assets.h"
 
 char stringbuffer[2048];
 
@@ -49,12 +50,12 @@ static void params_dump(FILE*fo, multiname_list_t*l, constant_list_t*o)
     fprintf(fo, "(");
     while(l) {
         char*s = multiname_tostring(l->multiname);
-        fprintf(fo, s);
+        fprintf(fo, "%s", s);
         free(s);
         if(i>=n-no) {
             s = constant_tostring(o->constant);
             fprintf(fo, " = ");
-            fprintf(fo, s);
+            fprintf(fo, "%s", s);
             free(s);
             o = o->next;
         }
@@ -74,7 +75,7 @@ static void parse_metadata(TAG*tag, abc_file_t*file, pool_t*pool)
     int t;
     int num_metadata = swf_GetU30(tag);
 
-    DEBUG printf("%d metadata\n");
+    DEBUG printf("%d metadata\n", num_metadata);
     for(t=0;t<num_metadata;t++) {
         const char*entry_name = pool_lookup_string(pool, swf_GetU30(tag));
         int num = swf_GetU30(tag);
@@ -119,7 +120,8 @@ abc_file_t*abc_file_new()
 abc_class_t* abc_class_new(abc_file_t*file, multiname_t*classname, multiname_t*superclass) {
     
     NEW(abc_class_t,c);
-    array_append(file->classes, NO_KEY, c);
+    if(file)
+        array_append(file->classes, NO_KEY, c);
 
     c->file = file;
     c->classname = multiname_clone(classname);
@@ -156,6 +158,20 @@ void abc_class_add_interface(abc_class_t*c, multiname_t*interface)
 {
     list_append(c->interfaces, multiname_clone(interface));
 }
+char*abc_class_fullname(abc_class_t*cls)
+{
+    const char*package = cls->classname->ns->name;
+    const char*name = cls->classname->name;
+    int l1 = strlen(package);
+    int l2 = strlen(name);
+    char*fullname = malloc(l1+l2+2);
+    if(l1) {
+       memcpy(fullname, package, l1);
+       fullname[l1++]='.';
+    }
+    memcpy(fullname+l1, name, l2+1);
+    return fullname;
+}
 
 void abc_method_init(abc_method_t*m, abc_file_t*file, multiname_t*returntype, char body)
 {
@@ -308,7 +324,7 @@ void abc_method_body_addClassTrait(abc_method_body_t*code, char*multiname, int s
 
 /* notice: traits of a method (body) belonging to an init script
    and traits of the init script are *not* the same thing */
-int abc_initscript_addClassTrait(abc_script_t*script, multiname_t*multiname, abc_class_t*cls)
+trait_t* abc_initscript_addClassTrait(abc_script_t*script, multiname_t*multiname, abc_class_t*cls)
 {
     abc_file_t*file = script->file;
     multiname_t*m = multiname_clone(multiname);
@@ -316,7 +332,7 @@ int abc_initscript_addClassTrait(abc_script_t*script, multiname_t*multiname, abc
     trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0);
     trait->cls = cls;
     list_append(script->traits, trait);
-    return slotid;
+    return trait;
 }
 
 abc_script_t* abc_initscript(abc_file_t*file)
@@ -345,7 +361,7 @@ static void dump_method(FILE*fo, const char*prefix,
     if(m->return_type)
         return_type = multiname_tostring(m->return_type);
     else
-        return_type = strdup("void");
+        return_type = strdup("*");
 
     fprintf(fo, "%s", prefix);
     fprintf(fo, "%s %s ", attr, type);
@@ -450,7 +466,7 @@ static trait_list_t* traits_parse(TAG*tag, pool_t*pool, abc_file_t*file)
        } else if(kind == TRAIT_CLASS) { // class
            trait->slot_id = swf_GetU30(tag);
            trait->cls = (abc_class_t*)array_getvalue(file->classes, swf_GetU30(tag));
-           DEBUG printf("  class %s %d %d\n", name, trait->slot_id, trait->cls);
+           DEBUG printf("  class %s %d %08x\n", name, trait->slot_id, (int)trait->cls);
        } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
            trait->slot_id = swf_GetU30(tag);
             trait->type_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
@@ -619,7 +635,7 @@ void* swf_DumpABC(FILE*fo, void*code, char*prefix)
         int s;
         array_t*items = (array_t*)array_getvalue(file->metadata, t);
         for(s=0;s<items->num;s++) {
-            fprintf(fo, "%s#  %s=%s\n", prefix, array_getkey(items, s), array_getvalue(items,s));
+            fprintf(fo, "%s#  %s=%s\n", prefix, (char*)array_getkey(items, s), (char*)array_getvalue(items,s));
         }
         fprintf(fo, "%s#\n", prefix);
     }
@@ -647,6 +663,8 @@ void* swf_DumpABC(FILE*fo, void*code, char*prefix)
             char*supername = multiname_tostring(cls->superclass);
             fprintf(fo, " extends %s", supername);
             free(supername);
+        }
+        if(cls->interfaces) {
             multiname_list_t*ilist = cls->interfaces;
             if(ilist)
                 fprintf(fo, " implements");
@@ -675,6 +693,11 @@ void* swf_DumpABC(FILE*fo, void*code, char*prefix)
            dump_method(fo, prefix2, "", "constructor", n, cls->constructor, file, methods_seen);
         free(n);
        traits_dump(fo, prefix2,cls->traits, file, methods_seen);
+
+       if(cls->asset) {
+           swf_DumpAsset(fo, cls->asset, prefix2);
+       }
+
         fprintf(fo, "%s}\n", prefix);
     }
     fprintf(fo, "%s\n", prefix);
@@ -752,9 +775,9 @@ void* swf_ReadABC(TAG*tag)
 
        m->flags = swf_GetU8(tag);
        
-        DEBUG printf("method %d) %s ", m->name);
+        DEBUG printf("method %d) %s ", t, m->name);
         DEBUG params_dump(stdout, m->parameters, m->optional_parameters);
-        DEBUG printf("flags=%02x\n", t, m->flags);
+        DEBUG printf("flags=%02x\n", m->flags);
 
         if(m->flags&0x08) {
             m->optional_parameters = list_new();
@@ -1128,6 +1151,9 @@ static pool_t*writeABC(TAG*abctag, void*code, pool_t*pool)
     swf_SetU30(tag, file->scripts->num);
     for(t=0;t<file->scripts->num;t++) {
        abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
+        if(!s->method->body || !s->method->body->code) {
+            fprintf(stderr, "Internal Error: initscript has no body\n");
+        }
        swf_SetU30(tag, s->method->index); //!=t!
        traits_write(pool, tag, s->traits);
     }