forked from jmckaskill/c-capnproto
-
Notifications
You must be signed in to change notification settings - Fork 1
/
capn-stream.c
190 lines (158 loc) · 3.48 KB
/
capn-stream.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* vim: set sw=8 ts=8 sts=8 noet: */
#include "capn.h"
#include <string.h>
#ifndef min
static int min(int a, int b) { return (a < b) ? a : b; }
#endif
int capn_deflate(struct capn_stream* s) {
if (s->avail_in % 8) {
return CAPN_MISALIGNED;
}
while (s->avail_in) {
int i, sz = 0;
uint8_t hdr = 0;
uint8_t *p;
if (!s->avail_out)
return CAPN_NEED_MORE;
if (s->raw > 0) {
sz = min(s->raw, min(s->avail_in, s->avail_out));
memcpy(s->next_out, s->next_in, sz);
s->next_out += sz;
s->next_in += sz;
s->avail_out -= sz;
s->avail_in -= sz;
s->raw -= sz;
continue;
}
if (s->avail_in < 8)
return CAPN_NEED_MORE;
for (i = 0; i < 8; i++) {
if (s->next_in[i]) {
sz ++;
hdr |= 1 << i;
}
}
switch (sz) {
case 0:
if (s->avail_out < 2)
return CAPN_NEED_MORE;
s->next_out[0] = hdr;
for (sz = 1; sz < min(s->avail_in/8, 256); sz++) {
if (((uint64_t*) s->next_in)[sz] != 0) {
break;
}
}
s->next_out[1] = (uint8_t) (sz-1);
s->next_in += sz*8;
s->avail_in -= sz*8;
s->next_out += 2;
s->avail_out -= 2;
continue;
case 8:
if (s->avail_out < 10)
return CAPN_NEED_MORE;
s->next_out[0] = hdr;
memcpy(s->next_out+1, s->next_in, 8);
s->next_in += 8;
s->avail_in -= 8;
s->raw = min(s->avail_in, 256*8);
if ((p = (uint8_t*) memchr(s->next_in, 0, s->raw)) != NULL) {
s->raw = (p - s->next_in) & ~7;
}
s->next_out[9] = (uint8_t) (s->raw/8);
s->next_out += 10;
s->avail_out -= 10;
continue;
default:
if (s->avail_out < 1 + sz)
return CAPN_NEED_MORE;
*(s->next_out++) = hdr;
for (i = 0; i < 8; i++) {
if (s->next_in[i]) {
*(s->next_out++) = s->next_in[i];
}
}
s->avail_out -= sz + 1;
s->next_in += 8;
s->avail_in -= 8;
continue;
}
}
return 0;
}
int capn_inflate(struct capn_stream* s) {
if (s->avail_out % 8) {
return CAPN_MISALIGNED;
}
while (s->avail_out) {
int i, sz;
uint8_t hdr;
if (s->zeros > 0) {
sz = min(s->avail_out, s->zeros);
memset(s->next_out, 0, sz);
s->next_out += sz;
s->avail_out -= sz;
s->zeros -= sz;
continue;
}
if (s->raw > 0) {
if (s->avail_in == 0)
return CAPN_NEED_MORE;
sz = min(min(s->avail_out, s->raw), s->avail_in);
memcpy(s->next_out, s->next_in, sz);
s->next_in += sz;
s->next_out += sz;
s->avail_in -= sz;
s->avail_out -= sz;
s->raw -= sz;
continue;
}
if (s->avail_in == 0)
return 0;
else if (s->avail_in < 2)
return CAPN_NEED_MORE;
switch (s->next_in[0]) {
case 0xFF:
/* 0xFF is followed by 8 bytes raw, followed by
* a byte with length in words to read raw */
if (s->avail_in < 10)
return CAPN_NEED_MORE;
memcpy(s->next_out, s->next_in+1, 8);
s->raw = (int) s->next_in[9] * 8;
s->next_in += 10;
s->avail_in -= 10;
s->next_out += 8;
s->avail_out -= 8;
continue;
case 0x00:
/* 0x00 is followed by a single byte indicating
* the count of consecutive zero value words
* minus 1 */
s->zeros = (int) (s->next_in[1] + 1) * 8;
s->next_in += 2;
s->avail_in -= 2;
continue;
default:
sz = 0;
hdr = s->next_in[1];
for (i = 0; i < 8; i++) {
if (hdr & (1 << i))
sz++;
}
if (s->avail_in < 2 + sz)
return CAPN_NEED_MORE;
s->next_in += 2;
for (i = 0; i < 8; i++) {
if (hdr & (1 << i)) {
*(s->next_out++) = *(s->next_in++);
} else {
*(s->next_out++) = 0;
}
}
s->avail_out -= 8;
s->avail_in -= 2 + sz;
continue;
}
}
return 0;
}