Debian Codegraph @

strings/librt_strings_common.h

raw
1 #ifndef LIBRT_STRINGS_COMMON_H
2 #define LIBRT_STRINGS_COMMON_H
3
4 #include <Python.h>
5 #include <stdint.h>
6 #include <string.h>
7
8 // Byte-swap functions for endianness conversion (needed for both LE and BE operations)
9 #if defined(_MSC_VER)
10 # include <stdlib.h>
11 # define BSWAP16(x) _byteswap_ushort(x)
12 # define BSWAP32(x) _byteswap_ulong(x)
13 # define BSWAP64(x) _byteswap_uint64(x)
14 #elif defined(__GNUC__) || defined(__clang__)
15 # define BSWAP16(x) __builtin_bswap16(x)
16 # define BSWAP32(x) __builtin_bswap32(x)
17 # define BSWAP64(x) __builtin_bswap64(x)
18 #else
19 // Fallback for other compilers (slower but portable)
20 static inline uint16_t BSWAP16(uint16_t x) {
21 return (uint16_t)((x >> 8) | (x << 8));
22 }
23 static inline uint32_t BSWAP32(uint32_t x) {
24 return ((x >> 24) & 0xFFU) |
25 ((x >> 8) & 0xFF00U) |
26 ((x << 8) & 0xFF0000U) |
27 ((x << 24) & 0xFF000000U);
28 }
29 static inline uint64_t BSWAP64(uint64_t x) {
30 return ((x >> 56) & 0xFFULL) |
31 ((x >> 40) & 0xFF00ULL) |
32 ((x >> 24) & 0xFF0000ULL) |
33 ((x >> 8) & 0xFF000000ULL) |
34 ((x << 8) & 0xFF00000000ULL) |
35 ((x << 24) & 0xFF0000000000ULL) |
36 ((x << 40) & 0xFF000000000000ULL) |
37 ((x << 56) & 0xFF00000000000000ULL);
38 }
39 #endif
40
41 // Length of the default buffer embedded directly in a BytesWriter object
42 #define WRITER_EMBEDDED_BUF_LEN 256
43
44 typedef struct {
45 PyObject_HEAD
46 char *buf; // Beginning of the buffer
47 Py_ssize_t len; // Current length (number of bytes written)
48 Py_ssize_t capacity; // Total capacity of the buffer
49 char data[WRITER_EMBEDDED_BUF_LEN]; // Default buffer
50 } BytesWriterObject;
51
52 // Write a 16-bit signed integer in little-endian format to BytesWriter.
53 // NOTE: This does NOT check buffer capacity - caller must ensure space is available.
54 static inline void
55 BytesWriter_WriteI16LEUnsafe(BytesWriterObject *self, int16_t value) {
56 // memcpy is reliably optimized to a single store by GCC, Clang, and MSVC
57 #if PY_BIG_ENDIAN
58 uint16_t swapped = BSWAP16((uint16_t)value);
59 memcpy(self->buf + self->len, &swapped, 2);
60 #else
61 memcpy(self->buf + self->len, &value, 2);
62 #endif
63 self->len += 2;
64 }
65
66 // Write a 16-bit signed integer in big-endian format to BytesWriter.
67 // NOTE: This does NOT check buffer capacity - caller must ensure space is available.
68 static inline void
69 BytesWriter_WriteI16BEUnsafe(BytesWriterObject *self, int16_t value) {
70 // memcpy is reliably optimized to a single store by GCC, Clang, and MSVC
71 #if PY_BIG_ENDIAN
72 memcpy(self->buf + self->len, &value, 2);
73 #else
74 uint16_t swapped = BSWAP16((uint16_t)value);
75 memcpy(self->buf + self->len, &swapped, 2);
76 #endif
77 self->len += 2;
78 }
79
80 // Read a 16-bit signed integer in little-endian format from bytes.
81 // NOTE: This does NOT check bounds - caller must ensure valid index.
82 static inline int16_t
83 CPyBytes_ReadI16LEUnsafe(const unsigned char *data) {
84 // memcpy is reliably optimized to a single load by GCC, Clang, and MSVC
85 uint16_t value;
86 memcpy(&value, data, 2);
87 #if PY_BIG_ENDIAN
88 value = BSWAP16(value);
89 #endif
90 return (int16_t)value;
91 }
92
93 // Read a 16-bit signed integer in big-endian format from bytes.
94 // NOTE: This does NOT check bounds - caller must ensure valid index.
95 static inline int16_t
96 CPyBytes_ReadI16BEUnsafe(const unsigned char *data) {
97 // memcpy is reliably optimized to a single load by GCC, Clang, and MSVC
98 uint16_t value;
99 memcpy(&value, data, 2);
100 #if PY_BIG_ENDIAN
101 // Already in big-endian format, no swap needed
102 #else
103 value = BSWAP16(value);
104 #endif
105 return (int16_t)value;
106 }
107
108 // Write a 32-bit signed integer in little-endian format to BytesWriter.
109 // NOTE: This does NOT check buffer capacity - caller must ensure space is available.
110 static inline void
111 BytesWriter_WriteI32LEUnsafe(BytesWriterObject *self, int32_t value) {
112 // memcpy is reliably optimized to a single store by GCC, Clang, and MSVC
113 #if PY_BIG_ENDIAN
114 uint32_t swapped = BSWAP32((uint32_t)value);
115 memcpy(self->buf + self->len, &swapped, 4);
116 #else
117 memcpy(self->buf + self->len, &value, 4);
118 #endif
119 self->len += 4;
120 }
121
122 // Write a 32-bit signed integer in big-endian format to BytesWriter.
123 // NOTE: This does NOT check buffer capacity - caller must ensure space is available.
124 static inline void
125 BytesWriter_WriteI32BEUnsafe(BytesWriterObject *self, int32_t value) {
126 // memcpy is reliably optimized to a single store by GCC, Clang, and MSVC
127 #if PY_BIG_ENDIAN
128 memcpy(self->buf + self->len, &value, 4);
129 #else
130 uint32_t swapped = BSWAP32((uint32_t)value);
131 memcpy(self->buf + self->len, &swapped, 4);
132 #endif
133 self->len += 4;
134 }
135
136 // Read a 32-bit signed integer in little-endian format from bytes.
137 // NOTE: This does NOT check bounds - caller must ensure valid index.
138 static inline int32_t
139 CPyBytes_ReadI32LEUnsafe(const unsigned char *data) {
140 // memcpy is reliably optimized to a single load by GCC, Clang, and MSVC
141 uint32_t value;
142 memcpy(&value, data, 4);
143 #if PY_BIG_ENDIAN
144 value = BSWAP32(value);
145 #endif
146 return (int32_t)value;
147 }
148
149 // Read a 32-bit signed integer in big-endian format from bytes.
150 // NOTE: This does NOT check bounds - caller must ensure valid index.
151 static inline int32_t
152 CPyBytes_ReadI32BEUnsafe(const unsigned char *data) {
153 // memcpy is reliably optimized to a single load by GCC, Clang, and MSVC
154 uint32_t value;
155 memcpy(&value, data, 4);
156 #if PY_BIG_ENDIAN
157 // Already in big-endian format, no swap needed
158 #else
159 value = BSWAP32(value);
160 #endif
161 return (int32_t)value;
162 }
163
164 // Write a 64-bit signed integer in little-endian format to BytesWriter.
165 // NOTE: This does NOT check buffer capacity - caller must ensure space is available.
166 static inline void
167 BytesWriter_WriteI64LEUnsafe(BytesWriterObject *self, int64_t value) {
168 // memcpy is reliably optimized to a single store by GCC, Clang, and MSVC
169 #if PY_BIG_ENDIAN
170 uint64_t swapped = BSWAP64((uint64_t)value);
171 memcpy(self->buf + self->len, &swapped, 8);
172 #else
173 memcpy(self->buf + self->len, &value, 8);
174 #endif
175 self->len += 8;
176 }
177
178 // Write a 64-bit signed integer in big-endian format to BytesWriter.
179 // NOTE: This does NOT check buffer capacity - caller must ensure space is available.
180 static inline void
181 BytesWriter_WriteI64BEUnsafe(BytesWriterObject *self, int64_t value) {
182 // memcpy is reliably optimized to a single store by GCC, Clang, and MSVC
183 #if PY_BIG_ENDIAN
184 memcpy(self->buf + self->len, &value, 8);
185 #else
186 uint64_t swapped = BSWAP64((uint64_t)value);
187 memcpy(self->buf + self->len, &swapped, 8);
188 #endif
189 self->len += 8;
190 }
191
192 // Read a 64-bit signed integer in little-endian format from bytes.
193 // NOTE: This does NOT check bounds - caller must ensure valid index.
194 static inline int64_t
195 CPyBytes_ReadI64LEUnsafe(const unsigned char *data) {
196 // memcpy is reliably optimized to a single load by GCC, Clang, and MSVC
197 uint64_t value;
198 memcpy(&value, data, 8);
199 #if PY_BIG_ENDIAN
200 value = BSWAP64(value);
201 #endif
202 return (int64_t)value;
203 }
204
205 // Read a 64-bit signed integer in big-endian format from bytes.
206 // NOTE: This does NOT check bounds - caller must ensure valid index.
207 static inline int64_t
208 CPyBytes_ReadI64BEUnsafe(const unsigned char *data) {
209 // memcpy is reliably optimized to a single load by GCC, Clang, and MSVC
210 uint64_t value;
211 memcpy(&value, data, 8);
212 #if PY_BIG_ENDIAN
213 // Already in big-endian format, no swap needed
214 #else
215 value = BSWAP64(value);
216 #endif
217 return (int64_t)value;
218 }
219
220 // Write a 32-bit float in little-endian format to BytesWriter.
221 // NOTE: This does NOT check buffer capacity - caller must ensure space is available.
222 static inline void
223 BytesWriter_WriteF32LEUnsafe(BytesWriterObject *self, float value) {
224 // memcpy is reliably optimized to a single store by GCC, Clang, and MSVC
225 #if PY_BIG_ENDIAN
226 uint32_t bits;
227 memcpy(&bits, &value, 4);
228 bits = BSWAP32(bits);
229 memcpy(self->buf + self->len, &bits, 4);
230 #else
231 memcpy(self->buf + self->len, &value, 4);
232 #endif
233 self->len += 4;
234 }
235
236 // Write a 32-bit float in big-endian format to BytesWriter.
237 // NOTE: This does NOT check buffer capacity - caller must ensure space is available.
238 static inline void
239 BytesWriter_WriteF32BEUnsafe(BytesWriterObject *self, float value) {
240 // memcpy is reliably optimized to a single store by GCC, Clang, and MSVC
241 #if PY_BIG_ENDIAN
242 memcpy(self->buf + self->len, &value, 4);
243 #else
244 uint32_t bits;
245 memcpy(&bits, &value, 4);
246 bits = BSWAP32(bits);
247 memcpy(self->buf + self->len, &bits, 4);
248 #endif
249 self->len += 4;
250 }
251
252 // Read a 32-bit float in little-endian format from bytes.
253 // NOTE: This does NOT check bounds - caller must ensure valid index.
254 static inline float
255 CPyBytes_ReadF32LEUnsafe(const unsigned char *data) {
256 // memcpy is reliably optimized to a single load by GCC, Clang, and MSVC
257 float value;
258 #if PY_BIG_ENDIAN
259 uint32_t bits;
260 memcpy(&bits, data, 4);
261 bits = BSWAP32(bits);
262 memcpy(&value, &bits, 4);
263 #else
264 memcpy(&value, data, 4);
265 #endif
266 return value;
267 }
268
269 // Read a 32-bit float in big-endian format from bytes.
270 // NOTE: This does NOT check bounds - caller must ensure valid index.
271 static inline float
272 CPyBytes_ReadF32BEUnsafe(const unsigned char *data) {
273 // memcpy is reliably optimized to a single load by GCC, Clang, and MSVC
274 float value;
275 #if PY_BIG_ENDIAN
276 memcpy(&value, data, 4);
277 #else
278 uint32_t bits;
279 memcpy(&bits, data, 4);
280 bits = BSWAP32(bits);
281 memcpy(&value, &bits, 4);
282 #endif
283 return value;
284 }
285
286 // Write a 64-bit float (double) in little-endian format to BytesWriter.
287 // NOTE: This does NOT check buffer capacity - caller must ensure space is available.
288 static inline void
289 BytesWriter_WriteF64LEUnsafe(BytesWriterObject *self, double value) {
290 // memcpy is reliably optimized to a single store by GCC, Clang, and MSVC
291 #if PY_BIG_ENDIAN
292 uint64_t bits;
293 memcpy(&bits, &value, 8);
294 bits = BSWAP64(bits);
295 memcpy(self->buf + self->len, &bits, 8);
296 #else
297 memcpy(self->buf + self->len, &value, 8);
298 #endif
299 self->len += 8;
300 }
301
302 // Write a 64-bit float (double) in big-endian format to BytesWriter.
303 // NOTE: This does NOT check buffer capacity - caller must ensure space is available.
304 static inline void
305 BytesWriter_WriteF64BEUnsafe(BytesWriterObject *self, double value) {
306 // memcpy is reliably optimized to a single store by GCC, Clang, and MSVC
307 #if PY_BIG_ENDIAN
308 memcpy(self->buf + self->len, &value, 8);
309 #else
310 uint64_t bits;
311 memcpy(&bits, &value, 8);
312 bits = BSWAP64(bits);
313 memcpy(self->buf + self->len, &bits, 8);
314 #endif
315 self->len += 8;
316 }
317
318 // Read a 64-bit float (double) in little-endian format from bytes.
319 // NOTE: This does NOT check bounds - caller must ensure valid index.
320 static inline double
321 CPyBytes_ReadF64LEUnsafe(const unsigned char *data) {
322 // memcpy is reliably optimized to a single load by GCC, Clang, and MSVC
323 double value;
324 #if PY_BIG_ENDIAN
325 uint64_t bits;
326 memcpy(&bits, data, 8);
327 bits = BSWAP64(bits);
328 memcpy(&value, &bits, 8);
329 #else
330 memcpy(&value, data, 8);
331 #endif
332 return value;
333 }
334
335 // Read a 64-bit float (double) in big-endian format from bytes.
336 // NOTE: This does NOT check bounds - caller must ensure valid index.
337 static inline double
338 CPyBytes_ReadF64BEUnsafe(const unsigned char *data) {
339 // memcpy is reliably optimized to a single load by GCC, Clang, and MSVC
340 double value;
341 #if PY_BIG_ENDIAN
342 memcpy(&value, data, 8);
343 #else
344 uint64_t bits;
345 memcpy(&bits, data, 8);
346 bits = BSWAP64(bits);
347 memcpy(&value, &bits, 8);
348 #endif
349 return value;
350 }
351
352 #endif // LIBRT_STRINGS_COMMON_H