1/*	$NetBSD$	*/
2
3/*
4 * Copyright (C) 2004, 2007-2009  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
29static inline isc_result_t
30fromtext_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	for (;;) {
44		RETERR(isc_lex_getmastertoken(lexer, &token,
45					      isc_tokentype_qstring,
46					      ISC_TRUE));
47		if (token.type != isc_tokentype_qstring &&
48		    token.type != isc_tokentype_string)
49			break;
50		RETTOK(txt_fromtext(&token.value.as_textregion, target));
51		strings++;
52	}
53	/* Let upper layer handle eol/eof. */
54	isc_lex_ungettoken(lexer, &token);
55	return (strings == 0 ? ISC_R_UNEXPECTEDEND : ISC_R_SUCCESS);
56}
57
58static inline isc_result_t
59totext_txt(ARGS_TOTEXT) {
60	isc_region_t region;
61
62	UNUSED(tctx);
63
64	REQUIRE(rdata->type == 16);
65
66	dns_rdata_toregion(rdata, &region);
67
68	while (region.length > 0) {
69		RETERR(txt_totext(&region, target));
70		if (region.length > 0)
71			RETERR(str_totext(" ", target));
72	}
73
74	return (ISC_R_SUCCESS);
75}
76
77static inline isc_result_t
78fromwire_txt(ARGS_FROMWIRE) {
79	isc_result_t result;
80
81	REQUIRE(type == 16);
82
83	UNUSED(type);
84	UNUSED(dctx);
85	UNUSED(rdclass);
86	UNUSED(options);
87
88	do {
89		result = txt_fromwire(source, target);
90		if (result != ISC_R_SUCCESS)
91			return (result);
92	} while (!buffer_empty(source));
93	return (ISC_R_SUCCESS);
94}
95
96static inline isc_result_t
97towire_txt(ARGS_TOWIRE) {
98	isc_region_t region;
99
100	REQUIRE(rdata->type == 16);
101
102	UNUSED(cctx);
103
104	isc_buffer_availableregion(target, &region);
105	if (region.length < rdata->length)
106		return (ISC_R_NOSPACE);
107
108	memcpy(region.base, rdata->data, rdata->length);
109	isc_buffer_add(target, rdata->length);
110	return (ISC_R_SUCCESS);
111}
112
113static inline int
114compare_txt(ARGS_COMPARE) {
115	isc_region_t r1;
116	isc_region_t r2;
117
118	REQUIRE(rdata1->type == rdata2->type);
119	REQUIRE(rdata1->rdclass == rdata2->rdclass);
120	REQUIRE(rdata1->type == 16);
121
122	dns_rdata_toregion(rdata1, &r1);
123	dns_rdata_toregion(rdata2, &r2);
124	return (isc_region_compare(&r1, &r2));
125}
126
127static inline isc_result_t
128fromstruct_txt(ARGS_FROMSTRUCT) {
129	dns_rdata_txt_t *txt = source;
130	isc_region_t region;
131	isc_uint8_t length;
132
133	REQUIRE(type == 16);
134	REQUIRE(source != NULL);
135	REQUIRE(txt->common.rdtype == type);
136	REQUIRE(txt->common.rdclass == rdclass);
137	REQUIRE(txt->txt != NULL && txt->txt_len != 0);
138
139	UNUSED(type);
140	UNUSED(rdclass);
141
142	region.base = txt->txt;
143	region.length = txt->txt_len;
144	while (region.length > 0) {
145		length = uint8_fromregion(&region);
146		isc_region_consume(&region, 1);
147		if (region.length < length)
148			return (ISC_R_UNEXPECTEDEND);
149		isc_region_consume(&region, length);
150	}
151
152	return (mem_tobuffer(target, txt->txt, txt->txt_len));
153}
154
155static inline isc_result_t
156tostruct_txt(ARGS_TOSTRUCT) {
157	dns_rdata_txt_t *txt = target;
158	isc_region_t r;
159
160	REQUIRE(rdata->type == 16);
161	REQUIRE(target != NULL);
162
163	txt->common.rdclass = rdata->rdclass;
164	txt->common.rdtype = rdata->type;
165	ISC_LINK_INIT(&txt->common, link);
166
167	dns_rdata_toregion(rdata, &r);
168	txt->txt_len = r.length;
169	txt->txt = mem_maybedup(mctx, r.base, r.length);
170	if (txt->txt == NULL)
171		return (ISC_R_NOMEMORY);
172
173	txt->offset = 0;
174	txt->mctx = mctx;
175	return (ISC_R_SUCCESS);
176}
177
178static inline void
179freestruct_txt(ARGS_FREESTRUCT) {
180	dns_rdata_txt_t *txt = source;
181
182	REQUIRE(source != NULL);
183	REQUIRE(txt->common.rdtype == 16);
184
185	if (txt->mctx == NULL)
186		return;
187
188	if (txt->txt != NULL)
189		isc_mem_free(txt->mctx, txt->txt);
190	txt->mctx = NULL;
191}
192
193static inline isc_result_t
194additionaldata_txt(ARGS_ADDLDATA) {
195	REQUIRE(rdata->type == 16);
196
197	UNUSED(rdata);
198	UNUSED(add);
199	UNUSED(arg);
200
201	return (ISC_R_SUCCESS);
202}
203
204static inline isc_result_t
205digest_txt(ARGS_DIGEST) {
206	isc_region_t r;
207
208	REQUIRE(rdata->type == 16);
209
210	dns_rdata_toregion(rdata, &r);
211
212	return ((digest)(arg, &r));
213}
214
215static inline isc_boolean_t
216checkowner_txt(ARGS_CHECKOWNER) {
217
218	REQUIRE(type == 16);
219
220	UNUSED(name);
221	UNUSED(type);
222	UNUSED(rdclass);
223	UNUSED(wildcard);
224
225	return (ISC_TRUE);
226}
227
228static inline isc_boolean_t
229checknames_txt(ARGS_CHECKNAMES) {
230
231	REQUIRE(rdata->type == 16);
232
233	UNUSED(rdata);
234	UNUSED(owner);
235	UNUSED(bad);
236
237	return (ISC_TRUE);
238}
239
240static inline isc_result_t
241casecompare_txt(ARGS_COMPARE) {
242	return (compare_txt(rdata1, rdata2));
243}
244
245#endif	/* RDATA_GENERIC_TXT_16_C */
246