fixed some compiler warnings
[swftools.git] / lib / as3 / pool.c
index 5b708e2..ce279a3 100644 (file)
@@ -136,7 +136,7 @@ char*escape_string(const char*str)
     if(!str)
         return strdup("NULL");
     int len=0;
-    unsigned const char*s=str;
+    unsigned const char*s=(unsigned const char*)str;
     while(*s) {
         if(*s<10) {
             len+=2; // \d
@@ -151,7 +151,7 @@ char*escape_string(const char*str)
     }
     char*newstr = malloc(len+1);
     char*dest = newstr;
-    s=str;
+    s=(unsigned const char*)str;
     while(*s) {
         if(*s<9) {
             dest+=sprintf(dest, "\\%d", *s);
@@ -185,9 +185,9 @@ char* namespace_tostring(namespace_t*ns)
     char*s = escape_string(ns->name);
     char*string = (char*)malloc(strlen(access)+strlen(s)+7);
     if(!s)
-        sprintf(string, "[%s]NULL", access, s);
+        sprintf(string, "[%s]NULL", access);
     else if(!*s)
-        sprintf(string, "[%s]\"\"", access, s);
+        sprintf(string, "[%s]\"\"", access);
     else 
         sprintf(string, "[%s]%s", access, s);
     free(s);
@@ -209,7 +209,7 @@ namespace_t* namespace_fromstring(const char*name)
     namespace_t*ns = malloc(sizeof(namespace_t));
     memset(ns, 0, sizeof(namespace_t));
     if(name[0] == '[') {
-        U8 access;
+        U8 access = 0;
         char*n = strdup(name);
         char*bracket = strchr(n, ']');
         if(bracket) {
@@ -250,7 +250,7 @@ namespace_t* namespace_new(U8 access, const char*name)
     ns->name = name?strdup(name):0;
     return ns;
 }
-namespace_t* namespace_new_undefined(const char*name) {
+namespace_t* namespace_new_namespace(const char*name) {
     return namespace_new(0x08, name); // public?
 }
 namespace_t* namespace_new_package(const char*name) {
@@ -487,6 +487,7 @@ char* access2str(int type)
     else if(type==0x19) return "explicit";
     else if(type==0x1A) return "staticprotected";
     else if(type==0x05) return "private";
+    else if(type==0x00) return "any";
     else {
         fprintf(stderr, "Undefined access type %02x\n", type);
         return "undefined";
@@ -522,14 +523,17 @@ char* multiname_tostring(multiname_t*m)
     int namelen = strlen(name);
 
     if(m->type==QNAME || m->type==QNAMEA || m->type==POSTFIXTYPE) {
-        char*nsname = escape_string(m->ns->name);
+        char*nsname = m->ns?escape_string(m->ns->name):strdup("NULL");
         mname = malloc(strlen(nsname)+namelen+32);
         strcpy(mname, "<q");
         if(m->type == QNAMEA)
             strcat(mname, ",attr");
-        strcat(mname, ">[");
-        strcat(mname,access2str(m->ns->access));
-        strcat(mname, "]");
+       strcat(mname, ">");
+       if(m->ns) {
+           strcat(mname,"[");
+           strcat(mname,access2str(m->ns->access));
+           strcat(mname, "]");
+       }
         strcat(mname, nsname);
         free(nsname);
         strcat(mname, "::");
@@ -634,9 +638,6 @@ type_t multiname_type = {
 
 // ------------------------------- constants -------------------------------------
 
-#define NS_TYPE(x) ((x) == 0x08 || (x) == 0x16 || (x) == 0x17 || (x) == 0x18 ||  \
-                                   (x) == 0x19 || (x) == 0x1a || (x) == 0x05)
-
 #define UNIQUE_CONSTANT(x) ((x) == CONSTANT_TRUE || (x) == CONSTANT_FALSE || (x) == CONSTANT_NULL || (x) == CONSTANT_UNDEFINED)
 
 constant_t* constant_new_int(int i) 
@@ -660,7 +661,7 @@ constant_t* constant_new_float(double f)
     c->type = CONSTANT_FLOAT;
     return c;
 }
-constant_t* constant_new_string(char*s)
+constant_t* constant_new_string(const char*s)
 {
     NEW(constant_t,c);
     c->s = string_new4(s);
@@ -706,6 +707,18 @@ constant_t* constant_new_undefined()
     c->type = CONSTANT_UNDEFINED;
     return c;
 }
+constant_t* constant_clone(constant_t*other)
+{
+    if(!other) return 0;
+    constant_t*c = malloc(sizeof(constant_t));
+    memcpy(c, other, sizeof(constant_t));
+    if(NS_TYPE(c->type)) {
+        c->ns = namespace_clone(other->ns);
+    } else if(c->type == CONSTANT_STRING) {
+        c->s = string_dup3(other->s);
+    }
+    return c;
+}
 constant_t* constant_fromindex(pool_t*pool, int index, int type)
 {
     if(!index) {
@@ -791,6 +804,10 @@ int constant_get_index(pool_t*pool, constant_t*c)
         return pool_register_float(pool, c->f);
     } else if(c->type == CONSTANT_STRING) {
         return pool_register_string2(pool, c->s);
+    } else if(c->type == CONSTANT_UNDEFINED) {
+        /* write undefined with index 0 (and no type). Otherwise, the FlashPlayer 
+           seems to throw an "attempt to read out of bounds" exception */
+        return 0;
     } else if(!constant_has_index(c)) {
         return 1;
     } else {
@@ -886,7 +903,7 @@ int pool_register_namespace(pool_t*pool, namespace_t*ns)
 {
     if(!ns) return 0;
     int pos = array_append_or_increase(pool->x_namespaces, ns);
-    assert(pos!=0);
+    assert(pos!=0 || ns->access==ZERONAMESPACE);
     return pos;
 }
 int pool_register_namespace_set(pool_t*pool, namespace_set_t*set)
@@ -936,7 +953,7 @@ int pool_find_float(pool_t*pool, double x)
 {
     int i = array_find(pool->x_ints, &x);
     if(i<=0) {
-        fprintf(stderr, "Couldn't find int \"%d\" in constant pool\n", x);
+        fprintf(stderr, "Couldn't find int \"%f\" in constant pool\n", x);
         return 0;
     }
     return i;
@@ -946,9 +963,9 @@ int pool_find_namespace(pool_t*pool, namespace_t*ns)
     if(!ns)
         return 0;
     int i = array_find(pool->x_namespaces, ns);
-    if(i<=0) {
+    if(i<0) {
         char*s = namespace_tostring(ns);
-        fprintf(stderr, "Couldn't find namespace \"%s\" %08x in constant pool\n", s, ns);
+        fprintf(stderr, "Couldn't find namespace \"%s\" %08x in constant pool\n", s, (int)ns);
         free(s);
         return 0;
     }
@@ -974,7 +991,7 @@ int pool_find_string(pool_t*pool, const char*str)
     string_t s = string_new2(str);
     int i = array_find(pool->x_strings, &s);
     if(i<=0) {
-        fprintf(stderr, "Couldn't find string \"%s\" in constant pool\n", s);
+        fprintf(stderr, "Couldn't find string \"%s\" in constant pool\n", str);
         return 0;
     }
     return i;
@@ -1032,6 +1049,7 @@ multiname_t*pool_lookup_multiname(pool_t*pool, int i)
     return (multiname_t*)array_getkey(pool->x_multinames, i);
 }
 
+static namespace_t zeronamespace={ZERONAMESPACE,"*"};
 pool_t*pool_new()
 {
     NEW(pool_t, p);
@@ -1050,7 +1068,7 @@ pool_t*pool_new()
     array_append(p->x_uints, 0, 0);
     array_append(p->x_floats, 0, 0);
     array_append(p->x_strings, 0, 0);
-    array_append(p->x_namespaces, 0, 0);
+    array_append(p->x_namespaces, &zeronamespace, 0);
     array_append(p->x_namespace_sets, 0, 0);
     array_append(p->x_multinames, 0, 0);
     return p;
@@ -1101,7 +1119,7 @@ void pool_read(pool_t*pool, TAG*tag)
     DEBUG printf("%d strings\n", num_strings);
     for(t=1;t<num_strings;t++) {
        int len = swf_GetU30(tag);
-        string_t s = string_new(&tag->data[tag->pos], len);
+        string_t s = string_new((char*)&tag->data[tag->pos], len);
        swf_GetBlock(tag, 0, len);
        array_append(pool->x_strings, &s, 0);
        DEBUG printf("%d) \"%s\"\n", t, ((string_t*)array_getkey(pool->x_strings, t))->str);
@@ -1153,6 +1171,9 @@ void pool_read(pool_t*pool, TAG*tag)
        if(m.type==0x07 || m.type==0x0d) {
            int namespace_index = swf_GetU30(tag);
             m.ns = (namespace_t*)array_getkey(pool->x_namespaces, namespace_index);
+           if(!m.ns) {
+               fprintf(stderr, "Error: Illegal reference to namespace #%d in constant pool.\n", namespace_index);
+           }
             int name_index = swf_GetU30(tag);
             if(name_index) // 0 = '*' (any)
                m.name = pool_lookup_string(pool, name_index);
@@ -1211,7 +1232,7 @@ void pool_dump(pool_t*pool, FILE*fo, char flags)
         int freq = (int)(ptroff_t)array_getvalue(pool->x_strings, t);
         if(flags&1) fprintf(fo, "%5d %d) ", freq, t);
         if(flags&1) fwrite(str.str, str.len, 1, fo);
-        if(flags&1) fprintf(fo, "\n", t);
+        if(flags&1) fprintf(fo, "\n");
     }
     fprintf(fo, "%d namespaces\n", pool->x_namespaces->num);
     for(t=1;t<pool->x_namespaces->num;t++) {
@@ -1338,13 +1359,17 @@ void pool_write(pool_t*pool, TAG*tag)
         } else {
             assert(m->type!=0x07 && m->type!=0x0d);
         }
+       
         if(m->name) {
             assert(m->type==0x09 || m->type==0x0e || m->type==0x07 || m->type==0x0d || m->type==0x0f || m->type==0x10);
            int i = pool_find_string(pool, m->name);
             if(i<0) fprintf(stderr, "internal error: unregistered name\n");
            swf_SetU30(tag, i);
         } else {
-            assert(m->type!=0x09 && m->type!=0x0e && m->type!=0x07 && m->type!=0x0d && m->type!=0x0f && m->type!=0x10);
+           if(m->type == 0x09) {
+               swf_SetU30(tag, 0);
+           }
+            assert(m->type!=0x0e && m->type!=0x07 && m->type!=0x0d && m->type!=0x0f && m->type!=0x10);
         }
         if(m->namespace_set) {
             assert(m->type==0x09 || m->type==0x0e || m->type==0x1c || m->type==0x1b);