File: netstring.c

package info (click to toggle)
mailfront 2.12-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,072 kB
  • sloc: sh: 5,549; ansic: 5,083; makefile: 35
file content (31 lines) | stat: -rw-r--r-- 685 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <bglibs/iobuf.h>
#include <bglibs/str.h>
#include "mailfront.h"

int get_netstring_len(ibuf* in, unsigned long* i)
     /* Returns: 1 good number, 0 format error, -1 EOF */
{
  char c;
  *i = 0;
  for (*i = 0; ibuf_getc(in, &c); *i = (*i * 10) + (c - '0')) {
    if (c == ':') return 1;
    if (c < '0' || c > '9') return 0;
  }
  return -1;
}

int get_netstring(ibuf* in, str* s)
{
  unsigned long len;
  char ch;
  switch (get_netstring_len(in, &len)) {
  case -1: return -1;
  case 0: return 0;
  }
  if (!str_ready(s, len)) return -1;
  s->s[len] = 0;
  if (!ibuf_read(in, s->s, len)) return -1;
  s->len = len;
  if (!ibuf_getc(in, &ch)) return -1;
  return ch == ',';
}