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