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