diff options
Diffstat (limited to 'netbytes.c')
-rw-r--r-- | netbytes.c | 123 |
1 files changed, 118 insertions, 5 deletions
@@ -117,14 +117,24 @@ int nb_init( netbyte_store *store ) int nb_add_u8( netbyte_store *store, nb_u8 *u8 ) { + nb_u8 *new_u8=NULL; + if ( store->count >= __NBT_MAX_TYPES - 1 ) + { return -1; + } if ( u8->type != NBT_U8 ) + { return -2; + } store->types[store->count].type = NBT_U8; - store->types[store->count].nb_val = (uint8_t *)u8; + + new_u8 = malloc(sizeof(nb_u8)); + memcpy(new_u8, u8, sizeof(nb_u8)); + store->types[store->count].nb_val = (uint8_t *)new_u8; + store->count += 1; return 0; } @@ -132,6 +142,8 @@ int nb_add_u8( netbyte_store *store, nb_u8 *u8 ) int nb_add_u8arr( netbyte_store *store, nb_u8arr *u8arr ) { + nb_u8arr *new_u8arr = NULL; + if ( store->count >= __NBT_MAX_TYPES - 1 ) return -1; @@ -139,7 +151,13 @@ int nb_add_u8arr( netbyte_store *store, nb_u8arr *u8arr ) return -2; store->types[store->count].type = NBT_U8ARRAY; - store->types[store->count].nb_val = (uint8_t *)u8arr; + + new_u8arr = malloc(sizeof(nb_u8arr)); + memcpy(new_u8arr, u8arr, sizeof(nb_u8arr)); + new_u8arr->val = malloc(sizeof(nb_u8arr)*u8arr->len); + memcpy(new_u8arr->val, u8arr->val, u8arr->len); + store->types[store->count].nb_val = (uint8_t *)new_u8arr; + store->count += 1; return 0; } @@ -310,7 +328,9 @@ uint8_t *nb_create( netbyte_store *store ) /* create netbyte memory chunk */ buf = malloc( store->size ); if ( buf == NULL ) + { return NULL; + } c = buf; /* write size of netbytes */ @@ -409,6 +429,49 @@ uint8_t *nb_create( netbyte_store *store ) } +int nb_free(netbyte_store *store) +{ + int i; + + if (!store) + { + return -1; + } + + for (i=0;i<store->count;i++) + { + __NBT_TYPED type = store->types[i].type; + uint8_t *val = store->types[i].nb_val; + + switch(type) + { + case NBT_U8: + { + nb_u8 *u8 = (nb_u8 *)val; + free(u8); + u8=NULL; + store->types[i].nb_val=NULL; + store->types[i].type=0; + break; + } + case NBT_U8ARRAY: + { + nb_u8arr *u8arr = (nb_u8arr *)val; + free(u8arr->val); + free(u8arr); + store->types[i].nb_val=NULL; + store->types[i].type=0; + break; + } + default: + printf("Unknown type\n"); + } + } + free(store); + + return 0; +} + int nb_load( netbyte_store *store, uint8_t *data ) { __NBT_TYPED type; @@ -417,10 +480,14 @@ int nb_load( netbyte_store *store, uint8_t *data ) int count; if ( store == NULL ) + { return -1; + } if ( data == NULL ) + { return -2; + } __NBT_SIZE size; memcpy( &size, c, sizeof(__NBT_SIZE) ); @@ -429,7 +496,7 @@ int nb_load( netbyte_store *store, uint8_t *data ) size -= sizeof(__NBT_SIZE); c += sizeof(__NBT_SIZE); - if ( c == data ) + if (c == data) { store->size = size; return 0; @@ -450,6 +517,8 @@ int nb_load( netbyte_store *store, uint8_t *data ) c += sizeof(nb_u8); nb_add_u8( store, u8 ); + + free(u8); break; } @@ -468,6 +537,8 @@ int nb_load( netbyte_store *store, uint8_t *data ) c += u8arr->len; nb_add_u8arr( store, u8arr ); + + free(u8arr); break; } @@ -578,6 +649,7 @@ int nb_count( netbyte_store *store ) int nb_type( netbyte_store *store, int count, __NBT_TYPED **type ) { + __NBT_TYPED nbt; if ( store == NULL ) { return -1; @@ -588,8 +660,9 @@ int nb_type( netbyte_store *store, int count, __NBT_TYPED **type ) return -2; } - *type = store->types[count].type; //warnign? who cares - + //nbt = (__NBT_TYPED)store->types[count].type; //warnign? who cares + //*type = nbt; + *type = store->types[count].type; return 0; } @@ -651,3 +724,43 @@ int nb_fread( netbyte_store *store, int fd) return 0; } + +int nb_print(netbyte_store *store) +{ + int i; + + if (!store) + { + printf("nb: NULL\n"); + return -1; + } + + printf("nb: size=0x%x count=0x%x\n", store->size, store->count); + + for (i=0;i<store->count;i++) + { + printf("\t[%x] -> ", i); + switch (store->types[i].type) + { + case NBT_U8: + { + nb_u8 *u8 = (nb_u8 *)store->types[i].nb_val; + printf("u8: t=0x%x v=0x%x", + u8->type, u8->val); + break; + } + case NBT_U8ARRAY: + { + nb_u8arr *u8arr = (nb_u8arr *)store->types[i].nb_val; + printf("u8arr: t=0x%x l=0x%x v=0x%x", + u8arr->type, u8arr->len, u8arr->val); + break; + } + default: + printf("Unknown"); + } + printf("\n"); + } + + return 0; +}
\ No newline at end of file |