Make sure prototypes are always included, make some functions static and
[kai/samba.git] / source4 / lib / 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} charset_t;
32
33 #define NUM_CHARSETS 5
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 extern struct smb_iconv_convenience *global_smb_iconv_convenience;
80
81 /* replace some string functions with multi-byte
82    versions */
83 #define strlower(s) strlower_m(s)
84 #define strupper(s) strupper_m(s)
85
86 char *strchr_m(const char *s, char c);
87 size_t strlen_m_term(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_w(char *s, char oldc, char newc);
91 bool strcsequal_w(const char *s1,const char *s2);
92 bool strequal_w(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_w(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 /* codepoints */
109 codepoint_t next_codepoint(struct smb_iconv_convenience *ic, 
110                             const char *str, size_t *size);
111 ssize_t push_codepoint(struct smb_iconv_convenience *ic, 
112                                 char *str, codepoint_t c);
113 codepoint_t toupper_w(codepoint_t val);
114 codepoint_t tolower_w(codepoint_t val);
115 int codepoint_cmpi(codepoint_t c1, codepoint_t c2);
116 ssize_t push_string(struct smb_iconv_convenience *ic, void *dest, const char *src, size_t dest_len, int flags);
117 ssize_t pull_string(struct smb_iconv_convenience *ic,
118                     char *dest, const void *src, size_t dest_len, size_t src_len, int flags);
119 ssize_t convert_string(struct smb_iconv_convenience *ic,
120                                 charset_t from, charset_t to,
121                                 void const *src, size_t srclen, 
122                                 void *dest, size_t destlen);
123 ssize_t convert_string_talloc_descriptor(TALLOC_CTX *ctx, smb_iconv_t descriptor, void const *src, size_t srclen, void **dest);
124 ssize_t convert_string_talloc(TALLOC_CTX *ctx, 
125                                        struct smb_iconv_convenience *ic, 
126                                        charset_t from, charset_t to, 
127                                        void const *src, size_t srclen, 
128                                        void **dest);
129 ssize_t push_ascii_talloc(TALLOC_CTX *ctx, struct smb_iconv_convenience *ic, char **dest, const char *src);
130 ssize_t push_ucs2_talloc(TALLOC_CTX *ctx, struct smb_iconv_convenience *ic, void **dest, const char *src);
131 ssize_t push_utf8_talloc(TALLOC_CTX *ctx, struct smb_iconv_convenience *ic, char **dest, const char *src);
132 ssize_t pull_ascii_talloc(TALLOC_CTX *ctx, struct smb_iconv_convenience *ic, char **dest, const char *src);
133 ssize_t pull_ucs2_talloc(TALLOC_CTX *ctx, struct smb_iconv_convenience *ic, char **dest, const void *src);
134 ssize_t pull_utf8_talloc(TALLOC_CTX *ctx, struct smb_iconv_convenience *ic, char **dest, const char *src);
135
136 /* iconv */
137 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode);
138 int smb_iconv_close(smb_iconv_t cd);
139 size_t smb_iconv(smb_iconv_t cd, 
140                  const char **inbuf, size_t *inbytesleft,
141                  char **outbuf, size_t *outbytesleft);
142 smb_iconv_t smb_iconv_open_ex(TALLOC_CTX *mem_ctx, const char *tocode, 
143                               const char *fromcode, bool native_iconv);
144
145 /* iconv convenience */
146 struct smb_iconv_convenience *smb_iconv_convenience_init(TALLOC_CTX *mem_ctx,
147                                                          const char *dos_charset,
148                                                          const char *unix_charset,
149                                                          bool native_iconv);
150
151 void load_case_tables(void);
152 bool charset_register_backend(const void *_funcs);
153
154 #endif /* __CHARSET_H__ */