Merge branch 'vendor/OPENSSL'
[dragonfly.git] / contrib / bind-9.3 / lib / dns / rdataslab.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: rdataslab.c,v 1.29.2.2.2.6 2004/03/08 09:04:31 marka Exp $ */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23
24 #include <isc/mem.h>
25 #include <isc/region.h>
26 #include <isc/string.h>         /* Required for HP/UX (and others?) */
27 #include <isc/util.h>
28
29 #include <dns/result.h>
30 #include <dns/rdata.h>
31 #include <dns/rdataset.h>
32 #include <dns/rdataslab.h>
33
34 /* Note: the "const void *" are just to make qsort happy. */
35 static int
36 compare_rdata(const void *p1, const void *p2) {
37         const dns_rdata_t *rdata1 = p1;
38         const dns_rdata_t *rdata2 = p2;
39         return (dns_rdata_compare(rdata1, rdata2));
40 }
41
42 isc_result_t
43 dns_rdataslab_fromrdataset(dns_rdataset_t *rdataset, isc_mem_t *mctx,
44                            isc_region_t *region, unsigned int reservelen)
45 {
46         dns_rdata_t    *rdatas;
47         unsigned char  *rawbuf;
48         unsigned int    buflen;
49         isc_result_t    result;
50         unsigned int    nitems;
51         unsigned int    nalloc;
52         unsigned int    i;
53
54         buflen = reservelen + 2;
55
56         nalloc = dns_rdataset_count(rdataset);
57         nitems = nalloc;
58         if (nitems == 0)
59                 return (ISC_R_FAILURE);
60
61         rdatas = isc_mem_get(mctx, nalloc * sizeof(dns_rdata_t));
62         if (rdatas == NULL)
63                 return (ISC_R_NOMEMORY);
64
65         /*
66          * Save all of the rdata members into an array.
67          */
68         result = dns_rdataset_first(rdataset);
69         if (result != ISC_R_SUCCESS)
70                 goto free_rdatas;
71         for (i = 0; i < nalloc && result == ISC_R_SUCCESS; i++) {
72                 INSIST(result == ISC_R_SUCCESS);
73                 dns_rdata_init(&rdatas[i]);
74                 dns_rdataset_current(rdataset, &rdatas[i]);
75                 result = dns_rdataset_next(rdataset);
76         }
77         if (result != ISC_R_NOMORE)
78                 goto free_rdatas;
79         if (i != nalloc) {
80                 /*
81                  * Somehow we iterated over fewer rdatas than
82                  * dns_rdataset_count() said there were!
83                  */
84                 result = ISC_R_FAILURE;
85                 goto free_rdatas;
86         }
87
88         qsort(rdatas, nalloc, sizeof(dns_rdata_t), compare_rdata);
89
90         /*
91          * Remove duplicates and compute the total storage required.
92          *
93          * If an rdata is not a duplicate, accumulate the storage size
94          * required for the rdata.  We do not store the class, type, etc,
95          * just the rdata, so our overhead is 2 bytes for the number of
96          * records, and 2 for each rdata length, and then the rdata itself.
97          */
98         for (i = 1; i < nalloc; i++) {
99                 if (compare_rdata(&rdatas[i-1], &rdatas[i]) == 0) {
100                         rdatas[i-1].data = NULL;
101                         rdatas[i-1].length = 0;
102                         nitems--;
103                 } else
104                         buflen += (2 + rdatas[i-1].length);
105         }
106         /*
107          * Don't forget the last item!
108          */
109         buflen += (2 + rdatas[i-1].length);
110
111         /*
112          * Ensure that singleton types are actually singletons.
113          */
114         if (nitems > 1 && dns_rdatatype_issingleton(rdataset->type)) {
115                 /*
116                  * We have a singleton type, but there's more than one
117                  * RR in the rdataset.
118                  */
119                 result = DNS_R_SINGLETON;
120                 goto free_rdatas;
121         }
122
123         /*
124          * Allocate the memory, set up a buffer, start copying in
125          * data.
126          */
127         rawbuf = isc_mem_get(mctx, buflen);
128         if (rawbuf == NULL) {
129                 result = ISC_R_NOMEMORY;
130                 goto free_rdatas;
131         }
132
133         region->base = rawbuf;
134         region->length = buflen;
135
136         rawbuf += reservelen;
137
138         *rawbuf++ = (nitems & 0xff00) >> 8;
139         *rawbuf++ = (nitems & 0x00ff);
140         for (i = 0; i < nalloc; i++) {
141                 if (rdatas[i].data == NULL)
142                         continue;
143                 *rawbuf++ = (rdatas[i].length & 0xff00) >> 8;
144                 *rawbuf++ = (rdatas[i].length & 0x00ff);
145                 memcpy(rawbuf, rdatas[i].data, rdatas[i].length);
146                 rawbuf += rdatas[i].length;
147         }
148         result = ISC_R_SUCCESS;
149
150  free_rdatas:
151         isc_mem_put(mctx, rdatas, nalloc * sizeof(dns_rdata_t));
152         return (result);
153 }
154
155 static void
156 rdataset_disassociate(dns_rdataset_t *rdataset) {
157         UNUSED(rdataset);
158 }
159
160 static isc_result_t
161 rdataset_first(dns_rdataset_t *rdataset) {
162         unsigned char *raw = rdataset->private3;
163         unsigned int count;
164
165         count = raw[0] * 256 + raw[1];
166         if (count == 0) {
167                 rdataset->private5 = NULL;
168                 return (ISC_R_NOMORE);
169         }
170         raw += 2;
171         /*
172          * The privateuint4 field is the number of rdata beyond the cursor
173          * position, so we decrement the total count by one before storing
174          * it.
175          */
176         count--;
177         rdataset->privateuint4 = count;
178         rdataset->private5 = raw;
179
180         return (ISC_R_SUCCESS);
181 }
182
183 static isc_result_t
184 rdataset_next(dns_rdataset_t *rdataset) {
185         unsigned int count;
186         unsigned int length;
187         unsigned char *raw;
188
189         count = rdataset->privateuint4;
190         if (count == 0)
191                 return (ISC_R_NOMORE);
192         count--;
193         rdataset->privateuint4 = count;
194         raw = rdataset->private5;
195         length = raw[0] * 256 + raw[1];
196         raw += length + 2;
197         rdataset->private5 = raw;
198
199         return (ISC_R_SUCCESS);
200 }
201
202 static void
203 rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
204         unsigned char *raw = rdataset->private5;
205         isc_region_t r;
206
207         REQUIRE(raw != NULL);
208
209         r.length = raw[0] * 256 + raw[1];
210         raw += 2;
211         r.base = raw;
212         dns_rdata_fromregion(rdata, rdataset->rdclass, rdataset->type, &r);
213 }
214
215 static void
216 rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
217         *target = *source;
218
219         /*
220          * Reset iterator state.
221          */
222         target->privateuint4 = 0;
223         target->private5 = NULL;
224 }
225
226 static unsigned int
227 rdataset_count(dns_rdataset_t *rdataset) {
228         unsigned char *raw = rdataset->private3;
229         unsigned int count;
230
231         count = raw[0] * 256 + raw[1];
232
233         return (count);
234 }
235
236 static dns_rdatasetmethods_t rdataset_methods = {
237         rdataset_disassociate,
238         rdataset_first,
239         rdataset_next,
240         rdataset_current,
241         rdataset_clone,
242         rdataset_count,
243         NULL,
244         NULL
245 };
246
247 void
248 dns_rdataslab_tordataset(unsigned char *slab, unsigned int reservelen,
249                          dns_rdataclass_t rdclass, dns_rdatatype_t rdtype,
250                          dns_rdatatype_t covers, dns_ttl_t ttl,
251                          dns_rdataset_t *rdataset)
252 {
253         REQUIRE(slab != NULL);
254         REQUIRE(!dns_rdataset_isassociated(rdataset));
255
256         rdataset->methods = &rdataset_methods;
257         rdataset->rdclass = rdclass;
258         rdataset->type = rdtype;
259         rdataset->covers = covers;
260         rdataset->ttl = ttl;
261         rdataset->trust = 0;
262         rdataset->private1 = NULL;
263         rdataset->private2 = NULL;
264         rdataset->private3 = slab + reservelen;
265
266         /*
267          * Reset iterator state.
268          */
269         rdataset->privateuint4 = 0;
270         rdataset->private5 = NULL;
271 }
272
273 unsigned int
274 dns_rdataslab_size(unsigned char *slab, unsigned int reservelen) {
275         unsigned int count, length;
276         unsigned char *current;
277
278         REQUIRE(slab != NULL);
279
280         current = slab + reservelen;
281         count = *current++ * 256;
282         count += *current++;
283         while (count > 0) {
284                 count--;
285                 length = *current++ * 256;
286                 length += *current++;
287                 current += length;
288         }
289
290         return ((unsigned int)(current - slab));
291 }
292
293 /*
294  * Make the dns_rdata_t 'rdata' refer to the slab item
295  * beginning at '*current', which is part of a slab of type
296  * 'type' and class 'rdclass', and advance '*current' to
297  * point to the next item in the slab.
298  */
299 static inline void
300 rdata_from_slab(unsigned char **current,
301               dns_rdataclass_t rdclass, dns_rdatatype_t type,
302               dns_rdata_t *rdata)
303 {
304         unsigned char *tcurrent = *current;
305         isc_region_t region;
306
307         region.length = *tcurrent++ * 256;
308         region.length += *tcurrent++;
309         region.base = tcurrent;
310         tcurrent += region.length;
311         dns_rdata_fromregion(rdata, rdclass, type, &region);
312         *current = tcurrent;
313 }
314
315 /*
316  * Return true iff 'slab' (slab data of type 'type' and class 'rdclass')
317  * contains an rdata identical to 'rdata'.  This does case insensitive
318  * comparisons per DNSSEC.
319  */
320 static inline isc_boolean_t
321 rdata_in_slab(unsigned char *slab, unsigned int reservelen,
322               dns_rdataclass_t rdclass, dns_rdatatype_t type,
323               dns_rdata_t *rdata)
324 {
325         unsigned int count, i;
326         unsigned char *current;
327         dns_rdata_t trdata = DNS_RDATA_INIT;
328
329         current = slab + reservelen;
330         count = *current++ * 256;
331         count += *current++;
332
333         for (i = 0; i < count; i++) {
334                 rdata_from_slab(&current, rdclass, type, &trdata);
335                 if (dns_rdata_compare(&trdata, rdata) == 0)
336                         return (ISC_TRUE);
337                 dns_rdata_reset(&trdata);
338         }
339         return (ISC_FALSE);
340 }
341
342 isc_result_t
343 dns_rdataslab_merge(unsigned char *oslab, unsigned char *nslab,
344                     unsigned int reservelen, isc_mem_t *mctx,
345                     dns_rdataclass_t rdclass, dns_rdatatype_t type,
346                     unsigned int flags, unsigned char **tslabp)
347 {
348         unsigned char *ocurrent, *ostart, *ncurrent, *tstart, *tcurrent;
349         unsigned int ocount, ncount, count, olength, tlength, tcount, length;
350         isc_region_t nregion;
351         dns_rdata_t ordata = DNS_RDATA_INIT;
352         dns_rdata_t nrdata = DNS_RDATA_INIT;
353         isc_boolean_t added_something = ISC_FALSE;
354         unsigned int oadded = 0;
355         unsigned int nadded = 0;
356         unsigned int nncount = 0;
357
358         /*
359          * XXX  Need parameter to allow "delete rdatasets in nslab" merge,
360          * or perhaps another merge routine for this purpose.
361          */
362
363         REQUIRE(tslabp != NULL && *tslabp == NULL);
364         REQUIRE(oslab != NULL && nslab != NULL);
365
366         ocurrent = oslab + reservelen;
367         ocount = *ocurrent++ * 256;
368         ocount += *ocurrent++;
369         ostart = ocurrent;
370         ncurrent = nslab + reservelen;
371         ncount = *ncurrent++ * 256;
372         ncount += *ncurrent++;
373         INSIST(ocount > 0 && ncount > 0);
374
375         /*
376          * Yes, this is inefficient!
377          */
378
379         /*
380          * Figure out the length of the old slab's data.
381          */
382         olength = 0;
383         for (count = 0; count < ocount; count++) {
384                 length = *ocurrent++ * 256;
385                 length += *ocurrent++;
386                 olength += length + 2;
387                 ocurrent += length;
388         }
389
390         /*
391          * Start figuring out the target length and count.
392          */
393         tlength = reservelen + 2 + olength;
394         tcount = ocount;
395
396         /*
397          * Add in the length of rdata in the new slab that aren't in
398          * the old slab.
399          */
400         do {
401                 nregion.length = *ncurrent++ * 256;
402                 nregion.length += *ncurrent++;
403                 nregion.base = ncurrent;
404                 dns_rdata_init(&nrdata);
405                 dns_rdata_fromregion(&nrdata, rdclass, type, &nregion);
406                 if (!rdata_in_slab(oslab, reservelen, rdclass, type, &nrdata))
407                 {
408                         /*
409                          * This rdata isn't in the old slab.
410                          */
411                         tlength += nregion.length + 2;
412                         tcount++;
413                         nncount++;
414                         added_something = ISC_TRUE;
415                 }
416                 ncurrent += nregion.length;
417                 ncount--;
418         } while (ncount > 0);
419         ncount = nncount;
420
421         if (((flags & DNS_RDATASLAB_EXACT) != 0) &&
422             (tcount != ncount + ocount))
423                 return (DNS_R_NOTEXACT);
424
425         if (!added_something && (flags & DNS_RDATASLAB_FORCE) == 0)
426                 return (DNS_R_UNCHANGED);
427
428         /*
429          * Ensure that singleton types are actually singletons.
430          */
431         if (tcount > 1 && dns_rdatatype_issingleton(type)) {
432                 /*
433                  * We have a singleton type, but there's more than one
434                  * RR in the rdataset.
435                  */
436                 return (DNS_R_SINGLETON);
437         }
438
439         /*
440          * Copy the reserved area from the new slab.
441          */
442         tstart = isc_mem_get(mctx, tlength);
443         if (tstart == NULL)
444                 return (ISC_R_NOMEMORY);
445         memcpy(tstart, nslab, reservelen);
446         tcurrent = tstart + reservelen;
447
448         /*
449          * Write the new count.
450          */
451         *tcurrent++ = (tcount & 0xff00) >> 8;
452         *tcurrent++ = (tcount & 0x00ff);
453
454         /*
455          * Merge the two slabs.
456          */
457         ocurrent = ostart;
458         INSIST(ocount != 0);
459         rdata_from_slab(&ocurrent, rdclass, type, &ordata);
460
461         ncurrent = nslab + reservelen + 2;
462         if (ncount > 0) {
463                 do {
464                         dns_rdata_reset(&nrdata);
465                         rdata_from_slab(&ncurrent, rdclass, type, &nrdata);
466                 } while (rdata_in_slab(oslab, reservelen, rdclass,
467                                        type, &nrdata));
468         }
469
470         while (oadded < ocount || nadded < ncount) {
471                 isc_boolean_t fromold;
472                 if (oadded == ocount)
473                         fromold = ISC_FALSE;
474                 else if (nadded == ncount)
475                         fromold = ISC_TRUE;
476                 else
477                         fromold = ISC_TF(compare_rdata(&ordata, &nrdata) < 0);
478                 if (fromold) {
479                         length = ordata.length;
480                         *tcurrent++ = (length & 0xff00) >> 8;
481                         *tcurrent++ = (length & 0x00ff);
482                         memcpy(tcurrent, ordata.data, length);
483                         tcurrent += length;
484                         oadded++;
485                         if (oadded < ocount) {
486                                 dns_rdata_reset(&ordata);
487                                 rdata_from_slab(&ocurrent, rdclass, type,
488                                                 &ordata);
489                         }
490                 } else {
491                         length = nrdata.length;
492                         *tcurrent++ = (length & 0xff00) >> 8;
493                         *tcurrent++ = (length & 0x00ff);
494                         memcpy(tcurrent, nrdata.data, length);
495                         tcurrent += length;
496                         nadded++;
497                         if (nadded < ncount) {
498                                 do {
499                                         dns_rdata_reset(&nrdata);
500                                         rdata_from_slab(&ncurrent, rdclass,
501                                                         type, &nrdata);
502                                 } while (rdata_in_slab(oslab, reservelen,
503                                                        rdclass, type,
504                                                        &nrdata));
505                         }
506                 }
507         }
508
509         INSIST(tcurrent == tstart + tlength);
510
511         *tslabp = tstart;
512
513         return (ISC_R_SUCCESS);
514 }
515
516 isc_result_t
517 dns_rdataslab_subtract(unsigned char *mslab, unsigned char *sslab,
518                        unsigned int reservelen, isc_mem_t *mctx,
519                        dns_rdataclass_t rdclass, dns_rdatatype_t type,
520                        unsigned int flags, unsigned char **tslabp)
521 {
522         unsigned char *mcurrent, *sstart, *scurrent, *tstart, *tcurrent;
523         unsigned int mcount, scount, rcount ,count, tlength, tcount;
524         dns_rdata_t srdata = DNS_RDATA_INIT;
525         dns_rdata_t mrdata = DNS_RDATA_INIT;
526
527         REQUIRE(tslabp != NULL && *tslabp == NULL);
528         REQUIRE(mslab != NULL && sslab != NULL);
529
530         mcurrent = mslab + reservelen;
531         mcount = *mcurrent++ * 256;
532         mcount += *mcurrent++;
533         scurrent = sslab + reservelen;
534         scount = *scurrent++ * 256;
535         scount += *scurrent++;
536         sstart = scurrent;
537         INSIST(mcount > 0 && scount > 0);
538
539         /*
540          * Yes, this is inefficient!
541          */
542
543         /*
544          * Start figuring out the target length and count.
545          */
546         tlength = reservelen + 2;
547         tcount = 0;
548         rcount = 0;
549
550         /*
551          * Add in the length of rdata in the mslab that aren't in
552          * the sslab.
553          */
554         do {
555                 unsigned char *mrdatabegin = mcurrent;
556                 rdata_from_slab(&mcurrent, rdclass, type, &mrdata);
557                 scurrent = sstart;
558                 for (count = 0; count < scount; count++) {
559                         dns_rdata_reset(&srdata);
560                         rdata_from_slab(&scurrent, rdclass, type, &srdata);
561                         if (dns_rdata_compare(&mrdata, &srdata) == 0)
562                                 break;
563                 }
564                 if (count == scount) {
565                         /*
566                          * This rdata isn't in the sslab, and thus isn't
567                          * being subtracted.
568                          */
569                         tlength += mcurrent - mrdatabegin;
570                         tcount++;
571                 } else
572                         rcount++;
573                 mcount--;
574                 dns_rdata_reset(&mrdata);
575         } while (mcount > 0);
576
577         /*
578          * Check that all the records originally existed.  The numeric
579          * check only works as rdataslabs do not contain duplicates.
580          */
581         if (((flags & DNS_RDATASLAB_EXACT) != 0) && (rcount != scount))
582                 return (DNS_R_NOTEXACT);
583
584         /*
585          * Don't continue if the new rdataslab would be empty.
586          */
587         if (tcount == 0)
588                 return (DNS_R_NXRRSET);
589
590         /*
591          * If nothing is going to change, we can stop.
592          */
593         if (rcount == 0)
594                 return (DNS_R_UNCHANGED);
595
596         /*
597          * Copy the reserved area from the mslab.
598          */
599         tstart = isc_mem_get(mctx, tlength);
600         if (tstart == NULL)
601                 return (ISC_R_NOMEMORY);
602         memcpy(tstart, mslab, reservelen);
603         tcurrent = tstart + reservelen;
604
605         /*
606          * Write the new count.
607          */
608         *tcurrent++ = (tcount & 0xff00) >> 8;
609         *tcurrent++ = (tcount & 0x00ff);
610
611         /*
612          * Copy the parts of mslab not in sslab.
613          */
614         mcurrent = mslab + reservelen;
615         mcount = *mcurrent++ * 256;
616         mcount += *mcurrent++;
617         do {
618                 unsigned char *mrdatabegin = mcurrent;
619                 rdata_from_slab(&mcurrent, rdclass, type, &mrdata);
620                 scurrent = sstart;
621                 for (count = 0; count < scount; count++) {
622                         dns_rdata_reset(&srdata);
623                         rdata_from_slab(&scurrent, rdclass, type, &srdata);
624                         if (dns_rdata_compare(&mrdata, &srdata) == 0)
625                                 break;
626                 }
627                 if (count == scount) {
628                         /*
629                          * This rdata isn't in the sslab, and thus should be
630                          * copied to the tslab.
631                          */
632                         unsigned int length = mcurrent - mrdatabegin;
633                         memcpy(tcurrent, mrdatabegin, length);
634                         tcurrent += length;
635                 }
636                 dns_rdata_reset(&mrdata);
637                 mcount--;
638         } while (mcount > 0);
639
640         INSIST(tcurrent == tstart + tlength);
641
642         *tslabp = tstart;
643
644         return (ISC_R_SUCCESS);
645 }
646
647 isc_boolean_t
648 dns_rdataslab_equal(unsigned char *slab1, unsigned char *slab2,
649                     unsigned int reservelen)
650 {
651         unsigned char *current1, *current2;
652         unsigned int count1, count2;
653         unsigned int length1, length2;
654
655         current1 = slab1 + reservelen;
656         count1 = *current1++ * 256;
657         count1 += *current1++;
658
659         current2 = slab2 + reservelen;
660         count2 = *current2++ * 256;
661         count2 += *current2++;
662
663         if (count1 != count2)
664                 return (ISC_FALSE);
665
666         while (count1 > 0) {
667                 length1 = *current1++ * 256;
668                 length1 += *current1++;
669
670                 length2 = *current2++ * 256;
671                 length2 += *current2++;
672
673                 if (length1 != length2 ||
674                     memcmp(current1, current2, length1) != 0)
675                         return (ISC_FALSE);
676
677                 current1 += length1;
678                 current2 += length1;
679
680                 count1--;
681         }
682         return (ISC_TRUE);
683 }
684
685 isc_boolean_t
686 dns_rdataslab_equalx(unsigned char *slab1, unsigned char *slab2,
687                      unsigned int reservelen, dns_rdataclass_t rdclass,
688                      dns_rdatatype_t type)
689 {
690         unsigned char *current1, *current2;
691         unsigned int count1, count2;
692         dns_rdata_t rdata1 = DNS_RDATA_INIT;
693         dns_rdata_t rdata2 = DNS_RDATA_INIT;
694
695         current1 = slab1 + reservelen;
696         count1 = *current1++ * 256;
697         count1 += *current1++;
698
699         current2 = slab2 + reservelen;
700         count2 = *current2++ * 256;
701         count2 += *current2++;
702
703         if (count1 != count2)
704                 return (ISC_FALSE);
705
706         while (count1-- > 0) {
707                 rdata_from_slab(&current1, rdclass, type, &rdata1);
708                 rdata_from_slab(&current2, rdclass, type, &rdata2);
709                 if (dns_rdata_compare(&rdata1, &rdata2) != 0)
710                         return (ISC_FALSE);
711                 dns_rdata_reset(&rdata1);
712                 dns_rdata_reset(&rdata2);
713         }
714         return (ISC_TRUE);
715 }