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(__WIN32) || defined(_WIN32) || defined(sun)
49 /* Quick & dirty solution, nothing seems to exist for this in Win32 API */
50 #define SCByteSwap16(x) \
51  ((((x) & 0xff00) >> 8) \
52  | (((x) & 0x00ff) << 8))
53 #define SCByteSwap32(x) \
54  ((((x) & 0xff000000) >> 24) \
55  | (((x) & 0x00ff0000) >> 8) \
56  | (((x) & 0x0000ff00) << 8) \
57  | (((x) & 0x000000ff) << 24))
58 #define SCByteSwap64(x) \
59  ((((x) & 0xff00000000000000ull) >> 56) \
60  | (((x) & 0x00ff000000000000ull) >> 40) \
61  | (((x) & 0x0000ff0000000000ull) >> 24) \
62  | (((x) & 0x000000ff00000000ull) >> 8) \
63  | (((x) & 0x00000000ff000000ull) << 8) \
64  | (((x) & 0x0000000000ff0000ull) << 24) \
65  | (((x) & 0x000000000000ff00ull) << 40) \
66  | (((x) & 0x00000000000000ffull) << 56))
67 #else
68 #include <byteswap.h>
69 #define SCByteSwap16(x) bswap_16(x)
70 #define SCByteSwap32(x) bswap_32(x)
71 #define SCByteSwap64(x) bswap_64(x)
72 #endif /* OS_FREEBSD */
73 
74 /** \brief Turn byte array into string.
75  *
76  * All non-printables are copied over, except for '\0', which is
77  * turned into literal \0 in the string.
78  *
79  * \param bytes byte array
80  * \param nbytes number of bytes
81  * \return string nul-terminated string or NULL on error
82  */
83 char *BytesToString(const uint8_t *bytes, size_t nbytes);
84 
85 /**
86  * Extract bytes from a byte string and convert to a unint64_t.
87  *
88  * \param res Stores result
89  * \param e Endianness (BYTE_BIG_ENDIAN or BYTE_LITTLE_ENDIAN)
90  * \param len Number of bytes to extract (8 max)
91  * \param bytes Data to extract from
92  *
93  * \return n Number of bytes extracted on success
94  * \return -1 On error
95  */
96 int WARN_UNUSED ByteExtractUint64(uint64_t *res, int e, uint16_t len, const uint8_t *bytes);
97 
98 /**
99  * Extract bytes from a byte string and convert to a uint32_t.
100  *
101  * \param res Stores result
102  * \param e Endianness (BYTE_BIG_ENDIAN or BYTE_LITTLE_ENDIAN)
103  * \param len Number of bytes to extract (8 max)
104  * \param bytes Data to extract from
105  *
106  * \return n Number of bytes extracted on success
107  * \return -1 On error
108  */
109 int WARN_UNUSED ByteExtractUint32(uint32_t *res, int e, uint16_t len, const uint8_t *bytes);
110 
111 /**
112  * Extract bytes from a byte string and convert to a unint16_t.
113  *
114  * \param res Stores result
115  * \param e Endianness (BYTE_BIG_ENDIAN or BYTE_LITTLE_ENDIAN)
116  * \param len Number of bytes to extract (8 max)
117  * \param bytes Data to extract from
118  *
119  * \return n Number of bytes extracted on success
120  * \return -1 On error
121  */
122 int WARN_UNUSED ByteExtractUint16(uint16_t *res, int e, uint16_t len, const uint8_t *bytes);
123 
124 /**
125  * Extract unsigned integer value from a string.
126  *
127  * \param res Stores result
128  * \param base Base of the number to extract
129  * \param len Number of bytes to extract (23 max or 0 for unbounded)
130  * \param str String to extract from
131  * \param bool Enable strict check for parsers
132  *
133  * \return n Number of bytes extracted on success
134  * \return -1 On error
135  */
137  uint64_t *res, int base, size_t len, const char *str, bool strict);
138 
139 /**
140  * Extract unsigned integer value from a string as uint64_t.
141  *
142  * \param res Stores result
143  * \param base Base of the number to extract
144  * \param len Number of bytes to extract (23 max or 0 for unbounded)
145  * \param str String to extract from
146  *
147  * \return n Number of bytes extracted on success
148  * \return -1 On error
149  */
150 int WARN_UNUSED ByteExtractStringUint64(uint64_t *res, int base, size_t len, const char *str);
151 
152 /**
153  * Extract unsigned integer value from a string as uint32_t.
154  *
155  * \param res Stores result
156  * \param base Base of the number to extract
157  * \param len Number of bytes to extract (23 max or 0 for unbounded)
158  * \param str String to extract from
159  *
160  * \return n Number of bytes extracted on success
161  * \return -1 On error
162  */
163 int WARN_UNUSED ByteExtractStringUint32(uint32_t *res, int base, size_t len, const char *str);
164 
165 /**
166  * Extract unsigned integer value from a string as uint16_t.
167  *
168  * \param res Stores result
169  * \param base Base of the number to extract
170  * \param len Number of bytes to extract (23 max or 0 for unbounded)
171  * \param str String to extract from
172  *
173  * \return n Number of bytes extracted on success
174  * \return -1 On error
175  */
176 int WARN_UNUSED ByteExtractStringUint16(uint16_t *res, int base, size_t len, const char *str);
177 
178 /**
179  * Extract unsigned integer value from a string as uint8_t.
180  *
181  * \param res Stores result
182  * \param base Base of the number to extract
183  * \param len Number of bytes to extract (23 max or 0 for unbounded)
184  * \param str String to extract from
185  *
186  * \return n Number of bytes extracted on success
187  * \return -1 On error
188  */
189 int WARN_UNUSED ByteExtractStringUint8(uint8_t *res, int base, size_t len, const char *str);
190 
191 /**
192  * Extract signed integer value from a string.
193  *
194  * \param res Stores result
195  * \param base Base of the number to extract
196  * \param len Number of bytes to extract (23 max or 0 for unbounded)
197  * \param str String to extract from
198  * \param bool Enable strict check for parsers
199  *
200  * \return n Number of bytes extracted on success
201  * \return -1 On error
202  */
204  int64_t *res, int base, size_t len, const char *str, bool strict);
205 
206 /**
207  * Extract signed integer value from a string as uint64_t.
208  *
209  * \param res Stores result
210  * \param base Base of the number to extract
211  * \param len Number of bytes to extract (23 max or 0 for unbounded)
212  * \param str String to extract from
213  *
214  * \return n Number of bytes extracted on success
215  * \return -1 On error
216  */
217 int WARN_UNUSED ByteExtractStringInt64(int64_t *res, int base, size_t len, const char *str);
218 
219 /**
220  * Extract signed integer value from a string as uint32_t.
221  *
222  * \param res Stores result
223  * \param base Base of the number to extract
224  * \param len Number of bytes to extract (23 max or 0 for unbounded)
225  * \param str String to extract from
226  *
227  * \return n Number of bytes extracted on success
228  * \return -1 On error
229  */
230 int WARN_UNUSED ByteExtractStringInt32(int32_t *res, int base, size_t len, const char *str);
231 
232 /**
233  * Extract signed integer value from a string as uint16_t.
234  *
235  * \param res Stores result
236  * \param base Base of the number to extract
237  * \param len Number of bytes to extract (23 max or 0 for unbounded)
238  * \param str String to extract from
239  *
240  * \return n Number of bytes extracted on success
241  * \return -1 On error
242  */
243 int WARN_UNUSED ByteExtractStringInt16(int16_t *res, int base, size_t len, const char *str);
244 
245 /**
246  * Extract signed integer value from a string as uint8_t.
247  *
248  * \param res Stores result
249  * \param base Base of the number to extract
250  * \param len Number of bytes to extract (23 max or 0 for unbounded)
251  * \param str String to extract from
252  *
253  * \return n Number of bytes extracted on success
254  * \return -1 On error
255  */
256 int WARN_UNUSED ByteExtractStringInt8(int8_t *res, int base, size_t len, const char *str);
257 
258 /**
259  * Extract unsigned integer value from a string as uint64_t strictly.
260  *
261  * \param res Stores result
262  * \param base Base of the number to extract
263  * \param len Number of bytes to extract (23 max or 0 for unbounded)
264  * \param str String to extract from
265  *
266  * \return n Number of bytes extracted on success
267  * \return -1 On error
268  */
269 int StringParseUint64(uint64_t *res, int base, size_t len, const char *str);
270 
271 /**
272  * Extract unsigned integer value from a string as uint32_t strictly.
273  *
274  * \param res Stores result
275  * \param base Base of the number to extract
276  * \param len Number of bytes to extract (23 max or 0 for unbounded)
277  * \param str String to extract from
278  *
279  * \return n Number of bytes extracted on success
280  * \return -1 On error
281  */
282 int StringParseUint32(uint32_t *res, int base, size_t len, const char *str);
283 
284 /**
285  * Extract unsigned integer value from a string as uint16_t strictly.
286  *
287  * \param res Stores result
288  * \param base Base of the number to extract
289  * \param len Number of bytes to extract (23 max or 0 for unbounded)
290  * \param str String to extract from
291  *
292  * \return n Number of bytes extracted on success
293  * \return -1 On error
294  */
295 int StringParseUint16(uint16_t *res, int base, size_t len, const char *str);
296 
297 /**
298  * Extract unsigned integer value from a string as uint8_t strictly.
299  *
300  * \param res Stores result
301  * \param base Base of the number to extract
302  * \param len Number of bytes to extract (23 max or 0 for unbounded)
303  * \param str String to extract from
304  *
305  * \return n Number of bytes extracted on success
306  * \return -1 On error
307  */
308 int StringParseUint8(uint8_t *res, int base, size_t len, const char *str);
309 
310 /**
311  * Extract signed integer value from a string as int64_t strictly.
312  *
313  * \param res Stores result
314  * \param base Base of the number to extract
315  * \param len Number of bytes to extract (23 max or 0 for unbounded)
316  * \param str String to extract from
317  *
318  * \return n Number of bytes extracted on success
319  * \return -1 On error
320  */
321 int StringParseInt64(int64_t *res, int base, size_t len, const char *str);
322 
323 /**
324  * Extract signed integer value from a string as int32_t strictly.
325  *
326  * \param res Stores result
327  * \param base Base of the number to extract
328  * \param len Number of bytes to extract (23 max or 0 for unbounded)
329  * \param str String to extract from
330  *
331  * \return n Number of bytes extracted on success
332  * \return -1 On error
333  */
334 int StringParseInt32(int32_t *res, int base, size_t len, const char *str);
335 
336 /**
337  * Extract signed integer value from a string as int16_t strictly.
338  *
339  * \param res Stores result
340  * \param base Base of the number to extract
341  * \param len Number of bytes to extract (23 max or 0 for unbounded)
342  * \param str String to extract from
343  *
344  * \return n Number of bytes extracted on success
345  * \return -1 On error
346  */
347 int StringParseInt16(int16_t *res, int base, size_t len, const char *str);
348 
349 /**
350  * Extract signed integer value from a string as int8_t strictly.
351  *
352  * \param res Stores result
353  * \param base Base of the number to extract
354  * \param len Number of bytes to extract (23 max or 0 for unbounded)
355  * \param str String to extract from
356  *
357  * \return n Number of bytes extracted on success
358  * \return -1 On error
359  */
360 int StringParseInt8(int8_t *res, int base, size_t len, const char *str);
361 
362 /**
363  * Extract unsigned integer value from a string as uint64_t strictly within the range.
364  *
365  * \param res Stores result
366  * \param base Base of the number to extract
367  * \param len Number of bytes to extract (23 max or 0 for unbounded)
368  * \param str String to extract from
369  *
370  * \return n Number of bytes extracted on success
371  * \return -1 On error
372  */
374  uint64_t *res, int base, size_t len, const char *str, uint64_t min, uint64_t max);
375 
376 /**
377  * Extract unsigned integer value from a string as uint32_t strictly within the range.
378  *
379  * \param res Stores result
380  * \param base Base of the number to extract
381  * \param len Number of bytes to extract (23 max or 0 for unbounded)
382  * \param str String to extract from
383  *
384  * \return n Number of bytes extracted on success
385  * \return -1 On error
386  */
388  uint32_t *res, int base, size_t len, const char *str, uint32_t min, uint32_t max);
389 
390 /**
391  * Extract unsigned integer value from a string as uint16_t strictly within the range.
392  *
393  * \param res Stores result
394  * \param base Base of the number to extract
395  * \param len Number of bytes to extract (23 max or 0 for unbounded)
396  * \param str String to extract from
397  *
398  * \return n Number of bytes extracted on success
399  * \return -1 On error
400  */
402  uint16_t *res, int base, size_t len, const char *str, uint16_t min, uint16_t max);
403 
404 /**
405  * Extract unsigned integer value from a string as uint8_t strictly within the range.
406  *
407  * \param res Stores result
408  * \param base Base of the number to extract
409  * \param len Number of bytes to extract (23 max or 0 for unbounded)
410  * \param str String to extract from
411  *
412  * \return n Number of bytes extracted on success
413  * \return -1 On error
414  */
416  uint8_t *res, int base, size_t len, const char *str, uint8_t min, uint8_t max);
417 
418 /**
419  * Extract signed integer value from a string as int64_t strictly within the range.
420  *
421  * \param res Stores result
422  * \param base Base of the number to extract
423  * \param len Number of bytes to extract (23 max or 0 for unbounded)
424  * \param str String to extract from
425  *
426  * \return n Number of bytes extracted on success
427  * \return -1 On error
428  */
430  int64_t *res, int base, size_t len, const char *str, int64_t min, int64_t max);
431 
432 /**
433  * Extract signed integer value from a string as int32_t strictly within the range.
434  *
435  * \param res Stores result
436  * \param base Base of the number to extract
437  * \param len Number of bytes to extract (23 max or 0 for unbounded)
438  * \param str String to extract from
439  *
440  * \return n Number of bytes extracted on success
441  * \return -1 On error
442  */
444  int32_t *res, int base, size_t len, const char *str, int32_t min, int32_t max);
445 
446 /**
447  * Extract signed integer value from a string as int16_t strictly within the range.
448  *
449  * \param res Stores result
450  * \param base Base of the number to extract
451  * \param len Number of bytes to extract (23 max or 0 for unbounded)
452  * \param str String to extract from
453  *
454  * \return n Number of bytes extracted on success
455  * \return -1 On error
456  */
458  int16_t *res, int base, size_t len, const char *str, int16_t min, int16_t max);
459 
460 /**
461  * Extract signed integer value from a string as int8_t strictly within the range.
462  *
463  * \param res Stores result
464  * \param base Base of the number to extract
465  * \param len Number of bytes to extract (23 max or 0 for unbounded)
466  * \param str String to extract from
467  *
468  * \return n Number of bytes extracted on success
469  * \return -1 On error
470  */
472  int8_t *res, int base, size_t len, const char *str, int8_t min, int8_t max);
473 
474 #ifdef UNITTESTS
475 void ByteRegisterTests(void);
476 #endif /* UNITTESTS */
477 
478 /** ------ Inline functions ----- */
479 static inline int WARN_UNUSED ByteExtract(uint64_t *res, int e, uint16_t len, const uint8_t *bytes)
480 {
481  if ((e != BYTE_BIG_ENDIAN) && (e != BYTE_LITTLE_ENDIAN)) {
482  /** \todo Need standard return values */
483  return -1;
484  }
485 
486  *res = 0;
487 
488  /* Go through each byte and merge it into the result in the correct order */
489  /** \todo Probably a more efficient way to do this. */
490  for (int i = 0; i < len; i++) {
491  uint64_t b;
492  if (e == BYTE_LITTLE_ENDIAN) {
493  b = bytes[i];
494  }
495  else {
496  b = bytes[len - i - 1];
497  }
498 
499  *res |= (b << ((i & 7) << 3));
500  }
501 
502  return len;
503 }
504 
505 int HexToRaw(const uint8_t *in, size_t ins, uint8_t *out, size_t outs);
506 
507 #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:308
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:1029
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