diff options
-rw-r--r-- | netbytes.c | 40 | ||||
-rw-r--r-- | netbytes.h | 18 |
2 files changed, 46 insertions, 12 deletions
@@ -165,6 +165,8 @@ int nb_add_u8arr( netbyte_store *store, nb_u8arr *u8arr ) int nb_add_u16( netbyte_store *store, nb_u16 *u16 ) { + nb_u16 *new_u16 = NULL; + if ( store->count >= __NBT_MAX_TYPES - 1 ) return -1; @@ -172,7 +174,11 @@ int nb_add_u16( netbyte_store *store, nb_u16 *u16 ) return -2; store->types[store->count].type = NBT_U16; - store->types[store->count].nb_val = (uint8_t *)u16; + + new_u16 = malloc(sizeof(nb_u16)); + memcpy(new_u16, u16, sizeof(nb_u16)); + store->types[store->count].nb_val = (uint8_t *)new_u16; + store->count += 1; return 0; } @@ -195,6 +201,8 @@ int nb_add_u16arr( netbyte_store *store, nb_u16arr *u16arr ) int nb_add_u32( netbyte_store *store, nb_u32 *u32 ) { + nb_u32 *new_u32 = NULL; + if ( store->count >= __NBT_MAX_TYPES - 1 ) return -1; @@ -202,7 +210,11 @@ int nb_add_u32( netbyte_store *store, nb_u32 *u32 ) return -2; store->types[store->count].type = NBT_U32; - store->types[store->count].nb_val = (uint8_t *)u32; + + new_u32 = malloc(sizeof(nb_u32)); + memcpy(new_u32, u32, sizeof(nb_u32)); + store->types[store->count].nb_val = (uint8_t *)new_u32; + store->count += 1; return 0; } @@ -225,6 +237,8 @@ int nb_add_u32arr( netbyte_store *store, nb_u32arr *u32arr ) int nb_add_u64( netbyte_store *store, nb_u64 *u64 ) { + nb_u64 *new_u64 = NULL; + if ( store->count >= __NBT_MAX_TYPES - 1 ) return -1; @@ -232,7 +246,11 @@ int nb_add_u64( netbyte_store *store, nb_u64 *u64 ) return -2; store->types[store->count].type = NBT_U64; - store->types[store->count].nb_val = (uint8_t *)u64; + + new_u64 = malloc(sizeof(nb_u64)); + memcpy(new_u64, u64, sizeof(nb_u64)); + store->types[store->count].nb_val = (uint8_t *)new_u64; + store->count += 1; return 0; } @@ -463,6 +481,15 @@ int nb_free(netbyte_store *store) store->types[i].type=0; break; } + case NBT_U32: + { + nb_u32 *u32 = (nb_u32 *)val; + //free(u32); + //u32 = NULL; + store->types[i].nb_val=NULL; + store->types[i].type=0; + break; + } default: printf("Unknown type\n"); } @@ -766,6 +793,13 @@ int nb_print(netbyte_store *store) u8arr->type, u8arr->len, u8arr->val); break; } + case NBT_U32: + { + nb_u32 *u32 = (nb_u32 *)store->types[i].nb_val; + printf("u32: t=0x%x v=0x%x", + u32->type, u32->val); + break; + } default: printf("Unknown"); } @@ -148,24 +148,24 @@ int nb_u32arr_create( nb_u32arr *s, __NBT_U32ARR_LEN len, uint32_t *val ); int nb_u64_create( nb_u64 *s, uint64_t val ); int nb_u64arr_create( nb_u64arr *s, __NBT_U64ARR_LEN len, uint64_t *val ); -int nb_init( netbyte_store *store ); -int nb_add_u8( netbyte_store *store, nb_u8 *u8 ); -int nb_add_u8arr( netbyte_store *store, nb_u8arr *u8arr ); -int nb_add_u16( netbyte_store *store, nb_u16 *u16 ); +int nb_init( netbyte_store *store ); +int nb_add_u8( netbyte_store *store, nb_u8 *u8 ); +int nb_add_u8arr( netbyte_store *store, nb_u8arr *u8arr ); +int nb_add_u16( netbyte_store *store, nb_u16 *u16 ); int nb_add_u16arr( netbyte_store *store, nb_u16arr *u16arr ); -int nb_add_u32( netbyte_store *store, nb_u32 *u32 ); +int nb_add_u32( netbyte_store *store, nb_u32 *u32 ); int nb_add_u32arr( netbyte_store *store, nb_u32arr *u32arr ); -int nb_add_u64( netbyte_store *store, nb_u64 *u64 ); +int nb_add_u64( netbyte_store *store, nb_u64 *u64 ); int nb_add_u64arr( netbyte_store *store, nb_u64arr *u64arr ); uint8_t *nb_create( netbyte_store *store ); int nb_free(netbyte_store *store); -int nb_load( netbyte_store *store, uint8_t *data ); +int nb_load( netbyte_store *store, uint8_t *data ); int nb_count( netbyte_store *store ); -int nb_type( netbyte_store *store, int count, __NBT_TYPED **type ); -int nb_val( netbyte_store *store, int count, __nb_type **type ); +int nb_type( netbyte_store *store, int count, __NBT_TYPED **type ); +int nb_val( netbyte_store *store, int count, __nb_type **type ); int nb_fread( netbyte_store *store, int fd); //print all all values in netbyte string |