1 /* $NetBSD: txt_16.c,v 1.7 2015/07/08 17:28:59 christos Exp $ */
4 * Copyright (C) 2004, 2007-2009, 2012, 2014 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2002 Internet Software Consortium.
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.
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.
20 /* Id: txt_16.c,v 1.47 2009/12/04 22:06:37 tbox Exp */
22 /* Reviewed: Thu Mar 16 15:40:00 PST 2000 by bwelling */
24 #ifndef RDATA_GENERIC_TXT_16_C
25 #define RDATA_GENERIC_TXT_16_C
27 #define RRTYPE_TXT_ATTRIBUTES (0)
29 static inline isc_result_t
30 fromtext_txt(ARGS_FROMTEXT
) {
43 if ((options
& DNS_RDATA_UNKNOWNESCAPE
) != 0) {
45 DE_CONST("#", r
.base
);
47 RETERR(txt_fromtext(&r
, target
));
51 RETERR(isc_lex_getmastertoken(lexer
, &token
,
52 isc_tokentype_qstring
,
54 if (token
.type
!= isc_tokentype_qstring
&&
55 token
.type
!= isc_tokentype_string
)
57 RETTOK(txt_fromtext(&token
.value
.as_textregion
, target
));
60 /* Let upper layer handle eol/eof. */
61 isc_lex_ungettoken(lexer
, &token
);
62 return (strings
== 0 ? ISC_R_UNEXPECTEDEND
: ISC_R_SUCCESS
);
65 static inline isc_result_t
66 totext_txt(ARGS_TOTEXT
) {
71 REQUIRE(rdata
->type
== 16);
73 dns_rdata_toregion(rdata
, ®ion
);
75 while (region
.length
> 0) {
76 RETERR(txt_totext(®ion
, ISC_TRUE
, target
));
77 if (region
.length
> 0)
78 RETERR(str_totext(" ", target
));
81 return (ISC_R_SUCCESS
);
84 static inline isc_result_t
85 fromwire_txt(ARGS_FROMWIRE
) {
96 result
= txt_fromwire(source
, target
);
97 if (result
!= ISC_R_SUCCESS
)
99 } while (!buffer_empty(source
));
100 return (ISC_R_SUCCESS
);
103 static inline isc_result_t
104 towire_txt(ARGS_TOWIRE
) {
107 REQUIRE(rdata
->type
== 16);
111 isc_buffer_availableregion(target
, ®ion
);
112 if (region
.length
< rdata
->length
)
113 return (ISC_R_NOSPACE
);
115 memmove(region
.base
, rdata
->data
, rdata
->length
);
116 isc_buffer_add(target
, rdata
->length
);
117 return (ISC_R_SUCCESS
);
121 compare_txt(ARGS_COMPARE
) {
125 REQUIRE(rdata1
->type
== rdata2
->type
);
126 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
127 REQUIRE(rdata1
->type
== 16);
129 dns_rdata_toregion(rdata1
, &r1
);
130 dns_rdata_toregion(rdata2
, &r2
);
131 return (isc_region_compare(&r1
, &r2
));
134 static inline isc_result_t
135 fromstruct_txt(ARGS_FROMSTRUCT
) {
136 dns_rdata_txt_t
*txt
= source
;
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);
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
);
159 return (mem_tobuffer(target
, txt
->txt
, txt
->txt_len
));
162 static inline isc_result_t
163 tostruct_txt(ARGS_TOSTRUCT
) {
164 dns_rdata_txt_t
*txt
= target
;
167 REQUIRE(rdata
->type
== 16);
168 REQUIRE(target
!= NULL
);
170 txt
->common
.rdclass
= rdata
->rdclass
;
171 txt
->common
.rdtype
= rdata
->type
;
172 ISC_LINK_INIT(&txt
->common
, link
);
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
);
182 return (ISC_R_SUCCESS
);
186 freestruct_txt(ARGS_FREESTRUCT
) {
187 dns_rdata_txt_t
*txt
= source
;
189 REQUIRE(source
!= NULL
);
190 REQUIRE(txt
->common
.rdtype
== 16);
192 if (txt
->mctx
== NULL
)
195 if (txt
->txt
!= NULL
)
196 isc_mem_free(txt
->mctx
, txt
->txt
);
200 static inline isc_result_t
201 additionaldata_txt(ARGS_ADDLDATA
) {
202 REQUIRE(rdata
->type
== 16);
208 return (ISC_R_SUCCESS
);
211 static inline isc_result_t
212 digest_txt(ARGS_DIGEST
) {
215 REQUIRE(rdata
->type
== 16);
217 dns_rdata_toregion(rdata
, &r
);
219 return ((digest
)(arg
, &r
));
222 static inline isc_boolean_t
223 checkowner_txt(ARGS_CHECKOWNER
) {
235 static inline isc_boolean_t
236 checknames_txt(ARGS_CHECKNAMES
) {
238 REQUIRE(rdata
->type
== 16);
247 static inline isc_result_t
248 casecompare_txt(ARGS_COMPARE
) {
249 return (compare_txt(rdata1
, rdata2
));
253 dns_rdata_txt_first(dns_rdata_txt_t
*txt
) {
255 REQUIRE(txt
!= NULL
);
256 REQUIRE(txt
->common
.rdtype
== 16);
257 REQUIRE(txt
->txt
!= NULL
|| txt
->txt_len
== 0);
259 if (txt
->txt_len
== 0)
260 return (ISC_R_NOMORE
);
263 return (ISC_R_SUCCESS
);
267 dns_rdata_txt_next(dns_rdata_txt_t
*txt
) {
271 REQUIRE(txt
!= NULL
);
272 REQUIRE(txt
->common
.rdtype
== 16);
273 REQUIRE(txt
->txt
!= NULL
&& txt
->txt_len
!= 0);
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
);
287 dns_rdata_txt_current(dns_rdata_txt_t
*txt
, dns_rdata_txt_string_t
*string
) {
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
);
296 INSIST(txt
->offset
+ 1 <= txt
->txt_len
);
297 r
.base
= txt
->txt
+ txt
->offset
;
298 r
.length
= txt
->txt_len
- txt
->offset
;
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
);
305 return (ISC_R_SUCCESS
);
307 #endif /* RDATA_GENERIC_TXT_16_C */