summaryrefslogtreecommitdiff
path: root/extlibs/netbytes.c
diff options
context:
space:
mode:
Diffstat (limited to 'extlibs/netbytes.c')
-rw-r--r--extlibs/netbytes.c70
1 files changed, 51 insertions, 19 deletions
diff --git a/extlibs/netbytes.c b/extlibs/netbytes.c
index ecbb87e..81ba4d0 100644
--- a/extlibs/netbytes.c
+++ b/extlibs/netbytes.c
@@ -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");
}
@@ -492,7 +519,7 @@ int nb_load( netbyte_store *store, uint8_t *data )
__NBT_SIZE size;
memcpy( &size, c, sizeof(__NBT_SIZE) );
- printf("size: %d\n", size);
+ printf("NB_LOAD size: %d\n", size);
size -= sizeof(__NBT_SIZE);
c += sizeof(__NBT_SIZE);
@@ -512,14 +539,12 @@ int nb_load( netbyte_store *store, uint8_t *data )
{
case NBT_U8:
{
- nb_u8 *u8 = malloc( sizeof(nb_u8) );
+ nb_u8 u8;
- memcpy( u8, c, sizeof(nb_u8) );
+ memcpy( &u8, c, sizeof(nb_u8) );
c += sizeof(nb_u8);
- nb_add_u8( store, u8 );
-
- free(u8);
+ nb_add_u8( store, &u8 );
break;
}
@@ -545,12 +570,12 @@ int nb_load( netbyte_store *store, uint8_t *data )
}
case NBT_U16:
{
- nb_u16 *u16 = malloc( sizeof(nb_u16) );
+ nb_u16 u16;
- memcpy( u16, c, sizeof(nb_u16) );
+ memcpy( &u16, c, sizeof(nb_u16) );
c += sizeof(nb_u16);
- nb_add_u16( store, u16 );
+ nb_add_u16( store, &u16 );
break;
}
@@ -573,12 +598,12 @@ int nb_load( netbyte_store *store, uint8_t *data )
}
case NBT_U32:
{
- nb_u32 *u32 = malloc( sizeof(nb_u32) );
-
- memcpy( u32, c, sizeof(nb_u32) );
+ nb_u32 u32;
+
+ memcpy( &u32, c, sizeof(nb_u32) );
c += sizeof(nb_u32);
- nb_add_u32( store, u32 );
+ nb_add_u32( store, &u32 );
break;
}
case NBT_U32ARRAY:
@@ -600,12 +625,12 @@ int nb_load( netbyte_store *store, uint8_t *data )
}
case NBT_U64:
{
- nb_u64 *u64 = malloc( sizeof(nb_u64) );
+ nb_u64 u64;
- memcpy( u64, c, sizeof(nb_u64) );
+ memcpy( &u64, c, sizeof(nb_u64) );
c += sizeof(nb_u64);
- nb_add_u64( store, u64 );
+ nb_add_u64( store, &u64 );
break;
}
case NBT_U64ARRAY:
@@ -766,6 +791,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");
}