-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
93 lines (80 loc) · 1.62 KB
/
parse.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* parsing primitives.
*/
#define _GNU_SOURCE
#include <string.h>
#include "drv.h"
void mkSlice(struct slice *b, void *base, size_t sz)
{
b->cur = (u_int8_t *)base;
b->end = b->cur + sz;
}
unsigned char *sliceBuf(struct slice *b)
{
return b->cur;
}
size_t sliceSize(struct slice *b)
{
return b->end - b->cur;
}
int getEOF(struct slice *b)
{
if(b->cur != b->end)
return -1;
return 0;
}
int getU8(struct slice *b, u_int8_t *x)
{
if(b->cur >= b->end) return -1;
*x = *b->cur;
b->cur++;
return 0;
}
int getU16(struct slice *b, u_int16_t *x)
{
u_int8_t h, l;
if(getU8(b, &h) == -1
|| getU8(b, &l) == -1)
return -1;
*x = (h << 8) | l;
return 0;
}
int getU32(struct slice *b, u_int32_t *x)
{
u_int16_t h, l;
if(getU16(b, &h) == -1
|| getU16(b, &l) == -1)
return -1;
*x = (h << 16) | l;
return 0;
}
int getU64(struct slice *b, u_int64_t *x)
{
u_int32_t h, l;
if(getU32(b, &h) == -1
|| getU32(b, &l) == -1)
return -1;
*x = ((u_int64_t)h << 32) | l;
return 0;
}
/* split a slice up into up to max slices */
int getDelimSlices(struct slice *b, char *delim, int delsz, size_t max, struct slice *x, size_t *nx)
{
unsigned char *ep;
size_t i;
for(i = 0; i < max && b->cur != b->end; i++) {
ep = memmem(b->cur, b->end - b->cur, delim, delsz);
x[i].cur = b->cur;
if(ep) {
b->cur = ep + delsz;
} else {
b->cur = b->end;
ep = b->end;
}
x[i].end = ep;
}
if(b->cur != b->end)
return -1;
*nx = i;
return 0;
}