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

#include <skalibs/netstring.h>
#include <skalibs/stralloc.h>
#include <skalibs/uint.h>

int netstring_encode (stralloc *sa, char const *s, unsigned int len)
{
  char fmt[UINT_FMT] ;
  unsigned int pos = uint_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 ;
}