xref: /illumos-gate/usr/src/lib/libresolv2/common/isc/bitncmp.c (revision 458f44a49dc56cd17a39815122214e7a1b4793e3)
17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
3*9525b14bSRao Shoaib  * Copyright (C) 1996, 1999, 2001  Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
5*9525b14bSRao Shoaib  * Permission to use, copy, modify, and/or distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*9525b14bSRao Shoaib  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*9525b14bSRao Shoaib  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*9525b14bSRao Shoaib  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*9525b14bSRao Shoaib  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*9525b14bSRao Shoaib  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*9525b14bSRao Shoaib  * PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #include "port_before.h"
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include <sys/types.h>
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include <string.h>
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #include "port_after.h"
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <isc/misc.h>
277c478bd9Sstevel@tonic-gate 
28*9525b14bSRao Shoaib /*%
297c478bd9Sstevel@tonic-gate  * int
307c478bd9Sstevel@tonic-gate  * bitncmp(l, r, n)
317c478bd9Sstevel@tonic-gate  *	compare bit masks l and r, for n bits.
327c478bd9Sstevel@tonic-gate  * return:
337c478bd9Sstevel@tonic-gate  *	-1, 1, or 0 in the libc tradition.
347c478bd9Sstevel@tonic-gate  * note:
357c478bd9Sstevel@tonic-gate  *	network byte order assumed.  this means 192.5.5.240/28 has
367c478bd9Sstevel@tonic-gate  *	0x11110000 in its fourth octet.
377c478bd9Sstevel@tonic-gate  * author:
387c478bd9Sstevel@tonic-gate  *	Paul Vixie (ISC), June 1996
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate int
bitncmp(const void * l,const void * r,int n)417c478bd9Sstevel@tonic-gate bitncmp(const void *l, const void *r, int n) {
427c478bd9Sstevel@tonic-gate 	u_int lb, rb;
437c478bd9Sstevel@tonic-gate 	int x, b;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	b = n / 8;
467c478bd9Sstevel@tonic-gate 	x = memcmp(l, r, b);
47*9525b14bSRao Shoaib 	if (x || (n % 8) == 0)
487c478bd9Sstevel@tonic-gate 		return (x);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	lb = ((const u_char *)l)[b];
517c478bd9Sstevel@tonic-gate 	rb = ((const u_char *)r)[b];
527c478bd9Sstevel@tonic-gate 	for (b = n % 8; b > 0; b--) {
537c478bd9Sstevel@tonic-gate 		if ((lb & 0x80) != (rb & 0x80)) {
547c478bd9Sstevel@tonic-gate 			if (lb & 0x80)
557c478bd9Sstevel@tonic-gate 				return (1);
567c478bd9Sstevel@tonic-gate 			return (-1);
577c478bd9Sstevel@tonic-gate 		}
587c478bd9Sstevel@tonic-gate 		lb <<= 1;
597c478bd9Sstevel@tonic-gate 		rb <<= 1;
607c478bd9Sstevel@tonic-gate 	}
617c478bd9Sstevel@tonic-gate 	return (0);
627c478bd9Sstevel@tonic-gate }
63*9525b14bSRao Shoaib 
64*9525b14bSRao Shoaib /*! \file */
65