suricata
util-byte.c
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  * Byte utility functions
24  */
25 
26 #include "suricata-common.h"
27 #include "util-byte.h"
28 #include "util-unittest.h"
29 #include "util-debug.h"
30 #include "util-validate.h"
31 
32 /** \brief Turn byte array into string.
33  *
34  * All non-printables are copied over, except for '\0', which is
35  * turned into literal \0 in the string.
36  *
37  * \param bytes byte array
38  * \param nbytes number of bytes
39  * \return string nul-terminated string or NULL on error
40  */
41 char *BytesToString(const uint8_t *bytes, size_t nbytes)
42 {
43  size_t n = nbytes + 1;
44  size_t nulls = 0;
45 
46  size_t u;
47  for (u = 0; u < nbytes; u++) {
48  if (bytes[u] == '\0')
49  nulls++;
50  }
51  n += nulls;
52 
53  char *string = SCCalloc(1, n);
54  if (string == NULL)
55  return NULL;
56 
57  if (nulls == 0) {
58  /* no nulls */
59  memcpy(string, bytes, nbytes);
60  } else {
61  /* nulls present */
62  char *dst = string;
63  for (u = 0; u < nbytes; u++) {
64  if (bytes[u] == '\0') {
65  *dst++ = '\\';
66  *dst++ = '0';
67  } else {
68  *dst++ = bytes[u];
69  }
70  }
71  }
72  return string;
73 }
74 
75 /** \brief Turn byte array into string.
76  *
77  * All non-printables are copied over, except for '\0', which is
78  * turned into literal \0 in the string.
79  *
80  * \param bytes byte array
81  * \param nbytes number of bytes
82  * \param outstr[out] buffer to fill
83  * \param outlen size of outstr. Must be at least 2 * nbytes + 1 in size
84  */
85 void BytesToStringBuffer(const uint8_t *bytes, size_t nbytes, char *outstr, size_t outlen)
86 {
87  DEBUG_VALIDATE_BUG_ON(outlen < (nbytes * 2 + 1));
88 
89  size_t n = nbytes + 1;
90  size_t nulls = 0;
91 
92  size_t u;
93  for (u = 0; u < nbytes; u++) {
94  if (bytes[u] == '\0')
95  nulls++;
96  }
97  n += nulls;
98 
99  char string[n];
100 
101  if (nulls == 0) {
102  /* no nulls */
103  memcpy(string, bytes, nbytes);
104  string[nbytes] = '\0';
105  } else {
106  /* nulls present */
107  char *dst = string;
108  for (u = 0; u < nbytes; u++) {
109  if (bytes[u] == '\0') {
110  *dst++ = '\\';
111  *dst++ = '0';
112  } else {
113  *dst++ = bytes[u];
114  }
115  }
116  *dst = '\0';
117  }
118 
119  strlcpy(outstr, string, outlen);
120 }
121 
122 int ByteExtractUint64(uint64_t *res, int e, uint16_t len, const uint8_t *bytes)
123 {
124  uint64_t i64;
125  int ret;
126 
127  /* Uint64 is limited to 8 bytes */
128  if (len > 8) {
129  /** \todo Need standard return values */
130  return -1;
131  }
132 
133  ret = ByteExtract(&i64, e, len, bytes);
134  if (ret <= 0) {
135  return ret;
136  }
137 
138  *res = (uint64_t)i64;
139 
140  return ret;
141 }
142 
143 /**
144  * \retval Greater than 0 if successful, 0 or negative on failure.
145  */
146 int ByteExtractUint32(uint32_t *res, int e, uint16_t len, const uint8_t *bytes)
147 {
148  uint64_t i64;
149  int ret;
150 
151  /* Uint32 is limited to 4 bytes */
152  if (len > 4) {
153  /** \todo Need standard return values */
154  return -1;
155  }
156 
157  ret = ByteExtract(&i64, e, len, bytes);
158  if (ret <= 0) {
159  return ret;
160  }
161 
162  *res = (uint32_t)i64;
163 
164  return ret;
165 }
166 
167 int ByteExtractUint16(uint16_t *res, int e, uint16_t len, const uint8_t *bytes)
168 {
169  uint64_t i64;
170  int ret;
171 
172  /* Uint16 is limited to 2 bytes */
173  if (len > 2) {
174  /** \todo Need standard return values */
175  return -1;
176  }
177 
178  ret = ByteExtract(&i64, e, len, bytes);
179  if (ret <= 0) {
180  return ret;
181  }
182 
183  *res = (uint16_t)i64;
184 
185  return ret;
186 }
187 
188 int ByteExtractString(uint64_t *res, int base, size_t len, const char *str, bool strict)
189 {
190  const char *ptr = str;
191  char *endptr = NULL;
192 
193  /* 23 - This is the largest string (octal, with a zero prefix) that
194  * will not overflow uint64_t. The only way this length
195  * could be over 23 and still not overflow is if it were zero
196  * prefixed and we only support 1 byte of zero prefix for octal.
197  *
198  * "01777777777777777777777" = 0xffffffffffffffff
199  */
200  char strbuf[24];
201 
202  if (len > 23) {
203  SCLogDebug("len too large (23 max)");
204  return -1;
205  }
206 
207  if (len) {
208  /* Extract out the string so it can be null terminated */
209  memcpy(strbuf, str, len);
210  strbuf[len] = '\0';
211  ptr = strbuf;
212  }
213 
214  errno = 0;
215  *res = strtoull(ptr, &endptr, base);
216 
217  if (errno == ERANGE) {
218  SCLogDebug("numeric value out of range");
219  return -1;
220  /* If there is no numeric value in the given string then strtoull(), makes
221  endptr equals to ptr and return 0 as result */
222  } else if (endptr == ptr && *res == 0) {
223  SCLogDebug("no numeric value");
224  return -1;
225  } else if (endptr == ptr) {
226  SCLogDebug("invalid numeric value");
227  return -1;
228  }
229  else if (strict && *endptr != '\0') {
230  SCLogError("Extra characters following numeric value");
231  return -1;
232  }
233 
234  return (int)(endptr - ptr);
235 }
236 
237 int ByteExtractStringUint64(uint64_t *res, int base, size_t len, const char *str)
238 {
239  return ByteExtractString(res, base, len, str, false);
240 }
241 
242 int ByteExtractStringUint32(uint32_t *res, int base, size_t len, const char *str)
243 {
244  uint64_t i64;
245 
246  int ret = ByteExtractString(&i64, base, len, str, false);
247  if (ret <= 0) {
248  return ret;
249  }
250  if (i64 > UINT32_MAX) {
251  return -1;
252  }
253 
254  *res = (uint32_t)i64;
255 
256  if ((uint64_t)(*res) != i64) {
257  SCLogDebug("Numeric value out of range (%" PRIu64 " > %" PRIuMAX ")",
258  i64, (uintmax_t)UINT_MAX);
259  return -1;
260  }
261 
262  return ret;
263 }
264 
265 int ByteExtractStringUint16(uint16_t *res, int base, size_t len, const char *str)
266 {
267  uint64_t i64;
268 
269  int ret = ByteExtractString(&i64, base, len, str, false);
270  if (ret <= 0) {
271  return ret;
272  }
273  if (i64 > UINT16_MAX) {
274  return -1;
275  }
276 
277  *res = (uint16_t)i64;
278 
279  if ((uint64_t)(*res) != i64) {
280  SCLogDebug("Numeric value out of range (%" PRIu64 " > %" PRIuMAX ")",
281  i64, (uintmax_t)USHRT_MAX);
282  return -1;
283  }
284 
285  return ret;
286 }
287 
288 int ByteExtractStringUint8(uint8_t *res, int base, size_t len, const char *str)
289 {
290  uint64_t i64;
291 
292  int ret = ByteExtractString(&i64, base, len, str, false);
293  if (ret <= 0) {
294  return ret;
295  }
296  if (i64 > UINT8_MAX) {
297  return -1;
298  }
299 
300  *res = (uint8_t)i64;
301 
302  if ((uint64_t)(*res) != i64) {
303  SCLogDebug("Numeric value out of range (%" PRIu64 " > %" PRIuMAX ")",
304  i64, (uintmax_t)UCHAR_MAX);
305  return -1;
306  }
307 
308  return ret;
309 }
310 
311 int StringParseUint64(uint64_t *res, int base, size_t len, const char *str)
312 {
313  return ByteExtractString(res, base, len, str, true);
314 }
315 
316 int StringParseUint32(uint32_t *res, int base, size_t len, const char *str)
317 {
318  uint64_t i64;
319 
320  int ret = ByteExtractString(&i64, base, len, str, true);
321  if (ret <= 0) {
322  return ret;
323  }
324  if (i64 > UINT32_MAX) {
325  return -1;
326  }
327 
328  *res = (uint32_t)i64;
329 
330  if ((uint64_t)(*res) != i64) {
331  SCLogError("Numeric value out of range "
332  "(%" PRIu64 " > %" PRIuMAX ")",
333  i64, (uintmax_t)UINT_MAX);
334  return -1;
335  }
336 
337  return ret;
338 }
339 
340 /**
341  * \retval Greater than 0 if successful, 0 or negative on failure.
342  */
343 int StringParseUint16(uint16_t *res, int base, size_t len, const char *str)
344 {
345  uint64_t i64;
346 
347  int ret = ByteExtractString(&i64, base, len, str, true);
348  if (ret <= 0) {
349  return ret;
350  }
351  if (i64 > UINT16_MAX) {
352  return -1;
353  }
354 
355  *res = (uint16_t)i64;
356 
357  if ((uint64_t)(*res) != i64) {
358  SCLogError("Numeric value out of range "
359  "(%" PRIu64 " > %" PRIuMAX ")",
360  i64, (uintmax_t)USHRT_MAX);
361  return -1;
362  }
363 
364  return ret;
365 }
366 
367 /**
368  * \retval Greater than 0 if successful, 0 or negative on failure.
369  */
370 int StringParseUint8(uint8_t *res, int base, size_t len, const char *str)
371 {
372  uint64_t i64;
373 
374  int ret = ByteExtractString(&i64, base, len, str, true);
375  if (ret <= 0) {
376  return ret;
377  }
378  if (i64 > UINT8_MAX) {
379  return -1;
380  }
381 
382  *res = (uint8_t)i64;
383 
384  if ((uint64_t)(*res) != i64) {
385  SCLogError("Numeric value out of range "
386  "(%" PRIu64 " > %" PRIuMAX ")",
387  i64, (uintmax_t)UCHAR_MAX);
388  return -1;
389  }
390 
391  return ret;
392 }
393 
395  uint64_t *res, int base, size_t len, const char *str, uint64_t min, uint64_t max)
396 {
397  uint64_t u64;
398 
399  int ret = ByteExtractString(&u64, base, len, str, true);
400  if (ret <= 0) {
401  return ret;
402  }
403 
404  *res = u64;
405 
406  if (*res < min || *res > max) {
407  return -1;
408  }
409 
410  return ret;
411 }
412 
414  uint32_t *res, int base, size_t len, const char *str, uint32_t min, uint32_t max)
415 {
416  uint64_t u64;
417 
418  int ret = ByteExtractString(&u64, base, len, str, true);
419  if (ret <= 0) {
420  return ret;
421  }
422  if (u64 > UINT32_MAX) {
423  return -1;
424  }
425 
426  *res = (uint32_t)u64;
427 
428  if (*res < min || *res > max) {
429  return -1;
430  }
431 
432  if ((uint64_t)(*res) != u64) {
433  SCLogError("Numeric value out of range "
434  "(%" PRIu64 " > %" PRIuMAX ")",
435  u64, (uintmax_t)UINT_MAX);
436  return -1;
437  }
438 
439  return ret;
440 }
441 
443  uint16_t *res, int base, size_t len, const char *str, uint16_t min, uint16_t max)
444 {
445  uint64_t u64;
446 
447  int ret = ByteExtractString(&u64, base, len, str, true);
448  if (ret <= 0) {
449  return ret;
450  }
451  if (u64 > UINT16_MAX) {
452  return -1;
453  }
454 
455  *res = (uint16_t)u64;
456 
457  if (*res < min || *res > max) {
458  return -1;
459  }
460 
461  if ((uint64_t)(*res) != u64) {
462  SCLogError("Numeric value out of range "
463  "(%" PRIu64 " > %" PRIuMAX ")",
464  u64, (uintmax_t)USHRT_MAX);
465  return -1;
466  }
467 
468  return ret;
469 }
470 
471 /**
472  * \retval Greater than 0 if successful, 0 or negative on failure.
473  */
475  uint8_t *res, int base, size_t len, const char *str, uint8_t min, uint8_t max)
476 {
477  uint64_t u64;
478 
479  int ret = ByteExtractString(&u64, base, len, str, true);
480  if (ret <= 0) {
481  return ret;
482  }
483  if (u64 > UINT8_MAX) {
484  return -1;
485  }
486 
487  *res = (uint8_t)u64;
488 
489  if (*res < min || *res > max) {
490  return -1;
491  }
492 
493  if ((uint64_t)(*res) != u64) {
494  SCLogError("Numeric value out of range "
495  "(%" PRIu64 " > %" PRIuMAX ")",
496  u64, (uintmax_t)UCHAR_MAX);
497  return -1;
498  }
499 
500  return ret;
501 }
502 
503 int ByteExtractStringSigned(int64_t *res, int base, size_t len, const char *str, bool strict)
504 {
505  const char *ptr = str;
506  char *endptr;
507 
508  /* 23 - This is the largest string (octal, with a zero prefix) that
509  * will not overflow int64_t. The only way this length
510  * could be over 23 and still not overflow is if it were zero
511  * prefixed and we only support 1 byte of zero prefix for octal.
512  *
513  * "-0777777777777777777777" = 0xffffffffffffffff
514  */
515  char strbuf[24];
516 
517  if (len > 23) {
518  SCLogError("len too large (23 max)");
519  return -1;
520  }
521 
522  if (len) {
523  /* Extract out the string so it can be null terminated */
524  memcpy(strbuf, str, len);
525  strbuf[len] = '\0';
526  ptr = strbuf;
527  }
528 
529  errno = 0;
530  *res = strtoll(ptr, &endptr, base);
531 
532  if (errno == ERANGE) {
533  SCLogError("Numeric value out of range");
534  return -1;
535  } else if (endptr == str) {
536  SCLogError("Invalid numeric value");
537  return -1;
538  }
539  else if (strict && len && *endptr != '\0') {
540  SCLogError("Extra characters following numeric value");
541  return -1;
542  }
543 
544  //fprintf(stderr, "ByteExtractStringSigned: Extracted base %d: 0x%" PRIx64 "\n", base, *res);
545 
546  return (int)(endptr - ptr);
547 }
548 
549 int ByteExtractStringInt64(int64_t *res, int base, size_t len, const char *str)
550 {
551  return ByteExtractStringSigned(res, base, len, str, false);
552 }
553 
554 int ByteExtractStringInt32(int32_t *res, int base, size_t len, const char *str)
555 {
556  int64_t i64;
557  int ret;
558 
559  ret = ByteExtractStringSigned(&i64, base, len, str, false);
560  if (ret <= 0) {
561  return ret;
562  }
563  if (i64 < INT32_MIN || i64 > INT32_MAX) {
564  return -1;
565  }
566 
567  *res = (int32_t)i64;
568 
569  if ((int64_t)(*res) != i64) {
570  SCLogError("Numeric value out of range "
571  "(%" PRIi64 " > %" PRIiMAX ")\n",
572  i64, (intmax_t)INT_MAX);
573  return -1;
574  }
575 
576  return ret;
577 }
578 
579 int ByteExtractStringInt16(int16_t *res, int base, size_t len, const char *str)
580 {
581  int64_t i64;
582  int ret;
583 
584  ret = ByteExtractStringSigned(&i64, base, len, str, false);
585  if (ret <= 0) {
586  return ret;
587  }
588  if (i64 < INT16_MIN || i64 > INT16_MAX) {
589  return -1;
590  }
591 
592  *res = (int16_t)i64;
593 
594  if ((int64_t)(*res) != i64) {
595  SCLogError("Numeric value out of range "
596  "(%" PRIi64 " > %" PRIiMAX ")\n",
597  i64, (intmax_t)SHRT_MAX);
598  return -1;
599  }
600 
601  return ret;
602 }
603 
604 int ByteExtractStringInt8(int8_t *res, int base, size_t len, const char *str)
605 {
606  int64_t i64;
607  int ret;
608 
609  ret = ByteExtractStringSigned(&i64, base, len, str, false);
610  if (ret <= 0) {
611  return ret;
612  }
613  if (i64 < INT8_MIN || i64 > INT8_MAX) {
614  return -1;
615  }
616 
617  *res = (int8_t)i64;
618 
619  if ((int64_t)(*res) != i64) {
620  SCLogError("Numeric value out of range "
621  "(%" PRIi64 " > %" PRIiMAX ")\n",
622  i64, (intmax_t)CHAR_MAX);
623  return -1;
624  }
625 
626  return ret;
627 }
628 
629 int StringParseInt64(int64_t *res, int base, size_t len, const char *str)
630 {
631  return ByteExtractStringSigned(res, base, len, str, true);
632 }
633 
634 int StringParseInt32(int32_t *res, int base, size_t len, const char *str)
635 {
636  int64_t i64;
637  int ret;
638 
639  ret = ByteExtractStringSigned(&i64, base, len, str, true);
640  if (ret <= 0) {
641  return ret;
642  }
643  if (i64 < INT32_MIN || i64 > INT32_MAX) {
644  return -1;
645  }
646 
647  *res = (int32_t)i64;
648 
649  if ((int64_t)(*res) != i64) {
650  SCLogError("Numeric value out of range "
651  "(%" PRIi64 " > %" PRIiMAX ")\n",
652  i64, (intmax_t)INT_MAX);
653  return -1;
654  }
655 
656  return ret;
657 }
658 
659 int StringParseInt16(int16_t *res, int base, size_t len, const char *str)
660 {
661  int64_t i64;
662  int ret;
663 
664  ret = ByteExtractStringSigned(&i64, base, len, str, true);
665  if (ret <= 0) {
666  return ret;
667  }
668  if (i64 < INT16_MIN || i64 > INT16_MAX) {
669  return -1;
670  }
671 
672  *res = (int16_t)i64;
673 
674  if ((int64_t)(*res) != i64) {
675  SCLogError("Numeric value out of range "
676  "(%" PRIi64 " > %" PRIiMAX ")\n",
677  i64, (intmax_t)SHRT_MAX);
678  return -1;
679  }
680 
681  return ret;
682 }
683 
684 int StringParseInt8(int8_t *res, int base, size_t len, const char *str)
685 {
686  int64_t i64;
687  int ret;
688 
689  ret = ByteExtractStringSigned(&i64, base, len, str, true);
690  if (ret <= 0) {
691  return ret;
692  }
693  if (i64 < INT8_MIN || i64 > INT8_MAX) {
694  return -1;
695  }
696 
697  *res = (int8_t)i64;
698 
699  if ((int64_t)(*res) != i64) {
700  SCLogError("Numeric value out of range "
701  "(%" PRIi64 " > %" PRIiMAX ")\n",
702  i64, (intmax_t)CHAR_MAX);
703  return -1;
704  }
705 
706  return ret;
707 }
708 
710  int64_t *res, int base, size_t len, const char *str, int64_t min, int64_t max)
711 {
712  int64_t i64;
713  int ret;
714 
715  ret = ByteExtractStringSigned(&i64, base, len, str, true);
716  if (ret <= 0) {
717  return ret;
718  }
719 
720  *res = i64;
721  if (*res < min || *res > max) {
722  return -1;
723  }
724 
725  return ret;
726 }
727 
729  int32_t *res, int base, size_t len, const char *str, int32_t min, int32_t max)
730 {
731  int64_t i64;
732  int ret;
733 
734  ret = ByteExtractStringSigned(&i64, base, len, str, true);
735  if (ret <= 0) {
736  return ret;
737  }
738  if (i64 < INT32_MIN || i64 > INT32_MAX) {
739  return -1;
740  }
741 
742  *res = (int32_t)i64;
743 
744  if (*res < min || *res > max) {
745  return -1;
746  }
747 
748  if ((int64_t)(*res) != i64) {
749  SCLogError("Numeric value out of range "
750  "(%" PRIi64 " > %" PRIiMAX ")\n",
751  i64, (intmax_t)INT_MAX);
752  return -1;
753  }
754 
755  return ret;
756 }
757 
759  int16_t *res, int base, size_t len, const char *str, int16_t min, int16_t max)
760 {
761  int64_t i64;
762  int ret;
763 
764  ret = ByteExtractStringSigned(&i64, base, len, str, true);
765  if (ret <= 0) {
766  return ret;
767  }
768  if (i64 < INT16_MIN || i64 > INT16_MAX) {
769  return -1;
770  }
771 
772  *res = (int16_t)i64;
773 
774  if (*res < min || *res > max) {
775  return -1;
776  }
777 
778  if ((int64_t)(*res) != i64) {
779  SCLogError("Numeric value out of range "
780  "(%" PRIi64 " > %" PRIiMAX ")\n",
781  i64, (intmax_t)SHRT_MAX);
782  return -1;
783  }
784 
785  return ret;
786 }
787 
789  int8_t *res, int base, size_t len, const char *str, int8_t min, int8_t max)
790 {
791  int64_t i64;
792  int ret;
793 
794  ret = ByteExtractStringSigned(&i64, base, len, str, true);
795  if (ret <= 0) {
796  return ret;
797  }
798  if (i64 < INT8_MIN || i64 > INT8_MAX) {
799  return -1;
800  }
801 
802  *res = (int8_t)i64;
803 
804  if (*res < min || *res > max) {
805  return -1;
806  }
807 
808  if ((int64_t)(*res) != i64) {
809  SCLogError("Numeric value out of range "
810  "(%" PRIi64 " > %" PRIiMAX ")\n",
811  i64, (intmax_t)CHAR_MAX);
812  return -1;
813  }
814 
815  return ret;
816 }
817 
818 int HexToRaw(const uint8_t *in, size_t ins, uint8_t *out, size_t outs)
819 {
820  if (ins < 2)
821  return -1;
822  if (ins % 2 != 0)
823  return -1;
824  if (outs != ins / 2)
825  return -1;
826 
827  uint8_t hash[outs];
828  memset(hash, 0, outs);
829  size_t i, x;
830  for (x = 0, i = 0; i < ins; i += 2, x++) {
831  char buf[3] = { 0, 0, 0 };
832  buf[0] = in[i];
833  buf[1] = in[i + 1];
834 
835  long value = strtol(buf, NULL, 16);
836  if (value >= 0 && value <= 255)
837  hash[x] = (uint8_t)value;
838  else {
839  SCLogError("hash byte out of range %ld", value);
840  return -1;
841  }
842  }
843 
844  memcpy(out, hash, outs);
845  return 0;
846 }
847 
848 /* UNITTESTS */
849 #ifdef UNITTESTS
850 
851 static int ByteTest01 (void)
852 {
853  uint16_t val = 0x0102;
854  uint16_t i16 = 0xbfbf;
855  uint8_t bytes[2] = { 0x02, 0x01 };
856  int ret = ByteExtractUint16(&i16, BYTE_LITTLE_ENDIAN, sizeof(bytes), bytes);
857 
858  if ((ret == 2) && (i16 == val)) {
859  return 1;
860  }
861 
862  return 0;
863 }
864 
865 static int ByteTest02 (void)
866 {
867  uint16_t val = 0x0102;
868  uint16_t i16 = 0xbfbf;
869  uint8_t bytes[2] = { 0x01, 0x02 };
870  int ret = ByteExtractUint16(&i16, BYTE_BIG_ENDIAN, sizeof(bytes), bytes);
871 
872  if ((ret == 2) && (i16 == val)) {
873  return 1;
874  }
875 
876  return 0;
877 }
878 
879 static int ByteTest03 (void)
880 {
881  uint32_t val = 0x01020304;
882  uint32_t i32 = 0xbfbfbfbf;
883  uint8_t bytes[4] = { 0x04, 0x03, 0x02, 0x01 };
884  int ret = ByteExtractUint32(&i32, BYTE_LITTLE_ENDIAN, sizeof(bytes), bytes);
885 
886  if ((ret == 4) && (i32 == val)) {
887  return 1;
888  }
889 
890  return 0;
891 }
892 
893 static int ByteTest04 (void)
894 {
895  uint32_t val = 0x01020304;
896  uint32_t i32 = 0xbfbfbfbf;
897  uint8_t bytes[4] = { 0x01, 0x02, 0x03, 0x04 };
898  int ret = ByteExtractUint32(&i32, BYTE_BIG_ENDIAN, sizeof(bytes), bytes);
899 
900  if ((ret == 4) && (i32 == val)) {
901  return 1;
902  }
903 
904  return 0;
905 }
906 
907 static int ByteTest05 (void)
908 {
909  uint64_t val = 0x0102030405060708ULL;
910  uint64_t i64 = 0xbfbfbfbfbfbfbfbfULL;
911  uint8_t bytes[8] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 };
912  int ret = ByteExtractUint64(&i64, BYTE_LITTLE_ENDIAN, sizeof(bytes), bytes);
913 
914  if ((ret == 8) && (i64 == val)) {
915  return 1;
916  }
917 
918  return 0;
919 }
920 
921 static int ByteTest06 (void)
922 {
923  uint64_t val = 0x0102030405060708ULL;
924  uint64_t i64 = 0xbfbfbfbfbfbfbfbfULL;
925  uint8_t bytes[8] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
926  int ret = ByteExtractUint64(&i64, BYTE_BIG_ENDIAN, sizeof(bytes), bytes);
927 
928  if ((ret == 8) && (i64 == val)) {
929  return 1;
930  }
931 
932  return 0;
933 }
934 
935 static int ByteTest07 (void)
936 {
937  const char str[] = "1234567890";
938  uint64_t val = 1234567890;
939  uint64_t i64 = 0xbfbfbfbfbfbfbfbfULL;
940  int ret = ByteExtractStringUint64(&i64, 10, sizeof(str) - 1, str);
941 
942  if ((ret == 10) && (i64 == val)) {
943  return 1;
944  }
945 
946  return 0;
947 }
948 
949 static int ByteTest08 (void)
950 {
951  const char str[] = "1234567890";
952  uint32_t val = 1234567890;
953  uint32_t i32 = 0xbfbfbfbf;
954  int ret = ByteExtractStringUint32(&i32, 10, sizeof(str) - 1, str);
955 
956  if ((ret == 10) && (i32 == val)) {
957  return 1;
958  }
959 
960  return 0;
961 }
962 
963 static int ByteTest09 (void)
964 {
965  const char str[] = "12345";
966  uint16_t val = 12345;
967  uint16_t i16 = 0xbfbf;
968  int ret = ByteExtractStringUint16(&i16, 10, sizeof(str) - 1, str);
969 
970  if ((ret == 5) && (i16 == val)) {
971  return 1;
972  }
973 
974  return 0;
975 }
976 
977 static int ByteTest10 (void)
978 {
979  const char str[] = "123";
980  uint8_t val = 123;
981  uint8_t i8 = 0xbf;
982  int ret = ByteExtractStringUint8(&i8, 10, sizeof(str) - 1, str);
983 
984  if ((ret == 3) && (i8 == val)) {
985  return 1;
986  }
987 
988  return 0;
989 }
990 
991 static int ByteTest11 (void)
992 {
993  const char str[] = "-1234567890";
994  int64_t val = -1234567890;
995  int64_t i64 = 0xbfbfbfbfbfbfbfbfULL;
996  int ret = ByteExtractStringInt64(&i64, 10, sizeof(str) - 1, str);
997 
998  if ((ret == 11) && (i64 == val)) {
999  return 1;
1000  }
1001 
1002  return 0;
1003 }
1004 
1005 static int ByteTest12 (void)
1006 {
1007  const char str[] = "-1234567890";
1008  int32_t val = -1234567890;
1009  int32_t i32 = 0xbfbfbfbf;
1010  int ret = ByteExtractStringInt32(&i32, 10, sizeof(str) - 1, str);
1011 
1012  if ((ret == 11) && (i32 == val)) {
1013  return 1;
1014  }
1015 
1016  return 0;
1017 }
1018 
1019 static int ByteTest13 (void)
1020 {
1021  const char str[] = "-12345";
1022  int16_t val = -12345;
1023  int16_t i16 = 0xbfbf;
1024  int ret = ByteExtractStringInt16(&i16, 10, sizeof(str) - 1, str);
1025 
1026  if ((ret == 6) && (i16 == val)) {
1027  return 1;
1028  }
1029 
1030  return 0;
1031 }
1032 
1033 static int ByteTest14 (void)
1034 {
1035  const char str[] = "-123";
1036  int8_t val = -123;
1037  int8_t i8 = 0xbf;
1038  int ret = ByteExtractStringInt8(&i8, 10, sizeof(str) - 1, str);
1039 
1040  if ((ret == 4) && (i8 == val)) {
1041  return 1;
1042  }
1043 
1044  return 0;
1045 }
1046 
1047 /** \test max u32 value */
1048 static int ByteTest15 (void)
1049 {
1050  const char str[] = "4294967295";
1051  uint32_t val = 4294967295UL;
1052  uint32_t u32 = 0xffffffff;
1053 
1054  int ret = ByteExtractStringUint32(&u32, 10, sizeof(str) - 1, str);
1055  if ((ret == 10) && (u32 == val)) {
1056  return 1;
1057  }
1058 
1059  return 0;
1060 }
1061 
1062 /** \test max u32 value + 1 */
1063 static int ByteTest16 (void)
1064 {
1065  const char str[] = "4294967296";
1066  uint32_t u32 = 0;
1067 
1068  int ret = ByteExtractStringUint32(&u32, 10, sizeof(str) - 1, str);
1069  if (ret != 0) {
1070  return 1;
1071  }
1072 
1073  return 0;
1074 }
1075 
1077 {
1078  UtRegisterTest("ByteTest01", ByteTest01);
1079  UtRegisterTest("ByteTest02", ByteTest02);
1080  UtRegisterTest("ByteTest03", ByteTest03);
1081  UtRegisterTest("ByteTest04", ByteTest04);
1082  UtRegisterTest("ByteTest05", ByteTest05);
1083  UtRegisterTest("ByteTest06", ByteTest06);
1084  UtRegisterTest("ByteTest07", ByteTest07);
1085  UtRegisterTest("ByteTest08", ByteTest08);
1086  UtRegisterTest("ByteTest09", ByteTest09);
1087  UtRegisterTest("ByteTest10", ByteTest10);
1088  UtRegisterTest("ByteTest11", ByteTest11);
1089  UtRegisterTest("ByteTest12", ByteTest12);
1090  UtRegisterTest("ByteTest13", ByteTest13);
1091  UtRegisterTest("ByteTest14", ByteTest14);
1092  UtRegisterTest("ByteTest15", ByteTest15);
1093  UtRegisterTest("ByteTest16", ByteTest16);
1094 }
1095 #endif /* UNITTESTS */
1096 
util-byte.h
len
uint8_t len
Definition: app-layer-dnp3.h:2
StringParseI64RangeCheck
int StringParseI64RangeCheck(int64_t *res, int base, size_t len, const char *str, int64_t min, int64_t max)
Definition: util-byte.c:709
ByteExtractUint64
int ByteExtractUint64(uint64_t *res, int e, uint16_t len, const uint8_t *bytes)
Definition: util-byte.c:122
BytesToString
char * BytesToString(const uint8_t *bytes, size_t nbytes)
Turn byte array into string.
Definition: util-byte.c:41
ByteExtractUint16
int ByteExtractUint16(uint16_t *res, int e, uint16_t len, const uint8_t *bytes)
Definition: util-byte.c:167
ByteExtractStringInt8
int ByteExtractStringInt8(int8_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:604
ByteExtractStringInt16
int ByteExtractStringInt16(int16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:579
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
ByteExtractStringUint16
int ByteExtractStringUint16(uint16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:265
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:275
HexToRaw
int HexToRaw(const uint8_t *in, size_t ins, uint8_t *out, size_t outs)
Definition: util-byte.c:818
StringParseI8RangeCheck
int StringParseI8RangeCheck(int8_t *res, int base, size_t len, const char *str, int8_t min, int8_t max)
Definition: util-byte.c:788
StringParseUint16
int StringParseUint16(uint16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:343
ByteExtractStringInt64
int ByteExtractStringInt64(int64_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:549
ByteExtractStringUint32
int ByteExtractStringUint32(uint32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:242
StringParseInt32
int StringParseInt32(int32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:634
ByteExtractStringUint64
int ByteExtractStringUint64(uint64_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:237
util-unittest.h
StringParseU32RangeCheck
int StringParseU32RangeCheck(uint32_t *res, int base, size_t len, const char *str, uint32_t min, uint32_t max)
Definition: util-byte.c:413
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
StringParseI16RangeCheck
int StringParseI16RangeCheck(int16_t *res, int base, size_t len, const char *str, int16_t min, int16_t max)
Definition: util-byte.c:758
StringParseUint8
int StringParseUint8(uint8_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:370
ByteRegisterTests
void ByteRegisterTests(void)
Definition: util-byte.c:1076
util-debug.h
StringParseI32RangeCheck
int StringParseI32RangeCheck(int32_t *res, int base, size_t len, const char *str, int32_t min, int32_t max)
Definition: util-byte.c:728
ByteExtractStringInt32
int ByteExtractStringInt32(int32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:554
ByteExtractUint32
int ByteExtractUint32(uint32_t *res, int e, uint16_t len, const uint8_t *bytes)
Definition: util-byte.c:146
StringParseU16RangeCheck
int StringParseU16RangeCheck(uint16_t *res, int base, size_t len, const char *str, uint16_t min, uint16_t max)
Definition: util-byte.c:442
StringParseUint32
int StringParseUint32(uint32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:316
BYTE_BIG_ENDIAN
#define BYTE_BIG_ENDIAN
Definition: util-byte.h:29
StringParseInt64
int StringParseInt64(int64_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:629
StringParseU8RangeCheck
int StringParseU8RangeCheck(uint8_t *res, int base, size_t len, const char *str, uint8_t min, uint8_t max)
Definition: util-byte.c:474
ByteExtractStringUint8
int ByteExtractStringUint8(uint8_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:288
BYTE_LITTLE_ENDIAN
#define BYTE_LITTLE_ENDIAN
Definition: util-byte.h:30
StringParseUint64
int StringParseUint64(uint64_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:311
StringParseU64RangeCheck
int StringParseU64RangeCheck(uint64_t *res, int base, size_t len, const char *str, uint64_t min, uint64_t max)
Definition: util-byte.c:394
suricata-common.h
ByteExtractString
int ByteExtractString(uint64_t *res, int base, size_t len, const char *str, bool strict)
Definition: util-byte.c:188
StringParseInt16
int StringParseInt16(int16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:659
util-validate.h
str
#define str(s)
Definition: suricata-common.h:308
ByteExtractStringSigned
int ByteExtractStringSigned(int64_t *res, int base, size_t len, const char *str, bool strict)
Definition: util-byte.c:503
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:267
BytesToStringBuffer
void BytesToStringBuffer(const uint8_t *bytes, size_t nbytes, char *outstr, size_t outlen)
Turn byte array into string.
Definition: util-byte.c:85
StringParseInt8
int StringParseInt8(int8_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:684
dst
uint16_t dst
Definition: app-layer-dnp3.h:4
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102