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