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