Merge from vendor branch OPENSSH:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / dns / rdata / generic / x25_19.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and 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: x25_19.c,v 1.31.2.1 2004/03/09 06:11:35 marka Exp $ */
19
20 /* Reviewed: Thu Mar 16 16:15:57 PST 2000 by bwelling */
21
22 /* RFC 1183 */
23
24 #ifndef RDATA_GENERIC_X25_19_C
25 #define RDATA_GENERIC_X25_19_C
26
27 #define RRTYPE_X25_ATTRIBUTES (0)
28
29 static inline isc_result_t
30 fromtext_x25(ARGS_FROMTEXT) {
31         isc_token_t token;
32         unsigned int i;
33
34         REQUIRE(type == 19);
35
36         UNUSED(type);
37         UNUSED(rdclass);
38         UNUSED(origin);
39         UNUSED(downcase);
40         UNUSED(callbacks);
41
42         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
43                                       ISC_FALSE));
44         if (token.value.as_textregion.length < 4)
45                 RETTOK(DNS_R_SYNTAX);
46         for (i = 0; i < token.value.as_textregion.length; i++)
47                 if (!isdigit(token.value.as_textregion.base[i] & 0xff))
48                         RETTOK(ISC_R_RANGE);
49         RETTOK(txt_fromtext(&token.value.as_textregion, target));
50         return (ISC_R_SUCCESS);
51 }
52
53 static inline isc_result_t
54 totext_x25(ARGS_TOTEXT) {
55         isc_region_t region;
56
57         UNUSED(tctx);
58
59         REQUIRE(rdata->type == 19);
60         REQUIRE(rdata->length != 0);
61
62         dns_rdata_toregion(rdata, &region);
63         return (txt_totext(&region, target));
64 }
65
66 static inline isc_result_t
67 fromwire_x25(ARGS_FROMWIRE) {
68         isc_region_t sr;
69
70         REQUIRE(type == 19);
71
72         UNUSED(type);
73         UNUSED(dctx);
74         UNUSED(rdclass);
75         UNUSED(downcase);
76
77         isc_buffer_activeregion(source, &sr);
78         if (sr.length < 5)
79                 return (DNS_R_FORMERR);
80         return (txt_fromwire(source, target));
81 }
82
83 static inline isc_result_t
84 towire_x25(ARGS_TOWIRE) {
85         UNUSED(cctx);
86
87         REQUIRE(rdata->type == 19);
88         REQUIRE(rdata->length != 0);
89
90         return (mem_tobuffer(target, rdata->data, rdata->length));
91 }
92
93 static inline int
94 compare_x25(ARGS_COMPARE) {
95         isc_region_t r1;
96         isc_region_t r2;
97
98         REQUIRE(rdata1->type == rdata2->type);
99         REQUIRE(rdata1->rdclass == rdata2->rdclass);
100         REQUIRE(rdata1->type == 19);
101         REQUIRE(rdata1->length != 0);
102         REQUIRE(rdata2->length != 0);
103
104         dns_rdata_toregion(rdata1, &r1);
105         dns_rdata_toregion(rdata2, &r2);
106         return (compare_region(&r1, &r2));
107 }
108
109 static inline isc_result_t
110 fromstruct_x25(ARGS_FROMSTRUCT) {
111         dns_rdata_x25_t *x25 = source;
112         isc_uint8_t i;
113
114         REQUIRE(type == 19);
115         REQUIRE(source != NULL);
116         REQUIRE(x25->common.rdtype == type);
117         REQUIRE(x25->common.rdclass == rdclass);
118         REQUIRE(x25->x25 != NULL && x25->x25_len != 0);
119
120         UNUSED(type);
121         UNUSED(rdclass);
122
123         if (x25->x25_len < 4)
124                 return (ISC_R_RANGE);
125
126         for (i = 0; i < x25->x25_len; i++)
127                 if (!isdigit(x25->x25[i] & 0xff))
128                         return (ISC_R_RANGE);
129
130         RETERR(uint8_tobuffer(x25->x25_len, target));
131         return (mem_tobuffer(target, x25->x25, x25->x25_len));
132 }
133
134 static inline isc_result_t
135 tostruct_x25(ARGS_TOSTRUCT) {
136         dns_rdata_x25_t *x25 = target;
137         isc_region_t r;
138
139         REQUIRE(rdata->type == 19);
140         REQUIRE(target != NULL);
141         REQUIRE(rdata->length != 0);
142
143         x25->common.rdclass = rdata->rdclass;
144         x25->common.rdtype = rdata->type;
145         ISC_LINK_INIT(&x25->common, link);
146
147         dns_rdata_toregion(rdata, &r);
148         x25->x25_len = uint8_fromregion(&r);
149         isc_region_consume(&r, 1);
150         x25->x25 = mem_maybedup(mctx, r.base, x25->x25_len);
151         if (x25->x25 == NULL)
152                 return (ISC_R_NOMEMORY);
153
154         x25->mctx = mctx;
155         return (ISC_R_SUCCESS);
156 }
157
158 static inline void
159 freestruct_x25(ARGS_FREESTRUCT) {
160         dns_rdata_x25_t *x25 = source;
161         REQUIRE(source != NULL);
162         REQUIRE(x25->common.rdtype == 19);
163
164         if (x25->mctx == NULL)
165                 return;
166
167         if (x25->x25 != NULL)
168                 isc_mem_free(x25->mctx, x25->x25);
169         x25->mctx = NULL;
170 }
171
172 static inline isc_result_t
173 additionaldata_x25(ARGS_ADDLDATA) {
174         REQUIRE(rdata->type == 19);
175
176         UNUSED(rdata);
177         UNUSED(add);
178         UNUSED(arg);
179
180         return (ISC_R_SUCCESS);
181 }
182
183 static inline isc_result_t
184 digest_x25(ARGS_DIGEST) {
185         isc_region_t r;
186
187         REQUIRE(rdata->type == 19);
188
189         dns_rdata_toregion(rdata, &r);
190
191         return ((digest)(arg, &r));
192 }
193
194 #endif  /* RDATA_GENERIC_X25_19_C */