From d053eb8096a790d81b67c844d918341e797c2659 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Mon, 6 Jan 2020 03:28:28 +0100 Subject: Cleanup: use parse_size function to parse compressor options The XZ option parser had a similar function to parse_size. This commit removes the other implementation and extends parse_size with the one missing feature, i.e. allowing a '%' suffix for a relative value. Signed-off-by: David Oberhollenzer --- lib/common/parse_size.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'lib/common/parse_size.c') diff --git a/lib/common/parse_size.c b/lib/common/parse_size.c index aede38a..1285d3b 100644 --- a/lib/common/parse_size.c +++ b/lib/common/parse_size.c @@ -9,7 +9,8 @@ #include #include -int parse_size(const char *what, size_t *out, const char *str) +int parse_size(const char *what, size_t *out, const char *str, + size_t reference) { const char *in = str; size_t acc = 0, x; @@ -49,6 +50,15 @@ int parse_size(const char *what, size_t *out, const char *str) acc <<= 30; ++in; break; + case '%': + if (reference == 0) + goto fail_suffix; + + if (SZ_MUL_OV(acc, reference, &acc)) + goto fail_ov; + + acc /= 100; + break; case '\0': break; default: -- cgit v1.2.3