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