skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

netstring_appendv.c (716B)


      1 /* ISC license. */
      2 
      3 #include <string.h>
      4 #include <skalibs/uint64.h>
      5 #include <skalibs/bytestr.h>
      6 #include <skalibs/siovec.h>
      7 #include <skalibs/stralloc.h>
      8 #include <skalibs/netstring.h>
      9 
     10 int netstring_appendv (stralloc *sa, struct iovec const *v, unsigned int n)
     11 {
     12   char fmt[UINT64_FMT] ;
     13   size_t len = 0, pos ;
     14   unsigned int i = 0 ;
     15   for (; i < n ; i++) len += v[i].iov_len ;
     16   pos = uint64_fmt(fmt, len) ;
     17   if (!stralloc_readyplus(sa, len + pos + 2)) return 0 ;
     18   fmt[pos] = ':' ;
     19   memcpy(sa->s + sa->len, fmt, pos+1) ;
     20   sa->len += pos+1 ;
     21   for (i = 0 ; i < n ; i++)
     22   {
     23     memcpy(sa->s + sa->len, v[i].iov_base, v[i].iov_len) ;
     24     sa->len += v[i].iov_len ;
     25   }
     26   sa->s[sa->len++] = ',' ;
     27   return 1 ;
     28 }