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 <sys/types.h>
30 #include <wchar.h>
31 
32 /* Bits for 'flags' parameter of 'convert' call */
33 #define ICONV_DONT_SAVE_BIT 1
34 #define ICONV_FAIL_BIT      2
35 
36 /*
37  * iconv_conversion_handlers_t - keeps iconv conversion handlers.
38  *
39  * Keeps 6 interface function handlers:
40  * open(), close(), convert(), get_mb_cur_max(), get_state(), set_state(),
41  * get_mb_cur_max() and is_stateful(). Last 5 interface functions are needed to
42  * support locale subsystem.
43  *
44  * ============================================================================
45  */
46 typedef struct
47 {
48   /*
49    * open - open and initialize conversion.
50    *
51    * PARAMETERS:
52    *   const char *to     - output encoding's normalized name;
53    *   const char *from   - input encoding's normalized name.
54    *
55    * DESCRIPTION:
56    *   This function is called from iconv_open() to open conversion. Returns
57    *   a pointer to conversion-specific data.
58    *
59    * RETURN:
60    *   Pointer to conversion-specific data if success. In case of error
61    *   returns NULL and sets current thread's/process's errno.
62    */
63   void *(*open) (
64                           const char *to,
65                           const char *from);
66 
67   /*
68    * close - close conversion.
69    *
70    * PARAMETRS:
71    *   void *data      - conversion-specific data.
72    *
73    * DESCRIPTION:
74    *   This function is called from iconv_close() to close conversion.
75    *
76    * RETURN:
77    *   When successful, returns (size_t)0. In case of error, sets current
78    *   thread's/process's errno and returns (size_t)-1 (same as iconv_open()).
79    */
80   size_t (*close) (
81                         void *data);
82 
83   /* convert - perform encoding conversion.
84    *
85    * PARAMETERS:
86    *   void *data      - conversion-specific data;
87    *   const unsigned char **inbuf - input data buffer;
88    *   size_t *inbytesleft          - input buffer's length;
89    *   unsigned char **outbuf       - output data buffer;
90    *   size_t *outbytesleft         - output buffer free space;
91    *   int flags                    - conversion options.
92    *
93    * DESCRIPTION:
94    *   This function is called from iconv() to perform conversion and, if 'flags'
95    *   is 0, behaves similarly to iconv(). 'inbuf', 'inbytesleft', 'outbuf' and
96    *   'outbytesleft' are same as in case of iconv() function.
97    *
98    *   When flags & 1 isn't 0, 'outbuf' value is ignored and result isn't saved.
99    *   Another conversion aspects aren't changed.
100    *
101    *   When flags & 2 isn't 0, function changes it's behavior in situations,
102    *   when there is no character in "to" encoding that corresponds to valid
103    *   character from "from" encoding. iconv() specification stands to perform
104    *   implimentation-spacific default conversion. If flag & 2 isn't 0,
105    *   function generates error.
106    *
107    * RETURN:
108    *   Returns the number of characters converted in a non-reversible way.
109    *   Reversible conversions are not counted. In case of error, sets current
110    *   thread's/process's errno and returns (size_t)-1 (same as iconv()).
111    */
112   size_t (*convert) (
113                            void *data,
114                            const unsigned char **inbuf,
115                            size_t *inbytesleft,
116                            unsigned char **outbuf,
117                            size_t *outbytesleft,
118                            int flags);
119 
120   /*
121    * get_state - get current shift state.
122    *
123    * PARAMETERS:
124    *   void *data   - conversion-specific data;
125    *   mbstate_t *state - mbstate_t object where shift state will be written;
126    *   int direction      - 0-"from", 1-"to".
127    *
128    * DESCRIPTION:
129    *   Returns encoding's current shift sequence.
130    *   If 'direction' is 0, "from" encoding is tested, else
131    *   "to" encoding is tested.
132    */
133   void (*get_state) (void *data,
134                            mbstate_t *state,
135                            int direction);
136 
137   /*
138    * set_state - set shift state.
139    *
140    * PARAMETERS:
141    *   void *data   - conversion-specific data;
142    *   mbstate_t *state - mbstate_t object to which shift state will be set.
143    *   int direction     - 0-"from", 1-"to".
144    *
145    * DESCRIPTION:
146    *   Sets encoding's current shift state to 'state'. if 'state'
147    *   object is zero-object - reset current shift state.
148    *   If 'direction' is 0, "from" encoding is set, else
149    *   "to" encoding is set.
150    *   Returns 0 if '*state' object has right format, -1 else.
151    */
152   int (*set_state) (void *data,
153                          mbstate_t *state,
154                          int direction);
155 
156   /*
157    * get_mb_cur_max - get maximum character length in bytes.
158    *
159    * PARAMETERS:
160    *   void *data     - conversion-specific data;
161    *   int direction      - 0-"from", 1-"to".
162    *
163    * DESCRIPTION:
164    *   Returns encoding's maximum character length.
165    *   If 'direction' is 0, "from" encoding is tested, else
166    *   "to" encoding is tested.
167    */
168   int (*get_mb_cur_max) (void *data,
169                               int direction);
170 
171   /*
172    * is_stateful - is encoding stateful or stateless.
173    *
174    * PARAMETERS:
175    *   void *data - conversion-specific data;
176    *   int direction  - 0-"from", 1-"to".
177    *
178    * DESCRIPTION:
179    *   Returns 0 if encoding is stateless and 1 if stateful.
180    *   If 'direction' is 0, "from" encoding is tested, else
181    *   "to" encoding is tested.
182    */
183   int (*is_stateful) (void *data,
184                            int direction);
185 
186 } iconv_conversion_handlers_t;
187 
188 
189 /*
190  * iconv_conversion_t - iconv conversion definition structure.
191  *
192  * ============================================================================
193  */
194 typedef struct
195 {
196   /* Iconv conversion handlers. */
197   const iconv_conversion_handlers_t *handlers;
198 
199   /*
200    * Conversion-specific data (e.g., points to iconv_ucs_conversion_t
201    * object if UCS-based conversion is used).
202    */
203   void *data;
204 } iconv_conversion_t;
205 
206 
207 /* UCS-based conversion handlers */
208 extern const iconv_conversion_handlers_t
209 _iconv_ucs_conversion_handlers;
210 
211 /* Null conversion handlers */
212 extern const iconv_conversion_handlers_t
213 _iconv_null_conversion_handlers;
214 
215 #endif /* !__ICONV_CONVERSION_H__ */
216 
217