#include "netbytes.h"
int nb_u8_create( nb_u8 *s, uint8_t val )
{
if ( s == NULL )
return -1;
s->type = NBT_U8;
s->val = val;
return 0;
}
int nb_u8arr_create( nb_u8arr *s, __NBT_U8ARR_LEN len, uint8_t *val )
{
if ( s == NULL )
return -1;
s->type = NBT_U8ARRAY;
s->len = len;
s->val = val;
return 0;
}
int nb_u16_create( nb_u16 *s, uint16_t val )
{
if ( s == NULL )
return -1;
s->type = NBT_U16;
s->val = val;
return 0;
}
int nb_u16arr_create( nb_u16arr *s, __NBT_U16ARR_LEN len, uint16_t *val )
{
if ( s == NULL )
return -1;
s->type = NBT_U16ARRAY;
s->len = len;
s->val = val;
return 0;
}
int nb_u32_create( nb_u32 *s, uint32_t val )
{
if ( s == NULL )
return -1;
s->type = NBT_U32;
s->val = val;
return 0;
}
int nb_u32arr_create( nb_u32arr *s, __NBT_U32ARR_LEN len, uint32_t *val )
{
if ( s == NULL )
return -1;
s->type = NBT_U32ARRAY;
s->len = len;
s->val = val;
return 0;
}
int nb_u64_create( nb_u64 *s, uint64_t val )
{
if ( s == NULL )
return -1;
s->type = NBT_U64;
s->val = val;
return 0;
}
int nb_u64arr_create( nb_u64arr *s, __NBT_U64ARR_LEN len, uint64_t *val )
{
if ( s == NULL )
return -1;
s->type = NBT_U64ARRAY;
s->len = len;
s->val = val;
return 0;
}
int nb_init( netbyte_store *store )
{
if ( store == NULL )
return -1;
memset( store, 0, sizeof(netbyte_store) );
/* we know that they allready zero, but philosophy more important */
store->size = 0;
store->count = 0;
return 0;
}
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;
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;
}
int nb_add_u8arr( netbyte_store *store, nb_u8arr *u8arr )
{
nb_u8arr *new_u8arr = NULL;
if ( store->count >= __NBT_MAX_TYPES - 1 )
return -1;
if ( u8arr->type != NBT_U8ARRAY )
return -2;
store->types[store->count].type = NBT_U8ARRAY;
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;
}
int nb_add_u16( netbyte_store *store, nb_u16 *u16 )
{
nb_u16 *new_u16 = NULL;
if ( store->count >= __NBT_MAX_TYPES - 1 )
return -1;
if ( u16->type != NBT_U16 )
return -2;
store->types[store->count].type = NBT_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;
}
int nb_add_u16arr( netbyte_store *store, nb_u16arr *u16arr )
{
if ( store->count >= __NBT_MAX_TYPES - 1 )
return -1;
if ( u16arr->type != NBT_U16ARRAY )
return -2;
store->types[store->count].type = NBT_U16ARRAY;
store->types[store->count].nb_val = (uint8_t *)u16arr;
store->count += 1;
return 0;
}
int nb_add_u32( netbyte_store *store, nb_u32 *u32 )
{
nb_u32 *new_u32 = NULL;
if ( store->count >= __NBT_MAX_TYPES - 1 )
return -1;
if ( u32->type != NBT_U32 )
return -2;
store->types[store->count].type = NBT_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;
}
int nb_add_u32arr( netbyte_store *store, nb_u32arr *u32arr )
{
if ( store->count >= __NBT_MAX_TYPES - 1 )
return -1;
if ( u32arr->type != NBT_U32ARRAY )
return -2;
store->types[store->count].type = NBT_U32ARRAY;
store->types[store->count].nb_val = (uint8_t *)u32arr;
store->count += 1;
return 0;
}
int nb_add_u64( netbyte_store *
|