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

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

int netstring_encode (stralloc *sa, char const *s, size_t len)
{
  char fmt[UINT64_FMT] ;
  size_t pos = uint64_fmt(fmt, len) ;
  if (!stralloc_readyplus(sa, pos + len + 2)) return 0 ;
  stralloc_catb(sa, fmt, pos) ;
  stralloc_catb(sa, ":", 1) ;
  stralloc_catb(sa, s, len) ;
  stralloc_catb(sa, ",", 1) ;
  return 1 ;
}