vendor/BIND: Update to 9.5.2-P3
[dragonfly.git] / contrib / bind / lib / dns / rdataslab.c
blob48dc0e0e4d4e8d97dfb39a921e13bd81804f1db2
1 /*
2 * Copyright (C) 2004-2010 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
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.
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.
18 /* $Id: rdataslab.c,v 1.43.128.4.2.2 2010/02/25 10:56:01 tbox Exp $ */
20 /*! \file */
22 #include <config.h>
24 #include <stdlib.h>
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>
31 #include <dns/result.h>
32 #include <dns/rdata.h>
33 #include <dns/rdataset.h>
34 #include <dns/rdataslab.h>
37 * The rdataslab structure allows iteration to occur in both load order
38 * and DNSSEC order. The structure is as follows:
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 * data (data length bytes)
48 * If DNS_RDATASET_FIXED is defined to be zero (0) the format of a
49 * rdataslab is as follows:
51 * header (reservelen bytes)
52 * record count (2 bytes)
53 * data records
54 * data length (2 bytes)
55 * data (data length bytes)
57 * Offsets are from the end of the header.
59 * Load order traversal is performed by walking the offset table to find
60 * the start of the record (DNS_RDATASET_FIXED = 1).
62 * DNSSEC order traversal is performed by walking the data records.
64 * The order is stored with record to allow for efficient reconstruction
65 * of the offset table following a merge or subtraction.
67 * The iterator methods here currently only support DNSSEC order iteration.
69 * The iterator methods in rbtdb support both load order and DNSSEC order
70 * iteration.
72 * WARNING:
73 * rbtdb.c directly interacts with the slab's raw structures. If the
74 * structure changes then rbtdb.c also needs to be updated to reflect
75 * the changes. See the areas tagged with "RDATASLAB".
78 struct xrdata {
79 dns_rdata_t rdata;
80 unsigned int order;
83 /*% Note: the "const void *" are just to make qsort happy. */
84 static int
85 compare_rdata(const void *p1, const void *p2) {
86 const struct xrdata *x1 = p1;
87 const struct xrdata *x2 = p2;
88 return (dns_rdata_compare(&x1->rdata, &x2->rdata));
91 #if DNS_RDATASET_FIXED
92 static void
93 fillin_offsets(unsigned char *offsetbase, unsigned int *offsettable,
94 unsigned length)
96 unsigned int i, j;
97 unsigned char *raw;
99 for (i = 0, j = 0; i < length; i++) {
101 if (offsettable[i] == 0)
102 continue;
105 * Fill in offset table.
107 raw = &offsetbase[j*4 + 2];
108 *raw++ = (offsettable[i] & 0xff000000) >> 24;
109 *raw++ = (offsettable[i] & 0xff0000) >> 16;
110 *raw++ = (offsettable[i] & 0xff00) >> 8;
111 *raw = offsettable[i] & 0xff;
114 * Fill in table index.
116 raw = offsetbase + offsettable[i] + 2;
117 *raw++ = (j & 0xff00) >> 8;
118 *raw = j++ & 0xff;
121 #endif
123 isc_result_t
124 dns_rdataslab_fromrdataset(dns_rdataset_t *rdataset, isc_mem_t *mctx,
125 isc_region_t *region, unsigned int reservelen)
127 struct xrdata *x;
128 unsigned char *rawbuf;
129 #if DNS_RDATASET_FIXED
130 unsigned char *offsetbase;
131 #endif
132 unsigned int buflen;
133 isc_result_t result;
134 unsigned int nitems;
135 unsigned int nalloc;
136 unsigned int i;
137 #if DNS_RDATASET_FIXED
138 unsigned int *offsettable;
139 #endif
141 buflen = reservelen + 2;
143 nalloc = dns_rdataset_count(rdataset);
144 nitems = nalloc;
145 if (nitems == 0)
146 return (ISC_R_FAILURE);
148 if (nalloc > 0xffff)
149 return (ISC_R_NOSPACE);
151 x = isc_mem_get(mctx, nalloc * sizeof(struct xrdata));
152 if (x == NULL)
153 return (ISC_R_NOMEMORY);
156 * Save all of the rdata members into an array.
158 result = dns_rdataset_first(rdataset);
159 if (result != ISC_R_SUCCESS)
160 goto free_rdatas;
161 for (i = 0; i < nalloc && result == ISC_R_SUCCESS; i++) {
162 INSIST(result == ISC_R_SUCCESS);
163 dns_rdata_init(&x[i].rdata);
164 dns_rdataset_current(rdataset, &x[i].rdata);
165 #if DNS_RDATASET_FIXED
166 x[i].order = i;
167 #endif
168 result = dns_rdataset_next(rdataset);
170 if (result != ISC_R_NOMORE)
171 goto free_rdatas;
172 if (i != nalloc) {
174 * Somehow we iterated over fewer rdatas than
175 * dns_rdataset_count() said there were!
177 result = ISC_R_FAILURE;
178 goto free_rdatas;
182 * Put into DNSSEC order.
184 qsort(x, nalloc, sizeof(struct xrdata), compare_rdata);
187 * Remove duplicates and compute the total storage required.
189 * If an rdata is not a duplicate, accumulate the storage size
190 * required for the rdata. We do not store the class, type, etc,
191 * just the rdata, so our overhead is 2 bytes for the number of
192 * records, and 8 for each rdata, (length(2), offset(4) and order(2))
193 * and then the rdata itself.
195 for (i = 1; i < nalloc; i++) {
196 if (compare_rdata(&x[i-1].rdata, &x[i].rdata) == 0) {
197 x[i-1].rdata.data = NULL;
198 x[i-1].rdata.length = 0;
199 #if DNS_RDATASET_FIXED
201 * Preserve the least order so A, B, A -> A, B
202 * after duplicate removal.
204 if (x[i-1].order < x[i].order)
205 x[i].order = x[i-1].order;
206 #endif
207 nitems--;
208 } else
209 #if DNS_RDATASET_FIXED
210 buflen += (8 + x[i-1].rdata.length);
211 #else
212 buflen += (2 + x[i-1].rdata.length);
213 #endif
216 * Don't forget the last item!
218 #if DNS_RDATASET_FIXED
219 buflen += (8 + x[i-1].rdata.length);
220 #else
221 buflen += (2 + x[i-1].rdata.length);
222 #endif
225 * Ensure that singleton types are actually singletons.
227 if (nitems > 1 && dns_rdatatype_issingleton(rdataset->type)) {
229 * We have a singleton type, but there's more than one
230 * RR in the rdataset.
232 result = DNS_R_SINGLETON;
233 goto free_rdatas;
237 * Allocate the memory, set up a buffer, start copying in
238 * data.
240 rawbuf = isc_mem_get(mctx, buflen);
241 if (rawbuf == NULL) {
242 result = ISC_R_NOMEMORY;
243 goto free_rdatas;
246 #if DNS_RDATASET_FIXED
247 /* Allocate temporary offset table. */
248 offsettable = isc_mem_get(mctx, nalloc * sizeof(unsigned int));
249 if (offsettable == NULL) {
250 isc_mem_put(mctx, rawbuf, buflen);
251 result = ISC_R_NOMEMORY;
252 goto free_rdatas;
254 memset(offsettable, 0, nalloc * sizeof(unsigned int));
255 #endif
257 region->base = rawbuf;
258 region->length = buflen;
260 rawbuf += reservelen;
261 #if DNS_RDATASET_FIXED
262 offsetbase = rawbuf;
263 #endif
265 *rawbuf++ = (nitems & 0xff00) >> 8;
266 *rawbuf++ = (nitems & 0x00ff);
268 #if DNS_RDATASET_FIXED
269 /* Skip load order table. Filled in later. */
270 rawbuf += nitems * 4;
271 #endif
273 for (i = 0; i < nalloc; i++) {
274 if (x[i].rdata.data == NULL)
275 continue;
276 #if DNS_RDATASET_FIXED
277 offsettable[x[i].order] = rawbuf - offsetbase;
278 #endif
279 *rawbuf++ = (x[i].rdata.length & 0xff00) >> 8;
280 *rawbuf++ = (x[i].rdata.length & 0x00ff);
281 #if DNS_RDATASET_FIXED
282 rawbuf += 2; /* filled in later */
283 #endif
284 memcpy(rawbuf, x[i].rdata.data, x[i].rdata.length);
285 rawbuf += x[i].rdata.length;
288 #if DNS_RDATASET_FIXED
289 fillin_offsets(offsetbase, offsettable, nalloc);
290 isc_mem_put(mctx, offsettable, nalloc * sizeof(unsigned int));
291 #endif
293 result = ISC_R_SUCCESS;
295 free_rdatas:
296 isc_mem_put(mctx, x, nalloc * sizeof(struct xrdata));
297 return (result);
300 static void
301 rdataset_disassociate(dns_rdataset_t *rdataset) {
302 UNUSED(rdataset);
305 static isc_result_t
306 rdataset_first(dns_rdataset_t *rdataset) {
307 unsigned char *raw = rdataset->private3;
308 unsigned int count;
310 count = raw[0] * 256 + raw[1];
311 if (count == 0) {
312 rdataset->private5 = NULL;
313 return (ISC_R_NOMORE);
315 #if DNS_RDATASET_FIXED
316 raw += 2 + (4 * count);
317 #else
318 raw += 2;
319 #endif
321 * The privateuint4 field is the number of rdata beyond the cursor
322 * position, so we decrement the total count by one before storing
323 * it.
325 count--;
326 rdataset->privateuint4 = count;
327 rdataset->private5 = raw;
329 return (ISC_R_SUCCESS);
332 static isc_result_t
333 rdataset_next(dns_rdataset_t *rdataset) {
334 unsigned int count;
335 unsigned int length;
336 unsigned char *raw;
338 count = rdataset->privateuint4;
339 if (count == 0)
340 return (ISC_R_NOMORE);
341 count--;
342 rdataset->privateuint4 = count;
343 raw = rdataset->private5;
344 length = raw[0] * 256 + raw[1];
345 #if DNS_RDATASET_FIXED
346 raw += length + 4;
347 #else
348 raw += length + 2;
349 #endif
350 rdataset->private5 = raw;
352 return (ISC_R_SUCCESS);
355 static void
356 rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
357 unsigned char *raw = rdataset->private5;
358 isc_region_t r;
360 REQUIRE(raw != NULL);
362 r.length = raw[0] * 256 + raw[1];
363 #if DNS_RDATASET_FIXED
364 raw += 4;
365 #else
366 raw += 2;
367 #endif
368 r.base = raw;
369 dns_rdata_fromregion(rdata, rdataset->rdclass, rdataset->type, &r);
372 static void
373 rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
374 *target = *source;
377 * Reset iterator state.
379 target->privateuint4 = 0;
380 target->private5 = NULL;
383 static unsigned int
384 rdataset_count(dns_rdataset_t *rdataset) {
385 unsigned char *raw = rdataset->private3;
386 unsigned int count;
388 count = raw[0] * 256 + raw[1];
390 return (count);
393 static dns_rdatasetmethods_t rdataset_methods = {
394 rdataset_disassociate,
395 rdataset_first,
396 rdataset_next,
397 rdataset_current,
398 rdataset_clone,
399 rdataset_count,
400 NULL,
401 NULL,
402 NULL,
403 NULL,
404 NULL,
405 NULL,
406 NULL
409 void
410 dns_rdataslab_tordataset(unsigned char *slab, unsigned int reservelen,
411 dns_rdataclass_t rdclass, dns_rdatatype_t rdtype,
412 dns_rdatatype_t covers, dns_ttl_t ttl,
413 dns_rdataset_t *rdataset)
415 REQUIRE(slab != NULL);
416 REQUIRE(!dns_rdataset_isassociated(rdataset));
418 rdataset->methods = &rdataset_methods;
419 rdataset->rdclass = rdclass;
420 rdataset->type = rdtype;
421 rdataset->covers = covers;
422 rdataset->ttl = ttl;
423 rdataset->trust = 0;
424 rdataset->private1 = NULL;
425 rdataset->private2 = NULL;
426 rdataset->private3 = slab + reservelen;
429 * Reset iterator state.
431 rdataset->privateuint4 = 0;
432 rdataset->private5 = NULL;
435 unsigned int
436 dns_rdataslab_size(unsigned char *slab, unsigned int reservelen) {
437 unsigned int count, length;
438 unsigned char *current;
440 REQUIRE(slab != NULL);
442 current = slab + reservelen;
443 count = *current++ * 256;
444 count += *current++;
445 #if DNS_RDATASET_FIXED
446 current += (4 * count);
447 #endif
448 while (count > 0) {
449 count--;
450 length = *current++ * 256;
451 length += *current++;
452 #if DNS_RDATASET_FIXED
453 current += length + 2;
454 #else
455 current += length;
456 #endif
459 return ((unsigned int)(current - slab));
463 * Make the dns_rdata_t 'rdata' refer to the slab item
464 * beginning at '*current', which is part of a slab of type
465 * 'type' and class 'rdclass', and advance '*current' to
466 * point to the next item in the slab.
468 static inline void
469 rdata_from_slab(unsigned char **current,
470 dns_rdataclass_t rdclass, dns_rdatatype_t type,
471 dns_rdata_t *rdata)
473 unsigned char *tcurrent = *current;
474 isc_region_t region;
476 region.length = *tcurrent++ * 256;
477 region.length += *tcurrent++;
478 #if DNS_RDATASET_FIXED
479 tcurrent += 2;
480 #endif
481 region.base = tcurrent;
482 tcurrent += region.length;
483 dns_rdata_fromregion(rdata, rdclass, type, &region);
484 *current = tcurrent;
488 * Return true iff 'slab' (slab data of type 'type' and class 'rdclass')
489 * contains an rdata identical to 'rdata'. This does case insensitive
490 * comparisons per DNSSEC.
492 static inline isc_boolean_t
493 rdata_in_slab(unsigned char *slab, unsigned int reservelen,
494 dns_rdataclass_t rdclass, dns_rdatatype_t type,
495 dns_rdata_t *rdata)
497 unsigned int count, i;
498 unsigned char *current;
499 dns_rdata_t trdata = DNS_RDATA_INIT;
500 int n;
502 current = slab + reservelen;
503 count = *current++ * 256;
504 count += *current++;
506 #if DNS_RDATASET_FIXED
507 current += (4 * count);
508 #endif
510 for (i = 0; i < count; i++) {
511 rdata_from_slab(&current, rdclass, type, &trdata);
513 n = dns_rdata_compare(&trdata, rdata);
514 if (n == 0)
515 return (ISC_TRUE);
516 if (n > 0) /* In DNSSEC order. */
517 break;
518 dns_rdata_reset(&trdata);
520 return (ISC_FALSE);
523 isc_result_t
524 dns_rdataslab_merge(unsigned char *oslab, unsigned char *nslab,
525 unsigned int reservelen, isc_mem_t *mctx,
526 dns_rdataclass_t rdclass, dns_rdatatype_t type,
527 unsigned int flags, unsigned char **tslabp)
529 unsigned char *ocurrent, *ostart, *ncurrent, *tstart, *tcurrent;
530 unsigned int ocount, ncount, count, olength, tlength, tcount, length;
531 isc_region_t nregion;
532 dns_rdata_t ordata = DNS_RDATA_INIT;
533 dns_rdata_t nrdata = DNS_RDATA_INIT;
534 isc_boolean_t added_something = ISC_FALSE;
535 unsigned int oadded = 0;
536 unsigned int nadded = 0;
537 unsigned int nncount = 0;
538 #if DNS_RDATASET_FIXED
539 unsigned int oncount;
540 unsigned int norder = 0;
541 unsigned int oorder = 0;
542 unsigned char *offsetbase;
543 unsigned int *offsettable;
544 #endif
547 * XXX Need parameter to allow "delete rdatasets in nslab" merge,
548 * or perhaps another merge routine for this purpose.
551 REQUIRE(tslabp != NULL && *tslabp == NULL);
552 REQUIRE(oslab != NULL && nslab != NULL);
554 ocurrent = oslab + reservelen;
555 ocount = *ocurrent++ * 256;
556 ocount += *ocurrent++;
557 #if DNS_RDATASET_FIXED
558 ocurrent += (4 * ocount);
559 #endif
560 ostart = ocurrent;
561 ncurrent = nslab + reservelen;
562 ncount = *ncurrent++ * 256;
563 ncount += *ncurrent++;
564 #if DNS_RDATASET_FIXED
565 ncurrent += (4 * ncount);
566 #endif
567 INSIST(ocount > 0 && ncount > 0);
569 #if DNS_RDATASET_FIXED
570 oncount = ncount;
571 #endif
574 * Yes, this is inefficient!
578 * Figure out the length of the old slab's data.
580 olength = 0;
581 for (count = 0; count < ocount; count++) {
582 length = *ocurrent++ * 256;
583 length += *ocurrent++;
584 #if DNS_RDATASET_FIXED
585 olength += length + 8;
586 ocurrent += length + 2;
587 #else
588 olength += length + 2;
589 ocurrent += length;
590 #endif
594 * Start figuring out the target length and count.
596 tlength = reservelen + 2 + olength;
597 tcount = ocount;
600 * Add in the length of rdata in the new slab that aren't in
601 * the old slab.
603 do {
604 nregion.length = *ncurrent++ * 256;
605 nregion.length += *ncurrent++;
606 #if DNS_RDATASET_FIXED
607 ncurrent += 2; /* Skip order. */
608 #endif
609 nregion.base = ncurrent;
610 dns_rdata_init(&nrdata);
611 dns_rdata_fromregion(&nrdata, rdclass, type, &nregion);
612 if (!rdata_in_slab(oslab, reservelen, rdclass, type, &nrdata))
615 * This rdata isn't in the old slab.
617 #if DNS_RDATASET_FIXED
618 tlength += nregion.length + 8;
619 #else
620 tlength += nregion.length + 2;
621 #endif
622 tcount++;
623 nncount++;
624 added_something = ISC_TRUE;
626 ncurrent += nregion.length;
627 ncount--;
628 } while (ncount > 0);
629 ncount = nncount;
631 if (((flags & DNS_RDATASLAB_EXACT) != 0) &&
632 (tcount != ncount + ocount))
633 return (DNS_R_NOTEXACT);
635 if (!added_something && (flags & DNS_RDATASLAB_FORCE) == 0)
636 return (DNS_R_UNCHANGED);
639 * Ensure that singleton types are actually singletons.
641 if (tcount > 1 && dns_rdatatype_issingleton(type)) {
643 * We have a singleton type, but there's more than one
644 * RR in the rdataset.
646 return (DNS_R_SINGLETON);
649 if (tcount > 0xffff)
650 return (ISC_R_NOSPACE);
653 * Copy the reserved area from the new slab.
655 tstart = isc_mem_get(mctx, tlength);
656 if (tstart == NULL)
657 return (ISC_R_NOMEMORY);
658 memcpy(tstart, nslab, reservelen);
659 tcurrent = tstart + reservelen;
660 #if DNS_RDATASET_FIXED
661 offsetbase = tcurrent;
662 #endif
665 * Write the new count.
667 *tcurrent++ = (tcount & 0xff00) >> 8;
668 *tcurrent++ = (tcount & 0x00ff);
670 #if DNS_RDATASET_FIXED
672 * Skip offset table.
674 tcurrent += (tcount * 4);
676 offsettable = isc_mem_get(mctx,
677 (ocount + oncount) * sizeof(unsigned int));
678 if (offsettable == NULL) {
679 isc_mem_put(mctx, tstart, tlength);
680 return (ISC_R_NOMEMORY);
682 memset(offsettable, 0, (ocount + oncount) * sizeof(unsigned int));
683 #endif
686 * Merge the two slabs.
688 ocurrent = ostart;
689 INSIST(ocount != 0);
690 #if DNS_RDATASET_FIXED
691 oorder = ocurrent[2] * 256 + ocurrent[3];
692 INSIST(oorder < ocount);
693 #endif
694 rdata_from_slab(&ocurrent, rdclass, type, &ordata);
696 ncurrent = nslab + reservelen + 2;
697 #if DNS_RDATASET_FIXED
698 ncurrent += (4 * oncount);
699 #endif
701 if (ncount > 0) {
702 do {
703 dns_rdata_reset(&nrdata);
704 #if DNS_RDATASET_FIXED
705 norder = ncurrent[2] * 256 + ncurrent[3];
707 INSIST(norder < oncount);
708 #endif
709 rdata_from_slab(&ncurrent, rdclass, type, &nrdata);
710 } while (rdata_in_slab(oslab, reservelen, rdclass,
711 type, &nrdata));
714 while (oadded < ocount || nadded < ncount) {
715 isc_boolean_t fromold;
716 if (oadded == ocount)
717 fromold = ISC_FALSE;
718 else if (nadded == ncount)
719 fromold = ISC_TRUE;
720 else
721 fromold = ISC_TF(compare_rdata(&ordata, &nrdata) < 0);
722 if (fromold) {
723 #if DNS_RDATASET_FIXED
724 offsettable[oorder] = tcurrent - offsetbase;
725 #endif
726 length = ordata.length;
727 *tcurrent++ = (length & 0xff00) >> 8;
728 *tcurrent++ = (length & 0x00ff);
729 #if DNS_RDATASET_FIXED
730 tcurrent += 2; /* fill in later */
731 #endif
732 memcpy(tcurrent, ordata.data, length);
733 tcurrent += length;
734 oadded++;
735 if (oadded < ocount) {
736 dns_rdata_reset(&ordata);
737 #if DNS_RDATASET_FIXED
738 oorder = ocurrent[2] * 256 + ocurrent[3];
739 INSIST(oorder < ocount);
740 #endif
741 rdata_from_slab(&ocurrent, rdclass, type,
742 &ordata);
744 } else {
745 #if DNS_RDATASET_FIXED
746 offsettable[ocount + norder] = tcurrent - offsetbase;
747 #endif
748 length = nrdata.length;
749 *tcurrent++ = (length & 0xff00) >> 8;
750 *tcurrent++ = (length & 0x00ff);
751 #if DNS_RDATASET_FIXED
752 tcurrent += 2; /* fill in later */
753 #endif
754 memcpy(tcurrent, nrdata.data, length);
755 tcurrent += length;
756 nadded++;
757 if (nadded < ncount) {
758 do {
759 dns_rdata_reset(&nrdata);
760 #if DNS_RDATASET_FIXED
761 norder = ncurrent[2] * 256 + ncurrent[3];
762 INSIST(norder < oncount);
763 #endif
764 rdata_from_slab(&ncurrent, rdclass,
765 type, &nrdata);
766 } while (rdata_in_slab(oslab, reservelen,
767 rdclass, type,
768 &nrdata));
773 #if DNS_RDATASET_FIXED
774 fillin_offsets(offsetbase, offsettable, ocount + oncount);
776 isc_mem_put(mctx, offsettable,
777 (ocount + oncount) * sizeof(unsigned int));
778 #endif
780 INSIST(tcurrent == tstart + tlength);
782 *tslabp = tstart;
784 return (ISC_R_SUCCESS);
787 isc_result_t
788 dns_rdataslab_subtract(unsigned char *mslab, unsigned char *sslab,
789 unsigned int reservelen, isc_mem_t *mctx,
790 dns_rdataclass_t rdclass, dns_rdatatype_t type,
791 unsigned int flags, unsigned char **tslabp)
793 unsigned char *mcurrent, *sstart, *scurrent, *tstart, *tcurrent;
794 unsigned int mcount, scount, rcount ,count, tlength, tcount, i;
795 dns_rdata_t srdata = DNS_RDATA_INIT;
796 dns_rdata_t mrdata = DNS_RDATA_INIT;
797 #if DNS_RDATASET_FIXED
798 unsigned char *offsetbase;
799 unsigned int *offsettable;
800 unsigned int order;
801 #endif
803 REQUIRE(tslabp != NULL && *tslabp == NULL);
804 REQUIRE(mslab != NULL && sslab != NULL);
806 mcurrent = mslab + reservelen;
807 mcount = *mcurrent++ * 256;
808 mcount += *mcurrent++;
809 scurrent = sslab + reservelen;
810 scount = *scurrent++ * 256;
811 scount += *scurrent++;
812 INSIST(mcount > 0 && scount > 0);
815 * Yes, this is inefficient!
819 * Start figuring out the target length and count.
821 tlength = reservelen + 2;
822 tcount = 0;
823 rcount = 0;
825 #if DNS_RDATASET_FIXED
826 mcurrent += 4 * mcount;
827 scurrent += 4 * scount;
828 #endif
829 sstart = scurrent;
832 * Add in the length of rdata in the mslab that aren't in
833 * the sslab.
835 for (i = 0; i < mcount; i++) {
836 unsigned char *mrdatabegin = mcurrent;
837 rdata_from_slab(&mcurrent, rdclass, type, &mrdata);
838 scurrent = sstart;
839 for (count = 0; count < scount; count++) {
840 dns_rdata_reset(&srdata);
841 rdata_from_slab(&scurrent, rdclass, type, &srdata);
842 if (dns_rdata_compare(&mrdata, &srdata) == 0)
843 break;
845 if (count == scount) {
847 * This rdata isn't in the sslab, and thus isn't
848 * being subtracted.
850 tlength += mcurrent - mrdatabegin;
851 tcount++;
852 } else
853 rcount++;
854 dns_rdata_reset(&mrdata);
857 #if DNS_RDATASET_FIXED
858 tlength += (4 * tcount);
859 #endif
862 * Check that all the records originally existed. The numeric
863 * check only works as rdataslabs do not contain duplicates.
865 if (((flags & DNS_RDATASLAB_EXACT) != 0) && (rcount != scount))
866 return (DNS_R_NOTEXACT);
869 * Don't continue if the new rdataslab would be empty.
871 if (tcount == 0)
872 return (DNS_R_NXRRSET);
875 * If nothing is going to change, we can stop.
877 if (rcount == 0)
878 return (DNS_R_UNCHANGED);
881 * Copy the reserved area from the mslab.
883 tstart = isc_mem_get(mctx, tlength);
884 if (tstart == NULL)
885 return (ISC_R_NOMEMORY);
886 memcpy(tstart, mslab, reservelen);
887 tcurrent = tstart + reservelen;
888 #if DNS_RDATASET_FIXED
889 offsetbase = tcurrent;
891 offsettable = isc_mem_get(mctx, mcount * sizeof(unsigned int));
892 if (offsettable == NULL) {
893 isc_mem_put(mctx, tstart, tlength);
894 return (ISC_R_NOMEMORY);
896 memset(offsettable, 0, mcount * sizeof(unsigned int));
897 #endif
900 * Write the new count.
902 *tcurrent++ = (tcount & 0xff00) >> 8;
903 *tcurrent++ = (tcount & 0x00ff);
905 #if DNS_RDATASET_FIXED
906 tcurrent += (4 * tcount);
907 #endif
910 * Copy the parts of mslab not in sslab.
912 mcurrent = mslab + reservelen;
913 mcount = *mcurrent++ * 256;
914 mcount += *mcurrent++;
915 #if DNS_RDATASET_FIXED
916 mcurrent += (4 * mcount);
917 #endif
918 for (i = 0; i < mcount; i++) {
919 unsigned char *mrdatabegin = mcurrent;
920 #if DNS_RDATASET_FIXED
921 order = mcurrent[2] * 256 + mcurrent[3];
922 INSIST(order < mcount);
923 #endif
924 rdata_from_slab(&mcurrent, rdclass, type, &mrdata);
925 scurrent = sstart;
926 for (count = 0; count < scount; count++) {
927 dns_rdata_reset(&srdata);
928 rdata_from_slab(&scurrent, rdclass, type, &srdata);
929 if (dns_rdata_compare(&mrdata, &srdata) == 0)
930 break;
932 if (count == scount) {
934 * This rdata isn't in the sslab, and thus should be
935 * copied to the tslab.
937 unsigned int length = mcurrent - mrdatabegin;
938 #if DNS_RDATASET_FIXED
939 offsettable[order] = tcurrent - offsetbase;
940 #endif
941 memcpy(tcurrent, mrdatabegin, length);
942 tcurrent += length;
944 dns_rdata_reset(&mrdata);
947 #if DNS_RDATASET_FIXED
948 fillin_offsets(offsetbase, offsettable, mcount);
950 isc_mem_put(mctx, offsettable, mcount * sizeof(unsigned int));
951 #endif
953 INSIST(tcurrent == tstart + tlength);
955 *tslabp = tstart;
957 return (ISC_R_SUCCESS);
960 isc_boolean_t
961 dns_rdataslab_equal(unsigned char *slab1, unsigned char *slab2,
962 unsigned int reservelen)
964 unsigned char *current1, *current2;
965 unsigned int count1, count2;
966 unsigned int length1, length2;
968 current1 = slab1 + reservelen;
969 count1 = *current1++ * 256;
970 count1 += *current1++;
972 current2 = slab2 + reservelen;
973 count2 = *current2++ * 256;
974 count2 += *current2++;
976 if (count1 != count2)
977 return (ISC_FALSE);
979 #if DNS_RDATASET_FIXED
980 current1 += (4 * count1);
981 current2 += (4 * count2);
982 #endif
984 while (count1 > 0) {
985 length1 = *current1++ * 256;
986 length1 += *current1++;
988 length2 = *current2++ * 256;
989 length2 += *current2++;
991 #if DNS_RDATASET_FIXED
992 current1 += 2;
993 current2 += 2;
994 #endif
996 if (length1 != length2 ||
997 memcmp(current1, current2, length1) != 0)
998 return (ISC_FALSE);
1000 current1 += length1;
1001 current2 += length1;
1003 count1--;
1005 return (ISC_TRUE);
1008 isc_boolean_t
1009 dns_rdataslab_equalx(unsigned char *slab1, unsigned char *slab2,
1010 unsigned int reservelen, dns_rdataclass_t rdclass,
1011 dns_rdatatype_t type)
1013 unsigned char *current1, *current2;
1014 unsigned int count1, count2;
1015 dns_rdata_t rdata1 = DNS_RDATA_INIT;
1016 dns_rdata_t rdata2 = DNS_RDATA_INIT;
1018 current1 = slab1 + reservelen;
1019 count1 = *current1++ * 256;
1020 count1 += *current1++;
1022 current2 = slab2 + reservelen;
1023 count2 = *current2++ * 256;
1024 count2 += *current2++;
1026 if (count1 != count2)
1027 return (ISC_FALSE);
1029 #if DNS_RDATASET_FIXED
1030 current1 += (4 * count1);
1031 current2 += (4 * count2);
1032 #endif
1034 while (count1-- > 0) {
1035 rdata_from_slab(&current1, rdclass, type, &rdata1);
1036 rdata_from_slab(&current2, rdclass, type, &rdata2);
1037 if (dns_rdata_compare(&rdata1, &rdata2) != 0)
1038 return (ISC_FALSE);
1039 dns_rdata_reset(&rdata1);
1040 dns_rdata_reset(&rdata2);
1042 return (ISC_TRUE);