aboutsummaryrefslogtreecommitdiffstats
path: root/src/libtipidee/tipidee_util_parse_range.c
blob: e096fda8fb237d6789c92c9888fc5ccd78ff0608 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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 ;
  }
}