summaryrefslogtreecommitdiff
path: root/src/libstddjb/netstring_decode.c
blob: b1f4905aa0227207e622447b079cee8ac37a4be5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* ISC license. */

#include <errno.h>
#include <skalibs/uint64.h>
#include <skalibs/stralloc.h>
#include <skalibs/netstring.h>

ssize_t netstring_decode (stralloc *sa, char const *s, size_t len)
{
  uint64_t nlen ;
  size_t pos ;
  if (!len) return 0 ;
  pos = uint64_scan(s, &nlen) ;
  if (pos >= len) return (errno = EINVAL, -1) ;
  if (s[pos] != ':') return (errno = EINVAL, -1) ;
  s += pos+1 ; len -= pos+1 ;
  if (len <= nlen) return (errno = EINVAL, -1) ;
  if (s[nlen] != ',') return (errno = EINVAL, -1) ;
  if (!stralloc_catb(sa, s, nlen)) return -1 ;
  return pos + nlen + 2 ;
}