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