bitncmp.c 1.6 KB

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