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