2
0

bitncmp.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* $Id$ */
  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. #ifndef HAVE_BITNCMP
  19. #include "setup.h"
  20. #include "bitncmp.h"
  21. /*
  22. * int
  23. * bitncmp(l, r, n)
  24. * compare bit masks l and r, for n bits.
  25. * return:
  26. * -1, 1, or 0 in the libc tradition.
  27. * note:
  28. * network byte order assumed. this means 192.5.5.240/28 has
  29. * 0x11110000 in its fourth octet.
  30. * author:
  31. * Paul Vixie (ISC), June 1996
  32. */
  33. int
  34. ares_bitncmp(const void *l, const void *r, int n) {
  35. unsigned int lb, rb;
  36. int x, b;
  37. b = n / 8;
  38. x = memcmp(l, r, b);
  39. if (x)
  40. return (x);
  41. lb = ((const unsigned char *)l)[b];
  42. rb = ((const unsigned char *)r)[b];
  43. for (b = n % 8; b > 0; b--) {
  44. if ((lb & 0x80) != (rb & 0x80)) {
  45. if (lb & 0x80)
  46. return (1);
  47. return (-1);
  48. }
  49. lb <<= 1;
  50. rb <<= 1;
  51. }
  52. return (0);
  53. }
  54. #endif