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 #if defined(LIBC_SCCS) && !defined(lint) 19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: bitncmp.c,v 1.5 2008/11/14 02:36:51 marka Exp $"; 207c478bd9Sstevel@tonic-gate #endif 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate #include "port_before.h" 237c478bd9Sstevel@tonic-gate 247c478bd9Sstevel@tonic-gate #include <sys/types.h> 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <string.h> 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include "port_after.h" 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #include <isc/misc.h> 317c478bd9Sstevel@tonic-gate 32*9525b14bSRao Shoaib /*% 337c478bd9Sstevel@tonic-gate * int 347c478bd9Sstevel@tonic-gate * bitncmp(l, r, n) 357c478bd9Sstevel@tonic-gate * compare bit masks l and r, for n bits. 367c478bd9Sstevel@tonic-gate * return: 377c478bd9Sstevel@tonic-gate * -1, 1, or 0 in the libc tradition. 387c478bd9Sstevel@tonic-gate * note: 397c478bd9Sstevel@tonic-gate * network byte order assumed. this means 192.5.5.240/28 has 407c478bd9Sstevel@tonic-gate * 0x11110000 in its fourth octet. 417c478bd9Sstevel@tonic-gate * author: 427c478bd9Sstevel@tonic-gate * Paul Vixie (ISC), June 1996 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate int 457c478bd9Sstevel@tonic-gate bitncmp(const void *l, const void *r, int n) { 467c478bd9Sstevel@tonic-gate u_int lb, rb; 477c478bd9Sstevel@tonic-gate int x, b; 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate b = n / 8; 507c478bd9Sstevel@tonic-gate x = memcmp(l, r, b); 51*9525b14bSRao Shoaib if (x || (n % 8) == 0) 527c478bd9Sstevel@tonic-gate return (x); 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate lb = ((const u_char *)l)[b]; 557c478bd9Sstevel@tonic-gate rb = ((const u_char *)r)[b]; 567c478bd9Sstevel@tonic-gate for (b = n % 8; b > 0; b--) { 577c478bd9Sstevel@tonic-gate if ((lb & 0x80) != (rb & 0x80)) { 587c478bd9Sstevel@tonic-gate if (lb & 0x80) 597c478bd9Sstevel@tonic-gate return (1); 607c478bd9Sstevel@tonic-gate return (-1); 617c478bd9Sstevel@tonic-gate } 627c478bd9Sstevel@tonic-gate lb <<= 1; 637c478bd9Sstevel@tonic-gate rb <<= 1; 647c478bd9Sstevel@tonic-gate } 657c478bd9Sstevel@tonic-gate return (0); 667c478bd9Sstevel@tonic-gate } 67*9525b14bSRao Shoaib 68*9525b14bSRao Shoaib /*! \file */ 69