-
Notifications
You must be signed in to change notification settings - Fork 4
/
parsemsg.c
69 lines (53 loc) · 1 KB
/
parsemsg.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
62
63
64
65
66
67
68
69
#include "parsemsg.h"
static int flags;
static int size;
static int maxsize;
static int readsize;
static int readcount;
static char *data;
void
parsemsg_begin(void *buf, int sizearg, int flagsarg)
{
flags = flagsarg;
size = sizearg;
maxsize = 0;
readsize = 0;
readcount = 0;
data = buf;
}
int
parsemsg_canread(void)
{
if (!maxsize)
return readcount + readsize <= size;
return readcount + readsize <= size && size <= maxsize;
}
unsigned char
parsemsg_readbyte(void)
{
unsigned char byte;
readsize = 1;
if (!parsemsg_canread())
return PARSEMSG_INVALID;
byte = data[readcount];
readcount += readsize;
return byte;
}
char *
parsemsg_readstr(void)
{
int i, len, byte;
static char str[8192] = {0};
readsize = 1;
if (!parsemsg_canread())
return 0;
len = flags & PARSEMSG_USERMSG ? 2048 : (flags & PARSEMSG_NETMSG ? 8192 : 2048);
for (i = 0; i < len - 1; i++) {
byte = parsemsg_readbyte();
if (!byte || byte == PARSEMSG_INVALID)
break;
str[i] = byte;
}
str[i] = '\0';
return str;
}