Skip to content

Commit 16a4fc5

Browse files
committed
Add G, GB and GiB suffix support for 32-bit integers
1 parent 6e1e545 commit 16a4fc5

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

programs/zstdcli.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ static void errorOut(const char* msg)
347347

348348
/*! readU32FromCharChecked() :
349349
* @return 0 if success, and store the result in *value.
350-
* allows and interprets K, KB, KiB, M, MB and MiB suffix.
350+
* allows and interprets K, KB, KiB, M, MB, MiB, G, GB and GiB suffix.
351351
* Will also modify `*stringPtr`, advancing it to position where it stopped reading.
352352
* @return 1 if an overflow error occurs */
353353
static int readU32FromCharChecked(const char** stringPtr, unsigned* value)
@@ -362,15 +362,22 @@ static int readU32FromCharChecked(const char** stringPtr, unsigned* value)
362362
if (result < last) return 1; /* overflow error */
363363
(*stringPtr)++ ;
364364
}
365-
if ((**stringPtr=='K') || (**stringPtr=='M')) {
366-
unsigned const maxK = ((unsigned)(-1)) >> 10;
367-
if (result > maxK) return 1; /* overflow error */
368-
result <<= 10;
369-
if (**stringPtr=='M') {
370-
if (result > maxK) return 1; /* overflow error */
371-
result <<= 10;
365+
if ((**stringPtr=='K') || (**stringPtr=='M') || (**stringPtr=='G')) {
366+
switch (**stringPtr) {
367+
case 'K':
368+
if (result > (((unsigned)-1) >> 10)) return 1; /* overflow error */
369+
result <<= 10;
370+
break;
371+
case 'M':
372+
if (result > (((unsigned)-1) >> 20)) return 1; /* overflow error */
373+
result <<= 20;
374+
break;
375+
case 'G':
376+
if (result > (((unsigned)-1) >> 30)) return 1; /* overflow error */
377+
result <<= 30;
378+
break;
372379
}
373-
(*stringPtr)++; /* skip `K` or `M` */
380+
(*stringPtr)++; /* skip `K`, `M` or `G` */
374381
if (**stringPtr=='i') (*stringPtr)++;
375382
if (**stringPtr=='B') (*stringPtr)++;
376383
}
@@ -380,7 +387,7 @@ static int readU32FromCharChecked(const char** stringPtr, unsigned* value)
380387

381388
/*! readU32FromChar() :
382389
* @return : unsigned integer value read from input in `char` format.
383-
* allows and interprets K, KB, KiB, M, MB and MiB suffix.
390+
* allows and interprets K, KB, KiB, M, MB, MiB, G, GB and GiB suffix.
384391
* Will also modify `*stringPtr`, advancing it to position where it stopped reading.
385392
* Note : function will exit() program if digit sequence overflows */
386393
static unsigned readU32FromChar(const char** stringPtr) {
@@ -392,7 +399,7 @@ static unsigned readU32FromChar(const char** stringPtr) {
392399

393400
/*! readIntFromChar() :
394401
* @return : signed integer value read from input in `char` format.
395-
* allows and interprets K, KB, KiB, M, MB and MiB suffix.
402+
* allows and interprets K, KB, KiB, M, MB, MiB, G, GB and GiB suffix.
396403
* Will also modify `*stringPtr`, advancing it to position where it stopped reading.
397404
* Note : function will exit() program if digit sequence overflows */
398405
static int readIntFromChar(const char** stringPtr) {
@@ -830,7 +837,7 @@ static unsigned init_nbWorkers(unsigned defaultNbWorkers) {
830837
NEXT_FIELD(__nb); \
831838
_varu32 = readU32FromChar(&__nb); \
832839
if(*__nb != 0) { \
833-
errorOut("error: only numeric values with optional suffixes K, KB, KiB, M, MB, MiB are allowed"); \
840+
errorOut("error: only numeric values with optional suffixes K, KB, KiB, M, MB, MiB, G, GB, GiB are allowed"); \
834841
} \
835842
}
836843

0 commit comments

Comments
 (0)