]>
Commit | Line | Data |
---|---|---|
2b778ceb | 1 | /* Copyright (C) 1997-2021 Free Software Foundation, Inc. |
e62c19f1 UD |
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 | |
41bdb6e2 AJ |
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. | |
e62c19f1 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
e62c19f1 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
e62c19f1 UD |
18 | |
19 | #ifndef _GCONV_INT_H | |
20 | #define _GCONV_INT_H 1 | |
21 | ||
22 | #include "gconv.h" | |
c5738211 | 23 | #include <stdlib.h> /* For alloca used in macro below. */ |
6111babe RM |
24 | #include <ctype.h> /* For __toupper_l used in macro below. */ |
25 | #include <string.h> /* For strlen et al used in macro below. */ | |
ec999b8e | 26 | #include <libc-lock.h> |
e62c19f1 UD |
27 | |
28 | __BEGIN_DECLS | |
29 | ||
30 | ||
9a321276 | 31 | /* Structure for alias definition. Simply two strings. */ |
e62c19f1 UD |
32 | struct gconv_alias |
33 | { | |
17427edd UD |
34 | char *fromname; |
35 | char *toname; | |
e62c19f1 UD |
36 | }; |
37 | ||
38 | ||
0d9f6793 UD |
39 | /* Structure describing one loaded shared object. This normally are |
40 | objects to perform conversation but as a special case the db shared | |
41 | object is also handled. */ | |
d64b6ad0 | 42 | struct __gconv_loaded_object |
0d9f6793 | 43 | { |
b79f74cd | 44 | /* Name of the object. It must be the first structure element. */ |
0d9f6793 UD |
45 | const char *name; |
46 | ||
47 | /* Reference counter for the db functionality. If no conversion is | |
48 | needed we unload the db library. */ | |
49 | int counter; | |
50 | ||
51 | /* The handle for the shared object. */ | |
b3fc5f84 | 52 | void *handle; |
0d9f6793 UD |
53 | |
54 | /* Pointer to the functions the module defines. */ | |
d64b6ad0 UD |
55 | __gconv_fct fct; |
56 | __gconv_init_fct init_fct; | |
57 | __gconv_end_fct end_fct; | |
0d9f6793 UD |
58 | }; |
59 | ||
60 | ||
e62c19f1 UD |
61 | /* Description for an available conversion module. */ |
62 | struct gconv_module | |
63 | { | |
d2dfc5de | 64 | const char *from_string; |
0d9f6793 | 65 | const char *to_string; |
e62c19f1 | 66 | |
fab6d621 UD |
67 | int cost_hi; |
68 | int cost_lo; | |
e62c19f1 | 69 | |
0d9f6793 | 70 | const char *module_name; |
2bd60880 UD |
71 | |
72 | struct gconv_module *left; /* Prefix smaller. */ | |
73 | struct gconv_module *same; /* List of entries with identical prefix. */ | |
2bd60880 | 74 | struct gconv_module *right; /* Prefix larger. */ |
e62c19f1 UD |
75 | }; |
76 | ||
77 | ||
91927b7c AS |
78 | /* The specification of the conversion that needs to be performed. */ |
79 | struct gconv_spec | |
80 | { | |
81 | char *fromcode; | |
82 | char *tocode; | |
83 | bool translit; | |
84 | bool ignore; | |
85 | }; | |
86 | ||
c90a2db6 UD |
87 | /* Flags for `gconv_open'. */ |
88 | enum | |
89 | { | |
90 | GCONV_AVOID_NOCONV = 1 << 0 | |
91 | }; | |
92 | ||
7b503bcc UD |
93 | /* When GCONV_AVOID_NOCONV is set and no conversion is needed, |
94 | __GCONV_NULCONV should be returned. */ | |
95 | enum | |
96 | { | |
97 | __GCONV_NULCONV = -1 | |
98 | }; | |
c90a2db6 | 99 | |
e62c19f1 UD |
100 | /* Global variables. */ |
101 | ||
102 | /* Database of alias names. */ | |
230491f0 | 103 | extern void *__gconv_alias_db attribute_hidden; |
e62c19f1 UD |
104 | |
105 | /* Array with available modules. */ | |
230491f0 | 106 | extern struct gconv_module *__gconv_modules_db attribute_hidden; |
e62c19f1 | 107 | |
6b98979f | 108 | /* Value of the GCONV_PATH environment variable. */ |
aa32f798 | 109 | extern const char *__gconv_path_envvar attribute_hidden; |
6b98979f | 110 | |
9e26f129 | 111 | /* Lock for the conversion database content. */ |
6f8a7dff | 112 | __libc_lock_define (extern, __gconv_lock attribute_hidden) |
9e26f129 | 113 | |
e62c19f1 | 114 | |
17c389fc UD |
115 | /* The gconv functions expects the name to be in upper case and complete, |
116 | including the trailing slashes if necessary. */ | |
323fb88d | 117 | #define norm_add_slashes(str,suffix) \ |
17c389fc UD |
118 | ({ \ |
119 | const char *cp = (str); \ | |
120 | char *result; \ | |
121 | char *tmp; \ | |
122 | size_t cnt = 0; \ | |
db2f05ba | 123 | const size_t suffix_len = strlen (suffix); \ |
17c389fc UD |
124 | \ |
125 | while (*cp != '\0') \ | |
126 | if (*cp++ == '/') \ | |
127 | ++cnt; \ | |
128 | \ | |
c5738211 | 129 | tmp = result = __alloca (cp - (str) + 3 + suffix_len); \ |
17c389fc UD |
130 | cp = (str); \ |
131 | while (*cp != '\0') \ | |
4b5b009c | 132 | *tmp++ = __toupper_l (*cp++, _nl_C_locobj_ptr); \ |
17c389fc UD |
133 | if (cnt < 2) \ |
134 | { \ | |
135 | *tmp++ = '/'; \ | |
136 | if (cnt < 1) \ | |
323fb88d UD |
137 | { \ |
138 | *tmp++ = '/'; \ | |
db2f05ba | 139 | if (suffix_len != 0) \ |
323fb88d UD |
140 | tmp = __mempcpy (tmp, suffix, suffix_len); \ |
141 | } \ | |
17c389fc UD |
142 | } \ |
143 | *tmp = '\0'; \ | |
144 | result; \ | |
145 | }) | |
146 | ||
147 | ||
91927b7c AS |
148 | /* Return in *HANDLE, a decriptor for the transformation. The function expects |
149 | the specification of the transformation in the structure pointed to by | |
150 | CONV_SPEC. It only reads *CONV_SPEC and does not take ownership of it. */ | |
151 | extern int __gconv_open (struct gconv_spec *conv_spec, | |
152 | __gconv_t *handle, int flags); | |
153 | libc_hidden_proto (__gconv_open) | |
e62c19f1 | 154 | |
7d4ec75e AS |
155 | /* This function accepts the charset names of the source and destination of the |
156 | conversion and populates *conv_spec with an equivalent conversion | |
157 | specification that may later be used by __gconv_open. The charset names | |
158 | might contain options in the form of suffixes that alter the conversion, | |
159 | e.g. "ISO-10646/UTF-8/TRANSLIT". It processes the charset names, ignoring | |
160 | and truncating any suffix options in fromcode, and processing and truncating | |
161 | any suffix options in tocode. Supported suffix options ("TRANSLIT" or | |
162 | "IGNORE") when found in tocode lead to the corresponding flag in *conv_spec | |
163 | to be set to true. Unrecognized suffix options are silently discarded. If | |
164 | the function succeeds, it returns conv_spec back to the caller. It returns | |
165 | NULL upon failure. */ | |
166 | extern struct gconv_spec * | |
167 | __gconv_create_spec (struct gconv_spec *conv_spec, const char *fromcode, | |
168 | const char *tocode); | |
169 | libc_hidden_proto (__gconv_create_spec) | |
170 | ||
171 | /* This function frees all heap memory allocated by __gconv_create_spec. */ | |
172 | extern void | |
173 | __gconv_destroy_spec (struct gconv_spec *conv_spec); | |
174 | libc_hidden_proto (__gconv_destroy_spec) | |
175 | ||
e62c19f1 | 176 | /* Free resources associated with transformation descriptor CD. */ |
8bcdb7e0 L |
177 | extern int __gconv_close (__gconv_t cd) |
178 | attribute_hidden; | |
e62c19f1 UD |
179 | |
180 | /* Transform at most *INBYTESLEFT bytes from buffer starting at *INBUF | |
181 | according to rules described by CD and place up to *OUTBYTESLEFT | |
63e04088 | 182 | bytes in buffer starting at *OUTBUF. Return number of non-identical |
38677ace | 183 | conversions in *IRREVERSIBLE if this pointer is not null. */ |
55985355 UD |
184 | extern int __gconv (__gconv_t cd, const unsigned char **inbuf, |
185 | const unsigned char *inbufend, unsigned char **outbuf, | |
8bcdb7e0 L |
186 | unsigned char *outbufend, size_t *irreversible) |
187 | attribute_hidden; | |
e62c19f1 UD |
188 | |
189 | /* Return in *HANDLE a pointer to an array with *NSTEPS elements describing | |
190 | the single steps necessary for transformation from FROMSET to TOSET. */ | |
55985355 UD |
191 | extern int __gconv_find_transform (const char *toset, const char *fromset, |
192 | struct __gconv_step **handle, | |
8bcdb7e0 L |
193 | size_t *nsteps, int flags) |
194 | attribute_hidden; | |
e62c19f1 | 195 | |
6b98979f UD |
196 | /* Search for transformation in cache data. */ |
197 | extern int __gconv_lookup_cache (const char *toset, const char *fromset, | |
198 | struct __gconv_step **handle, size_t *nsteps, | |
8bcdb7e0 L |
199 | int flags) |
200 | attribute_hidden; | |
6b98979f | 201 | |
9a018f6c UD |
202 | /* Compare the two name for whether they are after alias expansion the |
203 | same. This function uses the cache and fails if none is | |
204 | loaded. */ | |
205 | extern int __gconv_compare_alias_cache (const char *name1, const char *name2, | |
8bcdb7e0 L |
206 | int *result) |
207 | attribute_hidden; | |
9a018f6c | 208 | |
6b98979f | 209 | /* Free data associated with a step's structure. */ |
8bcdb7e0 L |
210 | extern void __gconv_release_step (struct __gconv_step *step) |
211 | attribute_hidden; | |
6b98979f | 212 | |
c5288d37 AS |
213 | /* Read all the configuration data and cache it if not done so already. */ |
214 | extern void __gconv_load_conf (void) attribute_hidden; | |
e62c19f1 | 215 | |
6b98979f | 216 | /* Try to read module cache file. */ |
8bcdb7e0 | 217 | extern int __gconv_load_cache (void) attribute_hidden; |
6b98979f | 218 | |
230491f0 UD |
219 | /* Retrieve pointer to internal cache. */ |
220 | extern void *__gconv_get_cache (void); | |
221 | ||
222 | /* Retrieve pointer to internal module database. */ | |
223 | extern struct gconv_module *__gconv_get_modules_db (void); | |
224 | ||
225 | /* Retrieve pointer to internal alias database. */ | |
226 | extern void *__gconv_get_alias_db (void); | |
227 | ||
e62c19f1 | 228 | /* Comparison function to search alias. */ |
dff07c4b UD |
229 | extern int __gconv_alias_compare (const void *p1, const void *p2) |
230 | attribute_hidden; | |
e62c19f1 UD |
231 | |
232 | /* Clear reference to transformation step implementations which might | |
233 | cause the code to be unloaded. */ | |
55985355 | 234 | extern int __gconv_close_transform (struct __gconv_step *steps, |
8bcdb7e0 L |
235 | size_t nsteps) |
236 | attribute_hidden; | |
e62c19f1 | 237 | |
0db59742 UD |
238 | /* Free all resources allocated for the transformation record when |
239 | using the cache. */ | |
8bcdb7e0 L |
240 | extern void __gconv_release_cache (struct __gconv_step *steps, size_t nsteps) |
241 | attribute_hidden; | |
0db59742 | 242 | |
e62c19f1 UD |
243 | /* Load shared object named by NAME. If already loaded increment reference |
244 | count. */ | |
8bcdb7e0 L |
245 | extern struct __gconv_loaded_object *__gconv_find_shlib (const char *name) |
246 | attribute_hidden; | |
e62c19f1 UD |
247 | |
248 | /* Release shared object. If no further reference is available unload | |
249 | the object. */ | |
8bcdb7e0 L |
250 | extern void __gconv_release_shlib (struct __gconv_loaded_object *handle) |
251 | attribute_hidden; | |
e62c19f1 UD |
252 | |
253 | /* Fill STEP with information about builtin module with NAME. */ | |
55985355 | 254 | extern void __gconv_get_builtin_trans (const char *name, |
8bcdb7e0 L |
255 | struct __gconv_step *step) |
256 | attribute_hidden; | |
e62c19f1 | 257 | |
7ac6fad9 FW |
258 | /* Transliteration using the locale's data. */ |
259 | extern int __gconv_transliterate (struct __gconv_step *step, | |
260 | struct __gconv_step_data *step_data, | |
261 | const unsigned char *inbufstart, | |
262 | const unsigned char **inbufp, | |
263 | const unsigned char *inbufend, | |
264 | unsigned char **outbufstart, | |
265 | size_t *irreversible); | |
ba7b4d29 | 266 | libc_hidden_proto (__gconv_transliterate) |
e62c19f1 | 267 | |
dd9423a6 | 268 | /* If NAME is an codeset alias expand it. */ |
8bcdb7e0 L |
269 | extern int __gconv_compare_alias (const char *name1, const char *name2) |
270 | attribute_hidden; | |
dd9423a6 UD |
271 | |
272 | ||
e62c19f1 UD |
273 | /* Builtin transformations. */ |
274 | #ifdef _LIBC | |
f9ad060c | 275 | # define __BUILTIN_TRANSFORM(Name) \ |
55985355 UD |
276 | extern int Name (struct __gconv_step *step, \ |
277 | struct __gconv_step_data *data, \ | |
278 | const unsigned char **inbuf, \ | |
f1d5c60d UD |
279 | const unsigned char *inbufend, \ |
280 | unsigned char **outbufstart, size_t *irreversible, \ | |
281 | int do_flush, int consume_incomplete) | |
e62c19f1 | 282 | |
f9ad060c UD |
283 | __BUILTIN_TRANSFORM (__gconv_transform_ascii_internal); |
284 | __BUILTIN_TRANSFORM (__gconv_transform_internal_ascii); | |
285 | __BUILTIN_TRANSFORM (__gconv_transform_utf8_internal); | |
286 | __BUILTIN_TRANSFORM (__gconv_transform_internal_utf8); | |
287 | __BUILTIN_TRANSFORM (__gconv_transform_ucs2_internal); | |
288 | __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs2); | |
289 | __BUILTIN_TRANSFORM (__gconv_transform_ucs2reverse_internal); | |
290 | __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs2reverse); | |
291 | __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs4); | |
292 | __BUILTIN_TRANSFORM (__gconv_transform_ucs4_internal); | |
293 | __BUILTIN_TRANSFORM (__gconv_transform_internal_ucs4le); | |
294 | __BUILTIN_TRANSFORM (__gconv_transform_ucs4le_internal); | |
295 | __BUILTIN_TRANSFORM (__gconv_transform_internal_utf16); | |
296 | __BUILTIN_TRANSFORM (__gconv_transform_utf16_internal); | |
297 | # undef __BUITLIN_TRANSFORM | |
298 | ||
299 | /* Specialized conversion function for a single byte to INTERNAL, recognizing | |
300 | only ASCII characters. */ | |
301 | extern wint_t __gconv_btwoc_ascii (struct __gconv_step *step, unsigned char c); | |
e62c19f1 | 302 | |
e62c19f1 UD |
303 | #endif |
304 | ||
305 | __END_DECLS | |
306 | ||
307 | #endif /* gconv_int.h */ |