768ebf0a97af074cb0b7b1d6e8fcae52dc81059b
[jlayton/glibc.git] / iconv / gconv_int.h
1 /* Copyright (C) 1997-2002, 2003, 2004, 2005 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #ifndef _GCONV_INT_H
21 #define _GCONV_INT_H    1
22
23 #include "gconv.h"
24 #include <stdlib.h>             /* For alloca used in macro below.  */
25 #include <ctype.h>              /* For __toupper_l used in macro below.  */
26 #include <string.h>             /* For strlen et al used in macro below.  */
27 #include <bits/libc-lock.h>
28
29 __BEGIN_DECLS
30
31
32 /* Type to represent search path.  */
33 struct path_elem
34 {
35   const char *name;
36   size_t len;
37 };
38
39 /* Variable with search path for `gconv' implementation.  */
40 extern struct path_elem *__gconv_path_elem attribute_hidden;
41 /* Maximum length of a single path element.  */
42 extern size_t __gconv_max_path_elem_len attribute_hidden;
43
44
45 /* Structure for alias definition.  Simply two strings.  */
46 struct gconv_alias
47 {
48   char *fromname;
49   char *toname;
50 };
51
52
53 /* How many character should be conveted in one call?  */
54 #define GCONV_NCHAR_GOAL        8160
55
56
57 /* Structure describing one loaded shared object.  This normally are
58    objects to perform conversation but as a special case the db shared
59    object is also handled.  */
60 struct __gconv_loaded_object
61 {
62   /* Name of the object.  It must be the first structure element.  */
63   const char *name;
64
65   /* Reference counter for the db functionality.  If no conversion is
66      needed we unload the db library.  */
67   int counter;
68
69   /* The handle for the shared object.  */
70   void *handle;
71
72   /* Pointer to the functions the module defines.  */
73   __gconv_fct fct;
74   __gconv_init_fct init_fct;
75   __gconv_end_fct end_fct;
76 };
77
78
79 /* Description for an available conversion module.  */
80 struct gconv_module
81 {
82   const char *from_string;
83   const char *to_string;
84
85   int cost_hi;
86   int cost_lo;
87
88   const char *module_name;
89
90   struct gconv_module *left;    /* Prefix smaller.  */
91   struct gconv_module *same;    /* List of entries with identical prefix.  */
92   struct gconv_module *right;   /* Prefix larger.  */
93 };
94
95
96 /* Internal data structure to represent transliteration module.  */
97 struct trans_struct
98 {
99   const char *name;
100   struct trans_struct *next;
101
102   const char **csnames;
103   size_t ncsnames;
104   __gconv_trans_fct trans_fct;
105   __gconv_trans_context_fct trans_context_fct;
106   __gconv_trans_init_fct trans_init_fct;
107   __gconv_trans_end_fct trans_end_fct;
108 };
109
110
111 /* Flags for `gconv_open'.  */
112 enum
113 {
114   GCONV_AVOID_NOCONV = 1 << 0
115 };
116
117
118 /* Global variables.  */
119
120 /* Database of alias names.  */
121 extern void *__gconv_alias_db attribute_hidden;
122
123 /* Array with available modules.  */
124 extern size_t __gconv_nmodules;
125 extern struct gconv_module *__gconv_modules_db attribute_hidden;
126
127 /* Value of the GCONV_PATH environment variable.  */
128 extern const char *__gconv_path_envvar attribute_hidden;
129
130 /* Lock for the conversion database content.  */
131 __libc_lock_define (extern, __gconv_lock);
132
133
134 /* The gconv functions expects the name to be in upper case and complete,
135    including the trailing slashes if necessary.  */
136 #define norm_add_slashes(str,suffix) \
137   ({                                                                          \
138     const char *cp = (str);                                                   \
139     char *result;                                                             \
140     char *tmp;                                                                \
141     size_t cnt = 0;                                                           \
142     const size_t suffix_len = strlen (suffix);                                \
143                                                                               \
144     while (*cp != '\0')                                                       \
145       if (*cp++ == '/')                                                       \
146         ++cnt;                                                                \
147                                                                               \
148     tmp = result = __alloca (cp - (str) + 3 + suffix_len);                    \
149     cp = (str);                                                               \
150     while (*cp != '\0')                                                       \
151       *tmp++ = __toupper_l (*cp++, &_nl_C_locobj);                            \
152     if (cnt < 2)                                                              \
153       {                                                                       \
154         *tmp++ = '/';                                                         \
155         if (cnt < 1)                                                          \
156           {                                                                   \
157             *tmp++ = '/';                                                     \
158             if (suffix_len != 0)                                              \
159               tmp = __mempcpy (tmp, suffix, suffix_len);                      \
160           }                                                                   \
161       }                                                                       \
162     *tmp = '\0';                                                              \
163     result;                                                                   \
164   })
165
166
167 /* Return in *HANDLE decriptor for transformation from FROMSET to TOSET.  */
168 extern int __gconv_open (const char *toset, const char *fromset,
169                          __gconv_t *handle, int flags)
170      internal_function;
171
172 /* Free resources associated with transformation descriptor CD.  */
173 extern int __gconv_close (__gconv_t cd)
174      internal_function;
175
176 /* Transform at most *INBYTESLEFT bytes from buffer starting at *INBUF
177    according to rules described by CD and place up to *OUTBYTESLEFT
178    bytes in buffer starting at *OUTBUF.  Return number of non-identical
179    conversions in *IRREVERSIBLE if this pointer is not null.  */
180 extern int __gconv (__gconv_t cd, const unsigned char **inbuf,
181                     const unsigned char *inbufend, unsigned char **outbuf,
182                     unsigned char *outbufend, size_t *irreversible)
183      internal_function;
184
185 /* Return in *HANDLE a pointer to an array with *NSTEPS elements describing
186    the single steps necessary for transformation from FROMSET to TOSET.  */
187 extern int __gconv_find_transform (const char *toset, const char *fromset,
188                                    struct __gconv_step **handle,
189                                    size_t *nsteps, int flags)
190      internal_function;
191
192 /* Search for transformation in cache data.  */
193 extern int __gconv_lookup_cache (const char *toset, const char *fromset,
194                                  struct __gconv_step **handle, size_t *nsteps,
195                                  int flags)
196      internal_function;
197
198 /* Compare the two name for whether they are after alias expansion the
199    same.  This function uses the cache and fails if none is
200    loaded.  */
201 extern int __gconv_compare_alias_cache (const char *name1, const char *name2,
202                                         int *result) internal_function;
203
204 /* Free data associated with a step's structure.  */
205 extern void __gconv_release_step (struct __gconv_step *step)
206      internal_function;
207
208 /* Read all the configuration data and cache it.  */
209 extern void __gconv_read_conf (void) attribute_hidden;
210
211 /* Try to read module cache file.  */
212 extern int __gconv_load_cache (void) internal_function;
213
214 /* Retrieve pointer to internal cache.  */
215 extern void *__gconv_get_cache (void);
216
217 /* Retrieve pointer to internal module database.  */
218 extern struct gconv_module *__gconv_get_modules_db (void);
219
220 /* Retrieve pointer to internal alias database.  */
221 extern void *__gconv_get_alias_db (void);
222
223 /* Determine the directories we are looking in.  */
224 extern void __gconv_get_path (void) internal_function;
225
226 /* Comparison function to search alias.  */
227 extern int __gconv_alias_compare (const void *p1, const void *p2)
228      attribute_hidden;
229
230 /* Clear reference to transformation step implementations which might
231    cause the code to be unloaded.  */
232 extern int __gconv_close_transform (struct __gconv_step *steps,
233                                     size_t nsteps)
234      internal_function;
235
236 /* Free all resources allocated for the transformation record when
237    using the cache.  */
238 extern void __gconv_release_cache (struct __gconv_step *steps, size_t nsteps)
239      internal_function;
240
241 /* Load shared object named by NAME.  If already loaded increment reference
242    count.  */
243 extern struct __gconv_loaded_object *__gconv_find_shlib (const char *name)
244      internal_function;
245
246 /* Release shared object.  If no further reference is available unload
247    the object.  */
248 extern void __gconv_release_shlib (struct __gconv_loaded_object *handle)
249      internal_function;
250
251 /* Fill STEP with information about builtin module with NAME.  */
252 extern void __gconv_get_builtin_trans (const char *name,
253                                        struct __gconv_step *step)
254      internal_function;
255
256 /* Try to load transliteration step module.  */
257 extern int __gconv_translit_find (struct trans_struct *trans)
258      internal_function;
259
260 /* Transliteration using the locale's data.  */
261 extern int __gconv_transliterate (struct __gconv_step *step,
262                                   struct __gconv_step_data *step_data,
263                                   void *trans_data,
264                                   __const unsigned char *inbufstart,
265                                   __const unsigned char **inbufp,
266                                   __const unsigned char *inbufend,
267                                   unsigned char **outbufstart,
268                                   size_t *irreversible) attribute_hidden;
269
270
271 /* If NAME is an codeset alias expand it.  */
272 extern int __gconv_compare_alias (const char *name1, const char *name2)
273      internal_function;
274
275
276 /* Builtin transformations.  */
277 #ifdef _LIBC
278 # define __BUILTIN_TRANSFORM(Name) \
279   extern int Name (struct __gconv_step *step,                                 \
280                    struct __gconv_step_data *data,                            \
281                    const unsigned char **inbuf,                               \
282                    const unsigned char *inbufend,                             \
283                    unsigned char **outbufstart, size_t *irreversible,         \
284                    int do_flush, int consume_incomplete)
285
286 __BUILTIN_TRANSFORM (__gconv_transform_ascii_internal);
287 __BUILTIN_TRANSFORM (__gconv_transform_internal_ascii);
288 __BUILTIN_TRANSFORM (__gconv_transform_utf8_internal);
289 __BUILTIN_TRANSFORM (__gconv_transform_internal_utf8);
290 __BUILTIN_TRANSFORM (__gconv_transform_ucs2_internal);
291 __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs2);
292 __BUILTIN_TRANSFORM (__gconv_transform_ucs2reverse_internal);
293 __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs2reverse);
294 __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs4);
295 __BUILTIN_TRANSFORM (__gconv_transform_ucs4_internal);
296 __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs4le);
297 __BUILTIN_TRANSFORM (__gconv_transform_ucs4le_internal);
298 __BUILTIN_TRANSFORM (__gconv_transform_internal_utf16);
299 __BUILTIN_TRANSFORM (__gconv_transform_utf16_internal);
300 # undef __BUITLIN_TRANSFORM
301
302 /* Specialized conversion function for a single byte to INTERNAL, recognizing
303    only ASCII characters.  */
304 extern wint_t __gconv_btwoc_ascii (struct __gconv_step *step, unsigned char c);
305
306 #endif
307
308 __END_DECLS
309
310 #endif /* gconv_int.h */