diff options
Diffstat (limited to 'tinySIGCOMP/src/tcomp_buffer.c')
-rwxr-xr-x | tinySIGCOMP/src/tcomp_buffer.c | 776 |
1 files changed, 389 insertions, 387 deletions
diff --git a/tinySIGCOMP/src/tcomp_buffer.c b/tinySIGCOMP/src/tcomp_buffer.c index b7bfd4d..06883ff 100755 --- a/tinySIGCOMP/src/tcomp_buffer.c +++ b/tinySIGCOMP/src/tcomp_buffer.c @@ -2,19 +2,19 @@ * 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. * @@ -32,41 +32,40 @@ #include "tsk_memory.h" #include "tsk_debug.h" -#include <string.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. */ +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_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); + return tcomp_buffer_create(tsk_null, 0); } /**Compares two sigomp buffers. @@ -76,11 +75,11 @@ tcomp_buffer_handle_t* tcomp_buffer_create_null() */ 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); - } + if( tcomp_buffer_getSize(handle1) == tcomp_buffer_getSize(handle2) ) { + return tcomp_buffer_startsWith(handle1, handle2); + } - return tsk_false; + return tsk_false; } @@ -91,20 +90,20 @@ tsk_bool_t tcomp_buffer_equals(const tcomp_buffer_handle_t* handle1, const tcomp */ 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; + 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. @@ -114,14 +113,14 @@ tsk_bool_t tcomp_buffer_startsWith(const tcomp_buffer_handle_t* handle1, const t */ 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; + 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. @@ -131,18 +130,18 @@ const uint8_t* tcomp_buffer_getReadOnlyBufferAtPos(const tcomp_buffer_handle_t* */ 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; + 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; } @@ -151,14 +150,14 @@ uint8_t* tcomp_buffer_getBufferAtPos(const tcomp_buffer_handle_t* handle, tsk_si */ 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; + if(handle) { + return ((tcomp_buffer_t*)handle)->size; + } + else { + TSK_DEBUG_ERROR("Null SigComp handle"); + } + + return 0; } /**Gets the remainning bits. @@ -166,16 +165,16 @@ tsk_size_t tcomp_buffer_getSize(const tcomp_buffer_handle_t* handle) /*const*/ */ 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; + 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. @@ -185,24 +184,24 @@ tsk_size_t tcomp_buffer_getRemainingBits(const tcomp_buffer_handle_t* handle) /* */ 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(handle) { + tcomp_buffer_t* buffer = (tcomp_buffer_t*)handle; + tsk_size_t old_index; - if((buffer->index_bytes + length) > (buffer->size)) { - return tsk_null; - } + if((buffer->index_bytes + length) > (buffer->size)) { + return tsk_null; + } - old_index = buffer->index_bytes; - buffer->index_bytes += length; + old_index = buffer->index_bytes; + buffer->index_bytes += length; - return tcomp_buffer_getBufferAtPos(handle, old_index); - } - else{ - TSK_DEBUG_ERROR("Null SigComp handle"); - } + return tcomp_buffer_getBufferAtPos(handle, old_index); + } + else { + TSK_DEBUG_ERROR("Null SigComp handle"); + } - return tsk_null; + return tsk_null; } /**Reads the internal buffer from LSB to MSB as per RFC 3320 subclause 8.2. @@ -212,35 +211,35 @@ uint8_t* tcomp_buffer_readBytes(tcomp_buffer_handle_t* handle, tsk_size_t length */ 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; + // 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. @@ -250,35 +249,36 @@ uint32_t tcomp_buffer_readLsbToMsb(tcomp_buffer_handle_t* handle, tsk_size_t len */ 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; + // 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. @@ -286,16 +286,16 @@ uint32_t tcomp_buffer_readMsbToLsb(tcomp_buffer_handle_t* handle, tsk_size_t len */ 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"); - } + 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. @@ -304,18 +304,18 @@ void tcomp_buffer_discardBits(tcomp_buffer_handle_t* handle) */ 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"); - } + 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. @@ -324,25 +324,25 @@ void tcomp_buffer_discardLastBytes(tcomp_buffer_handle_t* handle, uint32_t count */ 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"); - } + 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). @@ -352,22 +352,22 @@ void tcomp_buffer_allocBuff(tcomp_buffer_handle_t* handle, tsk_size_t size) */ 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"); - } + 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. @@ -378,39 +378,39 @@ void tcomp_buffer_referenceBuff(tcomp_buffer_handle_t* handle, uint8_t* external */ 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; + 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. @@ -421,34 +421,36 @@ tsk_bool_t tcomp_buffer_appendBuff(tcomp_buffer_handle_t* handle, const void* da */ 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; + 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. @@ -456,16 +458,16 @@ tsk_bool_t tcomp_buffer_removeBuff(tcomp_buffer_handle_t* handle, tsk_size_t pos */ 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"); - } + 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. @@ -474,14 +476,14 @@ void tcomp_buffer_freeBuff(tcomp_buffer_handle_t* handle) */ 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; + if(handle) { + return &(((tcomp_buffer_t*)handle)->index_bytes); + } + else { + TSK_DEBUG_ERROR("Null SigComp handle"); + } + + return 0; } /**Gets the bits cursor position. @@ -490,14 +492,14 @@ tsk_size_t* tcomp_buffer_getIndexBytes(const tcomp_buffer_handle_t* handle) */ 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; + 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. @@ -509,45 +511,44 @@ tsk_size_t* tcomp_buffer_getIndexBits(const tcomp_buffer_handle_t* handle) */ 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; + 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; - } - { + 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; + 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. @@ -558,43 +559,45 @@ 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(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); + sprintf_s(s, 10, i?"%0.2x":"0x%0.2x", value); #else - sprintf(s, i ? "%0.2x" : "0x%0.2x", value); + 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); + } + 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); + sprintf_s(s, 10, i?"%0.4x":"0x%0.4x", value); #else - sprintf(s, i?"%0.4x":"0x%0.4x", value); + sprintf(s, i?"%0.4x":"0x%0.4x", value); #endif - } - printf("%s ", s); - } - printf("\n"); - } - else{ - TSK_DEBUG_ERROR("Null SigComp handle"); - } + } + printf("%s ", s); + } + printf("\n"); + } + else { + TSK_DEBUG_ERROR("Null SigComp handle"); + } #endif } @@ -603,18 +606,18 @@ void tcomp_buffer_nprint(const tcomp_buffer_handle_t* handle, tsk_ssize_t size) */ 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"); - } + 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"); + } } @@ -631,31 +634,30 @@ void tcomp_buffer_reset(tcomp_buffer_handle_t* handle) // static tsk_object_t* tcomp_buffer_ctor(tsk_object_t *self, va_list * app) { - tcomp_buffer_t* buffer = self; - if(buffer){ - } - return self; + 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; + 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 +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; |