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
|
/* Copyright (C) 2019 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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 3 of the License, or
(at your option) any later version.
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include "contrib/string.h"
#include "libknot/endian.h"
#include "libknot/errcode.h"
/*!
* \brief Struct to keep the wire context.
*/
typedef struct wire_ctx {
size_t size;
uint8_t *wire;
uint8_t *position;
int error;
bool readonly;
} wire_ctx_t;
/*!
* \brief Initialize wire context.
*/
static inline wire_ctx_t wire_ctx_init(uint8_t *data, size_t size)
{
assert(data);
wire_ctx_t result = {
.size = size,
.wire = data,
.position = data,
.error = KNOT_EOK,
.readonly = false
};
return result;
}
/*!
* \brief Initialize read only wire context.
*
* \note No write is performed, and error is set to KNOT_EACCES.
*
*/
static inline wire_ctx_t wire_ctx_init_const(const uint8_t *data, size_t size)
{
assert(data);
wire_ctx_t result = wire_ctx_init((uint8_t *)data, size);
result.readonly = true;
return result;
}
/*!
* \brief Gets actual position.
*
* \return position from the begin.
*/
static inline size_t wire_ctx_offset(wire_ctx_t *ctx)
{
assert(ctx);
return ctx->position - ctx->wire;
}
/*!
* \brief Set position offset from the begin.
*
* \param offset Wire offset (starts from 0).
*
* \note Noop if previous error.
*/
static inline void wire_ctx_set_offset(wire_ctx_t *ctx, size_t offset)
{
assert(ctx);
if (ctx->error != KNOT_EOK) {
return;
}
if (offset > ctx->size) {
ctx->error = KNOT_ERANGE;
return;
}
ctx->position = ctx->wire + offset;
}
/*!
* \brief Gets available bytes.
*
* \return Number of bytes to end.
*/
static inline size_t wire_ctx_available(wire_ctx_t *ctx)
{
assert(ctx);
return ctx->size - wire_ctx_offset(ctx);
}
/*!
* \brief Add offset to the current position.
*
* \note Noop if previous error.
*/
static inline void wire_ctx_skip(wire_ctx_t *ctx, ssize_t offset)
{
assert(ctx);
if (ctx->error != KNOT_EOK) {
return;
}
// Check for out of scope skip.
if (offset >= 0) {
if (offset > wire_ctx_available(ctx)) {
ctx->error = KNOT_ERANGE;
return;
}
} else {
if (-offset > wire_ctx_offset(ctx)) {
ctx->error = KNOT_ERANGE;
return;
}
}
ctx->position += offset;
}
/*!
* \brief Check the context if reading is possible.
*/
static inline int wire_ctx_can_read(wire_ctx_t *ctx, size_t size)
{
assert(ctx);
if (ctx->error != KNOT_EOK) {
return ctx->error;
}
if (wire_ctx_available(ctx) < size) {
return KNOT_EFEWDATA;
}
return KNOT_EOK;
}
/*!
* \brief Check the context if writing is possible.
*/
static inline int wire_ctx_can_write(wire_ctx_t *ctx, size_t size)
{
assert(ctx);
if (ctx->error != KNOT_EOK) {
return ctx->error;
}
if (ctx->readonly) {
return KNOT_EACCES;
}
if (wire_ctx_available(ctx) < size) {
return KNOT_ESPACE;
}
return KNOT_EOK;
}
static inline void wire_ctx_read(wire_ctx_t *ctx, void *data, size_t size)
{
assert(ctx);
assert(data);
if (ctx->error != KNOT_EOK) {
/* Avoid leaving data uninitialized. */
memzero(data, size);
return;
}
int ret = wire_ctx_can_read(ctx, size);
if (ret != KNOT_EOK) {
ctx->error = ret;
memzero(data, size);
return;
}
memcpy(data, ctx->position, size);
ctx->position += size;
}
static inline uint8_t wire_ctx_read_u8(wire_ctx_t *ctx)
{
uint8_t result;
wire_ctx_read(ctx, &result, sizeof(result));
return result;
}
static inline uint16_t wire_ctx_read_u16(wire_ctx_t *ctx)
{
uint16_t result;
wire_ctx_read(ctx, &result, sizeof(result));
return be16toh(result);
}
static inline uint32_t wire_ctx_read_u32(wire_ctx_t *ctx)
{
uint32_t result;
wire_ctx_read(ctx, &result, sizeof(result));
return be32toh(result);
}
static inline uint64_t wire_ctx_read_u48(wire_ctx_t *ctx)
{
/* This case is slightly tricky. */
uint64_t result = 0;
wire_ctx_read(ctx, (uint8_t *)&result + 1, 6);
return be64toh(result) >> 8;
}
static inline uint64_t wire_ctx_read_u64(wire_ctx_t *ctx)
{
uint64_t result;
wire_ctx_read(ctx, &result, sizeof(result));
return be64toh(result);
}
static inline void wire_ctx_write(wire_ctx_t *ctx, const void *data, size_t size)
{
assert(ctx);
if (ctx->error != KNOT_EOK) {
return;
}
if (size == 0) {
return;
}
assert(data);
int ret = wire_ctx_can_write(ctx, size);
if (ret != KNOT_EOK) {
ctx->error = ret;
return;
}
memcpy(ctx->position, data, size);
ctx->position += size;
}
static inline void wire_ctx_write_u8(wire_ctx_t *ctx, uint8_t value)
{
wire_ctx_write(ctx, &value, sizeof(value));
}
static inline void wire_ctx_write_u16(wire_ctx_t *ctx, uint16_t value)
{
uint16_t beval = htobe16(value);
wire_ctx_write(ctx, &beval, sizeof(beval));
}
static inline void wire_ctx_write_u32(wire_ctx_t *ctx, uint32_t value)
{
uint32_t beval = htobe32(value);
wire_ctx_write(ctx, &beval, sizeof(beval));
}
static inline void wire_ctx_write_u48(wire_ctx_t *ctx, uint64_t value)
{
/* This case is slightly tricky. */
uint64_t swapped = htobe64(value << 8);
wire_ctx_write(ctx, (uint8_t *)&swapped + 1, 6);
}
static inline void wire_ctx_write_u64(wire_ctx_t *ctx, uint64_t value)
{
uint64_t beval = htobe64(value);
wire_ctx_write(ctx, &beval, sizeof(beval));
}
static inline void wire_ctx_clear(wire_ctx_t *ctx, size_t size)
{
assert(ctx);
if (ctx->error != KNOT_EOK) {
return;
}
if (size == 0) {
return;
}
int ret = wire_ctx_can_write(ctx, size);
if (ret != KNOT_EOK) {
ctx->error = ret;
return;
}
memzero(ctx->position, size);
ctx->position += size;
}
static inline void wire_ctx_copy(wire_ctx_t *dst, wire_ctx_t *src, size_t size)
{
assert(dst);
assert(src);
if (size == 0 || dst->error != KNOT_EOK) {
return;
}
if (wire_ctx_can_read(src, size) != KNOT_EOK) {
dst->error = KNOT_EFEWDATA;
return;
}
int ret = wire_ctx_can_write(dst, size);
if (ret != KNOT_EOK) {
dst->error = ret;
return;
}
memcpy(dst->position, src->position, size);
dst->position += size;
src->position += size;
}
|