diff options
author | FreeArtMan <dos21h@gmail.com> | 2015-11-08 19:58:29 +0000 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2015-11-08 19:58:29 +0000 |
commit | ab329747c061820f8fc709f2c33bc7a2f9a3ff8d (patch) | |
tree | 824749507f325e6bca6b0b47bd10e5f1553726d0 /netbytes.c | |
parent | 3c89841f31cd7fc6cb35c129a9135c5e974a7649 (diff) | |
download | netbytes-ab329747c061820f8fc709f2c33bc7a2f9a3ff8d.tar.gz netbytes-ab329747c061820f8fc709f2c33bc7a2f9a3ff8d.zip |
Added types u16,u32,u64,u16arr,u32arr,u64arr
Diffstat (limited to 'netbytes.c')
-rw-r--r-- | netbytes.c | 332 |
1 files changed, 331 insertions, 1 deletions
@@ -25,6 +25,80 @@ int nb_u8arr_create( nb_u8arr *s, __NBT_U8ARR_LEN len, uint8_t *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 ) { @@ -71,6 +145,96 @@ int nb_add_u8arr( netbyte_store *store, nb_u8arr *u8arr ) } +int nb_add_u16( netbyte_store *store, nb_u16 *u16 ) +{ + if ( store->count >= __NBT_MAX_TYPES - 1 ) + return -1; + + if ( u16->type != NBT_U16 ) + return -2; + + store->types[store->count].type = NBT_U16; + store->types[store->count].nb_val = (uint8_t *)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 ) +{ + if ( store->count >= __NBT_MAX_TYPES - 1 ) + return -1; + + if ( u32->type != NBT_U32 ) + return -2; + + store->types[store->count].type = NBT_U32; + store->types[store->count].nb_val = (uint8_t *)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 *store, nb_u64 *u64 ) +{ + if ( store->count >= __NBT_MAX_TYPES - 1 ) + return -1; + + if ( u64->type != NBT_U64 ) + return -2; + + store->types[store->count].type = NBT_U64; + store->types[store->count].nb_val = (uint8_t *)u64; + store->count += 1; + return 0; +} + + +int nb_add_u64arr( netbyte_store *store, nb_u64arr *u64arr ) +{ + if ( store->count >= __NBT_MAX_TYPES - 1 ) + return -1; + + if ( u64arr->type != NBT_U64ARRAY ) + return -2; + + store->types[store->count].type = NBT_U64ARRAY; + store->types[store->count].nb_val = (uint8_t *)u64arr; + store->count += 1; + return 0; +} + + uint8_t *nb_create( netbyte_store *store ) { uint8_t *ret = NULL; @@ -95,7 +259,7 @@ uint8_t *nb_create( netbyte_store *store ) switch( iter.type ) { case NBT_U8: - store->size += sizeof(__NBT_TYPED) + 1; + store->size += sizeof(__NBT_TYPED) + sizeof(uint8_t); break; case NBT_U8ARRAY: { @@ -105,6 +269,39 @@ uint8_t *nb_create( netbyte_store *store ) u8arr->len; } break; + case NBT_U16: + store->size += sizeof(__NBT_TYPED) + sizeof(uint16_t); + break; + case NBT_U16ARRAY: + { + nb_u16arr *u16arr = (nb_u16arr *)iter.nb_val; + store->size += sizeof(__NBT_TYPED) + + sizeof(__NBT_U16ARR_LEN) + + u16arr->len*sizeof(uint16_t); + } + break; + case NBT_U32: + store->size += sizeof(__NBT_TYPED) + sizeof(uint32_t); + break; + case NBT_U32ARRAY: + { + nb_u32arr *u32arr = (nb_u32arr *)iter.nb_val; + store->size += sizeof(__NBT_TYPED) + + sizeof(__NBT_U32ARR_LEN) + + u32arr->len*sizeof(uint32_t); + } + break; + case NBT_U64: + store->size += sizeof(__NBT_TYPED) + sizeof(uint64_t); + break; + case NBT_U64ARRAY: + { + nb_u64arr *u64arr = (nb_u64arr *)iter.nb_val; + store->size += sizeof(__NBT_TYPED) + + sizeof(__NBT_U64ARR_LEN) + + u64arr->len*sizeof(uint64_t); + } + break; default: printf("Unknow type be carefull1\n"); } @@ -143,6 +340,57 @@ uint8_t *nb_create( netbyte_store *store ) c += u8arr->len; break; } + case NBT_U16: + memcpy( c, &iter.type, sizeof(__NBT_TYPED) ); + c += sizeof(__NBT_TYPED); + memcpy( c, &((nb_u16 *)(iter.nb_val))->val, sizeof(uint16_t) ); + c += sizeof(uint16_t); + break; + case NBT_U16ARRAY: + { + nb_u16arr *u16arr = (nb_u16arr *)iter.nb_val; + memcpy( c, &iter.type, sizeof(__NBT_TYPED) ); + c += sizeof(__NBT_TYPED); + memcpy( c, &u16arr->len, sizeof(__NBT_U16ARR_LEN)); + c += sizeof(__NBT_U16ARR_LEN); + memcpy( c, u16arr->val, u16arr->len ); + c += u16arr->len*sizeof(uint16_t); + break; + } + case NBT_U32: + memcpy( c, &iter.type, sizeof(__NBT_TYPED) ); + c += sizeof(__NBT_TYPED); + memcpy( c, &((nb_u32 *)(iter.nb_val))->val, sizeof(uint32_t) ); + c += sizeof(uint32_t); + break; + case NBT_U32ARRAY: + { + nb_u32arr *u32arr = (nb_u32arr *)iter.nb_val; + memcpy( c, &iter.type, sizeof(__NBT_TYPED) ); + c += sizeof(__NBT_TYPED); + memcpy( c, &u32arr->len, sizeof(__NBT_U32ARR_LEN)); + c += sizeof(__NBT_U32ARR_LEN); + memcpy( c, u32arr->val, u32arr->len ); + c += u32arr->len*sizeof(uint32_t); + break; + } + case NBT_U64: + memcpy( c, &iter.type, sizeof(__NBT_TYPED) ); + c += sizeof(__NBT_TYPED); + memcpy( c, &((nb_u64 *)(iter.nb_val))->val, sizeof(uint64_t) ); + c += sizeof(uint64_t); + break; + case NBT_U64ARRAY: + { + nb_u64arr *u64arr = (nb_u64arr *)iter.nb_val; + memcpy( c, &iter.type, sizeof(__NBT_TYPED) ); + c += sizeof(__NBT_TYPED); + memcpy( c, &u64arr->len, sizeof(__NBT_U64ARR_LEN)); + c += sizeof(__NBT_U64ARR_LEN); + memcpy( c, u64arr->val, u64arr->len ); + c += u64arr->len*sizeof(uint64_t); + break; + } default: printf("Unknow type be carefull2\n"); } @@ -223,6 +471,88 @@ int nb_load( netbyte_store *store, uint8_t *data ) break; } + case NBT_U16: + { + nb_u16 *u16 = malloc( sizeof(nb_u16) ); + + memcpy( u16, c, sizeof(nb_u16) ); + c += sizeof(nb_u16); + + nb_add_u16( store, u16 ); + + break; + } + case NBT_U16ARRAY: + { + nb_u16arr *u16arr = malloc( sizeof(nb_u16arr) ); + + memcpy( u16arr, c, sizeof(__NBT_TYPED)); + c += sizeof(__NBT_TYPED); + + memcpy( &u16arr->len, c, sizeof(__NBT_U16ARR_LEN) ); + c += sizeof(__NBT_U16ARR_LEN); + + u16arr->val = (uint16_t *)c; + //printf("->2 %s\n",c ); + c += u16arr->len*sizeof(uint16_t); + + nb_add_u16arr( store, u16arr ); + break; + } + case NBT_U32: + { + nb_u32 *u32 = malloc( sizeof(nb_u32) ); + + memcpy( u32, c, sizeof(nb_u32) ); + c += sizeof(nb_u32); + + nb_add_u32( store, u32 ); + break; + } + case NBT_U32ARRAY: + { + nb_u32arr *u32arr = malloc( sizeof(nb_u32arr) ); + + memcpy( u32arr, c, sizeof(__NBT_TYPED)); + c += sizeof(__NBT_TYPED); + + memcpy( &u32arr->len, c, sizeof(__NBT_U32ARR_LEN) ); + c += sizeof(__NBT_U32ARR_LEN); + + u32arr->val = (uint32_t *)c; + //printf("->2 %s\n",c ); + c += u32arr->len*sizeof(uint32_t); + + nb_add_u32arr( store, u32arr ); + break; + } + case NBT_U64: + { + nb_u64 *u64 = malloc( sizeof(nb_u64) ); + + memcpy( u64, c, sizeof(nb_u64) ); + c += sizeof(nb_u64); + + nb_add_u64( store, u64 ); + break; + } + case NBT_U64ARRAY: + { + nb_u64arr *u64arr = malloc( sizeof(nb_u64arr) ); + + memcpy( u64arr, c, sizeof(__NBT_TYPED)); + c += sizeof(__NBT_TYPED); + + memcpy( &u64arr->len, c, sizeof(__NBT_U64ARR_LEN) ); + c += sizeof(__NBT_U64ARR_LEN); + + u64arr->val = (uint64_t *)c; + //printf("->2 %s\n",c ); + c += u64arr->len; + + nb_add_u64arr( store, u64arr ); + break; + } default: printf("Unknown type"); } |