lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") |
| 4 | * Copyright (c) 1996,1999 by Internet Software Consortium. |
| 5 | * |
| 6 | * Permission to use, copy, modify, and distribute this software for any |
| 7 | * purpose with or without fee is hereby granted, provided that the above |
| 8 | * copyright notice and this permission notice appear in all copies. |
| 9 | * |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES |
| 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR |
| 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 16 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | */ |
| 18 | |
| 19 | #ifndef HAVE_BITNCMP |
| 20 | |
| 21 | #include "ares_setup.h" |
| 22 | #include "bitncmp.h" |
| 23 | |
| 24 | /* |
| 25 | * int |
| 26 | * bitncmp(l, r, n) |
| 27 | * compare bit masks l and r, for n bits. |
| 28 | * return: |
| 29 | * <0, >0, or 0 in the libc tradition. |
| 30 | * note: |
| 31 | * network byte order assumed. this means 192.5.5.240/28 has |
| 32 | * 0x11110000 in its fourth octet. |
| 33 | * author: |
| 34 | * Paul Vixie (ISC), June 1996 |
| 35 | */ |
| 36 | int ares__bitncmp(const void *l, const void *r, int n) |
| 37 | { |
| 38 | unsigned int lb, rb; |
| 39 | int x, b; |
| 40 | |
| 41 | b = n / 8; |
| 42 | x = memcmp(l, r, b); |
| 43 | if (x || (n % 8) == 0) |
| 44 | return (x); |
| 45 | |
| 46 | lb = ((const unsigned char *)l)[b]; |
| 47 | rb = ((const unsigned char *)r)[b]; |
| 48 | for (b = n % 8; b > 0; b--) { |
| 49 | if ((lb & 0x80) != (rb & 0x80)) { |
| 50 | if (lb & 0x80) |
| 51 | return (1); |
| 52 | return (-1); |
| 53 | } |
| 54 | lb <<= 1; |
| 55 | rb <<= 1; |
| 56 | } |
| 57 | return (0); |
| 58 | } |
| 59 | #endif |