- tchar *tmp;
- unsigned long chunk_size = tstrtoul(optarg, &tmp, 10);
- if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
- imagex_error(T("Chunk size must be a non-negative integer!"));
- return UINT32_MAX;
- } else {
- return chunk_size;
- }
+ tchar *tmp;
+ uint64_t chunk_size = tstrtoul(optarg, &tmp, 10);
+ if (chunk_size == 0) {
+ imagex_error(T("Invalid chunk size specification; must be a positive integer\n"
+ " with optional K, M, or G suffix"));
+ return UINT32_MAX;
+ }
+ if (*tmp) {
+ if (*tmp == T('k') || *tmp == T('K')) {
+ chunk_size <<= 10;
+ tmp++;
+ } else if (*tmp == T('m') || *tmp == T('M')) {
+ chunk_size <<= 20;
+ tmp++;
+ } else if (*tmp == T('g') || *tmp == T('G')) {
+ chunk_size <<= 30;
+ tmp++;
+ }
+ if (*tmp && !(*tmp == T('i') && *(tmp + 1) == T('B'))) {
+ imagex_error(T("Invalid chunk size specification; suffix must be K, M, or G"));
+ return UINT32_MAX;
+ }
+ }
+ if (chunk_size >= UINT32_MAX) {
+ imagex_error(T("Invalid chunk size specification; the value is too large!"));
+ return UINT32_MAX;
+ }
+ return chunk_size;