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 void BytesToStringBuffer(const uint8_t *bytes, size_t nbytes, char *outstr, size_t outlen);
85 
86 /**
87  * Extract bytes from a byte string and convert to a unint64_t.
88  *
89  * \param res Stores result
90  * \param e Endianness (BYTE_BIG_ENDIAN or BYTE_LITTLE_ENDIAN)
91  * \param len Number of bytes to extract (8 max)
92  * \param bytes Data to extract from
93  *
94  * \return n Number of bytes extracted on success
95  * \return -1 On error
96  */
97 int ByteExtractUint64(uint64_t *res, int e, uint16_t len, const uint8_t *bytes);
98 
99 /**
100  * Extract bytes from a byte string and convert to a unint32_t.
101  *
102  * \param res Stores result
103  * \param e Endianness (BYTE_BIG_ENDIAN or BYTE_LITTLE_ENDIAN)
104  * \param len Number of bytes to extract (8 max)
105  * \param bytes Data to extract from
106  *
107  * \return n Number of bytes extracted on success
108  * \return -1 On error
109  */
110 int ByteExtractUint32(uint32_t *res, int e, uint16_t len, const uint8_t *bytes);
111 
112 /**
113  * Extract bytes from a byte string and convert to a unint16_t.
114  *
115  * \param res Stores result
116  * \param e Endianness (BYTE_BIG_ENDIAN or BYTE_LITTLE_ENDIAN)
117  * \param len Number of bytes to extract (8 max)
118  * \param bytes Data to extract from
119  *
120  * \return n Number of bytes extracted on success
121  * \return -1 On error
122  */
123 int ByteExtractUint16(uint16_t *res, int e, uint16_t len, const uint8_t *bytes);
124 
125 /**
126  * Extract unsigned integer value from a string.
127  *
128  * \param res Stores result
129  * \param base Base of the number to extract
130  * \param len Number of bytes to extract (23 max or 0 for unbounded)
131  * \param str String to extract from
132  *
133  * \return n Number of bytes extracted on success
134  * \return -1 On error
135  */
136 int ByteExtractString(uint64_t *res, int base, uint16_t len, const char *str);
137 
138 /**
139  * Extract unsigned integer value from a string as uint64_t.
140  *
141  * \param res Stores result
142  * \param base Base of the number to extract
143  * \param len Number of bytes to extract (23 max or 0 for unbounded)
144  * \param len Number of bytes to extract (23 max)
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 ByteExtractStringUint64(uint64_t *res, int base, uint16_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 ByteExtractStringUint32(uint32_t *res, int base, uint16_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 ByteExtractStringUint16(uint16_t *res, int base, uint16_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 ByteExtractStringUint8(uint8_t *res, int base, uint16_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  *
199  * \return n Number of bytes extracted on success
200  * \return -1 On error
201  */
202 int ByteExtractStringSigned(int64_t *res, int base, uint16_t len, const char *str);
203 
204 /**
205  * Extract signed integer value from a string as uint64_t.
206  *
207  * \param res Stores result
208  * \param base Base of the number to extract
209  * \param len Number of bytes to extract (23 max or 0 for unbounded)
210  * \param str String to extract from
211  *
212  * \return n Number of bytes extracted on success
213  * \return -1 On error
214  */
215 int ByteExtractStringInt64(int64_t *res, int base, uint16_t len, const char *str);
216 
217 /**
218  * Extract signed integer value from a string as uint32_t.
219  *
220  * \param res Stores result
221  * \param base Base of the number to extract
222  * \param len Number of bytes to extract (23 max or 0 for unbounded)
223  * \param str String to extract from
224  *
225  * \return n Number of bytes extracted on success
226  * \return -1 On error
227  */
228 int ByteExtractStringInt32(int32_t *res, int base, uint16_t len, const char *str);
229 
230 /**
231  * Extract signed integer value from a string as uint16_t.
232  *
233  * \param res Stores result
234  * \param base Base of the number to extract
235  * \param len Number of bytes to extract (23 max or 0 for unbounded)
236  * \param str String to extract from
237  *
238  * \return n Number of bytes extracted on success
239  * \return -1 On error
240  */
241 int ByteExtractStringInt16(int16_t *res, int base, uint16_t len, const char *str);
242 
243 /**
244  * Extract signed integer value from a string as uint8_t.
245  *
246  * \param res Stores result
247  * \param base Base of the number to extract
248  * \param len Number of bytes to extract (23 max or 0 for unbounded)
249  * \param str String to extract from
250  *
251  * \return n Number of bytes extracted on success
252  * \return -1 On error
253  */
254 int ByteExtractStringInt8(int8_t *res, int base, uint16_t len, const char *str);
255 
256 #ifdef UNITTESTS
257 void ByteRegisterTests(void);
258 #endif /* UNITTESTS */
259 
260 /** ------ Inline functions ----- */
261 static inline int ByteExtract(uint64_t *res, int e, uint16_t len, const uint8_t *bytes)
262 {
263  uint64_t b = 0;
264  int i;
265 
266  if ((e != BYTE_BIG_ENDIAN) && (e != BYTE_LITTLE_ENDIAN)) {
267  /** \todo Need standard return values */
268  return -1;
269  }
270 
271  *res = 0;
272 
273  /* Go through each byte and merge it into the result in the correct order */
274  /** \todo Probably a more efficient way to do this. */
275  for (i = 0; i < len; i++) {
276 
277  if (e == BYTE_LITTLE_ENDIAN) {
278  b = bytes[i];
279  }
280  else {
281  b = bytes[len - i - 1];
282  }
283 
284  *res |= (b << ((i & 7) << 3));
285 
286  }
287 
288  return len;
289 }
290 
291 
292 #endif /* __UTIL_BYTE_H__ */
293 
int ByteExtractStringInt32(int32_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:360
int ByteExtractUint64(uint64_t *res, int e, uint16_t len, const uint8_t *bytes)
Definition: util-byte.c:122
int ByteExtractStringInt64(int64_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:355
int ByteExtractStringUint64(uint64_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:239
int ByteExtractStringInt16(int16_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:381
int ByteExtractStringInt8(int8_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:402
#define str(s)
char * BytesToString(const uint8_t *bytes, size_t nbytes)
Turn byte array into string.
Definition: util-byte.c:41
int ByteExtractUint16(uint16_t *res, int e, uint16_t len, const uint8_t *bytes)
Definition: util-byte.c:164
int ByteExtractUint32(uint32_t *res, int e, uint16_t len, const uint8_t *bytes)
Definition: util-byte.c:143
#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:244
int ByteExtractString(uint64_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:185
void ByteRegisterTests(void)
Definition: util-byte.c:651
int ByteExtractStringUint16(uint16_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:264
uint8_t len
int ByteExtractStringUint8(uint8_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:284
int ByteExtractStringSigned(int64_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:304
void BytesToStringBuffer(const uint8_t *bytes, size_t nbytes, char *outstr, size_t outlen)
Turn byte array into string.
Definition: util-byte.c:85