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