tld.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* tld.h --- Declarations for TLD restriction checking.
  2. Copyright (C) 2004-2024 Simon Josefsson.
  3. Copyright (C) 2003-2024 Free Software Foundation, Inc.
  4. Author: Thomas Jacob, Internet24.de
  5. This file is part of GNU Libidn.
  6. GNU Libidn is free software: you can redistribute it and/or
  7. modify it under the terms of either:
  8. * the GNU Lesser General Public License as published by the Free
  9. Software Foundation; either version 3 of the License, or (at
  10. your option) any later version.
  11. or
  12. * the GNU General Public License as published by the Free
  13. Software Foundation; either version 2 of the License, or (at
  14. your option) any later version.
  15. or both in parallel, as here.
  16. GNU Libidn is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. General Public License for more details.
  20. You should have received copies of the GNU General Public License and
  21. the GNU Lesser General Public License along with this program. If
  22. not, see <https://www.gnu.org/licenses/>. */
  23. #ifndef TLD_H
  24. # define TLD_H
  25. /**
  26. * SECTION:tld
  27. * @title: tld.h
  28. * @short_description: TLD-related functions
  29. *
  30. * TLD-related functions.
  31. */
  32. # ifndef IDNAPI
  33. # define IDNAPI
  34. # endif
  35. # ifdef __cplusplus
  36. extern "C"
  37. {
  38. # endif
  39. /* Get size_t. */
  40. # include <stdlib.h>
  41. /* Get uint32_t. */
  42. # include <idn-int.h>
  43. /* *INDENT-OFF* */
  44. /* Why INDENT-OFF? See stringprep.h */
  45. /**
  46. * Tld_table_element:
  47. * @start: Start of range.
  48. * @end: End of range, end == start if single.
  49. *
  50. * Interval of valid code points in the TLD.
  51. */
  52. struct Tld_table_element
  53. {
  54. uint32_t start;
  55. uint32_t end;
  56. };
  57. typedef struct Tld_table_element Tld_table_element;
  58. /**
  59. * Tld_table:
  60. * @name: TLD name, e.g., "no".
  61. * @version: Version string from TLD file.
  62. * @nvalid: Number of entries in data.
  63. * @valid: Sorted array (of size @nvalid) of valid code points.
  64. *
  65. * List valid code points in a TLD.
  66. */
  67. struct Tld_table
  68. {
  69. const char *name;
  70. const char *version;
  71. size_t nvalid;
  72. const Tld_table_element *valid;
  73. };
  74. typedef struct Tld_table Tld_table;
  75. /* *INDENT-ON* */
  76. /* Error codes. */
  77. typedef enum
  78. {
  79. TLD_SUCCESS = 0,
  80. TLD_INVALID = 1, /* Invalid character found. */
  81. TLD_NODATA = 2, /* Char, domain or inlen = 0. */
  82. TLD_MALLOC_ERROR = 3,
  83. TLD_ICONV_ERROR = 4,
  84. TLD_NO_TLD = 5,
  85. /* Workaround typo in earlier versions. */
  86. TLD_NOTLD = TLD_NO_TLD
  87. } Tld_rc;
  88. extern IDNAPI const char *tld_strerror (Tld_rc rc);
  89. /* Extract TLD, as ASCII string, of UCS4 domain name into "out". */
  90. extern IDNAPI int tld_get_4 (const uint32_t * in, size_t inlen, char **out);
  91. extern IDNAPI int tld_get_4z (const uint32_t * in, char **out);
  92. extern IDNAPI int tld_get_z (const char *in, char **out);
  93. /* Return structure corresponding to the named TLD from specified
  94. * list of TLD tables, or return NULL if no matching TLD can be
  95. * found. */
  96. extern IDNAPI const Tld_table *tld_get_table (const char *tld,
  97. const Tld_table ** tables);
  98. /* Return structure corresponding to the named TLD, first looking
  99. * thru overrides then thru built-in list, or return NULL if no
  100. * matching TLD can be found. */
  101. extern IDNAPI const Tld_table *tld_default_table (const char *tld,
  102. const Tld_table **
  103. overrides);
  104. /* Check NAMEPREPPED domain name for valid characters as defined by
  105. * the relevant registering body (plus [a-z0-9.-]). If error is
  106. * TLD_INVALID, set errpos to position of offending character. */
  107. extern IDNAPI int tld_check_4t (const uint32_t * in, size_t inlen,
  108. size_t *errpos, const Tld_table * tld);
  109. extern IDNAPI int tld_check_4tz (const uint32_t * in, size_t *errpos,
  110. const Tld_table * tld);
  111. /* Utility interfaces that uses tld_get_4* to find TLD of string,
  112. then tld_default_table (with overrides) to find proper TLD table
  113. for the string, and then hands over to tld_check_4t*. */
  114. extern IDNAPI int tld_check_4 (const uint32_t * in, size_t inlen,
  115. size_t *errpos,
  116. const Tld_table ** overrides);
  117. extern IDNAPI int tld_check_4z (const uint32_t * in, size_t *errpos,
  118. const Tld_table ** overrides);
  119. extern IDNAPI int tld_check_8z (const char *in, size_t *errpos,
  120. const Tld_table ** overrides);
  121. extern IDNAPI int tld_check_lz (const char *in, size_t *errpos,
  122. const Tld_table ** overrides);
  123. # ifdef __cplusplus
  124. }
  125. # endif
  126. #endif /* TLD_H */