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