Debian Codegraph @

common/byte_stream.h

raw symbols SCIP
1 /*
2 * Byte stream functions
3 *
4 * Copyright (C) 2012-2025, Joachim Metz <[email protected]>
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
22 #if !defined( _BYTE_STREAM_H )
23 #define _BYTE_STREAM_H
24
25 #include "common.h"
26 #include "types.h"
27
28 #if defined( __cplusplus )
29 extern "C" {
30 #endif
31
32 #define _BYTE_STREAM_HOST_IS_ENDIAN_BIG ( *((uint32_t *) "\x01\x02\x03\x04" ) == 0x01020304 )
33 #define _BYTE_STREAM_HOST_IS_ENDIAN_LITTLE ( *((uint32_t *) "\x01\x02\x03\x04" ) == 0x04030201 )
34 #define _BYTE_STREAM_HOST_IS_ENDIAN_MIDDLE ( *((uint32_t *) "\x01\x02\x03\x04" ) == 0x02010403 )
35
36 #define _BYTE_STREAM_ENDIAN_BIG (uint8_t) 'b'
37 #define _BYTE_STREAM_ENDIAN_LITTLE (uint8_t) 'l'
38 #define _BYTE_STREAM_ENDIAN_MIDDLE (uint8_t) 'm'
39
40 typedef union byte_stream_float32
41 {
42 float floating_point;
43 uint32_t integer;
44
45 } byte_stream_float32_t;
46
47 typedef union byte_stream_float64
48 {
49 double floating_point;
50 uint64_t integer;
51
52 } byte_stream_float64_t;
53
54 #define byte_stream_copy_to_uint16_big_endian( byte_stream, value ) \
55 ( value ) = ( byte_stream )[ 0 ]; \
56 ( value ) <<= 8; \
57 ( value ) |= ( byte_stream )[ 1 ];
58
59 #define byte_stream_copy_to_uint16_little_endian( byte_stream, value ) \
60 ( value ) = ( byte_stream )[ 1 ]; \
61 ( value ) <<= 8; \
62 ( value ) |= ( byte_stream )[ 0 ];
63
64 #define byte_stream_copy_to_uint24_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
71 #define byte_stream_copy_to_uint24_little_endian( byte_stream, value ) \
72 ( value ) = ( byte_stream )[ 2 ]; \
73 ( value ) <<= 8; \
74 ( value ) |= ( byte_stream )[ 1 ]; \
75 ( value ) <<= 8; \
76 ( value ) |= ( byte_stream )[ 0 ];
77
78 #define byte_stream_copy_to_uint32_big_endian( byte_stream, value ) \
79 ( value ) = ( byte_stream )[ 0 ]; \
80 ( value ) <<= 8; \
81 ( value ) |= ( byte_stream )[ 1 ]; \
82 ( value ) <<= 8; \
83 ( value ) |= ( byte_stream )[ 2 ]; \
84 ( value ) <<= 8; \
85 ( value ) |= ( byte_stream )[ 3 ];
86
87 #define byte_stream_copy_to_uint32_little_endian( byte_stream, value ) \
88 ( value ) = ( byte_stream )[ 3 ]; \
89 ( value ) <<= 8; \
90 ( value ) |= ( byte_stream )[ 2 ]; \
91 ( value ) <<= 8; \
92 ( value ) |= ( byte_stream )[ 1 ]; \
93 ( value ) <<= 8; \
94 ( value ) |= ( byte_stream )[ 0 ];
95
96 #define byte_stream_copy_to_uint48_big_endian( byte_stream, value ) \
97 ( value ) = ( byte_stream )[ 0 ]; \
98 ( value ) <<= 8; \
99 ( value ) |= ( byte_stream )[ 1 ]; \
100 ( value ) <<= 8; \
101 ( value ) |= ( byte_stream )[ 2 ]; \
102 ( value ) <<= 8; \
103 ( value ) |= ( byte_stream )[ 3 ]; \
104 ( value ) <<= 8; \
105 ( value ) |= ( byte_stream )[ 4 ]; \
106 ( value ) <<= 8; \
107 ( value ) |= ( byte_stream )[ 5 ];
108
109 #define byte_stream_copy_to_uint48_little_endian( byte_stream, value ) \
110 ( value ) |= ( byte_stream )[ 5 ]; \
111 ( value ) <<= 8; \
112 ( value ) |= ( byte_stream )[ 4 ]; \
113 ( value ) <<= 8; \
114 ( value ) |= ( byte_stream )[ 3 ]; \
115 ( value ) <<= 8; \
116 ( value ) |= ( byte_stream )[ 2 ]; \
117 ( value ) <<= 8; \
118 ( value ) |= ( byte_stream )[ 1 ]; \
119 ( value ) <<= 8; \
120 ( value ) |= ( byte_stream )[ 0 ];
121
122 #define byte_stream_copy_to_uint64_big_endian( byte_stream, value ) \
123 ( value ) = ( byte_stream )[ 0 ]; \
124 ( value ) <<= 8; \
125 ( value ) |= ( byte_stream )[ 1 ]; \
126 ( value ) <<= 8; \
127 ( value ) |= ( byte_stream )[ 2 ]; \
128 ( value ) <<= 8; \
129 ( value ) |= ( byte_stream )[ 3 ]; \
130 ( value ) <<= 8; \
131 ( value ) |= ( byte_stream )[ 4 ]; \
132 ( value ) <<= 8; \
133 ( value ) |= ( byte_stream )[ 5 ]; \
134 ( value ) <<= 8; \
135 ( value ) |= ( byte_stream )[ 6 ]; \
136 ( value ) <<= 8; \
137 ( value ) |= ( byte_stream )[ 7 ];
138
139 #define byte_stream_copy_to_uint64_little_endian( byte_stream, value ) \
140 ( value ) = ( byte_stream )[ 7 ]; \
141 ( value ) <<= 8; \
142 ( value ) |= ( byte_stream )[ 6 ]; \
143 ( value ) <<= 8; \
144 ( value ) |= ( byte_stream )[ 5 ]; \
145 ( value ) <<= 8; \
146 ( value ) |= ( byte_stream )[ 4 ]; \
147 ( value ) <<= 8; \
148 ( value ) |= ( byte_stream )[ 3 ]; \
149 ( value ) <<= 8; \
150 ( value ) |= ( byte_stream )[ 2 ]; \
151 ( value ) <<= 8; \
152 ( value ) |= ( byte_stream )[ 1 ]; \
153 ( value ) <<= 8; \
154 ( value ) |= ( byte_stream )[ 0 ];
155
156 #define byte_stream_copy_from_uint16_big_endian( byte_stream, value ) \
157 ( byte_stream )[ 0 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
158 ( byte_stream )[ 1 ] = (uint8_t) ( ( value ) & 0x0ff )
159
160 #define byte_stream_copy_from_uint16_little_endian( byte_stream, value ) \
161 ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
162 ( byte_stream )[ 0 ] = (uint8_t) ( ( value ) & 0x0ff )
163
164 #define byte_stream_copy_from_uint24_big_endian( byte_stream, value ) \
165 ( byte_stream )[ 0 ] = (uint8_t) ( ( ( value ) >> 16 ) & 0x0ff ); \
166 ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
167 ( byte_stream )[ 2 ] = (uint8_t) ( ( value ) & 0x0ff )
168
169 #define byte_stream_copy_from_uint24_little_endian( byte_stream, value ) \
170 ( byte_stream )[ 2 ] = (uint8_t) ( ( ( value ) >> 16 ) & 0x0ff ); \
171 ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
172 ( byte_stream )[ 0 ] = (uint8_t) ( ( value ) & 0x0ff )
173
174 #define byte_stream_copy_from_uint32_big_endian( byte_stream, value ) \
175 ( byte_stream )[ 0 ] = (uint8_t) ( ( ( value ) >> 24 ) & 0x0ff ); \
176 ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 16 ) & 0x0ff ); \
177 ( byte_stream )[ 2 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
178 ( byte_stream )[ 3 ] = (uint8_t) ( ( value ) & 0x0ff )
179
180 #define byte_stream_copy_from_uint32_little_endian( byte_stream, value ) \
181 ( byte_stream )[ 3 ] = (uint8_t) ( ( ( value ) >> 24 ) & 0x0ff ); \
182 ( byte_stream )[ 2 ] = (uint8_t) ( ( ( value ) >> 16 ) & 0x0ff ); \
183 ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
184 ( byte_stream )[ 0 ] = (uint8_t) ( ( value ) & 0x0ff )
185
186 #define byte_stream_copy_from_uint48_big_endian( byte_stream, value ) \
187 ( byte_stream )[ 0 ] = (uint8_t) ( ( ( value ) >> 40 ) & 0x0ff ); \
188 ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 32 ) & 0x0ff ); \
189 ( byte_stream )[ 2 ] = (uint8_t) ( ( ( value ) >> 24 ) & 0x0ff ); \
190 ( byte_stream )[ 3 ] = (uint8_t) ( ( ( value ) >> 16 ) & 0x0ff ); \
191 ( byte_stream )[ 4 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
192 ( byte_stream )[ 5 ] = (uint8_t) ( ( value ) & 0x0ff )
193
194 #define byte_stream_copy_from_uint48_little_endian( byte_stream, value ) \
195 ( byte_stream )[ 5 ] = (uint8_t) ( ( ( value ) >> 40 ) & 0x0ff ); \
196 ( byte_stream )[ 4 ] = (uint8_t) ( ( ( value ) >> 32 ) & 0x0ff ); \
197 ( byte_stream )[ 3 ] = (uint8_t) ( ( ( value ) >> 24 ) & 0x0ff ); \
198 ( byte_stream )[ 2 ] = (uint8_t) ( ( ( value ) >> 16 ) & 0x0ff ); \
199 ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
200 ( byte_stream )[ 0 ] = (uint8_t) ( ( value ) & 0x0ff )
201
202 #define byte_stream_copy_from_uint64_big_endian( byte_stream, value ) \
203 ( byte_stream )[ 0 ] = (uint8_t) ( ( ( value ) >> 56 ) & 0x0ff ); \
204 ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 48 ) & 0x0ff ); \
205 ( byte_stream )[ 2 ] = (uint8_t) ( ( ( value ) >> 40 ) & 0x0ff ); \
206 ( byte_stream )[ 3 ] = (uint8_t) ( ( ( value ) >> 32 ) & 0x0ff ); \
207 ( byte_stream )[ 4 ] = (uint8_t) ( ( ( value ) >> 24 ) & 0x0ff ); \
208 ( byte_stream )[ 5 ] = (uint8_t) ( ( ( value ) >> 16 ) & 0x0ff ); \
209 ( byte_stream )[ 6 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
210 ( byte_stream )[ 7 ] = (uint8_t) ( ( value ) & 0x0ff )
211
212 #define byte_stream_copy_from_uint64_little_endian( byte_stream, value ) \
213 ( byte_stream )[ 7 ] = (uint8_t) ( ( ( value ) >> 56 ) & 0x0ff ); \
214 ( byte_stream )[ 6 ] = (uint8_t) ( ( ( value ) >> 48 ) & 0x0ff ); \
215 ( byte_stream )[ 5 ] = (uint8_t) ( ( ( value ) >> 40 ) & 0x0ff ); \
216 ( byte_stream )[ 4 ] = (uint8_t) ( ( ( value ) >> 32 ) & 0x0ff ); \
217 ( byte_stream )[ 3 ] = (uint8_t) ( ( ( value ) >> 24 ) & 0x0ff ); \
218 ( byte_stream )[ 2 ] = (uint8_t) ( ( ( value ) >> 16 ) & 0x0ff ); \
219 ( byte_stream )[ 1 ] = (uint8_t) ( ( ( value ) >> 8 ) & 0x0ff ); \
220 ( byte_stream )[ 0 ] = (uint8_t) ( ( value ) & 0x0ff )
221
222 #define byte_stream_bit_rotate_left_8bit( byte_stream, number_of_bits ) \
223 ( ( ( byte_stream ) << ( number_of_bits ) ) | ( ( byte_stream ) >> ( 8 - ( number_of_bits ) ) ) )
224
225 #define byte_stream_bit_rotate_right_8bit( byte_stream, number_of_bits ) \
226 ( ( ( byte_stream ) >> ( number_of_bits ) ) | ( ( byte_stream ) << ( 8 - ( number_of_bits ) ) ) )
227
228 #define byte_stream_bit_rotate_left_16bit( byte_stream, number_of_bits ) \
229 ( ( ( byte_stream ) << ( number_of_bits ) ) | ( ( byte_stream ) >> ( 16 - ( number_of_bits ) ) ) )
230
231 #define byte_stream_bit_rotate_right_16bit( byte_stream, number_of_bits ) \
232 ( ( ( byte_stream ) >> ( number_of_bits ) ) | ( ( byte_stream ) << ( 16 - ( number_of_bits ) ) ) )
233
234 #define byte_stream_bit_rotate_left_32bit( byte_stream, number_of_bits ) \
235 ( ( ( byte_stream ) << ( number_of_bits ) ) | ( ( byte_stream ) >> ( 32 - ( number_of_bits ) ) ) )
236
237 #define byte_stream_bit_rotate_right_32bit( byte_stream, number_of_bits ) \
238 ( ( ( byte_stream ) >> ( number_of_bits ) ) | ( ( byte_stream ) << ( 32 - ( number_of_bits ) ) ) )
239
240 #define byte_stream_bit_rotate_left_64bit( byte_stream, number_of_bits ) \
241 ( ( ( byte_stream ) << ( number_of_bits ) ) | ( ( byte_stream ) >> ( 64 - ( number_of_bits ) ) ) )
242
243 #define byte_stream_bit_rotate_right_64bit( byte_stream, number_of_bits ) \
244 ( ( ( byte_stream ) >> ( number_of_bits ) ) | ( ( byte_stream ) << ( 64 - ( number_of_bits ) ) ) )
245
246 #define byte_stream_bit_rotate_left( value, number_of_bits ) \
247 ( ( ( value ) << ( number_of_bits ) ) | ( ( value ) >> ( ( sizeof( value ) << 3 ) - ( number_of_bits ) ) ) )
248
249 #define byte_stream_bit_rotate_right( value, number_of_bits ) \
250 ( ( ( value ) >> ( number_of_bits ) ) | ( ( value ) << ( ( sizeof( value ) << 3 ) - ( number_of_bits ) ) ) )
251
252 #if defined( __cplusplus )
253 }
254 #endif
255
256 #endif /* !defined( _BYTE_STREAM_H ) */
257