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;
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 "
51 parse_regex_match = pcre2_match_data_create_from_pattern(parse_regex, NULL);
56 pcre2_code_free(parse_regex);
57 pcre2_match_data_free(parse_regex_match);
62 static int ParseSizeString(
const char *size,
double *res)
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");
83 pcre2_match_ret = pcre2_match(
84 parse_regex, (PCRE2_SPTR8)size, strlen(size), 0, 0, parse_regex_match, NULL);
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",
98 size_t copylen =
sizeof(
str);
99 r = pcre2_substring_copy_bynumber(parse_regex_match, 1, (PCRE2_UCHAR8 *)
str, ©len);
101 SCLogError(
"pcre2_substring_copy_bynumber failed");
106 char *endptr, *str_ptr =
str;
108 *res = strtod(str_ptr, &endptr);
109 if (errno == ERANGE) {
113 }
else if (endptr == str_ptr) {
119 if (pcre2_match_ret == 3) {
120 copylen =
sizeof(str2);
121 r = pcre2_substring_copy_bynumber(parse_regex_match, 2, (PCRE2_UCHAR8 *)str2, ©len);
124 SCLogError(
"pcre2_substring_copy_bynumber failed");
129 if (strcasecmp(str2,
"kb") == 0) {
131 }
else if (strcasecmp(str2,
"mb") == 0) {
133 }
else if (strcasecmp(str2,
"gb") == 0) {
134 *res *= 1024 * 1024 * 1024;
152 int r = ParseSizeString(size, &temp_res);
156 if (temp_res > UINT8_MAX)
169 int r = ParseSizeString(size, &temp_res);
173 if (temp_res > UINT16_MAX)
186 int r = ParseSizeString(size, &temp_res);
190 if (temp_res > UINT32_MAX)
203 int r = ParseSizeString(size, &temp_res);
207 if (temp_res > (
double) UINT64_MAX)
216 char *output,
size_t output_size,
char c)
218 const size_t str_len = strlen(input);
219 size_t half = (output_size - 1) / 2;
222 if (half * 2 == (output_size - 1)) {
226 size_t spaces = (output_size - 1) - (half * 2);
229 snprintf(output, half+1,
"%s", input);
232 size_t length = half;
233 for (
size_t i = half; i < half + spaces; i++) {
235 snprintf(s,
sizeof(s),
"%c", c);
236 length =
strlcat(output, s, output_size);
239 snprintf(output + length, half + 1,
"%s", input + (str_len - half));
246 static int UtilMiscParseSizeStringTest01(
void)
267 if (
result != 10 * 1024) {
276 if (
result != 10 * 1024) {
285 if (
result != 10 * 1024) {
294 if (
result != 10 * 1024 * 1024) {
303 if (
result != 10737418240UL) {
324 if (
result != 10 * 1024) {
333 if (
result != 10 * 1024) {
342 if (
result != 10 * 1024) {
351 if (
result != 10 * 1024 * 1024) {
360 if (
result != 10737418240) {
380 if (
result != 10 * 1024) {
389 if (
result != 10 * 1024) {
398 if (
result != 10 * 1024) {
407 if (
result != 10 * 1024 * 1024) {
416 if (
result != 10737418240) {
436 if (
result != 10 * 1024) {
445 if (
result != 10 * 1024) {
454 if (
result != 10 * 1024) {
463 if (
result != 10 * 1024 * 1024) {
472 if (
result != 10737418240) {
495 if (
result != 10 * 1024) {
504 if (
result != 10 * 1024) {
513 if (
result != 10 * 1024) {
522 if (
result != 10 * 1024 * 1024) {
531 if (
result != 10737418240) {
552 if (
result != 10 * 1024) {
561 if (
result != 10 * 1024) {
570 if (
result != 10 * 1024) {
579 if (
result != 10 * 1024 * 1024) {
588 if (
result != 10737418240) {
608 if (
result != 10 * 1024) {
617 if (
result != 10 * 1024) {
626 if (
result != 10 * 1024) {
635 if (
result != 10 * 1024 * 1024) {
644 if (
result != 10737418240) {
664 if (
result != 10 * 1024) {
673 if (
result != 10 * 1024) {
682 if (
result != 10 * 1024) {
691 if (
result != 10 * 1024 * 1024) {
700 if (
result != 10737418240) {
720 if (
result != 10.5 * 1024) {
729 if (
result != 10.5 * 1024) {
738 if (
result != 10.5 * 1024) {
747 if (
result != 10.5 * 1024 * 1024) {
756 if (
result != 10.5 * 1024 * 1024 * 1024) {
777 if (
result != 10.5 * 1024) {
786 if (
result != 10.5 * 1024) {
795 if (
result != 10.5 * 1024) {
804 if (
result != 10.5 * 1024 * 1024) {
813 if (
result != 10.5 * 1024 * 1024 * 1024) {
833 if (
result != 10.5 * 1024) {
842 if (
result != 10.5 * 1024) {
851 if (
result != 10.5 * 1024) {
860 if (
result != 10.5 * 1024 * 1024) {
869 if (
result != 10.5 * 1024 * 1024 * 1024) {
889 if (
result != 10.5 * 1024) {
898 if (
result != 10.5 * 1024) {
907 if (
result != 10.5 * 1024) {
916 if (
result != 10.5 * 1024 * 1024) {
925 if (
result != 10.5 * 1024 * 1024 * 1024) {
948 if (
result != 10.5 * 1024) {
957 if (
result != 10.5 * 1024) {
966 if (
result != 10.5 * 1024) {
975 if (
result != 10.5 * 1024 * 1024) {
984 if (
result != 10.5 * 1024 * 1024 * 1024) {
1002 if (ParseSizeString(
str, &
result) > 0) {
1005 if (
result != 10.5 * 1024) {
1011 if (ParseSizeString(
str, &
result) > 0) {
1014 if (
result != 10.5 * 1024) {
1020 if (ParseSizeString(
str, &
result) > 0) {
1023 if (
result != 10.5 * 1024) {
1029 if (ParseSizeString(
str, &
result) > 0) {
1032 if (
result != 10.5 * 1024 * 1024) {
1038 if (ParseSizeString(
str, &
result) > 0) {
1041 if (
result != 10.5 * 1024 * 1024 * 1024) {
1049 if (ParseSizeString(
str, &
result) > 0) {
1058 if (ParseSizeString(
str, &
result) > 0) {
1061 if (
result != 10.5 * 1024) {
1067 if (ParseSizeString(
str, &
result) > 0) {
1070 if (
result != 10.5 * 1024) {
1076 if (ParseSizeString(
str, &
result) > 0) {
1079 if (
result != 10.5 * 1024) {
1085 if (ParseSizeString(
str, &
result) > 0) {
1088 if (
result != 10.5 * 1024 * 1024) {
1094 if (ParseSizeString(
str, &
result) > 0) {
1097 if (
result != 10.5 * 1024 * 1024 * 1024) {
1105 if (ParseSizeString(
str, &
result) > 0) {
1114 if (ParseSizeString(
str, &
result) > 0) {
1117 if (
result != 10.5 * 1024) {
1123 if (ParseSizeString(
str, &
result) > 0) {
1126 if (
result != 10.5 * 1024) {
1132 if (ParseSizeString(
str, &
result) > 0) {
1135 if (
result != 10.5 * 1024) {
1141 if (ParseSizeString(
str, &
result) > 0) {
1144 if (
result != 10.5 * 1024 * 1024) {
1150 if (ParseSizeString(
str, &
result) > 0) {
1153 if (
result != 10.5 * 1024 * 1024 * 1024) {
1158 if (ParseSizeString(
"32eb", &
result) > 0) {
1170 UtilMiscParseSizeStringTest01);