summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxav/msi.c22
-rw-r--r--toxav/rtp.c87
-rw-r--r--toxcore/misc_tools.h36
-rw-r--r--toxcore/util.c58
-rw-r--r--toxcore/util.h15
5 files changed, 85 insertions, 133 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 @@
27#endif /* HAVE_CONFIG_H */ 27#endif /* HAVE_CONFIG_H */
28 28
29#include "../toxcore/logger.h" 29#include "../toxcore/logger.h"
30#include "../toxcore/util.h"
30 31
31#include "msi.h" 32#include "msi.h"
32#include "event.h" 33#include "event.h"
@@ -37,8 +38,6 @@
37#include <string.h> 38#include <string.h>
38#include <stdlib.h> 39#include <stdlib.h>
39 40
40#define inline__ inline __attribute__((always_inline))
41
42#define same(x, y) strcmp((const char*) x, (const char*) y) == 0 41#define same(x, y) strcmp((const char*) x, (const char*) y) == 0
43 42
44#define MSI_MAXMSG_SIZE 1024 43#define MSI_MAXMSG_SIZE 1024
@@ -226,13 +225,12 @@ int parse_raw_data ( MSIMessage *msg, const uint8_t *data, uint16_t length )
226( memcmp(iterator, descriptor, size_const) == 0){ /* Okay */ \ 225( memcmp(iterator, descriptor, size_const) == 0){ /* Okay */ \
227iterator += size_const; /* Set iterator at begining of value part */ \ 226iterator += size_const; /* Set iterator at begining of value part */ \
228if ( *iterator != value_byte ) { assert(0); return -1; }\ 227if ( *iterator != value_byte ) { assert(0); return -1; }\
229 iterator ++;\ 228iterator ++;\
230 uint16_t _value_size = (uint16_t) *(iterator ) << 8 | \ 229uint16_t _value_size; bytes_to_U16(&_value_size, iterator); \
231 (uint16_t) *(iterator + 1); \ 230header.header_value = calloc(sizeof(uint8_t), _value_size); \
232 header.header_value = calloc(sizeof(uint8_t), _value_size); \ 231header.size = _value_size; \
233 header.size = _value_size; \ 232memcpy(header.header_value, iterator + 2, _value_size);\
234 memcpy(header.header_value, iterator + 2, _value_size);\ 233iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte */ }
235 iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte */ }
236 234
237 if ( msg == NULL ) { 235 if ( msg == NULL ) {
238 LOGGER_ERROR("Could not parse message: no storage!"); 236 LOGGER_ERROR("Could not parse message: no storage!");
@@ -249,9 +247,9 @@ if ( *iterator != value_byte ) { assert(0); return -1; }\
249 247
250 if ( *_it == field_byte && itedlen < length ) { 248 if ( *_it == field_byte && itedlen < length ) {
251 249
252 uint16_t _size = ( uint16_t ) * ( _it + 1 ) << 8 | 250 uint16_t _size;
253 ( uint16_t ) * ( _it + 2 ); 251 bytes_to_U16(&_size, _it + 1);
254 252
255 if ( itedlen + _size > length ) return -1; 253 if ( itedlen + _size > length ) return -1;
256 254
257 _it += 3; /* place it at the field value beginning */ 255 _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 @@
26#endif /* HAVE_CONFIG_H */ 26#endif /* HAVE_CONFIG_H */
27 27
28#include "../toxcore/logger.h" 28#include "../toxcore/logger.h"
29#include "../toxcore/util.h"
29 30
30#include "rtp.h" 31#include "rtp.h"
31#include <assert.h> 32#include <assert.h>
@@ -37,9 +38,6 @@
37 38
38#define size_32 4 39#define size_32 4
39 40
40#define inline__ inline __attribute__((always_inline))
41
42
43#define ADD_FLAG_VERSION(_h, _v) do { ( _h->flags ) &= 0x3F; ( _h->flags ) |= ( ( ( _v ) << 6 ) & 0xC0 ); } while(0) 41#define ADD_FLAG_VERSION(_h, _v) do { ( _h->flags ) &= 0x3F; ( _h->flags ) |= ( ( ( _v ) << 6 ) & 0xC0 ); } while(0)
44#define ADD_FLAG_PADDING(_h, _v) do { if ( _v > 0 ) _v = 1; ( _h->flags ) &= 0xDF; ( _h->flags ) |= ( ( ( _v ) << 5 ) & 0x20 ); } while(0) 42#define ADD_FLAG_PADDING(_h, _v) do { if ( _v > 0 ) _v = 1; ( _h->flags ) &= 0xDF; ( _h->flags ) |= ( ( ( _v ) << 5 ) & 0x20 ); } while(0)
45#define ADD_FLAG_EXTENSION(_h, _v) do { if ( _v > 0 ) _v = 1; ( _h->flags ) &= 0xEF;( _h->flags ) |= ( ( ( _v ) << 4 ) & 0x10 ); } while(0) 43#define ADD_FLAG_EXTENSION(_h, _v) do { if ( _v > 0 ) _v = 1; ( _h->flags ) &= 0xEF;( _h->flags ) |= ( ( ( _v ) << 4 ) & 0x10 ); } while(0)
@@ -56,89 +54,6 @@
56 54
57 55
58/** 56/**
59 * @brief Converts 4 bytes to uint32_t
60 *
61 * @param dest Where to convert
62 * @param bytes What bytes
63 * @return void
64 */
65inline__ void bytes_to_U32(uint32_t *dest, const uint8_t *bytes)
66{
67 *dest =
68#ifdef WORDS_BIGENDIAN
69 ( ( uint32_t ) * bytes ) |
70 ( ( uint32_t ) * ( bytes + 1 ) << 8 ) |
71 ( ( uint32_t ) * ( bytes + 2 ) << 16 ) |
72 ( ( uint32_t ) * ( bytes + 3 ) << 24 ) ;
73#else
74 ( ( uint32_t ) * bytes << 24 ) |
75 ( ( uint32_t ) * ( bytes + 1 ) << 16 ) |
76 ( ( uint32_t ) * ( bytes + 2 ) << 8 ) |
77 ( ( uint32_t ) * ( bytes + 3 ) ) ;
78#endif
79}
80
81/**
82 * @brief Converts 2 bytes to uint16_t
83 *
84 * @param dest Where to convert
85 * @param bytes What bytes
86 * @return void
87 */
88inline__ void bytes_to_U16(uint16_t *dest, const uint8_t *bytes)
89{
90 *dest =
91#ifdef WORDS_BIGENDIAN
92 ( ( uint16_t ) * bytes ) |
93 ( ( uint16_t ) * ( bytes + 1 ) << 8 );
94#else
95 ( ( uint16_t ) * bytes << 8 ) |
96 ( ( uint16_t ) * ( bytes + 1 ) );
97#endif
98}
99
100/**
101 * @brief Convert uint32_t to byte string of size 4
102 *
103 * @param dest Where to convert
104 * @param value The value
105 * @return void
106 */
107inline__ void U32_to_bytes(uint8_t *dest, uint32_t value)
108{
109#ifdef WORDS_BIGENDIAN
110 *(dest) = ( value );
111 *(dest + 1) = ( value >> 8 );
112 *(dest + 2) = ( value >> 16 );
113 *(dest + 3) = ( value >> 24 );
114#else
115 *(dest) = ( value >> 24 );
116 *(dest + 1) = ( value >> 16 );
117 *(dest + 2) = ( value >> 8 );
118 *(dest + 3) = ( value );
119#endif
120}
121
122/**
123 * @brief Convert uint16_t to byte string of size 2
124 *
125 * @param dest Where to convert
126 * @param value The value
127 * @return void
128 */
129inline__ void U16_to_bytes(uint8_t *dest, uint16_t value)
130{
131#ifdef WORDS_BIGENDIAN
132 *(dest) = ( value );
133 *(dest + 1) = ( value >> 8 );
134#else
135 *(dest) = ( value >> 8 );
136 *(dest + 1) = ( value );
137#endif
138}
139
140
141/**
142 * @brief Checks if message came in late. 57 * @brief Checks if message came in late.
143 * 58 *
144 * @param session Control session. 59 * @param session Control session.
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 @@
28#include <stdint.h> 28#include <stdint.h>
29#include <string.h> /* for memcpy() */ 29#include <string.h> /* for memcpy() */
30 30
31/*********************Debugging Macros******************** 31
32 * wiki.tox.im/index.php/Internal_functions_and_data_structures#Debugging
33 *********************************************************/
34#ifdef DEBUG
35#include <assert.h>
36#include <stdio.h>
37#include <string.h>
38
39#define DEBUG_PRINT(str, ...) do { \
40 char msg[1000]; \
41 sprintf(msg, "%s(): line %d (file %s): %s%%c\n", __FUNCTION__, __LINE__, __FILE__, str); \
42 fprintf(stderr, msg, __VA_ARGS__); \
43 } while (0)
44
45#define WARNING(...) do { \
46 fprintf(stderr, "warning in "); \
47 DEBUG_PRINT(__VA_ARGS__, ' '); \
48 } while (0)
49
50#define INFO(...) do { \
51 DEBUG_PRINT(__VA_ARGS__, ' '); \
52 } while (0)
53
54#undef ERROR
55#define ERROR(exit_status, ...) do { \
56 fprintf(stderr, "error in "); \
57 DEBUG_PRINT(__VA_ARGS__, ' '); \
58 exit(exit_status); \
59 } while (0)
60#else
61#define WARNING(...)
62#define INFO(...)
63#undef ERROR
64#define ERROR(...)
65#endif // DEBUG
66 32
67/************************Linked List*********************** 33/************************Linked List***********************
68 * http://wiki.tox.im/index.php/Internal_functions_and_data_structures#Linked_List 34 * 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,
133 133
134 return length == 0 ? 0 : -1; 134 return length == 0 ? 0 : -1;
135}; 135};
136
137/* Converts 4 bytes to uint32_t */
138inline__ void bytes_to_U32(uint32_t *dest, const uint8_t *bytes)
139{
140 *dest =
141 #ifdef WORDS_BIGENDIAN
142 ( ( uint32_t ) * bytes ) |
143 ( ( uint32_t ) * ( bytes + 1 ) << 8 ) |
144 ( ( uint32_t ) * ( bytes + 2 ) << 16 ) |
145 ( ( uint32_t ) * ( bytes + 3 ) << 24 ) ;
146 #else
147 ( ( uint32_t ) * bytes << 24 ) |
148 ( ( uint32_t ) * ( bytes + 1 ) << 16 ) |
149 ( ( uint32_t ) * ( bytes + 2 ) << 8 ) |
150 ( ( uint32_t ) * ( bytes + 3 ) ) ;
151 #endif
152}
153
154/* Converts 2 bytes to uint16_t */
155inline__ void bytes_to_U16(uint16_t *dest, const uint8_t *bytes)
156{
157 *dest =
158 #ifdef WORDS_BIGENDIAN
159 ( ( uint16_t ) * bytes ) |
160 ( ( uint16_t ) * ( bytes + 1 ) << 8 );
161 #else
162 ( ( uint16_t ) * bytes << 8 ) |
163 ( ( uint16_t ) * ( bytes + 1 ) );
164 #endif
165}
166
167/* Convert uint32_t to byte string of size 4 */
168inline__ void U32_to_bytes(uint8_t *dest, uint32_t value)
169{
170 #ifdef WORDS_BIGENDIAN
171 *(dest) = ( value );
172 *(dest + 1) = ( value >> 8 );
173 *(dest + 2) = ( value >> 16 );
174 *(dest + 3) = ( value >> 24 );
175 #else
176 *(dest) = ( value >> 24 );
177 *(dest + 1) = ( value >> 16 );
178 *(dest + 2) = ( value >> 8 );
179 *(dest + 3) = ( value );
180 #endif
181}
182
183/* Convert uint16_t to byte string of size 2 */
184inline__ void U16_to_bytes(uint8_t *dest, uint16_t value)
185{
186 #ifdef WORDS_BIGENDIAN
187 *(dest) = ( value );
188 *(dest + 1) = ( value >> 8 );
189 #else
190 *(dest) = ( value >> 8 );
191 *(dest + 1) = ( value );
192 #endif
193} \ 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 @@
28#include <stdbool.h> 28#include <stdbool.h>
29#include <stdint.h> 29#include <stdint.h>
30 30
31#define inline__ inline __attribute__((always_inline))
32
31void unix_time_update(); 33void unix_time_update();
32uint64_t unix_time(); 34uint64_t unix_time();
33int is_timeout(uint64_t timestamp, uint64_t timeout); 35int 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
45int load_state(load_state_callback_func load_state_callback, void *outer, 47int load_state(load_state_callback_func load_state_callback, void *outer,
46 uint8_t *data, uint32_t length, uint16_t cookie_inner); 48 uint8_t *data, uint32_t length, uint16_t cookie_inner);
47 49
50/* Converts 4 bytes to uint32_t */
51void bytes_to_U32(uint32_t *dest, const uint8_t *bytes);
52
53/* Converts 2 bytes to uint16_t */
54void bytes_to_U16(uint16_t *dest, const uint8_t *bytes);
55
56/* Convert uint32_t to byte string of size 4 */
57void U32_to_bytes(uint8_t *dest, uint32_t value);
58
59/* Convert uint16_t to byte string of size 2 */
60void U16_to_bytes(uint8_t *dest, uint16_t value);
61
62
48#endif /* __UTIL_H__ */ 63#endif /* __UTIL_H__ */