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

#include <string.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 ;
}