xref: /freebsd/contrib/ldns/rr.c (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1 /* rr.c
2  *
3  * access functions for ldns_rr -
4  * a Net::DNS like library for C
5  * LibDNS Team @ NLnet Labs
6  *
7  * (c) NLnet Labs, 2004-2006
8  * See the file LICENSE for the license
9  */
10 #include <ldns/config.h>
11 
12 #include <ldns/ldns.h>
13 #include <ldns/internal.h>
14 
15 #include <strings.h>
16 #include <limits.h>
17 
18 #include <errno.h>
19 
20 #define LDNS_SYNTAX_DATALEN 16
21 #define LDNS_TTL_DATALEN    21
22 #define LDNS_RRLIST_INIT    8
23 
24 #define _IS_WHITESPACE(chr) \
25     ( NULL != strchr( LDNS_PARSE_NO_NL, chr) )
26 
27 #define _BUFFER_IS_AT_WHITESPACE(rd_buf) \
28     _IS_WHITESPACE(*(ldns_buffer_current(rd_buf)))
29 
30 ldns_rr *
31 ldns_rr_new(void)
32 {
33 	ldns_rr *rr;
34 	rr = LDNS_MALLOC(ldns_rr);
35         if (!rr) {
36                 return NULL;
37 	}
38 
39 	ldns_rr_set_owner(rr, NULL);
40 	ldns_rr_set_question(rr, false);
41 	ldns_rr_set_rd_count(rr, 0);
42 	rr->_rdata_fields = NULL;
43 	ldns_rr_set_class(rr, LDNS_RR_CLASS_IN);
44 	ldns_rr_set_ttl(rr, LDNS_DEFAULT_TTL);
45         return rr;
46 }
47 
48 ldns_rr *
49 ldns_rr_new_frm_type(ldns_rr_type t)
50 {
51 	ldns_rr *rr;
52 	const ldns_rr_descriptor *desc;
53 	size_t i;
54 
55 	rr = LDNS_MALLOC(ldns_rr);
56         if (!rr) {
57                 return NULL;
58 	}
59 
60 	desc = ldns_rr_descript(t);
61 
62 	rr->_rdata_fields = LDNS_XMALLOC(ldns_rdf *, ldns_rr_descriptor_minimum(desc));
63         if(!rr->_rdata_fields) {
64                 LDNS_FREE(rr);
65                 return NULL;
66         }
67 	for (i = 0; i < ldns_rr_descriptor_minimum(desc); i++) {
68 		rr->_rdata_fields[i] = NULL;
69 	}
70 
71 	ldns_rr_set_owner(rr, NULL);
72 	ldns_rr_set_question(rr, false);
73 	/* set the count to minimum */
74 	ldns_rr_set_rd_count(rr, ldns_rr_descriptor_minimum(desc));
75 	ldns_rr_set_class(rr, LDNS_RR_CLASS_IN);
76 	ldns_rr_set_ttl(rr, LDNS_DEFAULT_TTL);
77 	ldns_rr_set_type(rr, t);
78 	return rr;
79 }
80 
81 void
82 ldns_rr_free(ldns_rr *rr)
83 {
84 	size_t i;
85 	if (rr) {
86 		if (ldns_rr_owner(rr)) {
87 			ldns_rdf_deep_free(ldns_rr_owner(rr));
88 		}
89 		for (i = 0; i < ldns_rr_rd_count(rr); i++) {
90 			ldns_rdf_deep_free(ldns_rr_rdf(rr, i));
91 		}
92 		LDNS_FREE(rr->_rdata_fields);
93 		LDNS_FREE(rr);
94 	}
95 }
96 
97 /* Syntactic sugar for ldns_rr_new_frm_str_internal */
98 INLINE bool
99 ldns_rdf_type_maybe_quoted(ldns_rdf_type rdf_type)
100 {
101 	return  rdf_type == LDNS_RDF_TYPE_STR ||
102 		rdf_type == LDNS_RDF_TYPE_LONG_STR;
103 }
104 
105 /*
106  * trailing spaces are allowed
107  * leading spaces are not allowed
108  * allow ttl to be optional
109  * class is optional too
110  * if ttl is missing, and default_ttl is 0, use DEF_TTL
111  * allow ttl to be written as 1d3h
112  * So the RR should look like. e.g.
113  * miek.nl. 3600 IN MX 10 elektron.atoom.net
114  * or
115  * miek.nl. 1h IN MX 10 elektron.atoom.net
116  * or
117  * miek.nl. IN MX 10 elektron.atoom.net
118  */
119 static ldns_status
120 ldns_rr_new_frm_str_internal(ldns_rr **newrr, const char *str,
121                              uint32_t default_ttl, const ldns_rdf *origin,
122                              ldns_rdf **prev, bool question,
123 			     bool *explicit_ttl)
124 {
125 	ldns_rr *new;
126 	const ldns_rr_descriptor *desc;
127 	ldns_rr_type rr_type;
128 	ldns_buffer *rr_buf = NULL;
129 	ldns_buffer *rd_buf = NULL;
130 	uint32_t ttl_val;
131 	char  *owner = NULL;
132 	char  *ttl = NULL;
133 	ldns_rr_class clas_val;
134 	char  *clas = NULL;
135 	char  *type = NULL;
136 	size_t type_sz;
137 	char  *rdata = NULL;
138 	char  *rd = NULL;
139 	char  *xtok = NULL; /* For RDF types with spaces (i.e. extra tokens) */
140 	size_t rd_strlen;
141 	const char *delimiters;
142 	ssize_t c;
143 	ldns_rdf *owner_dname;
144         const char* endptr;
145         int was_unknown_rr_format = 0;
146 	ldns_status status = LDNS_STATUS_OK;
147 
148 	/* used for types with unknown number of rdatas */
149 	bool done;
150 	bool quoted;
151 
152 	ldns_rdf *r = NULL;
153 	uint16_t r_cnt;
154 	uint16_t r_min;
155 	uint16_t r_max;
156         size_t pre_data_pos;
157 
158 	uint16_t hex_data_size;
159 	char *hex_data_str = NULL;
160 	uint16_t cur_hex_data_size;
161 	size_t hex_pos = 0;
162 	uint8_t *hex_data = NULL;
163 
164 	new = ldns_rr_new();
165 
166 	owner = LDNS_XMALLOC(char, LDNS_MAX_DOMAINLEN + 1);
167 	ttl = LDNS_XMALLOC(char, LDNS_TTL_DATALEN);
168 	clas = LDNS_XMALLOC(char, LDNS_SYNTAX_DATALEN);
169 	rdata = LDNS_XMALLOC(char, LDNS_MAX_PACKETLEN + 1);
170 	rr_buf = LDNS_MALLOC(ldns_buffer);
171 	rd_buf = LDNS_MALLOC(ldns_buffer);
172 	rd = LDNS_XMALLOC(char, LDNS_MAX_RDFLEN);
173 	xtok = LDNS_XMALLOC(char, LDNS_MAX_RDFLEN);
174 	if (rr_buf) {
175 		rr_buf->_data = NULL;
176 	}
177 	if (rd_buf) {
178 		rd_buf->_data = NULL;
179 	}
180 	if (!new || !owner || !ttl || !clas || !rdata ||
181 			!rr_buf || !rd_buf || !rd || !xtok) {
182 
183 		goto memerror;
184 	}
185 
186 	ldns_buffer_new_frm_data(rr_buf, (char*)str, strlen(str));
187 
188 	/* split the rr in its parts -1 signals trouble */
189 	if (ldns_bget_token(rr_buf, owner, "\t\n ", LDNS_MAX_DOMAINLEN) == -1){
190 
191 		status = LDNS_STATUS_SYNTAX_ERR;
192 		goto error;
193 	}
194 
195 	if (ldns_bget_token(rr_buf, ttl, "\t\n ", LDNS_TTL_DATALEN) == -1) {
196 
197 		status = LDNS_STATUS_SYNTAX_TTL_ERR;
198 		goto error;
199 	}
200 	ttl_val = (uint32_t) ldns_str2period(ttl, &endptr);
201 
202 	if (strlen(ttl) > 0 && !isdigit((int) ttl[0])) {
203 		/* ah, it's not there or something */
204 		if (default_ttl == 0) {
205 			ttl_val = LDNS_DEFAULT_TTL;
206 		} else {
207 			ttl_val = default_ttl;
208 		}
209 		if (explicit_ttl)
210 			*explicit_ttl = false;
211 
212 		/* we not ASSUMING the TTL is missing and that
213 		 * the rest of the RR is still there. That is
214 		 * CLASS TYPE RDATA
215 		 * so ttl value we read is actually the class
216 		 */
217 		clas_val = ldns_get_rr_class_by_name(ttl);
218 		/* class can be left out too, assume IN, current
219 		 * token must be type
220 		 */
221 		if (clas_val == 0) {
222 			clas_val = LDNS_RR_CLASS_IN;
223 			type_sz = strlen(ttl) + 1;
224 			type = LDNS_XMALLOC(char, type_sz);
225 			if (!type) {
226 				goto memerror;
227 			}
228 			strlcpy(type, ttl, type_sz);
229 		}
230 	} else {
231 		if (explicit_ttl)
232 			*explicit_ttl = true;
233 
234 		if (-1 == ldns_bget_token(
235 				rr_buf, clas, "\t\n ", LDNS_SYNTAX_DATALEN)) {
236 
237 			status = LDNS_STATUS_SYNTAX_CLASS_ERR;
238 			goto error;
239 		}
240 		clas_val = ldns_get_rr_class_by_name(clas);
241 		/* class can be left out too, assume IN, current
242 		 * token must be type
243 		 */
244 		if (clas_val == 0) {
245 			clas_val = LDNS_RR_CLASS_IN;
246 			type_sz = strlen(clas) + 1;
247 			type = LDNS_XMALLOC(char, type_sz);
248 			if (!type) {
249 				goto memerror;
250 			}
251 			strlcpy(type, clas, type_sz);
252 		}
253 	}
254 	/* the rest should still be waiting for us */
255 
256 	if (!type) {
257 		type = LDNS_XMALLOC(char, LDNS_SYNTAX_DATALEN);
258 		if (!type) {
259 			goto memerror;
260 		}
261 		if (-1 == ldns_bget_token(
262 				rr_buf, type, "\t\n ", LDNS_SYNTAX_DATALEN)) {
263 
264 			status = LDNS_STATUS_SYNTAX_TYPE_ERR;
265 			goto error;
266 		}
267 	}
268 
269 	if (ldns_bget_token(rr_buf, rdata, "\0", LDNS_MAX_PACKETLEN) == -1) {
270 		/* apparently we are done, and it's only a question RR
271 		 * so do not set status and go to ldnserror here
272 		 */
273 	}
274 	ldns_buffer_new_frm_data(rd_buf, rdata, strlen(rdata));
275 
276 	if (strncmp(owner, "@", 1) == 0) {
277 		if (origin) {
278 			ldns_rr_set_owner(new, ldns_rdf_clone(origin));
279 		} else if (prev && *prev) {
280 			ldns_rr_set_owner(new, ldns_rdf_clone(*prev));
281 		} else {
282 			/* default to root */
283 			ldns_rr_set_owner(new, ldns_dname_new_frm_str("."));
284 		}
285 
286 		/* @ also overrides prev */
287 		if (prev) {
288 			ldns_rdf_deep_free(*prev);
289 			*prev = ldns_rdf_clone(ldns_rr_owner(new));
290 			if (!*prev) {
291 				goto memerror;
292 			}
293 		}
294 	} else {
295 		if (strlen(owner) == 0) {
296 			/* no ownername was given, try prev, if that fails
297 			 * origin, else default to root */
298 			if (prev && *prev) {
299 				ldns_rr_set_owner(new, ldns_rdf_clone(*prev));
300 			} else if (origin) {
301 				ldns_rr_set_owner(new, ldns_rdf_clone(origin));
302 			} else {
303 				ldns_rr_set_owner(new,
304 						ldns_dname_new_frm_str("."));
305 			}
306 			if(!ldns_rr_owner(new)) {
307 				goto memerror;
308 			}
309 		} else {
310 			owner_dname = ldns_dname_new_frm_str(owner);
311 			if (!owner_dname) {
312 				status = LDNS_STATUS_SYNTAX_ERR;
313 				goto error;
314 			}
315 
316 			ldns_rr_set_owner(new, owner_dname);
317 			if (!ldns_dname_str_absolute(owner) && origin) {
318 				if(ldns_dname_cat(ldns_rr_owner(new), origin)
319 						!= LDNS_STATUS_OK) {
320 
321 					status = LDNS_STATUS_SYNTAX_ERR;
322 					goto error;
323 				}
324 			}
325 			if (prev) {
326 				ldns_rdf_deep_free(*prev);
327 				*prev = ldns_rdf_clone(ldns_rr_owner(new));
328 				if (!*prev) {
329 					goto error;
330 				}
331 			}
332 		}
333 	}
334 	LDNS_FREE(owner);
335 
336 	ldns_rr_set_question(new, question);
337 
338 	ldns_rr_set_ttl(new, ttl_val);
339 	LDNS_FREE(ttl);
340 
341 	ldns_rr_set_class(new, clas_val);
342 	LDNS_FREE(clas);
343 
344 	rr_type = ldns_get_rr_type_by_name(type);
345 	LDNS_FREE(type);
346 
347 	desc = ldns_rr_descript((uint16_t)rr_type);
348 	ldns_rr_set_type(new, rr_type);
349 	if (desc) {
350 		/* only the rdata remains */
351 		r_max = ldns_rr_descriptor_maximum(desc);
352 		r_min = ldns_rr_descriptor_minimum(desc);
353 	} else {
354 		r_min = 0;
355 		r_max = 1;
356 	}
357 
358 	for (done = false, r_cnt = 0; !done && r_cnt < r_max; r_cnt++) {
359 		quoted = false;
360 
361 		switch (ldns_rr_descriptor_field_type(desc, r_cnt)) {
362 		case LDNS_RDF_TYPE_B64        :
363 		case LDNS_RDF_TYPE_HEX        : /* These rdf types may con- */
364 		case LDNS_RDF_TYPE_NSEC       : /* tain whitespace, only if */
365 		case LDNS_RDF_TYPE_LOC        : /* it is the last rd field. */
366 		case LDNS_RDF_TYPE_WKS        :
367 		case LDNS_RDF_TYPE_IPSECKEY   :
368 		case LDNS_RDF_TYPE_AMTRELAY   :
369 		case LDNS_RDF_TYPE_SVCPARAMS  :	if (r_cnt == r_max - 1) {
370 							delimiters = "\n";
371 							break;
372 						}
373 						/* fallthrough */
374 		default                       :	delimiters = "\n\t ";
375 		}
376 
377 		if (ldns_rdf_type_maybe_quoted(
378 				ldns_rr_descriptor_field_type(
379 				desc, r_cnt)) &&
380 				ldns_buffer_remaining(rd_buf) > 0){
381 
382 			/* skip whitespace */
383 			while (ldns_buffer_remaining(rd_buf) > 0 &&
384 				_BUFFER_IS_AT_WHITESPACE(rd_buf)) {
385 				ldns_buffer_skip(rd_buf, 1);
386 			}
387 
388 			if (ldns_buffer_remaining(rd_buf) > 0 &&
389 				*(ldns_buffer_current(rd_buf)) == '\"') {
390 				delimiters = "\"\0";
391 				ldns_buffer_skip(rd_buf, 1);
392 				quoted = true;
393 			}
394 			if (!quoted && ldns_rr_descriptor_field_type(desc, r_cnt)
395 					== LDNS_RDF_TYPE_LONG_STR) {
396 
397 				status = LDNS_STATUS_SYNTAX_RDATA_ERR;
398 				goto error;
399 			}
400 		}
401 
402 		/* because number of fields can be variable, we can't rely on
403 		 * _maximum() only
404 		 */
405 
406 		/* skip whitespace */
407 		while (ldns_buffer_position(rd_buf) < ldns_buffer_limit(rd_buf)
408 				&& _BUFFER_IS_AT_WHITESPACE(rd_buf)
409 				&& !quoted) {
410 
411 			ldns_buffer_skip(rd_buf, 1);
412 		}
413 
414 		pre_data_pos = ldns_buffer_position(rd_buf);
415 		if (-1 == (c = ldns_bget_token(
416 				rd_buf, rd, delimiters, LDNS_MAX_RDFLEN))) {
417 
418 			done = true;
419 			(void)done; /* we're breaking, so done not read anymore */
420 			break;
421 		}
422 		/* hmmz, rfc3597 specifies that any type can be represented
423 		 * with \# method, which can contain spaces...
424 		 * it does specify size though...
425 		 */
426 		rd_strlen = strlen(rd);
427 
428 		/* unknown RR data */
429 		if (strncmp(rd, "\\#", 2) == 0 && !quoted &&
430 				(rd_strlen == 2 || _IS_WHITESPACE(rd[2]))) {
431 
432 			was_unknown_rr_format = 1;
433 			/* go back to before \#
434 			 * and skip it while setting delimiters better
435 			 */
436 			ldns_buffer_set_position(rd_buf, pre_data_pos);
437 			delimiters = "\n\t ";
438 			(void)ldns_bget_token(rd_buf, rd,
439 					delimiters, LDNS_MAX_RDFLEN);
440 			/* read rdata octet length */
441 			c = ldns_bget_token(rd_buf, rd,
442 					delimiters, LDNS_MAX_RDFLEN);
443 			if (c == -1) {
444 				/* something goes very wrong here */
445 				status = LDNS_STATUS_SYNTAX_RDATA_ERR;
446 				goto error;
447 			}
448 			hex_data_size = (uint16_t) atoi(rd);
449 			/* copy hex chars into hex str (2 chars per byte) */
450 			hex_data_str = LDNS_XMALLOC(char, 2*hex_data_size + 1);
451 			if (!hex_data_str) {
452 				/* malloc error */
453 				goto memerror;
454 			}
455 			cur_hex_data_size = 0;
456 			while(cur_hex_data_size < 2 * hex_data_size) {
457 				c = ldns_bget_token(rd_buf, rd,
458 						delimiters, LDNS_MAX_RDFLEN);
459 				if (c == -1) {
460 					status = LDNS_STATUS_SYNTAX_RDATA_ERR;
461 					goto error;
462 				}
463 				rd_strlen = strlen(rd);
464 				if ((size_t)cur_hex_data_size + rd_strlen >
465 				    2 * (size_t)hex_data_size) {
466 					status = LDNS_STATUS_SYNTAX_RDATA_ERR;
467 					goto error;
468 				}
469 				strlcpy(hex_data_str + cur_hex_data_size, rd,
470 						rd_strlen + 1);
471 
472 				cur_hex_data_size += rd_strlen;
473 			}
474 			hex_data_str[cur_hex_data_size] = '\0';
475 
476 			/* correct the rdf type */
477 			/* if *we* know the type, interpret it as wireformat */
478 			if (desc) {
479 				hex_pos = 0;
480 				hex_data =
481 					LDNS_XMALLOC(uint8_t, hex_data_size+2);
482 
483 				if (!hex_data) {
484 					goto memerror;
485 				}
486 				ldns_write_uint16(hex_data, hex_data_size);
487 				ldns_hexstring_to_data(
488 						hex_data + 2, hex_data_str);
489 				status = ldns_wire2rdf(new, hex_data,
490 						hex_data_size + 2, &hex_pos);
491 				if (status != LDNS_STATUS_OK) {
492 					goto error;
493 				}
494 				LDNS_FREE(hex_data);
495 			} else {
496 				r = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_HEX,
497 						hex_data_str);
498 				if (!r) {
499 					goto memerror;
500 				}
501 				ldns_rdf_set_type(r, LDNS_RDF_TYPE_UNKNOWN);
502 				if (!ldns_rr_push_rdf(new, r)) {
503 					goto memerror;
504 				}
505 			}
506 			LDNS_FREE(hex_data_str);
507 
508 		} else if(rd_strlen > 0 || quoted) {
509 			/* Normal RR */
510 			switch(ldns_rr_descriptor_field_type(desc, r_cnt)) {
511 
512 			case LDNS_RDF_TYPE_HEX:
513 			case LDNS_RDF_TYPE_B64:
514 				/* When this is the last rdata field, then the
515 				 * rest should be read in (cause then these
516 				 * rdf types may contain spaces).
517 				 */
518 				if (r_cnt == r_max - 1) {
519 					c = ldns_bget_token(rd_buf, xtok,
520 							"\n", LDNS_MAX_RDFLEN);
521 					if (c != -1) {
522 						(void) strncat(rd, xtok,
523 							LDNS_MAX_RDFLEN -
524 							strlen(rd) - 1);
525 					}
526 				}
527 				r = ldns_rdf_new_frm_str(
528 						ldns_rr_descriptor_field_type(
529 							desc, r_cnt), rd);
530 				break;
531 
532 			case LDNS_RDF_TYPE_HIP:
533 				/*
534 				 * In presentation format this RDATA type has
535 				 * three tokens: An algorithm byte, then a
536 				 * variable length HIT (in hexbytes) and then
537 				 * a variable length Public Key (in base64).
538 				 *
539 				 * We have just read the algorithm, so we need
540 				 * two more tokens: HIT and Public Key.
541 				 */
542 				do {
543 					/* Read and append HIT */
544 					if (ldns_bget_token(rd_buf,
545 							xtok, delimiters,
546 							LDNS_MAX_RDFLEN) == -1)
547 						break;
548 
549 					(void) strncat(rd, " ",
550 							LDNS_MAX_RDFLEN -
551 							strlen(rd) - 1);
552 					(void) strncat(rd, xtok,
553 							LDNS_MAX_RDFLEN -
554 							strlen(rd) - 1);
555 
556 					/* Read and append Public Key*/
557 					if (ldns_bget_token(rd_buf,
558 							xtok, delimiters,
559 							LDNS_MAX_RDFLEN) == -1)
560 						break;
561 
562 					(void) strncat(rd, " ",
563 							LDNS_MAX_RDFLEN -
564 							strlen(rd) - 1);
565 					(void) strncat(rd, xtok,
566 							LDNS_MAX_RDFLEN -
567 							strlen(rd) - 1);
568 				} while (false);
569 
570 				r = ldns_rdf_new_frm_str(
571 						ldns_rr_descriptor_field_type(
572 							desc, r_cnt), rd);
573 				break;
574 
575 			case LDNS_RDF_TYPE_DNAME:
576 				r = ldns_rdf_new_frm_str(
577 						ldns_rr_descriptor_field_type(
578 							desc, r_cnt), rd);
579 
580 				/* check if the origin should be used
581 				 * or concatenated
582 				 */
583 				if (r && ldns_rdf_size(r) > 1 &&
584 						ldns_rdf_data(r)[0] == 1 &&
585 						ldns_rdf_data(r)[1] == '@') {
586 
587 					ldns_rdf_deep_free(r);
588 
589 					r = origin ? ldns_rdf_clone(origin)
590 
591 					  : ( rr_type == LDNS_RR_TYPE_SOA ?
592 
593 					      ldns_rdf_clone(
594 						      ldns_rr_owner(new))
595 
596 					    : ldns_rdf_new_frm_str(
597 						    LDNS_RDF_TYPE_DNAME, ".")
598 					    );
599 
600 				} else if (r && rd_strlen >= 1
601 				    && (origin || rr_type == LDNS_RR_TYPE_SOA)
602 				    && !ldns_dname_str_absolute(rd)) {
603 
604 					status = ldns_dname_cat(r, origin
605 					    ? origin : ldns_rr_owner(new));
606 					if (status != LDNS_STATUS_OK) {
607 						goto error;
608 					}
609 				}
610 				break;
611 			default:
612 				r = ldns_rdf_new_frm_str(
613 						ldns_rr_descriptor_field_type(
614 							desc, r_cnt), rd);
615 				break;
616 			}
617 			if (!r) {
618 				status = LDNS_STATUS_SYNTAX_RDATA_ERR;
619 				goto error;
620 			}
621 			ldns_rr_push_rdf(new, r);
622 		}
623 	} /* for (done = false, r_cnt = 0; !done && r_cnt < r_max; r_cnt++) */
624 	LDNS_FREE(rd);
625 	LDNS_FREE(xtok);
626 	ldns_buffer_free(rr_buf);
627 	LDNS_FREE(rdata);
628 	if (ldns_buffer_remaining(rd_buf) > 0) {
629 		ldns_buffer_free(rd_buf);
630 		ldns_rr_free(new);
631 		return LDNS_STATUS_SYNTAX_SUPERFLUOUS_TEXT_ERR;
632 	}
633 	ldns_buffer_free(rd_buf);
634 
635 	if (!question && desc && !was_unknown_rr_format &&
636 			ldns_rr_rd_count(new) < r_min) {
637 
638 		ldns_rr_free(new);
639 		return LDNS_STATUS_SYNTAX_MISSING_VALUE_ERR;
640 	}
641 
642 	if (newrr) {
643 		*newrr = new;
644 	} else {
645 		/* Maybe the caller just wanted to see if it would parse? */
646 		ldns_rr_free(new);
647 	}
648 	return LDNS_STATUS_OK;
649 
650 memerror:
651 	status = LDNS_STATUS_MEM_ERR;
652 error:
653 	if (rd_buf && rd_buf->_data) {
654 		ldns_buffer_free(rd_buf);
655 	} else {
656 		LDNS_FREE(rd_buf);
657 	}
658 	if (rr_buf && rr_buf->_data) {
659 		ldns_buffer_free(rr_buf);
660 	} else {
661 		LDNS_FREE(rr_buf);
662 	}
663 	LDNS_FREE(type);
664 	LDNS_FREE(owner);
665 	LDNS_FREE(ttl);
666 	LDNS_FREE(clas);
667 	LDNS_FREE(hex_data);
668 	LDNS_FREE(hex_data_str);
669 	LDNS_FREE(xtok);
670 	LDNS_FREE(rd);
671 	LDNS_FREE(rdata);
672 	ldns_rr_free(new);
673 	return status;
674 }
675 
676 ldns_status
677 ldns_rr_new_frm_str(ldns_rr **newrr, const char *str,
678                     uint32_t default_ttl, const ldns_rdf *origin,
679 				    ldns_rdf **prev)
680 {
681 	return ldns_rr_new_frm_str_internal(newrr,
682 	                                    str,
683 	                                    default_ttl,
684 	                                    origin,
685 	                                    prev,
686 	                                    false,
687 					    NULL);
688 }
689 
690 ldns_status
691 ldns_rr_new_question_frm_str(ldns_rr **newrr, const char *str,
692                              const ldns_rdf *origin, ldns_rdf **prev)
693 {
694 	return ldns_rr_new_frm_str_internal(newrr,
695 	                                    str,
696 	                                    0,
697 	                                    origin,
698 	                                    prev,
699 	                                    true,
700 					    NULL);
701 }
702 
703 /* Strip whitespace from the start and the end of <line>.  */
704 static char *
705 ldns_strip_ws(char *line)
706 {
707 	char *s = line, *e;
708 
709 	for (s = line; *s && isspace((unsigned char)*s); s++)
710 		;
711 
712 	for (e = strchr(s, 0); e > s+2 && isspace((unsigned char)e[-1]) && e[-2] != '\\'; e--)
713 		;
714 	*e = 0;
715 
716 	return s;
717 }
718 
719 ldns_status
720 ldns_rr_new_frm_fp(ldns_rr **newrr, FILE *fp, uint32_t *ttl, ldns_rdf **origin, ldns_rdf **prev)
721 {
722 	return ldns_rr_new_frm_fp_l(newrr, fp, ttl, origin, prev, NULL);
723 }
724 
725 ldns_status
726 _ldns_rr_new_frm_fp_l_internal(ldns_rr **newrr, FILE *fp,
727 		uint32_t *default_ttl, ldns_rdf **origin, ldns_rdf **prev,
728 		int *line_nr, bool *explicit_ttl)
729 {
730 	char *line = NULL;
731 	size_t limit = 0;
732 	const char *endptr;  /* unused */
733 	ldns_rr *rr;
734 	uint32_t ttl;
735 	ldns_rdf *tmp;
736 	ldns_status s;
737 
738 	if (default_ttl) {
739 		ttl = *default_ttl;
740 	} else {
741 		ttl = 0;
742 	}
743 	/* read an entire line in from the file */
744 	if ((s = ldns_fget_token_l_st( fp, &line, &limit, false
745 	                             , LDNS_PARSE_SKIP_SPACE, line_nr))) {
746 		LDNS_FREE(line);
747 		return s;
748 	}
749 
750 	if (strncmp(line, "$ORIGIN", 7) == 0 && isspace((unsigned char)line[7])) {
751 		if (*origin) {
752 			ldns_rdf_deep_free(*origin);
753 			*origin = NULL;
754 		}
755 		tmp = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME,
756 				ldns_strip_ws(line + 8));
757 		if (!tmp) {
758 			/* could not parse what next to $ORIGIN */
759 			LDNS_FREE(line);
760 			return LDNS_STATUS_SYNTAX_DNAME_ERR;
761 		}
762 		*origin = tmp;
763 		s = LDNS_STATUS_SYNTAX_ORIGIN;
764 	} else if (strncmp(line, "$TTL", 4) == 0 && isspace((unsigned char)line[4])) {
765 		if (default_ttl) {
766 			*default_ttl = ldns_str2period(
767 					ldns_strip_ws(line + 5), &endptr);
768 		}
769 		s = LDNS_STATUS_SYNTAX_TTL;
770 	} else if (strncmp(line, "$INCLUDE", 8) == 0) {
771 		s = LDNS_STATUS_SYNTAX_INCLUDE;
772 	} else if (!*ldns_strip_ws(line)) {
773 		LDNS_FREE(line);
774 		return LDNS_STATUS_SYNTAX_EMPTY;
775 	} else {
776 		if (origin && *origin) {
777 			s = ldns_rr_new_frm_str_internal(&rr, (const char*)line,
778 				ttl, *origin, prev, false, explicit_ttl);
779 		} else {
780 			s = ldns_rr_new_frm_str_internal(&rr, (const char*)line,
781 				ttl, NULL, prev, false, explicit_ttl);
782 		}
783 	}
784 	LDNS_FREE(line);
785 	if (s == LDNS_STATUS_OK) {
786 		if (newrr) {
787 			*newrr = rr;
788 		} else {
789 			/* Just testing if it would parse? */
790 			ldns_rr_free(rr);
791 		}
792 	}
793 	return s;
794 }
795 
796 ldns_status
797 ldns_rr_new_frm_fp_l(ldns_rr **newrr, FILE *fp, uint32_t *default_ttl,
798 		ldns_rdf **origin, ldns_rdf **prev, int *line_nr)
799 {
800 	return _ldns_rr_new_frm_fp_l_internal(newrr, fp, default_ttl, origin,
801 			prev, line_nr, NULL);
802 }
803 
804 void
805 ldns_rr_set_owner(ldns_rr *rr, ldns_rdf *owner)
806 {
807 	rr->_owner = owner;
808 }
809 
810 void
811 ldns_rr_set_question(ldns_rr *rr, bool question)
812 {
813    rr->_rr_question = question;
814 }
815 
816 void
817 ldns_rr_set_ttl(ldns_rr *rr, uint32_t ttl)
818 {
819 	rr->_ttl = ttl;
820 }
821 
822 void
823 ldns_rr_set_rd_count(ldns_rr *rr, size_t count)
824 {
825 	rr->_rd_count = count;
826 }
827 
828 void
829 ldns_rr_set_type(ldns_rr *rr, ldns_rr_type rr_type)
830 {
831 	rr->_rr_type = rr_type;
832 }
833 
834 void
835 ldns_rr_set_class(ldns_rr *rr, ldns_rr_class rr_class)
836 {
837 	rr->_rr_class = rr_class;
838 }
839 
840 ldns_rdf *
841 ldns_rr_set_rdf(ldns_rr *rr, const ldns_rdf *f, size_t position)
842 {
843 	size_t rd_count;
844 	ldns_rdf *pop;
845 
846 	rd_count = ldns_rr_rd_count(rr);
847 	if (position < rd_count) {
848 		/* discard the old one */
849 		pop = rr->_rdata_fields[position];
850 		rr->_rdata_fields[position] = (ldns_rdf*)f;
851 		return pop;
852 	} else {
853 		return NULL;
854 	}
855 }
856 
857 bool
858 ldns_rr_push_rdf(ldns_rr *rr, const ldns_rdf *f)
859 {
860 	size_t rd_count;
861 	ldns_rdf **rdata_fields;
862 
863 	rd_count = ldns_rr_rd_count(rr);
864 
865 	/* grow the array */
866 	rdata_fields = LDNS_XREALLOC(
867 		rr->_rdata_fields, ldns_rdf *, rd_count + 1);
868 	if (!rdata_fields) {
869 		return false;
870 	}
871 
872 	/* add the new member */
873 	rr->_rdata_fields = rdata_fields;
874 	rr->_rdata_fields[rd_count] = (ldns_rdf*)f;
875 
876 	ldns_rr_set_rd_count(rr, rd_count + 1);
877 	return true;
878 }
879 
880 ldns_rdf *
881 ldns_rr_pop_rdf(ldns_rr *rr)
882 {
883 	size_t rd_count;
884 	ldns_rdf *pop;
885 	ldns_rdf** newrd;
886 
887 	rd_count = ldns_rr_rd_count(rr);
888 
889 	if (rd_count == 0) {
890 		return NULL;
891 	}
892 
893 	pop = rr->_rdata_fields[rd_count - 1];
894 
895 	/* try to shrink the array */
896 	if(rd_count > 1) {
897 		newrd = LDNS_XREALLOC(
898 			rr->_rdata_fields, ldns_rdf *, rd_count - 1);
899 		if(newrd)
900 			rr->_rdata_fields = newrd;
901 	} else {
902 		LDNS_FREE(rr->_rdata_fields);
903 	}
904 
905 	ldns_rr_set_rd_count(rr, rd_count - 1);
906 	return pop;
907 }
908 
909 ldns_rdf *
910 ldns_rr_rdf(const ldns_rr *rr, size_t nr)
911 {
912 	if (rr && nr < ldns_rr_rd_count(rr)) {
913 		return rr->_rdata_fields[nr];
914 	} else {
915 		return NULL;
916 	}
917 }
918 
919 ldns_rdf *
920 ldns_rr_owner(const ldns_rr *rr)
921 {
922 	return rr->_owner;
923 }
924 
925 bool
926 ldns_rr_is_question(const ldns_rr *rr)
927 {
928    return rr->_rr_question;
929 }
930 
931 uint32_t
932 ldns_rr_ttl(const ldns_rr *rr)
933 {
934 	return rr->_ttl;
935 }
936 
937 size_t
938 ldns_rr_rd_count(const ldns_rr *rr)
939 {
940 	return rr->_rd_count;
941 }
942 
943 ldns_rr_type
944 ldns_rr_get_type(const ldns_rr *rr)
945 {
946         return rr->_rr_type;
947 }
948 
949 ldns_rr_class
950 ldns_rr_get_class(const ldns_rr *rr)
951 {
952         return rr->_rr_class;
953 }
954 
955 /* rr_lists */
956 
957 size_t
958 ldns_rr_list_rr_count(const ldns_rr_list *rr_list)
959 {
960 	if (rr_list) {
961 		return rr_list->_rr_count;
962 	} else {
963 		return 0;
964 	}
965 }
966 
967 ldns_rr *
968 ldns_rr_list_set_rr(ldns_rr_list *rr_list, const ldns_rr *r, size_t count)
969 {
970 	ldns_rr *old;
971 
972 	if (count > ldns_rr_list_rr_count(rr_list)) {
973 		return NULL;
974 	}
975 
976 	old = ldns_rr_list_rr(rr_list, count);
977 
978 	/* overwrite old's pointer */
979 	rr_list->_rrs[count] = (ldns_rr*)r;
980 	return old;
981 }
982 
983 void
984 ldns_rr_list_set_rr_count(ldns_rr_list *rr_list, size_t count)
985 {
986 	assert(count <= rr_list->_rr_capacity);
987 	rr_list->_rr_count = count;
988 }
989 
990 ldns_rr *
991 ldns_rr_list_rr(const ldns_rr_list *rr_list, size_t nr)
992 {
993 	if (nr < ldns_rr_list_rr_count(rr_list)) {
994 		return rr_list->_rrs[nr];
995 	} else {
996 		return NULL;
997 	}
998 }
999 
1000 ldns_rr_list *
1001 ldns_rr_list_new(void)
1002 {
1003 	ldns_rr_list *rr_list = LDNS_MALLOC(ldns_rr_list);
1004         if(!rr_list) return NULL;
1005 	rr_list->_rr_count = 0;
1006 	rr_list->_rr_capacity = 0;
1007 	rr_list->_rrs = NULL;
1008 	return rr_list;
1009 }
1010 
1011 void
1012 ldns_rr_list_free(ldns_rr_list *rr_list)
1013 {
1014 	if (rr_list) {
1015 		LDNS_FREE(rr_list->_rrs);
1016 		LDNS_FREE(rr_list);
1017 	}
1018 }
1019 
1020 void
1021 ldns_rr_list_deep_free(ldns_rr_list *rr_list)
1022 {
1023 	size_t i;
1024 
1025 	if (rr_list) {
1026 		for (i=0; i < ldns_rr_list_rr_count(rr_list); i++) {
1027 			ldns_rr_free(ldns_rr_list_rr(rr_list, i));
1028 		}
1029 		LDNS_FREE(rr_list->_rrs);
1030 		LDNS_FREE(rr_list);
1031 	}
1032 }
1033 
1034 
1035 /* add right to left. So we modify *left! */
1036 bool
1037 ldns_rr_list_cat(ldns_rr_list *left, const ldns_rr_list *right)
1038 {
1039 	size_t r_rr_count;
1040 	size_t i;
1041 
1042 	if (!left) {
1043 		return false;
1044 	}
1045 
1046 	if (right) {
1047 		r_rr_count = ldns_rr_list_rr_count(right);
1048 	} else {
1049 		r_rr_count = 0;
1050 	}
1051 
1052 	/* push right to left */
1053 	for(i = 0; i < r_rr_count; i++) {
1054 		ldns_rr_list_push_rr(left, ldns_rr_list_rr(right, i));
1055 	}
1056 	return true;
1057 }
1058 
1059 ldns_rr_list *
1060 ldns_rr_list_cat_clone(const ldns_rr_list *left, const ldns_rr_list *right)
1061 {
1062 	size_t l_rr_count;
1063 	size_t r_rr_count;
1064 	size_t i;
1065 	ldns_rr_list *cat;
1066 
1067 	if (left) {
1068 		l_rr_count = ldns_rr_list_rr_count(left);
1069 	} else {
1070 		return ldns_rr_list_clone(right);
1071 	}
1072 
1073 	if (right) {
1074 		r_rr_count = ldns_rr_list_rr_count(right);
1075 	} else {
1076 		r_rr_count = 0;
1077 	}
1078 
1079 	cat = ldns_rr_list_new();
1080 
1081 	if (!cat) {
1082 		return NULL;
1083 	}
1084 
1085 	/* left */
1086 	for(i = 0; i < l_rr_count; i++) {
1087 		ldns_rr_list_push_rr(cat,
1088 				ldns_rr_clone(ldns_rr_list_rr(left, i)));
1089 	}
1090 	/* right */
1091 	for(i = 0; i < r_rr_count; i++) {
1092 		ldns_rr_list_push_rr(cat,
1093 				ldns_rr_clone(ldns_rr_list_rr(right, i)));
1094 	}
1095 	return cat;
1096 }
1097 
1098 ldns_rr_list *
1099 ldns_rr_list_subtype_by_rdf(const ldns_rr_list *l, const ldns_rdf *r, size_t pos)
1100 {
1101 	size_t i;
1102 	ldns_rr_list *subtyped;
1103 	ldns_rdf *list_rdf;
1104 
1105 	subtyped = ldns_rr_list_new();
1106 
1107 	for(i = 0; i < ldns_rr_list_rr_count(l); i++) {
1108 		list_rdf = ldns_rr_rdf(
1109 			ldns_rr_list_rr(l, i),
1110 			pos);
1111 		if (!list_rdf) {
1112 			/* pos is too large or any other error */
1113 			ldns_rr_list_deep_free(subtyped);
1114 			return NULL;
1115 		}
1116 
1117 		if (ldns_rdf_compare(list_rdf, r) == 0) {
1118 			/* a match */
1119 			ldns_rr_list_push_rr(subtyped,
1120 					ldns_rr_clone(ldns_rr_list_rr(l, i)));
1121 		}
1122 	}
1123 
1124 	if (ldns_rr_list_rr_count(subtyped) > 0) {
1125 		return subtyped;
1126 	} else {
1127 		ldns_rr_list_free(subtyped);
1128 		return NULL;
1129 	}
1130 }
1131 
1132 bool
1133 ldns_rr_list_push_rr(ldns_rr_list *rr_list, const ldns_rr *rr)
1134 {
1135 	size_t rr_count;
1136 	size_t cap;
1137 
1138 	rr_count = ldns_rr_list_rr_count(rr_list);
1139 	cap = rr_list->_rr_capacity;
1140 
1141 	/* grow the array */
1142 	if(rr_count+1 > cap) {
1143 		ldns_rr **rrs;
1144 
1145 		if(cap == 0)
1146 			cap = LDNS_RRLIST_INIT;  /* initial list size */
1147 		else	cap *= 2;
1148 		rrs = LDNS_XREALLOC(rr_list->_rrs, ldns_rr *, cap);
1149 		if (!rrs) {
1150 			return false;
1151 		}
1152 		rr_list->_rrs = rrs;
1153 		rr_list->_rr_capacity = cap;
1154 	}
1155 
1156 	/* add the new member */
1157 	rr_list->_rrs[rr_count] = (ldns_rr*)rr;
1158 
1159 	ldns_rr_list_set_rr_count(rr_list, rr_count + 1);
1160 	return true;
1161 }
1162 
1163 bool
1164 ldns_rr_list_push_rr_list(ldns_rr_list *rr_list, const ldns_rr_list *push_list)
1165 {
1166 	size_t i;
1167 
1168 	for(i = 0; i < ldns_rr_list_rr_count(push_list); i++) {
1169 		if (!ldns_rr_list_push_rr(rr_list,
1170 				ldns_rr_list_rr(push_list, i))) {
1171 			return false;
1172 		}
1173 	}
1174 	return true;
1175 }
1176 
1177 ldns_rr *
1178 ldns_rr_list_pop_rr(ldns_rr_list *rr_list)
1179 {
1180 	size_t rr_count;
1181 	size_t cap;
1182 	ldns_rr *pop;
1183 
1184 	rr_count = ldns_rr_list_rr_count(rr_list);
1185 
1186 	if (rr_count == 0) {
1187 		return NULL;
1188 	}
1189 
1190 	cap = rr_list->_rr_capacity;
1191 	pop = ldns_rr_list_rr(rr_list, rr_count - 1);
1192 
1193 	/* shrink the array */
1194 	if(cap > LDNS_RRLIST_INIT && rr_count-1 <= cap/2) {
1195                 ldns_rr** a;
1196 		cap /= 2;
1197                 a = LDNS_XREALLOC(rr_list->_rrs, ldns_rr *, cap);
1198                 if(a) {
1199 		        rr_list->_rrs = a;
1200 		        rr_list->_rr_capacity = cap;
1201                 }
1202                 /* if the realloc fails, the capacity for the list remains unchanged */
1203 	}
1204 
1205 	ldns_rr_list_set_rr_count(rr_list, rr_count - 1);
1206 
1207 	return pop;
1208 }
1209 
1210 ldns_rr_list *
1211 ldns_rr_list_pop_rr_list(ldns_rr_list *rr_list, size_t howmany)
1212 {
1213 	/* pop a number of rr's and put them in a rr_list */
1214 	ldns_rr_list *popped;
1215 	ldns_rr *p;
1216 	size_t i = howmany;
1217 
1218 	popped = ldns_rr_list_new();
1219 
1220 	if (!popped) {
1221 		return NULL;
1222 	}
1223 
1224 
1225 	while(i > 0 &&
1226 			(p = ldns_rr_list_pop_rr(rr_list)) != NULL) {
1227 		ldns_rr_list_push_rr(popped, p);
1228 		i--;
1229 	}
1230 
1231 	if (i == howmany) { /* so i <= 0 */
1232 		ldns_rr_list_free(popped);
1233 		return NULL;
1234 	} else {
1235 		return popped;
1236 	}
1237 }
1238 
1239 
1240 bool
1241 ldns_rr_list_contains_rr(const ldns_rr_list *rr_list, const ldns_rr *rr)
1242 {
1243 	size_t i;
1244 
1245 	if (!rr_list || !rr || ldns_rr_list_rr_count(rr_list) == 0) {
1246 		return false;
1247 	}
1248 
1249 	for (i = 0; i < ldns_rr_list_rr_count(rr_list); i++) {
1250 		if (rr == ldns_rr_list_rr(rr_list, i)) {
1251 			return true;
1252 		} else if (ldns_rr_compare(rr, ldns_rr_list_rr(rr_list, i)) == 0) {
1253 			return true;
1254 		}
1255 	}
1256 	return false;
1257 }
1258 
1259 bool
1260 ldns_is_rrset(const ldns_rr_list *rr_list)
1261 {
1262 	ldns_rr_type t;
1263 	ldns_rr_class c;
1264 	ldns_rdf *o;
1265 	ldns_rr *tmp;
1266 	size_t i;
1267 
1268 	if (!rr_list || ldns_rr_list_rr_count(rr_list) == 0) {
1269 		return false;
1270 	}
1271 
1272 	tmp = ldns_rr_list_rr(rr_list, 0);
1273 
1274 	t = ldns_rr_get_type(tmp);
1275 	c = ldns_rr_get_class(tmp);
1276 	o = ldns_rr_owner(tmp);
1277 
1278 	/* compare these with the rest of the rr_list, start with 1 */
1279 	for (i = 1; i < ldns_rr_list_rr_count(rr_list); i++) {
1280 		tmp = ldns_rr_list_rr(rr_list, i);
1281 		if (t != ldns_rr_get_type(tmp)) {
1282 			return false;
1283 		}
1284 		if (c != ldns_rr_get_class(tmp)) {
1285 			return false;
1286 		}
1287 		if (ldns_rdf_compare(o, ldns_rr_owner(tmp)) != 0) {
1288 			return false;
1289 		}
1290 	}
1291 	return true;
1292 }
1293 
1294 bool
1295 ldns_rr_set_push_rr(ldns_rr_list *rr_list, ldns_rr *rr)
1296 {
1297 	size_t rr_count;
1298 	size_t i;
1299 	ldns_rr *last;
1300 
1301 	assert(rr != NULL);
1302 
1303 	rr_count = ldns_rr_list_rr_count(rr_list);
1304 
1305 	if (rr_count == 0) {
1306 		/* nothing there, so checking it is
1307 		 * not needed */
1308 		return ldns_rr_list_push_rr(rr_list, rr);
1309 	} else {
1310 		/* check with the final rr in the rr_list */
1311 		last = ldns_rr_list_rr(rr_list, rr_count - 1);
1312 
1313 		if (ldns_rr_get_class(last) != ldns_rr_get_class(rr)) {
1314 			return false;
1315 		}
1316 		if (ldns_rr_get_type(last) != ldns_rr_get_type(rr)) {
1317 			return false;
1318 		}
1319 		/* only check if not equal to RRSIG */
1320 		if (ldns_rr_get_type(rr) != LDNS_RR_TYPE_RRSIG) {
1321 			if (ldns_rr_ttl(last) != ldns_rr_ttl(rr)) {
1322 				return false;
1323 			}
1324 		}
1325 		if (ldns_rdf_compare(ldns_rr_owner(last),
1326 					ldns_rr_owner(rr)) != 0) {
1327 			return false;
1328 		}
1329 		/* ok, still alive - check if the rr already
1330 		 * exists - if so, don't add it */
1331 		for(i = 0; i < rr_count; i++) {
1332 			if(ldns_rr_compare(
1333 					ldns_rr_list_rr(rr_list, i), rr) == 0) {
1334 				return false;
1335 			}
1336 		}
1337 		/* it's safe, push it */
1338 		return ldns_rr_list_push_rr(rr_list, rr);
1339 	}
1340 }
1341 
1342 ldns_rr *
1343 ldns_rr_set_pop_rr(ldns_rr_list *rr_list)
1344 {
1345 	return ldns_rr_list_pop_rr(rr_list);
1346 }
1347 
1348 ldns_rr_list *
1349 ldns_rr_list_pop_rrset(ldns_rr_list *rr_list)
1350 {
1351 	ldns_rr_list *rrset;
1352 	ldns_rr *last_rr = NULL;
1353 	ldns_rr *next_rr;
1354 
1355 	if (!rr_list) {
1356 		return NULL;
1357 	}
1358 
1359 	rrset = ldns_rr_list_new();
1360 	if (!last_rr) {
1361 		last_rr = ldns_rr_list_pop_rr(rr_list);
1362 		if (!last_rr) {
1363 			ldns_rr_list_free(rrset);
1364 			return NULL;
1365 		} else {
1366 			ldns_rr_list_push_rr(rrset, last_rr);
1367 		}
1368 	}
1369 
1370 	if (ldns_rr_list_rr_count(rr_list) > 0) {
1371 		next_rr = ldns_rr_list_rr(rr_list, ldns_rr_list_rr_count(rr_list) - 1);
1372 	} else {
1373 		next_rr = NULL;
1374 	}
1375 
1376 	while (next_rr) {
1377 		if (
1378 			ldns_rdf_compare(ldns_rr_owner(next_rr),
1379 					 ldns_rr_owner(last_rr)) == 0
1380 			&&
1381 			ldns_rr_get_type(next_rr) == ldns_rr_get_type(last_rr)
1382 			&&
1383 			ldns_rr_get_class(next_rr) == ldns_rr_get_class(last_rr)
1384 		   ) {
1385 			ldns_rr_list_push_rr(rrset, ldns_rr_list_pop_rr(rr_list));
1386 			if (ldns_rr_list_rr_count(rr_list) > 0) {
1387 				last_rr = next_rr;
1388 				next_rr = ldns_rr_list_rr(rr_list, ldns_rr_list_rr_count(rr_list) - 1);
1389 			} else {
1390 				next_rr = NULL;
1391 			}
1392 		} else {
1393 			next_rr = NULL;
1394 		}
1395 	}
1396 
1397 	return rrset;
1398 }
1399 
1400 ldns_rr *
1401 ldns_rr_clone(const ldns_rr *rr)
1402 {
1403 	size_t i;
1404 	ldns_rr *new_rr;
1405 
1406 	if (!rr) {
1407 		return NULL;
1408 	}
1409 
1410 	new_rr = ldns_rr_new();
1411 	if (!new_rr) {
1412 		return NULL;
1413 	}
1414 	if (ldns_rr_owner(rr)) {
1415 		ldns_rr_set_owner(new_rr, ldns_rdf_clone(ldns_rr_owner(rr)));
1416   	}
1417 	ldns_rr_set_ttl(new_rr, ldns_rr_ttl(rr));
1418 	ldns_rr_set_type(new_rr, ldns_rr_get_type(rr));
1419 	ldns_rr_set_class(new_rr, ldns_rr_get_class(rr));
1420 	ldns_rr_set_question(new_rr, ldns_rr_is_question(rr));
1421 
1422 	for (i = 0; i < ldns_rr_rd_count(rr); i++) {
1423         	if (ldns_rr_rdf(rr,i)) {
1424         		ldns_rr_push_rdf(new_rr, ldns_rdf_clone(ldns_rr_rdf(rr, i)));
1425                 }
1426 	}
1427 
1428 	return new_rr;
1429 }
1430 
1431 ldns_rr_list *
1432 ldns_rr_list_clone(const ldns_rr_list *rrlist)
1433 {
1434 	size_t i;
1435 	ldns_rr_list *new_list;
1436 	ldns_rr *r;
1437 
1438 	if (!rrlist) {
1439 		return NULL;
1440 	}
1441 
1442 	new_list = ldns_rr_list_new();
1443 	if (!new_list) {
1444 		return NULL;
1445 	}
1446 	for (i = 0; i < ldns_rr_list_rr_count(rrlist); i++) {
1447 		r = ldns_rr_clone(
1448 			ldns_rr_list_rr(rrlist, i)
1449 		    );
1450 		if (!r) {
1451 			/* huh, failure in cloning */
1452 			ldns_rr_list_deep_free(new_list);
1453 			return NULL;
1454 		}
1455 		ldns_rr_list_push_rr(new_list, r);
1456 	}
1457 	return new_list;
1458 }
1459 
1460 
1461 static int
1462 qsort_schwartz_rr_compare(const void *a, const void *b)
1463 {
1464 	int result = 0;
1465 	ldns_rr *rr1, *rr2;
1466 	ldns_buffer *rr1_buf, *rr2_buf;
1467 	struct ldns_schwartzian_compare_struct *sa = *(struct ldns_schwartzian_compare_struct **) a;
1468 	struct ldns_schwartzian_compare_struct *sb = *(struct ldns_schwartzian_compare_struct **) b;
1469 	/* if we are doing 2wire, we need to do lowercasing on the dname (and maybe on the rdata)
1470 	 * this must be done for comparison only, so we need to have a temp var for both buffers,
1471 	 * which is only used when the transformed object value isn't there yet
1472 	 */
1473 	ldns_rr *canonical_a, *canonical_b;
1474 
1475 	rr1 = (ldns_rr *) sa->original_object;
1476 	rr2 = (ldns_rr *) sb->original_object;
1477 
1478 	result = ldns_rr_compare_no_rdata(rr1, rr2);
1479 
1480 	if (result == 0) {
1481 		if (!sa->transformed_object) {
1482 			canonical_a = ldns_rr_clone(sa->original_object);
1483 			ldns_rr2canonical(canonical_a);
1484 			sa->transformed_object = ldns_buffer_new(ldns_rr_uncompressed_size(canonical_a));
1485 			if (ldns_rr2buffer_wire(sa->transformed_object, canonical_a, LDNS_SECTION_ANY) != LDNS_STATUS_OK) {
1486 		                ldns_buffer_free((ldns_buffer *)sa->transformed_object);
1487                                 sa->transformed_object = NULL;
1488 				ldns_rr_free(canonical_a);
1489 				return 0;
1490 			}
1491 			ldns_rr_free(canonical_a);
1492 		}
1493 		if (!sb->transformed_object) {
1494 			canonical_b = ldns_rr_clone(sb->original_object);
1495 			ldns_rr2canonical(canonical_b);
1496 			sb->transformed_object = ldns_buffer_new(ldns_rr_uncompressed_size(canonical_b));
1497 			if (ldns_rr2buffer_wire(sb->transformed_object, canonical_b, LDNS_SECTION_ANY) != LDNS_STATUS_OK) {
1498 		                ldns_buffer_free((ldns_buffer *)sa->transformed_object);
1499 		                ldns_buffer_free((ldns_buffer *)sb->transformed_object);
1500                                 sa->transformed_object = NULL;
1501                                 sb->transformed_object = NULL;
1502 				ldns_rr_free(canonical_b);
1503 				return 0;
1504 			}
1505 			ldns_rr_free(canonical_b);
1506 		}
1507 		rr1_buf = (ldns_buffer *) sa->transformed_object;
1508 		rr2_buf = (ldns_buffer *) sb->transformed_object;
1509 
1510 		result = ldns_rr_compare_wire(rr1_buf, rr2_buf);
1511 	}
1512 
1513 	return result;
1514 }
1515 
1516 void
1517 ldns_rr_list_sort(ldns_rr_list *unsorted)
1518 {
1519 	struct ldns_schwartzian_compare_struct **sortables;
1520 	size_t item_count;
1521 	size_t i;
1522 
1523 	if (unsorted) {
1524 		item_count = ldns_rr_list_rr_count(unsorted);
1525 
1526 		sortables = LDNS_XMALLOC(struct ldns_schwartzian_compare_struct *,
1527 					 item_count);
1528                 if(!sortables) return; /* no way to return error */
1529 		for (i = 0; i < item_count; i++) {
1530 			sortables[i] = LDNS_XMALLOC(struct ldns_schwartzian_compare_struct, 1);
1531                         if(!sortables[i]) {
1532                                 /* free the allocated parts */
1533                                 while(i>0) {
1534                                         i--;
1535                                         LDNS_FREE(sortables[i]);
1536                                 }
1537                                 /* no way to return error */
1538 				LDNS_FREE(sortables);
1539                                 return;
1540                         }
1541 			sortables[i]->original_object = ldns_rr_list_rr(unsorted, i);
1542 			sortables[i]->transformed_object = NULL;
1543 		}
1544 		qsort(sortables,
1545 		      item_count,
1546 		      sizeof(struct ldns_schwartzian_compare_struct *),
1547 		      qsort_schwartz_rr_compare);
1548 		for (i = 0; i < item_count; i++) {
1549 			unsorted->_rrs[i] = sortables[i]->original_object;
1550 			if (sortables[i]->transformed_object) {
1551 				ldns_buffer_free(sortables[i]->transformed_object);
1552 			}
1553 			LDNS_FREE(sortables[i]);
1554 		}
1555 		LDNS_FREE(sortables);
1556 	}
1557 }
1558 
1559 int
1560 ldns_rr_compare_no_rdata(const ldns_rr *rr1, const ldns_rr *rr2)
1561 {
1562 	size_t rr1_len;
1563 	size_t rr2_len;
1564         size_t offset;
1565 
1566 	assert(rr1 != NULL);
1567 	assert(rr2 != NULL);
1568 
1569 	rr1_len = ldns_rr_uncompressed_size(rr1);
1570 	rr2_len = ldns_rr_uncompressed_size(rr2);
1571 
1572 	if (ldns_dname_compare(ldns_rr_owner(rr1), ldns_rr_owner(rr2)) < 0) {
1573 		return -1;
1574 	} else if (ldns_dname_compare(ldns_rr_owner(rr1), ldns_rr_owner(rr2)) > 0) {
1575 		return 1;
1576 	}
1577 
1578         /* should return -1 if rr1 comes before rr2, so need to do rr1 - rr2, not rr2 - rr1 */
1579         if (ldns_rr_get_class(rr1) != ldns_rr_get_class(rr2)) {
1580             return ldns_rr_get_class(rr1) - ldns_rr_get_class(rr2);
1581         }
1582 
1583         /* should return -1 if rr1 comes before rr2, so need to do rr1 - rr2, not rr2 - rr1 */
1584         if (ldns_rr_get_type(rr1) != ldns_rr_get_type(rr2)) {
1585             return ldns_rr_get_type(rr1) - ldns_rr_get_type(rr2);
1586         }
1587 
1588         /* offset is the owername length + ttl + type + class + rdlen == start of wire format rdata */
1589         offset = ldns_rdf_size(ldns_rr_owner(rr1)) + 4 + 2 + 2 + 2;
1590         /* if either record doesn't have any RDATA... */
1591         if (offset > rr1_len || offset > rr2_len) {
1592             if (rr1_len == rr2_len) {
1593               return 0;
1594             }
1595             return ((int) rr2_len - (int) rr1_len);
1596         }
1597 
1598 	return 0;
1599 }
1600 
1601 int ldns_rr_compare_wire(const ldns_buffer *rr1_buf, const ldns_buffer *rr2_buf)
1602 {
1603         size_t rr1_len, rr2_len, min_len, i, offset;
1604 
1605         rr1_len = ldns_buffer_capacity(rr1_buf);
1606         rr2_len = ldns_buffer_capacity(rr2_buf);
1607 
1608         /* jump past dname (checked in earlier part)
1609          * and especially past TTL */
1610         offset = 0;
1611         while (offset < rr1_len && *ldns_buffer_at(rr1_buf, offset) != 0) {
1612           offset += *ldns_buffer_at(rr1_buf, offset) + 1;
1613         }
1614         /* jump to rdata section (PAST the rdata length field, otherwise
1615            rrs with different lengths might be sorted erroneously */
1616         offset += 11;
1617 	   min_len = (rr1_len < rr2_len) ? rr1_len : rr2_len;
1618         /* Compare RRs RDATA byte for byte. */
1619         for(i = offset; i < min_len; i++) {
1620                 if (*ldns_buffer_at(rr1_buf,i) < *ldns_buffer_at(rr2_buf,i)) {
1621                         return -1;
1622                 } else if (*ldns_buffer_at(rr1_buf,i) > *ldns_buffer_at(rr2_buf,i)) {
1623                         return +1;
1624                 }
1625         }
1626 
1627         /* If both RDATAs are the same up to min_len, then the shorter one sorts first. */
1628         if (rr1_len < rr2_len) {
1629                 return -1;
1630         } else if (rr1_len > rr2_len) {
1631                 return +1;
1632 	}
1633         /* The RDATAs are equal. */
1634         return 0;
1635 
1636 }
1637 
1638 int
1639 ldns_rr_compare(const ldns_rr *rr1, const ldns_rr *rr2)
1640 {
1641 	int result;
1642 	size_t rr1_len, rr2_len;
1643 
1644 	ldns_buffer *rr1_buf;
1645 	ldns_buffer *rr2_buf;
1646 
1647 	result = ldns_rr_compare_no_rdata(rr1, rr2);
1648 	if (result == 0) {
1649 		rr1_len = ldns_rr_uncompressed_size(rr1);
1650 		rr2_len = ldns_rr_uncompressed_size(rr2);
1651 
1652 		rr1_buf = ldns_buffer_new(rr1_len);
1653 		rr2_buf = ldns_buffer_new(rr2_len);
1654 
1655 		if (ldns_rr2buffer_wire_canonical(rr1_buf,
1656 								    rr1,
1657 								    LDNS_SECTION_ANY)
1658 		    != LDNS_STATUS_OK) {
1659 			ldns_buffer_free(rr1_buf);
1660 			ldns_buffer_free(rr2_buf);
1661 			return 0;
1662 		}
1663 		if (ldns_rr2buffer_wire_canonical(rr2_buf,
1664 								    rr2,
1665 								    LDNS_SECTION_ANY)
1666 		    != LDNS_STATUS_OK) {
1667 			ldns_buffer_free(rr1_buf);
1668 			ldns_buffer_free(rr2_buf);
1669 			return 0;
1670 		}
1671 
1672 		result = ldns_rr_compare_wire(rr1_buf, rr2_buf);
1673 
1674 		ldns_buffer_free(rr1_buf);
1675 		ldns_buffer_free(rr2_buf);
1676 	}
1677 
1678 	return result;
1679 }
1680 
1681 /* convert dnskey to a ds with the given algorithm,
1682  * then compare the result with the given ds */
1683 static int
1684 ldns_rr_compare_ds_dnskey(ldns_rr *ds,
1685                           ldns_rr *dnskey)
1686 {
1687 	ldns_rr *ds_gen;
1688 	bool result = false;
1689 	ldns_hash algo;
1690 
1691 	if (!dnskey || !ds ||
1692 	    ldns_rr_get_type(ds) != LDNS_RR_TYPE_DS ||
1693 	    ldns_rr_get_type(dnskey) != LDNS_RR_TYPE_DNSKEY) {
1694 		return false;
1695 	}
1696 
1697 	if (ldns_rr_rdf(ds, 2) == NULL) {
1698 		return false;
1699 	}
1700 	algo = ldns_rdf2native_int8(ldns_rr_rdf(ds, 2));
1701 
1702 	ds_gen = ldns_key_rr2ds(dnskey, algo);
1703 	if (ds_gen) {
1704 		result = ldns_rr_compare(ds, ds_gen) == 0;
1705 		ldns_rr_free(ds_gen);
1706 	}
1707 	return result;
1708 }
1709 
1710 bool
1711 ldns_rr_compare_ds(const ldns_rr *orr1, const ldns_rr *orr2)
1712 {
1713 	bool result;
1714 	ldns_rr *rr1 = ldns_rr_clone(orr1);
1715 	ldns_rr *rr2 = ldns_rr_clone(orr2);
1716 
1717 	/* set ttls to zero */
1718 	ldns_rr_set_ttl(rr1, 0);
1719 	ldns_rr_set_ttl(rr2, 0);
1720 
1721 	if (ldns_rr_get_type(rr1) == LDNS_RR_TYPE_DS &&
1722 	    ldns_rr_get_type(rr2) == LDNS_RR_TYPE_DNSKEY) {
1723 		result = ldns_rr_compare_ds_dnskey(rr1, rr2);
1724 	} else if (ldns_rr_get_type(rr1) == LDNS_RR_TYPE_DNSKEY &&
1725 	    ldns_rr_get_type(rr2) == LDNS_RR_TYPE_DS) {
1726 		result = ldns_rr_compare_ds_dnskey(rr2, rr1);
1727 	} else {
1728 		result = (ldns_rr_compare(rr1, rr2) == 0);
1729 	}
1730 
1731 	ldns_rr_free(rr1);
1732 	ldns_rr_free(rr2);
1733 
1734 	return result;
1735 }
1736 
1737 int
1738 ldns_rr_list_compare(const ldns_rr_list *rrl1, const ldns_rr_list *rrl2)
1739 {
1740 	size_t i = 0;
1741 	int rr_cmp;
1742 
1743 	assert(rrl1 != NULL);
1744 	assert(rrl2 != NULL);
1745 
1746 	for (i = 0; i < ldns_rr_list_rr_count(rrl1) && i < ldns_rr_list_rr_count(rrl2); i++) {
1747 		rr_cmp = ldns_rr_compare(ldns_rr_list_rr(rrl1, i), ldns_rr_list_rr(rrl2, i));
1748 		if (rr_cmp != 0) {
1749 			return rr_cmp;
1750 		}
1751 	}
1752 
1753 	if (i == ldns_rr_list_rr_count(rrl1) &&
1754 	    i != ldns_rr_list_rr_count(rrl2)) {
1755 		return 1;
1756 	} else if (i == ldns_rr_list_rr_count(rrl2) &&
1757 	           i != ldns_rr_list_rr_count(rrl1)) {
1758 		return -1;
1759 	} else {
1760 		return 0;
1761 	}
1762 }
1763 
1764 size_t
1765 ldns_rr_uncompressed_size(const ldns_rr *r)
1766 {
1767 	size_t rrsize;
1768 	size_t i;
1769 
1770 	rrsize = 0;
1771 	/* add all the rdf sizes */
1772 	for(i = 0; i < ldns_rr_rd_count(r); i++) {
1773 		rrsize += ldns_rdf_size(ldns_rr_rdf(r, i));
1774 	}
1775 	/* ownername */
1776 	rrsize += ldns_rdf_size(ldns_rr_owner(r));
1777 	rrsize += LDNS_RR_OVERHEAD;
1778 	return rrsize;
1779 }
1780 
1781 void
1782 ldns_rr2canonical(ldns_rr *rr)
1783 {
1784 	uint16_t i;
1785 
1786 	if (!rr) {
1787 	  return;
1788         }
1789 
1790         ldns_dname2canonical(ldns_rr_owner(rr));
1791 
1792 	/*
1793 	 * lowercase the rdata dnames if the rr type is one
1794 	 * of the list in chapter 7 of RFC3597
1795 	 * Also added RRSIG, because a "Signer's Name" should be canonicalized
1796 	 * too. See dnssec-bis-updates-16. We can add it to this list because
1797 	 * the "Signer's Name"  is the only dname type rdata field in a RRSIG.
1798 	 */
1799 	switch(ldns_rr_get_type(rr)) {
1800         	case LDNS_RR_TYPE_NS:
1801         	case LDNS_RR_TYPE_MD:
1802         	case LDNS_RR_TYPE_MF:
1803         	case LDNS_RR_TYPE_CNAME:
1804         	case LDNS_RR_TYPE_SOA:
1805         	case LDNS_RR_TYPE_MB:
1806         	case LDNS_RR_TYPE_MG:
1807         	case LDNS_RR_TYPE_MR:
1808         	case LDNS_RR_TYPE_PTR:
1809         	case LDNS_RR_TYPE_MINFO:
1810         	case LDNS_RR_TYPE_MX:
1811         	case LDNS_RR_TYPE_RP:
1812         	case LDNS_RR_TYPE_AFSDB:
1813         	case LDNS_RR_TYPE_RT:
1814         	case LDNS_RR_TYPE_SIG:
1815         	case LDNS_RR_TYPE_PX:
1816         	case LDNS_RR_TYPE_NXT:
1817         	case LDNS_RR_TYPE_NAPTR:
1818         	case LDNS_RR_TYPE_KX:
1819         	case LDNS_RR_TYPE_SRV:
1820         	case LDNS_RR_TYPE_DNAME:
1821         	case LDNS_RR_TYPE_A6:
1822         	case LDNS_RR_TYPE_RRSIG:
1823 			for (i = 0; i < ldns_rr_rd_count(rr); i++) {
1824 				ldns_dname2canonical(ldns_rr_rdf(rr, i));
1825 			}
1826 			return;
1827 		default:
1828 			/* do nothing */
1829 			return;
1830 	}
1831 }
1832 
1833 void
1834 ldns_rr_list2canonical(const ldns_rr_list *rr_list)
1835 {
1836 	size_t i;
1837 	for (i = 0; i < ldns_rr_list_rr_count(rr_list); i++) {
1838 		ldns_rr2canonical(ldns_rr_list_rr(rr_list, i));
1839 	}
1840 }
1841 
1842 uint8_t
1843 ldns_rr_label_count(const ldns_rr *rr)
1844 {
1845 	if (!rr) {
1846 		return 0;
1847 	}
1848 	return ldns_dname_label_count(
1849 			ldns_rr_owner(rr));
1850 }
1851 
1852 /** \cond */
1853 static const ldns_rdf_type type_0_wireformat[] = { LDNS_RDF_TYPE_UNKNOWN };
1854 static const ldns_rdf_type type_a_wireformat[] = { LDNS_RDF_TYPE_A };
1855 static const ldns_rdf_type type_ns_wireformat[] = { LDNS_RDF_TYPE_DNAME };
1856 static const ldns_rdf_type type_md_wireformat[] = { LDNS_RDF_TYPE_DNAME };
1857 static const ldns_rdf_type type_mf_wireformat[] = { LDNS_RDF_TYPE_DNAME };
1858 static const ldns_rdf_type type_cname_wireformat[] = { LDNS_RDF_TYPE_DNAME };
1859 static const ldns_rdf_type type_soa_wireformat[] = {
1860 	LDNS_RDF_TYPE_DNAME, LDNS_RDF_TYPE_DNAME, LDNS_RDF_TYPE_INT32,
1861 	LDNS_RDF_TYPE_PERIOD, LDNS_RDF_TYPE_PERIOD, LDNS_RDF_TYPE_PERIOD,
1862 	LDNS_RDF_TYPE_PERIOD
1863 };
1864 static const ldns_rdf_type type_mb_wireformat[] = { LDNS_RDF_TYPE_DNAME };
1865 static const ldns_rdf_type type_mg_wireformat[] = { LDNS_RDF_TYPE_DNAME };
1866 static const ldns_rdf_type type_mr_wireformat[] = { LDNS_RDF_TYPE_DNAME };
1867 static const ldns_rdf_type type_wks_wireformat[] = {
1868 	LDNS_RDF_TYPE_A, LDNS_RDF_TYPE_WKS
1869 };
1870 static const ldns_rdf_type type_ptr_wireformat[] = { LDNS_RDF_TYPE_DNAME };
1871 static const ldns_rdf_type type_hinfo_wireformat[] = {
1872 	LDNS_RDF_TYPE_STR, LDNS_RDF_TYPE_STR
1873 };
1874 static const ldns_rdf_type type_minfo_wireformat[] = {
1875 	LDNS_RDF_TYPE_DNAME, LDNS_RDF_TYPE_DNAME
1876 };
1877 static const ldns_rdf_type type_mx_wireformat[] = {
1878 	LDNS_RDF_TYPE_INT16, LDNS_RDF_TYPE_DNAME
1879 };
1880 static const ldns_rdf_type type_rp_wireformat[] = {
1881 	LDNS_RDF_TYPE_DNAME, LDNS_RDF_TYPE_DNAME
1882 };
1883 static const ldns_rdf_type type_afsdb_wireformat[] = {
1884 	LDNS_RDF_TYPE_INT16, LDNS_RDF_TYPE_DNAME
1885 };
1886 static const ldns_rdf_type type_x25_wireformat[] = { LDNS_RDF_TYPE_STR };
1887 static const ldns_rdf_type type_isdn_wireformat[] = {
1888 	LDNS_RDF_TYPE_STR, LDNS_RDF_TYPE_STR
1889 };
1890 static const ldns_rdf_type type_rt_wireformat[] = {
1891 	LDNS_RDF_TYPE_INT16, LDNS_RDF_TYPE_DNAME
1892 };
1893 static const ldns_rdf_type type_nsap_wireformat[] = {
1894 	LDNS_RDF_TYPE_NSAP
1895 };
1896 static const ldns_rdf_type type_nsap_ptr_wireformat[] = {
1897 	LDNS_RDF_TYPE_STR
1898 };
1899 static const ldns_rdf_type type_sig_wireformat[] = {
1900 	LDNS_RDF_TYPE_TYPE, LDNS_RDF_TYPE_ALG, LDNS_RDF_TYPE_INT8, LDNS_RDF_TYPE_INT32,
1901 	LDNS_RDF_TYPE_TIME, LDNS_RDF_TYPE_TIME, LDNS_RDF_TYPE_INT16,
1902 	LDNS_RDF_TYPE_DNAME, LDNS_RDF_TYPE_B64
1903 };
1904 static const ldns_rdf_type type_key_wireformat[] = {
1905 	LDNS_RDF_TYPE_INT16, LDNS_RDF_TYPE_INT8, LDNS_RDF_TYPE_INT8, LDNS_RDF_TYPE_B64
1906 };
1907 static const ldns_rdf_type type_px_wireformat[] = {
1908 	LDNS_RDF_TYPE_INT16, LDNS_RDF_TYPE_DNAME, LDNS_RDF_TYPE_DNAME
1909 };
1910 static const ldns_rdf_type type_gpos_wireformat[] = {
1911 	LDNS_RDF_TYPE_STR, LDNS_RDF_TYPE_STR, LDNS_RDF_TYPE_STR
1912 };
1913 static const ldns_rdf_type type_aaaa_wireformat[] = { LDNS_RDF_TYPE_AAAA };
1914 static const ldns_rdf_type type_loc_wireformat[] = { LDNS_RDF_TYPE_LOC };
1915 static const ldns_rdf_type type_nxt_wireformat[] = {
1916 	LDNS_RDF_TYPE_DNAME, LDNS_RDF_TYPE_UNKNOWN
1917 };
1918 static const ldns_rdf_type type_eid_wireformat[] = {
1919 	LDNS_RDF_TYPE_HEX
1920 };
1921 static const ldns_rdf_type type_nimloc_wireformat[] = {
1922 	LDNS_RDF_TYPE_HEX
1923 };
1924 static const ldns_rdf_type type_srv_wireformat[] = {
1925 	LDNS_RDF_TYPE_INT16, LDNS_RDF_TYPE_INT16, LDNS_RDF_TYPE_INT16, LDNS_RDF_TYPE_DNAME
1926 };
1927 static const ldns_rdf_type type_atma_wireformat[] = {
1928 	LDNS_RDF_TYPE_ATMA
1929 };
1930 static const ldns_rdf_type type_naptr_wireformat[] = {
1931 	LDNS_RDF_TYPE_INT16, LDNS_RDF_TYPE_INT16, LDNS_RDF_TYPE_STR, LDNS_RDF_TYPE_STR, LDNS_RDF_TYPE_STR, LDNS_RDF_TYPE_DNAME
1932 };
1933 static const ldns_rdf_type type_kx_wireformat[] = {
1934 	LDNS_RDF_TYPE_INT16, LDNS_RDF_TYPE_DNAME
1935 };
1936 static const ldns_rdf_type type_cert_wireformat[] = {
1937 	 LDNS_RDF_TYPE_CERT_ALG, LDNS_RDF_TYPE_INT16, LDNS_RDF_TYPE_ALG, LDNS_RDF_TYPE_B64
1938 };
1939 static const ldns_rdf_type type_a6_wireformat[] = { LDNS_RDF_TYPE_UNKNOWN };
1940 static const ldns_rdf_type type_dname_wireformat[] = { LDNS_RDF_TYPE_DNAME };
1941 static const ldns_rdf_type type_sink_wireformat[] = { LDNS_RDF_TYPE_INT8,
1942 	LDNS_RDF_TYPE_INT8, LDNS_RDF_TYPE_INT8, LDNS_RDF_TYPE_B64
1943 };
1944 static const ldns_rdf_type type_apl_wireformat[] = {
1945 	LDNS_RDF_TYPE_APL
1946 };
1947 static const ldns_rdf_type type_ds_wireformat[] = {
1948 	LDNS_RDF_TYPE_INT16, LDNS_RDF_TYPE_ALG, LDNS_RDF_TYPE_INT8, LDNS_RDF_TYPE_HEX
1949 };
1950 static const ldns_rdf_type type_sshfp_wireformat[] = {
1951 	LDNS_RDF_TYPE_INT8, LDNS_RDF_TYPE_INT8, LDNS_RDF_TYPE_HEX
1952 };
1953 static const ldns_rdf_type type_ipseckey_wireformat[] = {
1954 	LDNS_RDF_TYPE_IPSECKEY
1955 };
1956 static const ldns_rdf_type type_rrsig_wireformat[] = {
1957 	LDNS_RDF_TYPE_TYPE, LDNS_RDF_TYPE_ALG, LDNS_RDF_TYPE_INT8, LDNS_RDF_TYPE_INT32,
1958 	LDNS_RDF_TYPE_TIME, LDNS_RDF_TYPE_TIME, LDNS_RDF_TYPE_INT16, LDNS_RDF_TYPE_DNAME, LDNS_RDF_TYPE_B64
1959 };
1960 static const ldns_rdf_type type_nsec_wireformat[] = {
1961 	LDNS_RDF_TYPE_DNAME, LDNS_RDF_TYPE_NSEC
1962 };
1963 static const ldns_rdf_type type_dhcid_wireformat[] = {
1964 	LDNS_RDF_TYPE_B64
1965 };
1966 static const ldns_rdf_type type_talink_wireformat[] = {
1967 	LDNS_RDF_TYPE_DNAME, LDNS_RDF_TYPE_DNAME
1968 };
1969 #ifdef RRTYPE_OPENPGPKEY
1970 static const ldns_rdf_type type_openpgpkey_wireformat[] = {
1971 	LDNS_RDF_TYPE_B64
1972 };
1973 #endif
1974 static const ldns_rdf_type type_csync_wireformat[] = {
1975 	LDNS_RDF_TYPE_INT32, LDNS_RDF_TYPE_INT16, LDNS_RDF_TYPE_NSEC
1976 };
1977 static const ldns_rdf_type type_zonemd_wireformat[] = {
1978 	LDNS_RDF_TYPE_INT32,
1979 	LDNS_RDF_TYPE_INT8, LDNS_RDF_TYPE_INT8, LDNS_RDF_TYPE_HEX
1980 };
1981 #ifdef RRTYPE_SVCB_HTTPS
1982 static const ldns_rdf_type type_svcb_wireformat[] = {
1983 	LDNS_RDF_TYPE_INT16,
1984 	LDNS_RDF_TYPE_DNAME,
1985 	LDNS_RDF_TYPE_SVCPARAMS
1986 };
1987 #endif
1988 /* nsec3 is some vars, followed by same type of data of nsec */
1989 static const ldns_rdf_type type_nsec3_wireformat[] = {
1990 /*	LDNS_RDF_TYPE_NSEC3_VARS, LDNS_RDF_TYPE_NSEC3_NEXT_OWNER, LDNS_RDF_TYPE_NSEC*/
1991 	LDNS_RDF_TYPE_INT8, LDNS_RDF_TYPE_INT8, LDNS_RDF_TYPE_INT16, LDNS_RDF_TYPE_NSEC3_SALT, LDNS_RDF_TYPE_NSEC3_NEXT_OWNER, LDNS_RDF_TYPE_NSEC
1992 };
1993 
1994 static const ldns_rdf_type type_nsec3param_wireformat[] = {
1995 /*	LDNS_RDF_TYPE_NSEC3_PARAMS_VARS*/
1996 	LDNS_RDF_TYPE_INT8,
1997 	LDNS_RDF_TYPE_INT8,
1998 	LDNS_RDF_TYPE_INT16,
1999 	LDNS_RDF_TYPE_NSEC3_SALT
2000 };
2001 
2002 static const ldns_rdf_type type_dnskey_wireformat[] = {
2003 	LDNS_RDF_TYPE_INT16,
2004 	LDNS_RDF_TYPE_INT8,
2005 	LDNS_RDF_TYPE_ALG,
2006 	LDNS_RDF_TYPE_B64
2007 };
2008 static const ldns_rdf_type type_tkey_wireformat[] = {
2009 	LDNS_RDF_TYPE_DNAME,
2010 	LDNS_RDF_TYPE_TIME,
2011 	LDNS_RDF_TYPE_TIME,
2012 	LDNS_RDF_TYPE_INT16,
2013 	LDNS_RDF_TYPE_INT16,
2014 	LDNS_RDF_TYPE_INT16_DATA,
2015 	LDNS_RDF_TYPE_INT16_DATA,
2016 };
2017 static const ldns_rdf_type type_tsig_wireformat[] = {
2018 	LDNS_RDF_TYPE_DNAME,
2019 	LDNS_RDF_TYPE_TSIGTIME,
2020 	LDNS_RDF_TYPE_INT16,
2021 	LDNS_RDF_TYPE_INT16_DATA,
2022 	LDNS_RDF_TYPE_INT16,
2023 	LDNS_RDF_TYPE_INT16,
2024 	LDNS_RDF_TYPE_INT16_DATA
2025 };
2026 static const ldns_rdf_type type_tlsa_wireformat[] = {
2027 	LDNS_RDF_TYPE_CERTIFICATE_USAGE,
2028 	LDNS_RDF_TYPE_SELECTOR,
2029 	LDNS_RDF_TYPE_MATCHING_TYPE,
2030 	LDNS_RDF_TYPE_HEX
2031 };
2032 static const ldns_rdf_type type_hip_wireformat[] = {
2033 	LDNS_RDF_TYPE_HIP
2034 };
2035 static const ldns_rdf_type type_nid_wireformat[] = {
2036 	LDNS_RDF_TYPE_INT16,
2037 	LDNS_RDF_TYPE_ILNP64
2038 };
2039 static const ldns_rdf_type type_l32_wireformat[] = {
2040 	LDNS_RDF_TYPE_INT16,
2041 	LDNS_RDF_TYPE_A
2042 };
2043 static const ldns_rdf_type type_l64_wireformat[] = {
2044 	LDNS_RDF_TYPE_INT16,
2045 	LDNS_RDF_TYPE_ILNP64
2046 };
2047 static const ldns_rdf_type type_lp_wireformat[] = {
2048 	LDNS_RDF_TYPE_INT16,
2049 	LDNS_RDF_TYPE_DNAME
2050 };
2051 static const ldns_rdf_type type_eui48_wireformat[] = {
2052 	LDNS_RDF_TYPE_EUI48
2053 };
2054 static const ldns_rdf_type type_eui64_wireformat[] = {
2055 	LDNS_RDF_TYPE_EUI64
2056 };
2057 static const ldns_rdf_type type_uri_wireformat[] = {
2058 	LDNS_RDF_TYPE_INT16,
2059 	LDNS_RDF_TYPE_INT16,
2060 	LDNS_RDF_TYPE_LONG_STR
2061 };
2062 static const ldns_rdf_type type_caa_wireformat[] = {
2063 	LDNS_RDF_TYPE_INT8,
2064 	LDNS_RDF_TYPE_TAG,
2065 	LDNS_RDF_TYPE_LONG_STR
2066 };
2067 #ifdef RRTYPE_DOA
2068 static const ldns_rdf_type type_doa_wireformat[] = {
2069 	LDNS_RDF_TYPE_INT32,
2070 	LDNS_RDF_TYPE_INT32,
2071 	LDNS_RDF_TYPE_INT8,
2072 	LDNS_RDF_TYPE_STR,
2073 	LDNS_RDF_TYPE_B64
2074 };
2075 #endif
2076 #ifdef RRTYPE_AMTRELAY
2077 static const ldns_rdf_type type_amtrelay_wireformat[] = {
2078 	LDNS_RDF_TYPE_AMTRELAY
2079 };
2080 #endif
2081 
2082 
2083 /** \endcond */
2084 
2085 /** \cond */
2086 /* All RR's defined in 1035 are well known and can thus
2087  * be compressed. See RFC3597. These RR's are:
2088  * CNAME HINFO MB MD MF MG MINFO MR MX NULL NS PTR SOA TXT
2089  */
2090 static ldns_rr_descriptor rdata_field_descriptors[] = {
2091 	/* 0 */
2092 	{ 0, NULL, 0, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2093 	/* 1 */
2094 	{LDNS_RR_TYPE_A, "A", 1, 1, type_a_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2095 	/* 2 */
2096 	{LDNS_RR_TYPE_NS, "NS", 1, 1, type_ns_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_COMPRESS, 1 },
2097 	/* 3 */
2098 	{LDNS_RR_TYPE_MD, "MD", 1, 1, type_md_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_COMPRESS, 1 },
2099 	/* 4 */
2100 	{LDNS_RR_TYPE_MF, "MF", 1, 1, type_mf_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_COMPRESS, 1 },
2101 	/* 5 */
2102 	{LDNS_RR_TYPE_CNAME, "CNAME", 1, 1, type_cname_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_COMPRESS, 1 },
2103 	/* 6 */
2104 	{LDNS_RR_TYPE_SOA, "SOA", 7, 7, type_soa_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_COMPRESS, 2 },
2105 	/* 7 */
2106 	{LDNS_RR_TYPE_MB, "MB", 1, 1, type_mb_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_COMPRESS, 1 },
2107 	/* 8 */
2108 	{LDNS_RR_TYPE_MG, "MG", 1, 1, type_mg_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_COMPRESS, 1 },
2109 	/* 9 */
2110 	{LDNS_RR_TYPE_MR, "MR", 1, 1, type_mr_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_COMPRESS, 1 },
2111 	/* 10 */
2112 	{LDNS_RR_TYPE_NULL, "NULL", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2113 	/* 11 */
2114 	{LDNS_RR_TYPE_WKS, "WKS", 2, 2, type_wks_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2115 	/* 12 */
2116 	{LDNS_RR_TYPE_PTR, "PTR", 1, 1, type_ptr_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_COMPRESS, 1 },
2117 	/* 13 */
2118 	{LDNS_RR_TYPE_HINFO, "HINFO", 2, 2, type_hinfo_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2119 	/* 14 */
2120 	{LDNS_RR_TYPE_MINFO, "MINFO", 2, 2, type_minfo_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_COMPRESS, 2 },
2121 	/* 15 */
2122 	{LDNS_RR_TYPE_MX, "MX", 2, 2, type_mx_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_COMPRESS, 1 },
2123 	/* 16 */
2124 	{LDNS_RR_TYPE_TXT, "TXT", 1, 0, NULL, LDNS_RDF_TYPE_STR, LDNS_RR_NO_COMPRESS, 0 },
2125 	/* 17 */
2126 	{LDNS_RR_TYPE_RP, "RP", 2, 2, type_rp_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 2 },
2127 	/* 18 */
2128 	{LDNS_RR_TYPE_AFSDB, "AFSDB", 2, 2, type_afsdb_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 1 },
2129 	/* 19 */
2130 	{LDNS_RR_TYPE_X25, "X25", 1, 1, type_x25_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2131 	/* 20 */
2132 	{LDNS_RR_TYPE_ISDN, "ISDN", 1, 2, type_isdn_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2133 	/* 21 */
2134 	{LDNS_RR_TYPE_RT, "RT", 2, 2, type_rt_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 1 },
2135 	/* 22 */
2136 	{LDNS_RR_TYPE_NSAP, "NSAP", 1, 1, type_nsap_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2137 	/* 23 */
2138 	{LDNS_RR_TYPE_NSAP_PTR, "NSAP-PTR", 1, 1, type_nsap_ptr_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2139 	/* 24 */
2140 	{LDNS_RR_TYPE_SIG, "SIG", 9, 9, type_sig_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 1 },
2141 	/* 25 */
2142 	{LDNS_RR_TYPE_KEY, "KEY", 4, 4, type_key_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2143 	/* 26 */
2144 	{LDNS_RR_TYPE_PX, "PX", 3, 3, type_px_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 2 },
2145 	/* 27 */
2146 	{LDNS_RR_TYPE_GPOS, "GPOS", 3, 3, type_gpos_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2147 	/* 28 */
2148 	{LDNS_RR_TYPE_AAAA, "AAAA", 1, 1, type_aaaa_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2149 	/* 29 */
2150 	{LDNS_RR_TYPE_LOC, "LOC", 1, 1, type_loc_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2151 	/* 30 */
2152 	{LDNS_RR_TYPE_NXT, "NXT", 2, 2, type_nxt_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 1 },
2153 	/* 31 */
2154 	{LDNS_RR_TYPE_EID, "EID", 1, 1, type_eid_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2155 	/* 32 */
2156 	{LDNS_RR_TYPE_NIMLOC, "NIMLOC", 1, 1, type_nimloc_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2157 	/* 33 */
2158 	{LDNS_RR_TYPE_SRV, "SRV", 4, 4, type_srv_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 1 },
2159 	/* 34 */
2160 	{LDNS_RR_TYPE_ATMA, "ATMA", 1, 1, type_atma_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2161 	/* 35 */
2162 	{LDNS_RR_TYPE_NAPTR, "NAPTR", 6, 6, type_naptr_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 1 },
2163 	/* 36 */
2164 	{LDNS_RR_TYPE_KX, "KX", 2, 2, type_kx_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 1 },
2165 	/* 37 */
2166 	{LDNS_RR_TYPE_CERT, "CERT", 4, 4, type_cert_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2167 	/* 38 */
2168 	{LDNS_RR_TYPE_A6, "A6", 1, 1, type_a6_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2169 	/* 39 */
2170 	{LDNS_RR_TYPE_DNAME, "DNAME", 1, 1, type_dname_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 1 },
2171 	/* 40 */
2172 	{LDNS_RR_TYPE_SINK, "SINK", 1, 1, type_sink_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2173 	/* 41 */
2174 	{LDNS_RR_TYPE_OPT, "OPT", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2175 	/* 42 */
2176 	{LDNS_RR_TYPE_APL, "APL", 0, 0, type_apl_wireformat, LDNS_RDF_TYPE_APL, LDNS_RR_NO_COMPRESS, 0 },
2177 	/* 43 */
2178 	{LDNS_RR_TYPE_DS, "DS", 4, 4, type_ds_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2179 	/* 44 */
2180 	{LDNS_RR_TYPE_SSHFP, "SSHFP", 3, 3, type_sshfp_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2181 	/* 45 */
2182 	{LDNS_RR_TYPE_IPSECKEY, "IPSECKEY", 1, 1, type_ipseckey_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2183 	/* 46 */
2184 	{LDNS_RR_TYPE_RRSIG, "RRSIG", 9, 9, type_rrsig_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 1 },
2185 	/* 47 */
2186 	{LDNS_RR_TYPE_NSEC, "NSEC", 1, 2, type_nsec_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 1 },
2187 	/* 48 */
2188 	{LDNS_RR_TYPE_DNSKEY, "DNSKEY", 4, 4, type_dnskey_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2189 	/* 49 */
2190 	{LDNS_RR_TYPE_DHCID, "DHCID", 1, 1, type_dhcid_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2191 	/* 50 */
2192 	{LDNS_RR_TYPE_NSEC3, "NSEC3", 5, 6, type_nsec3_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2193 	/* 51 */
2194 	{LDNS_RR_TYPE_NSEC3PARAM, "NSEC3PARAM", 4, 4, type_nsec3param_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2195 	/* 52 */
2196 	{LDNS_RR_TYPE_TLSA, "TLSA", 4, 4, type_tlsa_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2197 
2198 	{LDNS_RR_TYPE_SMIMEA, "SMIMEA", 4, 4, type_tlsa_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2199 {LDNS_RR_TYPE_NULL, "TYPE54", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2200 
2201 	/* 55
2202 	 * Hip ends with 0 or more Rendezvous Servers represented as dname's.
2203 	 * Hence the LDNS_RDF_TYPE_DNAME _variable field and the _maximum field
2204 	 * set to 0.
2205 	 */
2206 	{LDNS_RR_TYPE_HIP, "HIP", 1, 1, type_hip_wireformat, LDNS_RDF_TYPE_DNAME, LDNS_RR_NO_COMPRESS, 0 },
2207 
2208 #ifdef RRTYPE_NINFO
2209 	/* 56 */
2210 	{LDNS_RR_TYPE_NINFO, "NINFO", 1, 0, NULL, LDNS_RDF_TYPE_STR, LDNS_RR_NO_COMPRESS, 0 },
2211 #else
2212 {LDNS_RR_TYPE_NULL, "TYPE56", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2213 #endif
2214 #ifdef RRTYPE_RKEY
2215 	/* 57 */
2216 	{LDNS_RR_TYPE_RKEY, "RKEY", 4, 4, type_key_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2217 #else
2218 {LDNS_RR_TYPE_NULL, "TYPE57", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2219 #endif
2220 	/* 58 */
2221 	{LDNS_RR_TYPE_TALINK, "TALINK", 2, 2, type_talink_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 2 },
2222 
2223 	/* 59 */
2224 	{LDNS_RR_TYPE_CDS, "CDS", 4, 4, type_ds_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2225 	/* 60 */
2226 	{LDNS_RR_TYPE_CDNSKEY, "CDNSKEY", 4, 4, type_dnskey_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2227 
2228 #ifdef RRTYPE_OPENPGPKEY
2229 	/* 61 */
2230 	{LDNS_RR_TYPE_OPENPGPKEY, "OPENPGPKEY", 1, 1, type_openpgpkey_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2231 #else
2232 {LDNS_RR_TYPE_NULL, "TYPE61", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2233 #endif
2234 	/* 62 */
2235 	{LDNS_RR_TYPE_CSYNC, "CSYNC", 3, 3, type_csync_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2236 	/* 63 */
2237 	{LDNS_RR_TYPE_ZONEMD, "ZONEMD", 4, 4, type_zonemd_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2238 #ifdef RRTYPE_SVCB_HTTPS
2239 	/* 64 */
2240 	{LDNS_RR_TYPE_SVCB, "SVCB", 2, 3, type_svcb_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 1 },
2241 	/* 65 */
2242 	{LDNS_RR_TYPE_HTTPS, "HTTPS", 2, 3, type_svcb_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 1 },
2243 
2244 #else
2245 {LDNS_RR_TYPE_NULL, "TYPE64", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2246 {LDNS_RR_TYPE_NULL, "TYPE65", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2247 #endif
2248 {LDNS_RR_TYPE_NULL, "TYPE66", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2249 {LDNS_RR_TYPE_NULL, "TYPE67", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2250 {LDNS_RR_TYPE_NULL, "TYPE68", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2251 {LDNS_RR_TYPE_NULL, "TYPE69", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2252 {LDNS_RR_TYPE_NULL, "TYPE70", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2253 {LDNS_RR_TYPE_NULL, "TYPE71", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2254 {LDNS_RR_TYPE_NULL, "TYPE72", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2255 {LDNS_RR_TYPE_NULL, "TYPE73", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2256 {LDNS_RR_TYPE_NULL, "TYPE74", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2257 {LDNS_RR_TYPE_NULL, "TYPE75", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2258 {LDNS_RR_TYPE_NULL, "TYPE76", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2259 {LDNS_RR_TYPE_NULL, "TYPE77", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2260 {LDNS_RR_TYPE_NULL, "TYPE78", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2261 {LDNS_RR_TYPE_NULL, "TYPE79", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2262 {LDNS_RR_TYPE_NULL, "TYPE80", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2263 {LDNS_RR_TYPE_NULL, "TYPE81", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2264 {LDNS_RR_TYPE_NULL, "TYPE82", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2265 {LDNS_RR_TYPE_NULL, "TYPE83", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2266 {LDNS_RR_TYPE_NULL, "TYPE84", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2267 {LDNS_RR_TYPE_NULL, "TYPE85", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2268 {LDNS_RR_TYPE_NULL, "TYPE86", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2269 {LDNS_RR_TYPE_NULL, "TYPE87", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2270 {LDNS_RR_TYPE_NULL, "TYPE88", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2271 {LDNS_RR_TYPE_NULL, "TYPE89", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2272 {LDNS_RR_TYPE_NULL, "TYPE90", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2273 {LDNS_RR_TYPE_NULL, "TYPE91", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2274 {LDNS_RR_TYPE_NULL, "TYPE92", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2275 {LDNS_RR_TYPE_NULL, "TYPE93", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2276 {LDNS_RR_TYPE_NULL, "TYPE94", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2277 {LDNS_RR_TYPE_NULL, "TYPE95", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2278 {LDNS_RR_TYPE_NULL, "TYPE96", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2279 {LDNS_RR_TYPE_NULL, "TYPE97", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2280 {LDNS_RR_TYPE_NULL, "TYPE98", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2281 
2282 	/* 99 */
2283 	{LDNS_RR_TYPE_SPF,  "SPF", 1, 0, NULL, LDNS_RDF_TYPE_STR, LDNS_RR_NO_COMPRESS, 0 },
2284 
2285 	/* UINFO  [IANA-Reserved] */
2286 {LDNS_RR_TYPE_NULL, "TYPE100", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2287 	/* UID    [IANA-Reserved] */
2288 {LDNS_RR_TYPE_NULL, "TYPE101", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2289 	/* GID    [IANA-Reserved] */
2290 {LDNS_RR_TYPE_NULL, "TYPE102", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2291 	/* UNSPEC [IANA-Reserved] */
2292 {LDNS_RR_TYPE_NULL, "TYPE103", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2293 
2294 	/* 104 */
2295 	{LDNS_RR_TYPE_NID, "NID", 2, 2, type_nid_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2296 	/* 105 */
2297 	{LDNS_RR_TYPE_L32, "L32", 2, 2, type_l32_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2298 	/* 106 */
2299 	{LDNS_RR_TYPE_L64, "L64", 2, 2, type_l64_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2300 	/* 107 */
2301 	{LDNS_RR_TYPE_LP, "LP", 2, 2, type_lp_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 1 },
2302 	/* 108 */
2303 	{LDNS_RR_TYPE_EUI48, "EUI48", 1, 1, type_eui48_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2304 	/* 109 */
2305 	{LDNS_RR_TYPE_EUI64, "EUI64", 1, 1, type_eui64_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2306 
2307 {LDNS_RR_TYPE_NULL, "TYPE110", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2308 {LDNS_RR_TYPE_NULL, "TYPE111", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2309 {LDNS_RR_TYPE_NULL, "TYPE112", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2310 {LDNS_RR_TYPE_NULL, "TYPE113", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2311 {LDNS_RR_TYPE_NULL, "TYPE114", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2312 {LDNS_RR_TYPE_NULL, "TYPE115", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2313 {LDNS_RR_TYPE_NULL, "TYPE116", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2314 {LDNS_RR_TYPE_NULL, "TYPE117", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2315 {LDNS_RR_TYPE_NULL, "TYPE118", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2316 {LDNS_RR_TYPE_NULL, "TYPE119", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2317 {LDNS_RR_TYPE_NULL, "TYPE120", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2318 {LDNS_RR_TYPE_NULL, "TYPE121", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2319 {LDNS_RR_TYPE_NULL, "TYPE122", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2320 {LDNS_RR_TYPE_NULL, "TYPE123", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2321 {LDNS_RR_TYPE_NULL, "TYPE124", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2322 {LDNS_RR_TYPE_NULL, "TYPE125", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2323 {LDNS_RR_TYPE_NULL, "TYPE126", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2324 {LDNS_RR_TYPE_NULL, "TYPE127", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2325 {LDNS_RR_TYPE_NULL, "TYPE128", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2326 {LDNS_RR_TYPE_NULL, "TYPE129", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2327 {LDNS_RR_TYPE_NULL, "TYPE130", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2328 {LDNS_RR_TYPE_NULL, "TYPE131", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2329 {LDNS_RR_TYPE_NULL, "TYPE132", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2330 {LDNS_RR_TYPE_NULL, "TYPE133", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2331 {LDNS_RR_TYPE_NULL, "TYPE134", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2332 {LDNS_RR_TYPE_NULL, "TYPE135", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2333 {LDNS_RR_TYPE_NULL, "TYPE136", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2334 {LDNS_RR_TYPE_NULL, "TYPE137", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2335 {LDNS_RR_TYPE_NULL, "TYPE138", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2336 {LDNS_RR_TYPE_NULL, "TYPE139", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2337 {LDNS_RR_TYPE_NULL, "TYPE140", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2338 {LDNS_RR_TYPE_NULL, "TYPE141", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2339 {LDNS_RR_TYPE_NULL, "TYPE142", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2340 {LDNS_RR_TYPE_NULL, "TYPE143", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2341 {LDNS_RR_TYPE_NULL, "TYPE144", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2342 {LDNS_RR_TYPE_NULL, "TYPE145", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2343 {LDNS_RR_TYPE_NULL, "TYPE146", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2344 {LDNS_RR_TYPE_NULL, "TYPE147", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2345 {LDNS_RR_TYPE_NULL, "TYPE148", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2346 {LDNS_RR_TYPE_NULL, "TYPE149", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2347 {LDNS_RR_TYPE_NULL, "TYPE150", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2348 {LDNS_RR_TYPE_NULL, "TYPE151", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2349 {LDNS_RR_TYPE_NULL, "TYPE152", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2350 {LDNS_RR_TYPE_NULL, "TYPE153", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2351 {LDNS_RR_TYPE_NULL, "TYPE154", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2352 {LDNS_RR_TYPE_NULL, "TYPE155", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2353 {LDNS_RR_TYPE_NULL, "TYPE156", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2354 {LDNS_RR_TYPE_NULL, "TYPE157", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2355 {LDNS_RR_TYPE_NULL, "TYPE158", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2356 {LDNS_RR_TYPE_NULL, "TYPE159", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2357 {LDNS_RR_TYPE_NULL, "TYPE160", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2358 {LDNS_RR_TYPE_NULL, "TYPE161", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2359 {LDNS_RR_TYPE_NULL, "TYPE162", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2360 {LDNS_RR_TYPE_NULL, "TYPE163", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2361 {LDNS_RR_TYPE_NULL, "TYPE164", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2362 {LDNS_RR_TYPE_NULL, "TYPE165", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2363 {LDNS_RR_TYPE_NULL, "TYPE166", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2364 {LDNS_RR_TYPE_NULL, "TYPE167", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2365 {LDNS_RR_TYPE_NULL, "TYPE168", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2366 {LDNS_RR_TYPE_NULL, "TYPE169", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2367 {LDNS_RR_TYPE_NULL, "TYPE170", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2368 {LDNS_RR_TYPE_NULL, "TYPE171", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2369 {LDNS_RR_TYPE_NULL, "TYPE172", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2370 {LDNS_RR_TYPE_NULL, "TYPE173", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2371 {LDNS_RR_TYPE_NULL, "TYPE174", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2372 {LDNS_RR_TYPE_NULL, "TYPE175", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2373 {LDNS_RR_TYPE_NULL, "TYPE176", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2374 {LDNS_RR_TYPE_NULL, "TYPE177", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2375 {LDNS_RR_TYPE_NULL, "TYPE178", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2376 {LDNS_RR_TYPE_NULL, "TYPE179", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2377 {LDNS_RR_TYPE_NULL, "TYPE180", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2378 {LDNS_RR_TYPE_NULL, "TYPE181", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2379 {LDNS_RR_TYPE_NULL, "TYPE182", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2380 {LDNS_RR_TYPE_NULL, "TYPE183", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2381 {LDNS_RR_TYPE_NULL, "TYPE184", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2382 {LDNS_RR_TYPE_NULL, "TYPE185", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2383 {LDNS_RR_TYPE_NULL, "TYPE186", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2384 {LDNS_RR_TYPE_NULL, "TYPE187", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2385 {LDNS_RR_TYPE_NULL, "TYPE188", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2386 {LDNS_RR_TYPE_NULL, "TYPE189", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2387 {LDNS_RR_TYPE_NULL, "TYPE190", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2388 {LDNS_RR_TYPE_NULL, "TYPE191", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2389 {LDNS_RR_TYPE_NULL, "TYPE192", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2390 {LDNS_RR_TYPE_NULL, "TYPE193", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2391 {LDNS_RR_TYPE_NULL, "TYPE194", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2392 {LDNS_RR_TYPE_NULL, "TYPE195", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2393 {LDNS_RR_TYPE_NULL, "TYPE196", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2394 {LDNS_RR_TYPE_NULL, "TYPE197", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2395 {LDNS_RR_TYPE_NULL, "TYPE198", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2396 {LDNS_RR_TYPE_NULL, "TYPE199", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2397 {LDNS_RR_TYPE_NULL, "TYPE200", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2398 {LDNS_RR_TYPE_NULL, "TYPE201", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2399 {LDNS_RR_TYPE_NULL, "TYPE202", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2400 {LDNS_RR_TYPE_NULL, "TYPE203", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2401 {LDNS_RR_TYPE_NULL, "TYPE204", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2402 {LDNS_RR_TYPE_NULL, "TYPE205", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2403 {LDNS_RR_TYPE_NULL, "TYPE206", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2404 {LDNS_RR_TYPE_NULL, "TYPE207", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2405 {LDNS_RR_TYPE_NULL, "TYPE208", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2406 {LDNS_RR_TYPE_NULL, "TYPE209", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2407 {LDNS_RR_TYPE_NULL, "TYPE210", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2408 {LDNS_RR_TYPE_NULL, "TYPE211", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2409 {LDNS_RR_TYPE_NULL, "TYPE212", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2410 {LDNS_RR_TYPE_NULL, "TYPE213", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2411 {LDNS_RR_TYPE_NULL, "TYPE214", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2412 {LDNS_RR_TYPE_NULL, "TYPE215", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2413 {LDNS_RR_TYPE_NULL, "TYPE216", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2414 {LDNS_RR_TYPE_NULL, "TYPE217", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2415 {LDNS_RR_TYPE_NULL, "TYPE218", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2416 {LDNS_RR_TYPE_NULL, "TYPE219", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2417 {LDNS_RR_TYPE_NULL, "TYPE220", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2418 {LDNS_RR_TYPE_NULL, "TYPE221", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2419 {LDNS_RR_TYPE_NULL, "TYPE222", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2420 {LDNS_RR_TYPE_NULL, "TYPE223", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2421 {LDNS_RR_TYPE_NULL, "TYPE224", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2422 {LDNS_RR_TYPE_NULL, "TYPE225", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2423 {LDNS_RR_TYPE_NULL, "TYPE226", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2424 {LDNS_RR_TYPE_NULL, "TYPE227", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2425 {LDNS_RR_TYPE_NULL, "TYPE228", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2426 {LDNS_RR_TYPE_NULL, "TYPE229", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2427 {LDNS_RR_TYPE_NULL, "TYPE230", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2428 {LDNS_RR_TYPE_NULL, "TYPE231", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2429 {LDNS_RR_TYPE_NULL, "TYPE232", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2430 {LDNS_RR_TYPE_NULL, "TYPE233", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2431 {LDNS_RR_TYPE_NULL, "TYPE234", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2432 {LDNS_RR_TYPE_NULL, "TYPE235", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2433 {LDNS_RR_TYPE_NULL, "TYPE236", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2434 {LDNS_RR_TYPE_NULL, "TYPE237", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2435 {LDNS_RR_TYPE_NULL, "TYPE238", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2436 {LDNS_RR_TYPE_NULL, "TYPE239", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2437 {LDNS_RR_TYPE_NULL, "TYPE240", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2438 {LDNS_RR_TYPE_NULL, "TYPE241", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2439 {LDNS_RR_TYPE_NULL, "TYPE242", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2440 {LDNS_RR_TYPE_NULL, "TYPE243", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2441 {LDNS_RR_TYPE_NULL, "TYPE244", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2442 {LDNS_RR_TYPE_NULL, "TYPE245", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2443 {LDNS_RR_TYPE_NULL, "TYPE246", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2444 {LDNS_RR_TYPE_NULL, "TYPE247", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2445 {LDNS_RR_TYPE_NULL, "TYPE248", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2446 
2447 	/* LDNS_RDF_TYPE_INT16_DATA takes two fields (length and data) as one.
2448 	 * So, unlike RFC 2930 spec, we have 7 min/max rdf's i.s.o. 8/9.
2449 	 */
2450 	/* 249 */
2451 	{LDNS_RR_TYPE_TKEY, "TKEY", 7, 7, type_tkey_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 1 },
2452 	/* LDNS_RDF_TYPE_INT16_DATA takes two fields (length and data) as one.
2453 	 * So, unlike RFC 2930 spec, we have 7 min/max rdf's i.s.o. 8/9.
2454 	 */
2455 	/* 250 */
2456 	{LDNS_RR_TYPE_TSIG, "TSIG", 7, 7, type_tsig_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 1 },
2457 
2458 	/* IXFR: A request for a transfer of an incremental zone transfer */
2459 {LDNS_RR_TYPE_NULL, "TYPE251", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2460 	/* AXFR: A request for a transfer of an entire zone */
2461 {LDNS_RR_TYPE_NULL, "TYPE252", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2462 	/* MAILB: A request for mailbox-related records (MB, MG or MR) */
2463 {LDNS_RR_TYPE_NULL, "TYPE253", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2464 	/* MAILA: A request for mail agent RRs (Obsolete - see MX) */
2465 {LDNS_RR_TYPE_NULL, "TYPE254", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2466 	/* ANY: A request for all (available) records */
2467 {LDNS_RR_TYPE_NULL, "TYPE255", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2468 
2469 	/* 256 */
2470 	{LDNS_RR_TYPE_URI, "URI", 3, 3, type_uri_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2471 	/* 257 */
2472 	{LDNS_RR_TYPE_CAA, "CAA", 3, 3, type_caa_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2473 
2474 #ifdef RRTYPE_AVC
2475 	/* 258 */
2476 	{LDNS_RR_TYPE_AVC, "AVC", 1, 0, NULL, LDNS_RDF_TYPE_STR, LDNS_RR_NO_COMPRESS, 0 },
2477 #else
2478 {LDNS_RR_TYPE_NULL, "TYPE258", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2479 #endif
2480 #ifdef RRTYPE_DOA
2481 	/* 259 */
2482 	{LDNS_RR_TYPE_DOA, "DOA", 5, 5, type_doa_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2483 #else
2484 {LDNS_RR_TYPE_NULL, "TYPE259", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2485 #endif
2486 #ifdef RRTYPE_AMTRELAY
2487 	/* 260 */
2488 	{LDNS_RR_TYPE_AMTRELAY, "AMTRELAY", 1, 1, type_amtrelay_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2489 #else
2490 {LDNS_RR_TYPE_NULL, "TYPE260", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2491 #endif
2492 
2493 /* split in array, no longer contiguous */
2494 
2495 #ifdef RRTYPE_TA
2496 	/* 32768 */
2497 	{LDNS_RR_TYPE_TA, "TA", 4, 4, type_ds_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2498 #else
2499 {LDNS_RR_TYPE_NULL, "TYPE32768", 1, 1, type_0_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 },
2500 #endif
2501 	/* 32769 */
2502 	{LDNS_RR_TYPE_DLV, "DLV", 4, 4, type_ds_wireformat, LDNS_RDF_TYPE_NONE, LDNS_RR_NO_COMPRESS, 0 }
2503 };
2504 /** \endcond */
2505 
2506 /**
2507  * \def LDNS_RDATA_FIELD_DESCRIPTORS_COUNT
2508  * computes the number of rdata fields
2509  */
2510 #define LDNS_RDATA_FIELD_DESCRIPTORS_COUNT \
2511 	(sizeof(rdata_field_descriptors)/sizeof(rdata_field_descriptors[0]))
2512 
2513 
2514 /*---------------------------------------------------------------------------*
2515  * The functions below return an bitmap RDF with the space required to set
2516  * or unset all known RR types. Arguably these functions are better situated
2517  * in rdata.c, however for the space calculation it is necessary to walk
2518  * through rdata_field_descriptors which is not easily possible from anywhere
2519  * other than rr.c where it is declared static.
2520  *
2521  * Alternatively rr.c could have provided an iterator for rr_type or
2522  * rdf_descriptors, but this seemed overkill for internal use only.
2523  */
2524 static ldns_rr_descriptor* rdata_field_descriptors_end =
2525 	&rdata_field_descriptors[LDNS_RDATA_FIELD_DESCRIPTORS_COUNT];
2526 
2527 /* From RFC3845:
2528  *
2529  * 2.1.2.  The List of Type Bit Map(s) Field
2530  *
2531  *    The RR type space is split into 256 window blocks, each representing
2532  *    the low-order 8 bits of the 16-bit RR type space.  Each block that
2533  *    has at least one active RR type is encoded using a single octet
2534  *    window number (from 0 to 255), a single octet bitmap length (from 1
2535  *    to 32) indicating the number of octets used for the window block's
2536  *    bitmap, and up to 32 octets (256 bits) of bitmap.
2537  *
2538  *    Window blocks are present in the NSEC RR RDATA in increasing
2539  *    numerical order.
2540  *
2541  *    "|" denotes concatenation
2542  *
2543  *    Type Bit Map(s) Field = ( Window Block # | Bitmap Length | Bitmap ) +
2544  *
2545  *    <cut>
2546  *
2547  *    Blocks with no types present MUST NOT be included.  Trailing zero
2548  *    octets in the bitmap MUST be omitted.  The length of each block's
2549  *    bitmap is determined by the type code with the largest numerical
2550  *    value within that block, among the set of RR types present at the
2551  *    NSEC RR's owner name.  Trailing zero octets not specified MUST be
2552  *    interpreted as zero octets.
2553  */
2554 static ldns_status
2555 ldns_rdf_bitmap_known_rr_types_set(ldns_rdf** rdf, int value)
2556 {
2557 	uint8_t  window;		/*  most significant octet of type */
2558 	uint8_t  subtype;		/* least significant octet of type */
2559 	uint16_t windows[256]		/* Max subtype per window */
2560 #ifndef S_SPLINT_S
2561 	                      = { 0 }
2562 #endif
2563 	                             ;
2564 	ldns_rr_descriptor* d;	/* used to traverse rdata_field_descriptors */
2565 	size_t i;		/* used to traverse windows array */
2566 
2567 	size_t sz;			/* size needed for type bitmap rdf */
2568 	uint8_t* data = NULL;		/* rdf data */
2569 	uint8_t* dptr;			/* used to itraverse rdf data */
2570 
2571 	assert(rdf != NULL);
2572 
2573 	/* Which windows need to be in the bitmap rdf?
2574 	 */
2575 	for (d=rdata_field_descriptors; d < rdata_field_descriptors_end; d++) {
2576 		window  = d->_type >> 8;
2577 		subtype = d->_type & 0xff;
2578 		if (windows[window] < subtype) {
2579 			windows[window] = subtype;
2580 		}
2581 	}
2582 
2583 	/* How much space do we need in the rdf for those windows?
2584 	 */
2585 	sz = 0;
2586 	for (i = 0; i < 256; i++) {
2587 		if (windows[i]) {
2588 			sz += windows[i] / 8 + 3;
2589 		}
2590 	}
2591 	if (sz > 0) {
2592 		/* Format rdf data according RFC3845 Section 2.1.2 (see above)
2593 		 */
2594 		dptr = data = LDNS_XMALLOC(uint8_t, sz);
2595 		if (!data) {
2596 			return LDNS_STATUS_MEM_ERR;
2597 		}
2598 		memset(data, value, sz);
2599 		for (i = 0; i < 256; i++) {
2600 			if (windows[i]) {
2601 				*dptr++ = (uint8_t)i;
2602 				*dptr++ = (uint8_t)(windows[i] / 8 + 1);
2603 				dptr += dptr[-1];
2604 			}
2605 		}
2606 	}
2607 	/* Allocate and return rdf structure for the data
2608 	 */
2609 	*rdf = ldns_rdf_new(LDNS_RDF_TYPE_BITMAP, sz, data);
2610 	if (!*rdf) {
2611 		LDNS_FREE(data);
2612 		return LDNS_STATUS_MEM_ERR;
2613 	}
2614 	return LDNS_STATUS_OK;
2615 }
2616 
2617 ldns_status
2618 ldns_rdf_bitmap_known_rr_types_space(ldns_rdf** rdf)
2619 {
2620 	return ldns_rdf_bitmap_known_rr_types_set(rdf, 0);
2621 }
2622 
2623 ldns_status
2624 ldns_rdf_bitmap_known_rr_types(ldns_rdf** rdf)
2625 {
2626 	return ldns_rdf_bitmap_known_rr_types_set(rdf, 255);
2627 }
2628 /* End of RDF bitmap functions
2629  *---------------------------------------------------------------------------*/
2630 
2631 
2632 const ldns_rr_descriptor *
2633 ldns_rr_descript(uint16_t type)
2634 {
2635 	size_t i;
2636 	if (type < LDNS_RDATA_FIELD_DESCRIPTORS_COMMON) {
2637 		return &rdata_field_descriptors[type];
2638 	} else {
2639 		/* because not all array index equals type code */
2640 		for (i = LDNS_RDATA_FIELD_DESCRIPTORS_COMMON;
2641 		     i < LDNS_RDATA_FIELD_DESCRIPTORS_COUNT;
2642 		     i++) {
2643 		        if (rdata_field_descriptors[i]._type == type) {
2644 		     		return &rdata_field_descriptors[i];
2645 			}
2646 		}
2647                 return &rdata_field_descriptors[0];
2648 	}
2649 }
2650 
2651 size_t
2652 ldns_rr_descriptor_minimum(const ldns_rr_descriptor *descriptor)
2653 {
2654 	if (descriptor) {
2655 		return descriptor->_minimum;
2656 	} else {
2657 		return 0;
2658 	}
2659 }
2660 
2661 size_t
2662 ldns_rr_descriptor_maximum(const ldns_rr_descriptor *descriptor)
2663 {
2664 	if (descriptor) {
2665 		if (descriptor->_variable != LDNS_RDF_TYPE_NONE) {
2666 			/* Should really be SIZE_MAX... bad FreeBSD.  */
2667 			return UINT_MAX;
2668 		} else {
2669 			return descriptor->_maximum;
2670 		}
2671 	} else {
2672 		return 0;
2673 	}
2674 }
2675 
2676 ldns_rdf_type
2677 ldns_rr_descriptor_field_type(const ldns_rr_descriptor *descriptor,
2678                               size_t index)
2679 {
2680 	assert(descriptor != NULL);
2681 	assert(index < descriptor->_maximum
2682 	       || descriptor->_variable != LDNS_RDF_TYPE_NONE);
2683 	if (index < descriptor->_maximum) {
2684 		return descriptor->_wireformat[index];
2685 	} else {
2686 		return descriptor->_variable;
2687 	}
2688 }
2689 
2690 ldns_rr_type
2691 ldns_get_rr_type_by_name(const char *name)
2692 {
2693 	unsigned int i;
2694 	const char *desc_name;
2695 	const ldns_rr_descriptor *desc;
2696 
2697 	/* TYPEXX representation */
2698 	if (strlen(name) > 4 && strncasecmp(name, "TYPE", 4) == 0) {
2699 		return atoi(name + 4);
2700 	}
2701 
2702 	/* Normal types */
2703 	for (i = 0; i < (unsigned int) LDNS_RDATA_FIELD_DESCRIPTORS_COUNT; i++) {
2704 		desc = &rdata_field_descriptors[i];
2705 		desc_name = desc->_name;
2706 		if(desc_name &&
2707 		   strlen(name) == strlen(desc_name) &&
2708 		   strncasecmp(name, desc_name, strlen(desc_name)) == 0) {
2709 			/* because not all array index equals type code */
2710 			return desc->_type;
2711 		}
2712 	}
2713 
2714 	/* special cases for query types */
2715 	if (strlen(name) == 4 && strncasecmp(name, "IXFR", 4) == 0) {
2716 		return 251;
2717 	} else if (strlen(name) == 4 && strncasecmp(name, "AXFR", 4) == 0) {
2718 		return 252;
2719 	} else if (strlen(name) == 5 && strncasecmp(name, "MAILB", 5) == 0) {
2720 		return 253;
2721 	} else if (strlen(name) == 5 && strncasecmp(name, "MAILA", 5) == 0) {
2722 		return 254;
2723 	} else if (strlen(name) == 3 && strncasecmp(name, "ANY", 3) == 0) {
2724 		return 255;
2725 	}
2726 
2727 	return 0;
2728 }
2729 
2730 ldns_rr_class
2731 ldns_get_rr_class_by_name(const char *name)
2732 {
2733 	ldns_lookup_table *lt;
2734 
2735 	/* CLASSXX representation */
2736 	if (strlen(name) > 5 && strncasecmp(name, "CLASS", 5) == 0) {
2737 		return atoi(name + 5);
2738 	}
2739 
2740 	/* Normal types */
2741 	lt = ldns_lookup_by_name(ldns_rr_classes, name);
2742 
2743 	if (lt) {
2744 		return lt->id;
2745 	}
2746 	return 0;
2747 }
2748 
2749 
2750 ldns_rr_type
2751 ldns_rdf2rr_type(const ldns_rdf *rd)
2752 {
2753         ldns_rr_type r;
2754 
2755         if (!rd) {
2756                 return 0;
2757         }
2758 
2759         if (ldns_rdf_get_type(rd) != LDNS_RDF_TYPE_TYPE) {
2760                 return 0;
2761         }
2762 
2763         r = (ldns_rr_type) ldns_rdf2native_int16(rd);
2764         return r;
2765 }
2766 
2767 ldns_rr_type
2768 ldns_rr_list_type(const ldns_rr_list *rr_list)
2769 {
2770 	if (rr_list && ldns_rr_list_rr_count(rr_list) > 0) {
2771 		return ldns_rr_get_type(ldns_rr_list_rr(rr_list, 0));
2772 	} else {
2773 		return 0;
2774 	}
2775 }
2776 
2777 ldns_rdf *
2778 ldns_rr_list_owner(const ldns_rr_list *rr_list)
2779 {
2780 	if (rr_list && ldns_rr_list_rr_count(rr_list) > 0) {
2781 		return ldns_rr_owner(ldns_rr_list_rr(rr_list, 0));
2782 	} else {
2783 		return NULL;
2784 	}
2785 }
2786