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