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