1 /* Copyright (C) 1997-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
19 #define _GCONV_INT_H 1
22 #include <stdlib.h> /* For alloca used in macro below. */
23 #include <ctype.h> /* For __toupper_l used in macro below. */
24 #include <string.h> /* For strlen et al used in macro below. */
25 #include <libc-lock.h>
29 /* We have to provide support for machines which are not able to handled
30 unaligned memory accesses. Some of the character encodings have
31 representations with a fixed width of 2 or 4 bytes. */
34 const struct { uint16_t r; } __attribute__ ((__packed__)) *__ptr \
35 = (__typeof(__ptr))(addr); \
40 const struct { uint32_t r; } __attribute__ ((__packed__)) *__ptr \
41 = (__typeof(__ptr))(addr); \
45 #define put16(addr, val) \
47 struct { uint16_t r; } __attribute__ ((__packed__)) *__ptr \
48 = (__typeof(__ptr))(addr); \
51 #define put32(addr, val) \
53 struct { uint32_t r; } __attribute__ ((__packed__)) *__ptr \
54 = (__typeof(__ptr))(addr); \
58 /* Structure for alias definition. Simply two strings. */
66 /* Structure describing one loaded shared object. This normally are
67 objects to perform conversation but as a special case the db shared
68 object is also handled. */
69 struct __gconv_loaded_object
71 /* Name of the object. It must be the first structure element. */
74 /* Reference counter for the db functionality. If no conversion is
75 needed we unload the db library. */
78 /* The handle for the shared object. */
81 /* Pointer to the functions the module defines. */
83 __gconv_init_fct init_fct
;
84 __gconv_end_fct end_fct
;
88 /* Description for an available conversion module. */
91 const char *from_string
;
92 const char *to_string
;
97 const char *module_name
;
99 struct gconv_module
*left
; /* Prefix smaller. */
100 struct gconv_module
*same
; /* List of entries with identical prefix. */
101 struct gconv_module
*right
; /* Prefix larger. */
105 /* The specification of the conversion that needs to be performed. */
114 /* Flags for `gconv_open'. */
117 GCONV_AVOID_NOCONV
= 1 << 0
120 /* When GCONV_AVOID_NOCONV is set and no conversion is needed,
121 __GCONV_NULCONV should be returned. */
127 /* Global variables. */
129 /* Database of alias names. */
130 extern void *__gconv_alias_db attribute_hidden
;
132 /* Array with available modules. */
133 extern struct gconv_module
*__gconv_modules_db attribute_hidden
;
135 /* Value of the GCONV_PATH environment variable. */
136 extern const char *__gconv_path_envvar attribute_hidden
;
138 /* Lock for the conversion database content. */
139 __libc_lock_define (extern, __gconv_lock attribute_hidden
)
142 /* The gconv functions expects the name to be in upper case and complete,
143 including the trailing slashes if necessary. */
144 #define norm_add_slashes(str,suffix) \
146 const char *cp = (str); \
150 const size_t suffix_len = strlen (suffix); \
152 while (*cp != '\0') \
156 tmp = result = __alloca (cp - (str) + 3 + suffix_len); \
158 while (*cp != '\0') \
159 *tmp++ = __toupper_l (*cp++, _nl_C_locobj_ptr); \
166 if (suffix_len != 0) \
167 tmp = __mempcpy (tmp, suffix, suffix_len); \
175 /* Return in *HANDLE, a decriptor for the transformation. The function expects
176 the specification of the transformation in the structure pointed to by
177 CONV_SPEC. It only reads *CONV_SPEC and does not take ownership of it. */
178 extern int __gconv_open (struct gconv_spec
*conv_spec
,
179 __gconv_t
*handle
, int flags
);
180 libc_hidden_proto (__gconv_open
)
182 /* This function accepts the charset names of the source and destination of the
183 conversion and populates *conv_spec with an equivalent conversion
184 specification that may later be used by __gconv_open. The charset names
185 might contain options in the form of suffixes that alter the conversion,
186 e.g. "ISO-10646/UTF-8/TRANSLIT". It processes the charset names, ignoring
187 and truncating any suffix options in fromcode, and processing and truncating
188 any suffix options in tocode. Supported suffix options ("TRANSLIT" or
189 "IGNORE") when found in tocode lead to the corresponding flag in *conv_spec
190 to be set to true. Unrecognized suffix options are silently discarded. If
191 the function succeeds, it returns conv_spec back to the caller. It returns
192 NULL upon failure. */
193 extern struct gconv_spec
*
194 __gconv_create_spec (struct gconv_spec
*conv_spec
, const char *fromcode
,
196 libc_hidden_proto (__gconv_create_spec
)
198 /* This function frees all heap memory allocated by __gconv_create_spec. */
200 __gconv_destroy_spec (struct gconv_spec
*conv_spec
);
201 libc_hidden_proto (__gconv_destroy_spec
)
203 /* Free resources associated with transformation descriptor CD. */
204 extern int __gconv_close (__gconv_t cd
)
207 /* Transform at most *INBYTESLEFT bytes from buffer starting at *INBUF
208 according to rules described by CD and place up to *OUTBYTESLEFT
209 bytes in buffer starting at *OUTBUF. Return number of non-identical
210 conversions in *IRREVERSIBLE if this pointer is not null. */
211 extern int __gconv (__gconv_t cd
, const unsigned char **inbuf
,
212 const unsigned char *inbufend
, unsigned char **outbuf
,
213 unsigned char *outbufend
, size_t *irreversible
)
216 /* Return in *HANDLE a pointer to an array with *NSTEPS elements describing
217 the single steps necessary for transformation from FROMSET to TOSET. */
218 extern int __gconv_find_transform (const char *toset
, const char *fromset
,
219 struct __gconv_step
**handle
,
220 size_t *nsteps
, int flags
)
223 /* Search for transformation in cache data. */
224 extern int __gconv_lookup_cache (const char *toset
, const char *fromset
,
225 struct __gconv_step
**handle
, size_t *nsteps
,
229 /* Compare the two name for whether they are after alias expansion the
230 same. This function uses the cache and fails if none is
232 extern int __gconv_compare_alias_cache (const char *name1
, const char *name2
,
236 /* Free data associated with a step's structure. */
237 extern void __gconv_release_step (struct __gconv_step
*step
)
240 /* Read all the configuration data and cache it if not done so already. */
241 extern void __gconv_load_conf (void) attribute_hidden
;
243 /* Try to read module cache file. */
244 extern int __gconv_load_cache (void) attribute_hidden
;
246 /* Retrieve pointer to internal cache. */
247 extern void *__gconv_get_cache (void);
249 /* Retrieve pointer to internal module database. */
250 extern struct gconv_module
*__gconv_get_modules_db (void);
252 /* Retrieve pointer to internal alias database. */
253 extern void *__gconv_get_alias_db (void);
255 /* Comparison function to search alias. */
256 extern int __gconv_alias_compare (const void *p1
, const void *p2
)
259 /* Clear reference to transformation step implementations which might
260 cause the code to be unloaded. */
261 extern int __gconv_close_transform (struct __gconv_step
*steps
,
265 /* Free all resources allocated for the transformation record when
267 extern void __gconv_release_cache (struct __gconv_step
*steps
, size_t nsteps
)
270 /* Load shared object named by NAME. If already loaded increment reference
272 extern struct __gconv_loaded_object
*__gconv_find_shlib (const char *name
)
275 /* Release shared object. If no further reference is available unload
277 extern void __gconv_release_shlib (struct __gconv_loaded_object
*handle
)
280 /* Fill STEP with information about builtin module with NAME. */
281 extern void __gconv_get_builtin_trans (const char *name
,
282 struct __gconv_step
*step
)
285 /* Transliteration using the locale's data. */
286 extern int __gconv_transliterate (struct __gconv_step
*step
,
287 struct __gconv_step_data
*step_data
,
288 const unsigned char *inbufstart
,
289 const unsigned char **inbufp
,
290 const unsigned char *inbufend
,
291 unsigned char **outbufstart
,
292 size_t *irreversible
);
293 libc_hidden_proto (__gconv_transliterate
)
295 /* If NAME is an codeset alias expand it. */
296 extern int __gconv_compare_alias (const char *name1
, const char *name2
)
300 /* Builtin transformations. */
302 # define __BUILTIN_TRANSFORM(Name) \
303 extern int Name (struct __gconv_step *step, \
304 struct __gconv_step_data *data, \
305 const unsigned char **inbuf, \
306 const unsigned char *inbufend, \
307 unsigned char **outbufstart, size_t *irreversible, \
308 int do_flush, int consume_incomplete)
310 __BUILTIN_TRANSFORM (__gconv_transform_ascii_internal
);
311 __BUILTIN_TRANSFORM (__gconv_transform_internal_ascii
);
312 __BUILTIN_TRANSFORM (__gconv_transform_utf8_internal
);
313 __BUILTIN_TRANSFORM (__gconv_transform_internal_utf8
);
314 __BUILTIN_TRANSFORM (__gconv_transform_ucs2_internal
);
315 __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs2
);
316 __BUILTIN_TRANSFORM (__gconv_transform_ucs2reverse_internal
);
317 __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs2reverse
);
318 __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs4
);
319 __BUILTIN_TRANSFORM (__gconv_transform_ucs4_internal
);
320 __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs4le
);
321 __BUILTIN_TRANSFORM (__gconv_transform_ucs4le_internal
);
322 __BUILTIN_TRANSFORM (__gconv_transform_internal_utf16
);
323 __BUILTIN_TRANSFORM (__gconv_transform_utf16_internal
);
324 # undef __BUITLIN_TRANSFORM
326 /* Specialized conversion function for a single byte to INTERNAL, recognizing
327 only ASCII characters. */
328 extern wint_t __gconv_btwoc_ascii (struct __gconv_step
*step
, unsigned char c
);
334 #endif /* gconv_int.h */