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