#include "util-debug.h"
#include "util-unittest.h"
-/* size string parsing API */
-
-static int ParseSizeString(const char *size, double *res)
-{
#define PARSE_REGEX "^\\s*(\\d+(?:.\\d+)?)\\s*([a-zA-Z]{2})?\\s*$"
+static pcre *parse_regex = NULL;
+static pcre_extra *parse_regex_study = NULL;
- pcre *parse_regex;
- pcre_extra *parse_regex_study;
+void ParseSizeInit(void) {
const char *eb;
int eo;
int opts = 0;
-#define MAX_SUBSTRINGS 30
- int pcre_exec_ret;
- int r;
- int ov[MAX_SUBSTRINGS];
- int retval = 0;
-
- *res = 0;
parse_regex = pcre_compile(PARSE_REGEX, opts, &eb, &eo, NULL);
if (parse_regex == NULL) {
SCLogError(SC_ERR_PCRE_COMPILE, "Compile of \"%s\" failed at offset "
"%" PRId32 ": %s", PARSE_REGEX, eo, eb);
- retval = -2;
- goto end;
+ exit(EXIT_FAILURE);
}
-
parse_regex_study = pcre_study(parse_regex, 0, &eb);
if (eb != NULL) {
SCLogError(SC_ERR_PCRE_STUDY, "pcre study failed: %s", eb);
- retval = -2;
- goto end;
+ exit(EXIT_FAILURE);
}
+}
+
+void ParseSizeDeinit(void) {
+
+ if (parse_regex != NULL)
+ pcre_free(parse_regex);
+ if (parse_regex_study != NULL)
+ pcre_free_study(parse_regex_study);
+}
+
+/* size string parsing API */
+
+static int ParseSizeString(const char *size, double *res)
+{
+#define MAX_SUBSTRINGS 30
+ int pcre_exec_ret;
+ int r;
+ int ov[MAX_SUBSTRINGS];
+ int retval = 0;
+ const char *str_ptr = NULL;
+ const char *str_ptr2 = NULL;
+
+ *res = 0;
pcre_exec_ret = pcre_exec(parse_regex, parse_regex_study, size, strlen(size), 0, 0,
ov, MAX_SUBSTRINGS);
goto end;
}
- const char *str_ptr;
r = pcre_get_substring((char *)size, ov, MAX_SUBSTRINGS, 1,
&str_ptr);
if (r < 0) {
retval = -1;
goto end;
}
- pcre_free_substring(str_ptr);
if (pcre_exec_ret == 3) {
r = pcre_get_substring((char *)size, ov, MAX_SUBSTRINGS, 2,
- &str_ptr);
+ &str_ptr2);
if (r < 0) {
SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
retval = -2;
goto end;
}
- if (strcasecmp(str_ptr, "kb") == 0) {
+ if (strcasecmp(str_ptr2, "kb") == 0) {
*res *= 1024;
- } else if (strcasecmp(str_ptr, "mb") == 0) {
+ } else if (strcasecmp(str_ptr2, "mb") == 0) {
*res *= 1024 * 1024;
- } else if (strcasecmp(str_ptr, "gb") == 0) {
+ } else if (strcasecmp(str_ptr2, "gb") == 0) {
*res *= 1024 * 1024 * 1024;
} else {
/* not possible */
retval = 0;
end:
+ if (str_ptr != NULL)
+ pcre_free_substring(str_ptr);
+ if (str_ptr2 != NULL)
+ pcre_free_substring(str_ptr2);
return retval;
}