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

#include <string.h>
#include <skalibs/uint64.h>
#include <skalibs/stralloc.h>
#include <skalibs/netstring.h>

int netstring_appendb (stralloc *sa, char const *s, size_t len)
{
  char fmt[UINT64_FMT] ;
  size_t n = uint64_fmt(fmt, len) ;
  if (!stralloc_readyplus(sa, len + n + 2)) return 0 ;
  fmt[n] = ':' ;
  memcpy(sa->s + sa->len, fmt, n+1) ;
  memcpy(sa->s + sa->len + n+1, s, len) ;
  sa->s[sa->len + n+1 + len] = ',' ;
  sa->len += n + 2 + len ;
  return 1 ;
}