You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

258 lines
10 KiB

  1. /*
  2. * Byte stream functions
  3. *
  4. * Copyright (C) 2008-2019, Joachim Metz <joachim.metz@gmail.com>
  5. *
  6. * Refer to AUTHORS for acknowledgements.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #if !defined( _BYTE_STREAM_H )
  22. #define _BYTE_STREAM_H
  23. #include "common.h"
  24. #include "types.h"
  25. #if defined( __cplusplus )
  26. extern "C" {
  27. #endif
  28. #define _BYTE_STREAM_HOST_IS_ENDIAN_BIG ( *((uint32_t *) "\x01\x02\x03\x04" ) == 0x01020304 )
  29. #define _BYTE_STREAM_HOST_IS_ENDIAN_LITTLE ( *((uint32_t *) "\x01\x02\x03\x04" ) == 0x04030201 )
  30. #define _BYTE_STREAM_HOST_IS_ENDIAN_MIDDLE ( *((uint32_t *) "\x01\x02\x03\x04" ) == 0x02010403 )
  31. #define _BYTE_STREAM_ENDIAN_BIG (uint8_t) 'b'
  32. #define _BYTE_STREAM_ENDIAN_LITTLE (uint8_t) 'l'
  33. #define _BYTE_STREAM_ENDIAN_MIDDLE (uint8_t) 'm'
  34. typedef union byte_stream_float32
  35. {
  36. float floating_point;
  37. uint32_t integer;
  38. } byte_stream_float32_t;
  39. typedef union byte_stream_float64
  40. {
  41. double floating_point;
  42. uint64_t integer;
  43. } byte_stream_float64_t;
  44. #define byte_stream_copy_to_uint16_big_endian( byte_stream, value ) \
  45. ( value ) = ( byte_stream )[ 0 ]; \
  46. ( value ) <<= 8; \
  47. ( value ) |= ( byte_stream )[ 1 ];
  48. #define byte_stream_copy_to_uint16_little_endian( byte_stream, value ) \
  49. ( value ) = ( byte_stream )[ 1 ]; \
  50. ( value ) <<= 8; \
  51. ( value ) |= ( byte_stream )[ 0 ];
  52. #define byte_stream_copy_to_uint24_big_endian( byte_stream, value ) \
  53. ( value ) = ( byte_stream )[ 0 ]; \
  54. ( value ) <<= 8; \
  55. ( value ) |= ( byte_stream )[ 1 ]; \
  56. ( value ) <<= 8; \
  57. ( value ) |= ( byte_stream )[ 2 ];
  58. #define byte_stream_copy_to_uint24_little_endian( byte_stream, value ) \
  59. ( value ) = ( byte_stream )[ 2 ]; \
  60. ( value ) <<= 8; \
  61. ( value ) |= ( byte_stream )[ 1 ]; \
  62. ( value ) <<= 8; \
  63. ( value ) |= ( byte_stream )[ 0 ];
  64. #define byte_stream_copy_to_uint32_big_endian( byte_stream, value ) \
  65. ( value ) = ( byte_stream )[ 0 ]; \
  66. ( value ) <<= 8; \
  67. ( value ) |= ( byte_stream )[ 1 ]; \
  68. ( value ) <<= 8; \
  69. ( value ) |= ( byte_stream )[ 2 ]; \
  70. ( value ) <<= 8; \
  71. ( value ) |= ( byte_stream )[ 3 ];
  72. #define byte_stream_copy_to_uint32_little_endian( byte_stream, value ) \
  73. ( value ) = ( byte_stream )[ 3 ]; \
  74. ( value ) <<= 8; \
  75. ( value ) |= ( byte_stream )[ 2 ]; \
  76. ( value ) <<= 8; \
  77. ( value ) |= ( byte_stream )[ 1 ]; \
  78. ( value ) <<= 8; \
  79. ( value ) |= ( byte_stream )[ 0 ];
  80. #define byte_stream_copy_to_uint48_big_endian( byte_stream, value ) \
  81. ( value ) = ( byte_stream )[ 0 ]; \
  82. ( value ) <<= 8; \
  83. ( value ) |= ( byte_stream )[ 1 ]; \
  84. ( value ) <<= 8; \
  85. ( value ) |= ( byte_stream )[ 2 ]; \
  86. ( value ) <<= 8; \
  87. ( value ) |= ( byte_stream )[ 3 ]; \
  88. ( value ) <<= 8; \
  89. ( value ) |= ( byte_stream )[ 4 ]; \
  90. ( value ) <<= 8; \
  91. ( value ) |= ( byte_stream )[ 5 ];
  92. #define byte_stream_copy_to_uint48_little_endian( byte_stream, value ) \
  93. ( value ) |= ( byte_stream )[ 5 ]; \
  94. ( value ) <<= 8; \
  95. ( value ) |= ( byte_stream )[ 4 ]; \
  96. ( value ) <<= 8; \
  97. ( value ) |= ( byte_stream )[ 3 ]; \
  98. ( value ) <<= 8; \
  99. ( value ) |= ( byte_stream )[ 2 ]; \
  100. ( value ) <<= 8; \
  101. ( value ) |= ( byte_stream )[ 1 ]; \
  102. ( value ) <<= 8; \
  103. ( value ) |= ( byte_stream )[ 0 ];
  104. #define byte_stream_copy_to_uint64_big_endian( byte_stream, value ) \
  105. ( value ) = ( byte_stream )[ 0 ]; \
  106. ( value ) <<= 8; \
  107. ( value ) |= ( byte_stream )[ 1 ]; \
  108. ( value ) <<= 8; \
  109. ( value ) |= ( byte_stream )[ 2 ]; \
  110. ( value ) <<= 8; \
  111. ( value ) |= ( byte_stream )[ 3 ]; \
  112. ( value ) <<= 8; \
  113. ( value ) |= ( byte_stream )[ 4 ]; \
  114. ( value ) <<= 8; \
  115. ( value ) |= ( byte_stream )[ 5 ]; \
  116. ( value ) <<= 8; \
  117. ( value ) |= ( byte_stream )[ 6 ]; \
  118. ( value ) <<= 8; \
  119. ( value ) |= ( byte_stream )[ 7 ];
  120. #define byte_stream_copy_to_uint64_little_endian( byte_stream, value ) \
  121. ( value ) = ( byte_stream )[ 7 ]; \
  122. ( value ) <<= 8; \
  123. ( value ) |= ( byte_stream )[ 6 ]; \
  124. ( value ) <<= 8; \
  125. ( value ) |= ( byte_stream )[ 5 ]; \
  126. ( value ) <<= 8; \
  127. ( value ) |= ( byte_stream )[ 4 ]; \
  128. ( value ) <<= 8; \
  129. ( value ) |= ( byte_stream )[ 3 ]; \
  130. ( value ) <<= 8; \
  131. ( value ) |= ( byte_stream )[ 2 ]; \
  132. ( value ) <<= 8; \
  133. ( value ) |= ( byte_stream )[ 1 ]; \
  134. ( value ) <<= 8; \
  135. ( value ) |= ( byte_stream )[ 0 ];
  136. #define byte_stream_copy_from_uint16_big_endian( byte_stream, value ) \
  137. ( byte_stream )[ 0 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
  138. ( byte_stream )[ 1 ] = (uint8_t) ( ( value ) & 0x0ff )
  139. #define byte_stream_copy_from_uint16_little_endian( byte_stream, value ) \
  140. ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
  141. ( byte_stream )[ 0 ] = (uint8_t) ( ( value ) & 0x0ff )
  142. #define byte_stream_copy_from_uint24_big_endian( byte_stream, value ) \
  143. ( byte_stream )[ 0 ] = (uint8_t) ( ( ( value ) >> 16 ) & 0x0ff ); \
  144. ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
  145. ( byte_stream )[ 2 ] = (uint8_t) ( ( value ) & 0x0ff )
  146. #define byte_stream_copy_from_uint24_little_endian( byte_stream, value ) \
  147. ( byte_stream )[ 2 ] = (uint8_t) ( ( ( value ) >> 16 ) & 0x0ff ); \
  148. ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
  149. ( byte_stream )[ 0 ] = (uint8_t) ( ( value ) & 0x0ff )
  150. #define byte_stream_copy_from_uint32_big_endian( byte_stream, value ) \
  151. ( byte_stream )[ 0 ] = (uint8_t) ( ( ( value ) >> 24 ) & 0x0ff ); \
  152. ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 16 ) & 0x0ff ); \
  153. ( byte_stream )[ 2 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
  154. ( byte_stream )[ 3 ] = (uint8_t) ( ( value ) & 0x0ff )
  155. #define byte_stream_copy_from_uint32_little_endian( byte_stream, value ) \
  156. ( byte_stream )[ 3 ] = (uint8_t) ( ( ( value ) >> 24 ) & 0x0ff ); \
  157. ( byte_stream )[ 2 ] = (uint8_t) ( ( ( value ) >> 16 ) & 0x0ff ); \
  158. ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
  159. ( byte_stream )[ 0 ] = (uint8_t) ( ( value ) & 0x0ff )
  160. #define byte_stream_copy_from_uint48_big_endian( byte_stream, value ) \
  161. ( byte_stream )[ 0 ] = (uint8_t) ( ( ( value ) >> 40 ) & 0x0ff ); \
  162. ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 32 ) & 0x0ff ); \
  163. ( byte_stream )[ 2 ] = (uint8_t) ( ( ( value ) >> 24 ) & 0x0ff ); \
  164. ( byte_stream )[ 3 ] = (uint8_t) ( ( ( value ) >> 16 ) & 0x0ff ); \
  165. ( byte_stream )[ 4 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
  166. ( byte_stream )[ 5 ] = (uint8_t) ( ( value ) & 0x0ff )
  167. #define byte_stream_copy_from_uint48_little_endian( byte_stream, value ) \
  168. ( byte_stream )[ 5 ] = (uint8_t) ( ( ( value ) >> 40 ) & 0x0ff ); \
  169. ( byte_stream )[ 4 ] = (uint8_t) ( ( ( value ) >> 32 ) & 0x0ff ); \
  170. ( byte_stream )[ 3 ] = (uint8_t) ( ( ( value ) >> 24 ) & 0x0ff ); \
  171. ( byte_stream )[ 2 ] = (uint8_t) ( ( ( value ) >> 16 ) & 0x0ff ); \
  172. ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
  173. ( byte_stream )[ 0 ] = (uint8_t) ( ( value ) & 0x0ff )
  174. #define byte_stream_copy_from_uint64_big_endian( byte_stream, value ) \
  175. ( byte_stream )[ 0 ] = (uint8_t) ( ( ( value ) >> 56 ) & 0x0ff ); \
  176. ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 48 ) & 0x0ff ); \
  177. ( byte_stream )[ 2 ] = (uint8_t) ( ( ( value ) >> 40 ) & 0x0ff ); \
  178. ( byte_stream )[ 3 ] = (uint8_t) ( ( ( value ) >> 32 ) & 0x0ff ); \
  179. ( byte_stream )[ 4 ] = (uint8_t) ( ( ( value ) >> 24 ) & 0x0ff ); \
  180. ( byte_stream )[ 5 ] = (uint8_t) ( ( ( value ) >> 16 ) & 0x0ff ); \
  181. ( byte_stream )[ 6 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
  182. ( byte_stream )[ 7 ] = (uint8_t) ( ( value ) & 0x0ff )
  183. #define byte_stream_copy_from_uint64_little_endian( byte_stream, value ) \
  184. ( byte_stream )[ 7 ] = (uint8_t) ( ( ( value ) >> 56 ) & 0x0ff ); \
  185. ( byte_stream )[ 6 ] = (uint8_t) ( ( ( value ) >> 48 ) & 0x0ff ); \
  186. ( byte_stream )[ 5 ] = (uint8_t) ( ( ( value ) >> 40 ) & 0x0ff ); \
  187. ( byte_stream )[ 4 ] = (uint8_t) ( ( ( value ) >> 32 ) & 0x0ff ); \
  188. ( byte_stream )[ 3 ] = (uint8_t) ( ( ( value ) >> 24 ) & 0x0ff ); \
  189. ( byte_stream )[ 2 ] = (uint8_t) ( ( ( value ) >> 16 ) & 0x0ff ); \
  190. ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
  191. ( byte_stream )[ 0 ] = (uint8_t) ( ( value ) & 0x0ff )
  192. #define byte_stream_bit_rotate_left_8bit( byte_stream, number_of_bits ) \
  193. ( ( ( byte_stream ) << ( number_of_bits ) ) | ( ( byte_stream ) >> ( 8 - ( number_of_bits ) ) ) )
  194. #define byte_stream_bit_rotate_right_8bit( byte_stream, number_of_bits ) \
  195. ( ( ( byte_stream ) >> ( number_of_bits ) ) | ( ( byte_stream ) << ( 8 - ( number_of_bits ) ) ) )
  196. #define byte_stream_bit_rotate_left_16bit( byte_stream, number_of_bits ) \
  197. ( ( ( byte_stream ) << ( number_of_bits ) ) | ( ( byte_stream ) >> ( 16 - ( number_of_bits ) ) ) )
  198. #define byte_stream_bit_rotate_right_16bit( byte_stream, number_of_bits ) \
  199. ( ( ( byte_stream ) >> ( number_of_bits ) ) | ( ( byte_stream ) << ( 16 - ( number_of_bits ) ) ) )
  200. #define byte_stream_bit_rotate_left_32bit( byte_stream, number_of_bits ) \
  201. ( ( ( byte_stream ) << ( number_of_bits ) ) | ( ( byte_stream ) >> ( 32 - ( number_of_bits ) ) ) )
  202. #define byte_stream_bit_rotate_right_32bit( byte_stream, number_of_bits ) \
  203. ( ( ( byte_stream ) >> ( number_of_bits ) ) | ( ( byte_stream ) << ( 32 - ( number_of_bits ) ) ) )
  204. #define byte_stream_bit_rotate_left_64bit( byte_stream, number_of_bits ) \
  205. ( ( ( byte_stream ) << ( number_of_bits ) ) | ( ( byte_stream ) >> ( 64 - ( number_of_bits ) ) ) )
  206. #define byte_stream_bit_rotate_right_64bit( byte_stream, number_of_bits ) \
  207. ( ( ( byte_stream ) >> ( number_of_bits ) ) | ( ( byte_stream ) << ( 64 - ( number_of_bits ) ) ) )
  208. #define byte_stream_bit_rotate_left( value, number_of_bits ) \
  209. ( ( ( value ) << ( number_of_bits ) ) | ( ( value ) >> ( ( sizeof( value ) << 3 ) - ( number_of_bits ) ) ) )
  210. #define byte_stream_bit_rotate_right( value, number_of_bits ) \
  211. ( ( ( value ) >> ( number_of_bits ) ) | ( ( value ) << ( ( sizeof( value ) << 3 ) - ( number_of_bits ) ) ) )
  212. #if defined( __cplusplus )
  213. }
  214. #endif
  215. #endif /* !defined( _BYTE_STREAM_H ) */