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 #if defined(OS_FREEBSD) || defined(__DragonFly__)
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 uint32_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  * \param bool Enable strict check for parsers
133  *
134  * \return n Number of bytes extracted on success
135  * \return -1 On error
136  */
137 int ByteExtractString(uint64_t *res, int base, uint16_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 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  * \param bool Enable strict check for parsers
199  *
200  * \return n Number of bytes extracted on success
201  * \return -1 On error
202  */
203 int ByteExtractStringSigned(int64_t *res, int base, uint16_t len, const char *str, bool strict);
204 
205 /**
206  * Extract signed integer value from a string as uint64_t.
207  *
208  * \param res Stores result
209  * \param base Base of the number to extract
210  * \param len Number of bytes to extract (23 max or 0 for unbounded)
211  * \param str String to extract from
212  *
213  * \return n Number of bytes extracted on success
214  * \return -1 On error
215  */
216 int ByteExtractStringInt64(int64_t *res, int base, uint16_t len, const char *str);
217 
218 /**
219  * Extract signed integer value from a string as uint32_t.
220  *
221  * \param res Stores result
222  * \param base Base of the number to extract
223  * \param len Number of bytes to extract (23 max or 0 for unbounded)
224  * \param str String to extract from
225  *
226  * \return n Number of bytes extracted on success
227  * \return -1 On error
228  */
229 int ByteExtractStringInt32(int32_t *res, int base, uint16_t len, const char *str);
230 
231 /**
232  * Extract signed integer value from a string as uint16_t.
233  *
234  * \param res Stores result
235  * \param base Base of the number to extract
236  * \param len Number of bytes to extract (23 max or 0 for unbounded)
237  * \param str String to extract from
238  *
239  * \return n Number of bytes extracted on success
240  * \return -1 On error
241  */
242 int ByteExtractStringInt16(int16_t *res, int base, uint16_t len, const char *str);
243 
244 /**
245  * Extract signed integer value from a string as uint8_t.
246  *
247  * \param res Stores result
248  * \param base Base of the number to extract
249  * \param len Number of bytes to extract (23 max or 0 for unbounded)
250  * \param str String to extract from
251  *
252  * \return n Number of bytes extracted on success
253  * \return -1 On error
254  */
255 int ByteExtractStringInt8(int8_t *res, int base, uint16_t len, const char *str);
256 
257 /**
258  * Extract unsigned integer value from a string as uint64_t strictly.
259  *
260  * \param res Stores result
261  * \param base Base of the number to extract
262  * \param len Number of bytes to extract (23 max or 0 for unbounded)
263  * \param str String to extract from
264  *
265  * \return n Number of bytes extracted on success
266  * \return -1 On error
267  */
268 int StringParseUint64(uint64_t *res, int base, uint16_t len, const char *str);
269 
270 /**
271  * Extract unsigned integer value from a string as uint32_t strictly.
272  *
273  * \param res Stores result
274  * \param base Base of the number to extract
275  * \param len Number of bytes to extract (23 max or 0 for unbounded)
276  * \param str String to extract from
277  *
278  * \return n Number of bytes extracted on success
279  * \return -1 On error
280  */
281 int StringParseUint32(uint32_t *res, int base, uint16_t len, const char *str);
282 
283 /**
284  * Extract unsigned integer value from a string as uint16_t strictly.
285  *
286  * \param res Stores result
287  * \param base Base of the number to extract
288  * \param len Number of bytes to extract (23 max or 0 for unbounded)
289  * \param str String to extract from
290  *
291  * \return n Number of bytes extracted on success
292  * \return -1 On error
293  */
294 int StringParseUint16(uint16_t *res, int base, uint16_t len, const char *str);
295 
296 /**
297  * Extract unsigned integer value from a string as uint8_t strictly.
298  *
299  * \param res Stores result
300  * \param base Base of the number to extract
301  * \param len Number of bytes to extract (23 max or 0 for unbounded)
302  * \param str String to extract from
303  *
304  * \return n Number of bytes extracted on success
305  * \return -1 On error
306  */
307 int StringParseUint8(uint8_t *res, int base, uint16_t len, const char *str);
308 
309 /**
310  * Extract signed integer value from a string as int64_t strictly.
311  *
312  * \param res Stores result
313  * \param base Base of the number to extract
314  * \param len Number of bytes to extract (23 max or 0 for unbounded)
315  * \param str String to extract from
316  *
317  * \return n Number of bytes extracted on success
318  * \return -1 On error
319  */
320 int StringParseInt64(int64_t *res, int base, uint16_t len, const char *str);
321 
322 /**
323  * Extract signed integer value from a string as int32_t strictly.
324  *
325  * \param res Stores result
326  * \param base Base of the number to extract
327  * \param len Number of bytes to extract (23 max or 0 for unbounded)
328  * \param str String to extract from
329  *
330  * \return n Number of bytes extracted on success
331  * \return -1 On error
332  */
333 int StringParseInt32(int32_t *res, int base, uint16_t len, const char *str);
334 
335 /**
336  * Extract signed integer value from a string as int16_t strictly.
337  *
338  * \param res Stores result
339  * \param base Base of the number to extract
340  * \param len Number of bytes to extract (23 max or 0 for unbounded)
341  * \param str String to extract from
342  *
343  * \return n Number of bytes extracted on success
344  * \return -1 On error
345  */
346 int StringParseInt16(int16_t *res, int base, uint16_t len, const char *str);
347 
348 /**
349  * Extract signed integer value from a string as int8_t strictly.
350  *
351  * \param res Stores result
352  * \param base Base of the number to extract
353  * \param len Number of bytes to extract (23 max or 0 for unbounded)
354  * \param str String to extract from
355  *
356  * \return n Number of bytes extracted on success
357  * \return -1 On error
358  */
359 int StringParseInt8(int8_t *res, int base, uint16_t len, const char *str);
360 
361 /**
362  * Extract unsigned integer value from a string as uint64_t strictly within the range.
363  *
364  * \param res Stores result
365  * \param base Base of the number to extract
366  * \param len Number of bytes to extract (23 max or 0 for unbounded)
367  * \param str String to extract from
368  *
369  * \return n Number of bytes extracted on success
370  * \return -1 On error
371  */
372 int WARN_UNUSED StringParseU64RangeCheck(uint64_t *res, int base, uint16_t len, const char *str, uint64_t min, uint64_t max);
373 
374 /**
375  * Extract unsigned integer value from a string as uint32_t strictly within the range.
376  *
377  * \param res Stores result
378  * \param base Base of the number to extract
379  * \param len Number of bytes to extract (23 max or 0 for unbounded)
380  * \param str String to extract from
381  *
382  * \return n Number of bytes extracted on success
383  * \return -1 On error
384  */
385 int WARN_UNUSED StringParseU32RangeCheck(uint32_t *res, int base, uint16_t len, const char *str, uint32_t min, uint32_t max);
386 
387 /**
388  * Extract unsigned integer value from a string as uint16_t strictly within the range.
389  *
390  * \param res Stores result
391  * \param base Base of the number to extract
392  * \param len Number of bytes to extract (23 max or 0 for unbounded)
393  * \param str String to extract from
394  *
395  * \return n Number of bytes extracted on success
396  * \return -1 On error
397  */
398 int WARN_UNUSED StringParseU16RangeCheck(uint16_t *res, int base, uint16_t len, const char *str, uint16_t min, uint16_t max);
399 
400 /**
401  * Extract unsigned integer value from a string as uint8_t strictly within the range.
402  *
403  * \param res Stores result
404  * \param base Base of the number to extract
405  * \param len Number of bytes to extract (23 max or 0 for unbounded)
406  * \param str String to extract from
407  *
408  * \return n Number of bytes extracted on success
409  * \return -1 On error
410  */
411 int WARN_UNUSED StringParseU8RangeCheck(uint8_t *res, int base, uint16_t len, const char *str, uint8_t min, uint8_t max);
412 
413 /**
414  * Extract signed integer value from a string as int64_t strictly within the range.
415  *
416  * \param res Stores result
417  * \param base Base of the number to extract
418  * \param len Number of bytes to extract (23 max or 0 for unbounded)
419  * \param str String to extract from
420  *
421  * \return n Number of bytes extracted on success
422  * \return -1 On error
423  */
424 int WARN_UNUSED StringParseI64RangeCheck(int64_t *res, int base, uint16_t len, const char *str, int64_t min, int64_t max);
425 
426 /**
427  * Extract signed integer value from a string as int32_t strictly within the range.
428  *
429  * \param res Stores result
430  * \param base Base of the number to extract
431  * \param len Number of bytes to extract (23 max or 0 for unbounded)
432  * \param str String to extract from
433  *
434  * \return n Number of bytes extracted on success
435  * \return -1 On error
436  */
437 int WARN_UNUSED StringParseI32RangeCheck(int32_t *res, int base, uint16_t len, const char *str, int32_t min, int32_t max);
438 
439 /**
440  * Extract signed integer value from a string as int16_t strictly within the range.
441  *
442  * \param res Stores result
443  * \param base Base of the number to extract
444  * \param len Number of bytes to extract (23 max or 0 for unbounded)
445  * \param str String to extract from
446  *
447  * \return n Number of bytes extracted on success
448  * \return -1 On error
449  */
450 int WARN_UNUSED StringParseI16RangeCheck(int16_t *res, int base, uint16_t len, const char *str, int16_t min, int16_t max);
451 
452 /**
453  * Extract signed integer value from a string as int8_t strictly within the range.
454  *
455  * \param res Stores result
456  * \param base Base of the number to extract
457  * \param len Number of bytes to extract (23 max or 0 for unbounded)
458  * \param str String to extract from
459  *
460  * \return n Number of bytes extracted on success
461  * \return -1 On error
462  */
463 int WARN_UNUSED StringParseI8RangeCheck(int8_t *res, int base, uint16_t len, const char *str, int8_t min, int8_t max);
464 
465 #ifdef UNITTESTS
466 void ByteRegisterTests(void);
467 #endif /* UNITTESTS */
468 
469 /** ------ Inline functions ----- */
ByteExtract(uint64_t * res,int e,uint16_t len,const uint8_t * bytes)470 static inline int ByteExtract(uint64_t *res, int e, uint16_t len, const uint8_t *bytes)
471 {
472     uint64_t b = 0;
473     int i;
474 
475     if ((e != BYTE_BIG_ENDIAN) && (e != BYTE_LITTLE_ENDIAN)) {
476         /** \todo Need standard return values */
477         return -1;
478     }
479 
480     *res = 0;
481 
482     /* Go through each byte and merge it into the result in the correct order */
483     /** \todo Probably a more efficient way to do this. */
484     for (i = 0; i < len; i++) {
485 
486         if (e == BYTE_LITTLE_ENDIAN) {
487             b = bytes[i];
488         }
489         else {
490             b = bytes[len - i - 1];
491         }
492 
493         *res |= (b << ((i & 7) << 3));
494 
495     }
496 
497     return len;
498 }
499 
500 
501 #endif /* __UTIL_BYTE_H__ */
502 
503