lib/util/charset add functions isupper_m and islower_m
[Samba.git] / lib / util / charset / charset.h
blob901885d8462a670cb2d76b288df2135bd3e00a4d
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_ext_term(const char *s, charset_t src_charset,
125 charset_t dst_charset);
126 size_t strlen_m_term(const char *s);
127 size_t strlen_m_term_null(const char *s);
128 size_t strlen_m(const char *s);
129 char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength);
130 void string_replace_m(char *s, char oldc, char newc);
131 bool strcsequal_m(const char *s1,const char *s2);
132 bool strequal_m(const char *s1, const char *s2);
133 int strncasecmp_m(const char *s1, const char *s2, size_t n);
134 bool next_token(const char **ptr,char *buff, const char *sep, size_t bufsize);
135 int strcasecmp_m(const char *s1, const char *s2);
136 size_t count_chars_m(const char *s, char c);
137 void strupper_m(char *s);
138 void strlower_m(char *s);
139 char *strupper_talloc(TALLOC_CTX *ctx, const char *src);
140 char *talloc_strdup_upper(TALLOC_CTX *ctx, const char *src);
141 char *strupper_talloc_n(TALLOC_CTX *ctx, const char *src, size_t n);
142 char *strlower_talloc(TALLOC_CTX *ctx, const char *src);
143 bool strhasupper(const char *string);
144 bool strhaslower(const char *string);
145 char *strrchr_m(const char *s, char c);
146 char *strchr_m(const char *s, char c);
148 bool push_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src, size_t *converted_size);
149 bool push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src, size_t *converted_size);
150 bool push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src, size_t *converted_size);
151 bool pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src, size_t *converted_size);
152 bool pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const smb_ucs2_t *src, size_t *converted_size);
153 bool pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src, size_t *converted_size);
154 ssize_t push_string(void *dest, const char *src, size_t dest_len, int flags);
155 ssize_t pull_string(char *dest, const void *src, size_t dest_len, size_t src_len, int flags);
157 bool convert_string_talloc(TALLOC_CTX *ctx,
158 charset_t from, charset_t to,
159 void const *src, size_t srclen,
160 void *dest, size_t *converted_size,
161 bool allow_badcharcnv);
163 size_t convert_string(charset_t from, charset_t to,
164 void const *src, size_t srclen,
165 void *dest, size_t destlen, bool allow_badcharcnv);
167 ssize_t iconv_talloc(TALLOC_CTX *mem_ctx,
168 smb_iconv_t cd,
169 void const *src, size_t srclen,
170 void *dest);
172 extern struct smb_iconv_convenience *global_iconv_convenience;
174 codepoint_t next_codepoint_ext(const char *str, charset_t src_charset,
175 size_t *size);
176 codepoint_t next_codepoint(const char *str, size_t *size);
177 ssize_t push_codepoint(char *str, codepoint_t c);
179 /* codepoints */
180 codepoint_t next_codepoint_convenience_ext(struct smb_iconv_convenience *ic,
181 const char *str, charset_t src_charset,
182 size_t *size);
183 codepoint_t next_codepoint_convenience(struct smb_iconv_convenience *ic,
184 const char *str, size_t *size);
185 ssize_t push_codepoint_convenience(struct smb_iconv_convenience *ic,
186 char *str, codepoint_t c);
188 codepoint_t toupper_m(codepoint_t val);
189 codepoint_t tolower_m(codepoint_t val);
190 bool islower_m(codepoint_t val);
191 bool isupper_m(codepoint_t val);
192 int codepoint_cmpi(codepoint_t c1, codepoint_t c2);
194 /* Iconv convenience functions */
195 struct smb_iconv_convenience *smb_iconv_convenience_reinit(TALLOC_CTX *mem_ctx,
196 const char *dos_charset,
197 const char *unix_charset,
198 bool native_iconv,
199 struct smb_iconv_convenience *old_ic);
201 bool convert_string_convenience(struct smb_iconv_convenience *ic,
202 charset_t from, charset_t to,
203 void const *src, size_t srclen,
204 void *dest, size_t destlen, size_t *converted_size,
205 bool allow_badcharcnv);
206 bool convert_string_talloc_convenience(TALLOC_CTX *ctx,
207 struct smb_iconv_convenience *ic,
208 charset_t from, charset_t to,
209 void const *src, size_t srclen,
210 void *dest, size_t *converted_size, bool allow_badcharcnv);
211 /* iconv */
212 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode);
213 int smb_iconv_close(smb_iconv_t cd);
214 size_t smb_iconv(smb_iconv_t cd,
215 const char **inbuf, size_t *inbytesleft,
216 char **outbuf, size_t *outbytesleft);
217 smb_iconv_t smb_iconv_open_ex(TALLOC_CTX *mem_ctx, const char *tocode,
218 const char *fromcode, bool native_iconv);
220 void load_case_tables(void);
221 bool charset_register_backend(const void *_funcs);
224 * Define stub for charset module which implements 8-bit encoding with gaps.
225 * Encoding tables for such module should be produced from glibc's CHARMAPs
226 * using script source/script/gen-8bit-gap.sh
227 * CHARSETNAME is CAPITALIZED charset name
229 * */
230 #define SMB_GENERATE_CHARSET_MODULE_8_BIT_GAP(CHARSETNAME) \
231 static size_t CHARSETNAME ## _push(void *cd, const char **inbuf, size_t *inbytesleft, \
232 char **outbuf, size_t *outbytesleft) \
234 while (*inbytesleft >= 2 && *outbytesleft >= 1) { \
235 int i; \
236 int done = 0; \
238 uint16 ch = SVAL(*inbuf,0); \
240 for (i=0; from_idx[i].start != 0xffff; i++) { \
241 if ((from_idx[i].start <= ch) && (from_idx[i].end >= ch)) { \
242 ((unsigned char*)(*outbuf))[0] = from_ucs2[from_idx[i].idx+ch]; \
243 (*inbytesleft) -= 2; \
244 (*outbytesleft) -= 1; \
245 (*inbuf) += 2; \
246 (*outbuf) += 1; \
247 done = 1; \
248 break; \
251 if (!done) { \
252 errno = EINVAL; \
253 return -1; \
258 if (*inbytesleft == 1) { \
259 errno = EINVAL; \
260 return -1; \
263 if (*inbytesleft > 1) { \
264 errno = E2BIG; \
265 return -1; \
268 return 0; \
271 static size_t CHARSETNAME ## _pull(void *cd, const char **inbuf, size_t *inbytesleft, \
272 char **outbuf, size_t *outbytesleft) \
274 while (*inbytesleft >= 1 && *outbytesleft >= 2) { \
275 SSVAL(*outbuf, 0, to_ucs2[((unsigned char*)(*inbuf))[0]]); \
276 (*inbytesleft) -= 1; \
277 (*outbytesleft) -= 2; \
278 (*inbuf) += 1; \
279 (*outbuf) += 2; \
282 if (*inbytesleft > 0) { \
283 errno = E2BIG; \
284 return -1; \
287 return 0; \
290 struct charset_functions CHARSETNAME ## _functions = \
291 {#CHARSETNAME, CHARSETNAME ## _pull, CHARSETNAME ## _push}; \
293 NTSTATUS charset_ ## CHARSETNAME ## _init(void); \
294 NTSTATUS charset_ ## CHARSETNAME ## _init(void) \
296 return smb_register_charset(& CHARSETNAME ## _functions); \
300 #endif /* __CHARSET_H__ */