Merge branch 'v4-0-stable' into newmaster
[vlendec/samba-autobuild/.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 /*
43  * SMB UCS2 (16-bit unicode) internal type.
44  * smb_ucs2_t is *always* in little endian format.
45  */
46
47 #ifdef WORDS_BIGENDIAN
48 #define UCS2_SHIFT 8
49 #else
50 #define UCS2_SHIFT 0
51 #endif
52
53 /* turn a 7 bit character into a ucs2 character */
54 #define UCS2_CHAR(c) ((c) << UCS2_SHIFT)
55
56 /* return an ascii version of a ucs2 character */
57 #define UCS2_TO_CHAR(c) (((c) >> UCS2_SHIFT) & 0xff)
58
59 /* Copy into a smb_ucs2_t from a possibly unaligned buffer. Return the copied smb_ucs2_t */
60 #define COPY_UCS2_CHAR(dest,src) (((unsigned char *)(dest))[0] = ((unsigned char *)(src))[0],\
61                                 ((unsigned char *)(dest))[1] = ((unsigned char *)(src))[1], (dest))
62
63
64
65 /*
66  *   for each charset we have a function that pulls from that charset to
67  *     a ucs2 buffer, and a function that pushes to a ucs2 buffer
68  *     */
69
70 struct charset_functions {
71         const char *name;
72         size_t (*pull)(void *, const char **inbuf, size_t *inbytesleft,
73                                    char **outbuf, size_t *outbytesleft);
74         size_t (*push)(void *, const char **inbuf, size_t *inbytesleft,
75                                    char **outbuf, size_t *outbytesleft);
76         struct charset_functions *prev, *next;
77 };
78
79 /* this type is used for manipulating unicode codepoints */
80 typedef uint32_t codepoint_t;
81
82 #define INVALID_CODEPOINT ((codepoint_t)-1)
83
84 /*
85  * This is auxiliary struct used by source/script/gen-8-bit-gap.sh script
86  * during generation of an encoding table for charset module
87  *     */
88
89 struct charset_gap_table {
90   uint16_t start;
91   uint16_t end;
92   int32_t idx;
93 };
94
95
96 /* generic iconv conversion structure */
97 typedef struct smb_iconv_s {
98         size_t (*direct)(void *cd, const char **inbuf, size_t *inbytesleft,
99                          char **outbuf, size_t *outbytesleft);
100         size_t (*pull)(void *cd, const char **inbuf, size_t *inbytesleft,
101                        char **outbuf, size_t *outbytesleft);
102         size_t (*push)(void *cd, const char **inbuf, size_t *inbytesleft,
103                        char **outbuf, size_t *outbytesleft);
104         void *cd_direct, *cd_pull, *cd_push;
105         char *from_name, *to_name;
106 } *smb_iconv_t;
107
108 /* string manipulation flags */
109 #define STR_TERMINATE 1
110 #define STR_UPPER 2
111 #define STR_ASCII 4
112 #define STR_UNICODE 8
113 #define STR_NOALIGN 16
114 #define STR_NO_RANGE_CHECK 32
115 #define STR_LEN8BIT 64
116 #define STR_TERMINATE_ASCII 128 /* only terminate if ascii */
117 #define STR_LEN_NOTERM 256 /* the length field is the unterminated length */
118
119 struct loadparm_context;
120 struct smb_iconv_convenience;
121
122 /* replace some string functions with multi-byte
123    versions */
124 #define strlower(s) strlower_m(s)
125 #define strupper(s) strupper_m(s)
126
127 char *strchr_m(const char *s, char c);
128 size_t strlen_m_term(const char *s);
129 size_t strlen_m_term_null(const char *s);
130 size_t strlen_m(const char *s);
131 char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength);
132 void string_replace_m(char *s, char oldc, char newc);
133 bool strcsequal_m(const char *s1,const char *s2);
134 bool strequal_m(const char *s1, const char *s2);
135 int strncasecmp_m(const char *s1, const char *s2, size_t n);
136 bool next_token(const char **ptr,char *buff, const char *sep, size_t bufsize);
137 int strcasecmp_m(const char *s1, const char *s2);
138 size_t count_chars_m(const char *s, char c);
139 void strupper_m(char *s);
140 void strlower_m(char *s);
141 char *strupper_talloc(TALLOC_CTX *ctx, const char *src);
142 char *talloc_strdup_upper(TALLOC_CTX *ctx, const char *src);
143 char *strupper_talloc_n(TALLOC_CTX *ctx, const char *src, size_t n);
144 char *strlower_talloc(TALLOC_CTX *ctx, const char *src);
145 bool strhasupper(const char *string);
146 bool strhaslower(const char *string);
147 char *strrchr_m(const char *s, char c);
148 char *strchr_m(const char *s, char c);
149
150 bool push_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src, size_t *converted_size);
151 bool push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src, size_t *converted_size);
152 bool push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src, size_t *converted_size);
153 bool pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src, size_t *converted_size);
154 bool pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const smb_ucs2_t *src, size_t *converted_size);
155 bool pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src, size_t *converted_size);
156 ssize_t push_string(void *dest, const char *src, size_t dest_len, int flags);
157 ssize_t pull_string(char *dest, const void *src, size_t dest_len, size_t src_len, int flags);
158
159 bool convert_string_talloc(TALLOC_CTX *ctx, 
160                                        charset_t from, charset_t to, 
161                                        void const *src, size_t srclen, 
162                                        void *dest, size_t *converted_size, 
163                                            bool allow_badcharcnv);
164
165 size_t convert_string(charset_t from, charset_t to,
166                                 void const *src, size_t srclen, 
167                                 void *dest, size_t destlen, bool allow_badcharcnv);
168
169 ssize_t iconv_talloc(TALLOC_CTX *mem_ctx, 
170                                        smb_iconv_t cd,
171                                        void const *src, size_t srclen, 
172                                        void *dest);
173
174 extern struct smb_iconv_convenience *global_iconv_convenience;
175
176 codepoint_t next_codepoint(const char *str, size_t *size);
177 ssize_t push_codepoint(char *str, codepoint_t c);
178
179 /* codepoints */
180 codepoint_t next_codepoint_convenience(struct smb_iconv_convenience *ic, 
181                             const char *str, size_t *size);
182 ssize_t push_codepoint_convenience(struct smb_iconv_convenience *ic, 
183                                 char *str, codepoint_t c);
184 codepoint_t toupper_m(codepoint_t val);
185 codepoint_t tolower_m(codepoint_t val);
186 int codepoint_cmpi(codepoint_t c1, codepoint_t c2);
187
188 /* Iconv convenience functions */
189 struct smb_iconv_convenience *smb_iconv_convenience_reinit(TALLOC_CTX *mem_ctx,
190                                                            const char *dos_charset,
191                                                            const char *unix_charset,
192                                                            bool native_iconv,
193                                                            struct smb_iconv_convenience *old_ic);
194
195 bool convert_string_convenience(struct smb_iconv_convenience *ic,
196                                 charset_t from, charset_t to,
197                                 void const *src, size_t srclen, 
198                                 void *dest, size_t destlen, size_t *converted_size,
199                                 bool allow_badcharcnv);
200 bool convert_string_talloc_convenience(TALLOC_CTX *ctx, 
201                                        struct smb_iconv_convenience *ic, 
202                                        charset_t from, charset_t to, 
203                                        void const *src, size_t srclen, 
204                                        void *dest, size_t *converted_size, bool allow_badcharcnv);
205 /* iconv */
206 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode);
207 int smb_iconv_close(smb_iconv_t cd);
208 size_t smb_iconv(smb_iconv_t cd, 
209                  const char **inbuf, size_t *inbytesleft,
210                  char **outbuf, size_t *outbytesleft);
211 smb_iconv_t smb_iconv_open_ex(TALLOC_CTX *mem_ctx, const char *tocode, 
212                               const char *fromcode, bool native_iconv);
213
214 void load_case_tables(void);
215 bool charset_register_backend(const void *_funcs);
216
217 /*
218  *   Define stub for charset module which implements 8-bit encoding with gaps.
219  *   Encoding tables for such module should be produced from glibc's CHARMAPs
220  *   using script source/script/gen-8bit-gap.sh
221  *   CHARSETNAME is CAPITALIZED charset name
222  *
223  *     */
224 #define SMB_GENERATE_CHARSET_MODULE_8_BIT_GAP(CHARSETNAME)                                      \
225 static size_t CHARSETNAME ## _push(void *cd, const char **inbuf, size_t *inbytesleft,                   \
226                          char **outbuf, size_t *outbytesleft)                                   \
227 {                                                                                               \
228         while (*inbytesleft >= 2 && *outbytesleft >= 1) {                                       \
229                 int i;                                                                          \
230                 int done = 0;                                                                   \
231                                                                                                 \
232                 uint16 ch = SVAL(*inbuf,0);                                                     \
233                                                                                                 \
234                 for (i=0; from_idx[i].start != 0xffff; i++) {                                   \
235                         if ((from_idx[i].start <= ch) && (from_idx[i].end >= ch)) {             \
236                                 ((unsigned char*)(*outbuf))[0] = from_ucs2[from_idx[i].idx+ch]; \
237                                 (*inbytesleft) -= 2;                                            \
238                                 (*outbytesleft) -= 1;                                           \
239                                 (*inbuf)  += 2;                                                 \
240                                 (*outbuf) += 1;                                                 \
241                                 done = 1;                                                       \
242                                 break;                                                          \
243                         }                                                                       \
244                 }                                                                               \
245                 if (!done) {                                                                    \
246                         errno = EINVAL;                                                         \
247                         return -1;                                                              \
248                 }                                                                               \
249                                                                                                 \
250         }                                                                                       \
251                                                                                                 \
252         if (*inbytesleft == 1) {                                                                \
253                 errno = EINVAL;                                                                 \
254                 return -1;                                                                      \
255         }                                                                                       \
256                                                                                                 \
257         if (*inbytesleft > 1) {                                                                 \
258                 errno = E2BIG;                                                                  \
259                 return -1;                                                                      \
260         }                                                                                       \
261                                                                                                 \
262         return 0;                                                                               \
263 }                                                                                               \
264                                                                                                 \
265 static size_t CHARSETNAME ## _pull(void *cd, const char **inbuf, size_t *inbytesleft,                           \
266                          char **outbuf, size_t *outbytesleft)                                   \
267 {                                                                                               \
268         while (*inbytesleft >= 1 && *outbytesleft >= 2) {                                       \
269                 SSVAL(*outbuf, 0, to_ucs2[((unsigned char*)(*inbuf))[0]]);                      \
270                 (*inbytesleft)  -= 1;                                                           \
271                 (*outbytesleft) -= 2;                                                           \
272                 (*inbuf)  += 1;                                                                 \
273                 (*outbuf) += 2;                                                                 \
274         }                                                                                       \
275                                                                                                 \
276         if (*inbytesleft > 0) {                                                                 \
277                 errno = E2BIG;                                                                  \
278                 return -1;                                                                      \
279         }                                                                                       \
280                                                                                                 \
281         return 0;                                                                               \
282 }                                                                                               \
283                                                                                                 \
284 struct charset_functions CHARSETNAME ## _functions =                                            \
285                 {#CHARSETNAME, CHARSETNAME ## _pull, CHARSETNAME ## _push};                     \
286                                                                                                 \
287 NTSTATUS charset_ ## CHARSETNAME ## _init(void);                                                        \
288 NTSTATUS charset_ ## CHARSETNAME ## _init(void)                                                 \
289 {                                                                                               \
290         return smb_register_charset(& CHARSETNAME ## _functions);                               \
291 }                                                                                               \
292
293
294 #endif /* __CHARSET_H__ */