-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetstring_read.c
61 lines (57 loc) · 1.62 KB
/
netstring_read.c
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
%use buffer_get;
%use stralloc_copys;
%use stralloc_ready;
%use strerr_die;
%use scan_ulong;
%
*/
/* Public Domain */
#include <stdio.h>
#include "netstring.h"
#include "buffer.h"
#include "scan.h"
#include "fmt.h"
#include "stralloc.h"
#include "strerr.h"
#define FATAL "netstring_read failed: "
int netstring_read(buffer* inbuf, stralloc *outstr)
{
char tmp;
char lenbuf[FMT_ULONG];
int i;
unsigned long len;
int r;
if(!stralloc_copys(outstr,""))
strerr_die2x(111,FATAL,"failed to initialize stralloc");
if((r=buffer_GETC(inbuf,&tmp))==-1)
strerr_die2x(111,FATAL,"failed to read buffer");
if(r==0) return 0;
if(tmp!='&')
strerr_die2x(111,FATAL,"expected '&'");
for(i=0;i<FMT_ULONG-1;i++) {
if(buffer_GETC(inbuf,&tmp)!=1)
strerr_die2x(111,FATAL,"failed to read buffer [2]");
if(tmp==':') {
if(i==0) strerr_die2x(111,FATAL,"expected ':'");
break;
}
if(tmp <'0' || tmp > '9')
if(i==0) strerr_die2x(111,FATAL,"expected string length");
lenbuf[i] = tmp;
}
lenbuf[i] = 0;
if(scan_ulong(lenbuf,&len)==0)
strerr_die2x(111,FATAL,"failed to parse string length");
if(stralloc_ready(outstr,len)==0)
strerr_die2x(111,FATAL,"failed to allocate result string");
outstr->len = len;
if((r=buffer_getn(inbuf,outstr->s,len))!=len) {
strerr_die2x(111,FATAL,"failed to read buffer [3]");
}
if(buffer_GETC(inbuf,&tmp)!=1)
strerr_die2x(111,FATAL,"failed to read buffer [4]");
if(tmp!=',')
strerr_die2x(111,FATAL,"expected ','");
return 0;
}