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