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