d027daa052c85e6b02f15eb64a25f0c4c73ded53
[samba.git] / lib / util / charset / charset.h
1 /* 
2    Unix SMB/CIFS implementation.
3    charset defines
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Jelmer Vernooij 2002
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /* This is a public header file that is installed as part of Samba. 
22  * If you remove any functions or change their signature, update 
23  * the so version number. */
24
25 #ifndef __CHARSET_H__
26 #define __CHARSET_H__
27
28 #include <talloc.h>
29
30 /* this defines the charset types used in samba */
31 typedef enum {CH_UTF16LE=0, CH_UTF16=0, CH_UNIX, CH_DISPLAY, CH_DOS, CH_UTF8, CH_UTF16BE, CH_UTF16MUNGED} charset_t;
32
33 #define NUM_CHARSETS 7
34
35 /*
36  * SMB UCS2 (16-bit unicode) internal type.
37  * smb_ucs2_t is *always* in little endian format.
38  */
39
40 typedef uint16_t smb_ucs2_t;
41
42 #ifdef WORDS_BIGENDIAN
43 #define UCS2_SHIFT 8
44 #else
45 #define UCS2_SHIFT 0
46 #endif
47
48 /* turn a 7 bit character into a ucs2 character */
49 #define UCS2_CHAR(c) ((c) << UCS2_SHIFT)
50
51 /*
52  *   for each charset we have a function that pulls from that charset to
53  *     a ucs2 buffer, and a function that pushes to a ucs2 buffer
54  *     */
55
56 struct charset_functions {
57         const char *name;
58         size_t (*pull)(void *, const char **inbuf, size_t *inbytesleft,
59                                    char **outbuf, size_t *outbytesleft);
60         size_t (*push)(void *, const char **inbuf, size_t *inbytesleft,
61                                    char **outbuf, size_t *outbytesleft);
62         struct charset_functions *prev, *next;
63 };
64
65 /* this type is used for manipulating unicode codepoints */
66 typedef uint32_t codepoint_t;
67
68 #define INVALID_CODEPOINT ((codepoint_t)-1)
69
70 /*
71  * This is auxiliary struct used by source/script/gen-8-bit-gap.sh script
72  * during generation of an encoding table for charset module
73  *     */
74
75 struct charset_gap_table {
76   uint16_t start;
77   uint16_t end;
78   int32_t idx;
79 };
80
81
82 /* generic iconv conversion structure */
83 typedef struct smb_iconv_s {
84         size_t (*direct)(void *cd, const char **inbuf, size_t *inbytesleft,
85                          char **outbuf, size_t *outbytesleft);
86         size_t (*pull)(void *cd, const char **inbuf, size_t *inbytesleft,
87                        char **outbuf, size_t *outbytesleft);
88         size_t (*push)(void *cd, const char **inbuf, size_t *inbytesleft,
89                        char **outbuf, size_t *outbytesleft);
90         void *cd_direct, *cd_pull, *cd_push;
91         char *from_name, *to_name;
92 } *smb_iconv_t;
93
94 /* string manipulation flags */
95 #define STR_TERMINATE 1
96 #define STR_UPPER 2
97 #define STR_ASCII 4
98 #define STR_UNICODE 8
99 #define STR_NOALIGN 16
100 #define STR_NO_RANGE_CHECK 32
101 #define STR_LEN8BIT 64
102 #define STR_TERMINATE_ASCII 128 /* only terminate if ascii */
103 #define STR_LEN_NOTERM 256 /* the length field is the unterminated length */
104
105 struct loadparm_context;
106 struct smb_iconv_handle;
107
108 /* replace some string functions with multi-byte
109    versions */
110 #define strlower(s) strlower_m(s)
111 #define strupper(s) strupper_m(s)
112
113 char *strchr_m(const char *s, char c);
114 /**
115  * Calculate the number of units (8 or 16-bit, depending on the
116  * destination charset), that would be needed to convert the input
117  * string which is expected to be in in src_charset encoding to the
118  * destination charset (which should be a unicode charset).
119  */
120 size_t strlen_m_ext_handle(struct smb_iconv_handle *ic,
121                            const char *s, charset_t src_charset, charset_t dst_charset);
122 size_t strlen_m_ext(const char *s, charset_t src_charset, charset_t dst_charset);
123 size_t strlen_m_ext_term(const char *s, charset_t src_charset,
124                          charset_t dst_charset);
125 size_t strlen_m_term(const char *s);
126 size_t strlen_m_term_null(const char *s);
127 size_t strlen_m(const char *s);
128 char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength);
129 void string_replace_m(char *s, char oldc, char newc);
130 bool strcsequal(const char *s1,const char *s2);
131 bool strequal_m(const char *s1, const char *s2);
132 int strncasecmp_m(const char *s1, const char *s2, size_t n);
133 int strncasecmp_m_handle(struct smb_iconv_handle *iconv_handle,
134                          const char *s1, const char *s2, size_t n);
135 bool next_token(const char **ptr,char *buff, const char *sep, size_t bufsize);
136 int strcasecmp_m_handle(struct smb_iconv_handle *iconv_handle,
137                         const char *s1, const char *s2);
138 int strcasecmp_m(const char *s1, const char *s2);
139 size_t count_chars_m(const char *s, char c);
140 void strupper_m(char *s);
141 void strlower_m(char *s);
142 char *strupper_talloc(TALLOC_CTX *ctx, const char *src);
143 char *talloc_strdup_upper(TALLOC_CTX *ctx, const char *src);
144 char *strupper_talloc_n_handle(struct smb_iconv_handle *iconv_handle,
145                                 TALLOC_CTX *ctx, const char *src, size_t n);
146 char *strupper_talloc_n(TALLOC_CTX *ctx, const char *src, size_t n);
147  char *strlower_talloc_handle(struct smb_iconv_handle *iconv_handle,
148                               TALLOC_CTX *ctx, const char *src);
149 char *strlower_talloc(TALLOC_CTX *ctx, const char *src);
150 bool strhasupper(const char *string);
151 bool strhaslower_handle(struct smb_iconv_handle *ic,
152                         const char *string);
153 bool strhaslower(const char *string);
154 bool strhasupper_handle(struct smb_iconv_handle *ic,
155                         const char *string);
156 char *strrchr_m(const char *s, char c);
157 char *strchr_m(const char *s, char c);
158 char *strstr_m(const char *src, const char *findstr);
159
160 bool push_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src, size_t *converted_size);
161 bool push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src, size_t *converted_size);
162 bool push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src, size_t *converted_size);
163 bool pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src, size_t *converted_size);
164 bool pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const smb_ucs2_t *src, size_t *converted_size);
165 bool pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src, size_t *converted_size);
166 ssize_t push_string(void *dest, const char *src, size_t dest_len, int flags);
167 ssize_t pull_string(char *dest, const void *src, size_t dest_len, size_t src_len, int flags);
168
169 bool convert_string_talloc(TALLOC_CTX *ctx, 
170                            charset_t from, charset_t to, 
171                            void const *src, size_t srclen, 
172                            void *dest, size_t *converted_size);
173
174 bool convert_string(charset_t from, charset_t to,
175                       void const *src, size_t srclen, 
176                       void *dest, size_t destlen,
177                       size_t *converted_size);
178 bool convert_string_error(charset_t from, charset_t to,
179                           void const *src, size_t srclen,
180                           void *dest, size_t destlen,
181                           size_t *converted_size);
182
183 ssize_t iconv_talloc(TALLOC_CTX *mem_ctx, 
184                                        smb_iconv_t cd,
185                                        void const *src, size_t srclen, 
186                                        void *dest);
187
188 extern struct smb_iconv_handle *global_iconv_handle;
189 struct smb_iconv_handle *get_iconv_handle(void);
190 struct smb_iconv_handle *get_iconv_testing_handle(TALLOC_CTX *mem_ctx, 
191                                                   const char *dos_charset, 
192                                                   const char *unix_charset, 
193                                                   const char *display_charset);
194 smb_iconv_t get_conv_handle(struct smb_iconv_handle *ic,
195                             charset_t from, charset_t to);
196 const char *charset_name(struct smb_iconv_handle *ic, charset_t ch);
197
198 codepoint_t next_codepoint_ext(const char *str, charset_t src_charset,
199                                size_t *size);
200 codepoint_t next_codepoint(const char *str, size_t *size);
201 ssize_t push_codepoint(char *str, codepoint_t c);
202
203 /* codepoints */
204 codepoint_t next_codepoint_handle_ext(struct smb_iconv_handle *ic,
205                             const char *str, charset_t src_charset,
206                             size_t *size);
207 codepoint_t next_codepoint_handle(struct smb_iconv_handle *ic,
208                             const char *str, size_t *size);
209 ssize_t push_codepoint_handle(struct smb_iconv_handle *ic,
210                                 char *str, codepoint_t c);
211
212 codepoint_t toupper_m(codepoint_t val);
213 codepoint_t tolower_m(codepoint_t val);
214 bool islower_m(codepoint_t val);
215 bool isupper_m(codepoint_t val);
216 int codepoint_cmpi(codepoint_t c1, codepoint_t c2);
217
218 /* Iconv convenience functions */
219 struct smb_iconv_handle *smb_iconv_handle_reinit(TALLOC_CTX *mem_ctx,
220                                                            const char *dos_charset,
221                                                            const char *unix_charset,
222                                                            const char *display_charset,
223                                                            bool native_iconv,
224                                                            struct smb_iconv_handle *old_ic);
225
226 bool convert_string_handle(struct smb_iconv_handle *ic,
227                                 charset_t from, charset_t to,
228                                 void const *src, size_t srclen, 
229                                 void *dest, size_t destlen, size_t *converted_size);
230 bool convert_string_error_handle(struct smb_iconv_handle *ic,
231                                  charset_t from, charset_t to,
232                                  void const *src, size_t srclen,
233                                  void *dest, size_t destlen,
234                                  size_t *converted_size);
235
236 bool convert_string_talloc_handle(TALLOC_CTX *ctx,
237                                        struct smb_iconv_handle *ic,
238                                        charset_t from, charset_t to, 
239                                        void const *src, size_t srclen, 
240                                        void *dest, size_t *converted_size);
241 /* iconv */
242 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode);
243 int smb_iconv_close(smb_iconv_t cd);
244 size_t smb_iconv(smb_iconv_t cd, 
245                  const char **inbuf, size_t *inbytesleft,
246                  char **outbuf, size_t *outbytesleft);
247 smb_iconv_t smb_iconv_open_ex(TALLOC_CTX *mem_ctx, const char *tocode, 
248                               const char *fromcode, bool native_iconv);
249
250 void load_case_tables(void);
251 void load_case_tables_library(void);
252 bool smb_register_charset(const struct charset_functions *funcs_in);
253
254 /* The following definitions come from util_unistr_w.c  */
255
256 size_t strlen_w(const smb_ucs2_t *src);
257 size_t strnlen_w(const smb_ucs2_t *src, size_t max);
258 smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c);
259 smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c);
260 smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c);
261 smb_ucs2_t *strnrchr_w(const smb_ucs2_t *s, smb_ucs2_t c, unsigned int n);
262 smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins);
263 bool strlower_w(smb_ucs2_t *s);
264 bool strupper_w(smb_ucs2_t *s);
265 int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b);
266 int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b);
267 int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len);
268 int strcmp_wa(const smb_ucs2_t *a, const char *b);
269 int toupper_ascii(int c);
270 int tolower_ascii(int c);
271 int isupper_ascii(int c);
272 int islower_ascii(int c);
273
274 /*
275  *   Define stub for charset module which implements 8-bit encoding with gaps.
276  *   Encoding tables for such module should be produced from glibc's CHARMAPs
277  *   using script source/script/gen-8bit-gap.sh
278  *   CHARSETNAME is CAPITALIZED charset name
279  *
280  *     */
281 #define SMB_GENERATE_CHARSET_MODULE_8_BIT_GAP(CHARSETNAME)                                      \
282 static size_t CHARSETNAME ## _push(void *cd, const char **inbuf, size_t *inbytesleft,                   \
283                          char **outbuf, size_t *outbytesleft)                                   \
284 {                                                                                               \
285         while (*inbytesleft >= 2 && *outbytesleft >= 1) {                                       \
286                 int i;                                                                          \
287                 int done = 0;                                                                   \
288                                                                                                 \
289                 uint16 ch = SVAL(*inbuf,0);                                                     \
290                                                                                                 \
291                 for (i=0; from_idx[i].start != 0xffff; i++) {                                   \
292                         if ((from_idx[i].start <= ch) && (from_idx[i].end >= ch)) {             \
293                                 ((unsigned char*)(*outbuf))[0] = from_ucs2[from_idx[i].idx+ch]; \
294                                 (*inbytesleft) -= 2;                                            \
295                                 (*outbytesleft) -= 1;                                           \
296                                 (*inbuf)  += 2;                                                 \
297                                 (*outbuf) += 1;                                                 \
298                                 done = 1;                                                       \
299                                 break;                                                          \
300                         }                                                                       \
301                 }                                                                               \
302                 if (!done) {                                                                    \
303                         errno = EINVAL;                                                         \
304                         return -1;                                                              \
305                 }                                                                               \
306                                                                                                 \
307         }                                                                                       \
308                                                                                                 \
309         if (*inbytesleft == 1) {                                                                \
310                 errno = EINVAL;                                                                 \
311                 return -1;                                                                      \
312         }                                                                                       \
313                                                                                                 \
314         if (*inbytesleft > 1) {                                                                 \
315                 errno = E2BIG;                                                                  \
316                 return -1;                                                                      \
317         }                                                                                       \
318                                                                                                 \
319         return 0;                                                                               \
320 }                                                                                               \
321                                                                                                 \
322 static size_t CHARSETNAME ## _pull(void *cd, const char **inbuf, size_t *inbytesleft,                           \
323                          char **outbuf, size_t *outbytesleft)                                   \
324 {                                                                                               \
325         while (*inbytesleft >= 1 && *outbytesleft >= 2) {                                       \
326                 SSVAL(*outbuf, 0, to_ucs2[((unsigned char*)(*inbuf))[0]]);                      \
327                 (*inbytesleft)  -= 1;                                                           \
328                 (*outbytesleft) -= 2;                                                           \
329                 (*inbuf)  += 1;                                                                 \
330                 (*outbuf) += 2;                                                                 \
331         }                                                                                       \
332                                                                                                 \
333         if (*inbytesleft > 0) {                                                                 \
334                 errno = E2BIG;                                                                  \
335                 return -1;                                                                      \
336         }                                                                                       \
337                                                                                                 \
338         return 0;                                                                               \
339 }                                                                                               \
340                                                                                                 \
341 struct charset_functions CHARSETNAME ## _functions =                                            \
342                 {#CHARSETNAME, CHARSETNAME ## _pull, CHARSETNAME ## _push};                     \
343                                                                                                 \
344 NTSTATUS charset_ ## CHARSETNAME ## _init(void);                                                        \
345 NTSTATUS charset_ ## CHARSETNAME ## _init(void)                                                 \
346 {                                                                                               \
347         if (!smb_register_charset(& CHARSETNAME ## _functions)) {       \
348                 return NT_STATUS_INTERNAL_ERROR;                        \
349         }                                                               \
350         return NT_STATUS_OK; \
351 }                                               \
352
353
354 #endif /* __CHARSET_H__ */