1139825Simp/*-
2206361Sjoel * Copyright (c) 2000-2001 Boris Popov
375332Sbp * All rights reserved.
475332Sbp *
575332Sbp * Redistribution and use in source and binary forms, with or without
675332Sbp * modification, are permitted provided that the following conditions
775332Sbp * are met:
875332Sbp * 1. Redistributions of source code must retain the above copyright
975332Sbp *    notice, this list of conditions and the following disclaimer.
1075332Sbp * 2. Redistributions in binary form must reproduce the above copyright
1175332Sbp *    notice, this list of conditions and the following disclaimer in the
1275332Sbp *    documentation and/or other materials provided with the distribution.
1375332Sbp *
1475332Sbp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1575332Sbp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1675332Sbp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1775332Sbp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1875332Sbp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1975332Sbp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2075332Sbp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2175332Sbp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2275332Sbp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2375332Sbp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2475332Sbp * SUCH DAMAGE.
2575332Sbp *
2675332Sbp * $FreeBSD$
2775332Sbp */
2875332Sbp#ifndef _SYS_ICONV_H_
2975332Sbp#define _SYS_ICONV_H_
3075332Sbp
3175332Sbp#define	ICONV_CSNMAXLEN		31	/* maximum length of charset name */
3275332Sbp#define	ICONV_CNVNMAXLEN	31	/* maximum length of converter name */
33146573Simura/* maximum size of data associated with cs pair */
34146573Simura#define	ICONV_CSMAXDATALEN	(sizeof(caddr_t) * 0x200 + sizeof(uint32_t) * 0x200 * 0x80)
3575332Sbp
36120492Sfjoe#define	XLAT16_ACCEPT_NULL_OUT		0x01000000
37120492Sfjoe#define	XLAT16_ACCEPT_NULL_IN		0x02000000
38120492Sfjoe#define	XLAT16_HAS_LOWER_CASE		0x04000000
39120492Sfjoe#define	XLAT16_HAS_UPPER_CASE		0x08000000
40120492Sfjoe#define	XLAT16_HAS_FROM_LOWER_CASE	0x10000000
41120492Sfjoe#define	XLAT16_HAS_FROM_UPPER_CASE	0x20000000
42120492Sfjoe#define	XLAT16_IS_3BYTE_CHR		0x40000000
43120492Sfjoe
44120492Sfjoe#define	KICONV_LOWER		1	/* tolower converted character */
45120492Sfjoe#define	KICONV_UPPER		2	/* toupper converted character */
46120492Sfjoe#define	KICONV_FROM_LOWER	4	/* tolower source character, then convert */
47120492Sfjoe#define	KICONV_FROM_UPPER	8	/* toupper source character, then convert */
48194638Sdelphij#define	KICONV_WCTYPE		16	/* towlower/towupper characters */
49120492Sfjoe
50227650Skevlo#define	ENCODING_UNICODE	"UTF-16BE"
51194638Sdelphij#define	KICONV_WCTYPE_NAME	"_wctype"
52194638Sdelphij
5375332Sbp/*
5475332Sbp * Entry for cslist sysctl
5575332Sbp */
5675332Sbp#define	ICONV_CSPAIR_INFO_VER	1
5775332Sbp
5875332Sbpstruct iconv_cspair_info {
5975332Sbp	int	cs_version;
6075332Sbp	int	cs_id;
6175332Sbp	int	cs_base;
6275332Sbp	int	cs_refcount;
6375332Sbp	char	cs_to[ICONV_CSNMAXLEN];
6475332Sbp	char	cs_from[ICONV_CSNMAXLEN];
6575332Sbp};
6675332Sbp
6775332Sbp/*
68298981Spfg * Parameters for 'add' sysctl
6975332Sbp */
7075332Sbp#define	ICONV_ADD_VER	1
7175332Sbp
7275332Sbpstruct iconv_add_in {
7375332Sbp	int	ia_version;
7475332Sbp	char	ia_converter[ICONV_CNVNMAXLEN];
7575332Sbp	char	ia_to[ICONV_CSNMAXLEN];
7675332Sbp	char	ia_from[ICONV_CSNMAXLEN];
7775332Sbp	int	ia_datalen;
7875332Sbp	const void *ia_data;
7975332Sbp};
8075332Sbp
8175332Sbpstruct iconv_add_out {
8275332Sbp	int	ia_csid;
8375332Sbp};
8475332Sbp
8575332Sbp#ifndef _KERNEL
8675332Sbp
8775332Sbp__BEGIN_DECLS
8875332Sbp
89120492Sfjoe#define	KICONV_VENDOR_MICSFT	1	/* Microsoft Vendor Code for quirk */
90120492Sfjoe
9175332Sbpint   kiconv_add_xlat_table(const char *, const char *, const u_char *);
92120492Sfjoeint   kiconv_add_xlat16_cspair(const char *, const char *, int);
93123293Sfjoeint   kiconv_add_xlat16_cspairs(const char *, const char *);
94120492Sfjoeint   kiconv_add_xlat16_table(const char *, const char *, const void *, int);
95194638Sdelphijint   kiconv_lookupconv(const char *drvname);
96194638Sdelphijint   kiconv_lookupcs(const char *tocode, const char *fromcode);
97120492Sfjoeconst char *kiconv_quirkcs(const char *, int);
9875332Sbp
9975332Sbp__END_DECLS
10075332Sbp
10175332Sbp#else /* !_KERNEL */
10275332Sbp
10375332Sbp#include <sys/kobj.h>
104122098Sscottl#include <sys/module.h>			/* can't avoid that */
10575332Sbp#include <sys/queue.h>			/* can't avoid that */
10675332Sbp#include <sys/sysctl.h>			/* can't avoid that */
10775332Sbp
10875332Sbpstruct iconv_cspair;
10975332Sbpstruct iconv_cspairdata;
11075332Sbp
11175332Sbp/*
11275332Sbp * iconv converter class definition
11375332Sbp */
11475332Sbpstruct iconv_converter_class {
11575332Sbp	KOBJ_CLASS_FIELDS;
11675332Sbp	TAILQ_ENTRY(iconv_converter_class)	cc_link;
11775332Sbp};
11875332Sbp
11975332Sbpstruct iconv_cspair {
12075332Sbp	int		cp_id;		/* unique id of charset pair */
12175332Sbp	int		cp_refcount;	/* number of references from other pairs */
12275332Sbp	const char *	cp_from;
12375332Sbp	const char *	cp_to;
12475332Sbp	void *		cp_data;
12575332Sbp	struct iconv_converter_class * cp_dcp;
12675332Sbp	struct iconv_cspair *cp_base;
12775332Sbp	TAILQ_ENTRY(iconv_cspair)	cp_link;
12875332Sbp};
12975332Sbp
130194638Sdelphij#define	KICONV_CONVERTER(name,size)			\
131149799Simura    static struct iconv_converter_class iconv_ ## name ## _class = { \
132149799Simura	"iconv_"#name, iconv_ ## name ## _methods, size, NULL \
133149799Simura    };							\
13475332Sbp    static moduledata_t iconv_ ## name ## _mod = {	\
13575332Sbp	"iconv_"#name, iconv_converter_handler,		\
13675332Sbp	(void*)&iconv_ ## name ## _class		\
13775332Sbp    };							\
13875332Sbp    DECLARE_MODULE(iconv_ ## name, iconv_ ## name ## _mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
13975332Sbp
140194638Sdelphij#define	KICONV_CES(name,size)				\
14175332Sbp    static DEFINE_CLASS(iconv_ces_ ## name, iconv_ces_ ## name ## _methods, (size)); \
14275332Sbp    static moduledata_t iconv_ces_ ## name ## _mod = {	\
14375332Sbp	"iconv_ces_"#name, iconv_cesmod_handler,	\
14475332Sbp	(void*)&iconv_ces_ ## name ## _class		\
14575332Sbp    };							\
14675332Sbp    DECLARE_MODULE(iconv_ces_ ## name, iconv_ces_ ## name ## _mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
14775332Sbp
14875332Sbp#ifdef MALLOC_DECLARE
14975332SbpMALLOC_DECLARE(M_ICONV);
15075332Sbp#endif
15175332Sbp
15275332Sbp/*
15375332Sbp * Basic conversion functions
15475332Sbp */
15575332Sbpint iconv_open(const char *to, const char *from, void **handle);
15675332Sbpint iconv_close(void *handle);
15775332Sbpint iconv_conv(void *handle, const char **inbuf,
15875332Sbp	size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
159120492Sfjoeint iconv_conv_case(void *handle, const char **inbuf,
160120492Sfjoe	size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype);
161120492Sfjoeint iconv_convchr(void *handle, const char **inbuf,
162120492Sfjoe	size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
163120492Sfjoeint iconv_convchr_case(void *handle, const char **inbuf,
164120492Sfjoe	size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype);
165227650Skevloint iconv_add(const char *converter, const char *to, const char *from);
16675332Sbpchar* iconv_convstr(void *handle, char *dst, const char *src);
16775332Sbpvoid* iconv_convmem(void *handle, void *dst, const void *src, int size);
168120492Sfjoeint iconv_vfs_refcount(const char *fsname);
16975332Sbp
170194638Sdelphijint towlower(int c, void *handle);
171194638Sdelphijint towupper(int c, void *handle);
172194638Sdelphij
17375332Sbp/*
174120492Sfjoe * Bridge struct of iconv functions
175120492Sfjoe */
176120492Sfjoestruct iconv_functions {
177120492Sfjoe	int (*open)(const char *to, const char *from, void **handle);
178120492Sfjoe	int (*close)(void *handle);
179120492Sfjoe	int (*conv)(void *handle, const char **inbuf, size_t *inbytesleft,
180120492Sfjoe		char **outbuf, size_t *outbytesleft);
181120492Sfjoe	int (*conv_case)(void *handle, const char **inbuf, size_t *inbytesleft,
182120492Sfjoe		char **outbuf, size_t *outbytesleft, int casetype);
183120492Sfjoe	int (*convchr)(void *handle, const char **inbuf, size_t *inbytesleft,
184120492Sfjoe		char **outbuf, size_t *outbytesleft);
185120492Sfjoe	int (*convchr_case)(void *handle, const char **inbuf, size_t *inbytesleft,
186120492Sfjoe		char **outbuf, size_t *outbytesleft, int casetype);
187120492Sfjoe};
188120492Sfjoe
189120492Sfjoe#define VFS_DECLARE_ICONV(fsname)					\
190120492Sfjoe	static struct iconv_functions fsname ## _iconv_core = {		\
191120492Sfjoe		iconv_open,						\
192120492Sfjoe		iconv_close,						\
193120492Sfjoe		iconv_conv,						\
194120492Sfjoe		iconv_conv_case,					\
195120492Sfjoe		iconv_convchr,						\
196120492Sfjoe		iconv_convchr_case					\
197120492Sfjoe	};								\
198120492Sfjoe	extern struct iconv_functions *fsname ## _iconv;		\
199120492Sfjoe	static int fsname ## _iconv_mod_handler(module_t mod,		\
200120492Sfjoe		int type, void *d);					\
201120492Sfjoe	static int							\
202120492Sfjoe	fsname ## _iconv_mod_handler(module_t mod, int type, void *d)	\
203120492Sfjoe	{								\
204120492Sfjoe		int error = 0;						\
205120492Sfjoe		switch(type) {						\
206120492Sfjoe		case MOD_LOAD:						\
207120492Sfjoe			fsname ## _iconv = & fsname ## _iconv_core;	\
208120492Sfjoe			break;						\
209120492Sfjoe		case MOD_UNLOAD:					\
210120492Sfjoe			error = iconv_vfs_refcount(#fsname);		\
211120492Sfjoe			if (error)					\
212120492Sfjoe				return (EBUSY);				\
213120492Sfjoe			fsname ## _iconv = NULL;			\
214120492Sfjoe			break;						\
215120492Sfjoe		default:						\
216120492Sfjoe			error = EINVAL;					\
217120492Sfjoe			break;						\
218120492Sfjoe		}							\
219120492Sfjoe		return (error);						\
220120492Sfjoe	}								\
221120492Sfjoe	static moduledata_t fsname ## _iconv_mod = {			\
222120492Sfjoe		#fsname"_iconv",					\
223120492Sfjoe		fsname ## _iconv_mod_handler,				\
224120492Sfjoe		NULL							\
225120492Sfjoe	};								\
226120492Sfjoe	DECLARE_MODULE(fsname ## _iconv, fsname ## _iconv_mod,		\
227120492Sfjoe		       SI_SUB_DRIVERS, SI_ORDER_ANY);			\
228120492Sfjoe	MODULE_DEPEND(fsname ## _iconv, fsname, 1, 1, 1);		\
229120492Sfjoe	MODULE_DEPEND(fsname ## _iconv, libiconv, 2, 2, 2);		\
230120492Sfjoe	MODULE_VERSION(fsname ## _iconv, 1)
231120492Sfjoe
232120492Sfjoe/*
23375332Sbp * Internal functions
23475332Sbp */
23575332Sbpint iconv_lookupcp(char **cpp, const char *s);
23675332Sbp
23775332Sbpint iconv_converter_initstub(struct iconv_converter_class *dp);
23875332Sbpint iconv_converter_donestub(struct iconv_converter_class *dp);
239194638Sdelphijint iconv_converter_tolowerstub(int c, void *handle);
24075332Sbpint iconv_converter_handler(module_t mod, int type, void *data);
24175332Sbp
24275332Sbp#ifdef ICONV_DEBUG
243235711Skevlo#define ICDEBUG(format, ...) printf("%s: "format, __func__ , ## __VA_ARGS__)
24475332Sbp#else
245100080Smarkm#define ICDEBUG(format, ...)
24675332Sbp#endif
24775332Sbp
24875332Sbp#endif /* !_KERNEL */
24975332Sbp
25075332Sbp#endif /* !_SYS_ICONV_H_ */
251