xref: /freebsd/sys/sys/iconv.h (revision aa0a1e58)
1 /*-
2  * Copyright (c) 2000-2001 Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 #ifndef _SYS_ICONV_H_
29 #define _SYS_ICONV_H_
30 
31 #define	ICONV_CSNMAXLEN		31	/* maximum length of charset name */
32 #define	ICONV_CNVNMAXLEN	31	/* maximum length of converter name */
33 /* maximum size of data associated with cs pair */
34 #define	ICONV_CSMAXDATALEN	(sizeof(caddr_t) * 0x200 + sizeof(uint32_t) * 0x200 * 0x80)
35 
36 #define	XLAT16_ACCEPT_NULL_OUT		0x01000000
37 #define	XLAT16_ACCEPT_NULL_IN		0x02000000
38 #define	XLAT16_HAS_LOWER_CASE		0x04000000
39 #define	XLAT16_HAS_UPPER_CASE		0x08000000
40 #define	XLAT16_HAS_FROM_LOWER_CASE	0x10000000
41 #define	XLAT16_HAS_FROM_UPPER_CASE	0x20000000
42 #define	XLAT16_IS_3BYTE_CHR		0x40000000
43 
44 #define	KICONV_LOWER		1	/* tolower converted character */
45 #define	KICONV_UPPER		2	/* toupper converted character */
46 #define	KICONV_FROM_LOWER	4	/* tolower source character, then convert */
47 #define	KICONV_FROM_UPPER	8	/* toupper source character, then convert */
48 #define	KICONV_WCTYPE		16	/* towlower/towupper characters */
49 
50 #define	KICONV_WCTYPE_NAME	"_wctype"
51 
52 /*
53  * Entry for cslist sysctl
54  */
55 #define	ICONV_CSPAIR_INFO_VER	1
56 
57 struct iconv_cspair_info {
58 	int	cs_version;
59 	int	cs_id;
60 	int	cs_base;
61 	int	cs_refcount;
62 	char	cs_to[ICONV_CSNMAXLEN];
63 	char	cs_from[ICONV_CSNMAXLEN];
64 };
65 
66 /*
67  * Paramters for 'add' sysctl
68  */
69 #define	ICONV_ADD_VER	1
70 
71 struct iconv_add_in {
72 	int	ia_version;
73 	char	ia_converter[ICONV_CNVNMAXLEN];
74 	char	ia_to[ICONV_CSNMAXLEN];
75 	char	ia_from[ICONV_CSNMAXLEN];
76 	int	ia_datalen;
77 	const void *ia_data;
78 };
79 
80 struct iconv_add_out {
81 	int	ia_csid;
82 };
83 
84 #ifndef _KERNEL
85 
86 __BEGIN_DECLS
87 
88 #define	ENCODING_UNICODE	"UTF-16BE"
89 #define	KICONV_VENDOR_MICSFT	1	/* Microsoft Vendor Code for quirk */
90 
91 int   kiconv_add_xlat_table(const char *, const char *, const u_char *);
92 int   kiconv_add_xlat16_cspair(const char *, const char *, int);
93 int   kiconv_add_xlat16_cspairs(const char *, const char *);
94 int   kiconv_add_xlat16_table(const char *, const char *, const void *, int);
95 int   kiconv_lookupconv(const char *drvname);
96 int   kiconv_lookupcs(const char *tocode, const char *fromcode);
97 const char *kiconv_quirkcs(const char *, int);
98 
99 __END_DECLS
100 
101 #else /* !_KERNEL */
102 
103 #include <sys/kobj.h>
104 #include <sys/module.h>			/* can't avoid that */
105 #include <sys/queue.h>			/* can't avoid that */
106 #include <sys/sysctl.h>			/* can't avoid that */
107 
108 struct iconv_cspair;
109 struct iconv_cspairdata;
110 
111 /*
112  * iconv converter class definition
113  */
114 struct iconv_converter_class {
115 	KOBJ_CLASS_FIELDS;
116 	TAILQ_ENTRY(iconv_converter_class)	cc_link;
117 };
118 
119 struct iconv_cspair {
120 	int		cp_id;		/* unique id of charset pair */
121 	int		cp_refcount;	/* number of references from other pairs */
122 	const char *	cp_from;
123 	const char *	cp_to;
124 	void *		cp_data;
125 	struct iconv_converter_class * cp_dcp;
126 	struct iconv_cspair *cp_base;
127 	TAILQ_ENTRY(iconv_cspair)	cp_link;
128 };
129 
130 #define	KICONV_CONVERTER(name,size)			\
131     static struct iconv_converter_class iconv_ ## name ## _class = { \
132 	"iconv_"#name, iconv_ ## name ## _methods, size, NULL \
133     };							\
134     static moduledata_t iconv_ ## name ## _mod = {	\
135 	"iconv_"#name, iconv_converter_handler,		\
136 	(void*)&iconv_ ## name ## _class		\
137     };							\
138     DECLARE_MODULE(iconv_ ## name, iconv_ ## name ## _mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
139 
140 #define	KICONV_CES(name,size)				\
141     static DEFINE_CLASS(iconv_ces_ ## name, iconv_ces_ ## name ## _methods, (size)); \
142     static moduledata_t iconv_ces_ ## name ## _mod = {	\
143 	"iconv_ces_"#name, iconv_cesmod_handler,	\
144 	(void*)&iconv_ces_ ## name ## _class		\
145     };							\
146     DECLARE_MODULE(iconv_ces_ ## name, iconv_ces_ ## name ## _mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
147 
148 #ifdef MALLOC_DECLARE
149 MALLOC_DECLARE(M_ICONV);
150 #endif
151 
152 /*
153  * Basic conversion functions
154  */
155 int iconv_open(const char *to, const char *from, void **handle);
156 int iconv_close(void *handle);
157 int iconv_conv(void *handle, const char **inbuf,
158 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
159 int iconv_conv_case(void *handle, const char **inbuf,
160 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype);
161 int iconv_convchr(void *handle, const char **inbuf,
162 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
163 int iconv_convchr_case(void *handle, const char **inbuf,
164 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype);
165 char* iconv_convstr(void *handle, char *dst, const char *src);
166 void* iconv_convmem(void *handle, void *dst, const void *src, int size);
167 int iconv_vfs_refcount(const char *fsname);
168 
169 int towlower(int c, void *handle);
170 int towupper(int c, void *handle);
171 
172 /*
173  * Bridge struct of iconv functions
174  */
175 struct iconv_functions {
176 	int (*open)(const char *to, const char *from, void **handle);
177 	int (*close)(void *handle);
178 	int (*conv)(void *handle, const char **inbuf, size_t *inbytesleft,
179 		char **outbuf, size_t *outbytesleft);
180 	int (*conv_case)(void *handle, const char **inbuf, size_t *inbytesleft,
181 		char **outbuf, size_t *outbytesleft, int casetype);
182 	int (*convchr)(void *handle, const char **inbuf, size_t *inbytesleft,
183 		char **outbuf, size_t *outbytesleft);
184 	int (*convchr_case)(void *handle, const char **inbuf, size_t *inbytesleft,
185 		char **outbuf, size_t *outbytesleft, int casetype);
186 };
187 
188 #define VFS_DECLARE_ICONV(fsname)					\
189 	static struct iconv_functions fsname ## _iconv_core = {		\
190 		iconv_open,						\
191 		iconv_close,						\
192 		iconv_conv,						\
193 		iconv_conv_case,					\
194 		iconv_convchr,						\
195 		iconv_convchr_case					\
196 	};								\
197 	extern struct iconv_functions *fsname ## _iconv;		\
198 	static int fsname ## _iconv_mod_handler(module_t mod,		\
199 		int type, void *d);					\
200 	static int							\
201 	fsname ## _iconv_mod_handler(module_t mod, int type, void *d)	\
202 	{								\
203 		int error = 0;						\
204 		switch(type) {						\
205 		case MOD_LOAD:						\
206 			fsname ## _iconv = & fsname ## _iconv_core;	\
207 			break;						\
208 		case MOD_UNLOAD:					\
209 			error = iconv_vfs_refcount(#fsname);		\
210 			if (error)					\
211 				return (EBUSY);				\
212 			fsname ## _iconv = NULL;			\
213 			break;						\
214 		default:						\
215 			error = EINVAL;					\
216 			break;						\
217 		}							\
218 		return (error);						\
219 	}								\
220 	static moduledata_t fsname ## _iconv_mod = {			\
221 		#fsname"_iconv",					\
222 		fsname ## _iconv_mod_handler,				\
223 		NULL							\
224 	};								\
225 	DECLARE_MODULE(fsname ## _iconv, fsname ## _iconv_mod,		\
226 		       SI_SUB_DRIVERS, SI_ORDER_ANY);			\
227 	MODULE_DEPEND(fsname ## _iconv, fsname, 1, 1, 1);		\
228 	MODULE_DEPEND(fsname ## _iconv, libiconv, 2, 2, 2);		\
229 	MODULE_VERSION(fsname ## _iconv, 1)
230 
231 /*
232  * Internal functions
233  */
234 int iconv_lookupcp(char **cpp, const char *s);
235 
236 int iconv_converter_initstub(struct iconv_converter_class *dp);
237 int iconv_converter_donestub(struct iconv_converter_class *dp);
238 int iconv_converter_tolowerstub(int c, void *handle);
239 int iconv_converter_handler(module_t mod, int type, void *data);
240 
241 #ifdef ICONV_DEBUG
242 #define ICDEBUG(format, ...) printf("%s: "format, __func__ , __VA_ARGS__)
243 #else
244 #define ICDEBUG(format, ...)
245 #endif
246 
247 #endif /* !_KERNEL */
248 
249 #endif /* !_SYS_ICONV_H_ */
250