stringprep.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* stringprep.h --- Header file for stringprep functions.
  2. Copyright (C) 2002-2022 Simon Josefsson
  3. This file is part of GNU Libidn.
  4. GNU Libidn is free software: you can redistribute it and/or
  5. modify it under the terms of either:
  6. * the GNU Lesser General Public License as published by the Free
  7. Software Foundation; either version 3 of the License, or (at
  8. your option) any later version.
  9. or
  10. * the GNU General Public License as published by the Free
  11. Software Foundation; either version 2 of the License, or (at
  12. your option) any later version.
  13. or both in parallel, as here.
  14. GNU Libidn is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. General Public License for more details.
  18. You should have received copies of the GNU General Public License and
  19. the GNU Lesser General Public License along with this program. If
  20. not, see <https://www.gnu.org/licenses/>. */
  21. #ifndef STRINGPREP_H
  22. # define STRINGPREP_H
  23. /**
  24. * SECTION:stringprep
  25. * @title: stringprep.h
  26. * @short_description: Stringprep-related functions
  27. *
  28. * Stringprep-related functions.
  29. */
  30. # ifndef IDNAPI
  31. # define IDNAPI
  32. # endif
  33. # include <stddef.h> /* size_t */
  34. # include <sys/types.h> /* ssize_t */
  35. # include <idn-int.h> /* uint32_t */
  36. # ifdef __cplusplus
  37. extern "C"
  38. {
  39. # endif
  40. # define STRINGPREP_VERSION "1.41"
  41. /* Error codes. */
  42. typedef enum
  43. {
  44. STRINGPREP_OK = 0,
  45. /* Stringprep errors. */
  46. STRINGPREP_CONTAINS_UNASSIGNED = 1,
  47. STRINGPREP_CONTAINS_PROHIBITED = 2,
  48. STRINGPREP_BIDI_BOTH_L_AND_RAL = 3,
  49. STRINGPREP_BIDI_LEADTRAIL_NOT_RAL = 4,
  50. STRINGPREP_BIDI_CONTAINS_PROHIBITED = 5,
  51. /* Error in calling application. */
  52. STRINGPREP_TOO_SMALL_BUFFER = 100,
  53. STRINGPREP_PROFILE_ERROR = 101,
  54. STRINGPREP_FLAG_ERROR = 102,
  55. STRINGPREP_UNKNOWN_PROFILE = 103,
  56. STRINGPREP_ICONV_ERROR = 104,
  57. /* Internal errors. */
  58. STRINGPREP_NFKC_FAILED = 200,
  59. STRINGPREP_MALLOC_ERROR = 201
  60. } Stringprep_rc;
  61. /* Flags used when calling stringprep(). */
  62. typedef enum
  63. {
  64. STRINGPREP_NO_NFKC = 1,
  65. STRINGPREP_NO_BIDI = 2,
  66. STRINGPREP_NO_UNASSIGNED = 4
  67. } Stringprep_profile_flags;
  68. /* Steps in a stringprep profile. */
  69. typedef enum
  70. {
  71. STRINGPREP_NFKC = 1,
  72. STRINGPREP_BIDI = 2,
  73. STRINGPREP_MAP_TABLE = 3,
  74. STRINGPREP_UNASSIGNED_TABLE = 4,
  75. STRINGPREP_PROHIBIT_TABLE = 5,
  76. STRINGPREP_BIDI_PROHIBIT_TABLE = 6,
  77. STRINGPREP_BIDI_RAL_TABLE = 7,
  78. STRINGPREP_BIDI_L_TABLE = 8
  79. } Stringprep_profile_steps;
  80. # define STRINGPREP_MAX_MAP_CHARS 4
  81. /* *INDENT-OFF* */
  82. /* Why INDENT-OFF? GTK-DOC has a bug
  83. * <https://gitlab.gnome.org/GNOME/gtk-doc/-/issues/37> which causes
  84. * parsing of structs to fail unless the terminating } is at the
  85. * beginning of the line. We hard-code the header file to be like
  86. * that, and add the INDENT-OFF markers so that indent won't restore
  87. * them. When that bug is fixed, remove the INDENT-* marker, run
  88. * 'make indent', and make sure that
  89. * doc/reference/libidn-decl-list.txt stay the same.
  90. *
  91. * Of course, exposing these struct's in the public header file in
  92. * the first place was a mistake.
  93. */
  94. /**
  95. * Stringprep_table_element:
  96. * @start: starting codepoint.
  97. * @end: ending codepoint, 0 if only one character.
  98. * @map: codepoints to map @start into, NULL if end is not 0.
  99. *
  100. * Stringprep profile table element.
  101. */
  102. struct Stringprep_table_element
  103. {
  104. uint32_t start;
  105. uint32_t end;
  106. uint32_t map[STRINGPREP_MAX_MAP_CHARS];
  107. };
  108. typedef struct Stringprep_table_element Stringprep_table_element;
  109. /**
  110. * Stringprep_table:
  111. * @operation: a #Stringprep_profile_steps value
  112. * @flags: a #Stringprep_profile_flags value
  113. * @table: zero-terminated array of %Stringprep_table_element elements.
  114. * @table_size: size of @table, to speed up searching.
  115. *
  116. * Stringprep profile table.
  117. */
  118. struct Stringprep_table
  119. {
  120. Stringprep_profile_steps operation;
  121. Stringprep_profile_flags flags;
  122. const Stringprep_table_element *table;
  123. size_t table_size;
  124. };
  125. /**
  126. * Stringprep_profile:
  127. *
  128. * Stringprep profile table.
  129. */
  130. typedef struct Stringprep_table Stringprep_profile;
  131. /**
  132. * Stringprep_profiles:
  133. * @name: name of stringprep profile.
  134. * @tables: zero-terminated array of %Stringprep_profile elements.
  135. *
  136. * Element structure
  137. */
  138. struct Stringprep_profiles
  139. {
  140. const char *name;
  141. const Stringprep_profile *tables;
  142. };
  143. typedef struct Stringprep_profiles Stringprep_profiles;
  144. /* *INDENT-ON* */
  145. extern IDNAPI const Stringprep_profiles stringprep_profiles[];
  146. /* Profiles */
  147. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_A_1[];
  148. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_B_1[];
  149. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_B_2[];
  150. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_B_3[];
  151. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_1_1[];
  152. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_1_2[];
  153. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_2_1[];
  154. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_2_2[];
  155. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_3[];
  156. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_4[];
  157. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_5[];
  158. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_6[];
  159. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_7[];
  160. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_8[];
  161. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_9[];
  162. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_D_1[];
  163. extern IDNAPI const Stringprep_table_element stringprep_rfc3454_D_2[];
  164. /* Nameprep */
  165. extern IDNAPI const Stringprep_profile stringprep_nameprep[];
  166. # define stringprep_nameprep(in, maxlen) \
  167. stringprep(in, maxlen, 0, stringprep_nameprep)
  168. # define stringprep_nameprep_no_unassigned(in, maxlen) \
  169. stringprep(in, maxlen, STRINGPREP_NO_UNASSIGNED, stringprep_nameprep)
  170. /* SASL */
  171. extern IDNAPI const Stringprep_profile stringprep_saslprep[];
  172. extern IDNAPI const Stringprep_table_element
  173. stringprep_saslprep_space_map[];
  174. extern IDNAPI const Stringprep_profile stringprep_plain[];
  175. extern IDNAPI const Stringprep_profile stringprep_trace[];
  176. # define stringprep_plain(in, maxlen) \
  177. stringprep(in, maxlen, 0, stringprep_plain)
  178. /* Kerberos */
  179. extern IDNAPI const Stringprep_profile stringprep_kerberos5[];
  180. # define stringprep_kerberos5(in, maxlen) \
  181. stringprep(in, maxlen, 0, stringprep_kerberos5)
  182. /* XMPP */
  183. extern IDNAPI const Stringprep_profile stringprep_xmpp_nodeprep[];
  184. extern IDNAPI const Stringprep_profile stringprep_xmpp_resourceprep[];
  185. extern IDNAPI const Stringprep_table_element
  186. stringprep_xmpp_nodeprep_prohibit[];
  187. # define stringprep_xmpp_nodeprep(in, maxlen) \
  188. stringprep(in, maxlen, 0, stringprep_xmpp_nodeprep)
  189. # define stringprep_xmpp_resourceprep(in, maxlen) \
  190. stringprep(in, maxlen, 0, stringprep_xmpp_resourceprep)
  191. /* iSCSI */
  192. extern IDNAPI const Stringprep_profile stringprep_iscsi[];
  193. extern IDNAPI const Stringprep_table_element stringprep_iscsi_prohibit[];
  194. # define stringprep_iscsi(in, maxlen) \
  195. stringprep(in, maxlen, 0, stringprep_iscsi)
  196. /* API */
  197. extern IDNAPI int stringprep_4i (uint32_t * ucs4, size_t *len,
  198. size_t maxucs4len,
  199. Stringprep_profile_flags flags,
  200. const Stringprep_profile * profile);
  201. extern IDNAPI int stringprep_4zi (uint32_t * ucs4, size_t maxucs4len,
  202. Stringprep_profile_flags flags,
  203. const Stringprep_profile * profile);
  204. extern IDNAPI int stringprep (char *in, size_t maxlen,
  205. Stringprep_profile_flags flags,
  206. const Stringprep_profile * profile);
  207. extern IDNAPI int stringprep_profile (const char *in,
  208. char **out,
  209. const char *profile,
  210. Stringprep_profile_flags flags);
  211. extern IDNAPI const char *stringprep_strerror (Stringprep_rc rc);
  212. extern IDNAPI const char *stringprep_check_version (const char
  213. *req_version);
  214. /* Utility */
  215. extern IDNAPI int stringprep_unichar_to_utf8 (uint32_t c, char *outbuf);
  216. extern IDNAPI uint32_t stringprep_utf8_to_unichar (const char *p);
  217. extern IDNAPI uint32_t *stringprep_utf8_to_ucs4 (const char *str,
  218. ssize_t len,
  219. size_t *items_written);
  220. extern IDNAPI char *stringprep_ucs4_to_utf8 (const uint32_t * str,
  221. ssize_t len,
  222. size_t *items_read,
  223. size_t *items_written);
  224. extern IDNAPI char *stringprep_utf8_nfkc_normalize (const char *str,
  225. ssize_t len);
  226. extern IDNAPI uint32_t *stringprep_ucs4_nfkc_normalize (const uint32_t *
  227. str, ssize_t len);
  228. extern IDNAPI const char *stringprep_locale_charset (void);
  229. extern IDNAPI char *stringprep_convert (const char *str,
  230. const char *to_codeset,
  231. const char *from_codeset);
  232. extern IDNAPI char *stringprep_locale_to_utf8 (const char *str);
  233. extern IDNAPI char *stringprep_utf8_to_locale (const char *str);
  234. # ifdef __cplusplus
  235. }
  236. # endif
  237. #endif /* STRINGPREP_H */