]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
parse/size: support IEEE 1541 size units
authorVictor Julien <vjulien@oisf.net>
Wed, 10 Jul 2024 09:49:58 +0000 (11:49 +0200)
committerVictor Julien <victor@inliniac.net>
Fri, 12 Jul 2024 08:47:50 +0000 (10:47 +0200)
Introduce KiB, MiB and GiB. They are case sensitive as a lower case 'b'
means bits in the IEEE 1541 scheme.

KiB = 1024
MiB = 1048576
GiB = 1073741824

Ticket: #1457.

src/util-misc.c

index 4ed00575e61003be20dffedd95726c6f039dad60..f83b50454e49fb3d424c4de5d5e3bad29d0529d8 100644 (file)
@@ -28,7 +28,7 @@
 #include "util-unittest.h"
 #include "util-misc.h"
 
-#define PARSE_REGEX "^\\s*(\\d+(?:.\\d+)?)\\s*([a-zA-Z]{2})?\\s*$"
+#define PARSE_REGEX "^\\s*(\\d+(?:.\\d+)?)\\s*([a-zA-Z]{2,3})?\\s*$"
 static pcre2_code *parse_regex = NULL;
 static pcre2_match_data *parse_regex_match = NULL;
 
@@ -70,12 +70,8 @@ static int ParseSizeString(const char *size, double *res)
     *res = 0;
 
     if (size == NULL) {
-        SCLogError("invalid size argument - NULL. Valid size "
-                   "argument should be in the format - \n"
-                   "xxx <- indicates it is just bytes\n"
-                   "xxxkb or xxxKb or xxxKB or xxxkB <- indicates kilobytes\n"
-                   "xxxmb or xxxMb or xxxMB or xxxmB <- indicates megabytes\n"
-                   "xxxgb or xxxGb or xxxGB or xxxgB <- indicates gigabytes.\n");
+        SCLogError("invalid size argument: NULL. Valid input is <number><unit>. Unit can be "
+                   "kb/KiB, mb/MiB or gb/GiB");
         retval = -2;
         goto end;
     }
@@ -84,12 +80,8 @@ static int ParseSizeString(const char *size, double *res)
             parse_regex, (PCRE2_SPTR8)size, strlen(size), 0, 0, parse_regex_match, NULL);
 
     if (!(pcre2_match_ret == 2 || pcre2_match_ret == 3)) {
-        SCLogError("invalid size argument - %s. Valid size "
-                   "argument should be in the format - \n"
-                   "xxx <- indicates it is just bytes\n"
-                   "xxxkb or xxxKb or xxxKB or xxxkB <- indicates kilobytes\n"
-                   "xxxmb or xxxMb or xxxMB or xxxmB <- indicates megabytes\n"
-                   "xxxgb or xxxGb or xxxGB or xxxgB <- indicates gigabytes.\n",
+        SCLogError("invalid size argument: '%s'. Valid input is <number><unit>. Unit can be "
+                   "kb/KiB, mb/MiB or gb/GiB",
                 size);
         retval = -2;
         goto end;
@@ -126,11 +118,11 @@ static int ParseSizeString(const char *size, double *res)
             goto end;
         }
 
-        if (strcasecmp(str2, "kb") == 0) {
+        if (strcasecmp(str2, "kb") == 0 || strcmp(str2, "KiB") == 0) {
             *res *= 1024;
-        } else if (strcasecmp(str2, "mb") == 0) {
+        } else if (strcasecmp(str2, "mb") == 0 || strcmp(str2, "MiB") == 0) {
             *res *= 1024 * 1024;
-        } else if (strcasecmp(str2, "gb") == 0) {
+        } else if (strcasecmp(str2, "gb") == 0 || strcmp(str2, "GiB") == 0) {
             *res *= 1024 * 1024 * 1024;
         } else {
             /* Bad unit. */
@@ -770,9 +762,54 @@ static int UtilMiscParseSizeStringTest01(void)
     PASS;
 }
 
+static int UtilMiscParseSizeStringTest02(void)
+{
+    const char *str;
+    double result;
+
+    str = "10kib";
+    result = 0;
+    FAIL_IF_NOT(ParseSizeString(str, &result) == -1);
+
+    str = "10Kib";
+    result = 0;
+    FAIL_IF_NOT(ParseSizeString(str, &result) == -1);
+
+    str = "10KiB";
+    result = 0;
+    FAIL_IF_NOT(ParseSizeString(str, &result) == 0);
+    FAIL_IF(result != 10 * 1024);
+
+    str = "10mib";
+    result = 0;
+    FAIL_IF_NOT(ParseSizeString(str, &result) == -1);
+
+    str = "10gib";
+    result = 0;
+    FAIL_IF_NOT(ParseSizeString(str, &result) == -1);
+
+    str = " 10.5 KiB ";
+    result = 0;
+    FAIL_IF_NOT(ParseSizeString(str, &result) == 0);
+    FAIL_IF(result != 10.5 * 1024);
+
+    str = " 10.5 MiB ";
+    result = 0;
+    FAIL_IF_NOT(ParseSizeString(str, &result) == 0);
+    FAIL_IF(result != 10.5 * 1024 * 1024);
+
+    str = " 10.5 GiB ";
+    result = 0;
+    FAIL_IF_NOT(ParseSizeString(str, &result) == 0);
+    FAIL_IF(result != 10.5 * 1024 * 1024 * 1024);
+
+    PASS;
+}
+
 void UtilMiscRegisterTests(void)
 {
     UtRegisterTest("UtilMiscParseSizeStringTest01",
                    UtilMiscParseSizeStringTest01);
+    UtRegisterTest("UtilMiscParseSizeStringTest02", UtilMiscParseSizeStringTest02);
 }
 #endif /* UNITTESTS */