xref: /netbsd-src/external/bsd/unbound/dist/edns-subnet/edns-subnet.c (revision 0cd9f4ecf44538bbdd5619b5b2081449960ab3e6)
1*0cd9f4ecSchristos /*
2*0cd9f4ecSchristos  * edns-subnet/edns-subnet.c - Subnet option related constants
3*0cd9f4ecSchristos  *
4*0cd9f4ecSchristos  * Copyright (c) 2013, NLnet Labs. All rights reserved.
5*0cd9f4ecSchristos  *
6*0cd9f4ecSchristos  * This software is open source.
7*0cd9f4ecSchristos  *
8*0cd9f4ecSchristos  * Redistribution and use in source and binary forms, with or without
9*0cd9f4ecSchristos  * modification, are permitted provided that the following conditions
10*0cd9f4ecSchristos  * are met:
11*0cd9f4ecSchristos  *
12*0cd9f4ecSchristos  * Redistributions of source code must retain the above copyright notice,
13*0cd9f4ecSchristos  * this list of conditions and the following disclaimer.
14*0cd9f4ecSchristos  *
15*0cd9f4ecSchristos  * Redistributions in binary form must reproduce the above copyright notice,
16*0cd9f4ecSchristos  * this list of conditions and the following disclaimer in the documentation
17*0cd9f4ecSchristos  * and/or other materials provided with the distribution.
18*0cd9f4ecSchristos  *
19*0cd9f4ecSchristos  * Neither the name of the NLNET LABS nor the names of its contributors may
20*0cd9f4ecSchristos  * be used to endorse or promote products derived from this software without
21*0cd9f4ecSchristos  * specific prior written permission.
22*0cd9f4ecSchristos  *
23*0cd9f4ecSchristos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24*0cd9f4ecSchristos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25*0cd9f4ecSchristos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26*0cd9f4ecSchristos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27*0cd9f4ecSchristos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28*0cd9f4ecSchristos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29*0cd9f4ecSchristos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30*0cd9f4ecSchristos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31*0cd9f4ecSchristos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32*0cd9f4ecSchristos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*0cd9f4ecSchristos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*0cd9f4ecSchristos  */
35*0cd9f4ecSchristos /**
36*0cd9f4ecSchristos  * \file
37*0cd9f4ecSchristos  * Subnet option related constants.
38*0cd9f4ecSchristos  */
39*0cd9f4ecSchristos 
40*0cd9f4ecSchristos #include "config.h"
41*0cd9f4ecSchristos 
42*0cd9f4ecSchristos #ifdef CLIENT_SUBNET /* keeps splint happy */
43*0cd9f4ecSchristos #include "edns-subnet/edns-subnet.h"
44*0cd9f4ecSchristos #include <string.h>
45*0cd9f4ecSchristos 
46*0cd9f4ecSchristos int
copy_clear(uint8_t * dst,size_t dstlen,uint8_t * src,size_t srclen,size_t n)47*0cd9f4ecSchristos copy_clear(uint8_t* dst, size_t dstlen, uint8_t* src, size_t srclen, size_t n)
48*0cd9f4ecSchristos {
49*0cd9f4ecSchristos 	size_t intpart = n / 8;  /* bytes */
50*0cd9f4ecSchristos 	size_t fracpart = n % 8; /* bits */
51*0cd9f4ecSchristos 	size_t written = intpart;
52*0cd9f4ecSchristos 	if (intpart > dstlen || intpart > srclen)
53*0cd9f4ecSchristos 		return 1;
54*0cd9f4ecSchristos 	if (fracpart && (intpart+1 > dstlen || intpart+1 > srclen))
55*0cd9f4ecSchristos 		return 1;
56*0cd9f4ecSchristos 	memcpy(dst, src, intpart);
57*0cd9f4ecSchristos 	if (fracpart) {
58*0cd9f4ecSchristos 		dst[intpart] = src[intpart] & ~(0xFF >> fracpart);
59*0cd9f4ecSchristos 		written++;
60*0cd9f4ecSchristos 	}
61*0cd9f4ecSchristos 	memset(dst + written, 0, dstlen - written);
62*0cd9f4ecSchristos 	return 0;
63*0cd9f4ecSchristos }
64*0cd9f4ecSchristos 
65*0cd9f4ecSchristos #endif /* CLIENT_SUBNET */
66