-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfile_bits.c
271 lines (232 loc) · 6.1 KB
/
rfile_bits.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* rfile_bits.c */
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "rfile_bits.h"
static int rfile__bits_destroy_static(rfile_bits *bits);
static int rfile__bits_grow_static(rfile_bits *bits, size_t size);
static int rfile__bits_destroy_dynamic(rfile_bits *bits);
static int rfile__bits_grow_dynamic(rfile_bits *bits, size_t size);
static rfile_bits_vt vt_static = {
rfile__bits_destroy_static,
rfile__bits_grow_static
};
static rfile_bits_vt vt_dynamic = {
rfile__bits_destroy_dynamic,
rfile__bits_grow_dynamic
};
int rfile_bits_buf(rfile_bits *bits, unsigned char *buf, size_t size) {
memset(bits, 0, sizeof(*bits));
bits->buf = buf;
bits->size = size;
bits->vt = &vt_static;
return 0;
}
int rfile_bits_init(rfile_bits *bits, size_t size) {
memset(bits, 0, sizeof(*bits));
bits->vt = &vt_dynamic;
return rfile_bits_grow(bits, size);
}
static int rfile__bits_destroy_static(rfile_bits *bits) {
(void) bits;
return 0;
}
static int rfile__bits_grow_static(rfile_bits *bits, size_t size) {
if (bits->size < size) {
errno = ENOMEM;
return -1;
}
return 0;
}
static int rfile__bits_destroy_dynamic(rfile_bits *bits) {
free(bits->buf);
memset(bits, 0, sizeof(*bits));
return 0;
}
static int rfile__bits_grow_dynamic(rfile_bits *bits, size_t size) {
if (bits->size < size) {
unsigned char *buf = malloc(size);
if (!buf) {
errno = ENOMEM;
return -1;
}
if (bits->buf) {
memcpy(buf, bits->buf, bits->size);
free(bits->buf);
}
bits->buf = buf;
bits->size = size;
}
return 0;
}
int rfile_bits_destroy(rfile_bits *bits) {
return bits->vt->destroy(bits);
}
int rfile_bits_grow(rfile_bits *bits, size_t size) {
return bits->vt->grow(bits, size);
}
int rfile_bits_rewind(rfile_bits *bits) {
bits->pos = 0;
return 0;
}
int rfile_bits_put(rfile_bits *bits, const unsigned char *buf, size_t len) {
if (rfile_bits_grow(bits, bits->pos + len) < 0) return -1;
memcpy(bits->buf + bits->pos, buf, len);
bits->pos += len;
if (bits->used < (size_t) bits->pos) bits->used = bits->pos;
return 0;
}
int rfile_bits_pad(rfile_bits *bits, size_t len) {
if (rfile_bits_grow(bits, bits->pos + len) < 0) return -1;
memset(bits->buf + bits->pos, 0, len);
bits->pos += len;
if (bits->used < (size_t) bits->pos) bits->used = bits->pos;
return 0;
}
int rfile_bits_get(rfile_bits *bits, unsigned char *buf, size_t len) {
if (bits->pos + len > bits->used) {
errno = EIO;
return -1;
}
memcpy(buf, bits->buf + bits->pos, len);
bits->pos += len;
return 0;
}
int rfile_bits_put32(rfile_bits *bits, uint32_t v) {
unsigned char buf[4];
buf[0] = v >> 0;
buf[1] = v >> 8;
buf[2] = v >> 16;
buf[3] = v >> 24;
return rfile_bits_put(bits, buf, sizeof(buf));
}
int rfile_bits_put64(rfile_bits *bits, uint64_t v) {
unsigned char buf[8];
buf[0] = v >> 0;
buf[1] = v >> 8;
buf[2] = v >> 16;
buf[3] = v >> 24;
buf[4] = v >> 32;
buf[5] = v >> 40;
buf[6] = v >> 48;
buf[7] = v >> 56;
return rfile_bits_put(bits, buf, sizeof(buf));
}
int rfile_bits_get32(rfile_bits *bits, uint32_t *v) {
unsigned char buf[4];
if (rfile_bits_get(bits, buf, sizeof(buf)) < 0) return -1;
*v = ((uint32_t) buf[0] << 0)
| ((uint32_t) buf[1] << 8)
| ((uint32_t) buf[2] << 16)
| ((uint32_t) buf[3] << 24);
return 0;
}
int rfile_bits_get64(rfile_bits *bits, uint64_t *v) {
unsigned char buf[8];
if (rfile_bits_get(bits, buf, sizeof(buf)) < 0) return -1;
*v = ((uint64_t) buf[0] << 0)
| ((uint64_t) buf[1] << 8)
| ((uint64_t) buf[2] << 16)
| ((uint64_t) buf[3] << 24)
| ((uint64_t) buf[4] << 32)
| ((uint64_t) buf[5] << 40)
| ((uint64_t) buf[6] << 48)
| ((uint64_t) buf[7] << 56);
return 0;
}
int rfile_bits_put_data(rfile_bits *bits, const void *data, size_t len) {
return rfile_bits_put32(bits, len)
|| rfile_bits_put(bits, data, len)
|| rfile_bits_pad(bits, rfile_ALIGNUP(len) - len) ? -1 : 0;
}
const void *rfile_bits_get_data(rfile_bits *bits, size_t *len) {
uint32_t sz;
const void *data;
if (rfile_bits_get32(bits, &sz) < 0) return NULL;
*len = sz;
if (bits->pos + sz > (off_t) bits->used) {
errno = EIO;
return NULL;
}
data = bits->buf + bits->pos;
bits->pos = rfile_ALIGNUP(bits->pos + sz);
return data;
}
int rfile_bits_puts(rfile_bits *bits, const char *s) {
return rfile_bits_put_data(bits, s, strlen(s) + 1);
}
const char *rfile_bits_gets(rfile_bits *bits) {
size_t sz;
return (const char *) rfile_bits_get_data(bits, &sz);
}
int rfile_bits_read(rfile_bits *bits, int fd, size_t len) {
ssize_t sz;
if (rfile_bits_grow(bits, bits->pos + len) < 0) return -1;
sz = read(fd, bits->buf + bits->pos, len);
if (sz < 0) return -1;
if (sz < (ssize_t) len) {
errno = EIO;
return -1;
}
bits->pos += len;
if (bits->used < (size_t) bits->pos) bits->used = bits->pos;
return 0;
}
int rfile_bits_write(rfile_bits *bits, int fd, size_t len) {
ssize_t sz;
if (bits->pos + len > bits->used) {
errno = EIO;
return -1;
}
sz = write(fd, bits->buf + bits->pos, len);
if (sz < 0) return -1;
if (sz < (ssize_t) len) {
errno = EIO;
return -1;
}
bits->pos += len;
return 0;
}
int rfile_bits_piddle(rfile_bits *bits, const char *spec, ...) {
char cs;
va_list ap;
va_start(ap, spec);
while ((cs = *spec++)) {
switch (cs) {
case 'l':
if (rfile_bits_put32(bits, va_arg(ap, uint32_t))) return -1;
break;
case 'L':
if (rfile_bits_put64(bits, va_arg(ap, uint64_t))) return -1;
break;
default:
errno = EINVAL;
return -1;
}
}
va_end(ap);
return 0;
}
int rfile_bits_guzzle(rfile_bits *bits, const char *spec, ...) {
char cs;
va_list ap;
va_start(ap, spec);
while ((cs = *spec++)) {
switch (cs) {
case 'l':
if (rfile_bits_get32(bits, va_arg(ap, uint32_t *))) return -1;
break;
case 'L':
if (rfile_bits_get64(bits, va_arg(ap, uint64_t *))) return -1;
break;
default:
errno = EINVAL;
return -1;
}
}
va_end(ap);
return 0;
}
/* vim:ts=2:sw=2:sts=2:et:ft=c
*/