forked from rapier1/hpn-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binn.c
3541 lines (2667 loc) · 83.9 KB
/
binn.c
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <memory.h>
#include "binn.h"
#define UNUSED(x) (void)(x)
#define roundval(dbl) dbl >= 0.0 ? (int)(dbl + 0.5) : ((dbl - (double)(int)dbl) <= -0.5 ? (int)dbl : (int)(dbl - 0.5))
// magic number: 0x1F 0xb1 0x22 0x1F => 0x1FB1221F or 0x1F22B11F
// because the BINN_STORAGE_NOBYTES (binary 000) may not have so many sub-types (BINN_STORAGE_HAS_MORE = 0x10)
#define BINN_MAGIC 0x1F22B11F
#define MAX_BINN_HEADER 9 // [1:type][4:size][4:count]
#define MIN_BINN_SIZE 3 // [1:type][1:size][1:count]
#define CHUNK_SIZE 256 // 1024
#define BINN_STRUCT 1
#define BINN_BUFFER 2
void* (*malloc_fn)(size_t len) = 0;
void* (*realloc_fn)(void *ptr, size_t len) = 0;
void (*free_fn)(void *ptr) = 0;
/***************************************************************************/
#if defined(__alpha__) || defined(__hppa__) || defined(__mips__) || defined(__powerpc__) || defined(__sparc__)
#define BINN_ONLY_ALIGNED_ACCESS
#elif ( defined(__arm__) || defined(__aarch64__) ) && !defined(__ARM_FEATURE_UNALIGNED)
#define BINN_ONLY_ALIGNED_ACCESS
#endif
#if defined(_WIN32)
#define BIG_ENDIAN 0x1000
#define LITTLE_ENDIAN 0x0001
#define BYTE_ORDER LITTLE_ENDIAN
#elif defined(__APPLE__)
/* macros already defined */
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
#include <sys/endian.h>
#elif defined(_AIX)
#include <sys/machine.h>
#else
#include <endian.h>
#endif
#ifndef BYTE_ORDER
#error "BYTE_ORDER not defined"
#endif
#ifndef BIG_ENDIAN
#error "BIG_ENDIAN not defined"
#endif
#ifndef LITTLE_ENDIAN
#error "LITTLE_ENDIAN not defined"
#endif
#if BIG_ENDIAN == LITTLE_ENDIAN
#error "BIG_ENDIAN == LITTLE_ENDIAN"
#endif
#if BYTE_ORDER!=BIG_ENDIAN && BYTE_ORDER!=LITTLE_ENDIAN
#error "BYTE_ORDER not supported"
#endif
typedef unsigned short int u16;
typedef unsigned int u32;
typedef unsigned long long int u64;
BINN_PRIVATE void copy_be16(u16 *pdest, u16 *psource) {
#if BYTE_ORDER == LITTLE_ENDIAN
unsigned char *source = (unsigned char *) psource;
unsigned char *dest = (unsigned char *) pdest;
dest[0] = source[1];
dest[1] = source[0];
#else // if BYTE_ORDER == BIG_ENDIAN
#ifdef BINN_ONLY_ALIGNED_ACCESS
if ((uintptr_t)psource % 2 == 0){ // address aligned to 16 bit
*pdest = *psource;
} else {
unsigned char *source = (unsigned char *) psource;
unsigned char *dest = (unsigned char *) pdest;
dest[0] = source[0]; // indexes are the same
dest[1] = source[1];
}
#else
*pdest = *psource;
#endif
#endif
}
BINN_PRIVATE void copy_be32(u32 *pdest, u32 *psource) {
#if BYTE_ORDER == LITTLE_ENDIAN
unsigned char *source = (unsigned char *) psource;
unsigned char *dest = (unsigned char *) pdest;
dest[0] = source[3];
dest[1] = source[2];
dest[2] = source[1];
dest[3] = source[0];
#else // if BYTE_ORDER == BIG_ENDIAN
#ifdef BINN_ONLY_ALIGNED_ACCESS
if ((uintptr_t)psource % 4 == 0){ // address aligned to 32 bit
*pdest = *psource;
} else {
unsigned char *source = (unsigned char *) psource;
unsigned char *dest = (unsigned char *) pdest;
dest[0] = source[0]; // indexes are the same
dest[1] = source[1];
dest[2] = source[2];
dest[3] = source[3];
}
#else
*pdest = *psource;
#endif
#endif
}
BINN_PRIVATE void copy_be64(u64 *pdest, u64 *psource) {
#if BYTE_ORDER == LITTLE_ENDIAN
unsigned char *source = (unsigned char *) psource;
unsigned char *dest = (unsigned char *) pdest;
int i;
for (i=0; i < 8; i++) {
dest[i] = source[7-i];
}
#else // if BYTE_ORDER == BIG_ENDIAN
#ifdef BINN_ONLY_ALIGNED_ACCESS
if ((uintptr_t)psource % 8 == 0){ // address aligned to 64 bit
*pdest = *psource;
} else {
unsigned char *source = (unsigned char *) psource;
unsigned char *dest = (unsigned char *) pdest;
int i;
for (i=0; i < 8; i++) {
dest[i] = source[i]; // indexes are the same
}
}
#else
*pdest = *psource;
#endif
#endif
}
/***************************************************************************/
#ifndef _MSC_VER
#define stricmp strcasecmp
#define strnicmp strncasecmp
#endif
BINN_PRIVATE BOOL IsValidBinnHeader(const void *pbuf, int *ptype, int *pcount, int *psize, int *pheadersize);
/***************************************************************************/
char * APIENTRY binn_version() {
return BINN_VERSION;
}
/***************************************************************************/
void APIENTRY binn_set_alloc_functions(void* (*new_malloc)(size_t), void* (*new_realloc)(void*,size_t), void (*new_free)(void*)) {
malloc_fn = new_malloc;
realloc_fn = new_realloc;
free_fn = new_free;
}
/***************************************************************************/
BINN_PRIVATE void check_alloc_functions() {
if (malloc_fn == 0) malloc_fn = &malloc;
if (realloc_fn == 0) realloc_fn = &realloc;
if (free_fn == 0) free_fn = &free;
}
/***************************************************************************/
BINN_PRIVATE void * binn_malloc(int size) {
check_alloc_functions();
return malloc_fn(size);
}
/***************************************************************************/
BINN_PRIVATE void * binn_memdup(const void *src, int size) {
void *dest;
if (src == NULL || size <= 0) return NULL;
dest = binn_malloc(size);
if (dest == NULL) return NULL;
memcpy(dest, src, size);
return dest;
}
/***************************************************************************/
BINN_PRIVATE size_t strlen2(char *str) {
if (str == NULL) return 0;
return strlen(str);
}
/***************************************************************************/
int APIENTRY binn_create_type(int storage_type, int data_type_index) {
if (data_type_index < 0) return -1;
if (storage_type < BINN_STORAGE_MIN || storage_type > BINN_STORAGE_MAX) return -1;
if (data_type_index < 16)
return storage_type | data_type_index;
else if (data_type_index < 4096) {
storage_type |= BINN_STORAGE_HAS_MORE;
storage_type <<= 8;
data_type_index >>= 4;
return storage_type | data_type_index;
} else
return -1;
}
/***************************************************************************/
BOOL APIENTRY binn_get_type_info(int long_type, int *pstorage_type, int *pextra_type) {
int storage_type, extra_type;
BOOL retval=TRUE;
again:
if (long_type < 0) {
goto loc_invalid;
} else if (long_type <= 0xff) {
storage_type = long_type & BINN_STORAGE_MASK;
extra_type = long_type & BINN_TYPE_MASK;
} else if (long_type <= 0xffff) {
storage_type = long_type & BINN_STORAGE_MASK16;
storage_type >>= 8;
extra_type = long_type & BINN_TYPE_MASK16;
extra_type >>= 4;
} else if (long_type & BINN_STORAGE_VIRTUAL) {
//storage_type = BINN_STORAGE_VIRTUAL;
//extra_type = xxx;
long_type &= 0xffff;
goto again;
} else {
loc_invalid:
storage_type = -1;
extra_type = -1;
retval = FALSE;
}
if (pstorage_type) *pstorage_type = storage_type;
if (pextra_type) *pextra_type = extra_type;
return retval;
}
/***************************************************************************/
BOOL APIENTRY binn_create(binn *item, int type, int size, void *pointer) {
BOOL retval=FALSE;
switch (type) {
case BINN_LIST:
case BINN_MAP:
case BINN_OBJECT:
break;
default:
goto loc_exit;
}
if (item == NULL || size < 0) goto loc_exit;
if (size < MIN_BINN_SIZE) {
if (pointer) goto loc_exit;
else size = 0;
}
memset(item, 0, sizeof(binn));
if (pointer) {
item->pre_allocated = TRUE;
} else {
item->pre_allocated = FALSE;
if (size == 0) size = CHUNK_SIZE;
pointer = binn_malloc(size);
if (pointer == 0) return INVALID_BINN;
}
item->pbuf = pointer;
item->alloc_size = size;
item->header = BINN_MAGIC;
//item->allocated = FALSE; -- already zeroed
item->writable = TRUE;
item->used_size = MAX_BINN_HEADER; // save space for the header
item->type = type;
//item->count = 0; -- already zeroed
item->dirty = TRUE; // the header is not written to the buffer
retval = TRUE;
loc_exit:
return retval;
}
/***************************************************************************/
binn * APIENTRY binn_new(int type, int size, void *pointer) {
binn *item;
item = (binn*) binn_malloc(sizeof(binn));
if (binn_create(item, type, size, pointer) == FALSE) {
free_fn(item);
return NULL;
}
item->allocated = TRUE;
return item;
}
/*************************************************************************************/
BOOL APIENTRY binn_create_list(binn *list) {
return binn_create(list, BINN_LIST, 0, NULL);
}
/*************************************************************************************/
BOOL APIENTRY binn_create_map(binn *map) {
return binn_create(map, BINN_MAP, 0, NULL);
}
/*************************************************************************************/
BOOL APIENTRY binn_create_object(binn *object) {
return binn_create(object, BINN_OBJECT, 0, NULL);
}
/***************************************************************************/
binn * APIENTRY binn_list() {
return binn_new(BINN_LIST, 0, 0);
}
/***************************************************************************/
binn * APIENTRY binn_map() {
return binn_new(BINN_MAP, 0, 0);
}
/***************************************************************************/
binn * APIENTRY binn_object() {
return binn_new(BINN_OBJECT, 0, 0);
}
/***************************************************************************/
binn * APIENTRY binn_copy(const void *old) {
int type, count, size, header_size;
unsigned char *old_ptr = binn_ptr(old);
binn *item;
size = 0;
if (!IsValidBinnHeader(old_ptr, &type, &count, &size, &header_size)) return NULL;
item = binn_new(type, size - header_size + MAX_BINN_HEADER, NULL);
if( item ){
unsigned char *dest;
dest = ((unsigned char *) item->pbuf) + MAX_BINN_HEADER;
memcpy(dest, old_ptr + header_size, size - header_size);
item->used_size = MAX_BINN_HEADER + size - header_size;
item->count = count;
}
return item;
}
/*************************************************************************************/
// deprecated: unsecure. the size can be corrupted accidentally or intentionally
BOOL APIENTRY binn_load(const void *data, binn *value) {
if (data == NULL || value == NULL) return FALSE;
memset(value, 0, sizeof(binn));
value->header = BINN_MAGIC;
//value->allocated = FALSE; -- already zeroed
//value->writable = FALSE;
if (binn_is_valid(data, &value->type, &value->count, &value->size) == FALSE) return FALSE;
value->ptr = (void*) data;
return TRUE;
}
BOOL APIENTRY binn_load_ex(const void *data, int size, binn *value) {
if (data == NULL || value == NULL || size <= 0) return FALSE;
memset(value, 0, sizeof(binn));
value->header = BINN_MAGIC;
//value->allocated = FALSE; -- already zeroed
//value->writable = FALSE;
if (binn_is_valid_ex(data, &value->type, &value->count, &size) == FALSE) return FALSE;
value->ptr = (void*) data;
value->size = size;
return TRUE;
}
/*************************************************************************************/
binn * APIENTRY binn_open(const void *data) {
binn *item;
item = (binn*) binn_malloc(sizeof(binn));
if (binn_load(data, item) == FALSE) {
free_fn(item);
return NULL;
}
item->allocated = TRUE;
return item;
}
binn * APIENTRY binn_open_ex(const void *data, int size) {
binn *item;
item = (binn*) binn_malloc(sizeof(binn));
if (binn_load_ex(data, size, item) == FALSE) {
free_fn(item);
return NULL;
}
item->allocated = TRUE;
return item;
}
/***************************************************************************/
BINN_PRIVATE int binn_get_ptr_type(const void *ptr) {
if (ptr == NULL) return 0;
switch (*(unsigned int *)ptr) {
case BINN_MAGIC:
return BINN_STRUCT;
default:
return BINN_BUFFER;
}
}
/***************************************************************************/
BOOL APIENTRY binn_is_struct(const void *ptr) {
if (ptr == NULL) return FALSE;
if ((*(unsigned int *)ptr) == BINN_MAGIC) {
return TRUE;
} else {
return FALSE;
}
}
/***************************************************************************/
BINN_PRIVATE int CalcAllocation(int needed_size, int alloc_size) {
int calc_size;
calc_size = alloc_size;
while (calc_size < needed_size) {
calc_size <<= 1; // same as *= 2
//calc_size += CHUNK_SIZE; -- this is slower than the above line, because there are more reallocations
}
return calc_size;
}
/***************************************************************************/
BINN_PRIVATE BOOL CheckAllocation(binn *item, int add_size) {
int alloc_size;
void *ptr;
if (item->used_size + add_size > item->alloc_size) {
if (item->pre_allocated) return FALSE;
alloc_size = CalcAllocation(item->used_size + add_size, item->alloc_size);
ptr = realloc_fn(item->pbuf, alloc_size);
if (ptr == NULL) return FALSE;
item->pbuf = ptr;
item->alloc_size = alloc_size;
}
return TRUE;
}
/***************************************************************************/
#if BYTE_ORDER == BIG_ENDIAN
BINN_PRIVATE int get_storage_size(int storage_type) {
switch (storage_type) {
case BINN_STORAGE_NOBYTES:
return 0;
case BINN_STORAGE_BYTE:
return 1;
case BINN_STORAGE_WORD:
return 2;
case BINN_STORAGE_DWORD:
return 4;
case BINN_STORAGE_QWORD:
return 8;
default:
return 0;
}
}
#endif
/***************************************************************************/
BINN_PRIVATE unsigned char * AdvanceDataPos(unsigned char *p, unsigned char *plimit) {
unsigned char byte;
int storage_type, DataSize;
if (p > plimit) return 0;
byte = *p; p++;
storage_type = byte & BINN_STORAGE_MASK;
if (byte & BINN_STORAGE_HAS_MORE) p++;
switch (storage_type) {
case BINN_STORAGE_NOBYTES:
//p += 0;
break;
case BINN_STORAGE_BYTE:
p ++;
break;
case BINN_STORAGE_WORD:
p += 2;
break;
case BINN_STORAGE_DWORD:
p += 4;
break;
case BINN_STORAGE_QWORD:
p += 8;
break;
case BINN_STORAGE_BLOB:
case BINN_STORAGE_STRING:
if (p > plimit) return 0;
DataSize = *((unsigned char*)p);
if (DataSize & 0x80) {
if (p + sizeof(int) - 1 > plimit) return 0;
copy_be32((u32*)&DataSize, (u32*)p);
DataSize &= 0x7FFFFFFF;
p+=4;
} else {
p++;
}
p += DataSize;
if (storage_type == BINN_STORAGE_STRING) {
p++; // null terminator.
}
break;
case BINN_STORAGE_CONTAINER:
if (p > plimit) return 0;
DataSize = *((unsigned char*)p);
if (DataSize & 0x80) {
if (p + sizeof(int) - 1 > plimit) return 0;
copy_be32((u32*)&DataSize, (u32*)p);
DataSize &= 0x7FFFFFFF;
}
DataSize--; // remove the type byte already added before
p += DataSize;
break;
default:
return 0;
}
return p;
}
/***************************************************************************/
/*
The id can be stored with 1 to 5 bytes
S = signal bit
X = bit part of id
0SXX XXXX
100S XXXX + 1 byte
101S XXXX + 2 bytes
110S XXXX + 3 bytes
1110 0000 + 4 bytes
*/
BINN_PRIVATE int read_map_id(unsigned char **pp, unsigned char *plimit) {
unsigned char *p, c, sign, type;
int id, extra_bytes;
p = *pp;
if (p > plimit) return 0;
c = *p++;
if (c & 0x80) {
extra_bytes = ((c & 0x60) >> 5) + 1;
if (p + extra_bytes > plimit ) {
*pp = p + extra_bytes;
return 0;
}
}
type = c & 0xE0;
sign = c & 0x10;
if ((c & 0x80) == 0) {
sign = c & 0x40;
id = c & 0x3F;
} else if (type == 0x80) {
id = c & 0x0F;
id = (id << 8) | *p++;
} else if (type == 0xA0) {
id = c & 0x0F;
id = (id << 8) | *p++;
id = (id << 8) | *p++;
} else if (type == 0xC0) {
id = c & 0x0F;
id = (id << 8) | *p++;
id = (id << 8) | *p++;
id = (id << 8) | *p++;
} else if (type == 0xE0) {
copy_be32((u32*)&id, (u32*)p);
p += 4;
} else {
*pp = plimit + 2;
return 0;
}
if (sign) id = -id;
*pp = p;
return id;
}
/***************************************************************************/
BINN_PRIVATE unsigned char * SearchForID(unsigned char *p, int header_size, int size, int numitems, int id) {
unsigned char *plimit, *base;
int i, int32;
base = p;
plimit = p + size - 1;
p += header_size;
// search for the ID in all the arguments.
for (i = 0; i < numitems; i++) {
int32 = read_map_id(&p, plimit);
if (p > plimit) break;
// Compare if the IDs are equal.
if (int32 == id) return p;
// xxx
p = AdvanceDataPos(p, plimit);
if (p == 0 || p < base) break;
}
return NULL;
}
/***************************************************************************/
BINN_PRIVATE unsigned char * SearchForKey(unsigned char *p, int header_size, int size, int numitems, const char *key) {
unsigned char len, *plimit, *base;
int i, keylen;
base = p;
plimit = p + size - 1;
p += header_size;
keylen = strlen(key);
// search for the key in all the arguments.
for (i = 0; i < numitems; i++) {
if (p > plimit) break;
len = *((unsigned char *)p);
p++;
if (p + len > plimit) break;
// Compare if the strings are equal.
if (len > 0) {
if (strnicmp((char*)p, key, len) == 0) { // note that there is no null terminator here
if (keylen == len) {
p += len;
return p;
}
}
p += len;
} else if (len == keylen) { // in the case of empty string: ""
return p;
}
// xxx
p = AdvanceDataPos(p, plimit);
if (p == 0 || p < base) break;
}
return NULL;
}
/***************************************************************************/
BINN_PRIVATE BOOL AddValue(binn *item, int type, void *pvalue, int size);
/***************************************************************************/
BINN_PRIVATE BOOL binn_list_add_raw(binn *item, int type, void *pvalue, int size) {
if (item == NULL || item->type != BINN_LIST || item->writable == FALSE) return FALSE;
//if (CheckAllocation(item, 4) == FALSE) return FALSE; // 4 bytes used for data_store and data_format.
if (AddValue(item, type, pvalue, size) == FALSE) return FALSE;
item->count++;
return TRUE;
}
/***************************************************************************/
BINN_PRIVATE BOOL binn_object_set_raw(binn *item, const char *key, int type, void *pvalue, int size) {
unsigned char *p, len;
int int32;
if (item == NULL || item->type != BINN_OBJECT || item->writable == FALSE) return FALSE;
if (key == NULL) return FALSE;
int32 = strlen(key);
if (int32 > 255) return FALSE;
// is the key already in it?
p = SearchForKey(item->pbuf, MAX_BINN_HEADER, item->used_size, item->count, key);
if (p) return FALSE;
// start adding it
if (CheckAllocation(item, 1 + int32) == FALSE) return FALSE; // bytes used for the key size and the key itself.
p = ((unsigned char *) item->pbuf) + item->used_size;
len = int32;
*p = len;
p++;
memcpy(p, key, int32);
int32++; // now contains the strlen + 1 byte for the len
item->used_size += int32;
if (AddValue(item, type, pvalue, size) == FALSE) {
item->used_size -= int32;
return FALSE;
}
item->count++;
return TRUE;
}
/***************************************************************************/
BINN_PRIVATE BOOL binn_map_set_raw(binn *item, int id, int type, void *pvalue, int size) {
unsigned char *base, *p, sign;
int id_size;
if (item == NULL || item->type != BINN_MAP || item->writable == FALSE) return FALSE;
// is the ID already in it?
p = SearchForID(item->pbuf, MAX_BINN_HEADER, item->used_size, item->count, id);
if (p) return FALSE;
// start adding it
if (CheckAllocation(item, 5) == FALSE) return FALSE; // max 5 bytes used for the id.
p = base = ((unsigned char *) item->pbuf) + item->used_size;
sign = (id < 0);
if (sign) id = -id;
if (id <= 0x3F) {
*p++ = (sign << 6) | id;
} else if (id <= 0xFFF) {
*p++ = 0x80 | (sign << 4) | ((id & 0xF00) >> 8);
*p++ = id & 0xFF;
} else if (id <= 0xFFFFF) {
*p++ = 0xA0 | (sign << 4) | ((id & 0xF0000) >> 16);
*p++ = (id & 0xFF00) >> 8;
*p++ = id & 0xFF;
} else if (id <= 0xFFFFFFF) {
*p++ = 0xC0 | (sign << 4) | ((id & 0xF000000) >> 24);
*p++ = (id & 0xFF0000) >> 16;
*p++ = (id & 0xFF00) >> 8;
*p++ = id & 0xFF;
} else {
*p++ = 0xE0;
if (sign) id = -id;
copy_be32((u32*)p, (u32*)&id);
p += 4;
}
id_size = (p - base);
item->used_size += id_size;
if (AddValue(item, type, pvalue, size) == FALSE) {
item->used_size -= id_size;
return FALSE;
}
item->count++;
return TRUE;
}
/***************************************************************************/
BINN_PRIVATE void * compress_int(int *pstorage_type, int *ptype, void *psource) {
int storage_type, storage_type2, type, type2=0;
int64 vint = 0;
uint64 vuint;
char *pvalue;
#if BYTE_ORDER == BIG_ENDIAN
int size1, size2;
#endif
storage_type = *pstorage_type;
if (storage_type == BINN_STORAGE_BYTE) return psource;
type = *ptype;
switch (type) {
case BINN_INT64:
vint = *(int64*)psource;
goto loc_signed;
case BINN_INT32:
vint = *(int*)psource;
goto loc_signed;
case BINN_INT16:
vint = *(short*)psource;
goto loc_signed;
case BINN_UINT64:
vuint = *(uint64*)psource;
goto loc_positive;
case BINN_UINT32:
vuint = *(unsigned int*)psource;
goto loc_positive;
case BINN_UINT16:
vuint = *(unsigned short*)psource;
goto loc_positive;
}
loc_signed:
if (vint >= 0) {
vuint = vint;
goto loc_positive;
}
//loc_negative:
if (vint >= INT8_MIN) {
type2 = BINN_INT8;
} else
if (vint >= INT16_MIN) {
type2 = BINN_INT16;
} else
if (vint >= INT32_MIN) {
type2 = BINN_INT32;
}
goto loc_exit;
loc_positive:
if (vuint <= UINT8_MAX) {
type2 = BINN_UINT8;
} else
if (vuint <= UINT16_MAX) {
type2 = BINN_UINT16;
} else
if (vuint <= UINT32_MAX) {
type2 = BINN_UINT32;
}
loc_exit:
pvalue = (char *) psource;
if (type2 && type2 != type) {
*ptype = type2;
storage_type2 = binn_get_write_storage(type2);
*pstorage_type = storage_type2;
#if BYTE_ORDER == BIG_ENDIAN
size1 = get_storage_size(storage_type);
size2 = get_storage_size(storage_type2);
pvalue += (size1 - size2);
#endif
}
return pvalue;
}
/***************************************************************************/
BINN_PRIVATE int type_family(int type);
BINN_PRIVATE BOOL AddValue(binn *item, int type, void *pvalue, int size) {
int int32, ArgSize, storage_type, extra_type;
unsigned char *p;
binn_get_type_info(type, &storage_type, &extra_type);
if (pvalue == NULL) {
switch (storage_type) {
case BINN_STORAGE_NOBYTES:
break;
case BINN_STORAGE_BLOB:
case BINN_STORAGE_STRING:
if (size == 0) break; // the 2 above are allowed to have 0 length
/* fall through */
default:
return FALSE;
}
}
if (type_family(type) == BINN_FAMILY_INT && item->disable_int_compression == FALSE)
pvalue = compress_int(&storage_type, &type, pvalue);
switch (storage_type) {
case BINN_STORAGE_NOBYTES:
size = 0;
ArgSize = size;
break;
case BINN_STORAGE_BYTE:
size = 1;
ArgSize = size;
break;
case BINN_STORAGE_WORD:
size = 2;
ArgSize = size;
break;
case BINN_STORAGE_DWORD:
size = 4;
ArgSize = size;
break;
case BINN_STORAGE_QWORD:
size = 8;
ArgSize = size;
break;
case BINN_STORAGE_BLOB:
if (size < 0) return FALSE;
//if (size == 0) ...
ArgSize = size + 4; // at least this size
break;
case BINN_STORAGE_STRING:
if (size < 0) return FALSE;
if (size == 0) size = strlen2( (char *) pvalue);
ArgSize = size + 5; // at least this size
break;
case BINN_STORAGE_CONTAINER:
if (size <= 0) return FALSE;
ArgSize = size;
break;
default:
return FALSE;
}