docs: fix a typo in history file
[bbaumbach/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_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         bool samba_internal_charset;
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 /* generic iconv conversion structure */
71 typedef struct smb_iconv_s {
72         size_t (*direct)(void *cd, const char **inbuf, size_t *inbytesleft,
73                          char **outbuf, size_t *outbytesleft);
74         size_t (*pull)(void *cd, const char **inbuf, size_t *inbytesleft,
75                        char **outbuf, size_t *outbytesleft);
76         size_t (*push)(void *cd, const char **inbuf, size_t *inbytesleft,
77                        char **outbuf, size_t *outbytesleft);
78         void *cd_direct, *cd_pull, *cd_push;
79         char *from_name, *to_name;
80 } *smb_iconv_t;
81
82 /* string manipulation flags */
83 #define STR_TERMINATE 1
84 #define STR_UPPER 2
85 #define STR_ASCII 4
86 #define STR_UNICODE 8
87 #define STR_NOALIGN 16
88 #define STR_NO_RANGE_CHECK 32
89 #define STR_LEN8BIT 64
90 #define STR_TERMINATE_ASCII 128 /* only terminate if ascii */
91 #define STR_LEN_NOTERM 256 /* the length field is the unterminated length */
92
93 struct loadparm_context;
94 struct smb_iconv_handle;
95
96 size_t ucs2_align(const void *base_ptr, const void *p, int flags);
97
98 /**
99 return the number of bytes occupied by a buffer in CH_UTF16 format
100 the result includes the null termination
101 **/
102 size_t utf16_len(const void *buf);
103
104 /**
105 return the number of bytes occupied by a buffer in CH_UTF16 format
106 the result includes the null termination
107 limited by 'n' bytes
108 **/
109 size_t utf16_len_n(const void *src, size_t n);
110
111 char *strchr_m(const char *s, char c);
112 /**
113  * Calculate the number of units (8 or 16-bit, depending on the
114  * destination charset), that would be needed to convert the input
115  * string which is expected to be in in src_charset encoding to the
116  * destination charset (which should be a unicode charset).
117  */
118 size_t strlen_m_ext_handle(struct smb_iconv_handle *ic,
119                            const char *s, charset_t src_charset, charset_t dst_charset);
120 size_t strlen_m_ext(const char *s, charset_t src_charset, charset_t dst_charset);
121 size_t strlen_m_ext_term(const char *s, charset_t src_charset,
122                          charset_t dst_charset);
123 size_t strlen_m_ext_term_null(const char *s,
124                               charset_t src_charset,
125                               charset_t dst_charset);
126 size_t strlen_m(const char *s);
127 size_t strlen_m_term(const char *s);
128 size_t strlen_m_term_null(const char *s);
129 char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength);
130 char *talloc_alpha_strcpy(TALLOC_CTX *mem_ctx,
131                           const char *src,
132                           const char *other_safe_chars);
133 void string_replace_m(char *s, char oldc, char newc);
134 bool strcsequal(const char *s1,const char *s2);
135 bool strequal_m(const char *s1, const char *s2);
136 int strncasecmp_m(const char *s1, const char *s2, size_t n);
137 int strncasecmp_m_handle(struct smb_iconv_handle *iconv_handle,
138                          const char *s1, const char *s2, size_t n);
139 bool next_token(const char **ptr,char *buff, const char *sep, size_t bufsize);
140 int strcasecmp_m_handle(struct smb_iconv_handle *iconv_handle,
141                         const char *s1, const char *s2);
142 int strcasecmp_m(const char *s1, const char *s2);
143 size_t count_chars_m(const char *s, char c);
144 char *strupper_talloc(TALLOC_CTX *ctx, const char *src);
145 char *talloc_strdup_upper(TALLOC_CTX *ctx, const char *src);
146 char *strupper_talloc_n_handle(struct smb_iconv_handle *iconv_handle,
147                                 TALLOC_CTX *ctx, const char *src, size_t n);
148 char *strupper_talloc_n(TALLOC_CTX *ctx, const char *src, size_t n);
149  char *strlower_talloc_handle(struct smb_iconv_handle *iconv_handle,
150                               TALLOC_CTX *ctx, const char *src);
151 char *strlower_talloc(TALLOC_CTX *ctx, const char *src);
152 bool strhasupper(const char *string);
153 bool strhaslower_handle(struct smb_iconv_handle *ic,
154                         const char *string);
155 bool strhaslower(const char *string);
156 bool strhasupper_handle(struct smb_iconv_handle *ic,
157                         const char *string);
158 char *strrchr_m(const char *s, char c);
159 char *strchr_m(const char *s, char c);
160 char *strstr_m(const char *src, const char *findstr);
161
162 bool push_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src, size_t *converted_size);
163 bool push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src, size_t *converted_size);
164 bool push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src, size_t *converted_size);
165 bool pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src, size_t *converted_size);
166 bool pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const smb_ucs2_t *src, size_t *converted_size);
167 bool pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src, size_t *converted_size);
168 ssize_t push_string(void *dest, const char *src, size_t dest_len, int flags);
169 ssize_t pull_string(char *dest, const void *src, size_t dest_len, size_t src_len, int flags);
170
171 bool convert_string_talloc(TALLOC_CTX *ctx, 
172                            charset_t from, charset_t to, 
173                            void const *src, size_t srclen, 
174                            void *dest, size_t *converted_size);
175
176 bool convert_string(charset_t from, charset_t to,
177                       void const *src, size_t srclen, 
178                       void *dest, size_t destlen,
179                       size_t *converted_size);
180 bool convert_string_error(charset_t from, charset_t to,
181                           void const *src, size_t srclen,
182                           void *dest, size_t destlen,
183                           size_t *converted_size);
184
185 struct smb_iconv_handle *get_iconv_handle(void);
186 struct smb_iconv_handle *get_iconv_testing_handle(TALLOC_CTX *mem_ctx, 
187                                                   const char *dos_charset, 
188                                                   const char *unix_charset,
189                                                   bool use_builtin_handlers);
190 struct smb_iconv_handle *reinit_iconv_handle(TALLOC_CTX *mem_ctx,
191                                 const char *dos_charset,
192                                 const char *unix_charset);
193 void free_iconv_handle(void);
194
195 smb_iconv_t get_conv_handle(struct smb_iconv_handle *ic,
196                             charset_t from, charset_t to);
197 const char *charset_name(struct smb_iconv_handle *ic, charset_t ch);
198
199 codepoint_t next_codepoint_ext(const char *str, size_t len,
200                                charset_t src_charset, size_t *size);
201 codepoint_t next_codepoint(const char *str, size_t *size);
202 ssize_t push_codepoint(char *str, codepoint_t c);
203
204 /* codepoints */
205 codepoint_t next_codepoint_handle_ext(struct smb_iconv_handle *ic,
206                                       const char *str, size_t len,
207                                       charset_t src_charset,
208                                       size_t *size);
209 codepoint_t next_codepoint_handle(struct smb_iconv_handle *ic,
210                             const char *str, size_t *size);
211 ssize_t push_codepoint_handle(struct smb_iconv_handle *ic,
212                                 char *str, codepoint_t c);
213
214 codepoint_t toupper_m(codepoint_t val);
215 codepoint_t tolower_m(codepoint_t val);
216 bool islower_m(codepoint_t val);
217 bool isupper_m(codepoint_t val);
218 int codepoint_cmpi(codepoint_t c1, codepoint_t c2);
219
220 /* Iconv convenience functions */
221 struct smb_iconv_handle *smb_iconv_handle_reinit(TALLOC_CTX *mem_ctx,
222                                                            const char *dos_charset,
223                                                            const char *unix_charset,
224                                                            bool use_builtin_handlers,
225                                                            struct smb_iconv_handle *old_ic);
226
227 bool convert_string_handle(struct smb_iconv_handle *ic,
228                                 charset_t from, charset_t to,
229                                 void const *src, size_t srclen, 
230                                 void *dest, size_t destlen, size_t *converted_size);
231 bool convert_string_error_handle(struct smb_iconv_handle *ic,
232                                  charset_t from, charset_t to,
233                                  void const *src, size_t srclen,
234                                  void *dest, size_t destlen,
235                                  size_t *converted_size);
236
237 bool convert_string_talloc_handle(TALLOC_CTX *ctx,
238                                        struct smb_iconv_handle *ic,
239                                        charset_t from, charset_t to, 
240                                        void const *src, size_t srclen, 
241                                        void *dest, size_t *converted_size);
242 /* iconv */
243 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode);
244 int smb_iconv_close(smb_iconv_t cd);
245 size_t smb_iconv(smb_iconv_t cd, 
246                  const char **inbuf, size_t *inbytesleft,
247                  char **outbuf, size_t *outbytesleft);
248 smb_iconv_t smb_iconv_open_ex(TALLOC_CTX *mem_ctx, const char *tocode, 
249                               const char *fromcode, bool use_builtin_handlers);
250
251 void smb_init_locale(void);
252
253 /* The following definitions come from util_unistr_w.c  */
254
255 size_t strlen_w(const smb_ucs2_t *src);
256 size_t strnlen_w(const smb_ucs2_t *src, size_t max);
257 smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c);
258 smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c);
259 smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c);
260 smb_ucs2_t *strnrchr_w(const smb_ucs2_t *s, smb_ucs2_t c, unsigned int n);
261 smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins);
262 bool strlower_w(smb_ucs2_t *s);
263 bool strupper_w(smb_ucs2_t *s);
264 int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b);
265 int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len);
266 int strcmp_wa(const smb_ucs2_t *a, const char *b);
267 smb_ucs2_t toupper_w(smb_ucs2_t v);
268
269 #endif /* __CHARSET_H__ */