! readU32FromCharChecked() : * @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 */
| 284 | * Will also modify `*stringPtr`, advancing it to position where it stopped reading. |
| 285 | * @return 1 if an overflow error occurs */ |
| 286 | static int readU32FromCharChecked(const char** stringPtr, unsigned* value) |
| 287 | { |
| 288 | unsigned result = 0; |
| 289 | while ((**stringPtr >='0') && (**stringPtr <='9')) { |
| 290 | unsigned const max = ((unsigned)(-1)) / 10; |
| 291 | unsigned last = result; |
| 292 | if (result > max) return 1; /* overflow error */ |
| 293 | result *= 10; |
| 294 | result += (unsigned)(**stringPtr - '0'); |
| 295 | if (result < last) return 1; /* overflow error */ |
| 296 | (*stringPtr)++ ; |
| 297 | } |
| 298 | if ((**stringPtr=='K') || (**stringPtr=='M')) { |
| 299 | unsigned const maxK = ((unsigned)(-1)) >> 10; |
| 300 | if (result > maxK) return 1; /* overflow error */ |
| 301 | result <<= 10; |
| 302 | if (**stringPtr=='M') { |
| 303 | if (result > maxK) return 1; /* overflow error */ |
| 304 | result <<= 10; |
| 305 | } |
| 306 | (*stringPtr)++; /* skip `K` or `M` */ |
| 307 | if (**stringPtr=='i') (*stringPtr)++; |
| 308 | if (**stringPtr=='B') (*stringPtr)++; |
| 309 | } |
| 310 | *value = result; |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | /*! readU32FromChar() : |
| 315 | * @return : unsigned integer value read from input in `char` format. |
no outgoing calls
no test coverage detected