ns_samebinaryname.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Compare two binary domain names for quality.
  2. Copyright (C) 2022-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <arpa/nameser.h>
  16. #include <stdbool.h>
  17. /* Convert ASCII letters to upper case. */
  18. static inline int
  19. ascii_toupper (unsigned char ch)
  20. {
  21. if (ch >= 'a' && ch <= 'z')
  22. return ch - 'a' + 'A';
  23. else
  24. return ch;
  25. }
  26. bool
  27. __ns_samebinaryname (const unsigned char *a, const unsigned char *b)
  28. {
  29. while (*a != 0 && *b != 0)
  30. {
  31. if (*a != *b)
  32. /* Different label length. */
  33. return false;
  34. int labellen = *a;
  35. ++a;
  36. ++b;
  37. for (int i = 0; i < labellen; ++i)
  38. {
  39. if (*a != *b && ascii_toupper (*a) != ascii_toupper (*b))
  40. /* Different character in label. */
  41. return false;
  42. ++a;
  43. ++b;
  44. }
  45. }
  46. /* Match if both names are at the root label. */
  47. return *a == 0 && *b == 0;
  48. }