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,3})?\\s*$"
32static pcre2_code *parse_regex = NULL;
33static pcre2_match_data *parse_regex_match = NULL;
34
35void 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
55{
56 pcre2_code_free(parse_regex);
57 pcre2_match_data_free(parse_regex_match);
58}
59
60/* size string parsing API */
61
62static 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 input is <number><unit>. Unit can be "
74 "kb/KiB, mb/MiB or gb/GiB");
75 retval = -2;
76 goto end;
77 }
78
79 pcre2_match_ret = pcre2_match(
80 parse_regex, (PCRE2_SPTR8)size, strlen(size), 0, 0, parse_regex_match, NULL);
81
82 if (!(pcre2_match_ret == 2 || pcre2_match_ret == 3)) {
83 SCLogError("invalid size argument: '%s'. Valid input is <number><unit>. Unit can be "
84 "kb/KiB, mb/MiB or gb/GiB",
85 size);
86 retval = -2;
87 goto end;
88 }
89
90 size_t copylen = sizeof(str);
91 r = pcre2_substring_copy_bynumber(parse_regex_match, 1, (PCRE2_UCHAR8 *)str, &copylen);
92 if (r < 0) {
93 SCLogError("pcre2_substring_copy_bynumber failed");
94 retval = -2;
95 goto end;
96 }
97
98 char *endptr, *str_ptr = str;
99 errno = 0;
100 *res = strtod(str_ptr, &endptr);
101 if (errno == ERANGE) {
102 SCLogError("Numeric value out of range");
103 retval = -1;
104 goto end;
105 } else if (endptr == str_ptr) {
106 SCLogError("Invalid numeric value");
107 retval = -1;
108 goto end;
109 }
110
111 if (pcre2_match_ret == 3) {
112 copylen = sizeof(str2);
113 r = pcre2_substring_copy_bynumber(parse_regex_match, 2, (PCRE2_UCHAR8 *)str2, &copylen);
114
115 if (r < 0) {
116 SCLogError("pcre2_substring_copy_bynumber failed");
117 retval = -2;
118 goto end;
119 }
120
121 if (strcasecmp(str2, "kb") == 0 || strcmp(str2, "KiB") == 0) {
122 *res *= 1024;
123 } else if (strcasecmp(str2, "mb") == 0 || strcmp(str2, "MiB") == 0) {
124 *res *= 1024 * 1024;
125 } else if (strcasecmp(str2, "gb") == 0 || strcmp(str2, "GiB") == 0) {
126 *res *= 1024 * 1024 * 1024;
127 } else {
128 /* Bad unit. */
129 retval = -1;
130 goto end;
131 }
132 }
133
134 retval = 0;
135end:
136 return retval;
137}
138
139int ParseSizeStringU8(const char *size, uint8_t *res)
140{
141 double temp_res = 0;
142
143 *res = 0;
144 int r = ParseSizeString(size, &temp_res);
145 if (r < 0)
146 return r;
147
148 if (temp_res > UINT8_MAX)
149 return -1;
150
151 *res = temp_res;
152
153 return 0;
154}
155
156int ParseSizeStringU16(const char *size, uint16_t *res)
157{
158 double temp_res = 0;
159
160 *res = 0;
161 int r = ParseSizeString(size, &temp_res);
162 if (r < 0)
163 return r;
164
165 if (temp_res > UINT16_MAX)
166 return -1;
167
168 *res = temp_res;
169
170 return 0;
171}
172
173int ParseSizeStringU32(const char *size, uint32_t *res)
174{
175 double temp_res = 0;
176
177 *res = 0;
178 int r = ParseSizeString(size, &temp_res);
179 if (r < 0)
180 return r;
181
182 if (temp_res > UINT32_MAX)
183 return -1;
184
185 *res = temp_res;
186
187 return 0;
188}
189
190int ParseSizeStringU64(const char *size, uint64_t *res)
191{
192 double temp_res = 0;
193
194 *res = 0;
195 int r = ParseSizeString(size, &temp_res);
196 if (r < 0)
197 return r;
198
199 if (temp_res > (double) UINT64_MAX)
200 return -1;
201
202 *res = temp_res;
203
204 return 0;
205}
206
207void ShortenString(const char *input,
208 char *output, size_t output_size, char c)
209{
210 const size_t str_len = strlen(input);
211 size_t half = (output_size - 1) / 2;
212
213 /* If the output size is an even number */
214 if (half * 2 == (output_size - 1)) {
215 half = half - 1;
216 }
217
218 size_t spaces = (output_size - 1) - (half * 2);
219
220 /* Add the first half to the new string */
221 snprintf(output, half+1, "%s", input);
222
223 /* Add the amount of spaces wanted */
224 size_t length = half;
225 for (size_t i = half; i < half + spaces; i++) {
226 char s[2] = "";
227 snprintf(s, sizeof(s), "%c", c);
228 length = strlcat(output, s, output_size);
229 }
230
231 snprintf(output + length, half + 1, "%s", input + (str_len - half));
232}
233
234/*********************************Unittests********************************/
235
236#ifdef UNITTESTS
237
238static int UtilMiscParseSizeStringTest01(void)
239{
240 const char *str;
241 double result;
242
243 /* no space */
244
245 str = "10";
246 result = 0;
247 FAIL_IF(ParseSizeString(str, &result) != 0);
248 FAIL_IF(result != 10);
249
250 str = "10kb";
251 result = 0;
252 FAIL_IF(ParseSizeString(str, &result) != 0);
253 FAIL_IF(result != 10 * 1024);
254
255 str = "10Kb";
256 result = 0;
257 FAIL_IF(ParseSizeString(str, &result) != 0);
258 FAIL_IF(result != 10 * 1024);
259
260 str = "10KB";
261 result = 0;
262 FAIL_IF(ParseSizeString(str, &result) != 0);
263 FAIL_IF(result != 10 * 1024);
264
265 str = "10mb";
266 result = 0;
267 FAIL_IF(ParseSizeString(str, &result) != 0);
268 FAIL_IF(result != 10 * 1024 * 1024);
269
270 str = "10gb";
271 result = 0;
272 FAIL_IF(ParseSizeString(str, &result) != 0);
273 FAIL_IF(result != 10737418240UL);
274
275 /* space start */
276
277 str = " 10";
278 result = 0;
279 FAIL_IF(ParseSizeString(str, &result) != 0);
280 FAIL_IF(result != 10);
281
282 str = " 10kb";
283 result = 0;
284 FAIL_IF(ParseSizeString(str, &result) != 0);
285 FAIL_IF(result != 10 * 1024);
286
287 str = " 10Kb";
288 result = 0;
289 FAIL_IF(ParseSizeString(str, &result) != 0);
290 FAIL_IF(result != 10 * 1024);
291
292 str = " 10KB";
293 result = 0;
294 FAIL_IF(ParseSizeString(str, &result) != 0);
295 FAIL_IF(result != 10 * 1024);
296
297 str = " 10mb";
298 result = 0;
299 FAIL_IF(ParseSizeString(str, &result) != 0);
300 FAIL_IF(result != 10 * 1024 * 1024);
301
302 str = " 10gb";
303 result = 0;
304 FAIL_IF(ParseSizeString(str, &result) != 0);
305 FAIL_IF(result != 10737418240);
306
307 /* space end */
308
309 str = "10 ";
310 result = 0;
311 FAIL_IF(ParseSizeString(str, &result) != 0);
312 FAIL_IF(result != 10);
313
314 str = "10kb ";
315 result = 0;
316 FAIL_IF(ParseSizeString(str, &result) != 0);
317 FAIL_IF(result != 10 * 1024);
318
319 str = "10Kb ";
320 result = 0;
321 FAIL_IF(ParseSizeString(str, &result) != 0);
322 FAIL_IF(result != 10 * 1024);
323
324 str = "10KB ";
325 result = 0;
326 FAIL_IF(ParseSizeString(str, &result) != 0);
327 FAIL_IF(result != 10 * 1024);
328
329 str = "10mb ";
330 result = 0;
331 FAIL_IF(ParseSizeString(str, &result) != 0);
332 FAIL_IF(result != 10 * 1024 * 1024);
333
334 str = "10gb ";
335 result = 0;
336 FAIL_IF(ParseSizeString(str, &result) != 0);
337 FAIL_IF(result != 10737418240);
338
339 /* space start - space end */
340
341 str = " 10 ";
342 result = 0;
343 FAIL_IF(ParseSizeString(str, &result) != 0);
344 FAIL_IF(result != 10);
345
346 str = " 10kb ";
347 result = 0;
348 FAIL_IF(ParseSizeString(str, &result) != 0);
349 FAIL_IF(result != 10 * 1024);
350
351 str = " 10Kb ";
352 result = 0;
353 FAIL_IF(ParseSizeString(str, &result) != 0);
354 FAIL_IF(result != 10 * 1024);
355
356 str = " 10KB ";
357 result = 0;
358 FAIL_IF(ParseSizeString(str, &result) != 0);
359 FAIL_IF(result != 10 * 1024);
360
361 str = " 10mb ";
362 result = 0;
363 FAIL_IF(ParseSizeString(str, &result) != 0);
364 FAIL_IF(result != 10 * 1024 * 1024);
365
366 str = " 10gb ";
367 result = 0;
368 FAIL_IF(ParseSizeString(str, &result) != 0);
369 FAIL_IF(result != 10737418240);
370
371 /* space between number and scale */
372
373 /* no space */
374
375 str = "10";
376 result = 0;
377 FAIL_IF(ParseSizeString(str, &result) != 0);
378 FAIL_IF(result != 10);
379
380 str = "10 kb";
381 result = 0;
382 FAIL_IF(ParseSizeString(str, &result) != 0);
383 FAIL_IF(result != 10 * 1024);
384
385 str = "10 Kb";
386 result = 0;
387 FAIL_IF(ParseSizeString(str, &result) != 0);
388 FAIL_IF(result != 10 * 1024);
389
390 str = "10 KB";
391 result = 0;
392 FAIL_IF(ParseSizeString(str, &result) != 0);
393 FAIL_IF(result != 10 * 1024);
394
395 str = "10 mb";
396 result = 0;
397 FAIL_IF(ParseSizeString(str, &result) != 0);
398 FAIL_IF(result != 10 * 1024 * 1024);
399
400 str = "10 gb";
401 result = 0;
402 FAIL_IF(ParseSizeString(str, &result) != 0);
403 FAIL_IF(result != 10737418240);
404
405 /* space start */
406
407 str = " 10";
408 result = 0;
409 FAIL_IF(ParseSizeString(str, &result) != 0);
410 FAIL_IF(result != 10);
411
412 str = " 10 kb";
413 result = 0;
414 FAIL_IF(ParseSizeString(str, &result) != 0);
415 FAIL_IF(result != 10 * 1024);
416
417 str = " 10 Kb";
418 result = 0;
419 FAIL_IF(ParseSizeString(str, &result) != 0);
420 FAIL_IF(result != 10 * 1024);
421
422 str = " 10 KB";
423 result = 0;
424 FAIL_IF(ParseSizeString(str, &result) != 0);
425 FAIL_IF(result != 10 * 1024);
426
427 str = " 10 mb";
428 result = 0;
429 FAIL_IF(ParseSizeString(str, &result) != 0);
430 FAIL_IF(result != 10 * 1024 * 1024);
431
432 str = " 10 gb";
433 result = 0;
434 FAIL_IF(ParseSizeString(str, &result) != 0);
435 FAIL_IF(result != 10737418240);
436
437 /* space end */
438
439 str = "10 ";
440 result = 0;
441 FAIL_IF(ParseSizeString(str, &result) != 0);
442 FAIL_IF(result != 10);
443
444 str = "10 kb ";
445 result = 0;
446 FAIL_IF(ParseSizeString(str, &result) != 0);
447 FAIL_IF(result != 10 * 1024);
448
449 str = "10 Kb ";
450 result = 0;
451 FAIL_IF(ParseSizeString(str, &result) != 0);
452 FAIL_IF(result != 10 * 1024);
453
454 str = "10 KB ";
455 result = 0;
456 FAIL_IF(ParseSizeString(str, &result) != 0);
457 FAIL_IF(result != 10 * 1024);
458
459 str = "10 mb ";
460 result = 0;
461 FAIL_IF(ParseSizeString(str, &result) != 0);
462 FAIL_IF(result != 10 * 1024 * 1024);
463
464 str = "10 gb ";
465 result = 0;
466 FAIL_IF(ParseSizeString(str, &result) != 0);
467 FAIL_IF(result != 10737418240);
468
469 /* space start - space end */
470
471 str = " 10 ";
472 result = 0;
473 FAIL_IF(ParseSizeString(str, &result) != 0);
474 FAIL_IF(result != 10);
475
476 str = " 10 kb ";
477 result = 0;
478 FAIL_IF(ParseSizeString(str, &result) != 0);
479 FAIL_IF(result != 10 * 1024);
480
481 str = " 10 Kb ";
482 result = 0;
483 FAIL_IF(ParseSizeString(str, &result) != 0);
484 FAIL_IF(result != 10 * 1024);
485
486 str = " 10 KB ";
487 result = 0;
488 FAIL_IF(ParseSizeString(str, &result) != 0);
489 FAIL_IF(result != 10 * 1024);
490
491 str = " 10 mb ";
492 result = 0;
493 FAIL_IF(ParseSizeString(str, &result) != 0);
494 FAIL_IF(result != 10 * 1024 * 1024);
495
496 str = " 10 gb ";
497 result = 0;
498 FAIL_IF(ParseSizeString(str, &result) != 0);
499 FAIL_IF(result != 10737418240);
500
501 /* no space */
502
503 str = "10.5";
504 result = 0;
505 FAIL_IF(ParseSizeString(str, &result) != 0);
506 FAIL_IF(result != 10.5);
507
508 str = "10.5kb";
509 result = 0;
510 FAIL_IF(ParseSizeString(str, &result) != 0);
511 FAIL_IF(result != 10.5 * 1024);
512
513 str = "10.5Kb";
514 result = 0;
515 FAIL_IF(ParseSizeString(str, &result) != 0);
516 FAIL_IF(result != 10.5 * 1024);
517
518 str = "10.5KB";
519 result = 0;
520 FAIL_IF(ParseSizeString(str, &result) != 0);
521 FAIL_IF(result != 10.5 * 1024);
522
523 str = "10.5mb";
524 result = 0;
525 FAIL_IF(ParseSizeString(str, &result) != 0);
526 FAIL_IF(result != 10.5 * 1024 * 1024);
527
528 str = "10.5gb";
529 result = 0;
530 FAIL_IF(ParseSizeString(str, &result) != 0);
531 FAIL_IF(result != 10.5 * 1024 * 1024 * 1024);
532
533 /* space start */
534
535 str = " 10.5";
536 result = 0;
537 FAIL_IF(ParseSizeString(str, &result) != 0);
538 FAIL_IF(result != 10.5);
539
540 str = " 10.5kb";
541 result = 0;
542 FAIL_IF(ParseSizeString(str, &result) != 0);
543 FAIL_IF(result != 10.5 * 1024);
544
545 str = " 10.5Kb";
546 result = 0;
547 FAIL_IF(ParseSizeString(str, &result) != 0);
548 FAIL_IF(result != 10.5 * 1024);
549
550 str = " 10.5KB";
551 result = 0;
552 FAIL_IF(ParseSizeString(str, &result) != 0);
553 FAIL_IF(result != 10.5 * 1024);
554
555 str = " 10.5mb";
556 result = 0;
557 FAIL_IF(ParseSizeString(str, &result) != 0);
558 FAIL_IF(result != 10.5 * 1024 * 1024);
559
560 str = " 10.5gb";
561 result = 0;
562 FAIL_IF(ParseSizeString(str, &result) != 0);
563 FAIL_IF(result != 10.5 * 1024 * 1024 * 1024);
564
565 /* space end */
566
567 str = "10.5 ";
568 result = 0;
569 FAIL_IF(ParseSizeString(str, &result) != 0);
570 FAIL_IF(result != 10.5);
571
572 str = "10.5kb ";
573 result = 0;
574 FAIL_IF(ParseSizeString(str, &result) != 0);
575 FAIL_IF(result != 10.5 * 1024);
576
577 str = "10.5Kb ";
578 result = 0;
579 FAIL_IF(ParseSizeString(str, &result) != 0);
580 FAIL_IF(result != 10.5 * 1024);
581
582 str = "10.5KB ";
583 result = 0;
584 FAIL_IF(ParseSizeString(str, &result) != 0);
585 FAIL_IF(result != 10.5 * 1024);
586
587 str = "10.5mb ";
588 result = 0;
589 FAIL_IF(ParseSizeString(str, &result) != 0);
590 FAIL_IF(result != 10.5 * 1024 * 1024);
591
592 str = "10.5gb ";
593 result = 0;
594 FAIL_IF(ParseSizeString(str, &result) != 0);
595 FAIL_IF(result != 10.5 * 1024 * 1024 * 1024);
596
597 /* space start - space end */
598
599 str = " 10.5 ";
600 result = 0;
601 FAIL_IF(ParseSizeString(str, &result) != 0);
602 FAIL_IF(result != 10.5);
603
604 str = " 10.5kb ";
605 result = 0;
606 FAIL_IF(ParseSizeString(str, &result) != 0);
607 FAIL_IF(result != 10.5 * 1024);
608
609 str = " 10.5Kb ";
610 result = 0;
611 FAIL_IF(ParseSizeString(str, &result) != 0);
612 FAIL_IF(result != 10.5 * 1024);
613
614 str = " 10.5KB ";
615 result = 0;
616 FAIL_IF(ParseSizeString(str, &result) != 0);
617 FAIL_IF(result != 10.5 * 1024);
618
619 str = " 10.5mb ";
620 result = 0;
621 FAIL_IF(ParseSizeString(str, &result) != 0);
622 FAIL_IF(result != 10.5 * 1024 * 1024);
623
624 str = " 10.5gb ";
625 result = 0;
626 FAIL_IF(ParseSizeString(str, &result) != 0);
627 FAIL_IF(result != 10.5 * 1024 * 1024 * 1024);
628
629 /* space between number and scale */
630
631 /* no space */
632
633 str = "10.5";
634 result = 0;
635 FAIL_IF(ParseSizeString(str, &result) != 0);
636 FAIL_IF(result != 10.5);
637
638 str = "10.5 kb";
639 result = 0;
640 FAIL_IF(ParseSizeString(str, &result) != 0);
641 FAIL_IF(result != 10.5 * 1024);
642
643 str = "10.5 Kb";
644 result = 0;
645 FAIL_IF(ParseSizeString(str, &result) != 0);
646 FAIL_IF(result != 10.5 * 1024);
647
648 str = "10.5 KB";
649 result = 0;
650 FAIL_IF(ParseSizeString(str, &result) != 0);
651 FAIL_IF(result != 10.5 * 1024);
652
653 str = "10.5 mb";
654 result = 0;
655 FAIL_IF(ParseSizeString(str, &result) != 0);
656 FAIL_IF(result != 10.5 * 1024 * 1024);
657
658 str = "10.5 gb";
659 result = 0;
660 FAIL_IF(ParseSizeString(str, &result) != 0);
661 FAIL_IF(result != 10.5 * 1024 * 1024 * 1024);
662
663 /* space start */
664
665 str = " 10.5";
666 result = 0;
667 FAIL_IF(ParseSizeString(str, &result) != 0);
668 FAIL_IF(result != 10.5);
669
670 str = " 10.5 kb";
671 result = 0;
672 FAIL_IF(ParseSizeString(str, &result) != 0);
673 FAIL_IF(result != 10.5 * 1024);
674
675 str = " 10.5 Kb";
676 result = 0;
677 FAIL_IF(ParseSizeString(str, &result) != 0);
678 FAIL_IF(result != 10.5 * 1024);
679
680 str = " 10.5 KB";
681 result = 0;
682 FAIL_IF(ParseSizeString(str, &result) != 0);
683 FAIL_IF(result != 10.5 * 1024);
684
685 str = " 10.5 mb";
686 result = 0;
687 FAIL_IF(ParseSizeString(str, &result) != 0);
688 FAIL_IF(result != 10.5 * 1024 * 1024);
689
690 str = " 10.5 gb";
691 result = 0;
692 FAIL_IF(ParseSizeString(str, &result) != 0);
693 FAIL_IF(result != 10.5 * 1024 * 1024 * 1024);
694
695 /* space end */
696
697 str = "10.5 ";
698 result = 0;
699 FAIL_IF(ParseSizeString(str, &result) != 0);
700 FAIL_IF(result != 10.5);
701
702 str = "10.5 kb ";
703 result = 0;
704 FAIL_IF(ParseSizeString(str, &result) != 0);
705 FAIL_IF(result != 10.5 * 1024);
706
707 str = "10.5 Kb ";
708 result = 0;
709 FAIL_IF(ParseSizeString(str, &result) != 0);
710 FAIL_IF(result != 10.5 * 1024);
711
712 str = "10.5 KB ";
713 result = 0;
714 FAIL_IF(ParseSizeString(str, &result) != 0);
715 FAIL_IF(result != 10.5 * 1024);
716
717 str = "10.5 mb ";
718 result = 0;
719 FAIL_IF(ParseSizeString(str, &result) != 0);
720 FAIL_IF(result != 10.5 * 1024 * 1024);
721
722 str = "10.5 gb ";
723 result = 0;
724 FAIL_IF(ParseSizeString(str, &result) != 0);
725 FAIL_IF(result != 10.5 * 1024 * 1024 * 1024);
726
727 /* space start - space end */
728
729 str = " 10.5 ";
730 result = 0;
731 FAIL_IF(ParseSizeString(str, &result) != 0);
732 FAIL_IF(result != 10.5);
733
734 str = " 10.5 kb ";
735 result = 0;
736 FAIL_IF(ParseSizeString(str, &result) != 0);
737 FAIL_IF(result != 10.5 * 1024);
738
739 str = " 10.5 Kb ";
740 result = 0;
741 FAIL_IF(ParseSizeString(str, &result) != 0);
742 FAIL_IF(result != 10.5 * 1024);
743
744 str = " 10.5 KB ";
745 result = 0;
746 FAIL_IF(ParseSizeString(str, &result) != 0);
747 FAIL_IF(result != 10.5 * 1024);
748
749 str = " 10.5 mb ";
750 result = 0;
751 FAIL_IF(ParseSizeString(str, &result) != 0);
752 FAIL_IF(result != 10.5 * 1024 * 1024);
753
754 str = " 10.5 gb ";
755 result = 0;
756 FAIL_IF(ParseSizeString(str, &result) != 0);
757 FAIL_IF(result != 10.5 * 1024 * 1024 * 1024);
758
759 /* Should fail on unknown units. */
760 FAIL_IF(ParseSizeString("32eb", &result) == 0);
761
762 PASS;
763}
764
765static int UtilMiscParseSizeStringTest02(void)
766{
767 const char *str;
768 double result;
769
770 str = "10kib";
771 result = 0;
772 FAIL_IF_NOT(ParseSizeString(str, &result) == -1);
773
774 str = "10Kib";
775 result = 0;
776 FAIL_IF_NOT(ParseSizeString(str, &result) == -1);
777
778 str = "10KiB";
779 result = 0;
780 FAIL_IF_NOT(ParseSizeString(str, &result) == 0);
781 FAIL_IF(result != 10 * 1024);
782
783 str = "10mib";
784 result = 0;
785 FAIL_IF_NOT(ParseSizeString(str, &result) == -1);
786
787 str = "10gib";
788 result = 0;
789 FAIL_IF_NOT(ParseSizeString(str, &result) == -1);
790
791 str = " 10.5 KiB ";
792 result = 0;
793 FAIL_IF_NOT(ParseSizeString(str, &result) == 0);
794 FAIL_IF(result != 10.5 * 1024);
795
796 str = " 10.5 MiB ";
797 result = 0;
798 FAIL_IF_NOT(ParseSizeString(str, &result) == 0);
799 FAIL_IF(result != 10.5 * 1024 * 1024);
800
801 str = " 10.5 GiB ";
802 result = 0;
803 FAIL_IF_NOT(ParseSizeString(str, &result) == 0);
804 FAIL_IF(result != 10.5 * 1024 * 1024 * 1024);
805
806 PASS;
807}
808
810{
811 UtRegisterTest("UtilMiscParseSizeStringTest01",
812 UtilMiscParseSizeStringTest01);
813 UtRegisterTest("UtilMiscParseSizeStringTest02", UtilMiscParseSizeStringTest02);
814}
815#endif /* UNITTESTS */
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
#define PASS
Pass the test.
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
size_t strlcat(char *, const char *src, size_t siz)
#define str(s)
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267
int ParseSizeStringU32(const char *size, uint32_t *res)
Definition util-misc.c:173
void UtilMiscRegisterTests(void)
Definition util-misc.c:809
void ParseSizeDeinit(void)
Definition util-misc.c:54
void ShortenString(const char *input, char *output, size_t output_size, char c)
Definition util-misc.c:207
int ParseSizeStringU8(const char *size, uint8_t *res)
Definition util-misc.c:139
int ParseSizeStringU16(const char *size, uint16_t *res)
Definition util-misc.c:156
int ParseSizeStringU64(const char *size, uint64_t *res)
Definition util-misc.c:190
#define PARSE_REGEX
Definition util-misc.c:31
void ParseSizeInit(void)
Definition util-misc.c:35