From a46810a1976096ec6af2b809d85e6eeaa51b51ad Mon Sep 17 00:00:00 2001 From: sin Date: Fri, 26 Sep 2014 14:09:26 +0100 Subject: Rework toxav/rtp.c to use ntohl/htonl and ntohs/htons Now we can also remove the helper routines from toxcore/util.[ch]. --- toxav/rtp.c | 50 +++++++++++++++++++++++++++++++++++--------------- toxcore/util.c | 58 ---------------------------------------------------------- toxcore/util.h | 13 ------------- 3 files changed, 35 insertions(+), 86 deletions(-) diff --git a/toxav/rtp.c b/toxav/rtp.c index de6c9c41..a4e1b12e 100644 --- a/toxav/rtp.c +++ b/toxav/rtp.c @@ -88,7 +88,8 @@ RTPHeader *extract_header ( const uint8_t *payload, int length ) return NULL; } - bytes_to_U16(&_retu->sequnum, payload); + memcpy(&_retu->sequnum, payload, sizeof(_retu->sequnum)); + _retu->sequnum = ntohs(_retu->sequnum); const uint8_t *_it = payload + 2; @@ -128,15 +129,18 @@ RTPHeader *extract_header ( const uint8_t *payload, int length ) _retu->length = _length; - bytes_to_U32(&_retu->timestamp, _it); + memcpy(&_retu->timestamp, _it, sizeof(_retu->timestamp)); + _retu->timestamp = ntohl(_retu->timestamp); _it += 4; - bytes_to_U32(&_retu->ssrc, _it); + memcpy(&_retu->ssrc, _it, sizeof(_retu->ssrc)); + _retu->ssrc = ntohl(_retu->ssrc); uint8_t _x; for ( _x = 0; _x < _cc; _x++ ) { _it += 4; - bytes_to_U32(&(_retu->csrc[_x]), _it); + memcpy(&_retu->csrc[_x], _it, sizeof(_retu->csrc[_x])); + _retu->csrc[_x] = ntohl(_retu->csrc[_x]); } return _retu; @@ -162,7 +166,8 @@ RTPExtHeader *extract_ext_header ( const uint8_t *payload, uint16_t length ) } uint16_t _ext_length; - bytes_to_U16(&_ext_length, _it); + memcpy(&_ext_length, _it, sizeof(_ext_length)); + _ext_length = ntohs(_ext_length); _it += 2; @@ -173,7 +178,8 @@ RTPExtHeader *extract_ext_header ( const uint8_t *payload, uint16_t length ) } _retu->length = _ext_length; - bytes_to_U16(&_retu->type, _it); + memcpy(&_retu->type, _it, sizeof(_retu->type)); + _retu->type = ntohs(_retu->type); _it += 2; if ( !(_retu->table = calloc(_ext_length, sizeof (uint32_t))) ) { @@ -186,7 +192,8 @@ RTPExtHeader *extract_ext_header ( const uint8_t *payload, uint16_t length ) for ( _x = 0; _x < _ext_length; _x++ ) { _it += 4; - bytes_to_U32(&(_retu->table[_x]), _it); + memcpy(&(_retu->table[_x]), _it, sizeof(_retu->table[_x])); + _retu->table[_x] = ntohl(_retu->table[_x]); } return _retu; @@ -202,12 +209,16 @@ RTPExtHeader *extract_ext_header ( const uint8_t *payload, uint16_t length ) uint8_t *add_header ( RTPHeader *header, uint8_t *payload ) { uint8_t _cc = GET_FLAG_CSRCC ( header ); - uint8_t *_it = payload; + uint16_t sequnum; + uint32_t timestamp; + uint32_t ssrc; + uint32_t csrc; /* Add sequence number first */ - U16_to_bytes(_it, header->sequnum); + sequnum = htons(header->sequnum); + memcpy(_it, &sequnum, sizeof(sequnum)); _it += 2; *_it = header->flags; @@ -216,15 +227,18 @@ uint8_t *add_header ( RTPHeader *header, uint8_t *payload ) ++_it; - U32_to_bytes( _it, header->timestamp); + timestamp = htonl(header->timestamp); + memcpy(_it, ×tamp, sizeof(timestamp)); _it += 4; - U32_to_bytes( _it, header->ssrc); + ssrc = htonl(header->ssrc); + memcpy(_it, &ssrc, sizeof(ssrc)); uint8_t _x; for ( _x = 0; _x < _cc; _x++ ) { _it += 4; - U32_to_bytes( _it, header->csrc[_x]); + csrc = htonl(header->csrc[_x]); + memcpy(_it, &csrc, sizeof(csrc)); } return _it + 4; @@ -240,10 +254,15 @@ uint8_t *add_header ( RTPHeader *header, uint8_t *payload ) uint8_t *add_ext_header ( RTPExtHeader *header, uint8_t *payload ) { uint8_t *_it = payload; + uint16_t length; + uint16_t type; + uint32_t entry; - U16_to_bytes(_it, header->length); + length = htons(header->length); + memcpy(_it, &length, sizeof(length)); _it += 2; - U16_to_bytes(_it, header->type); + type = htons(header->type); + memcpy(_it, &type, sizeof(type)); _it -= 2; /* Return to 0 position */ if ( header->table ) { @@ -251,7 +270,8 @@ uint8_t *add_ext_header ( RTPExtHeader *header, uint8_t *payload ) for ( _x = 0; _x < header->length; _x++ ) { _it += 4; - U32_to_bytes(_it, header->table[_x]); + entry = htonl(header->table[_x]); + memcpy(_it, &entry, sizeof(entry)); } } diff --git a/toxcore/util.c b/toxcore/util.c index 969ee704..ee4fa3b2 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -133,61 +133,3 @@ 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 955ce8c4..7992a985 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -47,17 +47,4 @@ typedef int (*load_state_callback_func)(void *outer, const uint8_t *data, uint32 int load_state(load_state_callback_func load_state_callback, void *outer, const 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