diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-18 18:55:44 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-18 18:55:44 +0000 |
commit | 3534b428629be185e096be99e3bd5fdfe32d5544 (patch) | |
tree | 210ef3198ed66bc7f7b7bf6a85e4579f455e5a36 /src/libstddjb/netstring_decode.c | |
download | skalibs-3534b428629be185e096be99e3bd5fdfe32d5544.tar.xz |
initial commit with rc for skalibs-2.0.0.0
Diffstat (limited to 'src/libstddjb/netstring_decode.c')
-rw-r--r-- | src/libstddjb/netstring_decode.c | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/libstddjb/netstring_decode.c b/src/libstddjb/netstring_decode.c new file mode 100644 index 0000000..a81756b --- /dev/null +++ b/src/libstddjb/netstring_decode.c @@ -0,0 +1,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 ; +} |