1 /* $NetBSD: txt_16.c,v 1.7 2015/07/08 17:28:59 christos Exp $ */
2
3 /*
4 * Copyright (C) 2004, 2007-2009, 2012, 2014 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2002 Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /* Id: txt_16.c,v 1.47 2009/12/04 22:06:37 tbox Exp */
21
22 /* Reviewed: Thu Mar 16 15:40:00 PST 2000 by bwelling */
23
24 #ifndef RDATA_GENERIC_TXT_16_C
25 #define RDATA_GENERIC_TXT_16_C
26
27 #define RRTYPE_TXT_ATTRIBUTES (0)
28
29 static inline isc_result_t
fromtext_txt(ARGS_FROMTEXT)30 fromtext_txt(ARGS_FROMTEXT) {
31 isc_token_t token;
32 int strings;
33
34 REQUIRE(type == 16);
35
36 UNUSED(type);
37 UNUSED(rdclass);
38 UNUSED(origin);
39 UNUSED(options);
40 UNUSED(callbacks);
41
42 strings = 0;
43 if ((options & DNS_RDATA_UNKNOWNESCAPE) != 0) {
44 isc_textregion_t r;
45 DE_CONST("#", r.base);
46 r.length = 1;
47 RETERR(txt_fromtext(&r, target));
48 strings++;
49 }
50 for (;;) {
51 RETERR(isc_lex_getmastertoken(lexer, &token,
52 isc_tokentype_qstring,
53 ISC_TRUE));
54 if (token.type != isc_tokentype_qstring &&
55 token.type != isc_tokentype_string)
56 break;
57 RETTOK(txt_fromtext(&token.value.as_textregion, target));
58 strings++;
59 }
60 /* Let upper layer handle eol/eof. */
61 isc_lex_ungettoken(lexer, &token);
62 return (strings == 0 ? ISC_R_UNEXPECTEDEND : ISC_R_SUCCESS);
63 }
64
65 static inline isc_result_t
totext_txt(ARGS_TOTEXT)66 totext_txt(ARGS_TOTEXT) {
67 isc_region_t region;
68
69 UNUSED(tctx);
70
71 REQUIRE(rdata->type == 16);
72
73 dns_rdata_toregion(rdata, ®ion);
74
75 while (region.length > 0) {
76 RETERR(txt_totext(®ion, ISC_TRUE, target));
77 if (region.length > 0)
78 RETERR(str_totext(" ", target));
79 }
80
81 return (ISC_R_SUCCESS);
82 }
83
84 static inline isc_result_t
fromwire_txt(ARGS_FROMWIRE)85 fromwire_txt(ARGS_FROMWIRE) {
86 isc_result_t result;
87
88 REQUIRE(type == 16);
89
90 UNUSED(type);
91 UNUSED(dctx);
92 UNUSED(rdclass);
93 UNUSED(options);
94
95 do {
96 result = txt_fromwire(source, target);
97 if (result != ISC_R_SUCCESS)
98 return (result);
99 } while (!buffer_empty(source));
100 return (ISC_R_SUCCESS);
101 }
102
103 static inline isc_result_t
towire_txt(ARGS_TOWIRE)104 towire_txt(ARGS_TOWIRE) {
105 isc_region_t region;
106
107 REQUIRE(rdata->type == 16);
108
109 UNUSED(cctx);
110
111 isc_buffer_availableregion(target, ®ion);
112 if (region.length < rdata->length)
113 return (ISC_R_NOSPACE);
114
115 memmove(region.base, rdata->data, rdata->length);
116 isc_buffer_add(target, rdata->length);
117 return (ISC_R_SUCCESS);
118 }
119
120 static inline int
compare_txt(ARGS_COMPARE)121 compare_txt(ARGS_COMPARE) {
122 isc_region_t r1;
123 isc_region_t r2;
124
125 REQUIRE(rdata1->type == rdata2->type);
126 REQUIRE(rdata1->rdclass == rdata2->rdclass);
127 REQUIRE(rdata1->type == 16);
128
129 dns_rdata_toregion(rdata1, &r1);
130 dns_rdata_toregion(rdata2, &r2);
131 return (isc_region_compare(&r1, &r2));
132 }
133
134 static inline isc_result_t
fromstruct_txt(ARGS_FROMSTRUCT)135 fromstruct_txt(ARGS_FROMSTRUCT) {
136 dns_rdata_txt_t *txt = source;
137 isc_region_t region;
138 isc_uint8_t length;
139
140 REQUIRE(type == 16);
141 REQUIRE(source != NULL);
142 REQUIRE(txt->common.rdtype == type);
143 REQUIRE(txt->common.rdclass == rdclass);
144 REQUIRE(txt->txt != NULL && txt->txt_len != 0);
145
146 UNUSED(type);
147 UNUSED(rdclass);
148
149 region.base = txt->txt;
150 region.length = txt->txt_len;
151 while (region.length > 0) {
152 length = uint8_fromregion(®ion);
153 isc_region_consume(®ion, 1);
154 if (region.length < length)
155 return (ISC_R_UNEXPECTEDEND);
156 isc_region_consume(®ion, length);
157 }
158
159 return (mem_tobuffer(target, txt->txt, txt->txt_len));
160 }
161
162 static inline isc_result_t
tostruct_txt(ARGS_TOSTRUCT)163 tostruct_txt(ARGS_TOSTRUCT) {
164 dns_rdata_txt_t *txt = target;
165 isc_region_t r;
166
167 REQUIRE(rdata->type == 16);
168 REQUIRE(target != NULL);
169
170 txt->common.rdclass = rdata->rdclass;
171 txt->common.rdtype = rdata->type;
172 ISC_LINK_INIT(&txt->common, link);
173
174 dns_rdata_toregion(rdata, &r);
175 txt->txt_len = r.length;
176 txt->txt = mem_maybedup(mctx, r.base, r.length);
177 if (txt->txt == NULL)
178 return (ISC_R_NOMEMORY);
179
180 txt->offset = 0;
181 txt->mctx = mctx;
182 return (ISC_R_SUCCESS);
183 }
184
185 static inline void
freestruct_txt(ARGS_FREESTRUCT)186 freestruct_txt(ARGS_FREESTRUCT) {
187 dns_rdata_txt_t *txt = source;
188
189 REQUIRE(source != NULL);
190 REQUIRE(txt->common.rdtype == 16);
191
192 if (txt->mctx == NULL)
193 return;
194
195 if (txt->txt != NULL)
196 isc_mem_free(txt->mctx, txt->txt);
197 txt->mctx = NULL;
198 }
199
200 static inline isc_result_t
additionaldata_txt(ARGS_ADDLDATA)201 additionaldata_txt(ARGS_ADDLDATA) {
202 REQUIRE(rdata->type == 16);
203
204 UNUSED(rdata);
205 UNUSED(add);
206 UNUSED(arg);
207
208 return (ISC_R_SUCCESS);
209 }
210
211 static inline isc_result_t
digest_txt(ARGS_DIGEST)212 digest_txt(ARGS_DIGEST) {
213 isc_region_t r;
214
215 REQUIRE(rdata->type == 16);
216
217 dns_rdata_toregion(rdata, &r);
218
219 return ((digest)(arg, &r));
220 }
221
222 static inline isc_boolean_t
checkowner_txt(ARGS_CHECKOWNER)223 checkowner_txt(ARGS_CHECKOWNER) {
224
225 REQUIRE(type == 16);
226
227 UNUSED(name);
228 UNUSED(type);
229 UNUSED(rdclass);
230 UNUSED(wildcard);
231
232 return (ISC_TRUE);
233 }
234
235 static inline isc_boolean_t
checknames_txt(ARGS_CHECKNAMES)236 checknames_txt(ARGS_CHECKNAMES) {
237
238 REQUIRE(rdata->type == 16);
239
240 UNUSED(rdata);
241 UNUSED(owner);
242 UNUSED(bad);
243
244 return (ISC_TRUE);
245 }
246
247 static inline isc_result_t
casecompare_txt(ARGS_COMPARE)248 casecompare_txt(ARGS_COMPARE) {
249 return (compare_txt(rdata1, rdata2));
250 }
251
252 isc_result_t
dns_rdata_txt_first(dns_rdata_txt_t * txt)253 dns_rdata_txt_first(dns_rdata_txt_t *txt) {
254
255 REQUIRE(txt != NULL);
256 REQUIRE(txt->common.rdtype == 16);
257 REQUIRE(txt->txt != NULL || txt->txt_len == 0);
258
259 if (txt->txt_len == 0)
260 return (ISC_R_NOMORE);
261
262 txt->offset = 0;
263 return (ISC_R_SUCCESS);
264 }
265
266 isc_result_t
dns_rdata_txt_next(dns_rdata_txt_t * txt)267 dns_rdata_txt_next(dns_rdata_txt_t *txt) {
268 isc_region_t r;
269 isc_uint8_t length;
270
271 REQUIRE(txt != NULL);
272 REQUIRE(txt->common.rdtype == 16);
273 REQUIRE(txt->txt != NULL && txt->txt_len != 0);
274
275 INSIST(txt->offset + 1 <= txt->txt_len);
276 r.base = txt->txt + txt->offset;
277 r.length = txt->txt_len - txt->offset;
278 length = uint8_fromregion(&r);
279 INSIST(txt->offset + 1 + length <= txt->txt_len);
280 txt->offset = txt->offset + 1 + length;
281 if (txt->offset == txt->txt_len)
282 return (ISC_R_NOMORE);
283 return (ISC_R_SUCCESS);
284 }
285
286 isc_result_t
dns_rdata_txt_current(dns_rdata_txt_t * txt,dns_rdata_txt_string_t * string)287 dns_rdata_txt_current(dns_rdata_txt_t *txt, dns_rdata_txt_string_t *string) {
288 isc_region_t r;
289
290 REQUIRE(txt != NULL);
291 REQUIRE(string != NULL);
292 REQUIRE(txt->common.rdtype == 16);
293 REQUIRE(txt->txt != NULL);
294 REQUIRE(txt->offset < txt->txt_len);
295
296 INSIST(txt->offset + 1 <= txt->txt_len);
297 r.base = txt->txt + txt->offset;
298 r.length = txt->txt_len - txt->offset;
299
300 string->length = uint8_fromregion(&r);
301 isc_region_consume(&r, 1);
302 string->data = r.base;
303 INSIST(txt->offset + 1 + string->length <= txt->txt_len);
304
305 return (ISC_R_SUCCESS);
306 }
307 #endif /* RDATA_GENERIC_TXT_16_C */
308