suricata
util-byte.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2022 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Brian Rectanus <brectanu@gmail.com>
22  */
23 
24 #ifndef SURICATA_UTIL_BYTE_H
25 #define SURICATA_UTIL_BYTE_H
26 
27 #include <stdint.h>
28 
29 #define BYTE_BIG_ENDIAN 0
30 #define BYTE_LITTLE_ENDIAN 1
31 
32 /** Wrappers for OS dependent byte swapping functions */
33 #ifdef OS_FREEBSD
34 #include <sys/endian.h>
35 #define SCByteSwap16(x) bswap16(x)
36 #define SCByteSwap32(x) bswap32(x)
37 #define SCByteSwap64(x) bswap64(x)
38 #elif defined __OpenBSD__
39 #include <sys/types.h>
40 #define SCByteSwap16(x) swap16(x)
41 #define SCByteSwap32(x) swap32(x)
42 #define SCByteSwap64(x) swap64(x)
43 #elif OS_DARWIN
44 #include <libkern/OSByteOrder.h>
45 #define SCByteSwap16(x) OSSwapInt16(x)
46 #define SCByteSwap32(x) OSSwapInt32(x)
47 #define SCByteSwap64(x) OSSwapInt64(x)
48 #elif defined(__sun)
49 #include <sys/byteorder.h>
50 #define SCByteSwap16(x) BSWAP_16(x)
51 #define SCByteSwap32(x) BSWAP_32(x)
52 #define SCByteSwap64(x) BSWAP_64(x)
53 #elif defined(__WIN32) || defined(_WIN32)
54 /* Quick & dirty solution, nothing seems to exist for this in Win32 API */
55 #define SCByteSwap16(x) \
56  ((((x) & 0xff00) >> 8) \
57  | (((x) & 0x00ff) << 8))
58 #define SCByteSwap32(x) \
59  ((((x) & 0xff000000) >> 24) \
60  | (((x) & 0x00ff0000) >> 8) \
61  | (((x) & 0x0000ff00) << 8) \
62  | (((x) & 0x000000ff) << 24))
63 #define SCByteSwap64(x) \
64  ((((x) & 0xff00000000000000ull) >> 56) \
65  | (((x) & 0x00ff000000000000ull) >> 40) \
66  | (((x) & 0x0000ff0000000000ull) >> 24) \
67  | (((x) & 0x000000ff00000000ull) >> 8) \
68  | (((x) & 0x00000000ff000000ull) << 8) \
69  | (((x) & 0x0000000000ff0000ull) << 24) \
70  | (((x) & 0x000000000000ff00ull) << 40) \
71  | (((x) & 0x00000000000000ffull) << 56))
72 #else
73 #include <byteswap.h>
74 #define SCByteSwap16(x) bswap_16(x)
75 #define SCByteSwap32(x) bswap_32(x)
76 #define SCByteSwap64(x) bswap_64(x)
77 #endif /* OS_FREEBSD */
78 
79 /** \brief Turn byte array into string.
80  *
81  * All non-printables are copied over, except for '\0', which is
82  * turned into literal \0 in the string.
83  *
84  * \param bytes byte array
85  * \param nbytes number of bytes
86  * \return string nul-terminated string or NULL on error
87  */
88 char *BytesToString(const uint8_t *bytes, size_t nbytes);
89 
90 /**
91  * Extract bytes from a byte string and convert to a unint64_t.
92  *
93  * \param res Stores result
94  * \param e Endianness (BYTE_BIG_ENDIAN or BYTE_LITTLE_ENDIAN)
95  * \param len Number of bytes to extract (8 max)
96  * \param bytes Data to extract from
97  *
98  * \return n Number of bytes extracted on success
99  * \return -1 On error
100  */
101 int WARN_UNUSED ByteExtractUint64(uint64_t *res, int e, uint16_t len, const uint8_t *bytes);
102 
103 /**
104  * Extract bytes from a byte string and convert to a uint32_t.
105  *
106  * \param res Stores result
107  * \param e Endianness (BYTE_BIG_ENDIAN or BYTE_LITTLE_ENDIAN)
108  * \param len Number of bytes to extract (8 max)
109  * \param bytes Data to extract from
110  *
111  * \return n Number of bytes extracted on success
112  * \return -1 On error
113  */
114 int WARN_UNUSED ByteExtractUint32(uint32_t *res, int e, uint16_t len, const uint8_t *bytes);
115 
116 /**
117  * Extract bytes from a byte string and convert to a unint16_t.
118  *
119  * \param res Stores result
120  * \param e Endianness (BYTE_BIG_ENDIAN or BYTE_LITTLE_ENDIAN)
121  * \param len Number of bytes to extract (8 max)
122  * \param bytes Data to extract from
123  *
124  * \return n Number of bytes extracted on success
125  * \return -1 On error
126  */
127 int WARN_UNUSED ByteExtractUint16(uint16_t *res, int e, uint16_t len, const uint8_t *bytes);
128 
129 /**
130  * Extract unsigned integer value from a string.
131  *
132  * \param res Stores result
133  * \param base Base of the number to extract
134  * \param len Number of bytes to extract (23 max or 0 for unbounded)
135  * \param str String to extract from
136  * \param bool Enable strict check for parsers
137  *
138  * \return n Number of bytes extracted on success
139  * \return -1 On error
140  */
142  uint64_t *res, int base, size_t len, const char *str, bool strict);
143 
144 /**
145  * Extract unsigned integer value from a string as uint64_t.
146  *
147  * \param res Stores result
148  * \param base Base of the number to extract
149  * \param len Number of bytes to extract (23 max or 0 for unbounded)
150  * \param str String to extract from
151  *
152  * \return n Number of bytes extracted on success
153  * \return -1 On error
154  */
155 int WARN_UNUSED ByteExtractStringUint64(uint64_t *res, int base, size_t len, const char *str);
156 
157 /**
158  * Extract unsigned integer value from a string as uint32_t.
159  *
160  * \param res Stores result
161  * \param base Base of the number to extract
162  * \param len Number of bytes to extract (23 max or 0 for unbounded)
163  * \param str String to extract from
164  *
165  * \return n Number of bytes extracted on success
166  * \return -1 On error
167  */
168 int WARN_UNUSED ByteExtractStringUint32(uint32_t *res, int base, size_t len, const char *str);
169 
170 /**
171  * Extract unsigned integer value from a string as uint16_t.
172  *
173  * \param res Stores result
174  * \param base Base of the number to extract
175  * \param len Number of bytes to extract (23 max or 0 for unbounded)
176  * \param str String to extract from
177  *
178  * \return n Number of bytes extracted on success
179  * \return -1 On error
180  */
181 int WARN_UNUSED ByteExtractStringUint16(uint16_t *res, int base, size_t len, const char *str);
182 
183 /**
184  * Extract unsigned integer value from a string as uint8_t.
185  *
186  * \param res Stores result
187  * \param base Base of the number to extract
188  * \param len Number of bytes to extract (23 max or 0 for unbounded)
189  * \param str String to extract from
190  *
191  * \return n Number of bytes extracted on success
192  * \return -1 On error
193  */
194 int WARN_UNUSED ByteExtractStringUint8(uint8_t *res, int base, size_t len, const char *str);
195 
196 /**
197  * Extract signed integer value from a string.
198  *
199  * \param res Stores result
200  * \param base Base of the number to extract
201  * \param len Number of bytes to extract (23 max or 0 for unbounded)
202  * \param str String to extract from
203  * \param bool Enable strict check for parsers
204  *
205  * \return n Number of bytes extracted on success
206  * \return -1 On error
207  */
209  int64_t *res, int base, size_t len, const char *str, bool strict);
210 
211 /**
212  * Extract signed integer value from a string as uint64_t.
213  *
214  * \param res Stores result
215  * \param base Base of the number to extract
216  * \param len Number of bytes to extract (23 max or 0 for unbounded)
217  * \param str String to extract from
218  *
219  * \return n Number of bytes extracted on success
220  * \return -1 On error
221  */
222 int WARN_UNUSED ByteExtractStringInt64(int64_t *res, int base, size_t len, const char *str);
223 
224 /**
225  * Extract signed integer value from a string as uint32_t.
226  *
227  * \param res Stores result
228  * \param base Base of the number to extract
229  * \param len Number of bytes to extract (23 max or 0 for unbounded)
230  * \param str String to extract from
231  *
232  * \return n Number of bytes extracted on success
233  * \return -1 On error
234  */
235 int WARN_UNUSED ByteExtractStringInt32(int32_t *res, int base, size_t len, const char *str);
236 
237 /**
238  * Extract signed integer value from a string as uint16_t.
239  *
240  * \param res Stores result
241  * \param base Base of the number to extract
242  * \param len Number of bytes to extract (23 max or 0 for unbounded)
243  * \param str String to extract from
244  *
245  * \return n Number of bytes extracted on success
246  * \return -1 On error
247  */
248 int WARN_UNUSED ByteExtractStringInt16(int16_t *res, int base, size_t len, const char *str);
249 
250 /**
251  * Extract signed integer value from a string as uint8_t.
252  *
253  * \param res Stores result
254  * \param base Base of the number to extract
255  * \param len Number of bytes to extract (23 max or 0 for unbounded)
256  * \param str String to extract from
257  *
258  * \return n Number of bytes extracted on success
259  * \return -1 On error
260  */
261 int WARN_UNUSED ByteExtractStringInt8(int8_t *res, int base, size_t len, const char *str);
262 
263 /**
264  * Extract unsigned integer value from a string as uint64_t strictly.
265  *
266  * \param res Stores result
267  * \param base Base of the number to extract
268  * \param len Number of bytes to extract (23 max or 0 for unbounded)
269  * \param str String to extract from
270  *
271  * \return n Number of bytes extracted on success
272  * \return -1 On error
273  */
274 int StringParseUint64(uint64_t *res, int base, size_t len, const char *str);
275 
276 /**
277  * Extract unsigned integer value from a string as uint32_t strictly.
278  *
279  * \param res Stores result
280  * \param base Base of the number to extract
281  * \param len Number of bytes to extract (23 max or 0 for unbounded)
282  * \param str String to extract from
283  *
284  * \return n Number of bytes extracted on success
285  * \return -1 On error
286  */
287 int StringParseUint32(uint32_t *res, int base, size_t len, const char *str);
288 
289 /**
290  * Extract unsigned integer value from a string as uint16_t strictly.
291  *
292  * \param res Stores result
293  * \param base Base of the number to extract
294  * \param len Number of bytes to extract (23 max or 0 for unbounded)
295  * \param str String to extract from
296  *
297  * \return n Number of bytes extracted on success
298  * \return -1 On error
299  */
300 int StringParseUint16(uint16_t *res, int base, size_t len, const char *str);
301 
302 /**
303  * Extract unsigned integer value from a string as uint8_t strictly.
304  *
305  * \param res Stores result
306  * \param base Base of the number to extract
307  * \param len Number of bytes to extract (23 max or 0 for unbounded)
308  * \param str String to extract from
309  *
310  * \return n Number of bytes extracted on success
311  * \return -1 On error
312  */
313 int StringParseUint8(uint8_t *res, int base, size_t len, const char *str);
314 
315 /**
316  * Extract signed integer value from a string as int64_t strictly.
317  *
318  * \param res Stores result
319  * \param base Base of the number to extract
320  * \param len Number of bytes to extract (23 max or 0 for unbounded)
321  * \param str String to extract from
322  *
323  * \return n Number of bytes extracted on success
324  * \return -1 On error
325  */
326 int StringParseInt64(int64_t *res, int base, size_t len, const char *str);
327 
328 /**
329  * Extract signed integer value from a string as int32_t strictly.
330  *
331  * \param res Stores result
332  * \param base Base of the number to extract
333  * \param len Number of bytes to extract (23 max or 0 for unbounded)
334  * \param str String to extract from
335  *
336  * \return n Number of bytes extracted on success
337  * \return -1 On error
338  */
339 int StringParseInt32(int32_t *res, int base, size_t len, const char *str);
340 
341 /**
342  * Extract signed integer value from a string as int16_t strictly.
343  *
344  * \param res Stores result
345  * \param base Base of the number to extract
346  * \param len Number of bytes to extract (23 max or 0 for unbounded)
347  * \param str String to extract from
348  *
349  * \return n Number of bytes extracted on success
350  * \return -1 On error
351  */
352 int StringParseInt16(int16_t *res, int base, size_t len, const char *str);
353 
354 /**
355  * Extract signed integer value from a string as int8_t strictly.
356  *
357  * \param res Stores result
358  * \param base Base of the number to extract
359  * \param len Number of bytes to extract (23 max or 0 for unbounded)
360  * \param str String to extract from
361  *
362  * \return n Number of bytes extracted on success
363  * \return -1 On error
364  */
365 int StringParseInt8(int8_t *res, int base, size_t len, const char *str);
366 
367 /**
368  * Extract unsigned integer value from a string as uint64_t strictly within the range.
369  *
370  * \param res Stores result
371  * \param base Base of the number to extract
372  * \param len Number of bytes to extract (23 max or 0 for unbounded)
373  * \param str String to extract from
374  *
375  * \return n Number of bytes extracted on success
376  * \return -1 On error
377  */
379  uint64_t *res, int base, size_t len, const char *str, uint64_t min, uint64_t max);
380 
381 /**
382  * Extract unsigned integer value from a string as uint32_t strictly within the range.
383  *
384  * \param res Stores result
385  * \param base Base of the number to extract
386  * \param len Number of bytes to extract (23 max or 0 for unbounded)
387  * \param str String to extract from
388  *
389  * \return n Number of bytes extracted on success
390  * \return -1 On error
391  */
393  uint32_t *res, int base, size_t len, const char *str, uint32_t min, uint32_t max);
394 
395 /**
396  * Extract unsigned integer value from a string as uint16_t strictly within the range.
397  *
398  * \param res Stores result
399  * \param base Base of the number to extract
400  * \param len Number of bytes to extract (23 max or 0 for unbounded)
401  * \param str String to extract from
402  *
403  * \return n Number of bytes extracted on success
404  * \return -1 On error
405  */
407  uint16_t *res, int base, size_t len, const char *str, uint16_t min, uint16_t max);
408 
409 /**
410  * Extract unsigned integer value from a string as uint8_t strictly within the range.
411  *
412  * \param res Stores result
413  * \param base Base of the number to extract
414  * \param len Number of bytes to extract (23 max or 0 for unbounded)
415  * \param str String to extract from
416  *
417  * \return n Number of bytes extracted on success
418  * \return -1 On error
419  */
421  uint8_t *res, int base, size_t len, const char *str, uint8_t min, uint8_t max);
422 
423 /**
424  * Extract signed integer value from a string as int64_t strictly within the range.
425  *
426  * \param res Stores result
427  * \param base Base of the number to extract
428  * \param len Number of bytes to extract (23 max or 0 for unbounded)
429  * \param str String to extract from
430  *
431  * \return n Number of bytes extracted on success
432  * \return -1 On error
433  */
435  int64_t *res, int base, size_t len, const char *str, int64_t min, int64_t max);
436 
437 /**
438  * Extract signed integer value from a string as int32_t strictly within the range.
439  *
440  * \param res Stores result
441  * \param base Base of the number to extract
442  * \param len Number of bytes to extract (23 max or 0 for unbounded)
443  * \param str String to extract from
444  *
445  * \return n Number of bytes extracted on success
446  * \return -1 On error
447  */
449  int32_t *res, int base, size_t len, const char *str, int32_t min, int32_t max);
450 
451 /**
452  * Extract signed integer value from a string as int16_t strictly within the range.
453  *
454  * \param res Stores result
455  * \param base Base of the number to extract
456  * \param len Number of bytes to extract (23 max or 0 for unbounded)
457  * \param str String to extract from
458  *
459  * \return n Number of bytes extracted on success
460  * \return -1 On error
461  */
463  int16_t *res, int base, size_t len, const char *str, int16_t min, int16_t max);
464 
465 /**
466  * Extract signed integer value from a string as int8_t strictly within the range.
467  *
468  * \param res Stores result
469  * \param base Base of the number to extract
470  * \param len Number of bytes to extract (23 max or 0 for unbounded)
471  * \param str String to extract from
472  *
473  * \return n Number of bytes extracted on success
474  * \return -1 On error
475  */
477  int8_t *res, int base, size_t len, const char *str, int8_t min, int8_t max);
478 
479 #ifdef UNITTESTS
480 void ByteRegisterTests(void);
481 #endif /* UNITTESTS */
482 
483 /** ------ Inline functions ----- */
484 static inline int WARN_UNUSED ByteExtract(uint64_t *res, int e, uint16_t len, const uint8_t *bytes)
485 {
486  if ((e != BYTE_BIG_ENDIAN) && (e != BYTE_LITTLE_ENDIAN)) {
487  /** \todo Need standard return values */
488  return -1;
489  }
490 
491  *res = 0;
492 
493  /* Go through each byte and merge it into the result in the correct order */
494  /** \todo Probably a more efficient way to do this. */
495  for (int i = 0; i < len; i++) {
496  uint64_t b;
497  if (e == BYTE_LITTLE_ENDIAN) {
498  b = bytes[i];
499  }
500  else {
501  b = bytes[len - i - 1];
502  }
503 
504  *res |= (b << ((i & 7) << 3));
505  }
506 
507  return len;
508 }
509 
510 int HexToRaw(const uint8_t *in, size_t ins, uint8_t *out, size_t outs);
511 
512 #endif /* SURICATA_UTIL_BYTE_H */
ByteExtractStringInt32
int WARN_UNUSED ByteExtractStringInt32(int32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:507
ByteExtractUint32
int WARN_UNUSED ByteExtractUint32(uint32_t *res, int e, uint16_t len, const uint8_t *bytes)
Definition: util-byte.c:99
len
uint8_t len
Definition: app-layer-dnp3.h:2
StringParseUint16
int StringParseUint16(uint16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:296
ByteExtractStringInt64
int WARN_UNUSED ByteExtractStringInt64(int64_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:502
StringParseI32RangeCheck
int WARN_UNUSED StringParseI32RangeCheck(int32_t *res, int base, size_t len, const char *str, int32_t min, int32_t max)
Definition: util-byte.c:681
ByteExtractStringSigned
int WARN_UNUSED ByteExtractStringSigned(int64_t *res, int base, size_t len, const char *str, bool strict)
Definition: util-byte.c:456
ByteExtractStringUint64
int WARN_UNUSED ByteExtractStringUint64(uint64_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:190
ByteExtractStringUint32
int WARN_UNUSED ByteExtractStringUint32(uint32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:195
StringParseU16RangeCheck
int WARN_UNUSED StringParseU16RangeCheck(uint16_t *res, int base, size_t len, const char *str, uint16_t min, uint16_t max)
Definition: util-byte.c:395
ByteExtractUint16
int WARN_UNUSED ByteExtractUint16(uint16_t *res, int e, uint16_t len, const uint8_t *bytes)
Definition: util-byte.c:120
ByteExtractUint64
int WARN_UNUSED ByteExtractUint64(uint64_t *res, int e, uint16_t len, const uint8_t *bytes)
Definition: util-byte.c:75
StringParseUint8
int StringParseUint8(uint8_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:323
ByteExtractStringUint8
int WARN_UNUSED ByteExtractStringUint8(uint8_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:241
ByteExtractStringUint16
int WARN_UNUSED ByteExtractStringUint16(uint16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:218
StringParseU64RangeCheck
int WARN_UNUSED StringParseU64RangeCheck(uint64_t *res, int base, size_t len, const char *str, uint64_t min, uint64_t max)
Definition: util-byte.c:347
BytesToString
char * BytesToString(const uint8_t *bytes, size_t nbytes)
Turn byte array into string.
Definition: util-byte.c:41
BYTE_BIG_ENDIAN
#define BYTE_BIG_ENDIAN
Definition: util-byte.h:29
StringParseUint32
int StringParseUint32(uint32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:269
StringParseInt16
int StringParseInt16(int16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:612
StringParseI16RangeCheck
int WARN_UNUSED StringParseI16RangeCheck(int16_t *res, int base, size_t len, const char *str, int16_t min, int16_t max)
Definition: util-byte.c:711
BYTE_LITTLE_ENDIAN
#define BYTE_LITTLE_ENDIAN
Definition: util-byte.h:30
HexToRaw
int HexToRaw(const uint8_t *in, size_t ins, uint8_t *out, size_t outs)
Definition: util-byte.c:771
ByteExtractString
int WARN_UNUSED ByteExtractString(uint64_t *res, int base, size_t len, const char *str, bool strict)
Definition: util-byte.c:141
str
#define str(s)
Definition: suricata-common.h:316
StringParseI8RangeCheck
int WARN_UNUSED StringParseI8RangeCheck(int8_t *res, int base, size_t len, const char *str, int8_t min, int8_t max)
Definition: util-byte.c:741
WARN_UNUSED
#define WARN_UNUSED
Definition: bindgen.h:33
StringParseInt8
int StringParseInt8(int8_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:637
StringParseU32RangeCheck
int WARN_UNUSED StringParseU32RangeCheck(uint32_t *res, int base, size_t len, const char *str, uint32_t min, uint32_t max)
Definition: util-byte.c:366
ByteExtractStringInt16
int WARN_UNUSED ByteExtractStringInt16(int16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:532
StringParseI64RangeCheck
int WARN_UNUSED StringParseI64RangeCheck(int64_t *res, int base, size_t len, const char *str, int64_t min, int64_t max)
Definition: util-byte.c:662
StringParseInt32
int StringParseInt32(int32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:587
ByteRegisterTests
void ByteRegisterTests(void)
Definition: util-byte.c:1026
StringParseInt64
int StringParseInt64(int64_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:582
ByteExtractStringInt8
int WARN_UNUSED ByteExtractStringInt8(int8_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:557
StringParseUint64
int StringParseUint64(uint64_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:264
StringParseU8RangeCheck
int WARN_UNUSED StringParseU8RangeCheck(uint8_t *res, int base, size_t len, const char *str, uint8_t min, uint8_t max)
Definition: util-byte.c:427