suricata
util-misc.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2022 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 Anoop Saldanha <anoopsaldanha@gmail.com>
22  */
23 
24 #include "suricata-common.h"
25 #include "suricata.h"
26 #include "util-byte.h"
27 #include "util-debug.h"
28 #include "util-unittest.h"
29 #include "util-misc.h"
30 
31 #define PARSE_REGEX "^\\s*(\\d+(?:.\\d+)?)\\s*([a-zA-Z]{2})?\\s*$"
32 static pcre2_code *parse_regex = NULL;
33 static pcre2_match_data *parse_regex_match = NULL;
34 
35 void ParseSizeInit(void)
36 {
37  int en;
38  PCRE2_SIZE eo;
39  int opts = 0;
40 
41  parse_regex =
42  pcre2_compile((PCRE2_SPTR8)PARSE_REGEX, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
43  if (parse_regex == NULL) {
44  PCRE2_UCHAR errbuffer[256];
45  pcre2_get_error_message(en, errbuffer, sizeof(errbuffer));
46  SCLogError("pcre2 compile of \"%s\" failed at "
47  "offset %d: %s",
48  PARSE_REGEX, (int)eo, errbuffer);
49  exit(EXIT_FAILURE);
50  }
51  parse_regex_match = pcre2_match_data_create_from_pattern(parse_regex, NULL);
52 }
53 
54 void ParseSizeDeinit(void)
55 {
56  pcre2_code_free(parse_regex);
57  pcre2_match_data_free(parse_regex_match);
58 }
59 
60 /* size string parsing API */
61 
62 static int ParseSizeString(const char *size, double *res)
63 {
64  int pcre2_match_ret;
65  int r;
66  int retval = 0;
67  char str[128];
68  char str2[128];
69 
70  *res = 0;
71 
72  if (size == NULL) {
73  SCLogError("invalid size argument - NULL. Valid size "
74  "argument should be in the format - \n"
75  "xxx <- indicates it is just bytes\n"
76  "xxxkb or xxxKb or xxxKB or xxxkB <- indicates kilobytes\n"
77  "xxxmb or xxxMb or xxxMB or xxxmB <- indicates megabytes\n"
78  "xxxgb or xxxGb or xxxGB or xxxgB <- indicates gigabytes.\n");
79  retval = -2;
80  goto end;
81  }
82 
83  pcre2_match_ret = pcre2_match(
84  parse_regex, (PCRE2_SPTR8)size, strlen(size), 0, 0, parse_regex_match, NULL);
85 
86  if (!(pcre2_match_ret == 2 || pcre2_match_ret == 3)) {
87  SCLogError("invalid size argument - %s. Valid size "
88  "argument should be in the format - \n"
89  "xxx <- indicates it is just bytes\n"
90  "xxxkb or xxxKb or xxxKB or xxxkB <- indicates kilobytes\n"
91  "xxxmb or xxxMb or xxxMB or xxxmB <- indicates megabytes\n"
92  "xxxgb or xxxGb or xxxGB or xxxgB <- indicates gigabytes.\n",
93  size);
94  retval = -2;
95  goto end;
96  }
97 
98  size_t copylen = sizeof(str);
99  r = pcre2_substring_copy_bynumber(parse_regex_match, 1, (PCRE2_UCHAR8 *)str, &copylen);
100  if (r < 0) {
101  SCLogError("pcre2_substring_copy_bynumber failed");
102  retval = -2;
103  goto end;
104  }
105 
106  char *endptr, *str_ptr = str;
107  errno = 0;
108  *res = strtod(str_ptr, &endptr);
109  if (errno == ERANGE) {
110  SCLogError("Numeric value out of range");
111  retval = -1;
112  goto end;
113  } else if (endptr == str_ptr) {
114  SCLogError("Invalid numeric value");
115  retval = -1;
116  goto end;
117  }
118 
119  if (pcre2_match_ret == 3) {
120  copylen = sizeof(str2);
121  r = pcre2_substring_copy_bynumber(parse_regex_match, 2, (PCRE2_UCHAR8 *)str2, &copylen);
122 
123  if (r < 0) {
124  SCLogError("pcre2_substring_copy_bynumber failed");
125  retval = -2;
126  goto end;
127  }
128 
129  if (strcasecmp(str2, "kb") == 0) {
130  *res *= 1024;
131  } else if (strcasecmp(str2, "mb") == 0) {
132  *res *= 1024 * 1024;
133  } else if (strcasecmp(str2, "gb") == 0) {
134  *res *= 1024 * 1024 * 1024;
135  } else {
136  /* Bad unit. */
137  retval = -1;
138  goto end;
139  }
140  }
141 
142  retval = 0;
143 end:
144  return retval;
145 }
146 
147 int ParseSizeStringU8(const char *size, uint8_t *res)
148 {
149  double temp_res = 0;
150 
151  *res = 0;
152  int r = ParseSizeString(size, &temp_res);
153  if (r < 0)
154  return r;
155 
156  if (temp_res > UINT8_MAX)
157  return -1;
158 
159  *res = temp_res;
160 
161  return 0;
162 }
163 
164 int ParseSizeStringU16(const char *size, uint16_t *res)
165 {
166  double temp_res = 0;
167 
168  *res = 0;
169  int r = ParseSizeString(size, &temp_res);
170  if (r < 0)
171  return r;
172 
173  if (temp_res > UINT16_MAX)
174  return -1;
175 
176  *res = temp_res;
177 
178  return 0;
179 }
180 
181 int ParseSizeStringU32(const char *size, uint32_t *res)
182 {
183  double temp_res = 0;
184 
185  *res = 0;
186  int r = ParseSizeString(size, &temp_res);
187  if (r < 0)
188  return r;
189 
190  if (temp_res > UINT32_MAX)
191  return -1;
192 
193  *res = temp_res;
194 
195  return 0;
196 }
197 
198 int ParseSizeStringU64(const char *size, uint64_t *res)
199 {
200  double temp_res = 0;
201 
202  *res = 0;
203  int r = ParseSizeString(size, &temp_res);
204  if (r < 0)
205  return r;
206 
207  if (temp_res > (double) UINT64_MAX)
208  return -1;
209 
210  *res = temp_res;
211 
212  return 0;
213 }
214 
215 void ShortenString(const char *input,
216  char *output, size_t output_size, char c)
217 {
218  const size_t str_len = strlen(input);
219  size_t half = (output_size - 1) / 2;
220 
221  /* If the output size is an even number */
222  if (half * 2 == (output_size - 1)) {
223  half = half - 1;
224  }
225 
226  size_t spaces = (output_size - 1) - (half * 2);
227 
228  /* Add the first half to the new string */
229  snprintf(output, half+1, "%s", input);
230 
231  /* Add the amount of spaces wanted */
232  size_t length = half;
233  for (size_t i = half; i < half + spaces; i++) {
234  char s[2] = "";
235  snprintf(s, sizeof(s), "%c", c);
236  length = strlcat(output, s, output_size);
237  }
238 
239  snprintf(output + length, half + 1, "%s", input + (str_len - half));
240 }
241 
242 /*********************************Unittests********************************/
243 
244 #ifdef UNITTESTS
245 
246 static int UtilMiscParseSizeStringTest01(void)
247 {
248  const char *str;
249  double result;
250 
251  /* no space */
252 
253  str = "10";
254  result = 0;
255  if (ParseSizeString(str, &result) > 0) {
256  goto error;
257  }
258  if (result != 10) {
259  goto error;
260  }
261 
262  str = "10kb";
263  result = 0;
264  if (ParseSizeString(str, &result) > 0) {
265  goto error;
266  }
267  if (result != 10 * 1024) {
268  goto error;
269  }
270 
271  str = "10Kb";
272  result = 0;
273  if (ParseSizeString(str, &result) > 0) {
274  goto error;
275  }
276  if (result != 10 * 1024) {
277  goto error;
278  }
279 
280  str = "10KB";
281  result = 0;
282  if (ParseSizeString(str, &result) > 0) {
283  goto error;
284  }
285  if (result != 10 * 1024) {
286  goto error;
287  }
288 
289  str = "10mb";
290  result = 0;
291  if (ParseSizeString(str, &result) > 0) {
292  goto error;
293  }
294  if (result != 10 * 1024 * 1024) {
295  goto error;
296  }
297 
298  str = "10gb";
299  result = 0;
300  if (ParseSizeString(str, &result) > 0) {
301  goto error;
302  }
303  if (result != 10737418240UL) {
304  goto error;
305  }
306 
307 
308  /* space start */
309 
310  str = " 10";
311  result = 0;
312  if (ParseSizeString(str, &result) > 0) {
313  goto error;
314  }
315  if (result != 10) {
316  goto error;
317  }
318 
319  str = " 10kb";
320  result = 0;
321  if (ParseSizeString(str, &result) > 0) {
322  goto error;
323  }
324  if (result != 10 * 1024) {
325  goto error;
326  }
327 
328  str = " 10Kb";
329  result = 0;
330  if (ParseSizeString(str, &result) > 0) {
331  goto error;
332  }
333  if (result != 10 * 1024) {
334  goto error;
335  }
336 
337  str = " 10KB";
338  result = 0;
339  if (ParseSizeString(str, &result) > 0) {
340  goto error;
341  }
342  if (result != 10 * 1024) {
343  goto error;
344  }
345 
346  str = " 10mb";
347  result = 0;
348  if (ParseSizeString(str, &result) > 0) {
349  goto error;
350  }
351  if (result != 10 * 1024 * 1024) {
352  goto error;
353  }
354 
355  str = " 10gb";
356  result = 0;
357  if (ParseSizeString(str, &result) > 0) {
358  goto error;
359  }
360  if (result != 10737418240) {
361  goto error;
362  }
363 
364  /* space end */
365 
366  str = "10 ";
367  result = 0;
368  if (ParseSizeString(str, &result) > 0) {
369  goto error;
370  }
371  if (result != 10) {
372  goto error;
373  }
374 
375  str = "10kb ";
376  result = 0;
377  if (ParseSizeString(str, &result) > 0) {
378  goto error;
379  }
380  if (result != 10 * 1024) {
381  goto error;
382  }
383 
384  str = "10Kb ";
385  result = 0;
386  if (ParseSizeString(str, &result) > 0) {
387  goto error;
388  }
389  if (result != 10 * 1024) {
390  goto error;
391  }
392 
393  str = "10KB ";
394  result = 0;
395  if (ParseSizeString(str, &result) > 0) {
396  goto error;
397  }
398  if (result != 10 * 1024) {
399  goto error;
400  }
401 
402  str = "10mb ";
403  result = 0;
404  if (ParseSizeString(str, &result) > 0) {
405  goto error;
406  }
407  if (result != 10 * 1024 * 1024) {
408  goto error;
409  }
410 
411  str = "10gb ";
412  result = 0;
413  if (ParseSizeString(str, &result) > 0) {
414  goto error;
415  }
416  if (result != 10737418240) {
417  goto error;
418  }
419 
420  /* space start - space end */
421 
422  str = " 10 ";
423  result = 0;
424  if (ParseSizeString(str, &result) > 0) {
425  goto error;
426  }
427  if (result != 10) {
428  goto error;
429  }
430 
431  str = " 10kb ";
432  result = 0;
433  if (ParseSizeString(str, &result) > 0) {
434  goto error;
435  }
436  if (result != 10 * 1024) {
437  goto error;
438  }
439 
440  str = " 10Kb ";
441  result = 0;
442  if (ParseSizeString(str, &result) > 0) {
443  goto error;
444  }
445  if (result != 10 * 1024) {
446  goto error;
447  }
448 
449  str = " 10KB ";
450  result = 0;
451  if (ParseSizeString(str, &result) > 0) {
452  goto error;
453  }
454  if (result != 10 * 1024) {
455  goto error;
456  }
457 
458  str = " 10mb ";
459  result = 0;
460  if (ParseSizeString(str, &result) > 0) {
461  goto error;
462  }
463  if (result != 10 * 1024 * 1024) {
464  goto error;
465  }
466 
467  str = " 10gb ";
468  result = 0;
469  if (ParseSizeString(str, &result) > 0) {
470  goto error;
471  }
472  if (result != 10737418240) {
473  goto error;
474  }
475 
476 
477  /* space between number and scale */
478 
479  /* no space */
480 
481  str = "10";
482  result = 0;
483  if (ParseSizeString(str, &result) > 0) {
484  goto error;
485  }
486  if (result != 10) {
487  goto error;
488  }
489 
490  str = "10 kb";
491  result = 0;
492  if (ParseSizeString(str, &result) > 0) {
493  goto error;
494  }
495  if (result != 10 * 1024) {
496  goto error;
497  }
498 
499  str = "10 Kb";
500  result = 0;
501  if (ParseSizeString(str, &result) > 0) {
502  goto error;
503  }
504  if (result != 10 * 1024) {
505  goto error;
506  }
507 
508  str = "10 KB";
509  result = 0;
510  if (ParseSizeString(str, &result) > 0) {
511  goto error;
512  }
513  if (result != 10 * 1024) {
514  goto error;
515  }
516 
517  str = "10 mb";
518  result = 0;
519  if (ParseSizeString(str, &result) > 0) {
520  goto error;
521  }
522  if (result != 10 * 1024 * 1024) {
523  goto error;
524  }
525 
526  str = "10 gb";
527  result = 0;
528  if (ParseSizeString(str, &result) > 0) {
529  goto error;
530  }
531  if (result != 10737418240) {
532  goto error;
533  }
534 
535 
536  /* space start */
537 
538  str = " 10";
539  result = 0;
540  if (ParseSizeString(str, &result) > 0) {
541  goto error;
542  }
543  if (result != 10) {
544  goto error;
545  }
546 
547  str = " 10 kb";
548  result = 0;
549  if (ParseSizeString(str, &result) > 0) {
550  goto error;
551  }
552  if (result != 10 * 1024) {
553  goto error;
554  }
555 
556  str = " 10 Kb";
557  result = 0;
558  if (ParseSizeString(str, &result) > 0) {
559  goto error;
560  }
561  if (result != 10 * 1024) {
562  goto error;
563  }
564 
565  str = " 10 KB";
566  result = 0;
567  if (ParseSizeString(str, &result) > 0) {
568  goto error;
569  }
570  if (result != 10 * 1024) {
571  goto error;
572  }
573 
574  str = " 10 mb";
575  result = 0;
576  if (ParseSizeString(str, &result) > 0) {
577  goto error;
578  }
579  if (result != 10 * 1024 * 1024) {
580  goto error;
581  }
582 
583  str = " 10 gb";
584  result = 0;
585  if (ParseSizeString(str, &result) > 0) {
586  goto error;
587  }
588  if (result != 10737418240) {
589  goto error;
590  }
591 
592  /* space end */
593 
594  str = "10 ";
595  result = 0;
596  if (ParseSizeString(str, &result) > 0) {
597  goto error;
598  }
599  if (result != 10) {
600  goto error;
601  }
602 
603  str = "10 kb ";
604  result = 0;
605  if (ParseSizeString(str, &result) > 0) {
606  goto error;
607  }
608  if (result != 10 * 1024) {
609  goto error;
610  }
611 
612  str = "10 Kb ";
613  result = 0;
614  if (ParseSizeString(str, &result) > 0) {
615  goto error;
616  }
617  if (result != 10 * 1024) {
618  goto error;
619  }
620 
621  str = "10 KB ";
622  result = 0;
623  if (ParseSizeString(str, &result) > 0) {
624  goto error;
625  }
626  if (result != 10 * 1024) {
627  goto error;
628  }
629 
630  str = "10 mb ";
631  result = 0;
632  if (ParseSizeString(str, &result) > 0) {
633  goto error;
634  }
635  if (result != 10 * 1024 * 1024) {
636  goto error;
637  }
638 
639  str = "10 gb ";
640  result = 0;
641  if (ParseSizeString(str, &result) > 0) {
642  goto error;
643  }
644  if (result != 10737418240) {
645  goto error;
646  }
647 
648  /* space start - space end */
649 
650  str = " 10 ";
651  result = 0;
652  if (ParseSizeString(str, &result) > 0) {
653  goto error;
654  }
655  if (result != 10) {
656  goto error;
657  }
658 
659  str = " 10 kb ";
660  result = 0;
661  if (ParseSizeString(str, &result) > 0) {
662  goto error;
663  }
664  if (result != 10 * 1024) {
665  goto error;
666  }
667 
668  str = " 10 Kb ";
669  result = 0;
670  if (ParseSizeString(str, &result) > 0) {
671  goto error;
672  }
673  if (result != 10 * 1024) {
674  goto error;
675  }
676 
677  str = " 10 KB ";
678  result = 0;
679  if (ParseSizeString(str, &result) > 0) {
680  goto error;
681  }
682  if (result != 10 * 1024) {
683  goto error;
684  }
685 
686  str = " 10 mb ";
687  result = 0;
688  if (ParseSizeString(str, &result) > 0) {
689  goto error;
690  }
691  if (result != 10 * 1024 * 1024) {
692  goto error;
693  }
694 
695  str = " 10 gb ";
696  result = 0;
697  if (ParseSizeString(str, &result) > 0) {
698  goto error;
699  }
700  if (result != 10737418240) {
701  goto error;
702  }
703 
704  /* no space */
705 
706  str = "10.5";
707  result = 0;
708  if (ParseSizeString(str, &result) > 0) {
709  goto error;
710  }
711  if (result != 10.5) {
712  goto error;
713  }
714 
715  str = "10.5kb";
716  result = 0;
717  if (ParseSizeString(str, &result) > 0) {
718  goto error;
719  }
720  if (result != 10.5 * 1024) {
721  goto error;
722  }
723 
724  str = "10.5Kb";
725  result = 0;
726  if (ParseSizeString(str, &result) > 0) {
727  goto error;
728  }
729  if (result != 10.5 * 1024) {
730  goto error;
731  }
732 
733  str = "10.5KB";
734  result = 0;
735  if (ParseSizeString(str, &result) > 0) {
736  goto error;
737  }
738  if (result != 10.5 * 1024) {
739  goto error;
740  }
741 
742  str = "10.5mb";
743  result = 0;
744  if (ParseSizeString(str, &result) > 0) {
745  goto error;
746  }
747  if (result != 10.5 * 1024 * 1024) {
748  goto error;
749  }
750 
751  str = "10.5gb";
752  result = 0;
753  if (ParseSizeString(str, &result) > 0) {
754  goto error;
755  }
756  if (result != 10.5 * 1024 * 1024 * 1024) {
757  goto error;
758  }
759 
760 
761  /* space start */
762 
763  str = " 10.5";
764  result = 0;
765  if (ParseSizeString(str, &result) > 0) {
766  goto error;
767  }
768  if (result != 10.5) {
769  goto error;
770  }
771 
772  str = " 10.5kb";
773  result = 0;
774  if (ParseSizeString(str, &result) > 0) {
775  goto error;
776  }
777  if (result != 10.5 * 1024) {
778  goto error;
779  }
780 
781  str = " 10.5Kb";
782  result = 0;
783  if (ParseSizeString(str, &result) > 0) {
784  goto error;
785  }
786  if (result != 10.5 * 1024) {
787  goto error;
788  }
789 
790  str = " 10.5KB";
791  result = 0;
792  if (ParseSizeString(str, &result) > 0) {
793  goto error;
794  }
795  if (result != 10.5 * 1024) {
796  goto error;
797  }
798 
799  str = " 10.5mb";
800  result = 0;
801  if (ParseSizeString(str, &result) > 0) {
802  goto error;
803  }
804  if (result != 10.5 * 1024 * 1024) {
805  goto error;
806  }
807 
808  str = " 10.5gb";
809  result = 0;
810  if (ParseSizeString(str, &result) > 0) {
811  goto error;
812  }
813  if (result != 10.5 * 1024 * 1024 * 1024) {
814  goto error;
815  }
816 
817  /* space end */
818 
819  str = "10.5 ";
820  result = 0;
821  if (ParseSizeString(str, &result) > 0) {
822  goto error;
823  }
824  if (result != 10.5) {
825  goto error;
826  }
827 
828  str = "10.5kb ";
829  result = 0;
830  if (ParseSizeString(str, &result) > 0) {
831  goto error;
832  }
833  if (result != 10.5 * 1024) {
834  goto error;
835  }
836 
837  str = "10.5Kb ";
838  result = 0;
839  if (ParseSizeString(str, &result) > 0) {
840  goto error;
841  }
842  if (result != 10.5 * 1024) {
843  goto error;
844  }
845 
846  str = "10.5KB ";
847  result = 0;
848  if (ParseSizeString(str, &result) > 0) {
849  goto error;
850  }
851  if (result != 10.5 * 1024) {
852  goto error;
853  }
854 
855  str = "10.5mb ";
856  result = 0;
857  if (ParseSizeString(str, &result) > 0) {
858  goto error;
859  }
860  if (result != 10.5 * 1024 * 1024) {
861  goto error;
862  }
863 
864  str = "10.5gb ";
865  result = 0;
866  if (ParseSizeString(str, &result) > 0) {
867  goto error;
868  }
869  if (result != 10.5 * 1024 * 1024 * 1024) {
870  goto error;
871  }
872 
873  /* space start - space end */
874 
875  str = " 10.5 ";
876  result = 0;
877  if (ParseSizeString(str, &result) > 0) {
878  goto error;
879  }
880  if (result != 10.5) {
881  goto error;
882  }
883 
884  str = " 10.5kb ";
885  result = 0;
886  if (ParseSizeString(str, &result) > 0) {
887  goto error;
888  }
889  if (result != 10.5 * 1024) {
890  goto error;
891  }
892 
893  str = " 10.5Kb ";
894  result = 0;
895  if (ParseSizeString(str, &result) > 0) {
896  goto error;
897  }
898  if (result != 10.5 * 1024) {
899  goto error;
900  }
901 
902  str = " 10.5KB ";
903  result = 0;
904  if (ParseSizeString(str, &result) > 0) {
905  goto error;
906  }
907  if (result != 10.5 * 1024) {
908  goto error;
909  }
910 
911  str = " 10.5mb ";
912  result = 0;
913  if (ParseSizeString(str, &result) > 0) {
914  goto error;
915  }
916  if (result != 10.5 * 1024 * 1024) {
917  goto error;
918  }
919 
920  str = " 10.5gb ";
921  result = 0;
922  if (ParseSizeString(str, &result) > 0) {
923  goto error;
924  }
925  if (result != 10.5 * 1024 * 1024 * 1024) {
926  goto error;
927  }
928 
929 
930  /* space between number and scale */
931 
932  /* no space */
933 
934  str = "10.5";
935  result = 0;
936  if (ParseSizeString(str, &result) > 0) {
937  goto error;
938  }
939  if (result != 10.5) {
940  goto error;
941  }
942 
943  str = "10.5 kb";
944  result = 0;
945  if (ParseSizeString(str, &result) > 0) {
946  goto error;
947  }
948  if (result != 10.5 * 1024) {
949  goto error;
950  }
951 
952  str = "10.5 Kb";
953  result = 0;
954  if (ParseSizeString(str, &result) > 0) {
955  goto error;
956  }
957  if (result != 10.5 * 1024) {
958  goto error;
959  }
960 
961  str = "10.5 KB";
962  result = 0;
963  if (ParseSizeString(str, &result) > 0) {
964  goto error;
965  }
966  if (result != 10.5 * 1024) {
967  goto error;
968  }
969 
970  str = "10.5 mb";
971  result = 0;
972  if (ParseSizeString(str, &result) > 0) {
973  goto error;
974  }
975  if (result != 10.5 * 1024 * 1024) {
976  goto error;
977  }
978 
979  str = "10.5 gb";
980  result = 0;
981  if (ParseSizeString(str, &result) > 0) {
982  goto error;
983  }
984  if (result != 10.5 * 1024 * 1024 * 1024) {
985  goto error;
986  }
987 
988 
989  /* space start */
990 
991  str = " 10.5";
992  result = 0;
993  if (ParseSizeString(str, &result) > 0) {
994  goto error;
995  }
996  if (result != 10.5) {
997  goto error;
998  }
999 
1000  str = " 10.5 kb";
1001  result = 0;
1002  if (ParseSizeString(str, &result) > 0) {
1003  goto error;
1004  }
1005  if (result != 10.5 * 1024) {
1006  goto error;
1007  }
1008 
1009  str = " 10.5 Kb";
1010  result = 0;
1011  if (ParseSizeString(str, &result) > 0) {
1012  goto error;
1013  }
1014  if (result != 10.5 * 1024) {
1015  goto error;
1016  }
1017 
1018  str = " 10.5 KB";
1019  result = 0;
1020  if (ParseSizeString(str, &result) > 0) {
1021  goto error;
1022  }
1023  if (result != 10.5 * 1024) {
1024  goto error;
1025  }
1026 
1027  str = " 10.5 mb";
1028  result = 0;
1029  if (ParseSizeString(str, &result) > 0) {
1030  goto error;
1031  }
1032  if (result != 10.5 * 1024 * 1024) {
1033  goto error;
1034  }
1035 
1036  str = " 10.5 gb";
1037  result = 0;
1038  if (ParseSizeString(str, &result) > 0) {
1039  goto error;
1040  }
1041  if (result != 10.5 * 1024 * 1024 * 1024) {
1042  goto error;
1043  }
1044 
1045  /* space end */
1046 
1047  str = "10.5 ";
1048  result = 0;
1049  if (ParseSizeString(str, &result) > 0) {
1050  goto error;
1051  }
1052  if (result != 10.5) {
1053  goto error;
1054  }
1055 
1056  str = "10.5 kb ";
1057  result = 0;
1058  if (ParseSizeString(str, &result) > 0) {
1059  goto error;
1060  }
1061  if (result != 10.5 * 1024) {
1062  goto error;
1063  }
1064 
1065  str = "10.5 Kb ";
1066  result = 0;
1067  if (ParseSizeString(str, &result) > 0) {
1068  goto error;
1069  }
1070  if (result != 10.5 * 1024) {
1071  goto error;
1072  }
1073 
1074  str = "10.5 KB ";
1075  result = 0;
1076  if (ParseSizeString(str, &result) > 0) {
1077  goto error;
1078  }
1079  if (result != 10.5 * 1024) {
1080  goto error;
1081  }
1082 
1083  str = "10.5 mb ";
1084  result = 0;
1085  if (ParseSizeString(str, &result) > 0) {
1086  goto error;
1087  }
1088  if (result != 10.5 * 1024 * 1024) {
1089  goto error;
1090  }
1091 
1092  str = "10.5 gb ";
1093  result = 0;
1094  if (ParseSizeString(str, &result) > 0) {
1095  goto error;
1096  }
1097  if (result != 10.5 * 1024 * 1024 * 1024) {
1098  goto error;
1099  }
1100 
1101  /* space start - space end */
1102 
1103  str = " 10.5 ";
1104  result = 0;
1105  if (ParseSizeString(str, &result) > 0) {
1106  goto error;
1107  }
1108  if (result != 10.5) {
1109  goto error;
1110  }
1111 
1112  str = " 10.5 kb ";
1113  result = 0;
1114  if (ParseSizeString(str, &result) > 0) {
1115  goto error;
1116  }
1117  if (result != 10.5 * 1024) {
1118  goto error;
1119  }
1120 
1121  str = " 10.5 Kb ";
1122  result = 0;
1123  if (ParseSizeString(str, &result) > 0) {
1124  goto error;
1125  }
1126  if (result != 10.5 * 1024) {
1127  goto error;
1128  }
1129 
1130  str = " 10.5 KB ";
1131  result = 0;
1132  if (ParseSizeString(str, &result) > 0) {
1133  goto error;
1134  }
1135  if (result != 10.5 * 1024) {
1136  goto error;
1137  }
1138 
1139  str = " 10.5 mb ";
1140  result = 0;
1141  if (ParseSizeString(str, &result) > 0) {
1142  goto error;
1143  }
1144  if (result != 10.5 * 1024 * 1024) {
1145  goto error;
1146  }
1147 
1148  str = " 10.5 gb ";
1149  result = 0;
1150  if (ParseSizeString(str, &result) > 0) {
1151  goto error;
1152  }
1153  if (result != 10.5 * 1024 * 1024 * 1024) {
1154  goto error;
1155  }
1156 
1157  /* Should fail on unknown units. */
1158  if (ParseSizeString("32eb", &result) > 0) {
1159  goto error;
1160  }
1161 
1162  return 1;
1163  error:
1164  return 0;
1165 }
1166 
1168 {
1169  UtRegisterTest("UtilMiscParseSizeStringTest01",
1170  UtilMiscParseSizeStringTest01);
1171 }
1172 #endif /* UNITTESTS */
util-byte.h
ParseSizeStringU16
int ParseSizeStringU16(const char *size, uint16_t *res)
Definition: util-misc.c:164
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
ParseSizeStringU64
int ParseSizeStringU64(const char *size, uint64_t *res)
Definition: util-misc.c:198
MemcmpTest18Tests::result
int result
Definition: util-memcmp.c:345
ShortenString
void ShortenString(const char *input, char *output, size_t output_size, char c)
Definition: util-misc.c:215
util-unittest.h
ParseSizeInit
void ParseSizeInit(void)
Definition: util-misc.c:35
util-debug.h
ParseSizeStringU8
int ParseSizeStringU8(const char *size, uint8_t *res)
Definition: util-misc.c:147
strlcat
size_t strlcat(char *, const char *src, size_t siz)
Definition: util-strlcatu.c:45
suricata-common.h
ParseSizeDeinit
void ParseSizeDeinit(void)
Definition: util-misc.c:54
ParseSizeStringU32
int ParseSizeStringU32(const char *size, uint32_t *res)
Definition: util-misc.c:181
str
#define str(s)
Definition: suricata-common.h:286
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
UtilMiscRegisterTests
void UtilMiscRegisterTests(void)
Definition: util-misc.c:1167
suricata.h
util-misc.h
PARSE_REGEX
#define PARSE_REGEX
Definition: util-misc.c:31