1/* stringprep.h --- Header file for stringprep functions.
2 Copyright (C) 2002-2013 Simon Josefsson
3
4 This file is part of GNU Libidn.
5
6 GNU Libidn is free software: you can redistribute it and/or
7 modify it under the terms of either:
8
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version.
12
13 or
14
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version.
18
19 or both in parallel, as here.
20
21 GNU Libidn is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
25
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
29
30#ifndef STRINGPREP_H
31# define STRINGPREP_H
32
33# ifndef IDNAPI
34# if defined LIBIDN_BUILDING && defined HAVE_VISIBILITY && HAVE_VISIBILITY
35# define IDNAPI __attribute__((__visibility__("default")))
36# elif defined LIBIDN_BUILDING && defined _MSC_VER && ! defined LIBIDN_STATIC
37# define IDNAPI __declspec(dllexport)
38# elif defined _MSC_VER && ! defined LIBIDN_STATIC
39# define IDNAPI __declspec(dllimport)
40# else
41# define IDNAPI
42# endif
43# endif
44
45# include <stddef.h> /* size_t */
46# include <sys/types.h> /* ssize_t */
47# include <idn-int.h> /* uint32_t */
48
49# ifdef __cplusplus
50extern "C"
51{
52# endif
53
54# define STRINGPREP_VERSION "1.28"
55
56/* Error codes. */
57 typedef enum
58 {
59 STRINGPREP_OK = 0,
60 /* Stringprep errors. */
61 STRINGPREP_CONTAINS_UNASSIGNED = 1,
62 STRINGPREP_CONTAINS_PROHIBITED = 2,
63 STRINGPREP_BIDI_BOTH_L_AND_RAL = 3,
64 STRINGPREP_BIDI_LEADTRAIL_NOT_RAL = 4,
65 STRINGPREP_BIDI_CONTAINS_PROHIBITED = 5,
66 /* Error in calling application. */
67 STRINGPREP_TOO_SMALL_BUFFER = 100,
68 STRINGPREP_PROFILE_ERROR = 101,
69 STRINGPREP_FLAG_ERROR = 102,
70 STRINGPREP_UNKNOWN_PROFILE = 103,
71 /* Internal errors. */
72 STRINGPREP_NFKC_FAILED = 200,
73 STRINGPREP_MALLOC_ERROR = 201
74 } Stringprep_rc;
75
76/* Flags used when calling stringprep(). */
77 typedef enum
78 {
79 STRINGPREP_NO_NFKC = 1,
80 STRINGPREP_NO_BIDI = 2,
81 STRINGPREP_NO_UNASSIGNED = 4
82 } Stringprep_profile_flags;
83
84/* Steps in a stringprep profile. */
85 typedef enum
86 {
87 STRINGPREP_NFKC = 1,
88 STRINGPREP_BIDI = 2,
89 STRINGPREP_MAP_TABLE = 3,
90 STRINGPREP_UNASSIGNED_TABLE = 4,
91 STRINGPREP_PROHIBIT_TABLE = 5,
92 STRINGPREP_BIDI_PROHIBIT_TABLE = 6,
93 STRINGPREP_BIDI_RAL_TABLE = 7,
94 STRINGPREP_BIDI_L_TABLE = 8
95 } Stringprep_profile_steps;
96
97# define STRINGPREP_MAX_MAP_CHARS 4
98
99 struct Stringprep_table_element
100 {
101 uint32_t start;
102 uint32_t end; /* 0 if only one character */
103 uint32_t map[STRINGPREP_MAX_MAP_CHARS]; /* NULL if end is not 0 */
104 };
105 typedef struct Stringprep_table_element Stringprep_table_element;
106
107 struct Stringprep_table
108 {
109 Stringprep_profile_steps operation;
110 Stringprep_profile_flags flags;
111 const Stringprep_table_element *table;
112 };
113 typedef struct Stringprep_table Stringprep_profile;
114
115 struct Stringprep_profiles
116 {
117 const char *name;
118 const Stringprep_profile *tables;
119 };
120 typedef struct Stringprep_profiles Stringprep_profiles;
121
122 extern IDNAPI const Stringprep_profiles stringprep_profiles[];
123
124/* Profiles */
125 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_A_1[];
126 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_B_1[];
127 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_B_2[];
128 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_B_3[];
129 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_1_1[];
130 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_1_2[];
131 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_2_1[];
132 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_2_2[];
133 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_3[];
134 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_4[];
135 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_5[];
136 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_6[];
137 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_7[];
138 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_8[];
139 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_C_9[];
140 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_D_1[];
141 extern IDNAPI const Stringprep_table_element stringprep_rfc3454_D_2[];
142
143 /* Nameprep */
144
145 extern IDNAPI const Stringprep_profile stringprep_nameprep[];
146
147# define stringprep_nameprep(in, maxlen) \
148 stringprep(in, maxlen, 0, stringprep_nameprep)
149
150# define stringprep_nameprep_no_unassigned(in, maxlen) \
151 stringprep(in, maxlen, STRINGPREP_NO_UNASSIGNED, stringprep_nameprep)
152
153 /* SASL */
154
155 extern IDNAPI const Stringprep_profile stringprep_saslprep[];
156 extern IDNAPI const Stringprep_table_element stringprep_saslprep_space_map[];
157 extern IDNAPI const Stringprep_profile stringprep_plain[];
158 extern IDNAPI const Stringprep_profile stringprep_trace[];
159
160# define stringprep_plain(in, maxlen) \
161 stringprep(in, maxlen, 0, stringprep_plain)
162
163 /* Kerberos */
164
165 extern IDNAPI const Stringprep_profile stringprep_kerberos5[];
166
167# define stringprep_kerberos5(in, maxlen) \
168 stringprep(in, maxlen, 0, stringprep_kerberos5)
169
170 /* XMPP */
171
172 extern IDNAPI const Stringprep_profile stringprep_xmpp_nodeprep[];
173 extern IDNAPI const Stringprep_profile stringprep_xmpp_resourceprep[];
174 extern IDNAPI const Stringprep_table_element stringprep_xmpp_nodeprep_prohibit[];
175
176# define stringprep_xmpp_nodeprep(in, maxlen) \
177 stringprep(in, maxlen, 0, stringprep_xmpp_nodeprep)
178# define stringprep_xmpp_resourceprep(in, maxlen) \
179 stringprep(in, maxlen, 0, stringprep_xmpp_resourceprep)
180
181 /* iSCSI */
182
183 extern IDNAPI const Stringprep_profile stringprep_iscsi[];
184 extern IDNAPI const Stringprep_table_element stringprep_iscsi_prohibit[];
185
186# define stringprep_iscsi(in, maxlen) \
187 stringprep(in, maxlen, 0, stringprep_iscsi)
188
189 /* API */
190
191 extern IDNAPI int stringprep_4i (uint32_t * ucs4, size_t * len,
192 size_t maxucs4len,
193 Stringprep_profile_flags flags,
194 const Stringprep_profile * profile);
195 extern IDNAPI int stringprep_4zi (uint32_t * ucs4, size_t maxucs4len,
196 Stringprep_profile_flags flags,
197 const Stringprep_profile * profile);
198 extern IDNAPI int stringprep (char *in, size_t maxlen,
199 Stringprep_profile_flags flags,
200 const Stringprep_profile * profile);
201
202 extern IDNAPI int stringprep_profile (const char *in,
203 char **out,
204 const char *profile,
205 Stringprep_profile_flags flags);
206
207 extern IDNAPI const char *stringprep_strerror (Stringprep_rc rc);
208
209 extern IDNAPI const char *stringprep_check_version (const char
210 *req_version);
211
212/* Utility */
213
214 extern IDNAPI int stringprep_unichar_to_utf8 (uint32_t c, char *outbuf);
215 extern IDNAPI uint32_t stringprep_utf8_to_unichar (const char *p);
216
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
225 extern IDNAPI char *stringprep_utf8_nfkc_normalize (const char *str,
226 ssize_t len);
227 extern IDNAPI uint32_t *stringprep_ucs4_nfkc_normalize (const uint32_t * str,
228 ssize_t len);
229
230 extern IDNAPI const char *stringprep_locale_charset (void);
231 extern IDNAPI char *stringprep_convert (const char *str,
232 const char *to_codeset,
233 const char *from_codeset);
234 extern IDNAPI char *stringprep_locale_to_utf8 (const char *str);
235 extern IDNAPI char *stringprep_utf8_to_locale (const char *str);
236
237# ifdef __cplusplus
238}
239# endif
240
241#endif /* STRINGPREP_H */
242