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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
/*
* Copyright (C) 2012 Tobias Brunner
* Hochschule fuer Technik Rapperswil
*
* Copyright (C) 2010 Martin Willi
* Copyright (C) 2010 revosec AG
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "bio_reader.h"
#include <utils/debug.h>
typedef struct private_bio_reader_t private_bio_reader_t;
/**
* Private data of an bio_reader_t object.
*/
struct private_bio_reader_t {
/**
* Public bio_reader_t interface.
*/
bio_reader_t public;
/**
* Remaining data to process
*/
chunk_t buf;
/**
* Optional data to free during destruction
*/
chunk_t cleanup;
};
METHOD(bio_reader_t, remaining, u_int32_t,
private_bio_reader_t *this)
{
return this->buf.len;
}
METHOD(bio_reader_t, peek, chunk_t,
private_bio_reader_t *this)
{
return this->buf;
}
/**
* A version of chunk_skip() that supports skipping from the end (i.e. simply
* reducing the size)
*/
static inline chunk_t chunk_skip_end(chunk_t chunk, size_t bytes, bool from_end)
{
if (chunk.len > bytes)
{
if (!from_end)
{
chunk.ptr += bytes;
}
chunk.len -= bytes;
return chunk;
}
return chunk_empty;
}
/**
* Returns a pointer to the data to read, optionally from the end
*/
static inline u_char *get_ptr_end(private_bio_reader_t *this, u_int32_t len,
bool from_end)
{
return from_end ? this->buf.ptr + (this->buf.len - len) : this->buf.ptr;
}
/**
* Read an u_int8_t from the buffer, optionally from the end of the buffer
*/
static bool read_uint8_internal(private_bio_reader_t *this, u_int8_t *res,
bool from_end)
{
if (this->buf.len < 1)
{
DBG1(DBG_LIB, "%d bytes insufficient to parse u_int8 data",
this->buf.len);
return FALSE;
}
*res = *get_ptr_end(this, 1, from_end);
this->buf = chunk_skip_end(this->buf, 1, from_end);
return TRUE;
}
/**
* Read an u_int16_t from the buffer, optionally from the end
*/
static bool read_uint16_internal(private_bio_reader_t *this, u_int16_t *res,
bool from_end)
{
if (this->buf.len < 2)
{
DBG1(DBG_LIB, "%d bytes insufficient to parse u_int16 data",
this->buf.len);
return FALSE;
}
*res = untoh16(get_ptr_end(this, 2, from_end));
this->buf = chunk_skip_end(this->buf, 2, from_end);
return TRUE;
}
/**
* Read an u_int32_t (only 24-bit) from the buffer, optionally from the end
*/
static bool read_uint24_internal(private_bio_reader_t *this, u_int32_t *res,
bool from_end)
{
if (this->buf.len < 3)
{
DBG1(DBG_LIB, "%d bytes insufficient to parse u_int24 data",
this->buf.len);
return FALSE;
}
*res = untoh32(get_ptr_end(this, 3, from_end)) >> 8;
this->buf = chunk_skip_end(this->buf, 3, from_end);
return TRUE;
}
/**
* Read an u_int32_t from the buffer, optionally from the end
*/
static bool read_uint32_internal(private_bio_reader_t *this, u_int32_t *res,
bool from_end)
{
if (this->buf.len < 4)
{
DBG1(DBG_LIB, "%d bytes insufficient to parse u_int32 data",
this->buf.len);
return FALSE;
}
*res = untoh32(get_ptr_end(this, 4, from_end));
this->buf = chunk_skip_end(this->buf, 4, from_end);
return TRUE;
}
/**
* Read an u_int64_t from the buffer, optionally from the end
*/
static bool read_uint64_internal(private_bio_reader_t *this, u_int64_t *res,
bool from_end)
{
if (this->buf.len < 8)
{
DBG1(DBG_LIB, "%d bytes insufficient to parse u_int64 data",
this->buf.len);
return FALSE;
}
*res = untoh64(get_ptr_end(this, 8, from_end));
this->buf = chunk_skip_end(this->buf, 8, from_end);
return TRUE;
}
/**
* Read a chunk of data from the buffer, optionally from the end
*/
static bool read_data_internal(private_bio_reader_t *this, u_int32_t len,
chunk_t *res, bool from_end)
{
if (this->buf.len < len)
{
DBG1(DBG_LIB, "%d bytes insufficient to parse %d bytes of data",
this->buf.len, len);
return FALSE;
}
*res = chunk_create(get_ptr_end(this, len, from_end), len);
this->buf = chunk_skip_end(this->buf, len, from_end);
return TRUE;
}
METHOD(bio_reader_t, read_uint8, bool,
private_bio_reader_t *this, u_int8_t *res)
{
return read_uint8_internal(this, res, FALSE);
}
METHOD(bio_reader_t, read_uint16, bool,
private_bio_reader_t *this, u_int16_t *res)
{
return read_uint16_internal(this, res, FALSE);
}
METHOD(bio_reader_t, read_uint24, bool,
private_bio_reader_t *this, u_int32_t *res)
{
return read_uint24_internal(this, res, FALSE);
}
METHOD(bio_reader_t, read_uint32, bool,
private_bio_reader_t *this, u_int32_t *res)
{
return read_uint32_internal(this, res, FALSE);
}
METHOD(bio_reader_t, read_uint64, bool,
private_bio_reader_t *this, u_int64_t *res)
{
return read_uint64_internal(this, res, FALSE);
}
METHOD(bio_reader_t, read_data, bool,
private_bio_reader_t *this, u_int32_t len, chunk_t *res)
{
return read_data_internal(this, len, res, FALSE);
}
METHOD(bio_reader_t, read_uint8_end, bool,
private_bio_reader_t *this, u_int8_t *res)
{
return read_uint8_internal(this, res, TRUE);
}
METHOD(bio_reader_t, read_uint16_end, bool,
private_bio_reader_t *this, u_int16_t *res)
{
return read_uint16_internal(this, res, TRUE);
}
METHOD(bio_reader_t, read_uint24_end, bool,
private_bio_reader_t *this, u_int32_t *res)
{
return read_uint24_internal(this, res, TRUE);
}
METHOD(bio_reader_t, read_uint32_end, bool,
private_bio_reader_t *this, u_int32_t *res)
{
return read_uint32_internal(this, res, TRUE);
}
METHOD(bio_reader_t, read_uint64_end, bool,
private_bio_reader_t *this, u_int64_t *res)
{
return read_uint64_internal(this, res, TRUE);
}
METHOD(bio_reader_t, read_data_end, bool,
private_bio_reader_t *this, u_int32_t len, chunk_t *res)
{
return read_data_internal(this, len, res, TRUE);
}
METHOD(bio_reader_t, read_data8, bool,
private_bio_reader_t *this, chunk_t *res)
{
u_int8_t len;
if (!read_uint8(this, &len))
{
return FALSE;
}
return read_data(this, len, res);
}
METHOD(bio_reader_t, read_data16, bool,
private_bio_reader_t *this, chunk_t *res)
{
u_int16_t len;
if (!read_uint16(this, &len))
{
return FALSE;
}
return read_data(this, len, res);
}
METHOD(bio_reader_t, read_data24, bool,
private_bio_reader_t *this, chunk_t *res)
{
u_int32_t len;
if (!read_uint24(this, &len))
{
return FALSE;
}
return read_data(this, len, res);
}
METHOD(bio_reader_t, read_data32, bool,
private_bio_reader_t *this, chunk_t *res)
{
u_int32_t len;
if (!read_uint32(this, &len))
{
return FALSE;
}
return read_data(this, len, res);
}
METHOD(bio_reader_t, destroy, void,
private_bio_reader_t *this)
{
free(this->cleanup.ptr);
free(this);
}
/**
* See header
*/
bio_reader_t *bio_reader_create(chunk_t data)
{
private_bio_reader_t *this;
INIT(this,
.public = {
.remaining = _remaining,
.peek = _peek,
.read_uint8 = _read_uint8,
.read_uint16 = _read_uint16,
.read_uint24 = _read_uint24,
.read_uint32 = _read_uint32,
.read_uint64 = _read_uint64,
.read_data = _read_data,
.read_uint8_end = _read_uint8_end,
.read_uint16_end = _read_uint16_end,
.read_uint24_end = _read_uint24_end,
.read_uint32_end = _read_uint32_end,
.read_uint64_end = _read_uint64_end,
.read_data_end = _read_data_end,
.read_data8 = _read_data8,
.read_data16 = _read_data16,
.read_data24 = _read_data24,
.read_data32 = _read_data32,
.destroy = _destroy,
},
.buf = data,
);
return &this->public;
}
/**
* See header
*/
bio_reader_t *bio_reader_create_own(chunk_t data)
{
private_bio_reader_t *this;
this = (private_bio_reader_t*)bio_reader_create(data);
this->cleanup = data;
return &this->public;
}
|