summaryrefslogtreecommitdiff
path: root/src/libs6dns/s6dns_domain_decode.c
blob: 6fea0d614b7e36d1e4fdfa6ec00f97a4b56b1224 (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
/* ISC license. */

#include <errno.h>

#include <skalibs/posixishard.h>
#include <skalibs/bytestr.h>

#include <s6-dns/s6dns-domain.h>

static inline unsigned int s6dns_domain_label_decode (char *s, unsigned int max)
{
  unsigned int len = *(unsigned char *)s ;
  if ((len > 63) || (len >= max)) return (errno = EPROTO, 0) ;
  *s = '.' ;
  case_lowerb(s+1, len) ;
  return len + 1 ;
}

int s6dns_domain_decode (s6dns_domain_t *d)
{
  unsigned int max = 255 ;
  unsigned int pos = 0 ;
  for (;;)
  {
    unsigned int r = s6dns_domain_label_decode(d->s + pos, max - pos) ;
    if (!r) return 0 ;
    pos += r ;
    if (r == 1) break ;
  }
  d->len = pos ;
  return 1 ;
}