]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - contrib/bind9/lib/dns/rdata.c
Upgrade to 9.6-ESV-R7-P1, the latest from ISC. This version contains
[FreeBSD/stable/8.git] / contrib / bind9 / lib / dns / rdata.c
1 /*
2  * Copyright (C) 2004-2009, 2011, 2012  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/rcode.h>
42 #include <dns/rdata.h>
43 #include <dns/rdataclass.h>
44 #include <dns/rdatastruct.h>
45 #include <dns/rdatatype.h>
46 #include <dns/result.h>
47 #include <dns/secalg.h>
48 #include <dns/secproto.h>
49 #include <dns/time.h>
50 #include <dns/ttl.h>
51
52 #define RETERR(x) \
53         do { \
54                 isc_result_t _r = (x); \
55                 if (_r != ISC_R_SUCCESS) \
56                         return (_r); \
57         } while (0)
58
59 #define RETTOK(x) \
60         do { \
61                 isc_result_t _r = (x); \
62                 if (_r != ISC_R_SUCCESS) { \
63                         isc_lex_ungettoken(lexer, &token); \
64                         return (_r); \
65                 } \
66         } while (0)
67
68 #define DNS_AS_STR(t) ((t).value.as_textregion.base)
69
70 #define ARGS_FROMTEXT   int rdclass, dns_rdatatype_t type, \
71                         isc_lex_t *lexer, dns_name_t *origin, \
72                         unsigned int options, isc_buffer_t *target, \
73                         dns_rdatacallbacks_t *callbacks
74
75 #define ARGS_TOTEXT     dns_rdata_t *rdata, dns_rdata_textctx_t *tctx, \
76                         isc_buffer_t *target
77
78 #define ARGS_FROMWIRE   int rdclass, dns_rdatatype_t type, \
79                         isc_buffer_t *source, dns_decompress_t *dctx, \
80                         unsigned int options, isc_buffer_t *target
81
82 #define ARGS_TOWIRE     dns_rdata_t *rdata, dns_compress_t *cctx, \
83                         isc_buffer_t *target
84
85 #define ARGS_COMPARE    const dns_rdata_t *rdata1, const dns_rdata_t *rdata2
86
87 #define ARGS_FROMSTRUCT int rdclass, dns_rdatatype_t type, \
88                         void *source, isc_buffer_t *target
89
90 #define ARGS_TOSTRUCT   dns_rdata_t *rdata, void *target, isc_mem_t *mctx
91
92 #define ARGS_FREESTRUCT void *source
93
94 #define ARGS_ADDLDATA   dns_rdata_t *rdata, dns_additionaldatafunc_t add, \
95                         void *arg
96
97 #define ARGS_DIGEST     dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg
98
99 #define ARGS_CHECKOWNER dns_name_t *name, dns_rdataclass_t rdclass, \
100                         dns_rdatatype_t type, isc_boolean_t wildcard
101
102 #define ARGS_CHECKNAMES dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad
103
104
105 /*%
106  * Context structure for the totext_ functions.
107  * Contains formatting options for rdata-to-text
108  * conversion.
109  */
110 typedef struct dns_rdata_textctx {
111         dns_name_t *origin;     /*%< Current origin, or NULL. */
112         unsigned int flags;     /*%< DNS_STYLEFLAG_*  */
113         unsigned int width;     /*%< Width of rdata column. */
114         const char *linebreak;  /*%< Line break string. */
115 } dns_rdata_textctx_t;
116
117 static isc_result_t
118 txt_totext(isc_region_t *source, isc_buffer_t *target);
119
120 static isc_result_t
121 txt_fromtext(isc_textregion_t *source, isc_buffer_t *target);
122
123 static isc_result_t
124 txt_fromwire(isc_buffer_t *source, isc_buffer_t *target);
125
126 static isc_boolean_t
127 name_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target);
128
129 static unsigned int
130 name_length(dns_name_t *name);
131
132 static isc_result_t
133 str_totext(const char *source, isc_buffer_t *target);
134
135 static isc_result_t
136 inet_totext(int af, isc_region_t *src, isc_buffer_t *target);
137
138 static isc_boolean_t
139 buffer_empty(isc_buffer_t *source);
140
141 static void
142 buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region);
143
144 static isc_result_t
145 uint32_tobuffer(isc_uint32_t, isc_buffer_t *target);
146
147 static isc_result_t
148 uint16_tobuffer(isc_uint32_t, isc_buffer_t *target);
149
150 static isc_result_t
151 uint8_tobuffer(isc_uint32_t, isc_buffer_t *target);
152
153 static isc_result_t
154 name_tobuffer(dns_name_t *name, isc_buffer_t *target);
155
156 static isc_uint32_t
157 uint32_fromregion(isc_region_t *region);
158
159 static isc_uint16_t
160 uint16_fromregion(isc_region_t *region);
161
162 static isc_uint8_t
163 uint8_fromregion(isc_region_t *region);
164
165 static isc_uint8_t
166 uint8_consume_fromregion(isc_region_t *region);
167
168 static isc_result_t
169 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
170
171 static int
172 hexvalue(char value);
173
174 static int
175 decvalue(char value);
176
177 static isc_result_t
178 btoa_totext(unsigned char *inbuf, int inbuflen, isc_buffer_t *target);
179
180 static isc_result_t
181 atob_tobuffer(isc_lex_t *lexer, isc_buffer_t *target);
182
183 static void
184 default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *, ...)
185      ISC_FORMAT_PRINTF(2, 3);
186
187 static void
188 fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
189                dns_rdatacallbacks_t *callbacks, const char *name,
190                unsigned long line, isc_token_t *token, isc_result_t result);
191
192 static void
193 fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks);
194
195 static isc_result_t
196 rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
197              isc_buffer_t *target);
198
199 static void
200 warn_badname(dns_name_t *name, isc_lex_t *lexer,
201              dns_rdatacallbacks_t *callbacks);
202
203 static void
204 warn_badmx(isc_token_t *token, isc_lex_t *lexer,
205            dns_rdatacallbacks_t *callbacks);
206
207 static isc_uint16_t
208 uint16_consume_fromregion(isc_region_t *region);
209
210 static isc_result_t
211 unknown_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
212                isc_buffer_t *target);
213
214 static inline int
215 getquad(const void *src, struct in_addr *dst,
216         isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks)
217 {
218         int result;
219         struct in_addr *tmp;
220
221         result = inet_aton(src, dst);
222         if (result == 1 && callbacks != NULL &&
223             inet_pton(AF_INET, src, &tmp) != 1) {
224                 const char *name = isc_lex_getsourcename(lexer);
225                 if (name == NULL)
226                         name = "UNKNOWN";
227                 (*callbacks->warn)(callbacks, "%s:%lu: \"%s\" "
228                                    "is not a decimal dotted quad", name,
229                                    isc_lex_getsourceline(lexer), src);
230         }
231         return (result);
232 }
233
234 static inline isc_result_t
235 name_duporclone(dns_name_t *source, isc_mem_t *mctx, dns_name_t *target) {
236
237         if (mctx != NULL)
238                 return (dns_name_dup(source, mctx, target));
239         dns_name_clone(source, target);
240         return (ISC_R_SUCCESS);
241 }
242
243 static inline void *
244 mem_maybedup(isc_mem_t *mctx, void *source, size_t length) {
245         void *new;
246
247         if (mctx == NULL)
248                 return (source);
249         new = isc_mem_allocate(mctx, length);
250         if (new != NULL)
251                 memcpy(new, source, length);
252
253         return (new);
254 }
255
256 static const char hexdigits[] = "0123456789abcdef";
257 static const char decdigits[] = "0123456789";
258
259 #include "code.h"
260
261 #define META 0x0001
262 #define RESERVED 0x0002
263
264 /***
265  *** Initialization
266  ***/
267
268 void
269 dns_rdata_init(dns_rdata_t *rdata) {
270
271         REQUIRE(rdata != NULL);
272
273         rdata->data = NULL;
274         rdata->length = 0;
275         rdata->rdclass = 0;
276         rdata->type = 0;
277         rdata->flags = 0;
278         ISC_LINK_INIT(rdata, link);
279         /* ISC_LIST_INIT(rdata->list); */
280 }
281
282 #if 1
283 #define DNS_RDATA_INITIALIZED(rdata) \
284         ((rdata)->data == NULL && (rdata)->length == 0 && \
285          (rdata)->rdclass == 0 && (rdata)->type == 0 && (rdata)->flags == 0 && \
286          !ISC_LINK_LINKED((rdata), link))
287 #else
288 #ifdef ISC_LIST_CHECKINIT
289 #define DNS_RDATA_INITIALIZED(rdata) \
290         (!ISC_LINK_LINKED((rdata), link))
291 #else
292 #define DNS_RDATA_INITIALIZED(rdata) ISC_TRUE
293 #endif
294 #endif
295
296 #define DNS_RDATA_VALIDFLAGS(rdata) \
297         (((rdata)->flags & ~(DNS_RDATA_UPDATE|DNS_RDATA_OFFLINE)) == 0)
298
299 void
300 dns_rdata_reset(dns_rdata_t *rdata) {
301
302         REQUIRE(rdata != NULL);
303
304         REQUIRE(!ISC_LINK_LINKED(rdata, link));
305         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
306
307         rdata->data = NULL;
308         rdata->length = 0;
309         rdata->rdclass = 0;
310         rdata->type = 0;
311         rdata->flags = 0;
312 }
313
314 /***
315  ***
316  ***/
317
318 void
319 dns_rdata_clone(const dns_rdata_t *src, dns_rdata_t *target) {
320
321         REQUIRE(src != NULL);
322         REQUIRE(target != NULL);
323
324         REQUIRE(DNS_RDATA_INITIALIZED(target));
325
326         REQUIRE(DNS_RDATA_VALIDFLAGS(src));
327         REQUIRE(DNS_RDATA_VALIDFLAGS(target));
328
329         target->data = src->data;
330         target->length = src->length;
331         target->rdclass = src->rdclass;
332         target->type = src->type;
333         target->flags = src->flags;
334 }
335
336
337 /***
338  *** Comparisons
339  ***/
340
341 int
342 dns_rdata_compare(const dns_rdata_t *rdata1, const dns_rdata_t *rdata2) {
343         int result = 0;
344         isc_boolean_t use_default = ISC_FALSE;
345
346         REQUIRE(rdata1 != NULL);
347         REQUIRE(rdata2 != NULL);
348         REQUIRE(rdata1->length == 0 || rdata1->data != NULL);
349         REQUIRE(rdata2->length == 0 || rdata2->data != NULL);
350         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata1));
351         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata2));
352
353         if (rdata1->rdclass != rdata2->rdclass)
354                 return (rdata1->rdclass < rdata2->rdclass ? -1 : 1);
355
356         if (rdata1->type != rdata2->type)
357                 return (rdata1->type < rdata2->type ? -1 : 1);
358
359         COMPARESWITCH
360
361         if (use_default) {
362                 isc_region_t r1;
363                 isc_region_t r2;
364
365                 dns_rdata_toregion(rdata1, &r1);
366                 dns_rdata_toregion(rdata2, &r2);
367                 result = isc_region_compare(&r1, &r2);
368         }
369         return (result);
370 }
371
372 /***
373  *** Conversions
374  ***/
375
376 void
377 dns_rdata_fromregion(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
378                      dns_rdatatype_t type, isc_region_t *r)
379 {
380
381         REQUIRE(rdata != NULL);
382         REQUIRE(DNS_RDATA_INITIALIZED(rdata));
383         REQUIRE(r != NULL);
384
385         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
386
387         rdata->data = r->base;
388         rdata->length = r->length;
389         rdata->rdclass = rdclass;
390         rdata->type = type;
391         rdata->flags = 0;
392 }
393
394 void
395 dns_rdata_toregion(const dns_rdata_t *rdata, isc_region_t *r) {
396
397         REQUIRE(rdata != NULL);
398         REQUIRE(r != NULL);
399         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
400
401         r->base = rdata->data;
402         r->length = rdata->length;
403 }
404
405 isc_result_t
406 dns_rdata_fromwire(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
407                    dns_rdatatype_t type, isc_buffer_t *source,
408                    dns_decompress_t *dctx, unsigned int options,
409                    isc_buffer_t *target)
410 {
411         isc_result_t result = ISC_R_NOTIMPLEMENTED;
412         isc_region_t region;
413         isc_buffer_t ss;
414         isc_buffer_t st;
415         isc_boolean_t use_default = ISC_FALSE;
416         isc_uint32_t activelength;
417
418         REQUIRE(dctx != NULL);
419         if (rdata != NULL) {
420                 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
421                 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
422         }
423
424         if (type == 0)
425                 return (DNS_R_FORMERR);
426
427         ss = *source;
428         st = *target;
429
430         activelength = isc_buffer_activelength(source);
431         INSIST(activelength < 65536);
432
433         FROMWIRESWITCH
434
435         if (use_default) {
436                 if (activelength > isc_buffer_availablelength(target))
437                         result = ISC_R_NOSPACE;
438                 else {
439                         isc_buffer_putmem(target, isc_buffer_current(source),
440                                           activelength);
441                         isc_buffer_forward(source, activelength);
442                         result = ISC_R_SUCCESS;
443                 }
444         }
445
446         /*
447          * We should have consumed all of our buffer.
448          */
449         if (result == ISC_R_SUCCESS && !buffer_empty(source))
450                 result = DNS_R_EXTRADATA;
451
452         if (rdata != NULL && result == ISC_R_SUCCESS) {
453                 region.base = isc_buffer_used(&st);
454                 region.length = isc_buffer_usedlength(target) -
455                                 isc_buffer_usedlength(&st);
456                 dns_rdata_fromregion(rdata, rdclass, type, &region);
457         }
458
459         if (result != ISC_R_SUCCESS) {
460                 *source = ss;
461                 *target = st;
462         }
463         return (result);
464 }
465
466 isc_result_t
467 dns_rdata_towire(dns_rdata_t *rdata, dns_compress_t *cctx,
468                  isc_buffer_t *target)
469 {
470         isc_result_t result = ISC_R_NOTIMPLEMENTED;
471         isc_boolean_t use_default = ISC_FALSE;
472         isc_region_t tr;
473         isc_buffer_t st;
474
475         REQUIRE(rdata != NULL);
476         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
477
478         /*
479          * Some DynDNS meta-RRs have empty rdata.
480          */
481         if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
482                 INSIST(rdata->length == 0);
483                 return (ISC_R_SUCCESS);
484         }
485
486         st = *target;
487
488         TOWIRESWITCH
489
490         if (use_default) {
491                 isc_buffer_availableregion(target, &tr);
492                 if (tr.length < rdata->length)
493                         return (ISC_R_NOSPACE);
494                 memcpy(tr.base, rdata->data, rdata->length);
495                 isc_buffer_add(target, rdata->length);
496                 return (ISC_R_SUCCESS);
497         }
498         if (result != ISC_R_SUCCESS) {
499                 *target = st;
500                 INSIST(target->used < 65536);
501                 dns_compress_rollback(cctx, (isc_uint16_t)target->used);
502         }
503         return (result);
504 }
505
506 /*
507  * If the binary data in 'src' is valid uncompressed wire format
508  * rdata of class 'rdclass' and type 'type', return ISC_R_SUCCESS
509  * and copy the validated rdata to 'dest'.  Otherwise return an error.
510  */
511 static isc_result_t
512 rdata_validate(isc_buffer_t *src, isc_buffer_t *dest, dns_rdataclass_t rdclass,
513             dns_rdatatype_t type)
514 {
515         dns_decompress_t dctx;
516         dns_rdata_t rdata = DNS_RDATA_INIT;
517         isc_result_t result;
518
519         dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_NONE);
520         isc_buffer_setactive(src, isc_buffer_usedlength(src));
521         result = dns_rdata_fromwire(&rdata, rdclass, type, src,
522                                     &dctx, 0, dest);
523         dns_decompress_invalidate(&dctx);
524
525         return (result);
526 }
527
528 static isc_result_t
529 unknown_fromtext(dns_rdataclass_t rdclass, dns_rdatatype_t type,
530                  isc_lex_t *lexer, isc_mem_t *mctx, isc_buffer_t *target)
531 {
532         isc_result_t result;
533         isc_buffer_t *buf = NULL;
534         isc_token_t token;
535
536         if (type == 0 || dns_rdatatype_ismeta(type))
537                 return (DNS_R_METATYPE);
538
539         result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
540                                         ISC_FALSE);
541         if (result == ISC_R_SUCCESS && token.value.as_ulong > 65535U)
542                 return (ISC_R_RANGE);
543         result = isc_buffer_allocate(mctx, &buf, token.value.as_ulong);
544         if (result != ISC_R_SUCCESS)
545                 return (result);
546
547         result = isc_hex_tobuffer(lexer, buf,
548                                   (unsigned int)token.value.as_ulong);
549         if (result != ISC_R_SUCCESS)
550                goto failure;
551         if (isc_buffer_usedlength(buf) != token.value.as_ulong) {
552                 result = ISC_R_UNEXPECTEDEND;
553                 goto failure;
554         }
555
556         if (dns_rdatatype_isknown(type)) {
557                 result = rdata_validate(buf, target, rdclass, type);
558         } else {
559                 isc_region_t r;
560                 isc_buffer_usedregion(buf, &r);
561                 result = isc_buffer_copyregion(target, &r);
562         }
563         if (result != ISC_R_SUCCESS)
564                 goto failure;
565
566         isc_buffer_free(&buf);
567         return (ISC_R_SUCCESS);
568
569  failure:
570         isc_buffer_free(&buf);
571         return (result);
572 }
573
574 isc_result_t
575 dns_rdata_fromtext(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
576                    dns_rdatatype_t type, isc_lex_t *lexer,
577                    dns_name_t *origin, unsigned int options, isc_mem_t *mctx,
578                    isc_buffer_t *target, dns_rdatacallbacks_t *callbacks)
579 {
580         isc_result_t result = ISC_R_NOTIMPLEMENTED;
581         isc_region_t region;
582         isc_buffer_t st;
583         isc_token_t token;
584         unsigned int lexoptions = ISC_LEXOPT_EOL | ISC_LEXOPT_EOF |
585                                   ISC_LEXOPT_DNSMULTILINE | ISC_LEXOPT_ESCAPE;
586         char *name;
587         unsigned long line;
588         void (*callback)(dns_rdatacallbacks_t *, const char *, ...);
589         isc_result_t tresult;
590
591         REQUIRE(origin == NULL || dns_name_isabsolute(origin) == ISC_TRUE);
592         if (rdata != NULL) {
593                 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
594                 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
595         }
596         if (callbacks != NULL) {
597                 REQUIRE(callbacks->warn != NULL);
598                 REQUIRE(callbacks->error != NULL);
599         }
600
601         st = *target;
602
603         if (callbacks != NULL)
604                 callback = callbacks->error;
605         else
606                 callback = default_fromtext_callback;
607
608         result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
609                                         ISC_FALSE);
610         if (result != ISC_R_SUCCESS) {
611                 name = isc_lex_getsourcename(lexer);
612                 line = isc_lex_getsourceline(lexer);
613                 fromtext_error(callback, callbacks, name, line, NULL, result);
614                 return (result);
615         }
616
617         if (strcmp(DNS_AS_STR(token), "\\#") == 0)
618                 result = unknown_fromtext(rdclass, type, lexer, mctx, target);
619         else {
620                 isc_lex_ungettoken(lexer, &token);
621
622                 FROMTEXTSWITCH
623         }
624
625         /*
626          * Consume to end of line / file.
627          * If not at end of line initially set error code.
628          * Call callback via fromtext_error once if there was an error.
629          */
630         do {
631                 name = isc_lex_getsourcename(lexer);
632                 line = isc_lex_getsourceline(lexer);
633                 tresult = isc_lex_gettoken(lexer, lexoptions, &token);
634                 if (tresult != ISC_R_SUCCESS) {
635                         if (result == ISC_R_SUCCESS)
636                                 result = tresult;
637                         if (callback != NULL)
638                                 fromtext_error(callback, callbacks, name,
639                                                line, NULL, result);
640                         break;
641                 } else if (token.type != isc_tokentype_eol &&
642                            token.type != isc_tokentype_eof) {
643                         if (result == ISC_R_SUCCESS)
644                                 result = DNS_R_EXTRATOKEN;
645                         if (callback != NULL) {
646                                 fromtext_error(callback, callbacks, name,
647                                                line, &token, result);
648                                 callback = NULL;
649                         }
650                 } else if (result != ISC_R_SUCCESS && callback != NULL) {
651                         fromtext_error(callback, callbacks, name, line,
652                                        &token, result);
653                         break;
654                 } else {
655                         if (token.type == isc_tokentype_eof)
656                                 fromtext_warneof(lexer, callbacks);
657                         break;
658                 }
659         } while (1);
660
661         if (rdata != NULL && result == ISC_R_SUCCESS) {
662                 region.base = isc_buffer_used(&st);
663                 region.length = isc_buffer_usedlength(target) -
664                                 isc_buffer_usedlength(&st);
665                 dns_rdata_fromregion(rdata, rdclass, type, &region);
666         }
667         if (result != ISC_R_SUCCESS) {
668                 *target = st;
669         }
670         return (result);
671 }
672
673 static isc_result_t
674 unknown_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
675                isc_buffer_t *target)
676 {
677         isc_result_t result;
678         char buf[sizeof("65535")];
679         isc_region_t sr;
680
681         strlcpy(buf, "\\# ", sizeof(buf));
682         result = str_totext(buf, target);
683         if (result != ISC_R_SUCCESS)
684                 return (result);
685
686         dns_rdata_toregion(rdata, &sr);
687         INSIST(sr.length < 65536);
688         snprintf(buf, sizeof(buf), "%u", sr.length);
689         result = str_totext(buf, target);
690         if (result != ISC_R_SUCCESS)
691                 return (result);
692
693         if (sr.length != 0U) {
694                 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
695                         result = str_totext(" ( ", target);
696                 else
697                         result = str_totext(" ", target);
698
699                 if (result != ISC_R_SUCCESS)
700                         return (result);
701
702                 if (tctx->width == 0) /* No splitting */
703                         result = isc_hex_totext(&sr, 0, "", target);
704                 else
705                         result = isc_hex_totext(&sr, tctx->width - 2,
706                                                 tctx->linebreak,
707                                                 target);
708                 if (result == ISC_R_SUCCESS &&
709                     (tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
710                         result = str_totext(" )", target);
711         }
712         return (result);
713 }
714
715 static isc_result_t
716 rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
717              isc_buffer_t *target)
718 {
719         isc_result_t result = ISC_R_NOTIMPLEMENTED;
720         isc_boolean_t use_default = ISC_FALSE;
721
722         REQUIRE(rdata != NULL);
723         REQUIRE(tctx->origin == NULL ||
724                 dns_name_isabsolute(tctx->origin) == ISC_TRUE);
725
726         /*
727          * Some DynDNS meta-RRs have empty rdata.
728          */
729         if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
730                 INSIST(rdata->length == 0);
731                 return (ISC_R_SUCCESS);
732         }
733
734         TOTEXTSWITCH
735
736         if (use_default)
737                 result = unknown_totext(rdata, tctx, target);
738
739         return (result);
740 }
741
742 isc_result_t
743 dns_rdata_totext(dns_rdata_t *rdata, dns_name_t *origin, isc_buffer_t *target)
744 {
745         dns_rdata_textctx_t tctx;
746
747         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
748
749         /*
750          * Set up formatting options for single-line output.
751          */
752         tctx.origin = origin;
753         tctx.flags = 0;
754         tctx.width = 60;
755         tctx.linebreak = " ";
756         return (rdata_totext(rdata, &tctx, target));
757 }
758
759 isc_result_t
760 dns_rdata_tofmttext(dns_rdata_t *rdata, dns_name_t *origin,
761                     unsigned int flags, unsigned int width,
762                     const char *linebreak, isc_buffer_t *target)
763 {
764         dns_rdata_textctx_t tctx;
765
766         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
767
768         /*
769          * Set up formatting options for formatted output.
770          */
771         tctx.origin = origin;
772         tctx.flags = flags;
773         if ((flags & DNS_STYLEFLAG_MULTILINE) != 0) {
774                 tctx.width = width;
775                 tctx.linebreak = linebreak;
776         } else {
777                 tctx.width = 60; /* Used for hex word length only. */
778                 tctx.linebreak = " ";
779         }
780         return (rdata_totext(rdata, &tctx, target));
781 }
782
783 isc_result_t
784 dns_rdata_fromstruct(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
785                      dns_rdatatype_t type, void *source,
786                      isc_buffer_t *target)
787 {
788         isc_result_t result = ISC_R_NOTIMPLEMENTED;
789         isc_buffer_t st;
790         isc_region_t region;
791         isc_boolean_t use_default = ISC_FALSE;
792
793         REQUIRE(source != NULL);
794         if (rdata != NULL) {
795                 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
796                 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
797         }
798
799         st = *target;
800
801         FROMSTRUCTSWITCH
802
803         if (use_default)
804                 (void)NULL;
805
806         if (rdata != NULL && result == ISC_R_SUCCESS) {
807                 region.base = isc_buffer_used(&st);
808                 region.length = isc_buffer_usedlength(target) -
809                                 isc_buffer_usedlength(&st);
810                 dns_rdata_fromregion(rdata, rdclass, type, &region);
811         }
812         if (result != ISC_R_SUCCESS)
813                 *target = st;
814         return (result);
815 }
816
817 isc_result_t
818 dns_rdata_tostruct(dns_rdata_t *rdata, void *target, isc_mem_t *mctx) {
819         isc_result_t result = ISC_R_NOTIMPLEMENTED;
820         isc_boolean_t use_default = ISC_FALSE;
821
822         REQUIRE(rdata != NULL);
823         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
824
825         TOSTRUCTSWITCH
826
827         if (use_default)
828                 (void)NULL;
829
830         return (result);
831 }
832
833 void
834 dns_rdata_freestruct(void *source) {
835         dns_rdatacommon_t *common = source;
836         REQUIRE(source != NULL);
837
838         FREESTRUCTSWITCH
839 }
840
841 isc_result_t
842 dns_rdata_additionaldata(dns_rdata_t *rdata, dns_additionaldatafunc_t add,
843                          void *arg)
844 {
845         isc_result_t result = ISC_R_NOTIMPLEMENTED;
846         isc_boolean_t use_default = ISC_FALSE;
847
848         /*
849          * Call 'add' for each name and type from 'rdata' which is subject to
850          * additional section processing.
851          */
852
853         REQUIRE(rdata != NULL);
854         REQUIRE(add != NULL);
855         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
856
857         ADDITIONALDATASWITCH
858
859         /* No additional processing for unknown types */
860         if (use_default)
861                 result = ISC_R_SUCCESS;
862
863         return (result);
864 }
865
866 isc_result_t
867 dns_rdata_digest(dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg) {
868         isc_result_t result = ISC_R_NOTIMPLEMENTED;
869         isc_boolean_t use_default = ISC_FALSE;
870         isc_region_t r;
871
872         /*
873          * Send 'rdata' in DNSSEC canonical form to 'digest'.
874          */
875
876         REQUIRE(rdata != NULL);
877         REQUIRE(digest != NULL);
878         REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
879
880         DIGESTSWITCH
881
882         if (use_default) {
883                 dns_rdata_toregion(rdata, &r);
884                 result = (digest)(arg, &r);
885         }
886
887         return (result);
888 }
889
890 isc_boolean_t
891 dns_rdata_checkowner(dns_name_t *name, dns_rdataclass_t rdclass,
892                      dns_rdatatype_t type, isc_boolean_t wildcard)
893 {
894         isc_boolean_t result;
895
896         CHECKOWNERSWITCH
897         return (result);
898 }
899
900 isc_boolean_t
901 dns_rdata_checknames(dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad)
902 {
903         isc_boolean_t result;
904
905         CHECKNAMESSWITCH
906         return (result);
907 }
908
909 unsigned int
910 dns_rdatatype_attributes(dns_rdatatype_t type)
911 {
912         RDATATYPE_ATTRIBUTE_SW
913         if (type >= (dns_rdatatype_t)128 && type < (dns_rdatatype_t)255)
914                 return (DNS_RDATATYPEATTR_UNKNOWN | DNS_RDATATYPEATTR_META);
915         return (DNS_RDATATYPEATTR_UNKNOWN);
916 }
917
918 isc_result_t
919 dns_rdatatype_fromtext(dns_rdatatype_t *typep, isc_textregion_t *source) {
920         unsigned int hash;
921         unsigned int n;
922         unsigned char a, b;
923
924         n = source->length;
925
926         if (n == 0)
927                 return (DNS_R_UNKNOWN);
928
929         a = tolower((unsigned char)source->base[0]);
930         b = tolower((unsigned char)source->base[n - 1]);
931
932         hash = ((a + n) * b) % 256;
933
934         /*
935          * This switch block is inlined via \#define, and will use "return"
936          * to return a result to the caller if it is a valid (known)
937          * rdatatype name.
938          */
939         RDATATYPE_FROMTEXT_SW(hash, source->base, n, typep);
940
941         if (source->length > 4 && source->length < (4 + sizeof("65000")) &&
942             strncasecmp("type", source->base, 4) == 0) {
943                 char buf[sizeof("65000")];
944                 char *endp;
945                 unsigned int val;
946
947                 strncpy(buf, source->base + 4, source->length - 4);
948                 buf[source->length - 4] = '\0';
949                 val = strtoul(buf, &endp, 10);
950                 if (*endp == '\0' && val <= 0xffff) {
951                         *typep = (dns_rdatatype_t)val;
952                         return (ISC_R_SUCCESS);
953                 }
954         }
955
956         return (DNS_R_UNKNOWN);
957 }
958
959 isc_result_t
960 dns_rdatatype_totext(dns_rdatatype_t type, isc_buffer_t *target) {
961         char buf[sizeof("TYPE65535")];
962
963         RDATATYPE_TOTEXT_SW
964         snprintf(buf, sizeof(buf), "TYPE%u", type);
965         return (str_totext(buf, target));
966 }
967
968 void
969 dns_rdatatype_format(dns_rdatatype_t rdtype,
970                      char *array, unsigned int size)
971 {
972         isc_result_t result;
973         isc_buffer_t buf;
974
975         if (size == 0U)
976                 return;
977
978         isc_buffer_init(&buf, array, size);
979         result = dns_rdatatype_totext(rdtype, &buf);
980         /*
981          * Null terminate.
982          */
983         if (result == ISC_R_SUCCESS) {
984                 if (isc_buffer_availablelength(&buf) >= 1)
985                         isc_buffer_putuint8(&buf, 0);
986                 else
987                         result = ISC_R_NOSPACE;
988         }
989         if (result != ISC_R_SUCCESS)
990                 strlcpy(array, "<unknown>", size);
991 }
992
993 /*
994  * Private function.
995  */
996
997 static unsigned int
998 name_length(dns_name_t *name) {
999         return (name->length);
1000 }
1001
1002 static isc_result_t
1003 txt_totext(isc_region_t *source, isc_buffer_t *target) {
1004         unsigned int tl;
1005         unsigned int n;
1006         unsigned char *sp;
1007         char *tp;
1008         isc_region_t region;
1009
1010         isc_buffer_availableregion(target, &region);
1011         sp = source->base;
1012         tp = (char *)region.base;
1013         tl = region.length;
1014
1015         n = *sp++;
1016
1017         REQUIRE(n + 1 <= source->length);
1018
1019         if (tl < 1)
1020                 return (ISC_R_NOSPACE);
1021         *tp++ = '"';
1022         tl--;
1023         while (n--) {
1024                 if (*sp < 0x20 || *sp >= 0x7f) {
1025                         if (tl < 4)
1026                                 return (ISC_R_NOSPACE);
1027                         *tp++ = 0x5c;
1028                         *tp++ = 0x30 + ((*sp / 100) % 10);
1029                         *tp++ = 0x30 + ((*sp / 10) % 10);
1030                         *tp++ = 0x30 + (*sp % 10);
1031                         sp++;
1032                         tl -= 4;
1033                         continue;
1034                 }
1035                 /* double quote, semi-colon, backslash */
1036                 if (*sp == 0x22 || *sp == 0x3b || *sp == 0x5c) {
1037                         if (tl < 2)
1038                                 return (ISC_R_NOSPACE);
1039                         *tp++ = '\\';
1040                         tl--;
1041                 }
1042                 if (tl < 1)
1043                         return (ISC_R_NOSPACE);
1044                 *tp++ = *sp++;
1045                 tl--;
1046         }
1047         if (tl < 1)
1048                 return (ISC_R_NOSPACE);
1049         *tp++ = '"';
1050         tl--;
1051         isc_buffer_add(target, tp - (char *)region.base);
1052         isc_region_consume(source, *source->base + 1);
1053         return (ISC_R_SUCCESS);
1054 }
1055
1056 static isc_result_t
1057 txt_fromtext(isc_textregion_t *source, isc_buffer_t *target) {
1058         isc_region_t tregion;
1059         isc_boolean_t escape;
1060         unsigned int n, nrem;
1061         char *s;
1062         unsigned char *t;
1063         int d;
1064         int c;
1065
1066         isc_buffer_availableregion(target, &tregion);
1067         s = source->base;
1068         n = source->length;
1069         t = tregion.base;
1070         nrem = tregion.length;
1071         escape = ISC_FALSE;
1072         if (nrem < 1)
1073                 return (ISC_R_NOSPACE);
1074         /*
1075          * Length byte.
1076          */
1077         nrem--;
1078         t++;
1079         /*
1080          * Maximum text string length.
1081          */
1082         if (nrem > 255)
1083                 nrem = 255;
1084         while (n-- != 0) {
1085                 c = (*s++) & 0xff;
1086                 if (escape && (d = decvalue((char)c)) != -1) {
1087                         c = d;
1088                         if (n == 0)
1089                                 return (DNS_R_SYNTAX);
1090                         n--;
1091                         if ((d = decvalue(*s++)) != -1)
1092                                 c = c * 10 + d;
1093                         else
1094                                 return (DNS_R_SYNTAX);
1095                         if (n == 0)
1096                                 return (DNS_R_SYNTAX);
1097                         n--;
1098                         if ((d = decvalue(*s++)) != -1)
1099                                 c = c * 10 + d;
1100                         else
1101                                 return (DNS_R_SYNTAX);
1102                         if (c > 255)
1103                                 return (DNS_R_SYNTAX);
1104                 } else if (!escape && c == '\\') {
1105                         escape = ISC_TRUE;
1106                         continue;
1107                 }
1108                 escape = ISC_FALSE;
1109                 if (nrem == 0)
1110                         return ((tregion.length <= 256U) ?
1111                                 ISC_R_NOSPACE : DNS_R_SYNTAX);
1112                 *t++ = c;
1113                 nrem--;
1114         }
1115         if (escape)
1116                 return (DNS_R_SYNTAX);
1117         *tregion.base = t - tregion.base - 1;
1118         isc_buffer_add(target, *tregion.base + 1);
1119         return (ISC_R_SUCCESS);
1120 }
1121
1122 static isc_result_t
1123 txt_fromwire(isc_buffer_t *source, isc_buffer_t *target) {
1124         unsigned int n;
1125         isc_region_t sregion;
1126         isc_region_t tregion;
1127
1128         isc_buffer_activeregion(source, &sregion);
1129         if (sregion.length == 0)
1130                 return(ISC_R_UNEXPECTEDEND);
1131         n = *sregion.base + 1;
1132         if (n > sregion.length)
1133                 return (ISC_R_UNEXPECTEDEND);
1134
1135         isc_buffer_availableregion(target, &tregion);
1136         if (n > tregion.length)
1137                 return (ISC_R_NOSPACE);
1138
1139         memcpy(tregion.base, sregion.base, n);
1140         isc_buffer_forward(source, n);
1141         isc_buffer_add(target, n);
1142         return (ISC_R_SUCCESS);
1143 }
1144
1145 static isc_boolean_t
1146 name_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target) {
1147         int l1, l2;
1148
1149         if (origin == NULL)
1150                 goto return_false;
1151
1152         if (dns_name_compare(origin, dns_rootname) == 0)
1153                 goto return_false;
1154
1155         if (!dns_name_issubdomain(name, origin))
1156                 goto return_false;
1157
1158         l1 = dns_name_countlabels(name);
1159         l2 = dns_name_countlabels(origin);
1160
1161         if (l1 == l2)
1162                 goto return_false;
1163
1164         /* Master files should be case preserving. */
1165         dns_name_getlabelsequence(name, l1 - l2, l2, target);
1166         if (!dns_name_caseequal(origin, target))
1167                 goto return_false;
1168
1169         dns_name_getlabelsequence(name, 0, l1 - l2, target);
1170         return (ISC_TRUE);
1171
1172 return_false:
1173         *target = *name;
1174         return (ISC_FALSE);
1175 }
1176
1177 static isc_result_t
1178 str_totext(const char *source, isc_buffer_t *target) {
1179         unsigned int l;
1180         isc_region_t region;
1181
1182         isc_buffer_availableregion(target, &region);
1183         l = strlen(source);
1184
1185         if (l > region.length)
1186                 return (ISC_R_NOSPACE);
1187
1188         memcpy(region.base, source, l);
1189         isc_buffer_add(target, l);
1190         return (ISC_R_SUCCESS);
1191 }
1192
1193 static isc_result_t
1194 inet_totext(int af, isc_region_t *src, isc_buffer_t *target) {
1195         char tmpbuf[64];
1196
1197         /* Note - inet_ntop doesn't do size checking on its input. */
1198         if (inet_ntop(af, src->base, tmpbuf, sizeof(tmpbuf)) == NULL)
1199                 return (ISC_R_NOSPACE);
1200         if (strlen(tmpbuf) > isc_buffer_availablelength(target))
1201                 return (ISC_R_NOSPACE);
1202         isc_buffer_putstr(target, tmpbuf);
1203         return (ISC_R_SUCCESS);
1204 }
1205
1206 static isc_boolean_t
1207 buffer_empty(isc_buffer_t *source) {
1208         return((source->current == source->active) ? ISC_TRUE : ISC_FALSE);
1209 }
1210
1211 static void
1212 buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region) {
1213         isc_buffer_init(buffer, region->base, region->length);
1214         isc_buffer_add(buffer, region->length);
1215         isc_buffer_setactive(buffer, region->length);
1216 }
1217
1218 static isc_result_t
1219 uint32_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1220         isc_region_t region;
1221
1222         isc_buffer_availableregion(target, &region);
1223         if (region.length < 4)
1224                 return (ISC_R_NOSPACE);
1225         isc_buffer_putuint32(target, value);
1226         return (ISC_R_SUCCESS);
1227 }
1228
1229 static isc_result_t
1230 uint16_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1231         isc_region_t region;
1232
1233         if (value > 0xffff)
1234                 return (ISC_R_RANGE);
1235         isc_buffer_availableregion(target, &region);
1236         if (region.length < 2)
1237                 return (ISC_R_NOSPACE);
1238         isc_buffer_putuint16(target, (isc_uint16_t)value);
1239         return (ISC_R_SUCCESS);
1240 }
1241
1242 static isc_result_t
1243 uint8_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1244         isc_region_t region;
1245
1246         if (value > 0xff)
1247                 return (ISC_R_RANGE);
1248         isc_buffer_availableregion(target, &region);
1249         if (region.length < 1)
1250                 return (ISC_R_NOSPACE);
1251         isc_buffer_putuint8(target, (isc_uint8_t)value);
1252         return (ISC_R_SUCCESS);
1253 }
1254
1255 static isc_result_t
1256 name_tobuffer(dns_name_t *name, isc_buffer_t *target) {
1257         isc_region_t r;
1258         dns_name_toregion(name, &r);
1259         return (isc_buffer_copyregion(target, &r));
1260 }
1261
1262 static isc_uint32_t
1263 uint32_fromregion(isc_region_t *region) {
1264         isc_uint32_t value;
1265
1266         REQUIRE(region->length >= 4);
1267         value = region->base[0] << 24;
1268         value |= region->base[1] << 16;
1269         value |= region->base[2] << 8;
1270         value |= region->base[3];
1271         return(value);
1272 }
1273
1274 static isc_uint16_t
1275 uint16_consume_fromregion(isc_region_t *region) {
1276         isc_uint16_t r = uint16_fromregion(region);
1277
1278         isc_region_consume(region, 2);
1279         return r;
1280 }
1281
1282 static isc_uint16_t
1283 uint16_fromregion(isc_region_t *region) {
1284
1285         REQUIRE(region->length >= 2);
1286
1287         return ((region->base[0] << 8) | region->base[1]);
1288 }
1289
1290 static isc_uint8_t
1291 uint8_fromregion(isc_region_t *region) {
1292
1293         REQUIRE(region->length >= 1);
1294
1295         return (region->base[0]);
1296 }
1297
1298 static isc_uint8_t
1299 uint8_consume_fromregion(isc_region_t *region) {
1300         isc_uint8_t r = uint8_fromregion(region);
1301
1302         isc_region_consume(region, 1);
1303         return r;
1304 }
1305
1306 static isc_result_t
1307 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
1308         isc_region_t tr;
1309
1310         isc_buffer_availableregion(target, &tr);
1311         if (length > tr.length)
1312                 return (ISC_R_NOSPACE);
1313         memcpy(tr.base, base, length);
1314         isc_buffer_add(target, length);
1315         return (ISC_R_SUCCESS);
1316 }
1317
1318 static int
1319 hexvalue(char value) {
1320         char *s;
1321         unsigned char c;
1322
1323         c = (unsigned char)value;
1324
1325         if (!isascii(c))
1326                 return (-1);
1327         if (isupper(c))
1328                 c = tolower(c);
1329         if ((s = strchr(hexdigits, c)) == NULL)
1330                 return (-1);
1331         return (s - hexdigits);
1332 }
1333
1334 static int
1335 decvalue(char value) {
1336         char *s;
1337
1338         /*
1339          * isascii() is valid for full range of int values, no need to
1340          * mask or cast.
1341          */
1342         if (!isascii(value))
1343                 return (-1);
1344         if ((s = strchr(decdigits, value)) == NULL)
1345                 return (-1);
1346         return (s - decdigits);
1347 }
1348
1349 static const char atob_digits[86] =
1350         "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" \
1351         "abcdefghijklmnopqrstu";
1352 /*
1353  * Subroutines to convert between 8 bit binary bytes and printable ASCII.
1354  * Computes the number of bytes, and three kinds of simple checksums.
1355  * Incoming bytes are collected into 32-bit words, then printed in base 85:
1356  *      exp(85,5) > exp(2,32)
1357  * The ASCII characters used are between '!' and 'u';
1358  * 'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.
1359  *
1360  * Originally by Paul Rutter (philabs!per) and Joe Orost (petsd!joe) for
1361  * the atob/btoa programs, released with the compress program, in mod.sources.
1362  * Modified by Mike Schwartz 8/19/86 for use in BIND.
1363  * Modified to be re-entrant 3/2/99.
1364  */
1365
1366
1367 struct state {
1368         isc_int32_t Ceor;
1369         isc_int32_t Csum;
1370         isc_int32_t Crot;
1371         isc_int32_t word;
1372         isc_int32_t bcount;
1373 };
1374
1375 #define Ceor state->Ceor
1376 #define Csum state->Csum
1377 #define Crot state->Crot
1378 #define word state->word
1379 #define bcount state->bcount
1380
1381 #define times85(x)      ((((((x<<2)+x)<<2)+x)<<2)+x)
1382
1383 static isc_result_t     byte_atob(int c, isc_buffer_t *target,
1384                                   struct state *state);
1385 static isc_result_t     putbyte(int c, isc_buffer_t *, struct state *state);
1386 static isc_result_t     byte_btoa(int c, isc_buffer_t *, struct state *state);
1387
1388 /*
1389  * Decode ASCII-encoded byte c into binary representation and
1390  * place into *bufp, advancing bufp.
1391  */
1392 static isc_result_t
1393 byte_atob(int c, isc_buffer_t *target, struct state *state) {
1394         char *s;
1395         if (c == 'z') {
1396                 if (bcount != 0)
1397                         return(DNS_R_SYNTAX);
1398                 else {
1399                         RETERR(putbyte(0, target, state));
1400                         RETERR(putbyte(0, target, state));
1401                         RETERR(putbyte(0, target, state));
1402                         RETERR(putbyte(0, target, state));
1403                 }
1404         } else if ((s = strchr(atob_digits, c)) != NULL) {
1405                 if (bcount == 0) {
1406                         word = s - atob_digits;
1407                         ++bcount;
1408                 } else if (bcount < 4) {
1409                         word = times85(word);
1410                         word += s - atob_digits;
1411                         ++bcount;
1412                 } else {
1413                         word = times85(word);
1414                         word += s - atob_digits;
1415                         RETERR(putbyte((word >> 24) & 0xff, target, state));
1416                         RETERR(putbyte((word >> 16) & 0xff, target, state));
1417                         RETERR(putbyte((word >> 8) & 0xff, target, state));
1418                         RETERR(putbyte(word & 0xff, target, state));
1419                         word = 0;
1420                         bcount = 0;
1421                 }
1422         } else
1423                 return(DNS_R_SYNTAX);
1424         return(ISC_R_SUCCESS);
1425 }
1426
1427 /*
1428  * Compute checksum info and place c into target.
1429  */
1430 static isc_result_t
1431 putbyte(int c, isc_buffer_t *target, struct state *state) {
1432         isc_region_t tr;
1433
1434         Ceor ^= c;
1435         Csum += c;
1436         Csum += 1;
1437         if ((Crot & 0x80000000)) {
1438                 Crot <<= 1;
1439                 Crot += 1;
1440         } else {
1441                 Crot <<= 1;
1442         }
1443         Crot += c;
1444         isc_buffer_availableregion(target, &tr);
1445         if (tr.length < 1)
1446                 return (ISC_R_NOSPACE);
1447         tr.base[0] = c;
1448         isc_buffer_add(target, 1);
1449         return (ISC_R_SUCCESS);
1450 }
1451
1452 /*
1453  * Read the ASCII-encoded data from inbuf, of length inbuflen, and convert
1454  * it into T_UNSPEC (binary data) in outbuf, not to exceed outbuflen bytes;
1455  * outbuflen must be divisible by 4.  (Note: this is because outbuf is filled
1456  * in 4 bytes at a time.  If the actual data doesn't end on an even 4-byte
1457  * boundary, there will be no problem...it will be padded with 0 bytes, and
1458  * numbytes will indicate the correct number of bytes.  The main point is
1459  * that since the buffer is filled in 4 bytes at a time, even if there is
1460  * not a full 4 bytes of data at the end, there has to be room to 0-pad the
1461  * data, so the buffer must be of size divisible by 4).  Place the number of
1462  * output bytes in numbytes, and return a failure/success status.
1463  */
1464
1465 static isc_result_t
1466 atob_tobuffer(isc_lex_t *lexer, isc_buffer_t *target) {
1467         long oeor, osum, orot;
1468         struct state statebuf, *state= &statebuf;
1469         isc_token_t token;
1470         char c;
1471         char *e;
1472
1473         Ceor = Csum = Crot = word = bcount = 0;
1474
1475         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1476                                       ISC_FALSE));
1477         while (token.value.as_textregion.length != 0) {
1478                 if ((c = token.value.as_textregion.base[0]) == 'x') {
1479                         break;
1480                 } else
1481                         RETERR(byte_atob(c, target, state));
1482                 isc_textregion_consume(&token.value.as_textregion, 1);
1483         }
1484
1485         /*
1486          * Number of bytes.
1487          */
1488         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
1489                                       ISC_FALSE));
1490         if ((token.value.as_ulong % 4) != 0U)
1491                 isc_buffer_subtract(target,  4 - (token.value.as_ulong % 4));
1492
1493         /*
1494          * Checksum.
1495          */
1496         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1497                                       ISC_FALSE));
1498         oeor = strtol(DNS_AS_STR(token), &e, 16);
1499         if (*e != 0)
1500                 return (DNS_R_SYNTAX);
1501
1502         /*
1503          * Checksum.
1504          */
1505         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1506                                       ISC_FALSE));
1507         osum = strtol(DNS_AS_STR(token), &e, 16);
1508         if (*e != 0)
1509                 return (DNS_R_SYNTAX);
1510
1511         /*
1512          * Checksum.
1513          */
1514         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1515                                       ISC_FALSE));
1516         orot = strtol(DNS_AS_STR(token), &e, 16);
1517         if (*e != 0)
1518                 return (DNS_R_SYNTAX);
1519
1520         if ((oeor != Ceor) || (osum != Csum) || (orot != Crot))
1521                 return(DNS_R_BADCKSUM);
1522         return (ISC_R_SUCCESS);
1523 }
1524
1525 /*
1526  * Encode binary byte c into ASCII representation and place into *bufp,
1527  * advancing bufp.
1528  */
1529 static isc_result_t
1530 byte_btoa(int c, isc_buffer_t *target, struct state *state) {
1531         isc_region_t tr;
1532
1533         isc_buffer_availableregion(target, &tr);
1534         Ceor ^= c;
1535         Csum += c;
1536         Csum += 1;
1537         if ((Crot & 0x80000000)) {
1538                 Crot <<= 1;
1539                 Crot += 1;
1540         } else {
1541                 Crot <<= 1;
1542         }
1543         Crot += c;
1544
1545         word <<= 8;
1546         word |= c;
1547         if (bcount == 3) {
1548                 if (word == 0) {
1549                         if (tr.length < 1)
1550                                 return (ISC_R_NOSPACE);
1551                         tr.base[0] = 'z';
1552                         isc_buffer_add(target, 1);
1553                 } else {
1554                     register int tmp = 0;
1555                     register isc_int32_t tmpword = word;
1556
1557                     if (tmpword < 0) {
1558                            /*
1559                             * Because some don't support u_long.
1560                             */
1561                         tmp = 32;
1562                         tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
1563                     }
1564                     if (tmpword < 0) {
1565                         tmp = 64;
1566                         tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
1567                     }
1568                         if (tr.length < 5)
1569                                 return (ISC_R_NOSPACE);
1570                         tr.base[0] = atob_digits[(tmpword /
1571                                               (isc_int32_t)(85 * 85 * 85 * 85))
1572                                                 + tmp];
1573                         tmpword %= (isc_int32_t)(85 * 85 * 85 * 85);
1574                         tr.base[1] = atob_digits[tmpword / (85 * 85 * 85)];
1575                         tmpword %= (85 * 85 * 85);
1576                         tr.base[2] = atob_digits[tmpword / (85 * 85)];
1577                         tmpword %= (85 * 85);
1578                         tr.base[3] = atob_digits[tmpword / 85];
1579                         tmpword %= 85;
1580                         tr.base[4] = atob_digits[tmpword];
1581                         isc_buffer_add(target, 5);
1582                 }
1583                 bcount = 0;
1584         } else {
1585                 bcount += 1;
1586         }
1587         return (ISC_R_SUCCESS);
1588 }
1589
1590
1591 /*
1592  * Encode the binary data from inbuf, of length inbuflen, into a
1593  * target.  Return success/failure status
1594  */
1595 static isc_result_t
1596 btoa_totext(unsigned char *inbuf, int inbuflen, isc_buffer_t *target) {
1597         int inc;
1598         struct state statebuf, *state = &statebuf;
1599         char buf[sizeof("x 2000000000 ffffffff ffffffff ffffffff")];
1600
1601         Ceor = Csum = Crot = word = bcount = 0;
1602         for (inc = 0; inc < inbuflen; inbuf++, inc++)
1603                 RETERR(byte_btoa(*inbuf, target, state));
1604
1605         while (bcount != 0)
1606                 RETERR(byte_btoa(0, target, state));
1607
1608         /*
1609          * Put byte count and checksum information at end of buffer,
1610          * delimited by 'x'
1611          */
1612         snprintf(buf, sizeof(buf), "x %d %x %x %x", inbuflen, Ceor, Csum, Crot);
1613         return (str_totext(buf, target));
1614 }
1615
1616
1617 static void
1618 default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *fmt,
1619                           ...)
1620 {
1621         va_list ap;
1622
1623         UNUSED(callbacks);
1624
1625         va_start(ap, fmt);
1626         vfprintf(stderr, fmt, ap);
1627         va_end(ap);
1628         fprintf(stderr, "\n");
1629 }
1630
1631 static void
1632 fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks) {
1633         if (isc_lex_isfile(lexer) && callbacks != NULL) {
1634                 const char *name = isc_lex_getsourcename(lexer);
1635                 if (name == NULL)
1636                         name = "UNKNOWN";
1637                 (*callbacks->warn)(callbacks,
1638                                    "%s:%lu: file does not end with newline",
1639                                    name, isc_lex_getsourceline(lexer));
1640         }
1641 }
1642
1643 static void
1644 warn_badmx(isc_token_t *token, isc_lex_t *lexer,
1645            dns_rdatacallbacks_t *callbacks)
1646 {
1647         const char *file;
1648         unsigned long line;
1649
1650         if (lexer != NULL) {
1651                 file = isc_lex_getsourcename(lexer);
1652                 line = isc_lex_getsourceline(lexer);
1653                 (*callbacks->warn)(callbacks, "%s:%u: warning: '%s': %s",
1654                                    file, line, DNS_AS_STR(*token),
1655                                    dns_result_totext(DNS_R_MXISADDRESS));
1656         }
1657 }
1658
1659 static void
1660 warn_badname(dns_name_t *name, isc_lex_t *lexer,
1661              dns_rdatacallbacks_t *callbacks)
1662 {
1663         const char *file;
1664         unsigned long line;
1665         char namebuf[DNS_NAME_FORMATSIZE];
1666
1667         if (lexer != NULL) {
1668                 file = isc_lex_getsourcename(lexer);
1669                 line = isc_lex_getsourceline(lexer);
1670                 dns_name_format(name, namebuf, sizeof(namebuf));
1671                 (*callbacks->warn)(callbacks, "%s:%u: warning: %s: %s",
1672                                    file, line, namebuf,
1673                                    dns_result_totext(DNS_R_BADNAME));
1674         }
1675 }
1676
1677 static void
1678 fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
1679                dns_rdatacallbacks_t *callbacks, const char *name,
1680                unsigned long line, isc_token_t *token, isc_result_t result)
1681 {
1682         if (name == NULL)
1683                 name = "UNKNOWN";
1684
1685         if (token != NULL) {
1686                 switch (token->type) {
1687                 case isc_tokentype_eol:
1688                         (*callback)(callbacks, "%s: %s:%lu: near eol: %s",
1689                                     "dns_rdata_fromtext", name, line,
1690                                     dns_result_totext(result));
1691                         break;
1692                 case isc_tokentype_eof:
1693                         (*callback)(callbacks, "%s: %s:%lu: near eof: %s",
1694                                     "dns_rdata_fromtext", name, line,
1695                                     dns_result_totext(result));
1696                         break;
1697                 case isc_tokentype_number:
1698                         (*callback)(callbacks, "%s: %s:%lu: near %lu: %s",
1699                                     "dns_rdata_fromtext", name, line,
1700                                     token->value.as_ulong,
1701                                     dns_result_totext(result));
1702                         break;
1703                 case isc_tokentype_string:
1704                 case isc_tokentype_qstring:
1705                         (*callback)(callbacks, "%s: %s:%lu: near '%s': %s",
1706                                     "dns_rdata_fromtext", name, line,
1707                                     DNS_AS_STR(*token),
1708                                     dns_result_totext(result));
1709                         break;
1710                 default:
1711                         (*callback)(callbacks, "%s: %s:%lu: %s",
1712                                     "dns_rdata_fromtext", name, line,
1713                                     dns_result_totext(result));
1714                         break;
1715                 }
1716         } else {
1717                 (*callback)(callbacks, "dns_rdata_fromtext: %s:%lu: %s",
1718                             name, line, dns_result_totext(result));
1719         }
1720 }
1721
1722 dns_rdatatype_t
1723 dns_rdata_covers(dns_rdata_t *rdata) {
1724         if (rdata->type == 46)
1725                 return (covers_rrsig(rdata));
1726         return (covers_sig(rdata));
1727 }
1728
1729 isc_boolean_t
1730 dns_rdatatype_ismeta(dns_rdatatype_t type) {
1731         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_META) != 0)
1732                 return (ISC_TRUE);
1733         return (ISC_FALSE);
1734 }
1735
1736 isc_boolean_t
1737 dns_rdatatype_issingleton(dns_rdatatype_t type) {
1738         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_SINGLETON)
1739             != 0)
1740                 return (ISC_TRUE);
1741         return (ISC_FALSE);
1742 }
1743
1744 isc_boolean_t
1745 dns_rdatatype_notquestion(dns_rdatatype_t type) {
1746         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_NOTQUESTION)
1747             != 0)
1748                 return (ISC_TRUE);
1749         return (ISC_FALSE);
1750 }
1751
1752 isc_boolean_t
1753 dns_rdatatype_questiononly(dns_rdatatype_t type) {
1754         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_QUESTIONONLY)
1755             != 0)
1756                 return (ISC_TRUE);
1757         return (ISC_FALSE);
1758 }
1759
1760 isc_boolean_t
1761 dns_rdatatype_atparent(dns_rdatatype_t type) {
1762         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_ATPARENT) != 0)
1763                 return (ISC_TRUE);
1764         return (ISC_FALSE);
1765 }
1766
1767 isc_boolean_t
1768 dns_rdataclass_ismeta(dns_rdataclass_t rdclass) {
1769
1770         if (rdclass == dns_rdataclass_reserved0
1771             || rdclass == dns_rdataclass_none
1772             || rdclass == dns_rdataclass_any)
1773                 return (ISC_TRUE);
1774
1775         return (ISC_FALSE);  /* Assume it is not a meta class. */
1776 }
1777
1778 isc_boolean_t
1779 dns_rdatatype_isdnssec(dns_rdatatype_t type) {
1780         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_DNSSEC) != 0)
1781                 return (ISC_TRUE);
1782         return (ISC_FALSE);
1783 }
1784
1785 isc_boolean_t
1786 dns_rdatatype_iszonecutauth(dns_rdatatype_t type) {
1787         if ((dns_rdatatype_attributes(type)
1788              & (DNS_RDATATYPEATTR_DNSSEC | DNS_RDATATYPEATTR_ZONECUTAUTH))
1789             != 0)
1790                 return (ISC_TRUE);
1791         return (ISC_FALSE);
1792 }
1793
1794 isc_boolean_t
1795 dns_rdatatype_isknown(dns_rdatatype_t type) {
1796         if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_UNKNOWN)
1797             == 0)
1798                 return (ISC_TRUE);
1799         return (ISC_FALSE);
1800 }