⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rdataslab.c

📁 非常好的dns解析软件
💻 C
📖 第 1 页 / 共 2 页
字号:
	return (ISC_FALSE);}isc_result_tdns_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){	unsigned char *ocurrent, *ostart, *ncurrent, *tstart, *tcurrent;	unsigned int ocount, ncount, count, olength, tlength, tcount, length;	isc_region_t nregion;	dns_rdata_t ordata = DNS_RDATA_INIT;	dns_rdata_t nrdata = DNS_RDATA_INIT;	isc_boolean_t added_something = ISC_FALSE;	unsigned int oadded = 0;	unsigned int nadded = 0;	unsigned int nncount = 0;	unsigned int oncount;	unsigned int norder = 0;	unsigned int oorder = 0;	unsigned char *offsetbase;	unsigned int *offsettable;	/*	 * 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++;	ocurrent += (4 * ocount);	ostart = ocurrent;	ncurrent = nslab + reservelen;	ncount = *ncurrent++ * 256;	ncount += *ncurrent++;	ncurrent += (4 * ncount);	INSIST(ocount > 0 && ncount > 0);	oncount = ncount;	/*	 * 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 + 8;		ocurrent += length + 2;	}	/*	 * 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++;		ncurrent += 2;		nregion.base = ncurrent;		dns_rdata_init(&nrdata);		dns_rdata_fromregion(&nrdata, rdclass, type, &nregion);		if (!rdata_in_slab(oslab, reservelen, rdclass, type, &nrdata))		{			/*			 * This rdata isn't in the old slab.			 */			tlength += nregion.length + 8;			tcount++;			nncount++;			added_something = ISC_TRUE;		}		ncurrent += nregion.length;		ncount--;	} while (ncount > 0);	ncount = nncount;	if (((flags & DNS_RDATASLAB_EXACT) != 0) &&	    (tcount != ncount + ocount))		return (DNS_R_NOTEXACT);	if (!added_something && (flags & DNS_RDATASLAB_FORCE) == 0)		return (DNS_R_UNCHANGED);	/*	 * Ensure that singleton types are actually singletons.	 */	if (tcount > 1 && dns_rdatatype_issingleton(type)) {		/*		 * We have a singleton type, but there's more than one		 * RR in the rdataset.		 */		return (DNS_R_SINGLETON);	}	if (tcount > 0xffff)		return (ISC_R_NOSPACE);	/*	 * Copy the reserved area from the new slab.	 */	tstart = isc_mem_get(mctx, tlength);	if (tstart == NULL)		return (ISC_R_NOMEMORY);	memcpy(tstart, nslab, reservelen);	tcurrent = tstart + reservelen;	offsetbase = tcurrent;	/*	 * Write the new count.	 */	*tcurrent++ = (tcount & 0xff00) >> 8;	*tcurrent++ = (tcount & 0x00ff);	/*	 * Skip offset table.	 */	tcurrent += (tcount * 4);	offsettable = isc_mem_get(mctx,				  (ocount + oncount) * sizeof(unsigned int));	if (offsettable == NULL) {		isc_mem_put(mctx, tstart, tlength);		return (ISC_R_NOMEMORY);	}	memset(offsettable, 0, (ocount + oncount) * sizeof(unsigned int));	/*	 * Merge the two slabs.	 */	ocurrent = ostart;	INSIST(ocount != 0);	oorder = ocurrent[2] * 256 + ocurrent[3];	INSIST(oorder < ocount);	rdata_from_slab(&ocurrent, rdclass, type, &ordata);	ncurrent = nslab + reservelen + 2;	ncurrent += (4 * oncount);	if (ncount > 0) {		do {			dns_rdata_reset(&nrdata);			norder = ncurrent[2] * 256 + ncurrent[3];			INSIST(norder < oncount);       			rdata_from_slab(&ncurrent, rdclass, type, &nrdata);		} while (rdata_in_slab(oslab, reservelen, rdclass,				       type, &nrdata));	}	while (oadded < ocount || nadded < ncount) {		isc_boolean_t fromold;		if (oadded == ocount)			fromold = ISC_FALSE;		else if (nadded == ncount)			fromold = ISC_TRUE;		else			fromold = ISC_TF(compare_rdata(&ordata, &nrdata) < 0);		if (fromold) {			offsettable[oorder] = tcurrent - offsetbase;			length = ordata.length;			*tcurrent++ = (length & 0xff00) >> 8;			*tcurrent++ = (length & 0x00ff);			tcurrent += 2;	/* fill in later */			memcpy(tcurrent, ordata.data, length);			tcurrent += length;			oadded++;			if (oadded < ocount) {				dns_rdata_reset(&ordata);				oorder = ocurrent[2] * 256 + ocurrent[3];				INSIST(oorder < ocount);       				rdata_from_slab(&ocurrent, rdclass, type,						&ordata);			}		} else {			offsettable[ocount + norder] = tcurrent - offsetbase;			length = nrdata.length;			*tcurrent++ = (length & 0xff00) >> 8;			*tcurrent++ = (length & 0x00ff);			tcurrent += 2;	/* fill in later */			memcpy(tcurrent, nrdata.data, length);			tcurrent += length;			nadded++;			if (nadded < ncount) {				do {					dns_rdata_reset(&nrdata);					norder = ncurrent[2] * 256 + ncurrent[3];					INSIST(norder < oncount);       					rdata_from_slab(&ncurrent, rdclass,							type, &nrdata);				} while (rdata_in_slab(oslab, reservelen,						       rdclass, type,						       &nrdata));			}		}	}	fillin_offsets(offsetbase, offsettable, ocount + oncount);	isc_mem_put(mctx, offsettable,		    (ocount + oncount) * sizeof(unsigned int));	INSIST(tcurrent == tstart + tlength);	*tslabp = tstart;	return (ISC_R_SUCCESS);}isc_result_tdns_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){	unsigned char *mcurrent, *sstart, *scurrent, *tstart, *tcurrent;	unsigned int mcount, scount, rcount ,count, tlength, tcount, i;	dns_rdata_t srdata = DNS_RDATA_INIT;	dns_rdata_t mrdata = DNS_RDATA_INIT;	unsigned char *offsetbase;	unsigned int *offsettable;	unsigned int order;	REQUIRE(tslabp != NULL && *tslabp == NULL);	REQUIRE(mslab != NULL && sslab != NULL);	mcurrent = mslab + reservelen;	mcount = *mcurrent++ * 256;	mcount += *mcurrent++;	scurrent = sslab + reservelen;	scount = *scurrent++ * 256;	scount += *scurrent++;	INSIST(mcount > 0 && scount > 0);	/*	 * Yes, this is inefficient!	 */	/*	 * Start figuring out the target length and count.	 */	tlength = reservelen + 2;	tcount = 0;	rcount = 0;	mcurrent += 4 * mcount;	scurrent += 4 * scount;	sstart = scurrent;	/*	 * Add in the length of rdata in the mslab that aren't in	 * the sslab.	 */	for (i = 0; i < mcount; i++) {		unsigned char *mrdatabegin = mcurrent;		rdata_from_slab(&mcurrent, rdclass, type, &mrdata);		scurrent = sstart;		for (count = 0; count < scount; count++) {			dns_rdata_reset(&srdata);			rdata_from_slab(&scurrent, rdclass, type, &srdata);			if (dns_rdata_compare(&mrdata, &srdata) == 0)				break;		}		if (count == scount) {			/*			 * This rdata isn't in the sslab, and thus isn't			 * being subtracted.			 */			tlength += mcurrent - mrdatabegin;			tcount++;		} else			rcount++;		dns_rdata_reset(&mrdata);	}	tlength += (4 * tcount);	/*	 * Check that all the records originally existed.  The numeric 	 * check only works as rdataslabs do not contain duplicates.	 */	if (((flags & DNS_RDATASLAB_EXACT) != 0) && (rcount != scount))		return (DNS_R_NOTEXACT);	/*	 * Don't continue if the new rdataslab would be empty.	 */	if (tcount == 0)		return (DNS_R_NXRRSET);	/*	 * If nothing is going to change, we can stop.	 */	if (rcount == 0)		return (DNS_R_UNCHANGED);	/*	 * Copy the reserved area from the mslab.	 */	tstart = isc_mem_get(mctx, tlength);	if (tstart == NULL)		return (ISC_R_NOMEMORY);	memcpy(tstart, mslab, reservelen);	tcurrent = tstart + reservelen;	offsetbase = tcurrent;	offsettable = isc_mem_get(mctx, mcount * sizeof(unsigned int));	if (offsettable == NULL) {		isc_mem_put(mctx, tstart, tlength);		return (ISC_R_NOMEMORY);	}	memset(offsettable, 0, mcount * sizeof(unsigned int));	/*	 * Write the new count.	 */	*tcurrent++ = (tcount & 0xff00) >> 8;	*tcurrent++ = (tcount & 0x00ff);	tcurrent += (4 * tcount);	/*	 * Copy the parts of mslab not in sslab.	 */	mcurrent = mslab + reservelen;	mcount = *mcurrent++ * 256;	mcount += *mcurrent++;	mcurrent += (4 * mcount);	for (i = 0; i < mcount; i++) {		unsigned char *mrdatabegin = mcurrent;		order = mcurrent[2] * 256 + mcurrent[3];		INSIST(order < mcount);		rdata_from_slab(&mcurrent, rdclass, type, &mrdata);		scurrent = sstart;		for (count = 0; count < scount; count++) {			dns_rdata_reset(&srdata);			rdata_from_slab(&scurrent, rdclass, type, &srdata);			if (dns_rdata_compare(&mrdata, &srdata) == 0)				break;		}		if (count == scount) {			/*			 * This rdata isn't in the sslab, and thus should be			 * copied to the tslab.			 */			unsigned int length = mcurrent - mrdatabegin;			offsettable[order] = tcurrent - offsetbase;			memcpy(tcurrent, mrdatabegin, length);			tcurrent += length;		}		dns_rdata_reset(&mrdata);	}	fillin_offsets(offsetbase, offsettable, mcount);	isc_mem_put(mctx, offsettable, mcount * sizeof(unsigned int));	INSIST(tcurrent == tstart + tlength);	*tslabp = tstart;	return (ISC_R_SUCCESS);}isc_boolean_tdns_rdataslab_equal(unsigned char *slab1, unsigned char *slab2,		    unsigned int reservelen){	unsigned char *current1, *current2;	unsigned int count1, count2;	unsigned int length1, length2;	current1 = slab1 + reservelen;	count1 = *current1++ * 256;	count1 += *current1++;	current2 = slab2 + reservelen;	count2 = *current2++ * 256;	count2 += *current2++;	if (count1 != count2)		return (ISC_FALSE);	current1 += (4 * count1);	current2 += (4 * count2);	while (count1 > 0) {		length1 = *current1++ * 256;		length1 += *current1++;		length2 = *current2++ * 256;		length2 += *current2++;		current1 += 2;		current2 += 2;		if (length1 != length2 ||		    memcmp(current1, current2, length1) != 0)			return (ISC_FALSE);		current1 += length1;		current2 += length1;		count1--;	}	return (ISC_TRUE);}isc_boolean_tdns_rdataslab_equalx(unsigned char *slab1, unsigned char *slab2,		     unsigned int reservelen, dns_rdataclass_t rdclass,		     dns_rdatatype_t type){	unsigned char *current1, *current2;	unsigned int count1, count2;	dns_rdata_t rdata1 = DNS_RDATA_INIT;	dns_rdata_t rdata2 = DNS_RDATA_INIT;	current1 = slab1 + reservelen;	count1 = *current1++ * 256;	count1 += *current1++;	current2 = slab2 + reservelen;	count2 = *current2++ * 256;	count2 += *current2++;	if (count1 != count2)		return (ISC_FALSE);	current1 += (4 * count1);	current2 += (4 * count2);	while (count1-- > 0) {		rdata_from_slab(&current1, rdclass, type, &rdata1);		rdata_from_slab(&current2, rdclass, type, &rdata2);		if (dns_rdata_compare(&rdata1, &rdata2) != 0)			return (ISC_FALSE);		dns_rdata_reset(&rdata1);		dns_rdata_reset(&rdata2);	}	return (ISC_TRUE);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -