File: netstring.c

package info (click to toggle)
mailfront 0.98-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 648 kB
  • ctags: 495
  • sloc: ansic: 3,033; sh: 1,487; makefile: 78
file content (31 lines) | stat: -rw-r--r-- 681 bytes parent folder | download | duplicates (4)
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 <iobuf/iobuf.h>
#include <str/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 == ',';
}