]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/lib/dns/rdata.c
MFC r363988:
[FreeBSD/stable/9.git] / contrib / bind9 / lib / dns / rdata.c
1 /*
2  * Copyright (C) 2004-2015  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2003  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$ */
19
20 /*! \file */
21
22 #include <config.h>
23 #include <ctype.h>
24
25 #include <isc/base64.h>
26 #include <isc/hex.h>
27 #include <isc/lex.h>
28 #include <isc/mem.h>
29 #include <isc/parseint.h>
30 #include <isc/print.h>
31 #include <isc/string.h>
32 #include <isc/stdlib.h>
33 #include <isc/util.h>
34
35 #include <dns/callbacks.h>
36 #include <dns/cert.h>
37 #include <dns/compress.h>
38 #include <dns/enumtype.h>
39 #include <dns/keyflags.h>
40 #include <dns/keyvalues.h>
41 #include <dns/message.h>
42 #include <dns/rcode.h>
43 #include <dns/rdata.h>
44 #include <dns/rdataclass.h>
45 #include <dns/rdatastruct.h>
46 #include <dns/rdatatype.h>
47 #include <dns/result.h>
48 #include <dns/secalg.h>
49 #include <dns/secproto.h>
50 #include <dns/time.h>
51 #include <dns/ttl.h>
52
53 #define RETERR(x) \
54         do { \
55                 isc_result_t _r = (x); \
56                 if (_r != ISC_R_SUCCESS) \
57                         return (_r); \
58         } while (0)
59
60 #define RETTOK(x) \
61         do { \
62                 isc_result_t _r = (x); \
63                 if (_r != ISC_R_SUCCESS) { \
64                         isc_lex_ungettoken(lexer, &token); \
65                         return (_r); \
66                 } \
67         } while (0)
68
69 #define DNS_AS_STR(t) ((t).value.as_textregion.base)
70
71 #define ARGS_FROMTEXT   int rdclass, dns_rdatatype_t type, \
72                         isc_lex_t *lexer, dns_name_t *origin, \
73                         unsigned int options, isc_buffer_t *target, \
74                         dns_rdatacallbacks_t *callbacks
75
76 #define ARGS_TOTEXT     dns_rdata_t *rdata, dns_rdata_textctx_t *tctx, \
77                         isc_buffer_t *target
78
79 #define ARGS_FROMWIRE   int rdclass, dns_rdatatype_t type, \
80                         isc_buffer_t *source, dns_decompress_t *dctx, \
81                         unsigned int options, isc_buffer_t *target
82
83 #define ARGS_TOWIRE     dns_rdata_t *rdata, dns_compress_t *cctx, \
84                         isc_buffer_t *target
85
86 #define ARGS_COMPARE    const dns_rdata_t *rdata1, const dns_rdata_t *rdata2
87
88 #define ARGS_FROMSTRUCT int rdclass, dns_rdatatype_t type, \
89                         void *source, isc_buffer_t *target
90
91 #define ARGS_TOSTRUCT   dns_rdata_t *rdata, void *target, isc_mem_t *mctx
92
93 #define ARGS_FREESTRUCT void *source
94
95 #define ARGS_ADDLDATA   dns_rdata_t *rdata, dns_additionaldatafunc_t add, \
96                         void *arg
97
98 #define ARGS_DIGEST     dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg
99
100 #define ARGS_CHECKOWNER dns_name_t *name, dns_rdataclass_t rdclass, \
101                         dns_rdatatype_t type, isc_boolean_t wildcard
102
103 #define ARGS_CHECKNAMES dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad
104
105
106 /*%
107  * Context structure for the totext_ functions.
108  * Contains formatting options for rdata-to-text
109  * conversion.
110  */
111 typedef struct dns_rdata_textctx {
112         dns_name_t *origin;     /*%< Current origin, or NULL. */
113         unsigned int flags;     /*%< DNS_STYLEFLAG_*  */
114         unsigned int width;     /*%< Width of rdata column. */
115         const char *linebreak;  /*%< Line break string. */
116 } dns_rdata_textctx_t;
117
118 static isc_result_t
119 txt_totext(isc_region_t *source, isc_boolean_t quote, isc_buffer_t *target);
120
121 static isc_result_t
122 txt_fromtext(isc_textregion_t *source, isc_buffer_t *target);
123
124 static isc_result_t
125 txt_fromwire(isc_buffer_t *source, isc_buffer_t *target);
126
127 static isc_result_t
128 multitxt_totext(isc_region_t *source, isc_buffer_t *target);
129
130 static isc_result_t
131 multitxt_fromtext(isc_textregion_t *source, isc_buffer_t *target);
132
133 static isc_boolean_t
134 name_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target);
135
136 static unsigned int
137 name_length(dns_name_t *name);
138
139 static isc_result_t
140 str_totext(const char *source, isc_buffer_t *target);
141
142 static isc_result_t
143 inet_totext(int af, isc_region_t *src, isc_buffer_t *target);
144
145 static isc_boolean_t
146 buffer_empty(isc_buffer_t *source);
147
148 static void
149 buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region);
150
151 static isc_result_t
152 uint32_tobuffer(isc_uint32_t, isc_buffer_t *target);
153
154 static isc_result_t
155 uint16_tobuffer(isc_uint32_t, isc_buffer_t *target);
156
157 static isc_result_t
158 uint8_tobuffer(isc_uint32_t, isc_buffer_t *target);
159
160 static isc_result_t
161 name_tobuffer(dns_name_t *name, isc_buffer_t *target);
162
163 static isc_uint32_t
164 uint32_fromregion(isc_region_t *region);
165
166 static isc_uint16_t
167 uint16_fromregion(isc_region_t *region);
168
169 static isc_uint8_t
170 uint8_fromregion(isc_region_t *region);
171
172 static isc_uint8_t
173 uint8_consume_fromregion(isc_region_t *region);
174
175 static isc_result_t
176 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
177
178 static int
179 hexvalue(char value);
180
181 static int
182 decvalue(char value);
183
184 static isc_result_t
185 btoa_totext(unsigned char *inbuf, int inbuflen, isc_buffer_t *target);
186
187 static isc_result_t
188 atob_tobuffer(isc_lex_t *lexer, isc_buffer_t *target);
189
190 static void
191 default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *, ...)
192      ISC_FORMAT_PRINTF(2, 3);
193
194 static void
195 fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
196                dns_rdatacallbacks_t *callbacks, const char *name,
197                unsigned long line, isc_token_t *token, isc_result_t result);
198
199 static void
200 fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks);
201
202 static isc_result_t
203 rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
204              isc_buffer_t *target);
205
206 static void
207 warn_badname(dns_name_t *name, isc_lex_t *lexer,
208              dns_rdatacallbacks_t *callbacks);
209
210 static void
211 warn_badmx(isc_token_t *token, isc_lex_t *lexer,
212            dns_rdatacallbacks_t *callbacks);
213
214 static isc_uint16_t
215 uint16_consume_fromregion(isc_region_t *region);
216
217 static isc_result_t
218 unknown_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
219                isc_buffer_t *target);
220
221 static inline isc_result_t
222 generic_fromtext_key(ARGS_FROMTEXT);
223
224 static inline isc_result_t
225 generic_totext_key(ARGS_TOTEXT);
226
227 static inline isc_result_t
228 generic_fromwire_key(ARGS_FROMWIRE);
229
230 static inline isc_result_t
231 generic_fromstruct_key(ARGS_FROMSTRUCT);
232
233 static inline isc_result_t
234 generic_tostruct_key(ARGS_TOSTRUCT);
235
236 static inline void
237 generic_freestruct_key(ARGS_FREESTRUCT);
238
239 static isc_result_t
240 generic_fromtext_txt(ARGS_FROMTEXT);
241
242 static isc_result_t
243 generic_totext_txt(ARGS_TOTEXT);
244
245 static isc_result_t
246 generic_fromwire_txt(ARGS_FROMWIRE);
247
248 static isc_result_t
249 generic_fromstruct_txt(ARGS_FROMSTRUCT);
250
251 static isc_result_t
252 generic_tostruct_txt(ARGS_TOSTRUCT);
253
254 static void
255 generic_freestruct_txt(ARGS_FREESTRUCT);
256
257 static isc_result_t
258 generic_txt_first(dns_rdata_txt_t *txt);
259
260 static isc_result_t
261 generic_txt_next(dns_rdata_txt_t *txt);
262
263 static isc_result_t
264 generic_txt_current(dns_rdata_txt_t *txt, dns_rdata_txt_string_t *string);
265
266 static isc_result_t
267 generic_totext_ds(ARGS_TOTEXT);
268
269 static isc_result_t
270 generic_tostruct_ds(ARGS_TOSTRUCT);
271
272 static isc_result_t
273 generic_fromtext_ds(ARGS_FROMTEXT);
274
275 static isc_result_t
276 generic_fromwire_ds(ARGS_FROMWIRE);
277
278 static isc_result_t
279 generic_fromstruct_ds(ARGS_FROMSTRUCT);
280
281 static isc_result_t
282 generic_fromtext_tlsa(ARGS_FROMTEXT);
283
284 static isc_result_t
285 generic_totext_tlsa(ARGS_TOTEXT);
286
287 static isc_result_t
288 generic_fromwire_tlsa(ARGS_FROMWIRE);
289
290 static isc_result_t
291 generic_fromstruct_tlsa(ARGS_FROMSTRUCT);
292
293 static isc_result_t
294 generic_tostruct_tlsa(ARGS_TOSTRUCT);
295
296 static void
297 generic_freestruct_tlsa(ARGS_FREESTRUCT);
298
299 /*% INT16 Size */
300 #define NS_INT16SZ      2
301 /*% IPv6 Address Size */
302 #define NS_LOCATORSZ    8
303
304 /*
305  * Active Diretory gc._msdcs.<forest> prefix.
306  */
307 static unsigned char gc_msdcs_data[]  = "\002gc\006_msdcs";
308 static unsigned char gc_msdcs_offset [] = { 0, 3 };
309
310 static const dns_name_t gc_msdcs = {
311         DNS_NAME_MAGIC,
312         gc_msdcs_data, 10, 2,
313         DNS_NAMEATTR_READONLY,
314         gc_msdcs_offset, NULL,
315         {(void *)-1, (void *)-1},
316         {NULL, NULL}
317 };
318
319 /*%
320  *      convert presentation level address to network order binary form.
321  * \return
322  *      1 if `src' is a valid [RFC1884 2.2] address, else 0.
323  * \note
324  *      (1) does not touch `dst' unless it's returning 1.
325  */
326 static inline int
327 locator_pton(const char *src, unsigned char *dst) {
328         static const char xdigits_l[] = "0123456789abcdef",
329                           xdigits_u[] = "0123456789ABCDEF";
330         unsigned char tmp[NS_LOCATORSZ];
331         unsigned char *tp = tmp, *endp;
332         const char *xdigits;
333         int ch, seen_xdigits;
334         unsigned int val;
335
336         memset(tp, '\0', NS_LOCATORSZ);
337         endp = tp + NS_LOCATORSZ;
338         seen_xdigits = 0;
339         val = 0;
340         while ((ch = *src++) != '\0') {
341                 const char *pch;
342
343                 pch = strchr((xdigits = xdigits_l), ch);
344                 if (pch == NULL)
345                         pch = strchr((xdigits = xdigits_u), ch);
346                 if (pch != NULL) {
347                         val <<= 4;
348                         val |= (pch - xdigits);
349                         if (++seen_xdigits > 4)
350                                 return (0);
351                         continue;
352                 }
353                 if (ch == ':') {
354                         if (!seen_xdigits)
355                                 return (0);
356                         if (tp + NS_INT16SZ > endp)
357                                 return (0);
358                         *tp++ = (unsigned char) (val >> 8) & 0xff;
359                         *tp++ = (unsigned char) val & 0xff;
360                         seen_xdigits = 0;
361                         val = 0;
362                         continue;
363                 }
364                 return (0);
365         }
366         if (seen_xdigits) {
367                 if (tp + NS_INT16SZ > endp)
368                         return (0);
369                 *tp++ = (unsigned char) (val >> 8) & 0xff;
370                 *tp++ = (unsigned char) val & 0xff;
371         }
372         if (tp != endp)
373                 return (0);
374         memmove(dst, tmp, NS_LOCATORSZ);
375         return (1);
376 }
377
378 static inline int
379 getquad(const void *src, struct in_addr *dst,
380         isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks)
381 {
382         int result;
383         struct in_addr *tmp;
384
385         result = inet_aton(src, dst);
386         if (result == 1 && callbacks != NULL &&
387             inet_pton(AF_INET, src, &tmp) != 1) {
388                 const char *name = isc_lex_getsourcename(lexer);
389                 if (name == NULL)
390                         name = "UNKNOWN";
391                 (*callbacks->warn)(callbacks, "%s:%lu: \"%s\" "
392                                    "is not a decimal dotted quad", name,
393                                    isc_lex_getsourceline(lexer), src);
394         }
395         return (result);
396 }
397
398 static inline isc_result_t
399 name_duporclone(dns_name_t *source, isc_mem_t *mctx, dns_name_t *target) {
400
401         if (mctx != NULL)
402                 return (dns_name_dup(source, mctx, target));
403         dns_name_clone(source, target);
404         return (ISC_R_SUCCESS);
405 }
406
407 static inline void *
408 mem_maybedup(isc_mem_t *mctx, void *source, size_t length) {
409         void *copy;
410
411         if (mctx == NULL)
412                 return (source);
413         copy = isc_mem_allocate(mctx, length);
414         if (copy != NULL)
415                 memmove(copy, source, length);
416
417         return (copy);
418 }
419
420 static inline isc_result_t
421 typemap_fromtext(isc_lex_t *lexer, isc_buffer_t *target,
422                  isc_boolean_t allow_empty)
423 {
424         isc_token_t token;
425         unsigned char bm[8*1024]; /* 64k bits */
426         dns_rdatatype_t covered, max_used;
427         int octet;
428         unsigned int max_octet, newend, end;
429         int window;
430         isc_boolean_t first = ISC_TRUE;
431
432         max_used = 0;
433         bm[0] = 0;
434         end = 0;
435
436         do {
437                 RETERR(isc_lex_getmastertoken(lexer, &token,
438                                               isc_tokentype_string, ISC_TRUE));
439                 if (token.type != isc_tokentype_string)
440                         break;
441                 RETTOK(dns_rdatatype_fromtext(&covered,
442                                               &token.value.as_textregion));
443                 if (covered > max_used) {
444                         newend = covered / 8;
445                         if (newend > end) {
446                                 memset(&bm[end + 1], 0, newend - end);
447                                 end = newend;
448                         }
449                         max_used = covered;
450                 }
451                 bm[covered/8] |= (0x80>>(covered%8));
452                 first = ISC_FALSE;
453         } while (1);
454         isc_lex_ungettoken(lexer, &token);
455         if (!allow_empty && first)
456                 return (DNS_R_FORMERR);
457
458         for (window = 0; window < 256 ; window++) {
459                 if (max_used < window * 256)
460                         break;
461
462                 max_octet = max_used - (window * 256);
463                 if (max_octet >= 256)
464                         max_octet = 31;
465                 else
466                         max_octet /= 8;
467
468                 /*
469                  * Find if we have a type in this window.
470                  */
471                 for (octet = max_octet; octet >= 0; octet--) {
472                         if (bm[window * 32 + octet] != 0)
473                                 break;
474                 }
475                 if (octet < 0)
476                         continue;
477                 RETERR(uint8_tobuffer(window, target));
478                 RETERR(uint8_tobuffer(octet + 1, target));
479                 RETERR(mem_tobuffer(target, &bm[window * 32], octet + 1));
480         }
481         return (ISC_R_SUCCESS);
482 }
483
484 static inline isc_result_t
485 typemap_totext(isc_region_t *sr, dns_rdata_textctx_t *tctx,
486                isc_buffer_t *target)
487 {
488         unsigned int i, j, k;
489         unsigned int window, len;
490         isc_boolean_t first = ISC_FALSE;
491
492         for (i = 0; i < sr->length; i += len) {
493                 if (tctx != NULL &&
494                     (tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0) {
495                         RETERR(str_totext(tctx->linebreak, target));
496                         first = ISC_TRUE;
497                 }
498                 INSIST(i + 2 <= sr->length);
499                 window = sr->base[i];
500                 len = sr->base[i + 1];
501                 INSIST(len > 0 && len <= 32);
502                 i += 2;
503                 INSIST(i + len <= sr->length);
504                 for (j = 0; j < len; j++) {
505                         dns_rdatatype_t t;
506                         if (sr->base[i + j] == 0)
507                                 continue;
508                         for (k = 0; k < 8; k++) {
509                                 if ((sr->base[i + j] & (0x80 >> k)) == 0)
510                                         continue;
511                                 t = window * 256 + j * 8 + k;
512                                 if (!first)
513                                         RETERR(str_totext(" ", target));
514                                 first = ISC_FALSE;
515                                 if (dns_rdatatype_isknown(t)) {
516                                         RETERR(dns_rdatatype_totext(t, target));
517                                 } else {
518                                         char buf[sizeof("TYPE65535")];
519                                         sprintf(buf, "TYPE%u", t);
520                                         RETERR(str_totext(buf, target));
521                                 }
522                         }
523                 }
524         }
525         return (ISC_R_SUCCESS);
526 }
527
528 static isc_result_t
529 typemap_test(isc_region_t *sr, isc_boolean_t allow_empty) {
530         unsigned int window, lastwindow = 0;
531         unsigned int len;
532         isc_boolean_t first = ISC_TRUE;
533         unsigned int i;
534
535         for (i = 0; i < sr->length; i += len) {
536                 /*
537                  * Check for overflow.
538                  */
539                 if (i + 2 > sr->length)
540                         RETERR(DNS_R_FORMERR);
541                 window = sr->base[i];
542                 len = sr->base[i + 1];
543                 i += 2;
544                 /*
545                  * Check that bitmap windows are in the correct order.
546                  */
547                 if (!first && window <= lastwindow)
548                         RETERR(DNS_R_FORMERR);
549                 /*
550                  * Check for legal lengths.
551                  */
552                 if (len < 1 || len > 32)
553                         RETERR(DNS_R_FORMERR);
554                 /*
555                  * Check for overflow.
556                  */
557                 if (i + len > sr->length)
558                         RETERR(DNS_R_FORMERR);
559                 /*
560                  * The last octet of the bitmap must be non zero.
561                  */
562                 if (sr->base[i + len - 1] == 0)
563                         RETERR(DNS_R_FORMERR);
564                 lastwindow = window;
565                 first = ISC_FALSE;
566         }
567         if (i != sr->length)
568                 return (DNS_R_EXTRADATA);
569         if (!allow_empty && first)
570                 RETERR(DNS_R_FORMERR);
571         return (ISC_R_SUCCESS);
572 }
573
574 static const char hexdigits[] = "0123456789abcdef";
575 static const char decdigits[] = "0123456789";
576
577 #include "code.h"
578
579 #define META 0x0001
580 #define RESERVED 0x0002
581
582 /***
583  *** Initialization
584  ***/
585
586 void
587 dns_rdata_init(dns_rdata_t *rdata) {
588
589         REQUIRE(rdata != NULL);
590
591         rdata->data = NULL;
592         rdata->length = 0;
593         rdata->rdclass = 0;
594         rdata->type = 0;
595         rdata->flags = 0;
596         ISC_LINK_INIT(rdata, link);
597         /* ISC_LIST_INIT(rdata->list); */
598 }
599
600 void
601 dns_rdata_reset(dns_rdata_t *rdata) {
602
603         REQUIRE(rdata != NULL);
604
605         REQUIRE(!ISC_LINK_LINKED(rdata, link));
606         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
607
608         rdata->data = NULL;
609         rdata->length = 0;
610         rdata->rdclass = 0;
611         rdata->type = 0;
612         rdata->flags = 0;
613 }
614
615 /***
616  ***
617  ***/
618
619 void
620 dns_rdata_clone(const dns_rdata_t *src, dns_rdata_t *target) {
621
622         REQUIRE(src != NULL);
623         REQUIRE(target != NULL);
624
625         REQUIRE(DNS_RDATA_INITIALIZED(target));
626
627         REQUIRE(DNS_RDATA_VALIDFLAGS(src));
628         REQUIRE(DNS_RDATA_VALIDFLAGS(target));
629
630         target->data = src->data;
631         target->length = src->length;
632         target->rdclass = src->rdclass;
633         target->type = src->type;
634         target->flags = src->flags;
635 }
636
637
638 /***
639  *** Comparisons
640  ***/
641
642 int
643 dns_rdata_compare(const dns_rdata_t *rdata1, const dns_rdata_t *rdata2) {
644         int result = 0;
645         isc_boolean_t use_default = ISC_FALSE;
646
647         REQUIRE(rdata1 != NULL);
648         REQUIRE(rdata2 != NULL);
649         REQUIRE(rdata1->length == 0 || rdata1->data != NULL);
650         REQUIRE(rdata2->length == 0 || rdata2->data != NULL);
651         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata1));
652         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata2));
653
654         if (rdata1->rdclass != rdata2->rdclass)
655                 return (rdata1->rdclass < rdata2->rdclass ? -1 : 1);
656
657         if (rdata1->type != rdata2->type)
658                 return (rdata1->type < rdata2->type ? -1 : 1);
659
660         COMPARESWITCH
661
662         if (use_default) {
663                 isc_region_t r1;
664                 isc_region_t r2;
665
666                 dns_rdata_toregion(rdata1, &r1);
667                 dns_rdata_toregion(rdata2, &r2);
668                 result = isc_region_compare(&r1, &r2);
669         }
670         return (result);
671 }
672
673 int
674 dns_rdata_casecompare(const dns_rdata_t *rdata1, const dns_rdata_t *rdata2) {
675         int result = 0;
676         isc_boolean_t use_default = ISC_FALSE;
677
678         REQUIRE(rdata1 != NULL);
679         REQUIRE(rdata2 != NULL);
680         REQUIRE(rdata1->length == 0 || rdata1->data != NULL);
681         REQUIRE(rdata2->length == 0 || rdata2->data != NULL);
682         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata1));
683         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata2));
684
685         if (rdata1->rdclass != rdata2->rdclass)
686                 return (rdata1->rdclass < rdata2->rdclass ? -1 : 1);
687
688         if (rdata1->type != rdata2->type)
689                 return (rdata1->type < rdata2->type ? -1 : 1);
690
691         CASECOMPARESWITCH
692
693         if (use_default) {
694                 isc_region_t r1;
695                 isc_region_t r2;
696
697                 dns_rdata_toregion(rdata1, &r1);
698                 dns_rdata_toregion(rdata2, &r2);
699                 result = isc_region_compare(&r1, &r2);
700         }
701         return (result);
702 }
703
704 /***
705  *** Conversions
706  ***/
707
708 void
709 dns_rdata_fromregion(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
710                      dns_rdatatype_t type, isc_region_t *r)
711 {
712
713         REQUIRE(rdata != NULL);
714         REQUIRE(DNS_RDATA_INITIALIZED(rdata));
715         REQUIRE(r != NULL);
716
717         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
718
719         rdata->data = r->base;
720         rdata->length = r->length;
721         rdata->rdclass = rdclass;
722         rdata->type = type;
723         rdata->flags = 0;
724 }
725
726 void
727 dns_rdata_toregion(const dns_rdata_t *rdata, isc_region_t *r) {
728
729         REQUIRE(rdata != NULL);
730         REQUIRE(r != NULL);
731         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
732
733         r->base = rdata->data;
734         r->length = rdata->length;
735 }
736
737 isc_result_t
738 dns_rdata_fromwire(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
739                    dns_rdatatype_t type, isc_buffer_t *source,
740                    dns_decompress_t *dctx, unsigned int options,
741                    isc_buffer_t *target)
742 {
743         isc_result_t result = ISC_R_NOTIMPLEMENTED;
744         isc_region_t region;
745         isc_buffer_t ss;
746         isc_buffer_t st;
747         isc_boolean_t use_default = ISC_FALSE;
748         isc_uint32_t activelength;
749         unsigned int length;
750
751         REQUIRE(dctx != NULL);
752         if (rdata != NULL) {
753                 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
754                 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
755         }
756         REQUIRE(source != NULL);
757         REQUIRE(target != NULL);
758
759         if (type == 0)
760                 return (DNS_R_FORMERR);
761
762         ss = *source;
763         st = *target;
764
765         activelength = isc_buffer_activelength(source);
766         INSIST(activelength < 65536);
767
768         FROMWIRESWITCH
769
770         if (use_default) {
771                 if (activelength > isc_buffer_availablelength(target))
772                         result = ISC_R_NOSPACE;
773                 else {
774                         isc_buffer_putmem(target, isc_buffer_current(source),
775                                           activelength);
776                         isc_buffer_forward(source, activelength);
777                         result = ISC_R_SUCCESS;
778                 }
779         }
780
781         /*
782          * Reject any rdata that expands out to more than DNS_RDATA_MAXLENGTH
783          * as we cannot transmit it.
784          */
785         length = isc_buffer_usedlength(target) - isc_buffer_usedlength(&st);
786         if (result == ISC_R_SUCCESS && length > DNS_RDATA_MAXLENGTH)
787                 result = DNS_R_FORMERR;
788
789         /*
790          * We should have consumed all of our buffer.
791          */
792         if (result == ISC_R_SUCCESS && !buffer_empty(source))
793                 result = DNS_R_EXTRADATA;
794
795         if (rdata != NULL && result == ISC_R_SUCCESS) {
796                 region.base = isc_buffer_used(&st);
797                 region.length = length;
798                 dns_rdata_fromregion(rdata, rdclass, type, &region);
799         }
800
801         if (result != ISC_R_SUCCESS) {
802                 *source = ss;
803                 *target = st;
804         }
805         return (result);
806 }
807
808 isc_result_t
809 dns_rdata_towire(dns_rdata_t *rdata, dns_compress_t *cctx,
810                  isc_buffer_t *target)
811 {
812         isc_result_t result = ISC_R_NOTIMPLEMENTED;
813         isc_boolean_t use_default = ISC_FALSE;
814         isc_region_t tr;
815         isc_buffer_t st;
816
817         REQUIRE(rdata != NULL);
818         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
819
820         /*
821          * Some DynDNS meta-RRs have empty rdata.
822          */
823         if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
824                 INSIST(rdata->length == 0);
825                 return (ISC_R_SUCCESS);
826         }
827
828         st = *target;
829
830         TOWIRESWITCH
831
832         if (use_default) {
833                 isc_buffer_availableregion(target, &tr);
834                 if (tr.length < rdata->length)
835                         return (ISC_R_NOSPACE);
836                 memmove(tr.base, rdata->data, rdata->length);
837                 isc_buffer_add(target, rdata->length);
838                 return (ISC_R_SUCCESS);
839         }
840         if (result != ISC_R_SUCCESS) {
841                 *target = st;
842                 INSIST(target->used < 65536);
843                 dns_compress_rollback(cctx, (isc_uint16_t)target->used);
844         }
845         return (result);
846 }
847
848 /*
849  * If the binary data in 'src' is valid uncompressed wire format
850  * rdata of class 'rdclass' and type 'type', return ISC_R_SUCCESS
851  * and copy the validated rdata to 'dest'.  Otherwise return an error.
852  */
853 static isc_result_t
854 rdata_validate(isc_buffer_t *src, isc_buffer_t *dest, dns_rdataclass_t rdclass,
855             dns_rdatatype_t type)
856 {
857         dns_decompress_t dctx;
858         isc_result_t result;
859
860         dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_NONE);
861         isc_buffer_setactive(src, isc_buffer_usedlength(src));
862         result = dns_rdata_fromwire(NULL, rdclass, type, src, &dctx, 0, dest);
863         dns_decompress_invalidate(&dctx);
864
865         return (result);
866 }
867
868 static isc_result_t
869 unknown_fromtext(dns_rdataclass_t rdclass, dns_rdatatype_t type,
870                  isc_lex_t *lexer, isc_mem_t *mctx, isc_buffer_t *target)
871 {
872         isc_result_t result;
873         isc_buffer_t *buf = NULL;
874         isc_token_t token;
875
876         if (type == 0 || dns_rdatatype_ismeta(type))
877                 return (DNS_R_METATYPE);
878
879         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
880                                         ISC_FALSE));
881         if (token.value.as_ulong > 65535U)
882                 return (ISC_R_RANGE);
883         result = isc_buffer_allocate(mctx, &buf, token.value.as_ulong);
884         if (result != ISC_R_SUCCESS)
885                 return (result);
886
887         result = isc_hex_tobuffer(lexer, buf,
888                                   (unsigned int)token.value.as_ulong);
889         if (result != ISC_R_SUCCESS)
890                goto failure;
891         if (isc_buffer_usedlength(buf) != token.value.as_ulong) {
892                 result = ISC_R_UNEXPECTEDEND;
893                 goto failure;
894         }
895
896         if (dns_rdatatype_isknown(type)) {
897                 result = rdata_validate(buf, target, rdclass, type);
898         } else {
899                 isc_region_t r;
900                 isc_buffer_usedregion(buf, &r);
901                 result = isc_buffer_copyregion(target, &r);
902         }
903         if (result != ISC_R_SUCCESS)
904                 goto failure;
905
906         isc_buffer_free(&buf);
907         return (ISC_R_SUCCESS);
908
909  failure:
910         isc_buffer_free(&buf);
911         return (result);
912 }
913
914 isc_result_t
915 dns_rdata_fromtext(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
916                    dns_rdatatype_t type, isc_lex_t *lexer,
917                    dns_name_t *origin, unsigned int options, isc_mem_t *mctx,
918                    isc_buffer_t *target, dns_rdatacallbacks_t *callbacks)
919 {
920         isc_result_t result = ISC_R_NOTIMPLEMENTED;
921         isc_region_t region;
922         isc_buffer_t st;
923         isc_token_t token;
924         unsigned int lexoptions = ISC_LEXOPT_EOL | ISC_LEXOPT_EOF |
925                                   ISC_LEXOPT_DNSMULTILINE | ISC_LEXOPT_ESCAPE;
926         char *name;
927         unsigned long line;
928         void (*callback)(dns_rdatacallbacks_t *, const char *, ...);
929         isc_result_t tresult;
930         unsigned int length;
931         isc_boolean_t unknown;
932
933         REQUIRE(origin == NULL || dns_name_isabsolute(origin) == ISC_TRUE);
934         if (rdata != NULL) {
935                 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
936                 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
937         }
938         if (callbacks != NULL) {
939                 REQUIRE(callbacks->warn != NULL);
940                 REQUIRE(callbacks->error != NULL);
941         }
942
943         st = *target;
944
945         if (callbacks != NULL)
946                 callback = callbacks->error;
947         else
948                 callback = default_fromtext_callback;
949
950         result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
951                                         ISC_FALSE);
952         if (result != ISC_R_SUCCESS) {
953                 name = isc_lex_getsourcename(lexer);
954                 line = isc_lex_getsourceline(lexer);
955                 fromtext_error(callback, callbacks, name, line, NULL, result);
956                 return (result);
957         }
958
959         unknown = ISC_FALSE;
960         if (token.type == isc_tokentype_string &&
961             strcmp(DNS_AS_STR(token), "\\#") == 0) {
962                 /*
963                  * If this is a TXT record '\#' could be a escaped '#'.
964                  * Look to see if the next token is a number and if so
965                  * treat it as a unknown record format.
966                  */
967                 if (type == dns_rdatatype_txt) {
968                         result = isc_lex_getmastertoken(lexer, &token,
969                                                         isc_tokentype_number,
970                                                         ISC_FALSE);
971                         if (result == ISC_R_SUCCESS)
972                                 isc_lex_ungettoken(lexer, &token);
973                 }
974
975                 if (result == ISC_R_SUCCESS) {
976                         unknown = ISC_TRUE;
977                         result = unknown_fromtext(rdclass, type, lexer,
978                                                   mctx, target);
979                 } else
980                         options |= DNS_RDATA_UNKNOWNESCAPE;
981         } else
982                 isc_lex_ungettoken(lexer, &token);
983
984         if (!unknown)
985                 FROMTEXTSWITCH
986
987         /*
988          * Consume to end of line / file.
989          * If not at end of line initially set error code.
990          * Call callback via fromtext_error once if there was an error.
991          */
992         do {
993                 name = isc_lex_getsourcename(lexer);
994                 line = isc_lex_getsourceline(lexer);
995                 tresult = isc_lex_gettoken(lexer, lexoptions, &token);
996                 if (tresult != ISC_R_SUCCESS) {
997                         if (result == ISC_R_SUCCESS)
998                                 result = tresult;
999                         if (callback != NULL)
1000                                 fromtext_error(callback, callbacks, name,
1001                                                line, NULL, result);
1002                         break;
1003                 } else if (token.type != isc_tokentype_eol &&
1004                            token.type != isc_tokentype_eof) {
1005                         if (result == ISC_R_SUCCESS)
1006                                 result = DNS_R_EXTRATOKEN;
1007                         if (callback != NULL) {
1008                                 fromtext_error(callback, callbacks, name,
1009                                                line, &token, result);
1010                                 callback = NULL;
1011                         }
1012                 } else if (result != ISC_R_SUCCESS && callback != NULL) {
1013                         fromtext_error(callback, callbacks, name, line,
1014                                        &token, result);
1015                         break;
1016                 } else {
1017                         if (token.type == isc_tokentype_eof)
1018                                 fromtext_warneof(lexer, callbacks);
1019                         break;
1020                 }
1021         } while (1);
1022
1023         length = isc_buffer_usedlength(target) - isc_buffer_usedlength(&st);
1024         if (result == ISC_R_SUCCESS && length > DNS_RDATA_MAXLENGTH)
1025                 result = ISC_R_NOSPACE;
1026
1027         if (rdata != NULL && result == ISC_R_SUCCESS) {
1028                 region.base = isc_buffer_used(&st);
1029                 region.length = length;
1030                 dns_rdata_fromregion(rdata, rdclass, type, &region);
1031         }
1032         if (result != ISC_R_SUCCESS) {
1033                 *target = st;
1034         }
1035         return (result);
1036 }
1037
1038 static isc_result_t
1039 unknown_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
1040                isc_buffer_t *target)
1041 {
1042         isc_result_t result;
1043         char buf[sizeof("65535")];
1044         isc_region_t sr;
1045
1046         strlcpy(buf, "\\# ", sizeof(buf));
1047         result = str_totext(buf, target);
1048         if (result != ISC_R_SUCCESS)
1049                 return (result);
1050
1051         dns_rdata_toregion(rdata, &sr);
1052         INSIST(sr.length < 65536);
1053         snprintf(buf, sizeof(buf), "%u", sr.length);
1054         result = str_totext(buf, target);
1055         if (result != ISC_R_SUCCESS)
1056                 return (result);
1057
1058         if (sr.length != 0U) {
1059                 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
1060                         result = str_totext(" ( ", target);
1061                 else
1062                         result = str_totext(" ", target);
1063
1064                 if (result != ISC_R_SUCCESS)
1065                         return (result);
1066
1067                 if (tctx->width == 0) /* No splitting */
1068                         result = isc_hex_totext(&sr, 0, "", target);
1069                 else
1070                         result = isc_hex_totext(&sr, tctx->width - 2,
1071                                                 tctx->linebreak,
1072                                                 target);
1073                 if (result == ISC_R_SUCCESS &&
1074                     (tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
1075                         result = str_totext(" )", target);
1076         }
1077         return (result);
1078 }
1079
1080 static isc_result_t
1081 rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
1082              isc_buffer_t *target)
1083 {
1084         isc_result_t result = ISC_R_NOTIMPLEMENTED;
1085         isc_boolean_t use_default = ISC_FALSE;
1086         unsigned int cur;
1087
1088         REQUIRE(rdata != NULL);
1089         REQUIRE(tctx->origin == NULL ||
1090                 dns_name_isabsolute(tctx->origin) == ISC_TRUE);
1091
1092         /*
1093          * Some DynDNS meta-RRs have empty rdata.
1094          */
1095         if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
1096                 INSIST(rdata->length == 0);
1097                 return (ISC_R_SUCCESS);
1098         }
1099
1100         cur = isc_buffer_usedlength(target);
1101
1102         TOTEXTSWITCH
1103
1104         if (use_default || (result == ISC_R_NOTIMPLEMENTED)) {
1105                 unsigned int u = isc_buffer_usedlength(target);
1106
1107                 INSIST(u >= cur);
1108                 isc_buffer_subtract(target, u - cur);
1109                 result = unknown_totext(rdata, tctx, target);
1110         }
1111
1112         return (result);
1113 }
1114
1115 isc_result_t
1116 dns_rdata_totext(dns_rdata_t *rdata, dns_name_t *origin, isc_buffer_t *target)
1117 {
1118         dns_rdata_textctx_t tctx;
1119
1120         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
1121
1122         /*
1123          * Set up formatting options for single-line output.
1124          */
1125         tctx.origin = origin;
1126         tctx.flags = 0;
1127         tctx.width = 60;
1128         tctx.linebreak = " ";
1129         return (rdata_totext(rdata, &tctx, target));
1130 }
1131
1132 isc_result_t
1133 dns_rdata_tofmttext(dns_rdata_t *rdata, dns_name_t *origin,
1134                     unsigned int flags, unsigned int width,
1135                     unsigned int split_width, const char *linebreak,
1136                     isc_buffer_t *target)
1137 {
1138         dns_rdata_textctx_t tctx;
1139
1140         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
1141
1142         /*
1143          * Set up formatting options for formatted output.
1144          */
1145         tctx.origin = origin;
1146         tctx.flags = flags;
1147         if (split_width == 0xffffffff)
1148                 tctx.width = width;
1149         else
1150                 tctx.width = split_width;
1151
1152         if ((flags & DNS_STYLEFLAG_MULTILINE) != 0)
1153                 tctx.linebreak = linebreak;
1154         else {
1155                 if (split_width == 0xffffffff)
1156                         tctx.width = 60; /* Used for hex word length only. */
1157                 tctx.linebreak = " ";
1158         }
1159         return (rdata_totext(rdata, &tctx, target));
1160 }
1161
1162 isc_result_t
1163 dns_rdata_fromstruct(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
1164                      dns_rdatatype_t type, void *source,
1165                      isc_buffer_t *target)
1166 {
1167         isc_result_t result = ISC_R_NOTIMPLEMENTED;
1168         isc_buffer_t st;
1169         isc_region_t region;
1170         isc_boolean_t use_default = ISC_FALSE;
1171         unsigned int length;
1172
1173         REQUIRE(source != NULL);
1174         if (rdata != NULL) {
1175                 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
1176                 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
1177         }
1178
1179         st = *target;
1180
1181         FROMSTRUCTSWITCH
1182
1183         if (use_default)
1184                 (void)NULL;
1185
1186         length = isc_buffer_usedlength(target) - isc_buffer_usedlength(&st);
1187         if (result == ISC_R_SUCCESS && length > DNS_RDATA_MAXLENGTH)
1188                 result = ISC_R_NOSPACE;
1189
1190         if (rdata != NULL && result == ISC_R_SUCCESS) {
1191                 region.base = isc_buffer_used(&st);
1192                 region.length = length;
1193                 dns_rdata_fromregion(rdata, rdclass, type, &region);
1194         }
1195         if (result != ISC_R_SUCCESS)
1196                 *target = st;
1197         return (result);
1198 }
1199
1200 isc_result_t
1201 dns_rdata_tostruct(dns_rdata_t *rdata, void *target, isc_mem_t *mctx) {
1202         isc_result_t result = ISC_R_NOTIMPLEMENTED;
1203         isc_boolean_t use_default = ISC_FALSE;
1204
1205         REQUIRE(rdata != NULL);
1206         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
1207
1208         TOSTRUCTSWITCH
1209
1210         if (use_default)
1211                 (void)NULL;
1212
1213         return (result);
1214 }
1215
1216 void
1217 dns_rdata_freestruct(void *source) {
1218         dns_rdatacommon_t *common = source;
1219         REQUIRE(source != NULL);
1220
1221         FREESTRUCTSWITCH
1222 }
1223
1224 isc_result_t
1225 dns_rdata_additionaldata(dns_rdata_t *rdata, dns_additionaldatafunc_t add,
1226                          void *arg)
1227 {
1228         isc_result_t result = ISC_R_NOTIMPLEMENTED;
1229         isc_boolean_t use_default = ISC_FALSE;
1230
1231         /*
1232          * Call 'add' for each name and type from 'rdata' which is subject to
1233          * additional section processing.
1234          */
1235
1236         REQUIRE(rdata != NULL);
1237         REQUIRE(add != NULL);
1238         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
1239
1240         ADDITIONALDATASWITCH
1241
1242         /* No additional processing for unknown types */
1243         if (use_default)
1244                 result = ISC_R_SUCCESS;
1245
1246         return (result);
1247 }
1248
1249 isc_result_t
1250 dns_rdata_digest(dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg) {
1251         isc_result_t result = ISC_R_NOTIMPLEMENTED;
1252         isc_boolean_t use_default = ISC_FALSE;
1253         isc_region_t r;
1254
1255         /*
1256          * Send 'rdata' in DNSSEC canonical form to 'digest'.
1257          */
1258
1259         REQUIRE(rdata != NULL);
1260         REQUIRE(digest != NULL);
1261         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
1262
1263         DIGESTSWITCH
1264
1265         if (use_default) {
1266                 dns_rdata_toregion(rdata, &r);
1267                 result = (digest)(arg, &r);
1268         }
1269
1270         return (result);
1271 }
1272
1273 isc_boolean_t
1274 dns_rdata_checkowner(dns_name_t *name, dns_rdataclass_t rdclass,
1275                      dns_rdatatype_t type, isc_boolean_t wildcard)
1276 {
1277         isc_boolean_t result;
1278
1279         CHECKOWNERSWITCH
1280         return (result);
1281 }
1282
1283 isc_boolean_t
1284 dns_rdata_checknames(dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad)
1285 {
1286         isc_boolean_t result;
1287
1288         CHECKNAMESSWITCH
1289         return (result);
1290 }
1291
1292 unsigned int
1293 dns_rdatatype_attributes(dns_rdatatype_t type)
1294 {
1295         RDATATYPE_ATTRIBUTE_SW
1296         if (type >= (dns_rdatatype_t)128 && type < (dns_rdatatype_t)255)
1297                 return (DNS_RDATATYPEATTR_UNKNOWN | DNS_RDATATYPEATTR_META);
1298         return (DNS_RDATATYPEATTR_UNKNOWN);
1299 }
1300
1301 isc_result_t
1302 dns_rdatatype_fromtext(dns_rdatatype_t *typep, isc_textregion_t *source) {
1303         unsigned int hash;
1304         unsigned int n;
1305         unsigned char a, b;
1306
1307         n = source->length;
1308
1309         if (n == 0)
1310                 return (DNS_R_UNKNOWN);
1311
1312         a = tolower((unsigned char)source->base[0]);
1313         b = tolower((unsigned char)source->base[n - 1]);
1314
1315         hash = ((a + n) * b) % 256;
1316
1317         /*
1318          * This switch block is inlined via \#define, and will use "return"
1319          * to return a result to the caller if it is a valid (known)
1320          * rdatatype name.
1321          */
1322         RDATATYPE_FROMTEXT_SW(hash, source->base, n, typep);
1323
1324         if (source->length > 4 && source->length < (4 + sizeof("65000")) &&
1325             strncasecmp("type", source->base, 4) == 0) {
1326                 char buf[sizeof("65000")];
1327                 char *endp;
1328                 unsigned int val;
1329
1330                 strncpy(buf, source->base + 4, source->length - 4);
1331                 buf[source->length - 4] = '\0';
1332                 val = strtoul(buf, &endp, 10);
1333                 if (*endp == '\0' && val <= 0xffff) {
1334                         *typep = (dns_rdatatype_t)val;
1335                         return (ISC_R_SUCCESS);
1336                 }
1337         }
1338
1339         return (DNS_R_UNKNOWN);
1340 }
1341
1342 isc_result_t
1343 dns_rdatatype_totext(dns_rdatatype_t type, isc_buffer_t *target) {
1344         char buf[sizeof("TYPE65535")];
1345
1346         RDATATYPE_TOTEXT_SW
1347         snprintf(buf, sizeof(buf), "TYPE%u", type);
1348         return (str_totext(buf, target));
1349 }
1350
1351 void
1352 dns_rdatatype_format(dns_rdatatype_t rdtype,
1353                      char *array, unsigned int size)
1354 {
1355         isc_result_t result;
1356         isc_buffer_t buf;
1357
1358         if (size == 0U)
1359                 return;
1360
1361         isc_buffer_init(&buf, array, size);
1362         result = dns_rdatatype_totext(rdtype, &buf);
1363         /*
1364          * Null terminate.
1365          */
1366         if (result == ISC_R_SUCCESS) {
1367                 if (isc_buffer_availablelength(&buf) >= 1)
1368                         isc_buffer_putuint8(&buf, 0);
1369                 else
1370                         result = ISC_R_NOSPACE;
1371         }
1372         if (result != ISC_R_SUCCESS)
1373                 strlcpy(array, "<unknown>", size);
1374 }
1375
1376 /*
1377  * Private function.
1378  */
1379
1380 static unsigned int
1381 name_length(dns_name_t *name) {
1382         return (name->length);
1383 }
1384
1385 static isc_result_t
1386 txt_totext(isc_region_t *source, isc_boolean_t quote, isc_buffer_t *target) {
1387         unsigned int tl;
1388         unsigned int n;
1389         unsigned char *sp;
1390         char *tp;
1391         isc_region_t region;
1392
1393         isc_buffer_availableregion(target, &region);
1394         sp = source->base;
1395         tp = (char *)region.base;
1396         tl = region.length;
1397
1398         n = *sp++;
1399
1400         REQUIRE(n + 1 <= source->length);
1401         if (n == 0U)
1402                 REQUIRE(quote == ISC_TRUE);
1403
1404         if (quote) {
1405                 if (tl < 1)
1406                         return (ISC_R_NOSPACE);
1407                 *tp++ = '"';
1408                 tl--;
1409         }
1410         while (n--) {
1411                 /*
1412                  * \DDD space (0x20) if not quoting.
1413                  */
1414                 if (*sp < (quote ? 0x20 : 0x21) || *sp >= 0x7f) {
1415                         if (tl < 4)
1416                                 return (ISC_R_NOSPACE);
1417                         *tp++ = 0x5c;
1418                         *tp++ = 0x30 + ((*sp / 100) % 10);
1419                         *tp++ = 0x30 + ((*sp / 10) % 10);
1420                         *tp++ = 0x30 + (*sp % 10);
1421                         sp++;
1422                         tl -= 4;
1423                         continue;
1424                 }
1425                 /*
1426                  * Escape double quote and backslash.  If we are not
1427                  * enclosing the string in double quotes also escape
1428                  * at sign and semicolon.
1429                  */
1430                 if (*sp == 0x22 || *sp == 0x5c ||
1431                     (!quote && (*sp == 0x40 || *sp == 0x3b))) {
1432                         if (tl < 2)
1433                                 return (ISC_R_NOSPACE);
1434                         *tp++ = '\\';
1435                         tl--;
1436                 }
1437                 if (tl < 1)
1438                         return (ISC_R_NOSPACE);
1439                 *tp++ = *sp++;
1440                 tl--;
1441         }
1442         if (quote) {
1443                 if (tl < 1)
1444                         return (ISC_R_NOSPACE);
1445                 *tp++ = '"';
1446                 tl--;
1447         }
1448         isc_buffer_add(target, (unsigned int)(tp - (char *)region.base));
1449         isc_region_consume(source, *source->base + 1);
1450         return (ISC_R_SUCCESS);
1451 }
1452
1453 static isc_result_t
1454 txt_fromtext(isc_textregion_t *source, isc_buffer_t *target) {
1455         isc_region_t tregion;
1456         isc_boolean_t escape;
1457         unsigned int n, nrem;
1458         char *s;
1459         unsigned char *t;
1460         int d;
1461         int c;
1462
1463         isc_buffer_availableregion(target, &tregion);
1464         s = source->base;
1465         n = source->length;
1466         t = tregion.base;
1467         nrem = tregion.length;
1468         escape = ISC_FALSE;
1469         if (nrem < 1)
1470                 return (ISC_R_NOSPACE);
1471         /*
1472          * Length byte.
1473          */
1474         nrem--;
1475         t++;
1476         /*
1477          * Maximum text string length.
1478          */
1479         if (nrem > 255)
1480                 nrem = 255;
1481         while (n-- != 0) {
1482                 c = (*s++) & 0xff;
1483                 if (escape && (d = decvalue((char)c)) != -1) {
1484                         c = d;
1485                         if (n == 0)
1486                                 return (DNS_R_SYNTAX);
1487                         n--;
1488                         if ((d = decvalue(*s++)) != -1)
1489                                 c = c * 10 + d;
1490                         else
1491                                 return (DNS_R_SYNTAX);
1492                         if (n == 0)
1493                                 return (DNS_R_SYNTAX);
1494                         n--;
1495                         if ((d = decvalue(*s++)) != -1)
1496                                 c = c * 10 + d;
1497                         else
1498                                 return (DNS_R_SYNTAX);
1499                         if (c > 255)
1500                                 return (DNS_R_SYNTAX);
1501                 } else if (!escape && c == '\\') {
1502                         escape = ISC_TRUE;
1503                         continue;
1504                 }
1505                 escape = ISC_FALSE;
1506                 if (nrem == 0)
1507                         return ((tregion.length <= 256U) ?
1508                                 ISC_R_NOSPACE : DNS_R_SYNTAX);
1509                 *t++ = c;
1510                 nrem--;
1511         }
1512         if (escape)
1513                 return (DNS_R_SYNTAX);
1514         *tregion.base = (unsigned char)(t - tregion.base - 1);
1515         isc_buffer_add(target, *tregion.base + 1);
1516         return (ISC_R_SUCCESS);
1517 }
1518
1519 static isc_result_t
1520 txt_fromwire(isc_buffer_t *source, isc_buffer_t *target) {
1521         unsigned int n;
1522         isc_region_t sregion;
1523         isc_region_t tregion;
1524
1525         isc_buffer_activeregion(source, &sregion);
1526         if (sregion.length == 0)
1527                 return (ISC_R_UNEXPECTEDEND);
1528         n = *sregion.base + 1;
1529         if (n > sregion.length)
1530                 return (ISC_R_UNEXPECTEDEND);
1531
1532         isc_buffer_availableregion(target, &tregion);
1533         if (n > tregion.length)
1534                 return (ISC_R_NOSPACE);
1535
1536         if (tregion.base != sregion.base)
1537                 memmove(tregion.base, sregion.base, n);
1538         isc_buffer_forward(source, n);
1539         isc_buffer_add(target, n);
1540         return (ISC_R_SUCCESS);
1541 }
1542
1543 /*
1544  * Conversion of TXT-like rdata fields without length limits.
1545  */
1546 static isc_result_t
1547 multitxt_totext(isc_region_t *source, isc_buffer_t *target) {
1548         unsigned int tl;
1549         unsigned int n0, n;
1550         unsigned char *sp;
1551         char *tp;
1552         isc_region_t region;
1553
1554         isc_buffer_availableregion(target, &region);
1555         sp = source->base;
1556         tp = (char *)region.base;
1557         tl = region.length;
1558
1559         if (tl < 1)
1560                 return (ISC_R_NOSPACE);
1561         *tp++ = '"';
1562         tl--;
1563         do {
1564                 n = source->length;
1565                 n0 = source->length - 1;
1566
1567                 while (n--) {
1568                         if (*sp < 0x20 || *sp >= 0x7f) {
1569                                 if (tl < 4)
1570                                         return (ISC_R_NOSPACE);
1571                                 *tp++ = 0x5c;
1572                                 *tp++ = 0x30 + ((*sp / 100) % 10);
1573                                 *tp++ = 0x30 + ((*sp / 10) % 10);
1574                                 *tp++ = 0x30 + (*sp % 10);
1575                                 sp++;
1576                                 tl -= 4;
1577                                 continue;
1578                         }
1579                         /* double quote, semi-colon, backslash */
1580                         if (*sp == 0x22 || *sp == 0x3b || *sp == 0x5c) {
1581                                 if (tl < 2)
1582                                         return (ISC_R_NOSPACE);
1583                                 *tp++ = '\\';
1584                                 tl--;
1585                         }
1586                         if (tl < 1)
1587                                 return (ISC_R_NOSPACE);
1588                         *tp++ = *sp++;
1589                         tl--;
1590                 }
1591                 isc_region_consume(source, n0 + 1);
1592         } while (source->length != 0);
1593         if (tl < 1)
1594                 return (ISC_R_NOSPACE);
1595         *tp++ = '"';
1596         tl--;
1597         isc_buffer_add(target, (unsigned int)(tp - (char *)region.base));
1598         return (ISC_R_SUCCESS);
1599 }
1600
1601 static isc_result_t
1602 multitxt_fromtext(isc_textregion_t *source, isc_buffer_t *target) {
1603         isc_region_t tregion;
1604         isc_boolean_t escape;
1605         unsigned int n, nrem;
1606         char *s;
1607         unsigned char *t0, *t;
1608         int d;
1609         int c;
1610
1611         s = source->base;
1612         n = source->length;
1613         escape = ISC_FALSE;
1614
1615         do {
1616                 isc_buffer_availableregion(target, &tregion);
1617                 t0 = t = tregion.base;
1618                 nrem = tregion.length;
1619                 if (nrem < 1)
1620                         return (ISC_R_NOSPACE);
1621
1622                 while (n != 0) {
1623                         --n;
1624                         c = (*s++) & 0xff;
1625                         if (escape && (d = decvalue((char)c)) != -1) {
1626                                 c = d;
1627                                 if (n == 0)
1628                                         return (DNS_R_SYNTAX);
1629                                 n--;
1630                                 if ((d = decvalue(*s++)) != -1)
1631                                         c = c * 10 + d;
1632                                 else
1633                                         return (DNS_R_SYNTAX);
1634                                 if (n == 0)
1635                                         return (DNS_R_SYNTAX);
1636                                 n--;
1637                                 if ((d = decvalue(*s++)) != -1)
1638                                         c = c * 10 + d;
1639                                 else
1640                                         return (DNS_R_SYNTAX);
1641                                 if (c > 255)
1642                                         return (DNS_R_SYNTAX);
1643                         } else if (!escape && c == '\\') {
1644                                 escape = ISC_TRUE;
1645                                 continue;
1646                         }
1647                         escape = ISC_FALSE;
1648                         *t++ = c;
1649                         nrem--;
1650                         if (nrem == 0)
1651                                 break;
1652                 }
1653                 if (escape)
1654                         return (DNS_R_SYNTAX);
1655
1656                 isc_buffer_add(target, (unsigned int)(t - t0));
1657         } while (n != 0);
1658         return (ISC_R_SUCCESS);
1659 }
1660
1661 static isc_boolean_t
1662 name_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target) {
1663         int l1, l2;
1664
1665         if (origin == NULL)
1666                 goto return_false;
1667
1668         if (dns_name_compare(origin, dns_rootname) == 0)
1669                 goto return_false;
1670
1671         if (!dns_name_issubdomain(name, origin))
1672                 goto return_false;
1673
1674         l1 = dns_name_countlabels(name);
1675         l2 = dns_name_countlabels(origin);
1676
1677         if (l1 == l2)
1678                 goto return_false;
1679
1680         /* Master files should be case preserving. */
1681         dns_name_getlabelsequence(name, l1 - l2, l2, target);
1682         if (!dns_name_caseequal(origin, target))
1683                 goto return_false;
1684
1685         dns_name_getlabelsequence(name, 0, l1 - l2, target);
1686         return (ISC_TRUE);
1687
1688 return_false:
1689         *target = *name;
1690         return (ISC_FALSE);
1691 }
1692
1693 static isc_result_t
1694 str_totext(const char *source, isc_buffer_t *target) {
1695         unsigned int l;
1696         isc_region_t region;
1697
1698         isc_buffer_availableregion(target, &region);
1699         l = strlen(source);
1700
1701         if (l > region.length)
1702                 return (ISC_R_NOSPACE);
1703
1704         memmove(region.base, source, l);
1705         isc_buffer_add(target, l);
1706         return (ISC_R_SUCCESS);
1707 }
1708
1709 static isc_result_t
1710 inet_totext(int af, isc_region_t *src, isc_buffer_t *target) {
1711         char tmpbuf[64];
1712
1713         /* Note - inet_ntop doesn't do size checking on its input. */
1714         if (inet_ntop(af, src->base, tmpbuf, sizeof(tmpbuf)) == NULL)
1715                 return (ISC_R_NOSPACE);
1716         if (strlen(tmpbuf) > isc_buffer_availablelength(target))
1717                 return (ISC_R_NOSPACE);
1718         isc_buffer_putstr(target, tmpbuf);
1719         return (ISC_R_SUCCESS);
1720 }
1721
1722 static isc_boolean_t
1723 buffer_empty(isc_buffer_t *source) {
1724         return((source->current == source->active) ? ISC_TRUE : ISC_FALSE);
1725 }
1726
1727 static void
1728 buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region) {
1729         isc_buffer_init(buffer, region->base, region->length);
1730         isc_buffer_add(buffer, region->length);
1731         isc_buffer_setactive(buffer, region->length);
1732 }
1733
1734 static isc_result_t
1735 uint32_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1736         isc_region_t region;
1737
1738         isc_buffer_availableregion(target, &region);
1739         if (region.length < 4)
1740                 return (ISC_R_NOSPACE);
1741         isc_buffer_putuint32(target, value);
1742         return (ISC_R_SUCCESS);
1743 }
1744
1745 static isc_result_t
1746 uint16_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1747         isc_region_t region;
1748
1749         if (value > 0xffff)
1750                 return (ISC_R_RANGE);
1751         isc_buffer_availableregion(target, &region);
1752         if (region.length < 2)
1753                 return (ISC_R_NOSPACE);
1754         isc_buffer_putuint16(target, (isc_uint16_t)value);
1755         return (ISC_R_SUCCESS);
1756 }
1757
1758 static isc_result_t
1759 uint8_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1760         isc_region_t region;
1761
1762         if (value > 0xff)
1763                 return (ISC_R_RANGE);
1764         isc_buffer_availableregion(target, &region);
1765         if (region.length < 1)
1766                 return (ISC_R_NOSPACE);
1767         isc_buffer_putuint8(target, (isc_uint8_t)value);
1768         return (ISC_R_SUCCESS);
1769 }
1770
1771 static isc_result_t
1772 name_tobuffer(dns_name_t *name, isc_buffer_t *target) {
1773         isc_region_t r;
1774         dns_name_toregion(name, &r);
1775         return (isc_buffer_copyregion(target, &r));
1776 }
1777
1778 static isc_uint32_t
1779 uint32_fromregion(isc_region_t *region) {
1780         isc_uint32_t value;
1781
1782         REQUIRE(region->length >= 4);
1783         value = region->base[0] << 24;
1784         value |= region->base[1] << 16;
1785         value |= region->base[2] << 8;
1786         value |= region->base[3];
1787         return(value);
1788 }
1789
1790 static isc_uint16_t
1791 uint16_consume_fromregion(isc_region_t *region) {
1792         isc_uint16_t r = uint16_fromregion(region);
1793
1794         isc_region_consume(region, 2);
1795         return r;
1796 }
1797
1798 static isc_uint16_t
1799 uint16_fromregion(isc_region_t *region) {
1800
1801         REQUIRE(region->length >= 2);
1802
1803         return ((region->base[0] << 8) | region->base[1]);
1804 }
1805
1806 static isc_uint8_t
1807 uint8_fromregion(isc_region_t *region) {
1808
1809         REQUIRE(region->length >= 1);
1810
1811         return (region->base[0]);
1812 }
1813
1814 static isc_uint8_t
1815 uint8_consume_fromregion(isc_region_t *region) {
1816         isc_uint8_t r = uint8_fromregion(region);
1817
1818         isc_region_consume(region, 1);
1819         return r;
1820 }
1821
1822 static isc_result_t
1823 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
1824         isc_region_t tr;
1825
1826         if (length == 0U)
1827                 return (ISC_R_SUCCESS);
1828
1829         isc_buffer_availableregion(target, &tr);
1830         if (length > tr.length)
1831                 return (ISC_R_NOSPACE);
1832         if (tr.base != base)
1833                 memmove(tr.base, base, length);
1834         isc_buffer_add(target, length);
1835         return (ISC_R_SUCCESS);
1836 }
1837
1838 static int
1839 hexvalue(char value) {
1840         const char *s;
1841         unsigned char c;
1842
1843         c = (unsigned char)value;
1844
1845         if (!isascii(c))
1846                 return (-1);
1847         if (isupper(c))
1848                 c = tolower(c);
1849         if ((s = strchr(hexdigits, c)) == NULL)
1850                 return (-1);
1851         return (int)(s - hexdigits);
1852 }
1853
1854 static int
1855 decvalue(char value) {
1856         const char *s;
1857
1858         /*
1859          * isascii() is valid for full range of int values, no need to
1860          * mask or cast.
1861          */
1862         if (!isascii(value))
1863                 return (-1);
1864         if ((s = strchr(decdigits, value)) == NULL)
1865                 return (-1);
1866         return (int)(s - decdigits);
1867 }
1868
1869 static const char atob_digits[86] =
1870         "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" \
1871         "abcdefghijklmnopqrstu";
1872 /*
1873  * Subroutines to convert between 8 bit binary bytes and printable ASCII.
1874  * Computes the number of bytes, and three kinds of simple checksums.
1875  * Incoming bytes are collected into 32-bit words, then printed in base 85:
1876  *      exp(85,5) > exp(2,32)
1877  * The ASCII characters used are between '!' and 'u';
1878  * 'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.
1879  *
1880  * Originally by Paul Rutter (philabs!per) and Joe Orost (petsd!joe) for
1881  * the atob/btoa programs, released with the compress program, in mod.sources.
1882  * Modified by Mike Schwartz 8/19/86 for use in BIND.
1883  * Modified to be re-entrant 3/2/99.
1884  */
1885
1886
1887 struct state {
1888         isc_int32_t Ceor;
1889         isc_int32_t Csum;
1890         isc_int32_t Crot;
1891         isc_int32_t word;
1892         isc_int32_t bcount;
1893 };
1894
1895 #define Ceor state->Ceor
1896 #define Csum state->Csum
1897 #define Crot state->Crot
1898 #define word state->word
1899 #define bcount state->bcount
1900
1901 #define times85(x)      ((((((x<<2)+x)<<2)+x)<<2)+x)
1902
1903 static isc_result_t     byte_atob(int c, isc_buffer_t *target,
1904                                   struct state *state);
1905 static isc_result_t     putbyte(int c, isc_buffer_t *, struct state *state);
1906 static isc_result_t     byte_btoa(int c, isc_buffer_t *, struct state *state);
1907
1908 /*
1909  * Decode ASCII-encoded byte c into binary representation and
1910  * place into *bufp, advancing bufp.
1911  */
1912 static isc_result_t
1913 byte_atob(int c, isc_buffer_t *target, struct state *state) {
1914         const char *s;
1915         if (c == 'z') {
1916                 if (bcount != 0)
1917                         return(DNS_R_SYNTAX);
1918                 else {
1919                         RETERR(putbyte(0, target, state));
1920                         RETERR(putbyte(0, target, state));
1921                         RETERR(putbyte(0, target, state));
1922                         RETERR(putbyte(0, target, state));
1923                 }
1924         } else if ((s = strchr(atob_digits, c)) != NULL) {
1925                 if (bcount == 0) {
1926                         word = (isc_int32_t)(s - atob_digits);
1927                         ++bcount;
1928                 } else if (bcount < 4) {
1929                         word = times85(word);
1930                         word += (isc_int32_t)(s - atob_digits);
1931                         ++bcount;
1932                 } else {
1933                         word = times85(word);
1934                         word += (isc_int32_t)(s - atob_digits);
1935                         RETERR(putbyte((word >> 24) & 0xff, target, state));
1936                         RETERR(putbyte((word >> 16) & 0xff, target, state));
1937                         RETERR(putbyte((word >> 8) & 0xff, target, state));
1938                         RETERR(putbyte(word & 0xff, target, state));
1939                         word = 0;
1940                         bcount = 0;
1941                 }
1942         } else
1943                 return(DNS_R_SYNTAX);
1944         return(ISC_R_SUCCESS);
1945 }
1946
1947 /*
1948  * Compute checksum info and place c into target.
1949  */
1950 static isc_result_t
1951 putbyte(int c, isc_buffer_t *target, struct state *state) {
1952         isc_region_t tr;
1953
1954         Ceor ^= c;
1955         Csum += c;
1956         Csum += 1;
1957         if ((Crot & 0x80000000)) {
1958                 Crot <<= 1;
1959                 Crot += 1;
1960         } else {
1961                 Crot <<= 1;
1962         }
1963         Crot += c;
1964         isc_buffer_availableregion(target, &tr);
1965         if (tr.length < 1)
1966                 return (ISC_R_NOSPACE);
1967         tr.base[0] = c;
1968         isc_buffer_add(target, 1);
1969         return (ISC_R_SUCCESS);
1970 }
1971
1972 /*
1973  * Read the ASCII-encoded data from inbuf, of length inbuflen, and convert
1974  * it into T_UNSPEC (binary data) in outbuf, not to exceed outbuflen bytes;
1975  * outbuflen must be divisible by 4.  (Note: this is because outbuf is filled
1976  * in 4 bytes at a time.  If the actual data doesn't end on an even 4-byte
1977  * boundary, there will be no problem...it will be padded with 0 bytes, and
1978  * numbytes will indicate the correct number of bytes.  The main point is
1979  * that since the buffer is filled in 4 bytes at a time, even if there is
1980  * not a full 4 bytes of data at the end, there has to be room to 0-pad the
1981  * data, so the buffer must be of size divisible by 4).  Place the number of
1982  * output bytes in numbytes, and return a failure/success status.
1983  */
1984
1985 static isc_result_t
1986 atob_tobuffer(isc_lex_t *lexer, isc_buffer_t *target) {
1987         long oeor, osum, orot;
1988         struct state statebuf, *state= &statebuf;
1989         isc_token_t token;
1990         char c;
1991         char *e;
1992
1993         Ceor = Csum = Crot = word = bcount = 0;
1994
1995         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1996                                       ISC_FALSE));
1997         while (token.value.as_textregion.length != 0) {
1998                 if ((c = token.value.as_textregion.base[0]) == 'x') {
1999                         break;
2000                 } else
2001                         RETERR(byte_atob(c, target, state));
2002                 isc_textregion_consume(&token.value.as_textregion, 1);
2003         }
2004
2005         /*
2006          * Number of bytes.
2007          */
2008         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
2009                                       ISC_FALSE));
2010         if ((token.value.as_ulong % 4) != 0U) {
2011                 unsigned long padding = 4 - (token.value.as_ulong % 4);
2012                 if (isc_buffer_usedlength(target) < padding)
2013                         return (DNS_R_SYNTAX);
2014                 isc_buffer_subtract(target, padding);
2015         }
2016
2017         /*
2018          * Checksum.
2019          */
2020         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
2021                                       ISC_FALSE));
2022         oeor = strtol(DNS_AS_STR(token), &e, 16);
2023         if (*e != 0)
2024                 return (DNS_R_SYNTAX);
2025
2026         /*
2027          * Checksum.
2028          */
2029         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
2030                                       ISC_FALSE));
2031         osum = strtol(DNS_AS_STR(token), &e, 16);
2032         if (*e != 0)
2033                 return (DNS_R_SYNTAX);
2034
2035         /*
2036          * Checksum.
2037          */
2038         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
2039                                       ISC_FALSE));
2040         orot = strtol(DNS_AS_STR(token), &e, 16);
2041         if (*e != 0)
2042                 return (DNS_R_SYNTAX);
2043
2044         if ((oeor != Ceor) || (osum != Csum) || (orot != Crot))
2045                 return(DNS_R_BADCKSUM);
2046         return (ISC_R_SUCCESS);
2047 }
2048
2049 /*
2050  * Encode binary byte c into ASCII representation and place into *bufp,
2051  * advancing bufp.
2052  */
2053 static isc_result_t
2054 byte_btoa(int c, isc_buffer_t *target, struct state *state) {
2055         isc_region_t tr;
2056
2057         isc_buffer_availableregion(target, &tr);
2058         Ceor ^= c;
2059         Csum += c;
2060         Csum += 1;
2061         if ((Crot & 0x80000000)) {
2062                 Crot <<= 1;
2063                 Crot += 1;
2064         } else {
2065                 Crot <<= 1;
2066         }
2067         Crot += c;
2068
2069         word <<= 8;
2070         word |= c;
2071         if (bcount == 3) {
2072                 if (word == 0) {
2073                         if (tr.length < 1)
2074                                 return (ISC_R_NOSPACE);
2075                         tr.base[0] = 'z';
2076                         isc_buffer_add(target, 1);
2077                 } else {
2078                     register int tmp = 0;
2079                     register isc_int32_t tmpword = word;
2080
2081                     if (tmpword < 0) {
2082                            /*
2083                             * Because some don't support u_long.
2084                             */
2085                         tmp = 32;
2086                         tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
2087                     }
2088                     if (tmpword < 0) {
2089                         tmp = 64;
2090                         tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
2091                     }
2092                         if (tr.length < 5)
2093                                 return (ISC_R_NOSPACE);
2094                         tr.base[0] = atob_digits[(tmpword /
2095                                               (isc_int32_t)(85 * 85 * 85 * 85))
2096                                                 + tmp];
2097                         tmpword %= (isc_int32_t)(85 * 85 * 85 * 85);
2098                         tr.base[1] = atob_digits[tmpword / (85 * 85 * 85)];
2099                         tmpword %= (85 * 85 * 85);
2100                         tr.base[2] = atob_digits[tmpword / (85 * 85)];
2101                         tmpword %= (85 * 85);
2102                         tr.base[3] = atob_digits[tmpword / 85];
2103                         tmpword %= 85;
2104                         tr.base[4] = atob_digits[tmpword];
2105                         isc_buffer_add(target, 5);
2106                 }
2107                 bcount = 0;
2108         } else {
2109                 bcount += 1;
2110         }
2111         return (ISC_R_SUCCESS);
2112 }
2113
2114
2115 /*
2116  * Encode the binary data from inbuf, of length inbuflen, into a
2117  * target.  Return success/failure status
2118  */
2119 static isc_result_t
2120 btoa_totext(unsigned char *inbuf, int inbuflen, isc_buffer_t *target) {
2121         int inc;
2122         struct state statebuf, *state = &statebuf;
2123         char buf[sizeof("x 2000000000 ffffffff ffffffff ffffffff")];
2124
2125         Ceor = Csum = Crot = word = bcount = 0;
2126         for (inc = 0; inc < inbuflen; inbuf++, inc++)
2127                 RETERR(byte_btoa(*inbuf, target, state));
2128
2129         while (bcount != 0)
2130                 RETERR(byte_btoa(0, target, state));
2131
2132         /*
2133          * Put byte count and checksum information at end of buffer,
2134          * delimited by 'x'
2135          */
2136         snprintf(buf, sizeof(buf), "x %d %x %x %x", inbuflen, Ceor, Csum, Crot);
2137         return (str_totext(buf, target));
2138 }
2139
2140
2141 static void
2142 default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *fmt,
2143                           ...)
2144 {
2145         va_list ap;
2146
2147         UNUSED(callbacks);
2148
2149         va_start(ap, fmt);
2150         vfprintf(stderr, fmt, ap);
2151         va_end(ap);
2152         fprintf(stderr, "\n");
2153 }
2154
2155 static void
2156 fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks) {
2157         if (isc_lex_isfile(lexer) && callbacks != NULL) {
2158                 const char *name = isc_lex_getsourcename(lexer);
2159                 if (name == NULL)
2160                         name = "UNKNOWN";
2161                 (*callbacks->warn)(callbacks,
2162                                    "%s:%lu: file does not end with newline",
2163                                    name, isc_lex_getsourceline(lexer));
2164         }
2165 }
2166
2167 static void
2168 warn_badmx(isc_token_t *token, isc_lex_t *lexer,
2169            dns_rdatacallbacks_t *callbacks)
2170 {
2171         const char *file;
2172         unsigned long line;
2173
2174         if (lexer != NULL) {
2175                 file = isc_lex_getsourcename(lexer);
2176                 line = isc_lex_getsourceline(lexer);
2177                 (*callbacks->warn)(callbacks, "%s:%u: warning: '%s': %s",
2178                                    file, line, DNS_AS_STR(*token),
2179                                    dns_result_totext(DNS_R_MXISADDRESS));
2180         }
2181 }
2182
2183 static void
2184 warn_badname(dns_name_t *name, isc_lex_t *lexer,
2185              dns_rdatacallbacks_t *callbacks)
2186 {
2187         const char *file;
2188         unsigned long line;
2189         char namebuf[DNS_NAME_FORMATSIZE];
2190
2191         if (lexer != NULL) {
2192                 file = isc_lex_getsourcename(lexer);
2193                 line = isc_lex_getsourceline(lexer);
2194                 dns_name_format(name, namebuf, sizeof(namebuf));
2195                 (*callbacks->warn)(callbacks, "%s:%u: warning: %s: %s",
2196                                    file, line, namebuf,
2197                                    dns_result_totext(DNS_R_BADNAME));
2198         }
2199 }
2200
2201 static void
2202 fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
2203                dns_rdatacallbacks_t *callbacks, const char *name,
2204                unsigned long line, isc_token_t *token, isc_result_t result)
2205 {
2206         if (name == NULL)
2207                 name = "UNKNOWN";
2208
2209         if (token != NULL) {
2210                 switch (token->type) {
2211                 case isc_tokentype_eol:
2212                         (*callback)(callbacks, "%s: %s:%lu: near eol: %s",
2213                                     "dns_rdata_fromtext", name, line,
2214                                     dns_result_totext(result));
2215                         break;
2216                 case isc_tokentype_eof:
2217                         (*callback)(callbacks, "%s: %s:%lu: near eof: %s",
2218                                     "dns_rdata_fromtext", name, line,
2219                                     dns_result_totext(result));
2220                         break;
2221                 case isc_tokentype_number:
2222                         (*callback)(callbacks, "%s: %s:%lu: near %lu: %s",
2223                                     "dns_rdata_fromtext", name, line,
2224                                     token->value.as_ulong,
2225                                     dns_result_totext(result));
2226                         break;
2227                 case isc_tokentype_string:
2228                 case isc_tokentype_qstring:
2229                         (*callback)(callbacks, "%s: %s:%lu: near '%s': %s",
2230                                     "dns_rdata_fromtext", name, line,
2231                                     DNS_AS_STR(*token),
2232                                     dns_result_totext(result));
2233                         break;
2234                 default:
2235                         (*callback)(callbacks, "%s: %s:%lu: %s",
2236                                     "dns_rdata_fromtext", name, line,
2237                                     dns_result_totext(result));
2238                         break;
2239                 }
2240         } else {
2241                 (*callback)(callbacks, "dns_rdata_fromtext: %s:%lu: %s",
2242                             name, line, dns_result_totext(result));
2243         }
2244 }
2245
2246 dns_rdatatype_t
2247 dns_rdata_covers(dns_rdata_t *rdata) {
2248         if (rdata->type == 46)
2249                 return (covers_rrsig(rdata));
2250         return (covers_sig(rdata));
2251 }
2252
2253 isc_boolean_t
2254 dns_rdatatype_ismeta(dns_rdatatype_t type) {
2255         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_META) != 0)
2256                 return (ISC_TRUE);
2257         return (ISC_FALSE);
2258 }
2259
2260 isc_boolean_t
2261 dns_rdatatype_issingleton(dns_rdatatype_t type) {
2262         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_SINGLETON)
2263             != 0)
2264                 return (ISC_TRUE);
2265         return (ISC_FALSE);
2266 }
2267
2268 isc_boolean_t
2269 dns_rdatatype_notquestion(dns_rdatatype_t type) {
2270         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_NOTQUESTION)
2271             != 0)
2272                 return (ISC_TRUE);
2273         return (ISC_FALSE);
2274 }
2275
2276 isc_boolean_t
2277 dns_rdatatype_questiononly(dns_rdatatype_t type) {
2278         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_QUESTIONONLY)
2279             != 0)
2280                 return (ISC_TRUE);
2281         return (ISC_FALSE);
2282 }
2283
2284 isc_boolean_t
2285 dns_rdatatype_atparent(dns_rdatatype_t type) {
2286         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_ATPARENT) != 0)
2287                 return (ISC_TRUE);
2288         return (ISC_FALSE);
2289 }
2290
2291 isc_boolean_t
2292 dns_rdataclass_ismeta(dns_rdataclass_t rdclass) {
2293
2294         if (rdclass == dns_rdataclass_reserved0
2295             || rdclass == dns_rdataclass_none
2296             || rdclass == dns_rdataclass_any)
2297                 return (ISC_TRUE);
2298
2299         return (ISC_FALSE);  /* Assume it is not a meta class. */
2300 }
2301
2302 isc_boolean_t
2303 dns_rdatatype_isdnssec(dns_rdatatype_t type) {
2304         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_DNSSEC) != 0)
2305                 return (ISC_TRUE);
2306         return (ISC_FALSE);
2307 }
2308
2309 isc_boolean_t
2310 dns_rdatatype_iszonecutauth(dns_rdatatype_t type) {
2311         if ((dns_rdatatype_attributes(type)
2312              & (DNS_RDATATYPEATTR_DNSSEC | DNS_RDATATYPEATTR_ZONECUTAUTH))
2313             != 0)
2314                 return (ISC_TRUE);
2315         return (ISC_FALSE);
2316 }
2317
2318 isc_boolean_t
2319 dns_rdatatype_isknown(dns_rdatatype_t type) {
2320         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_UNKNOWN)
2321             == 0)
2322                 return (ISC_TRUE);
2323         return (ISC_FALSE);
2324 }
2325
2326 void
2327 dns_rdata_exists(dns_rdata_t *rdata, dns_rdatatype_t type) {
2328
2329         REQUIRE(rdata != NULL);
2330         REQUIRE(DNS_RDATA_INITIALIZED(rdata));
2331
2332         rdata->data = NULL;
2333         rdata->length = 0;
2334         rdata->flags = DNS_RDATA_UPDATE;
2335         rdata->type = type;
2336         rdata->rdclass = dns_rdataclass_any;
2337 }
2338
2339 void
2340 dns_rdata_notexist(dns_rdata_t *rdata, dns_rdatatype_t type) {
2341
2342         REQUIRE(rdata != NULL);
2343         REQUIRE(DNS_RDATA_INITIALIZED(rdata));
2344
2345         rdata->data = NULL;
2346         rdata->length = 0;
2347         rdata->flags = DNS_RDATA_UPDATE;
2348         rdata->type = type;
2349         rdata->rdclass = dns_rdataclass_none;
2350 }
2351
2352 void
2353 dns_rdata_deleterrset(dns_rdata_t *rdata, dns_rdatatype_t type) {
2354
2355         REQUIRE(rdata != NULL);
2356         REQUIRE(DNS_RDATA_INITIALIZED(rdata));
2357
2358         rdata->data = NULL;
2359         rdata->length = 0;
2360         rdata->flags = DNS_RDATA_UPDATE;
2361         rdata->type = type;
2362         rdata->rdclass = dns_rdataclass_any;
2363 }
2364
2365 void
2366 dns_rdata_makedelete(dns_rdata_t *rdata) {
2367         REQUIRE(rdata != NULL);
2368
2369         rdata->rdclass = dns_rdataclass_none;
2370 }
2371
2372 const char *
2373 dns_rdata_updateop(dns_rdata_t *rdata, dns_section_t section) {
2374
2375         REQUIRE(rdata != NULL);
2376         REQUIRE(DNS_RDATA_INITIALIZED(rdata));
2377
2378         switch (section) {
2379         case DNS_SECTION_PREREQUISITE:
2380                 switch (rdata->rdclass) {
2381                 case dns_rdataclass_none:
2382                         switch (rdata->type) {
2383                         case dns_rdatatype_any:
2384                                 return ("domain doesn't exist");
2385                         default:
2386                                 return ("rrset doesn't exist");
2387                         }
2388                 case dns_rdataclass_any:
2389                         switch (rdata->type) {
2390                         case dns_rdatatype_any:
2391                                 return ("domain exists");
2392                         default:
2393                                 return ("rrset exists (value independent)");
2394                         }
2395                 default:
2396                         return ("rrset exists (value dependent)");
2397                 }
2398         case DNS_SECTION_UPDATE:
2399                 switch (rdata->rdclass) {
2400                 case dns_rdataclass_none:
2401                         return ("delete");
2402                 case dns_rdataclass_any:
2403                         switch (rdata->type) {
2404                         case dns_rdatatype_any:
2405                                 return ("delete all rrsets");
2406                         default:
2407                                 return ("delete rrset");
2408                         }
2409                 default:
2410                         return ("add");
2411                 }
2412         }
2413         return ("invalid");
2414 }