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