suricata
util-byte.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 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 __UTIL_BYTE_H__
25 #define __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 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 unint32_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 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 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  *
132  * \return n Number of bytes extracted on success
133  * \return -1 On error
134  */
135 int ByteExtractString(uint64_t *res, int base, uint16_t len, const char *str);
136 
137 /**
138  * Extract unsigned integer value from a string as uint64_t.
139  *
140  * \param res Stores result
141  * \param base Base of the number to extract
142  * \param len Number of bytes to extract (23 max or 0 for unbounded)
143  * \param len Number of bytes to extract (23 max)
144  * \param str String to extract from
145  *
146  * \return n Number of bytes extracted on success
147  * \return -1 On error
148  */
149 int ByteExtractStringUint64(uint64_t *res, int base, uint16_t len, const char *str);
150 
151 /**
152  * Extract unsigned integer value from a string as uint32_t.
153  *
154  * \param res Stores result
155  * \param base Base of the number to extract
156  * \param len Number of bytes to extract (23 max or 0 for unbounded)
157  * \param str String to extract from
158  *
159  * \return n Number of bytes extracted on success
160  * \return -1 On error
161  */
162 int ByteExtractStringUint32(uint32_t *res, int base, uint16_t len, const char *str);
163 
164 /**
165  * Extract unsigned integer value from a string as uint16_t.
166  *
167  * \param res Stores result
168  * \param base Base of the number to extract
169  * \param len Number of bytes to extract (23 max or 0 for unbounded)
170  * \param str String to extract from
171  *
172  * \return n Number of bytes extracted on success
173  * \return -1 On error
174  */
175 int ByteExtractStringUint16(uint16_t *res, int base, uint16_t len, const char *str);
176 
177 /**
178  * Extract unsigned integer value from a string as uint8_t.
179  *
180  * \param res Stores result
181  * \param base Base of the number to extract
182  * \param len Number of bytes to extract (23 max or 0 for unbounded)
183  * \param str String to extract from
184  *
185  * \return n Number of bytes extracted on success
186  * \return -1 On error
187  */
188 int ByteExtractStringUint8(uint8_t *res, int base, uint16_t len, const char *str);
189 
190 /**
191  * Extract signed integer value from a string.
192  *
193  * \param res Stores result
194  * \param base Base of the number to extract
195  * \param len Number of bytes to extract (23 max or 0 for unbounded)
196  * \param str String to extract from
197  *
198  * \return n Number of bytes extracted on success
199  * \return -1 On error
200  */
201 int ByteExtractStringSigned(int64_t *res, int base, uint16_t len, const char *str);
202 
203 /**
204  * Extract signed integer value from a string as uint64_t.
205  *
206  * \param res Stores result
207  * \param base Base of the number to extract
208  * \param len Number of bytes to extract (23 max or 0 for unbounded)
209  * \param str String to extract from
210  *
211  * \return n Number of bytes extracted on success
212  * \return -1 On error
213  */
214 int ByteExtractStringInt64(int64_t *res, int base, uint16_t len, const char *str);
215 
216 /**
217  * Extract signed integer value from a string as uint32_t.
218  *
219  * \param res Stores result
220  * \param base Base of the number to extract
221  * \param len Number of bytes to extract (23 max or 0 for unbounded)
222  * \param str String to extract from
223  *
224  * \return n Number of bytes extracted on success
225  * \return -1 On error
226  */
227 int ByteExtractStringInt32(int32_t *res, int base, uint16_t len, const char *str);
228 
229 /**
230  * Extract signed integer value from a string as uint16_t.
231  *
232  * \param res Stores result
233  * \param base Base of the number to extract
234  * \param len Number of bytes to extract (23 max or 0 for unbounded)
235  * \param str String to extract from
236  *
237  * \return n Number of bytes extracted on success
238  * \return -1 On error
239  */
240 int ByteExtractStringInt16(int16_t *res, int base, uint16_t len, const char *str);
241 
242 /**
243  * Extract signed integer value from a string as uint8_t.
244  *
245  * \param res Stores result
246  * \param base Base of the number to extract
247  * \param len Number of bytes to extract (23 max or 0 for unbounded)
248  * \param str String to extract from
249  *
250  * \return n Number of bytes extracted on success
251  * \return -1 On error
252  */
253 int ByteExtractStringInt8(int8_t *res, int base, uint16_t len, const char *str);
254 
255 #ifdef UNITTESTS
256 void ByteRegisterTests(void);
257 #endif /* UNITTESTS */
258 
259 /** ------ Inline functions ----- */
260 static inline int ByteExtract(uint64_t *res, int e, uint16_t len, const uint8_t *bytes)
261 {
262  uint64_t b = 0;
263  int i;
264 
265  if ((e != BYTE_BIG_ENDIAN) && (e != BYTE_LITTLE_ENDIAN)) {
266  /** \todo Need standard return values */
267  return -1;
268  }
269 
270  *res = 0;
271 
272  /* Go through each byte and merge it into the result in the correct order */
273  /** \todo Probably a more efficient way to do this. */
274  for (i = 0; i < len; i++) {
275 
276  if (e == BYTE_LITTLE_ENDIAN) {
277  b = bytes[i];
278  }
279  else {
280  b = bytes[len - i - 1];
281  }
282 
283  *res |= (b << ((i & 7) << 3));
284 
285  }
286 
287  return len;
288 }
289 
290 
291 #endif /* __UTIL_BYTE_H__ */
292 
int ByteExtractStringInt32(int32_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:315
int ByteExtractUint64(uint64_t *res, int e, uint16_t len, const uint8_t *bytes)
Definition: util-byte.c:74
int ByteExtractStringInt64(int64_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:310
int ByteExtractStringUint64(uint64_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:191
int ByteExtractStringInt16(int16_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:336
int ByteExtractStringInt8(int8_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:357
#define str(s)
char * BytesToString(const uint8_t *bytes, size_t nbytes)
Turn byte array into string.
Definition: util-byte.c:40
int ByteExtractUint16(uint16_t *res, int e, uint16_t len, const uint8_t *bytes)
Definition: util-byte.c:116
int ByteExtractUint32(uint32_t *res, int e, uint16_t len, const uint8_t *bytes)
Definition: util-byte.c:95
#define BYTE_BIG_ENDIAN
Definition: util-byte.h:29
#define BYTE_LITTLE_ENDIAN
Definition: util-byte.h:30
PoolThreadReserved res
int ByteExtractStringUint32(uint32_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:196
int ByteExtractString(uint64_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:137
void ByteRegisterTests(void)
Definition: util-byte.c:606
int ByteExtractStringUint16(uint16_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:217
uint8_t len
int ByteExtractStringUint8(uint8_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:238
int ByteExtractStringSigned(int64_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:259