1 /*
2  * Copyright (c) 2003-2004, Artem B. Bityuckiy
3  * Copyright (c) 1999,2000, Konstantin Chuguev. 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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 #ifndef __ICONV_CONVERSION_H__
27 #define __ICONV_CONVERSION_H__
28 
29 #include <_ansi.h>
30 #include <sys/types.h>
31 #include <wchar.h>
32 
33 /* Bits for 'flags' parameter of 'convert' call */
34 #define ICONV_DONT_SAVE_BIT 1
35 #define ICONV_FAIL_BIT      2
36 
37 /*
38  * iconv_conversion_handlers_t - keeps iconv conversion handlers.
39  *
40  * Keeps 6 interface function handlers:
41  * open(), close(), convert(), get_mb_cur_max(), get_state(), set_state(),
42  * get_mb_cur_max() and is_stateful(). Last 5 interface functions are needed to
43  * support locale subsystem.
44  *
45  * ============================================================================
46  */
47 typedef struct
48 {
49   /*
50    * open - open and initialize conversion.
51    *
52    * PARAMETERS:
53    *   const char *to     - output encoding's normalized name;
54    *   const char *from   - input encoding's normalized name.
55    *
56    * DESCRIPTION:
57    *   This function is called from iconv_open() to open conversion. Returns
58    *   a pointer to conversion-specific data.
59    *
60    * RETURN:
61    *   Pointer to conversion-specific data if success. In case of error
62    *   returns NULL and sets current thread's/process's errno.
63    */
64   void *(*open) (
65                           const char *to,
66                           const char *from);
67 
68   /*
69    * close - close conversion.
70    *
71    * PARAMETRS:
72    *   void *data      - conversion-specific data.
73    *
74    * DESCRIPTION:
75    *   This function is called from iconv_close() to close conversion.
76    *
77    * RETURN:
78    *   When successful, returns (size_t)0. In case of error, sets current
79    *   thread's/process's errno and returns (size_t)-1 (same as iconv_open()).
80    */
81   size_t (*close) (
82                         void *data);
83 
84   /* convert - perform encoding conversion.
85    *
86    * PARAMETERS:
87    *   void *data      - conversion-specific data;
88    *   const unsigned char **inbuf - input data buffer;
89    *   size_t *inbytesleft          - input buffer's length;
90    *   unsigned char **outbuf       - output data buffer;
91    *   size_t *outbytesleft         - output buffer free space;
92    *   int flags                    - conversion options.
93    *
94    * DESCRIPTION:
95    *   This function is called from iconv() to perform conversion and, if 'flags'
96    *   is 0, behaves similarly to iconv(). 'inbuf', 'inbytesleft', 'outbuf' and
97    *   'outbytesleft' are same as in case of iconv() function.
98    *
99    *   When flags & 1 isn't 0, 'outbuf' value is ignored and result isn't saved.
100    *   Another conversion aspects aren't changed.
101    *
102    *   When flags & 2 isn't 0, function changes it's behavior in situations,
103    *   when there is no character in "to" encoding that corresponds to valid
104    *   character from "from" encoding. iconv() specification stands to perform
105    *   implimentation-spacific default conversion. If flag & 2 isn't 0,
106    *   function generates error.
107    *
108    * RETURN:
109    *   Returns the number of characters converted in a non-reversible way.
110    *   Reversible conversions are not counted. In case of error, sets current
111    *   thread's/process's errno and returns (size_t)-1 (same as iconv()).
112    */
113   size_t (*convert) (
114                            void *data,
115                            const unsigned char **inbuf,
116                            size_t *inbytesleft,
117                            unsigned char **outbuf,
118                            size_t *outbytesleft,
119                            int flags);
120 
121   /*
122    * get_state - get current shift state.
123    *
124    * PARAMETERS:
125    *   void *data   - conversion-specific data;
126    *   mbstate_t *state - mbstate_t object where shift state will be written;
127    *   int direction      - 0-"from", 1-"to".
128    *
129    * DESCRIPTION:
130    *   Returns encoding's current shift sequence.
131    *   If 'direction' is 0, "from" encoding is tested, else
132    *   "to" encoding is tested.
133    */
134   void (*get_state) (void *data,
135                            mbstate_t *state,
136                            int direction);
137 
138   /*
139    * set_state - set shift state.
140    *
141    * PARAMETERS:
142    *   void *data   - conversion-specific data;
143    *   mbstate_t *state - mbstate_t object to which shift state will be set.
144    *   int direction     - 0-"from", 1-"to".
145    *
146    * DESCRIPTION:
147    *   Sets encoding's current shift state to 'state'. if 'state'
148    *   object is zero-object - reset current shift state.
149    *   If 'direction' is 0, "from" encoding is set, else
150    *   "to" encoding is set.
151    *   Returns 0 if '*state' object has right format, -1 else.
152    */
153   int (*set_state) (void *data,
154                          mbstate_t *state,
155                          int direction);
156 
157   /*
158    * get_mb_cur_max - get maximum character length in bytes.
159    *
160    * PARAMETERS:
161    *   void *data     - conversion-specific data;
162    *   int direction      - 0-"from", 1-"to".
163    *
164    * DESCRIPTION:
165    *   Returns encoding's maximum character length.
166    *   If 'direction' is 0, "from" encoding is tested, else
167    *   "to" encoding is tested.
168    */
169   int (*get_mb_cur_max) (void *data,
170                               int direction);
171 
172   /*
173    * is_stateful - is encoding stateful or stateless.
174    *
175    * PARAMETERS:
176    *   void *data - conversion-specific data;
177    *   int direction  - 0-"from", 1-"to".
178    *
179    * DESCRIPTION:
180    *   Returns 0 if encoding is stateless and 1 if stateful.
181    *   If 'direction' is 0, "from" encoding is tested, else
182    *   "to" encoding is tested.
183    */
184   int (*is_stateful) (void *data,
185                            int direction);
186 
187 } iconv_conversion_handlers_t;
188 
189 
190 /*
191  * iconv_conversion_t - iconv conversion definition structure.
192  *
193  * ============================================================================
194  */
195 typedef struct
196 {
197   /* Iconv conversion handlers. */
198   const iconv_conversion_handlers_t *handlers;
199 
200   /*
201    * Conversion-specific data (e.g., points to iconv_ucs_conversion_t
202    * object if UCS-based conversion is used).
203    */
204   void *data;
205 } iconv_conversion_t;
206 
207 
208 /* UCS-based conversion handlers */
209 extern const iconv_conversion_handlers_t
210 _iconv_ucs_conversion_handlers;
211 
212 /* Null conversion handlers */
213 extern const iconv_conversion_handlers_t
214 _iconv_null_conversion_handlers;
215 
216 #endif /* !__ICONV_CONVERSION_H__ */
217 
218