tld.h 4.4 KB

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