summaryrefslogtreecommitdiff
path: root/netbytes.c
blob: dca7d2233a6c6df22c027dd13e1dcccdd3516491 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
#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 )
{
	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;
}


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;
	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) + sizeof(uint8_t);
			break;
		case NBT_U8ARRAY:
			{
				nb_u8arr *u8arr = (nb_u8arr *)iter.nb_val;
				store->size += sizeof(__NBT_TYPED) + 
					sizeof(__NBT_U8ARR_LEN) +
					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");
		}
	}

	/* 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;
		}
		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");
		}
	}

	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;
		}
		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");
		}
		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;
}