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 $ */
27 #include <isc/region.h>
28 #include <isc/string.h> /* Required for HP/UX (and others?) */
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)
44 * data length (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)
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
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".
83 /*% Note: the "const void *" are just to make qsort happy. */
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
93 fillin_offsets(unsigned char *offsetbase
, unsigned int *offsettable
,
99 for (i
= 0, j
= 0; i
< length
; i
++) {
101 if (offsettable
[i
] == 0)
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;
124 dns_rdataslab_fromrdataset(dns_rdataset_t
*rdataset
, isc_mem_t
*mctx
,
125 isc_region_t
*region
, unsigned int reservelen
)
128 unsigned char *rawbuf
;
129 #if DNS_RDATASET_FIXED
130 unsigned char *offsetbase
;
137 #if DNS_RDATASET_FIXED
138 unsigned int *offsettable
;
141 buflen
= reservelen
+ 2;
143 nalloc
= dns_rdataset_count(rdataset
);
146 return (ISC_R_FAILURE
);
149 return (ISC_R_NOSPACE
);
151 x
= isc_mem_get(mctx
, nalloc
* sizeof(struct xrdata
));
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
)
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
168 result
= dns_rdataset_next(rdataset
);
170 if (result
!= ISC_R_NOMORE
)
174 * Somehow we iterated over fewer rdatas than
175 * dns_rdataset_count() said there were!
177 result
= ISC_R_FAILURE
;
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
;
209 #if DNS_RDATASET_FIXED
210 buflen
+= (8 + x
[i
-1].rdata
.length
);
212 buflen
+= (2 + x
[i
-1].rdata
.length
);
216 * Don't forget the last item!
218 #if DNS_RDATASET_FIXED
219 buflen
+= (8 + x
[i
-1].rdata
.length
);
221 buflen
+= (2 + x
[i
-1].rdata
.length
);
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
;
237 * Allocate the memory, set up a buffer, start copying in
240 rawbuf
= isc_mem_get(mctx
, buflen
);
241 if (rawbuf
== NULL
) {
242 result
= ISC_R_NOMEMORY
;
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
;
254 memset(offsettable
, 0, nalloc
* sizeof(unsigned int));
257 region
->base
= rawbuf
;
258 region
->length
= buflen
;
260 rawbuf
+= reservelen
;
261 #if DNS_RDATASET_FIXED
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;
273 for (i
= 0; i
< nalloc
; i
++) {
274 if (x
[i
].rdata
.data
== NULL
)
276 #if DNS_RDATASET_FIXED
277 offsettable
[x
[i
].order
] = rawbuf
- offsetbase
;
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 */
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));
293 result
= ISC_R_SUCCESS
;
296 isc_mem_put(mctx
, x
, nalloc
* sizeof(struct xrdata
));
301 rdataset_disassociate(dns_rdataset_t
*rdataset
) {
306 rdataset_first(dns_rdataset_t
*rdataset
) {
307 unsigned char *raw
= rdataset
->private3
;
310 count
= raw
[0] * 256 + raw
[1];
312 rdataset
->private5
= NULL
;
313 return (ISC_R_NOMORE
);
315 #if DNS_RDATASET_FIXED
316 raw
+= 2 + (4 * count
);
321 * The privateuint4 field is the number of rdata beyond the cursor
322 * position, so we decrement the total count by one before storing
326 rdataset
->privateuint4
= count
;
327 rdataset
->private5
= raw
;
329 return (ISC_R_SUCCESS
);
333 rdataset_next(dns_rdataset_t
*rdataset
) {
338 count
= rdataset
->privateuint4
;
340 return (ISC_R_NOMORE
);
342 rdataset
->privateuint4
= count
;
343 raw
= rdataset
->private5
;
344 length
= raw
[0] * 256 + raw
[1];
345 #if DNS_RDATASET_FIXED
350 rdataset
->private5
= raw
;
352 return (ISC_R_SUCCESS
);
356 rdataset_current(dns_rdataset_t
*rdataset
, dns_rdata_t
*rdata
) {
357 unsigned char *raw
= rdataset
->private5
;
360 REQUIRE(raw
!= NULL
);
362 r
.length
= raw
[0] * 256 + raw
[1];
363 #if DNS_RDATASET_FIXED
369 dns_rdata_fromregion(rdata
, rdataset
->rdclass
, rdataset
->type
, &r
);
373 rdataset_clone(dns_rdataset_t
*source
, dns_rdataset_t
*target
) {
377 * Reset iterator state.
379 target
->privateuint4
= 0;
380 target
->private5
= NULL
;
384 rdataset_count(dns_rdataset_t
*rdataset
) {
385 unsigned char *raw
= rdataset
->private3
;
388 count
= raw
[0] * 256 + raw
[1];
393 static dns_rdatasetmethods_t rdataset_methods
= {
394 rdataset_disassociate
,
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
;
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
;
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;
445 #if DNS_RDATASET_FIXED
446 current
+= (4 * count
);
450 length
= *current
++ * 256;
451 length
+= *current
++;
452 #if DNS_RDATASET_FIXED
453 current
+= length
+ 2;
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.
469 rdata_from_slab(unsigned char **current
,
470 dns_rdataclass_t rdclass
, dns_rdatatype_t type
,
473 unsigned char *tcurrent
= *current
;
476 region
.length
= *tcurrent
++ * 256;
477 region
.length
+= *tcurrent
++;
478 #if DNS_RDATASET_FIXED
481 region
.base
= tcurrent
;
482 tcurrent
+= region
.length
;
483 dns_rdata_fromregion(rdata
, rdclass
, type
, ®ion
);
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
,
497 unsigned int count
, i
;
498 unsigned char *current
;
499 dns_rdata_t trdata
= DNS_RDATA_INIT
;
502 current
= slab
+ reservelen
;
503 count
= *current
++ * 256;
506 #if DNS_RDATASET_FIXED
507 current
+= (4 * count
);
510 for (i
= 0; i
< count
; i
++) {
511 rdata_from_slab(¤t
, rdclass
, type
, &trdata
);
513 n
= dns_rdata_compare(&trdata
, rdata
);
516 if (n
> 0) /* In DNSSEC order. */
518 dns_rdata_reset(&trdata
);
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
;
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
);
561 ncurrent
= nslab
+ reservelen
;
562 ncount
= *ncurrent
++ * 256;
563 ncount
+= *ncurrent
++;
564 #if DNS_RDATASET_FIXED
565 ncurrent
+= (4 * ncount
);
567 INSIST(ocount
> 0 && ncount
> 0);
569 #if DNS_RDATASET_FIXED
574 * Yes, this is inefficient!
578 * Figure out the length of the old slab's data.
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;
588 olength
+= length
+ 2;
594 * Start figuring out the target length and count.
596 tlength
= reservelen
+ 2 + olength
;
600 * Add in the length of rdata in the new slab that aren't in
604 nregion
.length
= *ncurrent
++ * 256;
605 nregion
.length
+= *ncurrent
++;
606 #if DNS_RDATASET_FIXED
607 ncurrent
+= 2; /* Skip order. */
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;
620 tlength
+= nregion
.length
+ 2;
624 added_something
= ISC_TRUE
;
626 ncurrent
+= nregion
.length
;
628 } while (ncount
> 0);
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
);
650 return (ISC_R_NOSPACE
);
653 * Copy the reserved area from the new slab.
655 tstart
= isc_mem_get(mctx
, tlength
);
657 return (ISC_R_NOMEMORY
);
658 memcpy(tstart
, nslab
, reservelen
);
659 tcurrent
= tstart
+ reservelen
;
660 #if DNS_RDATASET_FIXED
661 offsetbase
= tcurrent
;
665 * Write the new count.
667 *tcurrent
++ = (tcount
& 0xff00) >> 8;
668 *tcurrent
++ = (tcount
& 0x00ff);
670 #if DNS_RDATASET_FIXED
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));
686 * Merge the two slabs.
690 #if DNS_RDATASET_FIXED
691 oorder
= ocurrent
[2] * 256 + ocurrent
[3];
692 INSIST(oorder
< ocount
);
694 rdata_from_slab(&ocurrent
, rdclass
, type
, &ordata
);
696 ncurrent
= nslab
+ reservelen
+ 2;
697 #if DNS_RDATASET_FIXED
698 ncurrent
+= (4 * oncount
);
703 dns_rdata_reset(&nrdata
);
704 #if DNS_RDATASET_FIXED
705 norder
= ncurrent
[2] * 256 + ncurrent
[3];
707 INSIST(norder
< oncount
);
709 rdata_from_slab(&ncurrent
, rdclass
, type
, &nrdata
);
710 } while (rdata_in_slab(oslab
, reservelen
, rdclass
,
714 while (oadded
< ocount
|| nadded
< ncount
) {
715 isc_boolean_t fromold
;
716 if (oadded
== ocount
)
718 else if (nadded
== ncount
)
721 fromold
= ISC_TF(compare_rdata(&ordata
, &nrdata
) < 0);
723 #if DNS_RDATASET_FIXED
724 offsettable
[oorder
] = tcurrent
- offsetbase
;
726 length
= ordata
.length
;
727 *tcurrent
++ = (length
& 0xff00) >> 8;
728 *tcurrent
++ = (length
& 0x00ff);
729 #if DNS_RDATASET_FIXED
730 tcurrent
+= 2; /* fill in later */
732 memcpy(tcurrent
, ordata
.data
, length
);
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
);
741 rdata_from_slab(&ocurrent
, rdclass
, type
,
745 #if DNS_RDATASET_FIXED
746 offsettable
[ocount
+ norder
] = tcurrent
- offsetbase
;
748 length
= nrdata
.length
;
749 *tcurrent
++ = (length
& 0xff00) >> 8;
750 *tcurrent
++ = (length
& 0x00ff);
751 #if DNS_RDATASET_FIXED
752 tcurrent
+= 2; /* fill in later */
754 memcpy(tcurrent
, nrdata
.data
, length
);
757 if (nadded
< ncount
) {
759 dns_rdata_reset(&nrdata
);
760 #if DNS_RDATASET_FIXED
761 norder
= ncurrent
[2] * 256 + ncurrent
[3];
762 INSIST(norder
< oncount
);
764 rdata_from_slab(&ncurrent
, rdclass
,
766 } while (rdata_in_slab(oslab
, reservelen
,
773 #if DNS_RDATASET_FIXED
774 fillin_offsets(offsetbase
, offsettable
, ocount
+ oncount
);
776 isc_mem_put(mctx
, offsettable
,
777 (ocount
+ oncount
) * sizeof(unsigned int));
780 INSIST(tcurrent
== tstart
+ tlength
);
784 return (ISC_R_SUCCESS
);
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
;
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;
825 #if DNS_RDATASET_FIXED
826 mcurrent
+= 4 * mcount
;
827 scurrent
+= 4 * scount
;
832 * Add in the length of rdata in the mslab that aren't in
835 for (i
= 0; i
< mcount
; i
++) {
836 unsigned char *mrdatabegin
= mcurrent
;
837 rdata_from_slab(&mcurrent
, rdclass
, type
, &mrdata
);
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)
845 if (count
== scount
) {
847 * This rdata isn't in the sslab, and thus isn't
850 tlength
+= mcurrent
- mrdatabegin
;
854 dns_rdata_reset(&mrdata
);
857 #if DNS_RDATASET_FIXED
858 tlength
+= (4 * tcount
);
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.
872 return (DNS_R_NXRRSET
);
875 * If nothing is going to change, we can stop.
878 return (DNS_R_UNCHANGED
);
881 * Copy the reserved area from the mslab.
883 tstart
= isc_mem_get(mctx
, tlength
);
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));
900 * Write the new count.
902 *tcurrent
++ = (tcount
& 0xff00) >> 8;
903 *tcurrent
++ = (tcount
& 0x00ff);
905 #if DNS_RDATASET_FIXED
906 tcurrent
+= (4 * tcount
);
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
);
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
);
924 rdata_from_slab(&mcurrent
, rdclass
, type
, &mrdata
);
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)
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
;
941 memcpy(tcurrent
, mrdatabegin
, 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));
953 INSIST(tcurrent
== tstart
+ tlength
);
957 return (ISC_R_SUCCESS
);
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
)
979 #if DNS_RDATASET_FIXED
980 current1
+= (4 * count1
);
981 current2
+= (4 * count2
);
985 length1
= *current1
++ * 256;
986 length1
+= *current1
++;
988 length2
= *current2
++ * 256;
989 length2
+= *current2
++;
991 #if DNS_RDATASET_FIXED
996 if (length1
!= length2
||
997 memcmp(current1
, current2
, length1
) != 0)
1000 current1
+= length1
;
1001 current2
+= length1
;
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
)
1029 #if DNS_RDATASET_FIXED
1030 current1
+= (4 * count1
);
1031 current2
+= (4 * count2
);
1034 while (count1
-- > 0) {
1035 rdata_from_slab(¤t1
, rdclass
, type
, &rdata1
);
1036 rdata_from_slab(¤t2
, rdclass
, type
, &rdata2
);
1037 if (dns_rdata_compare(&rdata1
, &rdata2
) != 0)
1039 dns_rdata_reset(&rdata1
);
1040 dns_rdata_reset(&rdata2
);