From d7c11573751eea005d3b1938ae3298c265d48fd6 Mon Sep 17 00:00:00 2001 From: mannol Date: Sat, 31 May 2014 17:27:22 +0200 Subject: [PATCH 1/2] Fixed byte order and removed log functions from misc_tools --- toxav/msi.c | 22 +++++------ toxav/rtp.c | 87 +------------------------------------------- toxcore/misc_tools.h | 34 ----------------- toxcore/util.c | 58 +++++++++++++++++++++++++++++ toxcore/util.h | 15 ++++++++ 5 files changed, 84 insertions(+), 132 deletions(-) diff --git a/toxav/msi.c b/toxav/msi.c index 031f1e54..b5d3779f 100644 --- a/toxav/msi.c +++ b/toxav/msi.c @@ -27,6 +27,7 @@ #endif /* HAVE_CONFIG_H */ #include "../toxcore/logger.h" +#include "../toxcore/util.h" #include "msi.h" #include "event.h" @@ -37,8 +38,6 @@ #include #include -#define inline__ inline __attribute__((always_inline)) - #define same(x, y) strcmp((const char*) x, (const char*) y) == 0 #define MSI_MAXMSG_SIZE 1024 @@ -226,13 +225,12 @@ int parse_raw_data ( MSIMessage *msg, const uint8_t *data, uint16_t length ) ( memcmp(iterator, descriptor, size_const) == 0){ /* Okay */ \ iterator += size_const; /* Set iterator at begining of value part */ \ if ( *iterator != value_byte ) { assert(0); return -1; }\ - iterator ++;\ - uint16_t _value_size = (uint16_t) *(iterator ) << 8 | \ - (uint16_t) *(iterator + 1); \ - header.header_value = calloc(sizeof(uint8_t), _value_size); \ - header.size = _value_size; \ - memcpy(header.header_value, iterator + 2, _value_size);\ - iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte */ } +iterator ++;\ +uint16_t _value_size; bytes_to_U16(&_value_size, iterator); \ +header.header_value = calloc(sizeof(uint8_t), _value_size); \ +header.size = _value_size; \ +memcpy(header.header_value, iterator + 2, _value_size);\ +iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte */ } if ( msg == NULL ) { LOGGER_ERROR("Could not parse message: no storage!"); @@ -249,9 +247,9 @@ if ( *iterator != value_byte ) { assert(0); return -1; }\ if ( *_it == field_byte && itedlen < length ) { - uint16_t _size = ( uint16_t ) * ( _it + 1 ) << 8 | - ( uint16_t ) * ( _it + 2 ); - + uint16_t _size; + bytes_to_U16(&_size, _it + 1); + if ( itedlen + _size > length ) return -1; _it += 3; /* place it at the field value beginning */ diff --git a/toxav/rtp.c b/toxav/rtp.c index f44b2bfe..d4fdf656 100644 --- a/toxav/rtp.c +++ b/toxav/rtp.c @@ -26,6 +26,7 @@ #endif /* HAVE_CONFIG_H */ #include "../toxcore/logger.h" +#include "../toxcore/util.h" #include "rtp.h" #include @@ -37,9 +38,6 @@ #define size_32 4 -#define inline__ inline __attribute__((always_inline)) - - #define ADD_FLAG_VERSION(_h, _v) do { ( _h->flags ) &= 0x3F; ( _h->flags ) |= ( ( ( _v ) << 6 ) & 0xC0 ); } while(0) #define ADD_FLAG_PADDING(_h, _v) do { if ( _v > 0 ) _v = 1; ( _h->flags ) &= 0xDF; ( _h->flags ) |= ( ( ( _v ) << 5 ) & 0x20 ); } while(0) #define ADD_FLAG_EXTENSION(_h, _v) do { if ( _v > 0 ) _v = 1; ( _h->flags ) &= 0xEF;( _h->flags ) |= ( ( ( _v ) << 4 ) & 0x10 ); } while(0) @@ -55,89 +53,6 @@ #define GET_SETTING_PAYLOAD(_h) ((_h->marker_payloadt) & 0x7f) -/** - * @brief Converts 4 bytes to uint32_t - * - * @param dest Where to convert - * @param bytes What bytes - * @return void - */ -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 -} - -/** - * @brief Converts 2 bytes to uint16_t - * - * @param dest Where to convert - * @param bytes What bytes - * @return void - */ -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 -} - -/** - * @brief Convert uint32_t to byte string of size 4 - * - * @param dest Where to convert - * @param value The value - * @return void - */ -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 -} - -/** - * @brief Convert uint16_t to byte string of size 2 - * - * @param dest Where to convert - * @param value The value - * @return void - */ -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 -} - - /** * @brief Checks if message came in late. * 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__ */ From fa203aa1ad4d22a02fd55f6d04f4be353f6948f0 Mon Sep 17 00:00:00 2001 From: mannol Date: Sat, 31 May 2014 21:02:50 +0200 Subject: [PATCH 2/2] We gon let it burn burn burn --- toxav/msi.c | 65 ++++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/toxav/msi.c b/toxav/msi.c index b5d3779f..928a196a 100644 --- a/toxav/msi.c +++ b/toxav/msi.c @@ -221,12 +221,13 @@ static inline__ const uint8_t *stringify_response ( MSIResponse response ) int parse_raw_data ( MSIMessage *msg, const uint8_t *data, uint16_t length ) { -#define ON_HEADER(iterator, header, descriptor, size_const) \ -( memcmp(iterator, descriptor, size_const) == 0){ /* Okay */ \ -iterator += size_const; /* Set iterator at begining of value part */ \ -if ( *iterator != value_byte ) { assert(0); return -1; }\ -iterator ++;\ -uint16_t _value_size; bytes_to_U16(&_value_size, iterator); \ +#define ON_HEADER(iterator, size_con, header, descriptor, type_size_const) \ +( memcmp(iterator, descriptor, type_size_const) == 0){ /* Okay */ \ +iterator += type_size_const; /* Set iterator at begining of value part */ \ +if ( *iterator != value_byte || size_con <= type_size_const) { return -1; } size_con -= type_size_const; \ +iterator ++; if(size_con <= 3) {return -1;} size_con -= 3; \ +uint16_t _value_size; memcpy(&_value_size, iterator, sizeof(_value_size)); _value_size = ntohs(_value_size);\ +if(size_con < _value_size) { return -1; } size_con -= _value_size; \ header.header_value = calloc(sizeof(uint8_t), _value_size); \ header.size = _value_size; \ memcpy(header.header_value, iterator + 2, _value_size);\ @@ -234,13 +235,15 @@ iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte if ( msg == NULL ) { LOGGER_ERROR("Could not parse message: no storage!"); + return -1; } if ( data[length - 1] ) /* End byte must have value 0 */ return -1; const uint8_t *_it = data; - + uint16_t size_max = length; + while ( *_it ) {/* until end_byte is hit */ uint16_t itedlen = (_it - data) + 2; @@ -248,48 +251,50 @@ iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte if ( *_it == field_byte && itedlen < length ) { uint16_t _size; - bytes_to_U16(&_size, _it + 1); + memcpy(&_size, _it + 1, sizeof(_size)); + _size = ntohs(_size); if ( itedlen + _size > length ) return -1; _it += 3; /* place it at the field value beginning */ - + size_max -= 3; + switch ( _size ) { /* Compare the size of the hardcoded values ( vary fast and convenient ) */ case 4: { /* INFO header */ - if ON_HEADER ( _it, msg->info, INFO_FIELD, 4 ) + if ON_HEADER ( _it, size_max, msg->info, INFO_FIELD, 4 ) } break; case 5: { /* NONCE header */ - if ON_HEADER ( _it, msg->nonce, NONCE_FIELD, 5 ) + if ON_HEADER ( _it, size_max, msg->nonce, NONCE_FIELD, 5 ) } break; case 6: { /* Reason header */ - if ON_HEADER ( _it, msg->reason, REASON_FIELD, 6 ) + if ON_HEADER ( _it, size_max, msg->reason, REASON_FIELD, 6 ) } break; case 7: { /* Version, Request, Call-id headers */ - if ON_HEADER ( _it, msg->version, VERSION_FIELD, 7 ) - else if ON_HEADER ( _it, msg->request, REQUEST_FIELD, 7 ) - else if ON_HEADER ( _it, msg->callid, CALLID_FIELD, 7 ) - } + if ON_HEADER ( _it, size_max, msg->version, VERSION_FIELD, 7 ) + else if ON_HEADER ( _it, size_max, msg->request, REQUEST_FIELD, 7 ) + else if ON_HEADER ( _it, size_max, msg->callid, CALLID_FIELD, 7 ) + } break; case 8: { /* Response header */ - if ON_HEADER ( _it, msg->response, RESPONSE_FIELD, 8 ) + if ON_HEADER ( _it, size_max, msg->response, RESPONSE_FIELD, 8 ) } break; case 9: { /* Call-type header */ - if ON_HEADER ( _it, msg->calltype, CALLTYPE_FIELD, 9 ) + if ON_HEADER ( _it, size_max, msg->calltype, CALLTYPE_FIELD, 9 ) } break; case 10: { /* Crypto-key headers */ - if ON_HEADER ( _it, msg->cryptokey, CRYPTOKEY_FIELD, 10 ) + if ON_HEADER ( _it, size_max, msg->cryptokey, CRYPTOKEY_FIELD, 10 ) } break; @@ -507,24 +512,22 @@ uint8_t *append_header_to_string ( _total += _i; /* Now set the length of the field byte */ - *_getback_byte = ( uint8_t ) _i >> 8; - - _getback_byte++; - - *_getback_byte = ( uint8_t ) _i; + uint16_t _convert; + + + _convert = htons(_i); + memcpy(_getback_byte, &_convert, sizeof(_convert)); /* for value part do it regulary */ *dest = value_byte; dest++; - *dest = ( uint8_t ) value_len >> 8; - - dest++; - - *dest = ( uint8_t ) value_len; - - dest++; + + _convert = htons(value_len); + memcpy(dest, &_convert, sizeof(_convert)); + + dest+=2; for ( _i = value_len; _i; --_i ) { *dest = *_hvit;