summaryrefslogtreecommitdiff
path: root/netbytes.h
blob: 6e008bc73b3feec5c5496d627b63a589cb70d08c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef __NETBYTES_H
#define __NETBYTES_H

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <ctype.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 /* unsigned 16 bit */
#define NBT_U16ARRAY	0x04 /* u16 array */
#define NBT_U32 	0x05 /* unsigned 32 bit */
#define NBT_U32ARRAY	0x06 /* u32 array */
#define NBT_U64 	0x07 /* unsigned 64 bit */
#define NBT_U64ARRAY 	0x08 /* u64 array */
#define NBT_I8 		0x09 /* signed 8 bits */
#define NBT_I8ARRAY	0x0A /* i8 array */
#define NBT_I16 	0x0B /* signed 16bits */
#define NBT_I16ARRAY	0x0C /* i16 array */
#define NBT_I32 	0x0D /* signed 32 bits */
#define NBT_I32ARRAY 	0x0E /* i32 array */
#define NBT_I64 	0x0F /* signed 64 bits */
#define NBT_I64ARRAY 	0x10 /* i64 array */
#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_U16ARR_LEN	uint16_t
#define __NBT_U32ARR_LEN	uint16_t
#define __NBT_U64ARR_LEN	uint16_t

#define __NBT_MINIMAL_SIZE	(sizeof(__NBT_SIZE))
#define __NBT_MAX_TYPES         (256)

//creating netbyte structure
typedef struct __attribute__((packed)) __nb_type {
	__NBT_TYPED	 type;
	uint8_t		*nb_val;
} __nb_type ;

typedef struct __attribute__((packed)) netbyte_store
{
	__NBT_SIZE 	size;
	int		count;
	__nb_type	types[__NBT_MAX_TYPES];
} netbyte_store;

typedef struct __attribute__((packed)) nb_u8
{
	__NBT_TYPED   type;
	uint8_t       val;
} nb_u8;

typedef struct __attribute__((packed)) nb_u8arr
{
	__NBT_TYPED     type;
	__NBT_U8ARR_LEN len;
	uint8_t         *val;
} nb_u8arr;


/*-----------------*/
typedef struct __attribute__((packed)) nb_u16
{
	__NBT_TYPED    type;
	uint16_t       val;
} nb_u16;

typedef struct __attribute__((packed)) nb_u16arr
{
	__NBT_TYPED       type;
	__NBT_U16ARR_LEN  len;
	uint16_t         *val;
} nb_u16arr;


/*-----------------*/
typedef struct __attribute__((packed)) nb_u32
{
	__NBT_TYPED    type;
	uint32_t       val;
} nb_u32;

typedef struct __attribute__((packed)) nb_u32arr
{
	__NBT_TYPED       type;
	__NBT_U32ARR_LEN  len;
	uint32_t         *val;
} nb_u32arr;


/*------------------*/
typedef struct __attribute__((packed)) nb_u64
{
	__NBT_TYPED    type;
	uint64_t       val;
} nb_u64;

typedef struct __attribute__((packed)) nb_u64arr
{
	__NBT_TYPED       type;
	__NBT_U64ARR_LEN  len;
	uint64_t         *val;
} nb_u64arr;

//loading/parsing netbyte structure
typedef struct __attribute__((packed)) netbyte_load
{
	__NBT_SIZE  size;
	uint8_t    *buf;
} netbyte_load;

typedef struct nb_tok
{
	int sign;
	int type_size;
	int len;
	int arr;
} nb_tok;

typedef struct nb_tok_arr
{
	int     size;
	int     len;
	nb_tok *tok;
} nb_tok_arr;

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_u16_create(    nb_u16 *s,    uint16_t val );
int nb_u16arr_create( nb_u16arr *s, __NBT_U16ARR_LEN len, uint16_t *val );
int nb_u32_create(    nb_u32 *s,    uint32_t val );
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_add_u16arr( netbyte_store *store, nb_u16arr *u16arr );
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_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_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_fread( netbyte_store *store, int fd);

//print all all values in netbyte string
int nb_print(netbyte_store *store);

/*
check if netbyte matches particular format
-1 err
1 not match
0 match
*/
int nb_match(netbyte_store *store, nb_tok_arr *pattern);

#define SIGN_UNSIGNED 1
#define SIGN_SIGNED   2

nb_tok_arr *nb_tok_create(int size);
int nb_tok_add(nb_tok_arr *arr, int sign, int type_size, int len, int farr);
void nb_tok_destroy(nb_tok_arr *arr);

int nb_parse(char *str, nb_tok_arr *arr);

#endif