aboutsummaryrefslogtreecommitdiff
path: root/lib/common
diff options
context:
space:
mode:
Diffstat (limited to 'lib/common')
-rw-r--r--lib/common/Makemodule.am2
-rw-r--r--lib/common/parse_size.c72
2 files changed, 73 insertions, 1 deletions
diff --git a/lib/common/Makemodule.am b/lib/common/Makemodule.am
index 635c342..ac1a4e9 100644
--- a/lib/common/Makemodule.am
+++ b/lib/common/Makemodule.am
@@ -5,7 +5,7 @@ libcommon_a_SOURCES += lib/common/compress.c lib/common/comp_opt.c
libcommon_a_SOURCES += lib/common/data_writer.c include/common.h
libcommon_a_SOURCES += lib/common/get_path.c lib/common/io_stdin.c
libcommon_a_SOURCES += lib/common/writer.c lib/common/perror.c
-libcommon_a_SOURCES += lib/common/mkdir_p.c
+libcommon_a_SOURCES += lib/common/mkdir_p.c lib/common/parse_size.c
libcommon_a_CFLAGS = $(AM_CFLAGS) $(LZO_CFLAGS)
if WITH_LZO
diff --git a/lib/common/parse_size.c b/lib/common/parse_size.c
new file mode 100644
index 0000000..aede38a
--- /dev/null
+++ b/lib/common/parse_size.c
@@ -0,0 +1,72 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * parse_size.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "common.h"
+
+#include <ctype.h>
+#include <limits.h>
+
+int parse_size(const char *what, size_t *out, const char *str)
+{
+ const char *in = str;
+ size_t acc = 0, x;
+
+ if (!isdigit(*in))
+ goto fail_nan;
+
+ while (isdigit(*in)) {
+ x = *(in++) - '0';
+
+ if (SZ_MUL_OV(acc, 10, &acc))
+ goto fail_ov;
+
+ if (SZ_ADD_OV(acc, x, &acc))
+ goto fail_ov;
+ }
+
+ switch (*in) {
+ case 'k':
+ case 'K':
+ if (acc > ((1UL << (SIZEOF_SIZE_T * CHAR_BIT - 10)) - 1UL))
+ goto fail_ov;
+ acc <<= 10;
+ ++in;
+ break;
+ case 'm':
+ case 'M':
+ if (acc > ((1UL << (SIZEOF_SIZE_T * CHAR_BIT - 20)) - 1UL))
+ goto fail_ov;
+ acc <<= 20;
+ ++in;
+ break;
+ case 'g':
+ case 'G':
+ if (acc > ((1UL << (SIZEOF_SIZE_T * CHAR_BIT - 30)) - 1UL))
+ goto fail_ov;
+ acc <<= 30;
+ ++in;
+ break;
+ case '\0':
+ break;
+ default:
+ goto fail_suffix;
+ }
+
+ if (*in != '\0')
+ goto fail_suffix;
+
+ *out = acc;
+ return 0;
+fail_nan:
+ fprintf(stderr, "%s: '%s' is not a number.\n", what, str);
+ return -1;
+fail_ov:
+ fprintf(stderr, "%s: numeric overflow parsing '%s'.\n", what, str);
+ return -1;
+fail_suffix:
+ fprintf(stderr, "%s: unknown suffix in '%s'.\n", what, str);
+ return -1;
+}