aboutsummaryrefslogtreecommitdiff
path: root/src/xzdec/xzdec.c
diff options
context:
space:
mode:
authorLasse Collin <lasse.collin@tukaani.org>2010-03-07 13:59:32 +0200
committerLasse Collin <lasse.collin@tukaani.org>2010-03-07 13:59:32 +0200
commitcf38da00a140bd3bd65b192390ae5553380fd774 (patch)
tree5e696ca7ccbbf8052cc218e2ee41f32490388537 /src/xzdec/xzdec.c
parentConsistently round up the memory usage limit in messages. (diff)
downloadxz-cf38da00a140bd3bd65b192390ae5553380fd774.tar.xz
Treat all integer multiplier suffixes as base-2.
Originally both base-2 and base-10 were supported, but since there seems to be little need for base-10 in XZ Utils, treat everything as base-2 and also be more relaxed about the case of the first letter of the suffix. Now xz will accept e.g. KiB, Ki, k, K, kB, and KB, and interpret them all as 1024. The recommended spelling of the suffixes are still KiB, MiB, and GiB.
Diffstat (limited to '')
-rw-r--r--src/xzdec/xzdec.c42
1 files changed, 16 insertions, 26 deletions
diff --git a/src/xzdec/xzdec.c b/src/xzdec/xzdec.c
index 3b1ab0f1..8518d362 100644
--- a/src/xzdec/xzdec.c
+++ b/src/xzdec/xzdec.c
@@ -183,34 +183,24 @@ str_to_uint64(const char *value, uint64_t max)
if (*value != '\0') {
// Look for suffix.
- static const struct {
- const char name[4];
- uint32_t multiplier;
- } suffixes[] = {
- { "k", 1000 },
- { "kB", 1000 },
- { "M", 1000000 },
- { "MB", 1000000 },
- { "G", 1000000000 },
- { "GB", 1000000000 },
- { "Ki", 1024 },
- { "KiB", 1024 },
- { "Mi", 1048576 },
- { "MiB", 1048576 },
- { "Gi", 1073741824 },
- { "GiB", 1073741824 }
- };
-
- uint32_t multiplier = 0;
- for (size_t i = 0; i < ARRAY_SIZE(suffixes); ++i) {
- if (strcmp(value, suffixes[i].name) == 0) {
- multiplier = suffixes[i].multiplier;
- break;
- }
- }
+ uint64_t multiplier = 0;
+ if (*value == 'k' || *value == 'K')
+ multiplier = UINT64_C(1) << 10;
+ else if (*value == 'm' || *value == 'M')
+ multiplier = UINT64_C(1) << 20;
+ else if (*value == 'g' || *value == 'G')
+ multiplier = UINT64_C(1) << 30;
+
+ ++value;
+
+ // Allow also e.g. Ki, KiB, and KB.
+ if (*value != '\0' && strcmp(value, "i") != 0
+ && strcmp(value, "iB") != 0
+ && strcmp(value, "B") != 0)
+ multiplier = 0;
if (multiplier == 0) {
- my_errorf("%s: Invalid suffix", value);
+ my_errorf("%s: Invalid suffix", value - 1);
exit(EXIT_FAILURE);
}