skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

netstring_decode.c (591B)


      1 /* ISC license. */
      2 
      3 #include <errno.h>
      4 #include <skalibs/uint64.h>
      5 #include <skalibs/stralloc.h>
      6 #include <skalibs/netstring.h>
      7 
      8 ssize_t netstring_decode (stralloc *sa, char const *s, size_t len)
      9 {
     10   uint64_t nlen ;
     11   size_t pos ;
     12   if (!len) return 0 ;
     13   pos = uint64_scan(s, &nlen) ;
     14   if (pos >= len) return (errno = EINVAL, -1) ;
     15   if (s[pos] != ':') return (errno = EINVAL, -1) ;
     16   s += pos+1 ; len -= pos+1 ;
     17   if (len <= nlen) return (errno = EINVAL, -1) ;
     18   if (s[nlen] != ',') return (errno = EINVAL, -1) ;
     19   if (!stralloc_catb(sa, s, nlen)) return -1 ;
     20   return pos + nlen + 2 ;
     21 }