xref: /illumos-gate/usr/src/uts/common/sys/kiconv.h (revision fafb665d)
1*d14d7d31Sis /*
2*d14d7d31Sis  * CDDL HEADER START
3*d14d7d31Sis  *
4*d14d7d31Sis  * The contents of this file are subject to the terms of the
5*d14d7d31Sis  * Common Development and Distribution License (the "License").
6*d14d7d31Sis  * You may not use this file except in compliance with the License.
7*d14d7d31Sis  *
8*d14d7d31Sis  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*d14d7d31Sis  * or http://www.opensolaris.org/os/licensing.
10*d14d7d31Sis  * See the License for the specific language governing permissions
11*d14d7d31Sis  * and limitations under the License.
12*d14d7d31Sis  *
13*d14d7d31Sis  * When distributing Covered Code, include this CDDL HEADER in each
14*d14d7d31Sis  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*d14d7d31Sis  * If applicable, add the following below this CDDL HEADER, with the
16*d14d7d31Sis  * fields enclosed by brackets "[]" replaced with your own identifying
17*d14d7d31Sis  * information: Portions Copyright [yyyy] [name of copyright owner]
18*d14d7d31Sis  *
19*d14d7d31Sis  * CDDL HEADER END
20*d14d7d31Sis  */
21*d14d7d31Sis /*
22*d14d7d31Sis  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*d14d7d31Sis  * Use is subject to license terms.
24*d14d7d31Sis  */
25*d14d7d31Sis 
26*d14d7d31Sis #ifndef _SYS_KICONV_H
27*d14d7d31Sis #define	_SYS_KICONV_H
28*d14d7d31Sis 
29*d14d7d31Sis #ifdef __cplusplus
30*d14d7d31Sis extern "C" {
31*d14d7d31Sis #endif
32*d14d7d31Sis 
33*d14d7d31Sis #include <sys/types.h>
34*d14d7d31Sis 
35*d14d7d31Sis #ifdef	_KERNEL
36*d14d7d31Sis 
37*d14d7d31Sis /*
38*d14d7d31Sis  * Supported fromcode/tocode values are saved in the following component type
39*d14d7d31Sis  * of (name, id) pair. The id values of fromcode and tocode are used to
40*d14d7d31Sis  * find out the corresponding code conversions.
41*d14d7d31Sis  */
42*d14d7d31Sis typedef struct {
43*d14d7d31Sis 	char		*name;
44*d14d7d31Sis 	size_t		id;
45*d14d7d31Sis } kiconv_code_list_t;
46*d14d7d31Sis 
47*d14d7d31Sis /*
48*d14d7d31Sis  * Each unique kiconv code conversion identified by tocode and fromcode ids
49*d14d7d31Sis  * have corresponding module id and internal function pointers to open(),
50*d14d7d31Sis  * kiconv(), close(), and kiconvstr().
51*d14d7d31Sis  */
52*d14d7d31Sis typedef struct {
53*d14d7d31Sis 	uint16_t	tid;		/* tocode id. */
54*d14d7d31Sis 	uint16_t	fid;		/* fromcode id. */
55*d14d7d31Sis 	uint16_t	mid;		/* module id. */
56*d14d7d31Sis 	void		*(*open)(void);
57*d14d7d31Sis 	size_t		(*kiconv)(void *, char **, size_t *, char **, size_t *,
58*d14d7d31Sis 			int *);
59*d14d7d31Sis 	int		(*close)(void *);
60*d14d7d31Sis 	size_t		(*kiconvstr)(char *, size_t *, char *, size_t *, int,
61*d14d7d31Sis 			int *);
62*d14d7d31Sis } kiconv_conv_list_t;
63*d14d7d31Sis 
64*d14d7d31Sis /*
65*d14d7d31Sis  * Each module id has a corresponding module name that is used to load
66*d14d7d31Sis  * the module as needed and a reference counter.
67*d14d7d31Sis  */
68*d14d7d31Sis typedef struct {
69*d14d7d31Sis 	char		*name;
70*d14d7d31Sis 	uint_t		refcount;
71*d14d7d31Sis } kiconv_mod_list_t;
72*d14d7d31Sis 
73*d14d7d31Sis /*
74*d14d7d31Sis  * The following two data structures are being used to transfer information
75*d14d7d31Sis  * on the supported kiconv code conversions from a module to the framework.
76*d14d7d31Sis  *
77*d14d7d31Sis  * Details can be found from kiconv_ops(9S) and kiconv_module_info(9S)
78*d14d7d31Sis  * man pages at PSARC/2007/173.
79*d14d7d31Sis  */
80*d14d7d31Sis typedef struct {
81*d14d7d31Sis 	char		*tocode;
82*d14d7d31Sis 	char		*fromcode;
83*d14d7d31Sis 	void		*(*kiconv_open)(void);
84*d14d7d31Sis 	size_t		(*kiconv)(void *, char **, size_t *, char **, size_t *,
85*d14d7d31Sis 			int *);
86*d14d7d31Sis 	int		(*kiconv_close)(void *);
87*d14d7d31Sis 	size_t		(*kiconvstr)(char *, size_t *, char *, size_t *, int,
88*d14d7d31Sis 			int *);
89*d14d7d31Sis } kiconv_ops_t;
90*d14d7d31Sis 
91*d14d7d31Sis typedef struct kiconv_mod_info {
92*d14d7d31Sis 	char		*module_name;
93*d14d7d31Sis 	size_t		kiconv_num_convs;
94*d14d7d31Sis 	kiconv_ops_t	*kiconv_ops_tbl;
95*d14d7d31Sis 	size_t		kiconv_num_aliases;
96*d14d7d31Sis 	char		**aliases;
97*d14d7d31Sis 	char		**canonicals;
98*d14d7d31Sis 	int		nowait;
99*d14d7d31Sis } kiconv_module_info_t;
100*d14d7d31Sis 
101*d14d7d31Sis /* The kiconv code conversion descriptor data structure. */
102*d14d7d31Sis typedef struct {
103*d14d7d31Sis 	void		*handle;	/* Handle from the actual open(). */
104*d14d7d31Sis 	size_t		id;		/* Index to the conv_list[]. */
105*d14d7d31Sis } kiconv_data_t, *kiconv_t;
106*d14d7d31Sis 
107*d14d7d31Sis /* Common conversion state data structure. */
108*d14d7d31Sis typedef struct {
109*d14d7d31Sis 	uint8_t		id;
110*d14d7d31Sis 	uint8_t		bom_processed;
111*d14d7d31Sis } kiconv_state_data_t, *kiconv_state_t;
112*d14d7d31Sis 
113*d14d7d31Sis /* Common component types for possible code conversion mapping tables. */
114*d14d7d31Sis typedef struct {
115*d14d7d31Sis 	uchar_t		u8[3];
116*d14d7d31Sis } kiconv_to_utf8_tbl_comp_t;
117*d14d7d31Sis 
118*d14d7d31Sis typedef struct {
119*d14d7d31Sis 	uint32_t	u8:24;
120*d14d7d31Sis 	uint32_t	sb:8;
121*d14d7d31Sis } kiconv_to_sb_tbl_comp_t;
122*d14d7d31Sis 
123*d14d7d31Sis /*
124*d14d7d31Sis  * The maximum name length for any given codeset or alias names; the following
125*d14d7d31Sis  * should be plenty big enough.
126*d14d7d31Sis  */
127*d14d7d31Sis #define	KICONV_MAX_CODENAME_LEN		63
128*d14d7d31Sis 
129*d14d7d31Sis /* The following characters do not exist in the normalized code names. */
130*d14d7d31Sis #define	KICONV_SKIPPABLE_CHAR(c)	\
131*d14d7d31Sis 	((c) == '-' || (c) == '_' || (c) == '.' || (c) == '@')
132*d14d7d31Sis 
133*d14d7d31Sis /*
134*d14d7d31Sis  * When we encounter non-identical characters, as like iconv(3C) we have,
135*d14d7d31Sis  * map them into either one of the replacement characters based on what is
136*d14d7d31Sis  * the current target tocde.
137*d14d7d31Sis  *
138*d14d7d31Sis  * The 0xefbfdb in UTF-8 is U+FFFD in Unicode scalar value.
139*d14d7d31Sis  */
140*d14d7d31Sis #define	KICONV_ASCII_REPLACEMENT_CHAR	('?')
141*d14d7d31Sis #define	KICONV_UTF8_REPLACEMENT_CHAR	(0xefbfbd)
142*d14d7d31Sis 
143*d14d7d31Sis /* Numeric ids for kiconv modules. */
144*d14d7d31Sis #define	KICONV_EMBEDDED			(0)
145*d14d7d31Sis #define	KICONV_MODULE_ID_JA		(1)
146*d14d7d31Sis #define	KICONV_MODULE_ID_SC		(2)
147*d14d7d31Sis #define	KICONV_MODULE_ID_KO		(3)
148*d14d7d31Sis #define	KICONV_MODULE_ID_TC		(4)
149*d14d7d31Sis #define	KICONV_MODULE_ID_EMEA		(5)
150*d14d7d31Sis 
151*d14d7d31Sis #define	KICONV_MAX_MODULE_ID		KICONV_MODULE_ID_EMEA
152*d14d7d31Sis 
153*d14d7d31Sis /* Functions used in kiconv conversion and module management. */
154*d14d7d31Sis extern void	kiconv_init();
155*d14d7d31Sis extern int	kiconv_register_module(kiconv_module_info_t *);
156*d14d7d31Sis extern int	kiconv_unregister_module(kiconv_module_info_t *);
157*d14d7d31Sis extern size_t	kiconv_module_ref_count(size_t);
158*d14d7d31Sis 
159*d14d7d31Sis #endif	/* _KERNEL */
160*d14d7d31Sis 
161*d14d7d31Sis #ifdef __cplusplus
162*d14d7d31Sis }
163*d14d7d31Sis #endif
164*d14d7d31Sis 
165*d14d7d31Sis #endif /* _SYS_KICONV_H */
166