10Sstevel@tonic-gate /* 2*11038SRao.Shoaib@Sun.COM * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC") 3*11038SRao.Shoaib@Sun.COM * Copyright (C) 1996, 1999, 2001 Internet Software Consortium. 40Sstevel@tonic-gate * 5*11038SRao.Shoaib@Sun.COM * Permission to use, copy, modify, and/or distribute this software for any 60Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 70Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 80Sstevel@tonic-gate * 9*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10*11038SRao.Shoaib@Sun.COM * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11*11038SRao.Shoaib@Sun.COM * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12*11038SRao.Shoaib@Sun.COM * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13*11038SRao.Shoaib@Sun.COM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14*11038SRao.Shoaib@Sun.COM * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15*11038SRao.Shoaib@Sun.COM * PERFORMANCE OF THIS SOFTWARE. 160Sstevel@tonic-gate */ 170Sstevel@tonic-gate 180Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: bitncmp.c,v 1.5 2008/11/14 02:36:51 marka Exp $"; 200Sstevel@tonic-gate #endif 210Sstevel@tonic-gate 220Sstevel@tonic-gate #include "port_before.h" 230Sstevel@tonic-gate 240Sstevel@tonic-gate #include <sys/types.h> 250Sstevel@tonic-gate 260Sstevel@tonic-gate #include <string.h> 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include "port_after.h" 290Sstevel@tonic-gate 300Sstevel@tonic-gate #include <isc/misc.h> 310Sstevel@tonic-gate 32*11038SRao.Shoaib@Sun.COM /*% 330Sstevel@tonic-gate * int 340Sstevel@tonic-gate * bitncmp(l, r, n) 350Sstevel@tonic-gate * compare bit masks l and r, for n bits. 360Sstevel@tonic-gate * return: 370Sstevel@tonic-gate * -1, 1, or 0 in the libc tradition. 380Sstevel@tonic-gate * note: 390Sstevel@tonic-gate * network byte order assumed. this means 192.5.5.240/28 has 400Sstevel@tonic-gate * 0x11110000 in its fourth octet. 410Sstevel@tonic-gate * author: 420Sstevel@tonic-gate * Paul Vixie (ISC), June 1996 430Sstevel@tonic-gate */ 440Sstevel@tonic-gate int 450Sstevel@tonic-gate bitncmp(const void *l, const void *r, int n) { 460Sstevel@tonic-gate u_int lb, rb; 470Sstevel@tonic-gate int x, b; 480Sstevel@tonic-gate 490Sstevel@tonic-gate b = n / 8; 500Sstevel@tonic-gate x = memcmp(l, r, b); 51*11038SRao.Shoaib@Sun.COM if (x || (n % 8) == 0) 520Sstevel@tonic-gate return (x); 530Sstevel@tonic-gate 540Sstevel@tonic-gate lb = ((const u_char *)l)[b]; 550Sstevel@tonic-gate rb = ((const u_char *)r)[b]; 560Sstevel@tonic-gate for (b = n % 8; b > 0; b--) { 570Sstevel@tonic-gate if ((lb & 0x80) != (rb & 0x80)) { 580Sstevel@tonic-gate if (lb & 0x80) 590Sstevel@tonic-gate return (1); 600Sstevel@tonic-gate return (-1); 610Sstevel@tonic-gate } 620Sstevel@tonic-gate lb <<= 1; 630Sstevel@tonic-gate rb <<= 1; 640Sstevel@tonic-gate } 650Sstevel@tonic-gate return (0); 660Sstevel@tonic-gate } 67*11038SRao.Shoaib@Sun.COM 68*11038SRao.Shoaib@Sun.COM /*! \file */ 69