2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 18:19:42 +00:00
bind/lib/dns/rdataslab.c

269 lines
6.6 KiB
C
Raw Normal View History

/*
* Copyright (C) 1999 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
1999-04-01 04:00:39 +00:00
/* $Id: rdataslab.c,v 1.3 1999/04/01 04:00:39 halley Exp $ */
#include <config.h>
#include <isc/region.h>
#include <isc/buffer.h>
#include <isc/assertions.h>
#include <dns/types.h>
#include <dns/result.h>
#include <dns/rdata.h>
#include <dns/rdataclass.h>
#include <dns/rdatatype.h>
#include <dns/rdataset.h>
#include <dns/rdataslab.h>
dns_result_t
dns_rdataslab_fromrdataset(dns_rdataset_t *rdataset, isc_mem_t *mctx,
isc_region_t *region, unsigned int reservelen)
{
dns_rdata_t rdata;
unsigned char *rawbuf;
unsigned int buflen;
dns_result_t result;
unsigned int nitems;
result = dns_rdataset_first(rdataset);
REQUIRE(result == DNS_R_SUCCESS);
buflen = reservelen + 2;
nitems = 0;
/*
* Run through the rdataset list once, counting up the size
* of all the rdata members within it. We do not store the
* class, type, etc, just the rdata, so our overhead is 2 bytes
* for the number of records, and 2 for each rdata length, and
* then the rdata itself.
*/
do {
dns_rdataset_current(rdataset, &rdata);
buflen += (2 + rdata.length);
nitems++;
result = dns_rdataset_next(rdataset);
} while (result == DNS_R_SUCCESS);
if (result != DNS_R_NOMORE)
return (result);
/*
* Allocate the memory, set up a buffer, start copying in
* data.
*/
rawbuf = isc_mem_get(mctx, buflen);
if (rawbuf == NULL)
return (DNS_R_NOMEMORY);
region->base = rawbuf;
region->length = buflen;
rawbuf += reservelen;
*rawbuf++ = (nitems & 0xff00) >> 8;
*rawbuf++ = (nitems & 0x00ff);
result = dns_rdataset_first(rdataset);
REQUIRE(result == DNS_R_SUCCESS);
do {
dns_rdataset_current(rdataset, &rdata);
*rawbuf++ = (rdata.length & 0xff00) >> 8;
*rawbuf++ = (rdata.length & 0x00ff);
memcpy(rawbuf, rdata.data, rdata.length);
rawbuf += rdata.length;
result = dns_rdataset_next(rdataset);
} while (result == DNS_R_SUCCESS);
if (result != DNS_R_NOMORE) {
isc_mem_put(mctx, region->base, region->length);
region->base = NULL;
region->length = 0;
return (result);
}
return (DNS_R_SUCCESS);
}
unsigned int
dns_rdataslab_size(unsigned char *slab, unsigned int reservelen) {
unsigned int count, length;
unsigned char *current;
REQUIRE(slab != NULL);
current = slab + reservelen;
count = *current++ * 256;
count += *current++;
while (count > 0) {
count--;
length = *current++ * 256;
length += *current++;
current += length;
}
return ((unsigned int)(current - slab));
}
1999-04-01 04:00:39 +00:00
dns_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 char **tslabp)
{
unsigned char *ocurrent, *ostart, *ncurrent, *tstart, *tcurrent;
unsigned int ocount, ncount, count, olength, tlength, tcount, length;
isc_region_t oregion, nregion;
dns_rdata_t ordata, nrdata;
/*
* Merge 'oslab' and 'nslab'.
*/
/*
* XXX Need parameter to allow "delete rdatasets in nslab" merge,
* or perhaps another merge routine for this purpose.
*/
REQUIRE(tslabp != NULL && *tslabp == NULL);
REQUIRE(oslab != NULL && nslab != NULL);
ocurrent = oslab + reservelen;
ocount = *ocurrent++ * 256;
ocount += *ocurrent++;
ostart = ocurrent;
ncurrent = nslab + reservelen;
ncount = *ncurrent++ * 256;
ncount += *ncurrent++;
INSIST(ocount > 0 && ncount > 0);
/*
* Yes, this is inefficient!
*/
/*
* Figure out the length of the old slab's data.
*/
olength = 0;
for (count = 0; count < ocount; count++) {
length = *ocurrent++ * 256;
length += *ocurrent++;
olength += length + 2;
ocurrent += length;
}
/*
* Start figuring out the target length and count.
*/
tlength = reservelen + 2 + olength;
tcount = ocount;
/*
* Add in the length of rdata in the new slab that aren't in
* the old slab.
*/
do {
nregion.length = *ncurrent++ * 256;
nregion.length += *ncurrent++;
nregion.base = ncurrent;
dns_rdata_fromregion(&nrdata, rdclass, type, &nregion);
ocurrent = ostart;
for (count = 0; count < ocount; count++) {
oregion.length = *ocurrent++ * 256;
oregion.length += *ocurrent++;
oregion.base = ocurrent;
dns_rdata_fromregion(&ordata, rdclass, type, &oregion);
ocurrent += oregion.length;
if (dns_rdata_compare(&ordata, &nrdata) == 0)
break;
}
if (count == ocount) {
/*
* This rdata isn't in the old slab.
*/
tlength += nregion.length + 2;
tcount++;
}
ncurrent += nregion.length;
ncount--;
} while (ncount > 0);
/*
* Copy the reserved area from the new slab.
*/
tstart = isc_mem_get(mctx, tlength);
if (tstart == NULL)
return (DNS_R_NOMEMORY);
memcpy(tstart, nslab, reservelen);
tcurrent = tstart + reservelen;
/*
* Write the new count.
*/
*tcurrent++ = (tcount & 0xff00) >> 8;
*tcurrent++ = (tcount & 0x00ff);
/*
* Copy the old slab.
*/
memcpy(tcurrent, ostart, olength);
tcurrent += olength;
/*
* Copy the new parts of the new slab.
*/
ncurrent = nslab + reservelen;
ncount = *ncurrent++ * 256;
ncount += *ncurrent++;
do {
nregion.length = *ncurrent++ * 256;
nregion.length += *ncurrent++;
nregion.base = ncurrent;
dns_rdata_fromregion(&nrdata, rdclass, type, &nregion);
ocurrent = ostart;
for (count = 0; count < ocount; count++) {
oregion.length = *ocurrent++ * 256;
oregion.length += *ocurrent++;
oregion.base = ocurrent;
dns_rdata_fromregion(&ordata, rdclass, type, &oregion);
ocurrent += oregion.length;
if (dns_rdata_compare(&ordata, &nrdata) == 0)
break;
}
if (count == ocount) {
/*
* This rdata isn't in the old slab.
*/
*tcurrent++ = (nregion.length & 0xff00) >> 8;
*tcurrent++ = (nregion.length & 0x00ff);
memcpy(tcurrent, nregion.base, nregion.length);
tcurrent += nregion.length;
}
ncurrent += nregion.length;
ncount--;
} while (ncount > 0);
*tslabp = tstart;
return (DNS_R_SUCCESS);
}