From 51d0fdce74aeb23ef28336037b2d35aa4c955cee Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Wed, 20 Dec 2023 14:08:25 +0000 Subject: Major refactors; implement ranges Signed-off-by: Laurent Bercot --- src/libtipidee/tipidee_util_parse_range.c | 49 +++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/libtipidee/tipidee_util_parse_range.c (limited to 'src/libtipidee/tipidee_util_parse_range.c') diff --git a/src/libtipidee/tipidee_util_parse_range.c b/src/libtipidee/tipidee_util_parse_range.c new file mode 100644 index 0000000..e096fda --- /dev/null +++ b/src/libtipidee/tipidee_util_parse_range.c @@ -0,0 +1,49 @@ +/* ISC license. */ + +#include + +#include + +#include + +int tipidee_util_parse_range (char const *s, off_t max, uint64_t *start, uint64_t *len) +{ + if (strncmp(s, "bytes=", 6)) return -1 ; + s += 6 ; + if (*s == '-') + { + uint64_t n ; + size_t m = uint64_scan(++s, &n) ; + if (!m) return -1 ; + s += m ; + if (*s && *s != ',') return -1 ; + if (n > max) return -1 ; + *start = max - n ; + *len = n ; + return 1 ; + } + else + { + uint64_t beg ; + uint64_t n ; + size_t m = uint64_scan(s, &beg) ; + if (!m) return -1 ; + s += m ; + if (*s++ != '-') return -1 ; + if (beg >= max) return -1 ; + if (!*s || *s == ',') + { + *start = beg ; + *len = max - beg ; + return 1 ; + } + m = uint64_scan(s, &n) ; + if (!m) return -1 ; + s += m ; + if (*s && *s != ',') return -1 ; + if (n >= max || n < beg) return -1 ; + *start = beg ; + *len = n + 1 - beg ; + return 1 ; + } +} -- cgit v1.3.1