]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - contrib/bind9/lib/dns/rdataslab.c
Fix a problem where zero-length RDATA fields can cause named(8) to crash.
[FreeBSD/releng/8.2.git] / contrib / bind9 / lib / dns / rdataslab.c
1 /*
2  * Copyright (C) 2004-2010  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: rdataslab.c,v 1.48.50.2.24.1 2010/03/03 22:06:39 marka Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <stdlib.h>
25
26 #include <isc/mem.h>
27 #include <isc/region.h>
28 #include <isc/string.h>         /* Required for HP/UX (and others?) */
29 #include <isc/util.h>
30
31 #include <dns/result.h>
32 #include <dns/rdata.h>
33 #include <dns/rdataset.h>
34 #include <dns/rdataslab.h>
35
36 /*
37  * The rdataslab structure allows iteration to occur in both load order
38  * and DNSSEC order.  The structure is as follows:
39  *
40  *      header          (reservelen bytes)
41  *      record count    (2 bytes)
42  *      offset table    (4 x record count bytes in load order)
43  *      data records
44  *              data length     (2 bytes)
45  *              order           (2 bytes)
46  *              meta data       (1 byte for RRSIG's)
47  *              data            (data length bytes)
48  *
49  * If DNS_RDATASET_FIXED is defined to be zero (0) the format of a
50  * rdataslab is as follows:
51  *
52  *      header          (reservelen bytes)
53  *      record count    (2 bytes)
54  *      data records
55  *              data length     (2 bytes)
56  *              data            (data length bytes)
57  *
58  * Offsets are from the end of the header.
59  *
60  * Load order traversal is performed by walking the offset table to find
61  * the start of the record (DNS_RDATASET_FIXED = 1).
62  *
63  * DNSSEC order traversal is performed by walking the data records.
64  *
65  * The order is stored with record to allow for efficient reconstruction
66  * of the offset table following a merge or subtraction.
67  *
68  * The iterator methods here currently only support DNSSEC order iteration.
69  *
70  * The iterator methods in rbtdb support both load order and DNSSEC order
71  * iteration.
72  *
73  * WARNING:
74  *      rbtdb.c directly interacts with the slab's raw structures.  If the
75  *      structure changes then rbtdb.c also needs to be updated to reflect
76  *      the changes.  See the areas tagged with "RDATASLAB".
77  */
78
79 struct xrdata {
80         dns_rdata_t     rdata;
81         unsigned int    order;
82 };
83
84 /*% Note: the "const void *" are just to make qsort happy.  */
85 static int
86 compare_rdata(const void *p1, const void *p2) {
87         const struct xrdata *x1 = p1;
88         const struct xrdata *x2 = p2;
89         return (dns_rdata_compare(&x1->rdata, &x2->rdata));
90 }
91
92 #if DNS_RDATASET_FIXED
93 static void
94 fillin_offsets(unsigned char *offsetbase, unsigned int *offsettable,
95                unsigned length)
96 {
97         unsigned int i, j;
98         unsigned char *raw;
99
100         for (i = 0, j = 0; i < length; i++) {
101
102                 if (offsettable[i] == 0)
103                         continue;
104
105                 /*
106                  * Fill in offset table.
107                  */
108                 raw = &offsetbase[j*4 + 2];
109                 *raw++ = (offsettable[i] & 0xff000000) >> 24;
110                 *raw++ = (offsettable[i] & 0xff0000) >> 16;
111                 *raw++ = (offsettable[i] & 0xff00) >> 8;
112                 *raw = offsettable[i] & 0xff;
113
114                 /*
115                  * Fill in table index.
116                  */
117                 raw = offsetbase + offsettable[i] + 2;
118                 *raw++ = (j & 0xff00) >> 8;
119                 *raw = j++ & 0xff;
120         }
121 }
122 #endif
123
124 isc_result_t
125 dns_rdataslab_fromrdataset(dns_rdataset_t *rdataset, isc_mem_t *mctx,
126                            isc_region_t *region, unsigned int reservelen)
127 {
128         /*
129          * Use &removed as a sentinal pointer for duplicate
130          * rdata as rdata.data == NULL is valid.
131          */
132         static unsigned char removed;
133         struct xrdata  *x;
134         unsigned char  *rawbuf;
135 #if DNS_RDATASET_FIXED
136         unsigned char  *offsetbase;
137 #endif
138         unsigned int    buflen;
139         isc_result_t    result;
140         unsigned int    nitems;
141         unsigned int    nalloc;
142         unsigned int    i;
143 #if DNS_RDATASET_FIXED
144         unsigned int   *offsettable;
145 #endif
146         unsigned int    length;
147
148         buflen = reservelen + 2;
149
150         nalloc = dns_rdataset_count(rdataset);
151         nitems = nalloc;
152         if (nitems == 0)
153                 return (ISC_R_FAILURE);
154
155         if (nalloc > 0xffff)
156                 return (ISC_R_NOSPACE);
157
158         x = isc_mem_get(mctx, nalloc * sizeof(struct xrdata));
159         if (x == NULL)
160                 return (ISC_R_NOMEMORY);
161
162         /*
163          * Save all of the rdata members into an array.
164          */
165         result = dns_rdataset_first(rdataset);
166         if (result != ISC_R_SUCCESS)
167                 goto free_rdatas;
168         for (i = 0; i < nalloc && result == ISC_R_SUCCESS; i++) {
169                 INSIST(result == ISC_R_SUCCESS);
170                 dns_rdata_init(&x[i].rdata);
171                 dns_rdataset_current(rdataset, &x[i].rdata);
172                 INSIST(x[i].rdata.data != &removed);
173 #if DNS_RDATASET_FIXED
174                 x[i].order = i;
175 #endif
176                 result = dns_rdataset_next(rdataset);
177         }
178         if (result != ISC_R_NOMORE)
179                 goto free_rdatas;
180         if (i != nalloc) {
181                 /*
182                  * Somehow we iterated over fewer rdatas than
183                  * dns_rdataset_count() said there were!
184                  */
185                 result = ISC_R_FAILURE;
186                 goto free_rdatas;
187         }
188
189         /*
190          * Put into DNSSEC order.
191          */
192         qsort(x, nalloc, sizeof(struct xrdata), compare_rdata);
193
194         /*
195          * Remove duplicates and compute the total storage required.
196          *
197          * If an rdata is not a duplicate, accumulate the storage size
198          * required for the rdata.  We do not store the class, type, etc,
199          * just the rdata, so our overhead is 2 bytes for the number of
200          * records, and 8 for each rdata, (length(2), offset(4) and order(2))
201          * and then the rdata itself.
202          */
203         for (i = 1; i < nalloc; i++) {
204                 if (compare_rdata(&x[i-1].rdata, &x[i].rdata) == 0) {
205                         x[i-1].rdata.data = &removed;
206 #if DNS_RDATASET_FIXED
207                         /*
208                          * Preserve the least order so A, B, A -> A, B
209                          * after duplicate removal.
210                          */
211                         if (x[i-1].order < x[i].order)
212                                 x[i].order = x[i-1].order;
213 #endif
214                         nitems--;
215                 } else {
216 #if DNS_RDATASET_FIXED
217                         buflen += (8 + x[i-1].rdata.length);
218 #else
219                         buflen += (2 + x[i-1].rdata.length);
220 #endif
221                         /*
222                          * Provide space to store the per RR meta data.
223                          */
224                         if (rdataset->type == dns_rdatatype_rrsig)
225                                 buflen++;
226                 }
227         }
228         /*
229          * Don't forget the last item!
230          */
231 #if DNS_RDATASET_FIXED
232         buflen += (8 + x[i-1].rdata.length);
233 #else
234         buflen += (2 + x[i-1].rdata.length);
235 #endif
236         /*
237          * Provide space to store the per RR meta data.
238          */
239         if (rdataset->type == dns_rdatatype_rrsig)
240                 buflen++;
241
242         /*
243          * Ensure that singleton types are actually singletons.
244          */
245         if (nitems > 1 && dns_rdatatype_issingleton(rdataset->type)) {
246                 /*
247                  * We have a singleton type, but there's more than one
248                  * RR in the rdataset.
249                  */
250                 result = DNS_R_SINGLETON;
251                 goto free_rdatas;
252         }
253
254         /*
255          * Allocate the memory, set up a buffer, start copying in
256          * data.
257          */
258         rawbuf = isc_mem_get(mctx, buflen);
259         if (rawbuf == NULL) {
260                 result = ISC_R_NOMEMORY;
261                 goto free_rdatas;
262         }
263
264 #if DNS_RDATASET_FIXED
265         /* Allocate temporary offset table. */
266         offsettable = isc_mem_get(mctx, nalloc * sizeof(unsigned int));
267         if (offsettable == NULL) {
268                 isc_mem_put(mctx, rawbuf, buflen);
269                 result = ISC_R_NOMEMORY;
270                 goto free_rdatas;
271         }
272         memset(offsettable, 0, nalloc * sizeof(unsigned int));
273 #endif
274
275         region->base = rawbuf;
276         region->length = buflen;
277
278         rawbuf += reservelen;
279 #if DNS_RDATASET_FIXED
280         offsetbase = rawbuf;
281 #endif
282
283         *rawbuf++ = (nitems & 0xff00) >> 8;
284         *rawbuf++ = (nitems & 0x00ff);
285
286 #if DNS_RDATASET_FIXED
287         /* Skip load order table.  Filled in later. */
288         rawbuf += nitems * 4;
289 #endif
290
291         for (i = 0; i < nalloc; i++) {
292                 if (x[i].rdata.data == &removed)
293                         continue;
294 #if DNS_RDATASET_FIXED
295                 offsettable[x[i].order] = rawbuf - offsetbase;
296 #endif
297                 length = x[i].rdata.length;
298                 if (rdataset->type == dns_rdatatype_rrsig)
299                         length++;
300                 *rawbuf++ = (length & 0xff00) >> 8;
301                 *rawbuf++ = (length & 0x00ff);
302 #if DNS_RDATASET_FIXED
303                 rawbuf += 2;    /* filled in later */
304 #endif
305                 /*
306                  * Store the per RR meta data.
307                  */
308                 if (rdataset->type == dns_rdatatype_rrsig) {
309                         *rawbuf++ |= (x[i].rdata.flags & DNS_RDATA_OFFLINE) ?
310                                             DNS_RDATASLAB_OFFLINE : 0;
311                 }
312                 memcpy(rawbuf, x[i].rdata.data, x[i].rdata.length);
313                 rawbuf += x[i].rdata.length;
314         }
315
316 #if DNS_RDATASET_FIXED
317         fillin_offsets(offsetbase, offsettable, nalloc);
318         isc_mem_put(mctx, offsettable, nalloc * sizeof(unsigned int));
319 #endif
320
321         result = ISC_R_SUCCESS;
322
323  free_rdatas:
324         isc_mem_put(mctx, x, nalloc * sizeof(struct xrdata));
325         return (result);
326 }
327
328 static void
329 rdataset_disassociate(dns_rdataset_t *rdataset) {
330         UNUSED(rdataset);
331 }
332
333 static isc_result_t
334 rdataset_first(dns_rdataset_t *rdataset) {
335         unsigned char *raw = rdataset->private3;
336         unsigned int count;
337
338         count = raw[0] * 256 + raw[1];
339         if (count == 0) {
340                 rdataset->private5 = NULL;
341                 return (ISC_R_NOMORE);
342         }
343 #if DNS_RDATASET_FIXED
344         raw += 2 + (4 * count);
345 #else
346         raw += 2;
347 #endif
348         /*
349          * The privateuint4 field is the number of rdata beyond the cursor
350          * position, so we decrement the total count by one before storing
351          * it.
352          */
353         count--;
354         rdataset->privateuint4 = count;
355         rdataset->private5 = raw;
356
357         return (ISC_R_SUCCESS);
358 }
359
360 static isc_result_t
361 rdataset_next(dns_rdataset_t *rdataset) {
362         unsigned int count;
363         unsigned int length;
364         unsigned char *raw;
365
366         count = rdataset->privateuint4;
367         if (count == 0)
368                 return (ISC_R_NOMORE);
369         count--;
370         rdataset->privateuint4 = count;
371         raw = rdataset->private5;
372         length = raw[0] * 256 + raw[1];
373 #if DNS_RDATASET_FIXED
374         raw += length + 4;
375 #else
376         raw += length + 2;
377 #endif
378         rdataset->private5 = raw;
379
380         return (ISC_R_SUCCESS);
381 }
382
383 static void
384 rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
385         unsigned char *raw = rdataset->private5;
386         isc_region_t r;
387         unsigned int length;
388         unsigned int flags = 0;
389
390         REQUIRE(raw != NULL);
391
392         length = raw[0] * 256 + raw[1];
393 #if DNS_RDATASET_FIXED
394         raw += 4;
395 #else
396         raw += 2;
397 #endif
398         if (rdataset->type == dns_rdatatype_rrsig) {
399                 if (*raw & DNS_RDATASLAB_OFFLINE)
400                         flags |= DNS_RDATA_OFFLINE;
401                 length--;
402                 raw++;
403         }
404         r.length = length;
405         r.base = raw;
406         dns_rdata_fromregion(rdata, rdataset->rdclass, rdataset->type, &r);
407         rdata->flags |= flags;
408 }
409
410 static void
411 rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
412         *target = *source;
413
414         /*
415          * Reset iterator state.
416          */
417         target->privateuint4 = 0;
418         target->private5 = NULL;
419 }
420
421 static unsigned int
422 rdataset_count(dns_rdataset_t *rdataset) {
423         unsigned char *raw = rdataset->private3;
424         unsigned int count;
425
426         count = raw[0] * 256 + raw[1];
427
428         return (count);
429 }
430
431 static dns_rdatasetmethods_t rdataset_methods = {
432         rdataset_disassociate,
433         rdataset_first,
434         rdataset_next,
435         rdataset_current,
436         rdataset_clone,
437         rdataset_count,
438         NULL,
439         NULL,
440         NULL,
441         NULL,
442         NULL,
443         NULL,
444         NULL,
445         NULL,
446         NULL
447 };
448
449 void
450 dns_rdataslab_tordataset(unsigned char *slab, unsigned int reservelen,
451                          dns_rdataclass_t rdclass, dns_rdatatype_t rdtype,
452                          dns_rdatatype_t covers, dns_ttl_t ttl,
453                          dns_rdataset_t *rdataset)
454 {
455         REQUIRE(slab != NULL);
456         REQUIRE(!dns_rdataset_isassociated(rdataset));
457
458         rdataset->methods = &rdataset_methods;
459         rdataset->rdclass = rdclass;
460         rdataset->type = rdtype;
461         rdataset->covers = covers;
462         rdataset->ttl = ttl;
463         rdataset->trust = 0;
464         rdataset->private1 = NULL;
465         rdataset->private2 = NULL;
466         rdataset->private3 = slab + reservelen;
467
468         /*
469          * Reset iterator state.
470          */
471         rdataset->privateuint4 = 0;
472         rdataset->private5 = NULL;
473 }
474
475 unsigned int
476 dns_rdataslab_size(unsigned char *slab, unsigned int reservelen) {
477         unsigned int count, length;
478         unsigned char *current;
479
480         REQUIRE(slab != NULL);
481
482         current = slab + reservelen;
483         count = *current++ * 256;
484         count += *current++;
485 #if DNS_RDATASET_FIXED
486         current += (4 * count);
487 #endif
488         while (count > 0) {
489                 count--;
490                 length = *current++ * 256;
491                 length += *current++;
492 #if DNS_RDATASET_FIXED
493                 current += length + 2;
494 #else
495                 current += length;
496 #endif
497         }
498
499         return ((unsigned int)(current - slab));
500 }
501
502 /*
503  * Make the dns_rdata_t 'rdata' refer to the slab item
504  * beginning at '*current', which is part of a slab of type
505  * 'type' and class 'rdclass', and advance '*current' to
506  * point to the next item in the slab.
507  */
508 static inline void
509 rdata_from_slab(unsigned char **current,
510               dns_rdataclass_t rdclass, dns_rdatatype_t type,
511               dns_rdata_t *rdata)
512 {
513         unsigned char *tcurrent = *current;
514         isc_region_t region;
515         unsigned int length;
516         isc_boolean_t offline = ISC_FALSE;
517
518         length = *tcurrent++ * 256;
519         length += *tcurrent++;
520
521         if (type == dns_rdatatype_rrsig) {
522                 if ((*tcurrent & DNS_RDATASLAB_OFFLINE) != 0)
523                         offline = ISC_TRUE;
524                 length--;
525                 tcurrent++;
526         }
527         region.length = length;
528 #if DNS_RDATASET_FIXED
529         tcurrent += 2;
530 #endif
531         region.base = tcurrent;
532         tcurrent += region.length;
533         dns_rdata_fromregion(rdata, rdclass, type, &region);
534         if (offline)
535                 rdata->flags |= DNS_RDATA_OFFLINE;
536         *current = tcurrent;
537 }
538
539 /*
540  * Return true iff 'slab' (slab data of type 'type' and class 'rdclass')
541  * contains an rdata identical to 'rdata'.  This does case insensitive
542  * comparisons per DNSSEC.
543  */
544 static inline isc_boolean_t
545 rdata_in_slab(unsigned char *slab, unsigned int reservelen,
546               dns_rdataclass_t rdclass, dns_rdatatype_t type,
547               dns_rdata_t *rdata)
548 {
549         unsigned int count, i;
550         unsigned char *current;
551         dns_rdata_t trdata = DNS_RDATA_INIT;
552         int n;
553
554         current = slab + reservelen;
555         count = *current++ * 256;
556         count += *current++;
557
558 #if DNS_RDATASET_FIXED
559         current += (4 * count);
560 #endif
561
562         for (i = 0; i < count; i++) {
563                 rdata_from_slab(&current, rdclass, type, &trdata);
564
565                 n = dns_rdata_compare(&trdata, rdata);
566                 if (n == 0)
567                         return (ISC_TRUE);
568                 if (n > 0)      /* In DNSSEC order. */
569                         break;
570                 dns_rdata_reset(&trdata);
571         }
572         return (ISC_FALSE);
573 }
574
575 isc_result_t
576 dns_rdataslab_merge(unsigned char *oslab, unsigned char *nslab,
577                     unsigned int reservelen, isc_mem_t *mctx,
578                     dns_rdataclass_t rdclass, dns_rdatatype_t type,
579                     unsigned int flags, unsigned char **tslabp)
580 {
581         unsigned char *ocurrent, *ostart, *ncurrent, *tstart, *tcurrent, *data;
582         unsigned int ocount, ncount, count, olength, tlength, tcount, length;
583         dns_rdata_t ordata = DNS_RDATA_INIT;
584         dns_rdata_t nrdata = DNS_RDATA_INIT;
585         isc_boolean_t added_something = ISC_FALSE;
586         unsigned int oadded = 0;
587         unsigned int nadded = 0;
588         unsigned int nncount = 0;
589 #if DNS_RDATASET_FIXED
590         unsigned int oncount;
591         unsigned int norder = 0;
592         unsigned int oorder = 0;
593         unsigned char *offsetbase;
594         unsigned int *offsettable;
595 #endif
596
597         /*
598          * XXX  Need parameter to allow "delete rdatasets in nslab" merge,
599          * or perhaps another merge routine for this purpose.
600          */
601
602         REQUIRE(tslabp != NULL && *tslabp == NULL);
603         REQUIRE(oslab != NULL && nslab != NULL);
604
605         ocurrent = oslab + reservelen;
606         ocount = *ocurrent++ * 256;
607         ocount += *ocurrent++;
608 #if DNS_RDATASET_FIXED
609         ocurrent += (4 * ocount);
610 #endif
611         ostart = ocurrent;
612         ncurrent = nslab + reservelen;
613         ncount = *ncurrent++ * 256;
614         ncount += *ncurrent++;
615 #if DNS_RDATASET_FIXED
616         ncurrent += (4 * ncount);
617 #endif
618         INSIST(ocount > 0 && ncount > 0);
619
620 #if DNS_RDATASET_FIXED
621         oncount = ncount;
622 #endif
623
624         /*
625          * Yes, this is inefficient!
626          */
627
628         /*
629          * Figure out the length of the old slab's data.
630          */
631         olength = 0;
632         for (count = 0; count < ocount; count++) {
633                 length = *ocurrent++ * 256;
634                 length += *ocurrent++;
635 #if DNS_RDATASET_FIXED
636                 olength += length + 8;
637                 ocurrent += length + 2;
638 #else
639                 olength += length + 2;
640                 ocurrent += length;
641 #endif
642         }
643
644         /*
645          * Start figuring out the target length and count.
646          */
647         tlength = reservelen + 2 + olength;
648         tcount = ocount;
649
650         /*
651          * Add in the length of rdata in the new slab that aren't in
652          * the old slab.
653          */
654         do {
655                 dns_rdata_init(&nrdata);
656                 rdata_from_slab(&ncurrent, rdclass, type, &nrdata);
657                 if (!rdata_in_slab(oslab, reservelen, rdclass, type, &nrdata))
658                 {
659                         /*
660                          * This rdata isn't in the old slab.
661                          */
662 #if DNS_RDATASET_FIXED
663                         tlength += nrdata.length + 8;
664 #else
665                         tlength += nrdata.length + 2;
666 #endif
667                         if (type == dns_rdatatype_rrsig)
668                                 tlength++;
669                         tcount++;
670                         nncount++;
671                         added_something = ISC_TRUE;
672                 }
673                 ncount--;
674         } while (ncount > 0);
675         ncount = nncount;
676
677         if (((flags & DNS_RDATASLAB_EXACT) != 0) &&
678             (tcount != ncount + ocount))
679                 return (DNS_R_NOTEXACT);
680
681         if (!added_something && (flags & DNS_RDATASLAB_FORCE) == 0)
682                 return (DNS_R_UNCHANGED);
683
684         /*
685          * Ensure that singleton types are actually singletons.
686          */
687         if (tcount > 1 && dns_rdatatype_issingleton(type)) {
688                 /*
689                  * We have a singleton type, but there's more than one
690                  * RR in the rdataset.
691                  */
692                 return (DNS_R_SINGLETON);
693         }
694
695         if (tcount > 0xffff)
696                 return (ISC_R_NOSPACE);
697
698         /*
699          * Copy the reserved area from the new slab.
700          */
701         tstart = isc_mem_get(mctx, tlength);
702         if (tstart == NULL)
703                 return (ISC_R_NOMEMORY);
704         memcpy(tstart, nslab, reservelen);
705         tcurrent = tstart + reservelen;
706 #if DNS_RDATASET_FIXED
707         offsetbase = tcurrent;
708 #endif
709
710         /*
711          * Write the new count.
712          */
713         *tcurrent++ = (tcount & 0xff00) >> 8;
714         *tcurrent++ = (tcount & 0x00ff);
715
716 #if DNS_RDATASET_FIXED
717         /*
718          * Skip offset table.
719          */
720         tcurrent += (tcount * 4);
721
722         offsettable = isc_mem_get(mctx,
723                                   (ocount + oncount) * sizeof(unsigned int));
724         if (offsettable == NULL) {
725                 isc_mem_put(mctx, tstart, tlength);
726                 return (ISC_R_NOMEMORY);
727         }
728         memset(offsettable, 0, (ocount + oncount) * sizeof(unsigned int));
729 #endif
730
731         /*
732          * Merge the two slabs.
733          */
734         ocurrent = ostart;
735         INSIST(ocount != 0);
736 #if DNS_RDATASET_FIXED
737         oorder = ocurrent[2] * 256 + ocurrent[3];
738         INSIST(oorder < ocount);
739 #endif
740         rdata_from_slab(&ocurrent, rdclass, type, &ordata);
741
742         ncurrent = nslab + reservelen + 2;
743 #if DNS_RDATASET_FIXED
744         ncurrent += (4 * oncount);
745 #endif
746
747         if (ncount > 0) {
748                 do {
749                         dns_rdata_reset(&nrdata);
750 #if DNS_RDATASET_FIXED
751                         norder = ncurrent[2] * 256 + ncurrent[3];
752
753                         INSIST(norder < oncount);
754 #endif
755                         rdata_from_slab(&ncurrent, rdclass, type, &nrdata);
756                 } while (rdata_in_slab(oslab, reservelen, rdclass,
757                                        type, &nrdata));
758         }
759
760         while (oadded < ocount || nadded < ncount) {
761                 isc_boolean_t fromold;
762                 if (oadded == ocount)
763                         fromold = ISC_FALSE;
764                 else if (nadded == ncount)
765                         fromold = ISC_TRUE;
766                 else
767                         fromold = ISC_TF(compare_rdata(&ordata, &nrdata) < 0);
768                 if (fromold) {
769 #if DNS_RDATASET_FIXED
770                         offsettable[oorder] = tcurrent - offsetbase;
771 #endif
772                         length = ordata.length;
773                         data = ordata.data;
774                         if (type == dns_rdatatype_rrsig) {
775                                 length++;
776                                 data--;
777                         }
778                         *tcurrent++ = (length & 0xff00) >> 8;
779                         *tcurrent++ = (length & 0x00ff);
780 #if DNS_RDATASET_FIXED
781                         tcurrent += 2;  /* fill in later */
782 #endif
783                         memcpy(tcurrent, data, length);
784                         tcurrent += length;
785                         oadded++;
786                         if (oadded < ocount) {
787                                 dns_rdata_reset(&ordata);
788 #if DNS_RDATASET_FIXED
789                                 oorder = ocurrent[2] * 256 + ocurrent[3];
790                                 INSIST(oorder < ocount);
791 #endif
792                                 rdata_from_slab(&ocurrent, rdclass, type,
793                                                 &ordata);
794                         }
795                 } else {
796 #if DNS_RDATASET_FIXED
797                         offsettable[ocount + norder] = tcurrent - offsetbase;
798 #endif
799                         length = nrdata.length;
800                         data = nrdata.data;
801                         if (type == dns_rdatatype_rrsig) {
802                                 length++;
803                                 data--;
804                         }
805                         *tcurrent++ = (length & 0xff00) >> 8;
806                         *tcurrent++ = (length & 0x00ff);
807 #if DNS_RDATASET_FIXED
808                         tcurrent += 2;  /* fill in later */
809 #endif
810                         memcpy(tcurrent, data, length);
811                         tcurrent += length;
812                         nadded++;
813                         if (nadded < ncount) {
814                                 do {
815                                         dns_rdata_reset(&nrdata);
816 #if DNS_RDATASET_FIXED
817                                         norder = ncurrent[2] * 256 + ncurrent[3];
818                                         INSIST(norder < oncount);
819 #endif
820                                         rdata_from_slab(&ncurrent, rdclass,
821                                                         type, &nrdata);
822                                 } while (rdata_in_slab(oslab, reservelen,
823                                                        rdclass, type,
824                                                        &nrdata));
825                         }
826                 }
827         }
828
829 #if DNS_RDATASET_FIXED
830         fillin_offsets(offsetbase, offsettable, ocount + oncount);
831
832         isc_mem_put(mctx, offsettable,
833                     (ocount + oncount) * sizeof(unsigned int));
834 #endif
835
836         INSIST(tcurrent == tstart + tlength);
837
838         *tslabp = tstart;
839
840         return (ISC_R_SUCCESS);
841 }
842
843 isc_result_t
844 dns_rdataslab_subtract(unsigned char *mslab, unsigned char *sslab,
845                        unsigned int reservelen, isc_mem_t *mctx,
846                        dns_rdataclass_t rdclass, dns_rdatatype_t type,
847                        unsigned int flags, unsigned char **tslabp)
848 {
849         unsigned char *mcurrent, *sstart, *scurrent, *tstart, *tcurrent;
850         unsigned int mcount, scount, rcount ,count, tlength, tcount, i;
851         dns_rdata_t srdata = DNS_RDATA_INIT;
852         dns_rdata_t mrdata = DNS_RDATA_INIT;
853 #if DNS_RDATASET_FIXED
854         unsigned char *offsetbase;
855         unsigned int *offsettable;
856         unsigned int order;
857 #endif
858
859         REQUIRE(tslabp != NULL && *tslabp == NULL);
860         REQUIRE(mslab != NULL && sslab != NULL);
861
862         mcurrent = mslab + reservelen;
863         mcount = *mcurrent++ * 256;
864         mcount += *mcurrent++;
865         scurrent = sslab + reservelen;
866         scount = *scurrent++ * 256;
867         scount += *scurrent++;
868         INSIST(mcount > 0 && scount > 0);
869
870         /*
871          * Yes, this is inefficient!
872          */
873
874         /*
875          * Start figuring out the target length and count.
876          */
877         tlength = reservelen + 2;
878         tcount = 0;
879         rcount = 0;
880
881 #if DNS_RDATASET_FIXED
882         mcurrent += 4 * mcount;
883         scurrent += 4 * scount;
884 #endif
885         sstart = scurrent;
886
887         /*
888          * Add in the length of rdata in the mslab that aren't in
889          * the sslab.
890          */
891         for (i = 0; i < mcount; i++) {
892                 unsigned char *mrdatabegin = mcurrent;
893                 rdata_from_slab(&mcurrent, rdclass, type, &mrdata);
894                 scurrent = sstart;
895                 for (count = 0; count < scount; count++) {
896                         dns_rdata_reset(&srdata);
897                         rdata_from_slab(&scurrent, rdclass, type, &srdata);
898                         if (dns_rdata_compare(&mrdata, &srdata) == 0)
899                                 break;
900                 }
901                 if (count == scount) {
902                         /*
903                          * This rdata isn't in the sslab, and thus isn't
904                          * being subtracted.
905                          */
906                         tlength += mcurrent - mrdatabegin;
907                         tcount++;
908                 } else
909                         rcount++;
910                 dns_rdata_reset(&mrdata);
911         }
912
913 #if DNS_RDATASET_FIXED
914         tlength += (4 * tcount);
915 #endif
916
917         /*
918          * Check that all the records originally existed.  The numeric
919          * check only works as rdataslabs do not contain duplicates.
920          */
921         if (((flags & DNS_RDATASLAB_EXACT) != 0) && (rcount != scount))
922                 return (DNS_R_NOTEXACT);
923
924         /*
925          * Don't continue if the new rdataslab would be empty.
926          */
927         if (tcount == 0)
928                 return (DNS_R_NXRRSET);
929
930         /*
931          * If nothing is going to change, we can stop.
932          */
933         if (rcount == 0)
934                 return (DNS_R_UNCHANGED);
935
936         /*
937          * Copy the reserved area from the mslab.
938          */
939         tstart = isc_mem_get(mctx, tlength);
940         if (tstart == NULL)
941                 return (ISC_R_NOMEMORY);
942         memcpy(tstart, mslab, reservelen);
943         tcurrent = tstart + reservelen;
944 #if DNS_RDATASET_FIXED
945         offsetbase = tcurrent;
946
947         offsettable = isc_mem_get(mctx, mcount * sizeof(unsigned int));
948         if (offsettable == NULL) {
949                 isc_mem_put(mctx, tstart, tlength);
950                 return (ISC_R_NOMEMORY);
951         }
952         memset(offsettable, 0, mcount * sizeof(unsigned int));
953 #endif
954
955         /*
956          * Write the new count.
957          */
958         *tcurrent++ = (tcount & 0xff00) >> 8;
959         *tcurrent++ = (tcount & 0x00ff);
960
961 #if DNS_RDATASET_FIXED
962         tcurrent += (4 * tcount);
963 #endif
964
965         /*
966          * Copy the parts of mslab not in sslab.
967          */
968         mcurrent = mslab + reservelen;
969         mcount = *mcurrent++ * 256;
970         mcount += *mcurrent++;
971 #if DNS_RDATASET_FIXED
972         mcurrent += (4 * mcount);
973 #endif
974         for (i = 0; i < mcount; i++) {
975                 unsigned char *mrdatabegin = mcurrent;
976 #if DNS_RDATASET_FIXED
977                 order = mcurrent[2] * 256 + mcurrent[3];
978                 INSIST(order < mcount);
979 #endif
980                 rdata_from_slab(&mcurrent, rdclass, type, &mrdata);
981                 scurrent = sstart;
982                 for (count = 0; count < scount; count++) {
983                         dns_rdata_reset(&srdata);
984                         rdata_from_slab(&scurrent, rdclass, type, &srdata);
985                         if (dns_rdata_compare(&mrdata, &srdata) == 0)
986                                 break;
987                 }
988                 if (count == scount) {
989                         /*
990                          * This rdata isn't in the sslab, and thus should be
991                          * copied to the tslab.
992                          */
993                         unsigned int length = mcurrent - mrdatabegin;
994 #if DNS_RDATASET_FIXED
995                         offsettable[order] = tcurrent - offsetbase;
996 #endif
997                         memcpy(tcurrent, mrdatabegin, length);
998                         tcurrent += length;
999                 }
1000                 dns_rdata_reset(&mrdata);
1001         }
1002
1003 #if DNS_RDATASET_FIXED
1004         fillin_offsets(offsetbase, offsettable, mcount);
1005
1006         isc_mem_put(mctx, offsettable, mcount * sizeof(unsigned int));
1007 #endif
1008
1009         INSIST(tcurrent == tstart + tlength);
1010
1011         *tslabp = tstart;
1012
1013         return (ISC_R_SUCCESS);
1014 }
1015
1016 isc_boolean_t
1017 dns_rdataslab_equal(unsigned char *slab1, unsigned char *slab2,
1018                     unsigned int reservelen)
1019 {
1020         unsigned char *current1, *current2;
1021         unsigned int count1, count2;
1022         unsigned int length1, length2;
1023
1024         current1 = slab1 + reservelen;
1025         count1 = *current1++ * 256;
1026         count1 += *current1++;
1027
1028         current2 = slab2 + reservelen;
1029         count2 = *current2++ * 256;
1030         count2 += *current2++;
1031
1032         if (count1 != count2)
1033                 return (ISC_FALSE);
1034
1035 #if DNS_RDATASET_FIXED
1036         current1 += (4 * count1);
1037         current2 += (4 * count2);
1038 #endif
1039
1040         while (count1 > 0) {
1041                 length1 = *current1++ * 256;
1042                 length1 += *current1++;
1043
1044                 length2 = *current2++ * 256;
1045                 length2 += *current2++;
1046
1047 #if DNS_RDATASET_FIXED
1048                 current1 += 2;
1049                 current2 += 2;
1050 #endif
1051
1052                 if (length1 != length2 ||
1053                     memcmp(current1, current2, length1) != 0)
1054                         return (ISC_FALSE);
1055
1056                 current1 += length1;
1057                 current2 += length1;
1058
1059                 count1--;
1060         }
1061         return (ISC_TRUE);
1062 }
1063
1064 isc_boolean_t
1065 dns_rdataslab_equalx(unsigned char *slab1, unsigned char *slab2,
1066                      unsigned int reservelen, dns_rdataclass_t rdclass,
1067                      dns_rdatatype_t type)
1068 {
1069         unsigned char *current1, *current2;
1070         unsigned int count1, count2;
1071         dns_rdata_t rdata1 = DNS_RDATA_INIT;
1072         dns_rdata_t rdata2 = DNS_RDATA_INIT;
1073
1074         current1 = slab1 + reservelen;
1075         count1 = *current1++ * 256;
1076         count1 += *current1++;
1077
1078         current2 = slab2 + reservelen;
1079         count2 = *current2++ * 256;
1080         count2 += *current2++;
1081
1082         if (count1 != count2)
1083                 return (ISC_FALSE);
1084
1085 #if DNS_RDATASET_FIXED
1086         current1 += (4 * count1);
1087         current2 += (4 * count2);
1088 #endif
1089
1090         while (count1-- > 0) {
1091                 rdata_from_slab(&current1, rdclass, type, &rdata1);
1092                 rdata_from_slab(&current2, rdclass, type, &rdata2);
1093                 if (dns_rdata_compare(&rdata1, &rdata2) != 0)
1094                         return (ISC_FALSE);
1095                 dns_rdata_reset(&rdata1);
1096                 dns_rdata_reset(&rdata2);
1097         }
1098         return (ISC_TRUE);
1099 }