summaryrefslogtreecommitdiffstats
path: root/src/libknot/yparser/yptrafo.h
blob: 59fd4c8485582d930c9912f388ccb39b224953bc (plain)
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
/*  Copyright (C) 2018 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 <http://www.gnu.org/licenses/>.
*/
/*!
 * \file
 *
 * \brief Value transformations for Yparser.
 *
 * \addtogroup yparser
 * @{
 */

#pragma once

#include "libknot/yparser/ypschema.h"
#include "libknot/lookup.h"
#include "libknot/wire.h"

/*!
 * Transforms textual item value to binary form.
 *
 * \param[in] item Schema item to transform.
 * \param[in] txt Value to transform.
 * \param[in] txt_len Value length.
 * \param[out] bin Output buffer.
 * \param[in, out] bin_len Output buffer length, output length.
 *
 * \return Error code, KNOT_EOK if success.
 */
int yp_item_to_bin(
	const yp_item_t *item,
	const char *txt,
	size_t txt_len,
	uint8_t *bin,
	size_t *bin_len
);

/*!
 * Transforms binary item value to textual form.
 *
 * \param[in] item Schema item to transform.
 * \param[in] bin Value to transform.
 * \param[in] bin_len Value length.
 * \param[out] txt Output buffer.
 * \param[in, out] txt_len Output buffer length, output length.
 * \param[in] style Value style.
 *
 * \return Error code, KNOT_EOK if success.
 */
int yp_item_to_txt(
	const yp_item_t *item,
	const uint8_t *bin,
	size_t bin_len,
	char *txt,
	size_t *txt_len,
	yp_style_t style
);

/*!
 * Converts binary value to string pointer.
 *
 * \param[in] data Binary value to transform.
 *
 * \return String pointer.
 */
inline static const char* yp_str(
	const uint8_t *data)
{
	return (const char *)data;
}

/*!
 * Converts binary value to boolean value.
 *
 * \param[in] data Binary value to transform.
 *
 * \return Boolean value.
 */
inline static bool yp_bool(
	const uint8_t *data)
{
	return (data[0] == 0) ? false : true;
}

/*!
 * Converts binary value to integer value.
 *
 * \param[in] data Binary value to transform.
 *
 * \return Integer value.
 */
inline static int64_t yp_int(
	const uint8_t *data)
{
	return (int64_t)knot_wire_read_u64(data);
}

/*!
 * Converts binary value to address value.
 *
 * \param[in] data Binary value to transform.
 *
 * \return Address value.
 */
struct sockaddr_storage yp_addr_noport(
	const uint8_t *data
);

/*!
 * Converts binary value to address value with an optional port.
 *
 * \param[in] data Binary value to transform.
 * \param[out] no_port No port indicator.
 *
 * \return Address value.
 */
struct sockaddr_storage yp_addr(
	const uint8_t *data,
	bool *no_port
);

/*!
 * Converts binary value to option value.
 *
 * \param[in] data Binary value to transform.
 *
 * \return Unsigned value.
 */
inline static unsigned yp_opt(
	const uint8_t *data)
{
	return (unsigned)data[0];
}

/*!
 * Converts binary value to dname pointer.
 *
 * \param[in] data Binary value to transform.
 *
 * \return Dname pointer.
 */
inline static const uint8_t* yp_dname(
	const uint8_t *data)
{
	return data;
}

/*!
 * Converts binary value to data pointer.
 *
 * Applies to all data types with 2-byte length prefix (YP_THEX, YP_TB64).
 *
 * \param[in] data Binary value to transform.
 *
 * \return Data pointer.
 */
inline static const uint8_t* yp_bin(
	const uint8_t *data)
{
	return data + 2;
}

/*!
 * Gets binary value length.
 *
 * Applies to all data types with 2-byte length prefix (YP_THEX, YP_TB64).
 *
 * \param[in] data Binary value to transform.
 *
 * \return Data length.
 */
inline static size_t yp_bin_len(
	const uint8_t *data)
{
	return knot_wire_read_u16(data);
}

/*!
 * \brief Helper macros for conversion functions.
 */

#define YP_CHECK_CTX \
	if (in->error != KNOT_EOK) { \
		return in->error; \
	} else if (out->error != KNOT_EOK) { \
		return out->error; \
	} \

#define YP_CHECK_STOP \
	if (stop != NULL) { \
		assert(stop <= in->position + wire_ctx_available(in)); \
	} else { \
		stop = in->position + wire_ctx_available(in); \
	}

#define YP_LEN (stop - in->position)

#define YP_CHECK_PARAMS_BIN \
	YP_CHECK_CTX YP_CHECK_STOP

#define YP_CHECK_PARAMS_TXT \
	YP_CHECK_CTX

#define YP_CHECK_RET \
	YP_CHECK_CTX return KNOT_EOK;

/*!
 * \brief Conversion functions for basic types.
 */

int yp_str_to_bin(
	YP_TXT_BIN_PARAMS
);

int yp_str_to_txt(
	YP_BIN_TXT_PARAMS
);

int yp_bool_to_bin(
	YP_TXT_BIN_PARAMS
);

int yp_bool_to_txt(
	YP_BIN_TXT_PARAMS
);

int yp_int_to_bin(
	YP_TXT_BIN_PARAMS,
	int64_t min,
	int64_t max,
	yp_style_t style
);

int yp_int_to_txt(
	YP_BIN_TXT_PARAMS,
	yp_style_t style
);

int yp_addr_noport_to_bin(
	YP_TXT_BIN_PARAMS,
	bool allow_unix
);

int yp_addr_noport_to_txt(
	YP_BIN_TXT_PARAMS
);

int yp_addr_to_bin(
	YP_TXT_BIN_PARAMS
);

int yp_addr_to_txt(
	YP_BIN_TXT_PARAMS
);

int yp_addr_range_to_bin(
	YP_TXT_BIN_PARAMS
);

int yp_addr_range_to_txt(
	YP_BIN_TXT_PARAMS
);

int yp_option_to_bin(
	YP_TXT_BIN_PARAMS,
	const struct knot_lookup *opts
);

int yp_option_to_txt(
	YP_BIN_TXT_PARAMS,
	const struct knot_lookup *opts
);

int yp_dname_to_bin(
	YP_TXT_BIN_PARAMS
);

int yp_dname_to_txt(
	YP_BIN_TXT_PARAMS
);

int yp_hex_to_bin(
	YP_TXT_BIN_PARAMS
);

int yp_hex_to_txt(
	YP_BIN_TXT_PARAMS
);

int yp_base64_to_bin(
	YP_TXT_BIN_PARAMS
);

int yp_base64_to_txt(
	YP_BIN_TXT_PARAMS
);

/*! @} */