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