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

#include <string.h>
#include <errno.h>
#include <ctype.h>

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

int s6dns_domain_fromstring (s6dns_domain_t *d, char const *s, size_t len)
{
  size_t j = 1 ;
  size_t i = 0 ;
  unsigned int lastdot = 0 ;
  d->s[0] = '.' ;
  for (; i < len ; i++)
  {
    if (lastdot)
    {
      if ((j >= 255) || (lastdot++ >= 64)) return (errno = ENAMETOOLONG, 0) ;
      d->s[j++] = tolower(s[i]) ;
    }
    if (s[i] == '.') lastdot = 0 ;
    else if (!lastdot)
    {
      i-- ;
      lastdot = 1 ;
    }
  }
  d->len = j ;
  return 1 ;
}