MCPcopy Create free account
hub / github.com/F-Stack/f-stack / readSizeTFromCharChecked

Function readSizeTFromCharChecked

freebsd/contrib/zstd/programs/zstdcli.c:331–357  ·  view source on GitHub ↗

! readSizeTFromCharChecked() : * @return 0 if success, and store the result in *value. * allows and interprets K, KB, KiB, M, MB and MiB suffix. * Will also modify `*stringPtr`, advancing it to position where it stopped reading. * @return 1 if an overflow error occurs */

Source from the content-addressed store, hash-verified

329 * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
330 * @return 1 if an overflow error occurs */
331static int readSizeTFromCharChecked(const char** stringPtr, size_t* value)
332{
333 size_t result = 0;
334 while ((**stringPtr >='0') && (**stringPtr <='9')) {
335 size_t const max = ((size_t)(-1)) / 10;
336 size_t last = result;
337 if (result > max) return 1; /* overflow error */
338 result *= 10;
339 result += (size_t)(**stringPtr - '0');
340 if (result < last) return 1; /* overflow error */
341 (*stringPtr)++ ;
342 }
343 if ((**stringPtr=='K') || (**stringPtr=='M')) {
344 size_t const maxK = ((size_t)(-1)) >> 10;
345 if (result > maxK) return 1; /* overflow error */
346 result <<= 10;
347 if (**stringPtr=='M') {
348 if (result > maxK) return 1; /* overflow error */
349 result <<= 10;
350 }
351 (*stringPtr)++; /* skip `K` or `M` */
352 if (**stringPtr=='i') (*stringPtr)++;
353 if (**stringPtr=='B') (*stringPtr)++;
354 }
355 *value = result;
356 return 0;
357}
358
359/*! readSizeTFromChar() :
360 * @return : size_t value read from input in `char` format.

Callers 1

readSizeTFromCharFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected