2N/A/*
2N/A * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC")
2N/A * Copyright (C) 1996, 1999, 2001 Internet Software Consortium.
2N/A *
2N/A * Permission to use, copy, modify, and/or distribute this software for any
2N/A * purpose with or without fee is hereby granted, provided that the above
2N/A * copyright notice and this permission notice appear in all copies.
2N/A *
2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
2N/A * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
2N/A * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
2N/A * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
2N/A * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
2N/A * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
2N/A * PERFORMANCE OF THIS SOFTWARE.
2N/A */
2N/A
2N/A#if defined(LIBC_SCCS) && !defined(lint)
2N/Astatic const char rcsid[] = "$Id: bitncmp.c,v 1.5 2008/11/14 02:36:51 marka Exp $";
2N/A#endif
2N/A
2N/A#include "port_before.h"
2N/A
2N/A#include <sys/types.h>
2N/A
2N/A#include <string.h>
2N/A
2N/A#include "port_after.h"
2N/A
2N/A#include <isc/misc.h>
2N/A
2N/A/*%
2N/A * int
2N/A * bitncmp(l, r, n)
2N/A * compare bit masks l and r, for n bits.
2N/A * return:
2N/A * -1, 1, or 0 in the libc tradition.
2N/A * note:
2N/A * network byte order assumed. this means 192.5.5.240/28 has
2N/A * 0x11110000 in its fourth octet.
2N/A * author:
2N/A * Paul Vixie (ISC), June 1996
2N/A */
2N/Aint
2N/Abitncmp(const void *l, const void *r, int n) {
2N/A u_int lb, rb;
2N/A int x, b;
2N/A
2N/A b = n / 8;
2N/A x = memcmp(l, r, b);
2N/A if (x || (n % 8) == 0)
2N/A return (x);
2N/A
2N/A lb = ((const u_char *)l)[b];
2N/A rb = ((const u_char *)r)[b];
2N/A for (b = n % 8; b > 0; b--) {
2N/A if ((lb & 0x80) != (rb & 0x80)) {
2N/A if (lb & 0x80)
2N/A return (1);
2N/A return (-1);
2N/A }
2N/A lb <<= 1;
2N/A rb <<= 1;
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/A/*! \file */