mirror of https://git.tukaani.org/xz.git
Improve command line integer parsing a little in lzma and
lzmadec to make them accept also KiB in addition Ki etc. Fix also memory usage information in lzmadec --help.
This commit is contained in:
parent
436fa5fae9
commit
0ea98e52ba
|
@ -60,20 +60,25 @@ str_to_uint64(const char *name, const char *value, uint64_t min, uint64_t max)
|
||||||
if (*value != '\0') {
|
if (*value != '\0') {
|
||||||
// Look for suffix.
|
// Look for suffix.
|
||||||
static const struct {
|
static const struct {
|
||||||
const char *name;
|
const char name[4];
|
||||||
uint64_t multiplier;
|
uint64_t multiplier;
|
||||||
} suffixes[] = {
|
} suffixes[] = {
|
||||||
{ "k", UINT64_C(1000) },
|
{ "k", UINT64_C(1000) },
|
||||||
{ "M", UINT64_C(1000000) },
|
{ "kB", UINT64_C(1000) },
|
||||||
{ "G", UINT64_C(1000000000) },
|
{ "M", UINT64_C(1000000) },
|
||||||
{ "Ki", UINT64_C(1024) },
|
{ "MB", UINT64_C(1000000) },
|
||||||
{ "Mi", UINT64_C(1048576) },
|
{ "G", UINT64_C(1000000000) },
|
||||||
{ "Gi", UINT64_C(1073741824) },
|
{ "GB", UINT64_C(1000000000) },
|
||||||
{ NULL, 0 }
|
{ "Ki", UINT64_C(1024) },
|
||||||
|
{ "KiB", UINT64_C(1024) },
|
||||||
|
{ "Mi", UINT64_C(1048576) },
|
||||||
|
{ "MiB", UINT64_C(1048576) },
|
||||||
|
{ "Gi", UINT64_C(1073741824) },
|
||||||
|
{ "GiB", UINT64_C(1073741824) }
|
||||||
};
|
};
|
||||||
|
|
||||||
uint64_t multiplier = 0;
|
uint64_t multiplier = 0;
|
||||||
for (size_t i = 0; suffixes[i].name != NULL; ++i) {
|
for (size_t i = 0; i < ARRAY_SIZE(suffixes); ++i) {
|
||||||
if (strcmp(value, suffixes[i].name) == 0) {
|
if (strcmp(value, suffixes[i].name) == 0) {
|
||||||
multiplier = suffixes[i].multiplier;
|
multiplier = suffixes[i].multiplier;
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -116,7 +116,7 @@ help(void)
|
||||||
" MiB of memory at maximum.\n"
|
" MiB of memory at maximum.\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Report bugs to <" PACKAGE_BUGREPORT "> (in English or Finnish).\n",
|
"Report bugs to <" PACKAGE_BUGREPORT "> (in English or Finnish).\n",
|
||||||
argv0, (uint64_t)((mem_limit + 512 * 1024) / (1024 * 1024)));
|
argv0, ((uint64_t)(mem_limit) + 512 * 1024) / (1024 * 1024));
|
||||||
// Using PRIu64 above instead of %zu to support pre-C99 libc.
|
// Using PRIu64 above instead of %zu to support pre-C99 libc.
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
@ -196,20 +196,25 @@ str_to_size(const char *value)
|
||||||
if (*value != '\0') {
|
if (*value != '\0') {
|
||||||
// Look for suffix.
|
// Look for suffix.
|
||||||
static const struct {
|
static const struct {
|
||||||
const char *name;
|
const char name[4];
|
||||||
size_t multiplier;
|
size_t multiplier;
|
||||||
} suffixes[] = {
|
} suffixes[] = {
|
||||||
{ "k", 1000 },
|
{ "k", 1000 },
|
||||||
{ "M", 1000000 },
|
{ "kB", 1000 },
|
||||||
{ "G", 1000000000 },
|
{ "M", 1000000 },
|
||||||
{ "Ki", 1024 },
|
{ "MB", 1000000 },
|
||||||
{ "Mi", 1048576 },
|
{ "G", 1000000000 },
|
||||||
{ "Gi", 1073741824 },
|
{ "GB", 1000000000 },
|
||||||
{ NULL, 0 }
|
{ "Ki", 1024 },
|
||||||
|
{ "KiB", 1024 },
|
||||||
|
{ "Mi", 1048576 },
|
||||||
|
{ "MiB", 1048576 },
|
||||||
|
{ "Gi", 1073741824 },
|
||||||
|
{ "GiB", 1073741824 }
|
||||||
};
|
};
|
||||||
|
|
||||||
size_t multiplier = 0;
|
size_t multiplier = 0;
|
||||||
for (size_t i = 0; suffixes[i].name != NULL; ++i) {
|
for (size_t i = 0; i < ARRAY_SIZE(suffixes); ++i) {
|
||||||
if (strcmp(value, suffixes[i].name) == 0) {
|
if (strcmp(value, suffixes[i].name) == 0) {
|
||||||
multiplier = suffixes[i].multiplier;
|
multiplier = suffixes[i].multiplier;
|
||||||
break;
|
break;
|
||||||
|
@ -224,9 +229,9 @@ str_to_size(const char *value)
|
||||||
|
|
||||||
// Don't overflow here either.
|
// Don't overflow here either.
|
||||||
if (result > SIZE_MAX / multiplier)
|
if (result > SIZE_MAX / multiplier)
|
||||||
return SIZE_MAX;
|
result = SIZE_MAX;
|
||||||
|
else
|
||||||
result *= multiplier;
|
result *= multiplier;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
Loading…
Reference in New Issue