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)
776 UtilMiscParseSizeStringTest01);