xref: /netbsd-src/external/bsd/libbind/dist/isc/bitncmp.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1*5bbd2a12Schristos /*	$NetBSD: bitncmp.c,v 1.1.1.2 2012/09/09 16:08:00 christos Exp $	*/
2b5677b36Schristos 
3b5677b36Schristos /*
4b5677b36Schristos  * Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
5b5677b36Schristos  * Copyright (C) 1996, 1999, 2001  Internet Software Consortium.
6b5677b36Schristos  *
7b5677b36Schristos  * Permission to use, copy, modify, and/or distribute this software for any
8b5677b36Schristos  * purpose with or without fee is hereby granted, provided that the above
9b5677b36Schristos  * copyright notice and this permission notice appear in all copies.
10b5677b36Schristos  *
11b5677b36Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12b5677b36Schristos  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13b5677b36Schristos  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14b5677b36Schristos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15b5677b36Schristos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16b5677b36Schristos  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17b5677b36Schristos  * PERFORMANCE OF THIS SOFTWARE.
18b5677b36Schristos  */
19b5677b36Schristos 
20b5677b36Schristos #if defined(LIBC_SCCS) && !defined(lint)
21b5677b36Schristos static const char rcsid[] = "Id: bitncmp.c,v 1.5 2008/11/14 02:36:51 marka Exp ";
22b5677b36Schristos #endif
23b5677b36Schristos 
24b5677b36Schristos #include "port_before.h"
25b5677b36Schristos 
26b5677b36Schristos #include <sys/types.h>
27b5677b36Schristos 
28b5677b36Schristos #include <string.h>
29b5677b36Schristos 
30b5677b36Schristos #include "port_after.h"
31b5677b36Schristos 
32b5677b36Schristos #include <isc/misc.h>
33b5677b36Schristos 
34b5677b36Schristos /*%
35b5677b36Schristos  * int
36b5677b36Schristos  * bitncmp(l, r, n)
37b5677b36Schristos  *	compare bit masks l and r, for n bits.
38b5677b36Schristos  * return:
39b5677b36Schristos  *	-1, 1, or 0 in the libc tradition.
40b5677b36Schristos  * note:
41b5677b36Schristos  *	network byte order assumed.  this means 192.5.5.240/28 has
42b5677b36Schristos  *	0x11110000 in its fourth octet.
43b5677b36Schristos  * author:
44b5677b36Schristos  *	Paul Vixie (ISC), June 1996
45b5677b36Schristos  */
46b5677b36Schristos int
bitncmp(const void * l,const void * r,int n)47b5677b36Schristos bitncmp(const void *l, const void *r, int n) {
48b5677b36Schristos 	u_int lb, rb;
49b5677b36Schristos 	int x, b;
50b5677b36Schristos 
51b5677b36Schristos 	b = n / 8;
52b5677b36Schristos 	x = memcmp(l, r, b);
53b5677b36Schristos 	if (x || (n % 8) == 0)
54b5677b36Schristos 		return (x);
55b5677b36Schristos 
56b5677b36Schristos 	lb = ((const u_char *)l)[b];
57b5677b36Schristos 	rb = ((const u_char *)r)[b];
58b5677b36Schristos 	for (b = n % 8; b > 0; b--) {
59b5677b36Schristos 		if ((lb & 0x80) != (rb & 0x80)) {
60b5677b36Schristos 			if (lb & 0x80)
61b5677b36Schristos 				return (1);
62b5677b36Schristos 			return (-1);
63b5677b36Schristos 		}
64b5677b36Schristos 		lb <<= 1;
65b5677b36Schristos 		rb <<= 1;
66b5677b36Schristos 	}
67b5677b36Schristos 	return (0);
68b5677b36Schristos }
69b5677b36Schristos 
70b5677b36Schristos /*! \file */
71