-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibex_byte_array.c
More file actions
executable file
·736 lines (557 loc) · 24.7 KB
/
Copy pathlibex_byte_array.c
File metadata and controls
executable file
·736 lines (557 loc) · 24.7 KB
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
/* -------------------------------------------------------------------------- */
/* (c) ali@balarabe.com [libex_byte_array.c] */
/* -------------------------------------------------------------------------- */
#include "libex_c_precompiled.h"
#include "libex_byte_array_.h"
#if defined INCLUDED_LIBEX_BYTE_ARRAY_H
#include <stdio.h> /* for sprintf() */
#include <string.h> /* for memset() */
#include "libex_.h"
#include "libex_call_.h"
#include "libex_debug_.h"
#include "libex_error_.h"
#include "libex_macro_.h"
#include "libex_stringc_.h"
#include "libex_type_.h"
#if defined __cplusplus_cli
#pragma unmanaged
#endif
#if defined _MSC_VER
#pragma warning (disable:4710) /* W:L4 function not inlined */
#endif
/* -------------------------------------------------------------------------- */
NAMESPACE(c_)
typedef struct _byte_array_t {
uint8_t* mem; /* _byte_array_t */
/* Pointer to allocated memory block */
size_t size; /* _byte_array_t */
/* Size of allocated memory block, in bytes */
size_t pos; /* _byte_array_t */
/* The current read/write postion */
bool is_compressed; /* _byte_array_t */
/* If true, this is a compressed block */
char_t* temp_chars; /* _byte_array_t */
/* used by readAsciiString() and ReadHexString() */
}
_byte_array_t;
/* -------------------------------------------------------------------------- */
/* Functions: Private */
#define _BASE_OBJ_WRAP_INC
#define _OBJ_HANDLE_TYPE byte_array_t
#define _OBJ_INNER_TYPE _byte_array_t
#define _OBJ_ALLOC_FN_UID UID(FF1546)
#define _OBJ_FREE_FN_UID UID(F86574)
#define _OBJ_CONST_FN_UID UID(F432A6)
#define _OBJ_FN_UID UID(F2072E)
#define _OBJ_WRAP_FN_UID UID(FC8BB7)
#include "libex_object_wrapper.inc.c"
/* -------------------------------------------------------------------------- */
/* Constructors: */
PUBLIC new_byte_array_t ByteArray_init( void ) { /*C*/
GO (UID(C994E3));
_byte_array_t* ob = _object_alloc();
/* _object_alloc() zeros everything, so all data members */
/* will be NULL, 0 or false, as appropriate */
new_byte_array_t ret = _object_wrap(ob);
RETURN(ret);
} /* ByteArray_init */
PUBLIC void ByteArray_setCopy( /*C*/
byte_array_t object_, /*-*/
const byte_array_t copy_from_ ) { /*-*/
GO (UID(FF3C6E));
_byte_array_t* const me = _object(&object_, UID(E75B52));
const _byte_array_t* const copy_from = _object(©_from_, UID(E02BF1));
if (copy_from == me) {
RETURN(NIL);
}
ByteArray_clear(object_);
if (copy_from->size > 0) {
me->mem = CAST(uint8_t*, MALLOC(copy_from->size));
}
me->size = copy_from->size;
me->pos = copy_from->pos;
me->is_compressed = copy_from->is_compressed;
if (copy_from->size > 0) {
MEMCPY(me->mem, copy_from->mem, me->size);
}
RETURN(NIL);
} /* ByteArray_setCopy */
PUBLIC new_byte_array_t ByteArray_newCopy( /*C*/
const byte_array_t copy_from_ ) { /*-*/
GO (UID(F6F6BB));
new_byte_array_t ret = ByteArray_init();
ByteArray_setCopy(ret, copy_from_);
RETURN(ret);
} /* ByteArray_newCopy */
/* -------------------------------------------------------------------------- */
/* Destructor: */
PUBLIC void ByteArray_free( byte_array_t* object_ ) { /*D*/
GO (UID(F4100F));
_byte_array_t* const me = _object(object_, UID(E5C0BF));
if (me->mem != NULL) {
FREE(me->mem);
}
if (me->temp_chars != NULL) {
FREE(me->temp_chars);
}
_object_free(object_);
RETURN(NIL);
} /* ByteArray_free */
/* -------------------------------------------------------------------------- */
/* Properties: */
PUBLIC bool ByteArray_bof( const byte_array_t object_ ) { /*P*/
GO (UID(FB3065));
_byte_array_t* const me = _object(&object_, UID(E79125));
const bool ret = (me->pos == 0);
RETURN(ret);
} /* ByteArray_bof */
PUBLIC bool ByteArray_eof( const byte_array_t object_ ) { /*P*/
GO (UID(FFFE85));
_byte_array_t* const me = _object(&object_, UID(E1C373));
const bool ret = (me->pos == (me->size - 1));
RETURN(ret);
} /* ByteArray_eof */
PUBLIC size_t ByteArray_getPosition( const byte_array_t object_ ) { /*P*/
GO (UID(F500AE));
const _byte_array_t* const me = _object_const(&object_, UID(E4BF66));
const size_t ret = me->pos;
RETURN(ret);
} /* ByteArray_getPosition */
PUBLIC bool ByteArray_setPosition( /*P*/
byte_array_t object_, /*-*/
const size_t new_value_ ) { /*-*/
GO (UID(FAB286));
_byte_array_t* const me = _object(&object_, UID(EB382F));
if (new_value_ > (me->size - 1)) {
RETURN(false);
}
me->pos = new_value_;
RETURN(true);
} /* ByteArray_setPosition */
PUBLIC size_t ByteArray_getSize( const byte_array_t object_ ) { /*P*/
GO (UID(F620C3));
const _byte_array_t* const me = _object_const(&object_, UID(E1F273));
RETURN(me->size);
} /* ByteArray_getSize */
PUBLIC bool ByteArray_setSize( /*P*/
byte_array_t object_, /*-*/
const size_t new_value_ ) { /*-*/
GO (UID(F4646C));
_byte_array_t* const me = _object(&object_, UID(EA3288));
size_t new_size = 0;
uint8_t* bytes = NULL;
if (new_value_ == me->size) {
RETURN(true);
}
if (new_value_ < me->size) {
me->size = new_value_;
RETURN(true);
}
/* allocate memory */
new_size = new_value_;
bytes = CAST(uint8_t*, MALLOC(new_size));
if (bytes == NULL) {
new_size = 0;
}
if (new_size == 0) {
RETURN(false);
}
/* set all allocated bytes to 0 */
MEMSET(bytes, 0x00, new_size);
/* copy old buffer to bytes buffer, and release the old buffer */
if (me->mem != NULL) {
if (me->size > 0) {
MEMCPY(bytes, me->mem, me->size);
}
FREE(me->mem);
}
me->mem = bytes;
me->size = new_size;
RETURN(true);
} /* ByteArray_setSize */
/* returns true if the size and contents of the given block are identical */
/* to this memory block. (The current read/write position is ignored) */
PUBLIC bool ByteArray_equal( /*P*/
const byte_array_t object_, /*-*/
const byte_array_t compare_ ) { /*-*/
GO (UID(F4F050));
const _byte_array_t* const me = _object_const(&object_, UID(E45186));
const _byte_array_t* const cmp = _object_const(&compare_, UID(EC8499));
bool ret = false;
if (me->size == cmp->size && me->is_compressed == cmp->is_compressed) {
ret = MEMCMP(cmp->mem, me->mem, me->size) == 0;
}
RETURN(ret);
} /* ByteArray_equal */
PUBLIC bool ByteArray_matches( /*M*/
const byte_array_t object_, /*-*/
const char* ascii_chars_, /*-*/
const size_t length_ ) { /*-*/
GO (UID(FD88C6));
const _byte_array_t* const me = _object_const(&object_, UID(EDA43C));
const uint8_t* data = NULL;
const uint8_t* ascii = CAST(const uint8_t*, ascii_chars_);
size_t size = length_;
size_t i = 0;
if (size == 0) {
size = STRLEN(ascii_chars_);
}
if (size == 0 || me->pos + size > me->size) {
RETURN(false);
}
data = me->mem + me->pos;
for (i = 0; i < size; i++) {
if (*ascii++ != *data++) {
RETURN(false);
}
}
RETURN(true);
} /* ByteArray_matches */
PUBLIC bool ByteArray_isCompressed( const byte_array_t object_ ) { /*P*/
GO (UID(F0D9B5));
_byte_array_t* const me = _object(&object_, UID(E802E9));
RETURN(me->is_compressed);
} /* ByteArray_isCompressed */
/* -------------------------------------------------------------------------- */
/* Methods: */
PUBLIC uint8_t* ByteArray_getBuffer( byte_array_t object_ ) { /*M*/
GO (UID(F5E439));
_byte_array_t* const me = _object(&object_, UID(EC10D1));
RETURN(me->mem);
} /* ByteArray_getBuffer */
PUBLIC void ByteArray_clear( byte_array_t object_ ) { /*M*/
GO (UID(F087F4));
_byte_array_t* const me = _object(&object_, UID(EA080A));
if (me != NULL && me->mem) {
FREE(me->mem);
me->mem = NULL;
me->size = 0;
me->pos = 0;
me->is_compressed = false;
freeT(&me->temp_chars);
}
RETURN(NIL);
} /* ByteArray_clear */
PUBLIC void ByteArray_set( /*M*/
byte_array_t object_, /*-*/
const size_t size_in_bytes_, /*-*/
const uint8_t fill_ ) { /*-*/
GO (UID(F59140));
_byte_array_t* const me = _object(&object_, UID(EB88D4));
ByteArray_clear(object_);
me->size = size_in_bytes_;
me->mem = CAST(uint8_t*, MALLOC(me->size));
MEMSET(me->mem, fill_, me->size); /* fill memory */
RETURN(NIL);
} /* ByteArray_set */
PUBLIC void ByteArray_copyBytes( /*M*/
byte_array_t object_, /*-*/
const void* bytes_, /*-*/
const size_t size_in_bytes_, /*-*/
const bool compressed_ ) { /*-*/
GO (UID(FB17AC));
_byte_array_t* const me = _object(&object_, UID(ED46D4));
ByteArray_clear(object_);
me->size = size_in_bytes_;
me->mem = CAST(uint8_t*, MALLOC(size_in_bytes_));
me->pos = 0;
me->is_compressed = compressed_;
MEMCPY(me->mem, bytes_, size_in_bytes_);
RETURN(NIL);
} /* ByteArray_copyBytes */
/* Sets all bytes in the memory block to the specified value. */
PUBLIC void ByteArray_fill( /*M*/
byte_array_t object_, /*-*/
const uint8_t value_ ) { /*-*/
GO (UID(FDFA42));
_byte_array_t* const me = _object(&object_, UID(E8FD5D));
MEMSET(me->mem, value_, me->size);
RETURN(NIL);
} /* ByteArray_fill */
PUBLIC uint8_t* ByteArray_detachBuffer( byte_array_t object_ ) { /*M*/
GO (UID(FB41D4));
_byte_array_t* const me = _object(&object_, UID(EE59B2));
uint8_t* const ret = me->mem;
me->mem = NULL;
me->size = 0;
me->pos = 0;
me->is_compressed = false;
RETURN(ret);
} /* ByteArray_detachBuffer */
PUBLIC bool ByteArray_goBegin( byte_array_t object_ ) { /*M*/
GO (UID(FCAC83));
_byte_array_t* const me = _object(&object_, UID(E49036));
me->pos = 0;
RETURN(true);
} /* ByteArray_goBegin */
PUBLIC bool ByteArray_goEnd( byte_array_t object_ ) { /*M*/
GO (UID(F5D647));
_byte_array_t* const me = _object(&object_, UID(EFBC3A));
if (me->size == 0) {
me->pos = 0;
} else {
me->pos = me->size - 1;
}
RETURN(true);
} /* ByteArray_goEnd */
PUBLIC bool ByteArray_goBack( /*M*/
byte_array_t object_, /*-*/
const size_t count_ ) { /*-*/
GO (UID(F015BC));
_byte_array_t* const me = _object(&object_, UID(E62ADC));
if (me->pos <= count_) {
DEBUG_WARN(_T("Invalid position."), UID(E09C7B));
RETURN(false);
}
me->pos -= count_;
RETURN(true);
} /* ByteArray_goBack */
PUBLIC bool ByteArray_goForward( /*M*/
byte_array_t object_, /*-*/
const size_t count_ ) { /*-*/
GO (UID(F40646));
_byte_array_t* const me = _object(&object_, UID(E3FE17));
if (me->pos + count_ >= me->size) {
DEBUG_WARN(_T("Invalid position."), UID(ED14F5));
RETURN(false);
}
me->pos += count_;
RETURN(true);
} /* ByteArray_goForward */
PUBLIC bool ByteArray_writeInt( /*M*/
byte_array_t object_, /*-*/
const int value_ ) { /*-*/
GO (UID(F7E8E6));
_byte_array_t* const me = _object(&object_, UID(E82483));
if ((me->pos + sizeof(int)) > me->size) {
RETURN(false);
}
MEMCPY(me->mem + me->pos, &value_, sizeof(int));
me->pos += sizeof(int);
RETURN(true);
} /* ByteArray_writeInt */
/*
byte_1_out_ = NULL,
byte_2_out_ = NULL,
byte_3_out_ = NULL,
byte_4_out_ = NULL
*/
PUBLIC uint32_t ByteArray_readUnsigned( /*M*/
byte_array_t object_, /*-*/
const size_t bytes_, /*-*/
uint8_t* byte_1_out_, /*-*/
uint8_t* byte_2_out_, /*-*/
uint8_t* byte_3_out_, /*-*/
uint8_t* byte_4_out_ ) { /*-*/
GO (UID(F937A0));
_byte_array_t* const me = _object(&object_, UID(EB02FF));
if (bytes_ < 1 || bytes_ > 4) {
DEBUG_WARN(_T("Invalid bytes_ size."), UID(EF4306));
RETURN(0);
}
if (me->pos + bytes_ > me->size) {
DEBUG_WARN(_T("Read past end of buffer."), UID(E1101F));
RETURN(0);
}
if (byte_2_out_ != NULL && bytes_ < 2) {
DEBUG_WARN(_T("Invalid arguments."), UID(E22137));
RETURN(0);
}
if (byte_3_out_ != NULL && bytes_ < 3) {
DEBUG_WARN(_T("Invalid arguments."), UID(E01430));
RETURN(0);
}
if (byte_4_out_ != NULL && bytes_ < 4) {
DEBUG_WARN(_T("Invalid arguments."), UID(E0D005));
RETURN(0);
} else {
uint32_t ret = 0;
uint32_t power = 1;
size_t i = 0;
for (i = 1; i <= bytes_; i++) {
const uint8_t val = *(me->mem + me->pos + bytes_ - i);
switch (i) {
case 1:
if (byte_1_out_ != NULL) {
*byte_1_out_ = val;
}
break;
case 2:
if (byte_2_out_ != NULL) {
*byte_2_out_ = val;
}
break;
case 3:
if (byte_3_out_ != NULL) {
*byte_3_out_ = val;
}
break;
case 4:
if (byte_4_out_ != NULL) {
*byte_4_out_ = val;
}
break;
default:
DEBUG_WARN(_T("Invalid arguments."), UID(E01349));
}
ret += (val * power);
power *= 256;
}
me->pos += bytes_;
RETURN(ret);
}
} /* ByteArray_readUnsigned */
PUBLIC uint32_t ByteArray_readUnsignedVariable( /*M*/
byte_array_t object_, /*-*/
size_t* length_in_bytes_out_ ) { /*-*/
GO (UID(F35620));
_byte_array_t* const me = _object(&object_, UID(E7ED68));
size_t i = 0;
size_t len = 0;
uint8_t val = 0;
uint32_t power = 0;
uint32_t ret = 0;
if (length_in_bytes_out_) {
*length_in_bytes_out_ = 0;
}
/* determine the length of the variable-length number */
/* 4 is the maximum length of variable-length numbers */
for (i = 0; i < 4; i++) {
/* warn if the variable-length number exceeds the end of buffer */
if (me->pos + i > me->size) {
DEBUG_WARN(_T("Read past end of buffer."), UID(E7052D));
RETURN(0);
}
val = *(me->mem + me->pos + i);
len++;
if (!(val & 0x80)) {
break;
}
}
/* make sure the length does not exceed 4 bytes */
if (len == 4 && !(*(me->mem + me->pos + 4) & 0x80)) {
DEBUG_WARN(_T("Variable-length longer than 4 bytes."), UID(E9EFB5));
RETURN(0);
}
power = 1;
for (i = 1; i <= len; i++) {
/* only 7 bits of the byte store the value */
const size_t num = *(me->mem + me->pos + len - i) & 0x7F;
ret += (num * power);
power *= 128;
}
me->pos += len;
if (length_in_bytes_out_) {
*length_in_bytes_out_ = len;
}
RETURN(ret);
} /* ByteArray_readUnsignedVariable */
PUBLIC chars_t ByteArray_readAsciiString( /*M*/
byte_array_t object_, /*-*/
const size_t count_ ) { /*-*/
GO (UID(F9AB18));
_byte_array_t* const me = _object(&object_, UID(E3D264));
char_t* new_buf = NULL;
size_t i = 0;
if (count_ == 0) {
RETURN(NULL);
}
/* warn if the length to read exceeds the end of buffer */
if (me->pos + count_ > me->size) {
DEBUG_WARN(_T("Read past end of buffer."), UID(EFC83E));
RETURN(NULL);
}
/* release buffer, if previously allocated */
freeT(&me->temp_chars);
/* allocate a new buffer (+ 1 is for \0) */
new_buf = CAST(char_t*, MALLOC((count_ + 1) * sizeof(char_t)));
/* copy ASCII characters to new buffer */
for (i = 0; i < count_; i++) {
*(new_buf + i) = *(me->mem + me->pos + i);
}
*(new_buf + count_) = '\0';
/* store buffer address and advance current position */
me->temp_chars = new_buf;
me->pos += count_;
{
chars_t ret = me->temp_chars;
RETURN(ret);
}
} /* ByteArray_readAsciiString */
PUBLIC bool ByteArray_writeUnsignedInt( /*M*/
byte_array_t object_, /*-*/
const uint32_t value_ ) { /*-*/
GO (UID(F948AF));
_byte_array_t* const me = _object(&object_, UID(E35F18));
if ((me->pos + sizeof(uint32_t)) > me->size) {
RETURN(false);
}
MEMCPY(me->mem + me->pos, &value_, sizeof(uint32_t));
me->pos += sizeof(uint32_t);
RETURN(true);
} /* ByteArray_writeUnsignedInt */
PUBLIC bool ByteArray_writeString( /*M*/
byte_array_t object_, /*-*/
chars_t text_, /*-*/
const size_t max_len_ ) { /*-*/
GO (UID(F8D227));
_byte_array_t* const me = _object(&object_, UID(E79A2E));
const size_t size = LESSER(STRLEN_T(text_), max_len_) * sizeof(char_t);
if ((me->pos + size + 1) > me->size) {
RETURN(false);
}
MEMCPY(me->mem + me->pos, text_, size);
*(me->mem + size + 1) = '\0';
me->pos += (size + 1);
RETURN(true);
} /* ByteArray_writeString */
PUBLIC chars_t ByteArray_getHexString( /*M*/
byte_array_t object_, /*-*/
const size_t bytes_, /*-*/
const long offset_ ) { /*-*/
GO (UID(FBF122));
_byte_array_t* const me = _object(&object_, UID(E92464));
size_t pos = me->pos;
if (bytes_ == 0) {
RETURN(NULL);
}
/* warn if a negative offset is out of range */
if (offset_ < 0 && CAST(size_t, -offset_) > pos) {
DEBUG_WARN(_T("Invalid offset."), UID(ECD1E4));
RETURN(NULL);
}
/* adjust reading position by offset */
if (offset_ < 0) {
pos -= CAST(size_t, -offset_);
} else {
pos += CAST(size_t, offset_);
}
/* warn if the length to read exceeds the end of buffer */
if (pos + bytes_ > me->size) {
DEBUG_WARN(_T("Read past end of buffer."), UID(EFF4A3));
RETURN(NULL);
}
/* release buffer, if previously allocated */
freeT(&me->temp_chars);
{
/* allocate a new buffer */
char_t* new_buf =
CAST(char_t*, MALLOC((bytes_ * 3 + 1) * sizeof(char_t)));
/* ^ for \0 */
char_t* buf = new_buf;
{
/* copy bytes as hex characters in buffer */
size_t i = 0;
for (i = 0; i < bytes_; i++) {
const uint8_t val = *(me->mem + pos + i);
SNPRINTF_T(buf, 4, _T("%02X "), val);
buf += 3;
}
}
*(buf - 1) = '\0';
me->temp_chars = new_buf;
RETURN(new_buf);
}
} /* ByteArray_getHexString */
END_NAMESPACE /*c_*/
#endif /*eof*/