summaryrefslogtreecommitdiff
path: root/src/libstddjb/netstring_decode.c
blob: a81756bdd36a5e7cf2c94f8ce5e511d974a68166 (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 <errno.h>
#include <skalibs/fmtscan.h>
#include <skalibs/netstring.h>
#include <skalibs/stralloc.h>
#include <skalibs/uint.h>

int netstring_decode (stralloc *sa, char const *s, unsigned int len)
{
  unsigned int nlen ;
  register unsigned int pos ;
  if (!len) return 0 ;
  pos = uint_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 ;
}