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

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

ssize_t netstring_decode (stralloc *sa, char const *s, size_t len)
{
  uint64_t nlen ;
  register 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 ;
}