summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile26
-rw-r--r--netbytes.c323
-rw-r--r--netbytes.h93
3 files changed, 442 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..0b097c1
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,26 @@
+PROJ=libnetbytes
+CC=gcc
+CFLAGS=
+LDFLAGS=
+
+SOURCE=netbytes
+OBJECTS=$(SOURCE:=.o)
+SOURCES=$(SOURCE:=.c)
+
+%.o: %.c
+ $(CC) $(CFLAGS) -c $<
+
+make: dynamic static
+
+dynamic: CFLAGS+=-fPIC
+dynamic: clean $(OBJECTS)
+ $(CC) $(CFLAGS) $(OBJECTS) -shared -o $(PROJ).so
+
+static: clean $(OBJECTS)
+ ar rcs $(PROJ).a $(OBJECTS)
+
+
+clean:
+ rm -rf *.o
+ rm -rf *.a
+ rm -rf *.so \ No newline at end of file
diff --git a/netbytes.c b/netbytes.c
new file mode 100644
index 0000000..7250aac
--- /dev/null
+++ b/netbytes.c
@@ -0,0 +1,323 @@
+#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_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 )
+{
+ 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;
+ store->count += 1;
+ return 0;
+}
+
+
+int nb_add_u8arr( netbyte_store *store, nb_u8arr *u8arr )
+{
+ if ( store->count >= __NBT_MAX_TYPES - 1 )
+ return -1;
+
+ if ( u8arr->type != NBT_U8ARRAY )
+ return -2;
+
+ store->types[store->count].type = NBT_U8ARRAY;
+ store->types[store->count].nb_val = (uint8_t *)u8arr;
+ store->count += 1;
+ return 0;
+}
+
+
+uint8_t *nb_create( netbyte_store *store )
+{
+ uint8_t *ret = NULL;
+ uint8_t *buf = NULL;
+ uint8_t *c = NULL;
+
+ int i;
+
+ if ( store == NULL )
+ return NULL;
+
+ if ( store->count < 1 )
+ return NULL;
+
+ store->size == 0;
+
+ /* precalc needed size starting from zero not metter what */
+ store->size += sizeof(__NBT_SIZE);
+ for (i=0; i < store->count; i++)
+ {
+ __nb_type iter = store->types[i];
+ switch( iter.type )
+ {
+ case NBT_U8:
+ store->size += sizeof(__NBT_TYPED) + 1;
+ break;
+ case NBT_U8ARRAY:
+ {
+ nb_u8arr *u8arr = (nb_u8arr *)iter.nb_val;
+ store->size += sizeof(__NBT_TYPED) +
+ sizeof(__NBT_U8ARR_LEN) +
+ u8arr->len;
+ }
+ break;
+ default:
+ printf("Unknow type be carefull1\n");
+ }
+ }
+
+ /* create netbyte memory chunk */
+ buf = malloc( store->size );
+ if ( buf == NULL )
+ return NULL;
+ c = buf;
+
+ /* write size of netbytes */
+ memcpy( c, &store->size, sizeof(__NBT_SIZE) );
+ c += sizeof(__NBT_SIZE);
+
+ /* per each type copy stuff */
+ for ( i = 0; i < store->count; i++ )
+ {
+ __nb_type iter = store->types[i];
+ switch ( iter.type )
+ {
+ case NBT_U8:
+ memcpy( c, &iter.type, sizeof(__NBT_TYPED) );
+ c += sizeof(__NBT_TYPED);
+ memcpy( c, &((nb_u8 *)(iter.nb_val))->val, sizeof(uint8_t) );
+ c += sizeof(uint8_t);
+ break;
+ case NBT_U8ARRAY:
+ {
+ nb_u8arr *u8arr = (nb_u8arr *)iter.nb_val;
+ memcpy( c, &iter.type, sizeof(__NBT_TYPED) );
+ c += sizeof(__NBT_TYPED);
+ memcpy( c, &u8arr->len, sizeof(__NBT_U8ARR_LEN));
+ c += sizeof(__NBT_U8ARR_LEN);
+ memcpy( c, u8arr->val, u8arr->len );
+ c += u8arr->len;
+ break;
+ }
+ default:
+ printf("Unknow type be carefull2\n");
+ }
+ }
+
+ if ( c-buf-1 > store->size )
+ {
+ printf("Something went wrong while created netbytes\n");
+ free(buf);
+ return NULL;
+ }
+
+ ret = buf;
+
+ return ret;
+}
+
+
+int nb_load( netbyte_store *store, uint8_t *data )
+{
+ __NBT_TYPED type;
+ int iter=1;
+ uint8_t *c = data;
+ int count;
+
+ if ( store == NULL )
+ return -1;
+
+ if ( data == NULL )
+ return -2;
+
+ __NBT_SIZE size;
+ memcpy( &size, c, sizeof(__NBT_SIZE) );
+ printf("size: %d\n", size);
+
+ size -= sizeof(__NBT_SIZE);
+ c += sizeof(__NBT_SIZE);
+
+ if ( c == data )
+ {
+ store->size = size;
+ return 0;
+ }
+
+ count = 0;
+ memset( store, 0, sizeof(netbyte_store) );
+ while ( iter )
+ {
+ type = (__NBT_TYPED)c[0];
+ switch( type )
+ {
+ case NBT_U8:
+ {
+ nb_u8 *u8 = malloc( sizeof(nb_u8) );
+
+ memcpy( u8, c, sizeof(nb_u8) );
+ c += sizeof(nb_u8);
+
+ nb_add_u8( store, u8 );
+
+ break;
+ }
+ case NBT_U8ARRAY:
+ {
+ nb_u8arr *u8arr = malloc( sizeof(nb_u8arr) );
+
+ memcpy( u8arr, c, sizeof(__NBT_TYPED));
+ c += sizeof(__NBT_TYPED);
+
+ memcpy( &u8arr->len, c, sizeof(__NBT_U8ARR_LEN) );
+ c += sizeof(__NBT_U8ARR_LEN);
+
+ u8arr->val = c;
+ //printf("->2 %s\n",c );
+ c += u8arr->len;
+
+ nb_add_u8arr( store, u8arr );
+
+ break;
+ }
+ default:
+ printf("Unknown type");
+ }
+ if ( c >= data + size )
+ {
+ iter = 0;
+ }
+ }
+
+ return 0;
+}
+
+int nb_count( netbyte_store *store )
+{
+ if ( store == NULL )
+ {
+ return -1;
+ }
+
+ return store->count;
+}
+
+
+int nb_type( netbyte_store *store, int count, __NBT_TYPED **type )
+{
+ if ( store == NULL )
+ {
+ return -1;
+ }
+
+ if ( store->count < count )
+ {
+ return -2;
+ }
+
+ *type = store->types[count].type; //warnign? who cares
+
+ return 0;
+}
+
+int nb_val( netbyte_store *store, int count, uint8_t **val )
+{
+ if ( store == NULL )
+ return -1;
+
+ if ( store->count < count )
+ return -2;
+
+ *val = store->types[count].nb_val;
+
+ return 0;
+}
+
+
+int nb_fread( netbyte_store *store, int fd)
+{
+ __NBT_SIZE size;
+ ssize_t ret;
+ off_t old_seek;
+ uint8_t *nb;
+
+ if ( store == NULL )
+ return -1;
+
+ if ( fd < 1 )
+ return -2;
+
+
+ nb_init( store );
+
+ old_seek = lseek( fd, 0, SEEK_CUR);
+ if ( read( fd, &size, sizeof(__NBT_SIZE) ) != sizeof(__NBT_SIZE) )
+ {
+ printf("Couldn read netbyte size\n");
+ return -3;
+ }
+
+ if ( lseek( fd, old_seek, SEEK_SET ) == -1 )
+ {
+ return -4;
+ }
+
+ //different endianess can blow this up muahaha
+ nb = malloc( size );
+
+ if ( read( fd, nb, size ) != size )
+ {
+ return -5;
+ }
+
+ if ( nb_load( store, nb ) != 0 )
+ {
+ printf("Cannot read nebyte from file\n");
+ return -6;
+ }
+
+ return 0;
+}
diff --git a/netbytes.h b/netbytes.h
new file mode 100644
index 0000000..cab61f6
--- /dev/null
+++ b/netbytes.h
@@ -0,0 +1,93 @@
+#ifndef __NETBYTES_H
+#define __NETBYTES_H
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+/* types that are supported */
+#define NBT_NONE 0x00 /* maybe not yet prepared type */
+#define NBT_U8 0x01 /* single unsigned 8 bits */
+#define NBT_U8ARRAY 0x02 /* u8 array */
+#define NBT_U16 0x03 /**/
+#define NBT_U16ARRAY 0x04 /**/
+#define NBT_U32 0x05 /**/
+#define NBT_U32ARRAY 0x06 /**/
+#define NBT_U64 0x07 /**/
+#define NBT_U64ARRAY 0x08 /**/
+#define NBT_I8 0x09 /**/
+#define NBT_I8ARRAY 0x0A /**/
+#define NBT_I16 0x0B /**/
+#define NBT_I16ARRAY 0x0C /**/
+#define NBT_I32 0x0D /**/
+#define NBT_I32ARRAY 0x0E /**/
+#define NBT_I64 0x0F /**/
+#define NBT_I64ARRAY 0x10 /**/
+#define NBT_F16 0x11 /**/
+#define NBT_F32 0x12 /**/
+#define NBT_F64 0x13 /**/
+#define NBT_F80 0x14 /**/
+#define NBT_NULL 0x15 /* official empty type */
+#define NBT_LIST 0x16 /* list of data */
+
+
+/* data typed used */
+#define __NBT_SIZE uint32_t
+#define __NBT_TYPED uint8_t
+#define __NBT_U8ARR_LEN uint16_t
+
+#define __NBT_MINIMAL_SIZE (sizeof(__NBT_SIZE))
+#define __NBT_MAX_TYPES (256)
+
+//creating netbyte structure
+typedef struct __nb_type {
+ __NBT_TYPED type;
+ uint8_t *nb_val;
+} __nb_type;
+
+typedef struct netbyte_store
+{
+ __NBT_SIZE size;
+ int count;
+ __nb_type types[__NBT_MAX_TYPES];
+} netbyte_store;
+
+typedef struct nb_u8
+{
+ __NBT_TYPED type;
+ uint8_t val;
+} nb_u8;
+
+typedef struct nb_u8arr
+{
+ __NBT_TYPED type;
+ __NBT_U8ARR_LEN len;
+ uint8_t *val;
+} nb_u8arr;
+
+//loading/parsing netbyte structure
+typedef struct netbyte_load
+{
+ __NBT_SIZE size;
+ uint8_t *buf;
+} netbyte_load;
+
+int nb_u8_create( nb_u8 *s, uint8_t val );
+int nb_u8arr_create( nb_u8arr *s, __NBT_U8ARR_LEN len, uint8_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 );
+uint8_t *nb_create( netbyte_store *store );
+
+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, uint8_t **val );
+int nb_fread( netbyte_store *store, int fd);
+
+#endif \ No newline at end of file