Removed usage of u_int and u_char
[platform/upstream/c-ares.git] / bitncmp.c
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1996,1999 by Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #ifndef HAVE_BITNCMP
19
20 #include <sys/types.h>
21 #include <string.h>
22 #include <stdlib.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