aboutsummaryrefslogtreecommitdiffstats
path: root/src/libtipidee/tipidee_util_parse_range.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-12-20 14:08:25 +0000
committerLaurent Bercot <ska@appnovation.com>2023-12-20 14:08:25 +0000
commit51d0fdce74aeb23ef28336037b2d35aa4c955cee (patch)
tree9c178c8e6756ec770b85efd54235cd7c2a97888f /src/libtipidee/tipidee_util_parse_range.c
parentd2959364ef8d6bc948474a8facf497788b4e768b (diff)
downloadtipidee-51d0fdce74aeb23ef28336037b2d35aa4c955cee.tar.gz
Major refactors; implement ranges
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libtipidee/tipidee_util_parse_range.c')
-rw-r--r--src/libtipidee/tipidee_util_parse_range.c49
1 files changed, 49 insertions, 0 deletions
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 <string.h>
+
+#include <skalibs/uint64.h>
+
+#include <tipidee/util.h>
+
+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 ;
+ }
+}