| 52 | } |
| 53 | |
| 54 | int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max) |
| 55 | { |
| 56 | if (value && *value) { |
| 57 | char *end; |
| 58 | uintmax_t val; |
| 59 | uintmax_t factor; |
| 60 | |
| 61 | /* negative values would be accepted by strtoumax */ |
| 62 | if (strchr(value, '-')) { |
| 63 | errno = EINVAL; |
| 64 | return 0; |
| 65 | } |
| 66 | errno = 0; |
| 67 | val = strtoumax(value, &end, 0); |
| 68 | if (errno == ERANGE) |
| 69 | return 0; |
| 70 | if (end == value) { |
| 71 | errno = EINVAL; |
| 72 | return 0; |
| 73 | } |
| 74 | factor = get_unit_factor(end); |
| 75 | if (!factor) { |
| 76 | errno = EINVAL; |
| 77 | return 0; |
| 78 | } |
| 79 | if (unsigned_mult_overflows(factor, val) || |
| 80 | factor * val > max) { |
| 81 | errno = ERANGE; |
| 82 | return 0; |
| 83 | } |
| 84 | val *= factor; |
| 85 | *ret = val; |
| 86 | return 1; |
| 87 | } |
| 88 | errno = EINVAL; |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | int git_parse_int(const char *value, int *ret) |
| 93 | { |