32 #define PARSE_REGEX "^\\s*(\\d+(?:.\\d+)?)\\s*([a-zA-Z]{2,3})?\\s*$"
33 static pcre2_code *parse_regex = NULL;
34 static pcre2_match_data *parse_regex_match = NULL;
43 pcre2_compile((PCRE2_SPTR8)
PARSE_REGEX, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
44 if (parse_regex == NULL) {
45 PCRE2_UCHAR errbuffer[256];
46 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
47 SCLogError(
"pcre2 compile of \"%s\" failed at "
52 parse_regex_match = pcre2_match_data_create_from_pattern(parse_regex, NULL);
57 pcre2_code_free(parse_regex);
58 pcre2_match_data_free(parse_regex_match);
63 static int ParseSizeString(
const char *size,
double *res)
74 SCLogError(
"invalid size argument: NULL. Valid input is <number><unit>. Unit can be "
75 "kb/KiB, mb/MiB or gb/GiB");
80 pcre2_match_ret = pcre2_match(
81 parse_regex, (PCRE2_SPTR8)size, strlen(size), 0, 0, parse_regex_match, NULL);
83 if (!(pcre2_match_ret == 2 || pcre2_match_ret == 3)) {
84 SCLogError(
"invalid size argument: '%s'. Valid input is <number><unit>. Unit can be "
85 "kb/KiB, mb/MiB or gb/GiB",
91 size_t copylen =
sizeof(
str);
92 r = pcre2_substring_copy_bynumber(parse_regex_match, 1, (PCRE2_UCHAR8 *)
str, ©len);
94 SCLogError(
"pcre2_substring_copy_bynumber failed");
99 char *endptr, *str_ptr =
str;
101 *res = strtod(str_ptr, &endptr);
102 if (errno == ERANGE) {
106 }
else if (endptr == str_ptr) {
112 if (pcre2_match_ret == 3) {
113 copylen =
sizeof(str2);
114 r = pcre2_substring_copy_bynumber(parse_regex_match, 2, (PCRE2_UCHAR8 *)str2, ©len);
117 SCLogError(
"pcre2_substring_copy_bynumber failed");
122 if (strcasecmp(str2,
"kb") == 0 || strcmp(str2,
"KiB") == 0) {
124 }
else if (strcasecmp(str2,
"mb") == 0 || strcmp(str2,
"MiB") == 0) {
126 }
else if (strcasecmp(str2,
"gb") == 0 || strcmp(str2,
"GiB") == 0) {
127 *res *= 1024 * 1024 * 1024;
145 int r = ParseSizeString(size, &temp_res);
149 if (temp_res > UINT8_MAX)
162 int r = ParseSizeString(size, &temp_res);
166 if (temp_res > UINT16_MAX)
179 int r = ParseSizeString(size, &temp_res);
183 if (temp_res > UINT32_MAX)
196 int r = ParseSizeString(size, &temp_res);
200 if (temp_res > (
double) UINT64_MAX)
209 char *output,
size_t output_size,
char c)
211 if (output_size == 0)
214 const size_t str_len = strlen(input);
215 size_t half = (output_size - 1) / 2;
218 if (half * 2 == (output_size - 1)) {
222 if (half == 0 || half > output_size)
225 size_t spaces = (output_size - 1) - (half * 2);
228 snprintf(output, half+1,
"%s", input);
231 size_t length = half;
232 for (
size_t i = half; i < half + spaces; i++) {
234 snprintf(s,
sizeof(s),
"%c", c);
235 length =
strlcat(output, s, output_size);
238 snprintf(output + length, half + 1,
"%s", input + (str_len - half));
245 static int UtilMiscParseSizeStringTest01(
void)
772 static int UtilMiscParseSizeStringTest02(
void)
819 UtilMiscParseSizeStringTest01);
820 UtRegisterTest(
"UtilMiscParseSizeStringTest02", UtilMiscParseSizeStringTest02);