From d7c11573751eea005d3b1938ae3298c265d48fd6 Mon Sep 17 00:00:00 2001 From: mannol Date: Sat, 31 May 2014 17:27:22 +0200 Subject: Fixed byte order and removed log functions from misc_tools --- toxcore/misc_tools.h | 36 +------------------------------- toxcore/util.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ toxcore/util.h | 15 ++++++++++++++ 3 files changed, 74 insertions(+), 35 deletions(-) (limited to 'toxcore') diff --git a/toxcore/misc_tools.h b/toxcore/misc_tools.h index 4bf28ac7..170dd2b1 100644 --- a/toxcore/misc_tools.h +++ b/toxcore/misc_tools.h @@ -28,41 +28,7 @@ #include #include /* for memcpy() */ -/*********************Debugging Macros******************** - * wiki.tox.im/index.php/Internal_functions_and_data_structures#Debugging - *********************************************************/ -#ifdef DEBUG -#include -#include -#include - -#define DEBUG_PRINT(str, ...) do { \ - char msg[1000]; \ - sprintf(msg, "%s(): line %d (file %s): %s%%c\n", __FUNCTION__, __LINE__, __FILE__, str); \ - fprintf(stderr, msg, __VA_ARGS__); \ - } while (0) - -#define WARNING(...) do { \ - fprintf(stderr, "warning in "); \ - DEBUG_PRINT(__VA_ARGS__, ' '); \ - } while (0) - -#define INFO(...) do { \ - DEBUG_PRINT(__VA_ARGS__, ' '); \ - } while (0) - -#undef ERROR -#define ERROR(exit_status, ...) do { \ - fprintf(stderr, "error in "); \ - DEBUG_PRINT(__VA_ARGS__, ' '); \ - exit(exit_status); \ - } while (0) -#else -#define WARNING(...) -#define INFO(...) -#undef ERROR -#define ERROR(...) -#endif // DEBUG + /************************Linked List*********************** * http://wiki.tox.im/index.php/Internal_functions_and_data_structures#Linked_List diff --git a/toxcore/util.c b/toxcore/util.c index 7a2db450..f0bb4130 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -133,3 +133,61 @@ int load_state(load_state_callback_func load_state_callback, void *outer, return length == 0 ? 0 : -1; }; + +/* Converts 4 bytes to uint32_t */ +inline__ void bytes_to_U32(uint32_t *dest, const uint8_t *bytes) +{ + *dest = + #ifdef WORDS_BIGENDIAN + ( ( uint32_t ) * bytes ) | + ( ( uint32_t ) * ( bytes + 1 ) << 8 ) | + ( ( uint32_t ) * ( bytes + 2 ) << 16 ) | + ( ( uint32_t ) * ( bytes + 3 ) << 24 ) ; + #else + ( ( uint32_t ) * bytes << 24 ) | + ( ( uint32_t ) * ( bytes + 1 ) << 16 ) | + ( ( uint32_t ) * ( bytes + 2 ) << 8 ) | + ( ( uint32_t ) * ( bytes + 3 ) ) ; + #endif +} + +/* Converts 2 bytes to uint16_t */ +inline__ void bytes_to_U16(uint16_t *dest, const uint8_t *bytes) +{ + *dest = + #ifdef WORDS_BIGENDIAN + ( ( uint16_t ) * bytes ) | + ( ( uint16_t ) * ( bytes + 1 ) << 8 ); + #else + ( ( uint16_t ) * bytes << 8 ) | + ( ( uint16_t ) * ( bytes + 1 ) ); + #endif +} + +/* Convert uint32_t to byte string of size 4 */ +inline__ void U32_to_bytes(uint8_t *dest, uint32_t value) +{ + #ifdef WORDS_BIGENDIAN + *(dest) = ( value ); + *(dest + 1) = ( value >> 8 ); + *(dest + 2) = ( value >> 16 ); + *(dest + 3) = ( value >> 24 ); + #else + *(dest) = ( value >> 24 ); + *(dest + 1) = ( value >> 16 ); + *(dest + 2) = ( value >> 8 ); + *(dest + 3) = ( value ); + #endif +} + +/* Convert uint16_t to byte string of size 2 */ +inline__ void U16_to_bytes(uint8_t *dest, uint16_t value) +{ + #ifdef WORDS_BIGENDIAN + *(dest) = ( value ); + *(dest + 1) = ( value >> 8 ); + #else + *(dest) = ( value >> 8 ); + *(dest + 1) = ( value ); + #endif +} \ No newline at end of file diff --git a/toxcore/util.h b/toxcore/util.h index e40b6968..71c2f5c7 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -28,6 +28,8 @@ #include #include +#define inline__ inline __attribute__((always_inline)) + void unix_time_update(); uint64_t unix_time(); int is_timeout(uint64_t timestamp, uint64_t timeout); @@ -45,4 +47,17 @@ typedef int (*load_state_callback_func)(void *outer, uint8_t *data, uint32_t len int load_state(load_state_callback_func load_state_callback, void *outer, uint8_t *data, uint32_t length, uint16_t cookie_inner); +/* Converts 4 bytes to uint32_t */ +void bytes_to_U32(uint32_t *dest, const uint8_t *bytes); + +/* Converts 2 bytes to uint16_t */ +void bytes_to_U16(uint16_t *dest, const uint8_t *bytes); + +/* Convert uint32_t to byte string of size 4 */ +void U32_to_bytes(uint8_t *dest, uint32_t value); + +/* Convert uint16_t to byte string of size 2 */ +void U16_to_bytes(uint8_t *dest, uint16_t value); + + #endif /* __UTIL_H__ */ -- cgit v1.2.3