xref: /dragonfly/sys/sys/iconv.h (revision 1bf4b486)
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.1.2.1 2001/05/21 08:28:07 bp Exp $
33  * $DragonFly: src/sys/sys/iconv.h,v 1.3 2005/02/17 14:00:09 joerg Exp $
34  */
35 #ifndef _SYS_ICONV_H_
36 #define _SYS_ICONV_H_
37 
38 #define	ICONV_CSNMAXLEN		31	/* maximum length of charset name */
39 #define	ICONV_CNVNMAXLEN	31	/* maximum length of converter name */
40 #define	ICONV_CSMAXDATALEN	1024	/* maximum size of data associated with cs pair */
41 
42 /*
43  * Entry for cslist sysctl
44  */
45 #define	ICONV_CSPAIR_INFO_VER	1
46 
47 struct iconv_cspair_info {
48 	int	cs_version;
49 	int	cs_id;
50 	int	cs_base;
51 	int	cs_refcount;
52 	char	cs_to[ICONV_CSNMAXLEN];
53 	char	cs_from[ICONV_CSNMAXLEN];
54 };
55 
56 /*
57  * Paramters for 'add' sysctl
58  */
59 #define	ICONV_ADD_VER	1
60 
61 struct iconv_add_in {
62 	int	ia_version;
63 	char	ia_converter[ICONV_CNVNMAXLEN];
64 	char	ia_to[ICONV_CSNMAXLEN];
65 	char	ia_from[ICONV_CSNMAXLEN];
66 	int	ia_datalen;
67 	const void *ia_data;
68 };
69 
70 struct iconv_add_out {
71 	int	ia_csid;
72 };
73 
74 #ifndef _KERNEL
75 
76 __BEGIN_DECLS
77 
78 int   kiconv_add_xlat_table(const char *, const char *, const u_char *);
79 
80 __END_DECLS
81 
82 #else /* !_KERNEL */
83 
84 #include <sys/kobj.h>
85 #include <sys/queue.h>			/* can't avoid that */
86 #include <sys/sysctl.h>			/* can't avoid that */
87 
88 struct iconv_cspair;
89 struct iconv_cspairdata;
90 
91 /*
92  * iconv converter class definition
93  */
94 struct iconv_converter_class {
95 	KOBJ_CLASS_FIELDS;
96 	TAILQ_ENTRY(iconv_converter_class)	cc_link;
97 };
98 
99 struct iconv_cspair {
100 	int		cp_id;		/* unique id of charset pair */
101 	int		cp_refcount;	/* number of references from other pairs */
102 	const char *	cp_from;
103 	const char *	cp_to;
104 	void *		cp_data;
105 	struct iconv_converter_class * cp_dcp;
106 	struct iconv_cspair *cp_base;
107 	TAILQ_ENTRY(iconv_cspair)	cp_link;
108 };
109 
110 #define	KICONV_CONVERTER(name,size) 				\
111     static DEFINE_CLASS(iconv_ ## name, iconv_ ## name ## _methods, (size)); \
112     static moduledata_t iconv_ ## name ## _mod = {	\
113 	"iconv_"#name, iconv_converter_handler,		\
114 	(void*)&iconv_ ## name ## _class		\
115     };							\
116     DECLARE_MODULE(iconv_ ## name, iconv_ ## name ## _mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
117 
118 #define	KICONV_CES(name,size) 				\
119     static DEFINE_CLASS(iconv_ces_ ## name, iconv_ces_ ## name ## _methods, (size)); \
120     static moduledata_t iconv_ces_ ## name ## _mod = {	\
121 	"iconv_ces_"#name, iconv_cesmod_handler,	\
122 	(void*)&iconv_ces_ ## name ## _class		\
123     };							\
124     DECLARE_MODULE(iconv_ces_ ## name, iconv_ces_ ## name ## _mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
125 
126 #ifdef MALLOC_DECLARE
127 MALLOC_DECLARE(M_ICONV);
128 #endif
129 
130 /*
131  * Basic conversion functions
132  */
133 int iconv_open(const char *to, const char *from, void **handle);
134 int iconv_close(void *handle);
135 int iconv_conv(void *handle, const char **inbuf,
136 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
137 char* iconv_convstr(void *handle, char *dst, const char *src);
138 void* iconv_convmem(void *handle, void *dst, const void *src, int size);
139 
140 /*
141  * Internal functions
142  */
143 int iconv_lookupcp(char **cpp, const char *s);
144 
145 int iconv_converter_initstub(struct iconv_converter_class *dp);
146 int iconv_converter_donestub(struct iconv_converter_class *dp);
147 int iconv_converter_handler(module_t mod, int type, void *data);
148 
149 #ifdef ICONV_DEBUG
150 #define ICDEBUG(format, args...) printf("%s: "format, __func__ ,## args)
151 #else
152 #define ICDEBUG(format, args...)
153 #endif
154 
155 #endif /* !_KERNEL */
156 
157 #endif /* !_SYS_ICONV_H_ */
158