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