X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Fas3%2Fpool.c;h=a17797ffcffd73b916f69e6e09e0a538ce9e3e17;hb=0d0db46ffab07e85ece7961eb9c22b64749d918b;hp=59fc2e3319a91b8b3fbd82957260a1a5d2fd40a8;hpb=1f0a5e059f4f08808b763470f476ec016faf062b;p=swftools.git diff --git a/lib/as3/pool.c b/lib/as3/pool.c index 59fc2e3..a17797f 100644 --- a/lib/as3/pool.c +++ b/lib/as3/pool.c @@ -55,7 +55,10 @@ char float_equals(const void*_v1, const void*_v2) { const double*v2=_v2; if(!v1 || !v2) return v1==v2; - return *v1==*v2; + + if(*v1==*v2) return 1; + if(*v1!=*v1 && *v2!=*v2) return 1; //both values are NaN + return 0; } type_t float_type = { @@ -180,8 +183,14 @@ char* namespace_tostring(namespace_t*ns) U8 type = ns->access; access = access2str(type); char*s = escape_string(ns->name); - char*string = (char*)malloc(strlen(access)+strlen(s)+3); - int l = sprintf(string, "[%s]%s", access, s); + char*string = (char*)malloc(strlen(access)+strlen(s)+7); + if(!s) + sprintf(string, "[%s]NULL", access, s); + else if(!*s) + sprintf(string, "[%s]\"\"", access, s); + else + sprintf(string, "[%s]", access, s); + free(s); return string; } @@ -472,7 +481,7 @@ multiname_t* multiname_clone(multiname_t*other) char* access2str(int type) { - if(type==0x08) return "access08"; + if(type==0x08) return "namespace"; else if(type==0x16) return "public"; else if(type==0x17) return "packageinternal"; else if(type==0x18) return "protected"; @@ -611,6 +620,7 @@ void multiname_destroy(multiname_t*m) if(m->namespace_set) { namespace_set_destroy(m->namespace_set);m->namespace_set = 0; } + m->type=0; free(m); } } @@ -654,7 +664,14 @@ constant_t* constant_new_float(double f) constant_t* constant_new_string(char*s) { NEW(constant_t,c); - c->s = strdup(s); + c->s = string_new4(s); + c->type = CONSTANT_STRING; + return c; +} +constant_t* constant_new_string2(const char*s, int len) +{ + NEW(constant_t,c); + c->s = string_new3(s, len); c->type = CONSTANT_STRING; return c; } @@ -708,7 +725,8 @@ constant_t* constant_fromindex(pool_t*pool, int index, int type) } else if(c->type == CONSTANT_FLOAT) { c->f = pool_lookup_float(pool, index); } else if(c->type == CONSTANT_STRING) { - c->s = strdup(pool_lookup_string(pool, index)); + string_t s = pool_lookup_string2(pool, index); + c->s = string_dup3(&s); } else if(UNIQUE_CONSTANT(c->type)) { // ok } else { @@ -719,8 +737,8 @@ constant_t* constant_fromindex(pool_t*pool, int index, int type) char* constant_tostring(constant_t*c) { if(!c) - return 0; - char buf[30]; + return strdup("NULL"); + char buf[32]; if(NS_TYPE(c->type)) { return namespace_tostring(c->ns); } else if(c->type == CONSTANT_INT) { @@ -730,10 +748,12 @@ char* constant_tostring(constant_t*c) sprintf(buf, "%u", c->u); return strdup(buf); } else if(c->type == CONSTANT_FLOAT) { + char buf[1024]; sprintf(buf, "%f", c->f); return strdup(buf); } else if(c->type == CONSTANT_STRING) { - return strdup(c->s); + /* should we escape the string? \0 bytes won't be printed */ + return strdup_n(c->s->str,c->s->len); } else if(c->type == CONSTANT_TRUE) { return strdup("true"); } else if(c->type == CONSTANT_FALSE) { @@ -771,7 +791,7 @@ int constant_get_index(pool_t*pool, constant_t*c) } else if(c->type == CONSTANT_FLOAT) { return pool_register_float(pool, c->f); } else if(c->type == CONSTANT_STRING) { - return pool_register_string(pool, c->s); + return pool_register_string2(pool, c->s); } else if(!constant_has_index(c)) { return 1; } else { @@ -784,7 +804,7 @@ void constant_free(constant_t*c) if(!c) return; if(c->type == CONSTANT_STRING) { - free(c->s);c->s=0; + string_free(c->s); } else if (NS_TYPE(c->type)) { namespace_destroy(c->ns);c->ns=0; } @@ -810,11 +830,18 @@ int pool_register_float(pool_t*p, double d) assert(pos!=0); return pos; } -int pool_register_string(pool_t*pool, const char*s) +int pool_register_string(pool_t*pool, const char*str) { - if(!s) return 0; - ptroff_t l = strlen(s); - int pos = array_append_if_new(pool->x_strings, s, (void*)l); + if(!str) return 0; + string_t s = string_new2(str); + int pos = array_append_if_new(pool->x_strings, &s, 0); + assert(pos!=0); + return pos; +} +int pool_register_string2(pool_t*pool, string_t*s) +{ + if(!s || !s->str) return 0; + int pos = array_append_if_new(pool->x_strings, s, 0); assert(pos!=0); return pos; } @@ -836,9 +863,6 @@ int pool_register_multiname(pool_t*pool, multiname_t*n) { if(!n) return 0; int pos = array_append_if_new(pool->x_multinames, n, 0); - if(pos==0) { - *(int*)0=0xdead; - } assert(pos!=0); return pos; } @@ -906,11 +930,12 @@ int pool_find_namespace_set(pool_t*pool, namespace_set_t*set) } return i; } -int pool_find_string(pool_t*pool, const char*s) +int pool_find_string(pool_t*pool, const char*str) { - if(!s) + if(!str) return 0; - int i = array_find(pool->x_strings, s); + 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); return 0; @@ -946,15 +971,16 @@ double pool_lookup_float(pool_t*pool, int i) if(!i) return __builtin_nan(""); return *(double*)array_getkey(pool->x_floats, i); } -char*pool_lookup_string(pool_t*pool, int i) +const char*pool_lookup_string(pool_t*pool, int i) { - return (char*)array_getkey(pool->x_strings, i); + string_t*s = array_getkey(pool->x_strings, i); + if(!s) return 0; + return s->str; } string_t pool_lookup_string2(pool_t*pool, int i) { - char*s = (char*)array_getkey(pool->x_strings, i); - int len = (int)(ptroff_t)array_getvalue(pool->x_strings, i); - return string_new(s,len); + string_t*s = array_getkey(pool->x_strings, i); + return *s; } namespace_t*pool_lookup_namespace(pool_t*pool, int i) { @@ -976,7 +1002,7 @@ pool_t*pool_new() p->x_ints = array_new2(&uint_type); p->x_uints = array_new2(&uint_type); p->x_floats = array_new2(&float_type); - p->x_strings = array_new2(&charptr_type); + p->x_strings = array_new2(&stringstruct_type); p->x_namespaces = array_new2(&namespace_type); p->x_namespace_sets = array_new2(&namespace_set_type); p->x_multinames = array_new2(&multiname_type); @@ -1027,12 +1053,10 @@ void pool_read(pool_t*pool, TAG*tag) DEBUG printf("%d strings\n", num_strings); for(t=1;tx_strings, s, (void*)(ptroff_t)len); - free(s); - DEBUG printf("%d) \"%s\"\n", t, pool->x_strings->d[t].name); + string_t s = string_new(&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); } int num_namespaces = swf_GetU30(tag); DEBUG printf("%d namespaces\n", num_namespaces); @@ -1041,7 +1065,7 @@ void pool_read(pool_t*pool, TAG*tag) int namenr = swf_GetU30(tag); const char*name = 0; if(namenr) - name = array_getkey(pool->x_strings, namenr); + name = pool_lookup_string(pool, namenr); namespace_t*ns = namespace_new(type, name); array_append(pool->x_namespaces, ns, 0); DEBUG printf("%d) %02x \"%s\"\n", t, type, namespace_tostring(ns)); @@ -1077,17 +1101,17 @@ void pool_read(pool_t*pool, TAG*tag) m.ns = (namespace_t*)array_getkey(pool->x_namespaces, namespace_index); int name_index = swf_GetU30(tag); if(name_index) // 0 = '*' (any) - m.name = array_getkey(pool->x_strings, name_index); + m.name = pool_lookup_string(pool, name_index); } else if(m.type==0x0f || m.type==0x10) { int name_index = swf_GetU30(tag); if(name_index) // 0 = '*' (any name) - m.name = array_getkey(pool->x_strings, name_index); + m.name = pool_lookup_string(pool, 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); if(name_index) - m.name = array_getkey(pool->x_strings, name_index); + m.name = pool_lookup_string(pool, name_index); m.namespace_set = (namespace_set_t*)array_getkey(pool->x_namespace_sets, namespace_set_index); } else if(m.type==0x1b || m.type==0x1c) { int namespace_set_index = swf_GetU30(tag); @@ -1100,6 +1124,55 @@ void pool_read(pool_t*pool, TAG*tag) } } +void pool_dump(pool_t*pool, FILE*fo, char flags) +{ + int t; + fprintf(fo, "%d integers:\n", pool->x_ints->num); + for(t=1;tx_ints->num;t++) { + S32 val = *(int*)array_getkey(pool->x_ints, t); + if(flags&1) fprintf(fo, "%d) %d\n", t, val); + } + fprintf(fo, "%d unsigned integers:\n", pool->x_uints->num); + for(t=1;tx_uints->num;t++) { + U32 val = *(unsigned int*)array_getkey(pool->x_uints, t); + if(flags&1) fprintf(fo, "%d) %d\n", t, val); + } + fprintf(fo, "%d floats:\n", pool->x_floats->num); + for(t=1;tx_floats->num;t++) { + double d = pool_lookup_float(pool, t); + if(flags&2) fprintf(fo, "%d) %f\n", t, d); + } + fprintf(fo, "%d strings:\n", pool->x_strings->num); + for(t=1;tx_strings->num;t++) { + string_t str = pool_lookup_string2(pool, t); + if(flags&1) fprintf(fo, "%d) ", t); + if(flags&1) fwrite(str.str, str.len, 1, fo); + if(flags&1) fprintf(fo, "\n", t); + } + fprintf(fo, "%d namespaces:\n", pool->x_namespaces->num); + for(t=1;tx_namespaces->num;t++) { + namespace_t*ns= (namespace_t*)array_getkey(pool->x_namespaces, t); + char*s = namespace_tostring(ns); + if(flags&1) fprintf(fo, "%d) %s\n", t, s); + free(s); + } + fprintf(fo, "%d namespace sets:\n", pool->x_namespace_sets->num); + for(t=1;tx_namespace_sets->num;t++) { + namespace_set_t*set = (namespace_set_t*)array_getkey(pool->x_namespace_sets, t); + char*s = namespace_set_tostring(set); + if(flags&1) fprintf(fo, "%d) %s\n", t, s); + free(s); + } + + fprintf(fo, "%d multinames:\n", pool->x_multinames->num); + for(t=1;tx_multinames->num;t++) { + multiname_t*m = (multiname_t*)array_getkey(pool->x_multinames, t); + char*s = multiname_tostring(m); + if(flags&1) fprintf(fo, "%d) %s\n", t, s); + free(s); + } +} + void pool_write(pool_t*pool, TAG*tag) { int t;