aboutsummaryrefslogtreecommitdiff
path: root/lib/util/src
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-08-31 13:40:46 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-08-31 13:40:46 +0200
commit035433e913872bc9c99fb19cd5b2eec1463ccdec (patch)
tree3c66bbf8953db9a12256b7ecced5033e3bdb3f2a /lib/util/src
parent3a0ee386184093a130d6c61c3eeb1d5a65bdd167 (diff)
Document decimal integer parsing helpers, add an octal variant
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/util/src')
-rw-r--r--lib/util/src/parse_int.c36
1 files changed, 25 insertions, 11 deletions
diff --git a/lib/util/src/parse_int.c b/lib/util/src/parse_int.c
index 1bca528..18e5b9a 100644
--- a/lib/util/src/parse_int.c
+++ b/lib/util/src/parse_int.c
@@ -11,8 +11,8 @@
#include <ctype.h>
-int parse_uint(const char *in, size_t len, size_t *diff,
- sqfs_u64 vmin, sqfs_u64 vmax, sqfs_u64 *out)
+static int parse(const char *in, size_t len, size_t *diff,
+ sqfs_u64 base, sqfs_u64 vmin, sqfs_u64 vmax, sqfs_u64 *out)
{
/* init result */
if (diff != NULL)
@@ -25,25 +25,28 @@ int parse_uint(const char *in, size_t len, size_t *diff,
/* parse sequence */
while (len > 0 && isdigit(*in)) {
- sqfs_u64 x = *(in++) - '0';
- --len;
+ sqfs_u64 x = *in - '0';
+ if (x >= base)
+ break;
- if (diff != NULL)
- ++(*diff);
-
- if ((*out) >= (0xFFFFFFFFFFFFFFFFULL / 10ULL))
+ if ((*out) >= (0xFFFFFFFFFFFFFFFFULL / base))
return SQFS_ERROR_OVERFLOW;
- (*out) *= 10;
+ (*out) *= base;
if ((*out) > (0xFFFFFFFFFFFFFFFFULL - x))
return SQFS_ERROR_OVERFLOW;
(*out) += x;
+
+ --len;
+ ++in;
+ if (diff != NULL)
+ ++(*diff);
}
/* range check */
- if ((vmin != vmax) && ((*out < vmin) || (*out > vmax)))
+ if ((vmin < vmax) && ((*out < vmin) || (*out > vmax)))
return SQFS_ERROR_OUT_OF_BOUNDS;
/* if diff is not used, entire must have been processed */
@@ -53,6 +56,17 @@ int parse_uint(const char *in, size_t len, size_t *diff,
return 0;
}
+int parse_uint(const char *in, size_t len, size_t *diff,
+ sqfs_u64 vmin, sqfs_u64 vmax, sqfs_u64 *out)
+{
+ return parse(in, len, diff, 10, vmin, vmax, out);
+}
+
+int parse_uint_oct(const char *in, size_t len, size_t *diff,
+ sqfs_u64 vmin, sqfs_u64 vmax, sqfs_u64 *out)
+{
+ return parse(in, len, diff, 8, vmin, vmax, out);
+}
int parse_int(const char *in, size_t len, size_t *diff,
sqfs_s64 vmin, sqfs_s64 vmax, sqfs_s64 *out)
@@ -82,7 +96,7 @@ int parse_int(const char *in, size_t len, size_t *diff,
*out = (sqfs_s64)temp;
}
- if (vmin != vmax && ((*out < vmin) || (*out > vmax)))
+ if ((vmin < vmax) && ((*out < vmin) || (*out > vmax)))
return SQFS_ERROR_OUT_OF_BOUNDS;
return 0;