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
|
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/
/**@file tcomp_buffer.c
* @brief SigComp Buffer.
*
* @author Mamadou Diop <diopmamadou(at)yahoo.fr>
*
*/
#include "tcomp_buffer.h"
#include "tsk_binaryutils.h"
#include "tsk_memory.h"
#include "tsk_debug.h"
#include <string.h>
/** SigComp buffer.
*/
typedef struct tcomp_buffer_s
{
TSK_DECLARE_OBJECT;
tsk_size_t size; /**< The size of the buffer */
uint8_t* lpbuffer; /**< Pointer to the buffer */
tsk_size_t index_bytes; /**< Bytes (8bit size) cursor */
tsk_size_t index_bits; /**< Bits (1-bit size) cursor */
unsigned owner:1; /**< Indicates whether we are the owner of the buffer or not (external buffer) */
uint8_t P_BIT; /**< P-BIT controller. */
}
tcomp_buffer_t;
tcomp_buffer_handle_t* tcomp_buffer_create(const void* data, tsk_size_t len)
{
tcomp_buffer_t* buffer;
if((buffer = tsk_object_new(tcomp_buffer_def_t))){
buffer->owner = tsk_true;
// The P-bit controls the order in which bits are passed from the dispatcher to the INPUT instructions.
buffer->P_BIT = TCOMP_P_BIT_MSB_TO_LSB;
if(data && len){
tcomp_buffer_appendBuff(buffer, data, len);
}
}
return buffer;
}
tcomp_buffer_handle_t* tcomp_buffer_create_null()
{
return tcomp_buffer_create(tsk_null, 0);
}
/**Compares two sigomp buffers.
* @param handle1 First handle to compare.
* @param handle2 Second handle to compare.
* @retval @a tsk_true if the two handles are equals and @a tsk_false otherwise.
*/
tsk_bool_t tcomp_buffer_equals(const tcomp_buffer_handle_t* handle1, const tcomp_buffer_handle_t* handle2)
{
if( tcomp_buffer_getSize(handle1) == tcomp_buffer_getSize(handle2) ){
return tcomp_buffer_startsWith(handle1, handle2);
}
return tsk_false;
}
/**Checks if the first internal buffer starts with the second handle internal buffer.
* @param handle1 First handle
* @param handle2 Second handle
* @retval Returns @a tsk_true if the first internal buffer starts with the second handle internal buffer and @a tsk_false otherwise.
*/
tsk_bool_t tcomp_buffer_startsWith(const tcomp_buffer_handle_t* handle1, const tcomp_buffer_handle_t* handle2) /*const*/
{
tsk_size_t i;
tcomp_buffer_t* buffer1 = (tcomp_buffer_t*)handle1;
tcomp_buffer_t* buffer2 = (tcomp_buffer_t*)handle2;
if(buffer1->size < buffer2->size){
return tsk_false;
}
for(i = 0; i< buffer2->size; i++){
if(buffer1->lpbuffer[i] != buffer2->lpbuffer[i]){
return tsk_false;
}
}
return tsk_true;
}
/**Gets a readonly pointer to the internal buffer.
* @param handle The handle for which to get the internal buffer.
* @param position Position pointer
* @retval Pointer to the internal buffer.
*/
const uint8_t* tcomp_buffer_getReadOnlyBufferAtPos(const tcomp_buffer_handle_t* handle, tsk_size_t position)/*const*/
{
if(handle){
return (((tcomp_buffer_t*)handle)->lpbuffer + position);
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
return tsk_null;
}
/**Gets a read/write pointer to the internal buffer.
* @param handle The handle for which to get the internal buffer.
* @param position Position pointer
* @retval Pointer to the internal buffer.
*/
uint8_t* tcomp_buffer_getBufferAtPos(const tcomp_buffer_handle_t* handle, tsk_size_t position)
{
if(handle){
if(position && ((tcomp_buffer_t*)handle)->size <= position){
TSK_DEBUG_ERROR("%u <= %u", ((tcomp_buffer_t*)handle)->size, position);
return tsk_null;
}
return (((tcomp_buffer_t*)handle)->lpbuffer + position);
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
return tsk_null;
}
/** Gets the internal buffer size
* @retval The size of the internal buffer
*/
tsk_size_t tcomp_buffer_getSize(const tcomp_buffer_handle_t* handle) /*const*/
{
if(handle){
return ((tcomp_buffer_t*)handle)->size;
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
return 0;
}
/**Gets the remainning bits.
* @param handle The handle for which to get the remaining bits.
*/
tsk_size_t tcomp_buffer_getRemainingBits(const tcomp_buffer_handle_t* handle) /*const*/
{
if(handle){
tcomp_buffer_t* buffer = (tcomp_buffer_t*)handle;
tsk_ssize_t result = ((buffer->size * 8) - ((buffer->index_bytes * 8) + buffer->index_bits));
return (result < 0) ? 0: result;
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
return 0;
}
/**Reads @a size bytes.
* @param handle The handle for which to read bytes.
* @param length Number of bytes to read.
* @retval Pointer to the resulting buffer.
*/
uint8_t* tcomp_buffer_readBytes(tcomp_buffer_handle_t* handle, tsk_size_t length)
{
if(handle){
tcomp_buffer_t* buffer = (tcomp_buffer_t*)handle;
tsk_size_t old_index;
if((buffer->index_bytes + length) > (buffer->size)) {
return tsk_null;
}
old_index = buffer->index_bytes;
buffer->index_bytes += length;
return tcomp_buffer_getBufferAtPos(handle, old_index);
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
return tsk_null;
}
/**Reads the internal buffer from LSB to MSB as per RFC 3320 subclause 8.2.
* @param handle The SigComp handle holding the internal buffer to read.
* @param length The length of the buffer to read.
* @retval All bits as a 2-bytes integer value
*/
uint32_t tcomp_buffer_readLsbToMsb(tcomp_buffer_handle_t* handle, tsk_size_t length)
{
// UDV Memory is always MSB first
// MSB <-- LSB
// FIXME: use mask
if(handle)
{
tcomp_buffer_t* buffer = (tcomp_buffer_t*)handle;
uint8_t pos = 0;
char* end;
uint32_t result_val = 0;
char result_str[16]; memset(result_str, 0, 16);
while(pos < length){
result_str[pos++] = (buffer->lpbuffer[buffer->index_bytes]
&(1 << (buffer->index_bits))) ? '1' : '0';
if(++buffer->index_bits == 8){
buffer->index_bytes++;
buffer->index_bits = 0;
}
}
end = (result_str+length);
result_val = (uint32_t)strtol(result_str, &end, 2);
return result_val;
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
return 0;
}
/**Reads the internal buffer from MSB to LSB as per RFC 3320 subclause 8.2.
* @param handle The SigComp handle holding the internal buffer to read.
* @param length The length of the buffer to read.
* @retval All bits as a 2-bytes integer value
*/
uint32_t tcomp_buffer_readMsbToLsb(tcomp_buffer_handle_t* handle, tsk_size_t length)
{
// UDV Memory is always MSB first
// MSB --> LSB
// FIXME: use mask
if(handle){
tcomp_buffer_t* buffer = (tcomp_buffer_t*)handle;
uint8_t pos = 0;
char* end;
uint32_t result_val = 0;
char result_str[16]; memset(result_str, 0, 16);
while(pos < length){
result_str[pos++] = (buffer->lpbuffer[buffer->index_bytes]
&(128 >> (buffer->index_bits))) ? '1' : '0';
if(++buffer->index_bits == 8){
buffer->index_bytes++;
buffer->index_bits = 0;
}
}
end = (result_str + length);
result_val = (uint32_t)strtol(result_str, &end, 2);
return result_val;
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
return 0;
}
/**Discards bits as per RFC 3320 subclause 8.2.
* @param handle SigComp handle holding the internal buffer.
*/
void tcomp_buffer_discardBits(tcomp_buffer_handle_t* handle)
{
if(handle){
tcomp_buffer_t* buffer = (tcomp_buffer_t*)handle;
if(buffer->index_bits){
buffer->index_bits=0;
buffer->index_bytes++;
}
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
}
/**Discards last bytes as per RFC 3320 subclause 8.2.
* @param handle SigComp handle holding the internal buffer.
* @param count The number of bytes to discard.
*/
void tcomp_buffer_discardLastBytes(tcomp_buffer_handle_t* handle, uint32_t count)
{
if(handle){
tcomp_buffer_t* buffer = (tcomp_buffer_t*)handle;
if(buffer->size > count){
buffer->size -= count;
}
else{
tcomp_buffer_freeBuff(handle);
}
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
}
/**Allocs the internal buffer.
* @param handle SigComp handle holding the internal buffer to alloc.
* @param size Number of bytes to allocate.
*/
void tcomp_buffer_allocBuff(tcomp_buffer_handle_t* handle, tsk_size_t size)
{
if(handle){
tcomp_buffer_t* buffer = (tcomp_buffer_t*)handle;
if(!buffer->owner){
TSK_DEBUG_ERROR("The SigComp is not the owner of the internal buffer to alloc.");
return;
}
if(!size){
TSK_DEBUG_WARN("Cannot allocate zero bytes.");
return;
}
buffer->index_bits = buffer->index_bytes = 0;
buffer->size = 0;
if((buffer->lpbuffer = tsk_realloc(buffer->lpbuffer, size))){
buffer->size = size;
}
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
}
/**Adds a buffer as a reference (not owned).
* @param handle SigComp handle holding the internal buffer.
* @param externalBuff THe external buffer to reference.
* @param size The size of the external buffer.
*/
void tcomp_buffer_referenceBuff(tcomp_buffer_handle_t* handle, uint8_t* externalBuff, tsk_size_t size)
{
if(handle){
tcomp_buffer_t* buffer = (tcomp_buffer_t*)handle;
if(buffer->size && buffer->owner){
TSK_DEBUG_ERROR("The SigComp handle already hold an internal buffer.");
return;
}
buffer->size = size;
buffer->lpbuffer = externalBuff;
buffer->index_bytes = 0;
buffer->index_bits = 0;
buffer->owner = 0;
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
}
/**Appends data to our internal buffer.
* @param handle The handle to the SigComp buffer.
* @param data Data to append to our internal buffer.
* @param size The size of the data
* @retval @a tsk_true if succeed an @a tsk_false otherwise.
*/
tsk_bool_t tcomp_buffer_appendBuff(tcomp_buffer_handle_t* handle, const void* data, tsk_size_t size)
{
if(handle){
tcomp_buffer_t* buffer = (tcomp_buffer_t*)handle;
tsk_size_t oldSize = buffer->size;
tsk_size_t newSize = (oldSize + size);
{
// realloc buffer
if(!buffer->size){
buffer->lpbuffer = (uint8_t*)tsk_calloc(1, newSize);
}
else{
buffer->lpbuffer = (uint8_t*)tsk_realloc(buffer->lpbuffer, newSize);
}
}
if(!buffer->lpbuffer){
return tsk_false;
}
if(data){
memcpy((buffer->lpbuffer+oldSize), data, size);
}
else{
memset((buffer->lpbuffer+oldSize), 0, size);
}
buffer->size = newSize;
return tsk_true;
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
return tsk_false;
}
/**Removes @a size bytes from the internal buffer.
* @param handle SigComp handle holding the internal buffer from which to remove bytes.
* @param pos The starting position from which to start removing bytes
* @param size The number of bytes to remove
* @retval @a tsk_true if succeed an @a tsk_false otherwise.
*/
tsk_bool_t tcomp_buffer_removeBuff(tcomp_buffer_handle_t* handle, tsk_size_t pos, tsk_size_t size)
{
if(handle){
tcomp_buffer_t* buffer = (tcomp_buffer_t*)handle;
tsk_size_t oldSize, newSize;
if(((pos + size) > buffer->size)) size = (buffer->size - pos);
memcpy((buffer->lpbuffer + pos), (buffer->lpbuffer + pos + size), (buffer->size - (pos + size)));
oldSize = buffer->size;
newSize = (oldSize - size);
{
if(!(buffer->size)){
buffer->lpbuffer = (uint8_t*)tsk_calloc(1, newSize);
}
else{
buffer->lpbuffer = (uint8_t*)tsk_realloc(buffer->lpbuffer, newSize);
}
}
if(buffer->lpbuffer){
buffer->size = newSize;
return tsk_true;
}
return tsk_false;
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
return tsk_false;
}
/**Free the internal buffer.
* @param handle SigComp handle holding the internal buffer to free.
*/
void tcomp_buffer_freeBuff(tcomp_buffer_handle_t* handle)
{
if(handle){
tcomp_buffer_t* buffer = (tcomp_buffer_t*)handle;
if(buffer->lpbuffer && buffer->size && buffer->owner) {
tsk_free((void**)&(buffer->lpbuffer));
}
buffer->size = buffer->index_bytes = buffer->index_bits = 0;
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
}
/**Gets the bytes cursor position.
* @param handle SigComp handle holding the internal buffer.
* @retval The cursor position.
*/
tsk_size_t* tcomp_buffer_getIndexBytes(const tcomp_buffer_handle_t* handle)
{
if(handle){
return &(((tcomp_buffer_t*)handle)->index_bytes);
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
return 0;
}
/**Gets the bits cursor position.
* @param handle SigComp handle holding the internal buffer.
* @retval The cursor position.
*/
tsk_size_t* tcomp_buffer_getIndexBits(const tcomp_buffer_handle_t* handle)
{
if(handle){
return &(((tcomp_buffer_t*)handle)->index_bits);
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
return tsk_null;
}
/**Gets the P-bit controller value.
* The P-bit controls the order in which bits are passed from the dispatcher to the INPUT instructions. If set to 0, it indicates that
* the bits within an individual byte are passed to the INPUT instructions in MSB to LSB order. If it is set to 1, the bits are
* passed in LSB to MSB order.
* @param handle SigComp handle holding the internal buffer.
* @retval The P-Bit value.
*/
uint8_t* tcomp_buffer_getP_BIT(const tcomp_buffer_handle_t* handle)
{
if(handle){
return &(((tcomp_buffer_t*)handle)->P_BIT);
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
return tsk_null;
}
/**Creates a random HASH number.
*/
uint64_t tcomp_buffer_createHash(const void *data, tsk_size_t len)
{
if(!data || !len){
TSK_DEBUG_ERROR("Null data.");
return 0;
}
{
#define PRIME_1 500237
#define PRIME_2 700241
uint64_t hash = 0;
uint8_t* strid = (uint8_t*)data;
/* Generate Hash code from id */
{
uint64_t b = PRIME_1, a = PRIME_2;
tsk_size_t i;
for(i = 0; i < len; strid++, i++)
{
hash = hash * a + (*strid);
a = a * b;
}
}
return hash;
#undef PRIME_1
#undef PRIME_2
}
}
/**Prints the internal buffer.
* @param handle SigComp handle holding the internal buffer to print.
* @param size The number of bytes to print.
*/
void tcomp_buffer_nprint(const tcomp_buffer_handle_t* handle, tsk_ssize_t size)
{
#if defined(_DEBUG) || defined(DEBUG)
if(handle){
tsk_size_t i, tsk_size_to_print;
tcomp_buffer_t* buffer = (tcomp_buffer_t*)handle;
tsk_size_to_print = (size<0) ? buffer->size : size;
if( !tsk_size_to_print || !buffer->lpbuffer) return;
for(i = 0; i < tsk_size_to_print; i+=2){
char s[10];
uint32_t value;
memset(s, 0, 10);
if((i+1) == tsk_size_to_print){
// last 2-byte lay in 1byte
value = buffer->lpbuffer[i];
#if 0
sprintf_s(s, 10, i?"%0.2x":"0x%0.2x", value);
#else
sprintf(s, i ? "%0.2x" : "0x%0.2x", value);
#endif
}
else{
uint8_t *b_ptr = tcomp_buffer_getBufferAtPos(handle, i);
value = TSK_BINARY_GET_2BYTES(b_ptr);
#if 0
sprintf_s(s, 10, i?"%0.4x":"0x%0.4x", value);
#else
sprintf(s, i?"%0.4x":"0x%0.4x", value);
#endif
}
printf("%s ", s);
}
printf("\n");
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
#endif
}
/**Resets a sigcomp buffer.
* @param handle Handle holding the internal buffer to reset.
*/
void tcomp_buffer_reset(tcomp_buffer_handle_t* handle)
{
if(handle){
tcomp_buffer_t* buffer = (tcomp_buffer_t*)handle;
buffer->index_bytes = 0;
buffer->index_bits = 0;
if(buffer->lpbuffer){
memset(buffer->lpbuffer, 0, buffer->size);
}
}
else{
TSK_DEBUG_ERROR("Null SigComp handle");
}
}
//========================================================
// SigComp buffer object definition
//
static tsk_object_t* tcomp_buffer_ctor(tsk_object_t *self, va_list * app)
{
tcomp_buffer_t* buffer = self;
if(buffer){
}
return self;
}
static tsk_object_t* tcomp_buffer_dtor(tsk_object_t *self)
{
tcomp_buffer_t* buffer = self;
if(buffer){
tcomp_buffer_freeBuff(buffer);
}
else{
TSK_DEBUG_ERROR("Null SigComp handle.");
}
return self;
}
static const tsk_object_def_t tcomp_buffer_def_s =
{
sizeof(tcomp_buffer_t),
tcomp_buffer_ctor,
tcomp_buffer_dtor,
tsk_null
};
const tsk_object_def_t *tcomp_buffer_def_t = &tcomp_buffer_def_s;
|