]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/iconv.h
Fix a security problem in sysctl() the long way round.
[FreeBSD/FreeBSD.git] / sys / sys / iconv.h
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$
33  */
34 #ifndef _SYS_ICONV_H_
35 #define _SYS_ICONV_H_
36
37 #define ICONV_CSNMAXLEN         31      /* maximum length of charset name */
38 #define ICONV_CNVNMAXLEN        31      /* maximum length of converter name */
39 #define ICONV_CSMAXDATALEN      (2048+262144)   /* maximum size of data associated with cs pair */
40
41 #define XLAT16_ACCEPT_NULL_OUT          0x01000000
42 #define XLAT16_ACCEPT_NULL_IN           0x02000000
43 #define XLAT16_HAS_LOWER_CASE           0x04000000
44 #define XLAT16_HAS_UPPER_CASE           0x08000000
45 #define XLAT16_HAS_FROM_LOWER_CASE      0x10000000
46 #define XLAT16_HAS_FROM_UPPER_CASE      0x20000000
47 #define XLAT16_IS_3BYTE_CHR             0x40000000
48
49 #define KICONV_LOWER            1       /* tolower converted character */
50 #define KICONV_UPPER            2       /* toupper converted character */
51 #define KICONV_FROM_LOWER       4       /* tolower source character, then convert */
52 #define KICONV_FROM_UPPER       8       /* toupper source character, then convert */
53
54 /*
55  * Entry for cslist sysctl
56  */
57 #define ICONV_CSPAIR_INFO_VER   1
58
59 struct iconv_cspair_info {
60         int     cs_version;
61         int     cs_id;
62         int     cs_base;
63         int     cs_refcount;
64         char    cs_to[ICONV_CSNMAXLEN];
65         char    cs_from[ICONV_CSNMAXLEN];
66 };
67
68 /*
69  * Paramters for 'add' sysctl
70  */
71 #define ICONV_ADD_VER   1
72
73 struct iconv_add_in {
74         int     ia_version;
75         char    ia_converter[ICONV_CNVNMAXLEN];
76         char    ia_to[ICONV_CSNMAXLEN];
77         char    ia_from[ICONV_CSNMAXLEN];
78         int     ia_datalen;
79         const void *ia_data;
80 };
81
82 struct iconv_add_out {
83         int     ia_csid;
84 };
85
86 #ifndef _KERNEL
87
88 __BEGIN_DECLS
89
90 #define ENCODING_UNICODE        "ISO-10646-UCS-2"
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_table(const char *, const char *, const void *, int);
96 const char *kiconv_quirkcs(const char *, int);
97
98 __END_DECLS
99
100 #else /* !_KERNEL */
101
102 #include <sys/kobj.h>
103 #include <sys/queue.h>                  /* can't avoid that */
104 #include <sys/sysctl.h>                 /* can't avoid that */
105
106 struct iconv_cspair;
107 struct iconv_cspairdata;
108
109 /*
110  * iconv converter class definition
111  */
112 struct iconv_converter_class {
113         KOBJ_CLASS_FIELDS;
114         TAILQ_ENTRY(iconv_converter_class)      cc_link;
115 };
116
117 struct iconv_cspair {
118         int             cp_id;          /* unique id of charset pair */
119         int             cp_refcount;    /* number of references from other pairs */
120         const char *    cp_from;
121         const char *    cp_to;
122         void *          cp_data;
123         struct iconv_converter_class * cp_dcp;
124         struct iconv_cspair *cp_base;
125         TAILQ_ENTRY(iconv_cspair)       cp_link;
126 };
127
128 #define KICONV_CONVERTER(name,size)                             \
129     static DEFINE_CLASS(iconv_ ## name, iconv_ ## name ## _methods, (size)); \
130     static moduledata_t iconv_ ## name ## _mod = {      \
131         "iconv_"#name, iconv_converter_handler,         \
132         (void*)&iconv_ ## name ## _class                \
133     };                                                  \
134     DECLARE_MODULE(iconv_ ## name, iconv_ ## name ## _mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
135
136 #define KICONV_CES(name,size)                           \
137     static DEFINE_CLASS(iconv_ces_ ## name, iconv_ces_ ## name ## _methods, (size)); \
138     static moduledata_t iconv_ces_ ## name ## _mod = {  \
139         "iconv_ces_"#name, iconv_cesmod_handler,        \
140         (void*)&iconv_ces_ ## name ## _class            \
141     };                                                  \
142     DECLARE_MODULE(iconv_ces_ ## name, iconv_ces_ ## name ## _mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
143
144 #ifdef MALLOC_DECLARE
145 MALLOC_DECLARE(M_ICONV);
146 #endif
147
148 /*
149  * Basic conversion functions
150  */
151 int iconv_open(const char *to, const char *from, void **handle);
152 int iconv_close(void *handle);
153 int iconv_conv(void *handle, const char **inbuf,
154         size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
155 int iconv_conv_case(void *handle, const char **inbuf,
156         size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype);
157 int iconv_convchr(void *handle, const char **inbuf,
158         size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
159 int iconv_convchr_case(void *handle, const char **inbuf,
160         size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype);
161 char* iconv_convstr(void *handle, char *dst, const char *src);
162 void* iconv_convmem(void *handle, void *dst, const void *src, int size);
163 int iconv_vfs_refcount(const char *fsname);
164
165 /*
166  * Bridge struct of iconv functions
167  */
168 struct iconv_functions {
169         int (*open)(const char *to, const char *from, void **handle);
170         int (*close)(void *handle);
171         int (*conv)(void *handle, const char **inbuf, size_t *inbytesleft,
172                 char **outbuf, size_t *outbytesleft);
173         int (*conv_case)(void *handle, const char **inbuf, size_t *inbytesleft,
174                 char **outbuf, size_t *outbytesleft, int casetype);
175         int (*convchr)(void *handle, const char **inbuf, size_t *inbytesleft,
176                 char **outbuf, size_t *outbytesleft);
177         int (*convchr_case)(void *handle, const char **inbuf, size_t *inbytesleft,
178                 char **outbuf, size_t *outbytesleft, int casetype);
179 };
180
181 #define VFS_DECLARE_ICONV(fsname)                                       \
182         static struct iconv_functions fsname ## _iconv_core = {         \
183                 iconv_open,                                             \
184                 iconv_close,                                            \
185                 iconv_conv,                                             \
186                 iconv_conv_case,                                        \
187                 iconv_convchr,                                          \
188                 iconv_convchr_case                                      \
189         };                                                              \
190         extern struct iconv_functions *fsname ## _iconv;                \
191         static int fsname ## _iconv_mod_handler(module_t mod,           \
192                 int type, void *d);                                     \
193         static int                                                      \
194         fsname ## _iconv_mod_handler(module_t mod, int type, void *d)   \
195         {                                                               \
196                 int error = 0;                                          \
197                 switch(type) {                                          \
198                 case MOD_LOAD:                                          \
199                         fsname ## _iconv = & fsname ## _iconv_core;     \
200                         break;                                          \
201                 case MOD_UNLOAD:                                        \
202                         error = iconv_vfs_refcount(#fsname);            \
203                         if (error)                                      \
204                                 return (EBUSY);                         \
205                         fsname ## _iconv = NULL;                        \
206                         break;                                          \
207                 default:                                                \
208                         error = EINVAL;                                 \
209                         break;                                          \
210                 }                                                       \
211                 return (error);                                         \
212         }                                                               \
213         static moduledata_t fsname ## _iconv_mod = {                    \
214                 #fsname"_iconv",                                        \
215                 fsname ## _iconv_mod_handler,                           \
216                 NULL                                                    \
217         };                                                              \
218         DECLARE_MODULE(fsname ## _iconv, fsname ## _iconv_mod,          \
219                        SI_SUB_DRIVERS, SI_ORDER_ANY);                   \
220         MODULE_DEPEND(fsname ## _iconv, fsname, 1, 1, 1);               \
221         MODULE_DEPEND(fsname ## _iconv, libiconv, 2, 2, 2);             \
222         MODULE_VERSION(fsname ## _iconv, 1)
223
224 /*
225  * Internal functions
226  */
227 int iconv_lookupcp(char **cpp, const char *s);
228
229 int iconv_converter_initstub(struct iconv_converter_class *dp);
230 int iconv_converter_donestub(struct iconv_converter_class *dp);
231 int iconv_converter_handler(module_t mod, int type, void *data);
232
233 #ifdef ICONV_DEBUG
234 #define ICDEBUG(format, ...) printf("%s: "format, __func__ , __VA_ARGS__)
235 #else
236 #define ICDEBUG(format, ...)
237 #endif
238
239 #endif /* !_KERNEL */
240
241 #endif /* !_SYS_ICONV_H_ */