Add allow_badcharcnv argument to all conversion function, for
[kai/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_UTF16=0, CH_UNIX, CH_DOS, CH_UTF8, CH_UTF16BE, CH_UTF16MUNGED} charset_t;
32
33 #define NUM_CHARSETS 6
34
35 /*
36  *   for each charset we have a function that pulls from that charset to
37  *     a ucs2 buffer, and a function that pushes to a ucs2 buffer
38  *     */
39
40 struct charset_functions {
41         const char *name;
42         size_t (*pull)(void *, const char **inbuf, size_t *inbytesleft,
43                                    char **outbuf, size_t *outbytesleft);
44         size_t (*push)(void *, const char **inbuf, size_t *inbytesleft,
45                                    char **outbuf, size_t *outbytesleft);
46         struct charset_functions *prev, *next;
47 };
48
49 /* this type is used for manipulating unicode codepoints */
50 typedef uint32_t codepoint_t;
51
52 #define INVALID_CODEPOINT ((codepoint_t)-1)
53
54
55 /* generic iconv conversion structure */
56 typedef struct smb_iconv_s {
57         size_t (*direct)(void *cd, const char **inbuf, size_t *inbytesleft,
58                          char **outbuf, size_t *outbytesleft);
59         size_t (*pull)(void *cd, const char **inbuf, size_t *inbytesleft,
60                        char **outbuf, size_t *outbytesleft);
61         size_t (*push)(void *cd, const char **inbuf, size_t *inbytesleft,
62                        char **outbuf, size_t *outbytesleft);
63         void *cd_direct, *cd_pull, *cd_push;
64 } *smb_iconv_t;
65
66 /* string manipulation flags */
67 #define STR_TERMINATE 1
68 #define STR_UPPER 2
69 #define STR_ASCII 4
70 #define STR_UNICODE 8
71 #define STR_NOALIGN 16
72 #define STR_NO_RANGE_CHECK 32
73 #define STR_LEN8BIT 64
74 #define STR_TERMINATE_ASCII 128 /* only terminate if ascii */
75 #define STR_LEN_NOTERM 256 /* the length field is the unterminated length */
76
77 struct loadparm_context;
78 struct smb_iconv_convenience;
79
80 /* replace some string functions with multi-byte
81    versions */
82 #define strlower(s) strlower_m(s)
83 #define strupper(s) strupper_m(s)
84
85 char *strchr_m(const char *s, char c);
86 size_t strlen_m_term(const char *s);
87 size_t strlen_m_term_null(const char *s);
88 size_t strlen_m(const char *s);
89 char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength);
90 void string_replace_m(char *s, char oldc, char newc);
91 bool strcsequal_m(const char *s1,const char *s2);
92 bool strequal_m(const char *s1, const char *s2);
93 int strncasecmp_m(const char *s1, const char *s2, size_t n);
94 bool next_token(const char **ptr,char *buff, const char *sep, size_t bufsize);
95 int strcasecmp_m(const char *s1, const char *s2);
96 size_t count_chars_m(const char *s, char c);
97 void strupper_m(char *s);
98 void strlower_m(char *s);
99 char *strupper_talloc(TALLOC_CTX *ctx, const char *src);
100 char *talloc_strdup_upper(TALLOC_CTX *ctx, const char *src);
101 char *strupper_talloc_n(TALLOC_CTX *ctx, const char *src, size_t n);
102 char *strlower_talloc(TALLOC_CTX *ctx, const char *src);
103 bool strhasupper(const char *string);
104 bool strhaslower(const char *string);
105 char *strrchr_m(const char *s, char c);
106 char *strchr_m(const char *s, char c);
107
108 ssize_t push_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src);
109 ssize_t push_ucs2_talloc(TALLOC_CTX *ctx, void **dest, const char *src);
110 ssize_t push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src);
111 ssize_t pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src);
112 ssize_t pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const void *src);
113 ssize_t pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src);
114 ssize_t push_string(void *dest, const char *src, size_t dest_len, int flags);
115 ssize_t pull_string(char *dest, const void *src, size_t dest_len, size_t src_len, int flags);
116
117 ssize_t convert_string_talloc(TALLOC_CTX *ctx, 
118                                        charset_t from, charset_t to, 
119                                        void const *src, size_t srclen, 
120                                        void **dest, bool allow_badcharcnv);
121
122 ssize_t convert_string(charset_t from, charset_t to,
123                                 void const *src, size_t srclen, 
124                                 void *dest, size_t destlen, bool allow_badcharcnv);
125
126 ssize_t iconv_talloc(TALLOC_CTX *mem_ctx, 
127                                        smb_iconv_t cd,
128                                        void const *src, size_t srclen, 
129                                        void **dest);
130
131 extern struct smb_iconv_convenience *global_iconv_convenience;
132
133 codepoint_t next_codepoint(const char *str, size_t *size);
134
135 /* codepoints */
136 codepoint_t next_codepoint_convenience(struct smb_iconv_convenience *ic, 
137                             const char *str, size_t *size);
138 ssize_t push_codepoint(struct smb_iconv_convenience *ic, 
139                                 char *str, codepoint_t c);
140 codepoint_t toupper_m(codepoint_t val);
141 codepoint_t tolower_m(codepoint_t val);
142 int codepoint_cmpi(codepoint_t c1, codepoint_t c2);
143
144 /* Iconv convenience functions */
145 struct smb_iconv_convenience *smb_iconv_convenience_init(TALLOC_CTX *mem_ctx,
146                                                          const char *dos_charset,
147                                                          const char *unix_charset,
148                                                          bool native_iconv);
149
150 ssize_t convert_string_convenience(struct smb_iconv_convenience *ic,
151                                 charset_t from, charset_t to,
152                                 void const *src, size_t srclen, 
153                                 void *dest, size_t destlen, bool allow_badcharcnv);
154 ssize_t convert_string_talloc_convenience(TALLOC_CTX *ctx, 
155                                        struct smb_iconv_convenience *ic, 
156                                        charset_t from, charset_t to, 
157                                        void const *src, size_t srclen, 
158                                        void **dest, bool allow_badcharcnv);
159 /* iconv */
160 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode);
161 int smb_iconv_close(smb_iconv_t cd);
162 size_t smb_iconv(smb_iconv_t cd, 
163                  const char **inbuf, size_t *inbytesleft,
164                  char **outbuf, size_t *outbytesleft);
165 smb_iconv_t smb_iconv_open_ex(TALLOC_CTX *mem_ctx, const char *tocode, 
166                               const char *fromcode, bool native_iconv);
167
168 void load_case_tables(void);
169 bool charset_register_backend(const void *_funcs);
170
171 #endif /* __CHARSET_H__ */