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