libpsl.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright(c) 2014-2024 Tim Ruehsen
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. *
  22. * This file is part of libpsl.
  23. *
  24. * Header file for libpsl library routines
  25. *
  26. * Changelog
  27. * 20.03.2014 Tim Ruehsen created
  28. *
  29. */
  30. #ifndef LIBPSL_LIBPSL_H
  31. #define LIBPSL_LIBPSL_H
  32. #include <stdio.h>
  33. #include <time.h>
  34. #define PSL_VERSION "0.21.5"
  35. #define PSL_VERSION_MAJOR 0
  36. #define PSL_VERSION_MINOR 21
  37. #define PSL_VERSION_PATCH 5
  38. #define PSL_VERSION_NUMBER 0x001505
  39. /* support clang's __has_declspec_attribute attribute */
  40. #ifndef __has_declspec_attribute
  41. # define __has_declspec_attribute(x) 0
  42. #endif
  43. #ifndef PSL_API
  44. #if defined BUILDING_PSL && HAVE_VISIBILITY
  45. # define PSL_API __attribute__ ((__visibility__("default")))
  46. #elif defined BUILDING_PSL && (defined _MSC_VER || __has_declspec_attribute(dllexport)) && !defined PSL_STATIC
  47. # define PSL_API __declspec(dllexport)
  48. #elif (defined _MSC_VER || __has_declspec_attribute(dllimport)) && !defined PSL_STATIC
  49. # define PSL_API __declspec(dllimport)
  50. #else
  51. # define PSL_API
  52. #endif
  53. #endif
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /* types for psl_is_public_suffix2() */
  58. #define PSL_TYPE_ICANN (1<<0)
  59. #define PSL_TYPE_PRIVATE (1<<1)
  60. #define PSL_TYPE_NO_STAR_RULE (1<<2)
  61. #define PSL_TYPE_ANY (PSL_TYPE_ICANN | PSL_TYPE_PRIVATE)
  62. /**
  63. * psl_error_t:
  64. * @PSL_SUCCESS: Successful return.
  65. * @PSL_ERR_INVALID_ARG: Invalid argument.
  66. * @PSL_ERR_CONVERTER: Failed to open libicu utf-16 converter.
  67. * @PSL_ERR_TO_UTF16: Failed to convert to utf-16.
  68. * @PSL_ERR_TO_LOWER: Failed to convert utf-16 to lowercase.
  69. * @PSL_ERR_TO_UTF8: Failed to convert utf-16 to utf-8.
  70. * @PSL_ERR_NO_MEM: Failed to allocate memory.
  71. *
  72. * Return codes for PSL functions.
  73. * Negative return codes mean failure.
  74. * Positive values are reserved for non-error return codes.
  75. */
  76. typedef enum {
  77. PSL_SUCCESS = 0,
  78. PSL_ERR_INVALID_ARG = -1,
  79. PSL_ERR_CONVERTER = -2, /* failed to open libicu utf-16 converter */
  80. PSL_ERR_TO_UTF16 = -3, /* failed to convert to utf-16 */
  81. PSL_ERR_TO_LOWER = -4, /* failed to convert utf-16 to lowercase */
  82. PSL_ERR_TO_UTF8 = -5, /* failed to convert utf-16 to utf-8 */
  83. PSL_ERR_NO_MEM = -6 /* failed to allocate memory */
  84. } psl_error_t;
  85. typedef struct psl_ctx_st psl_ctx_t;
  86. /* frees PSL context */
  87. PSL_API
  88. void
  89. psl_free(psl_ctx_t *psl);
  90. /* frees memory allocated by libpsl routines */
  91. PSL_API
  92. void
  93. psl_free_string(char *str);
  94. /* loads PSL data from file */
  95. PSL_API
  96. psl_ctx_t *
  97. psl_load_file(const char *fname);
  98. /* loads PSL data from FILE pointer */
  99. PSL_API
  100. psl_ctx_t *
  101. psl_load_fp(FILE *fp);
  102. /* retrieves builtin PSL data */
  103. PSL_API
  104. const psl_ctx_t *
  105. psl_builtin(void);
  106. /* retrieves most recent PSL data */
  107. PSL_API
  108. psl_ctx_t *
  109. psl_latest(const char *fname);
  110. /* checks whether domain is a public suffix or not */
  111. PSL_API
  112. int
  113. psl_is_public_suffix(const psl_ctx_t *psl, const char *domain);
  114. /* checks whether domain is a public suffix regarding the type or not */
  115. PSL_API
  116. int
  117. psl_is_public_suffix2(const psl_ctx_t *psl, const char *domain, int type);
  118. /* checks whether cookie_domain is acceptable for domain or not */
  119. PSL_API
  120. int
  121. psl_is_cookie_domain_acceptable(const psl_ctx_t *psl, const char *hostname, const char *cookie_domain);
  122. /* returns the longest not registrable domain within 'domain' or NULL if none found */
  123. PSL_API
  124. const char *
  125. psl_unregistrable_domain(const psl_ctx_t *psl, const char *domain);
  126. /* returns the shortest possible registrable domain part or NULL if domain is not registrable at all */
  127. PSL_API
  128. const char *
  129. psl_registrable_domain(const psl_ctx_t *psl, const char *domain);
  130. /* convert a string into lowercase UTF-8 */
  131. PSL_API
  132. psl_error_t
  133. psl_str_to_utf8lower(const char *str, const char *encoding, const char *locale, char **lower);
  134. /* does not include exceptions */
  135. PSL_API
  136. int
  137. psl_suffix_count(const psl_ctx_t *psl);
  138. /* just counts exceptions */
  139. PSL_API
  140. int
  141. psl_suffix_exception_count(const psl_ctx_t *psl);
  142. /* just counts wildcards */
  143. PSL_API
  144. int
  145. psl_suffix_wildcard_count(const psl_ctx_t *psl);
  146. /* returns mtime of PSL source file */
  147. PSL_API
  148. time_t
  149. psl_builtin_file_time(void);
  150. /* returns SHA1 checksum (hex-encoded, lowercase) of PSL source file */
  151. PSL_API
  152. const char *
  153. psl_builtin_sha1sum(void);
  154. /* returns file name of PSL source file */
  155. PSL_API
  156. const char *
  157. psl_builtin_filename(void);
  158. /* returns name of distribution PSL data file */
  159. PSL_API
  160. const char *
  161. psl_dist_filename(void);
  162. /* returns library version string */
  163. PSL_API
  164. const char *
  165. psl_get_version(void);
  166. /* checks library version number */
  167. PSL_API
  168. int
  169. psl_check_version_number(int version);
  170. /* returns whether the built-in data is outdated or not */
  171. PSL_API
  172. int
  173. psl_builtin_outdated(void);
  174. #ifdef __cplusplus
  175. }
  176. #endif
  177. #endif /* LIBPSL_LIBPSL_H */