]>
Commit | Line | Data |
---|---|---|
6b98979f | 1 | /* Cache handling for iconv modules. |
2b778ceb | 2 | Copyright (C) 2001-2021 Free Software Foundation, Inc. |
6b98979f UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 2001. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 17 | License along with the GNU C Library; if not, see |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
6b98979f UD |
19 | |
20 | #include <dlfcn.h> | |
2c008571 | 21 | #include <errno.h> |
6b98979f UD |
22 | #include <fcntl.h> |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <unistd.h> | |
26 | #include <sys/mman.h> | |
27 | #include <sys/stat.h> | |
28 | ||
29 | #include <gconv_int.h> | |
30 | #include <iconvconfig.h> | |
2c008571 | 31 | #include <not-cancel.h> |
6b98979f UD |
32 | |
33 | #include "../intl/hash-string.h" | |
34 | ||
230491f0 | 35 | static void *gconv_cache; |
6b98979f UD |
36 | static size_t cache_size; |
37 | static int cache_malloced; | |
38 | ||
39 | ||
230491f0 UD |
40 | void * |
41 | __gconv_get_cache (void) | |
42 | { | |
43 | return gconv_cache; | |
44 | } | |
45 | ||
46 | ||
6b98979f | 47 | int |
6b98979f UD |
48 | __gconv_load_cache (void) |
49 | { | |
50 | int fd; | |
52a5fe70 | 51 | struct __stat64_t64 st; |
6b98979f UD |
52 | struct gconvcache_header *header; |
53 | ||
54 | /* We cannot use the cache if the GCONV_PATH environment variable is | |
55 | set. */ | |
56 | __gconv_path_envvar = getenv ("GCONV_PATH"); | |
57 | if (__gconv_path_envvar != NULL) | |
58 | return -1; | |
59 | ||
60 | /* See whether the cache file exists. */ | |
c2284574 | 61 | fd = __open_nocancel (GCONV_MODULES_CACHE, O_RDONLY, 0); |
6b98979f UD |
62 | if (__builtin_expect (fd, 0) == -1) |
63 | /* Not available. */ | |
64 | return -1; | |
65 | ||
66 | /* Get information about the file. */ | |
52a5fe70 | 67 | if (__glibc_unlikely (__fstat64_time64 (fd, &st) < 0) |
6b98979f UD |
68 | /* We do not have to start looking at the file if it cannot contain |
69 | at least the cache header. */ | |
6dd67bd5 | 70 | || (size_t) st.st_size < sizeof (struct gconvcache_header)) |
6b98979f UD |
71 | { |
72 | close_and_exit: | |
c181840c | 73 | __close_nocancel_nostatus (fd); |
6b98979f UD |
74 | return -1; |
75 | } | |
76 | ||
77 | /* Make the file content available. */ | |
78 | cache_size = st.st_size; | |
79 | #ifdef _POSIX_MAPPED_FILES | |
230491f0 | 80 | gconv_cache = __mmap (NULL, cache_size, PROT_READ, MAP_SHARED, fd, 0); |
a1ffb40e | 81 | if (__glibc_unlikely (gconv_cache == MAP_FAILED)) |
6b98979f UD |
82 | #endif |
83 | { | |
84 | size_t already_read; | |
85 | ||
230491f0 UD |
86 | gconv_cache = malloc (cache_size); |
87 | if (gconv_cache == NULL) | |
6b98979f UD |
88 | goto close_and_exit; |
89 | ||
90 | already_read = 0; | |
91 | do | |
92 | { | |
230491f0 | 93 | ssize_t n = __read (fd, (char *) gconv_cache + already_read, |
6b98979f UD |
94 | cache_size - already_read); |
95 | if (__builtin_expect (n, 0) == -1) | |
96 | { | |
230491f0 UD |
97 | free (gconv_cache); |
98 | gconv_cache = NULL; | |
6b98979f UD |
99 | goto close_and_exit; |
100 | } | |
101 | ||
102 | already_read += n; | |
103 | } | |
104 | while (already_read < cache_size); | |
105 | ||
106 | cache_malloced = 1; | |
107 | } | |
108 | ||
109 | /* We don't need the file descriptor anymore. */ | |
c181840c | 110 | __close_nocancel_nostatus (fd); |
6b98979f UD |
111 | |
112 | /* Check the consistency. */ | |
230491f0 | 113 | header = (struct gconvcache_header *) gconv_cache; |
6b98979f UD |
114 | if (__builtin_expect (header->magic, GCONVCACHE_MAGIC) != GCONVCACHE_MAGIC |
115 | || __builtin_expect (header->string_offset >= cache_size, 0) | |
116 | || __builtin_expect (header->hash_offset >= cache_size, 0) | |
117 | || __builtin_expect (header->hash_size == 0, 0) | |
118 | || __builtin_expect ((header->hash_offset | |
119 | + header->hash_size * sizeof (struct hash_entry)) | |
120 | > cache_size, 0) | |
121 | || __builtin_expect (header->module_offset >= cache_size, 0) | |
122 | || __builtin_expect (header->otherconv_offset > cache_size, 0)) | |
123 | { | |
124 | if (cache_malloced) | |
125 | { | |
230491f0 | 126 | free (gconv_cache); |
6b98979f UD |
127 | cache_malloced = 0; |
128 | } | |
129 | #ifdef _POSIX_MAPPED_FILES | |
130 | else | |
230491f0 | 131 | __munmap (gconv_cache, cache_size); |
6b98979f | 132 | #endif |
230491f0 | 133 | gconv_cache = NULL; |
6b98979f UD |
134 | |
135 | return -1; | |
136 | } | |
137 | ||
138 | /* That worked. */ | |
139 | return 0; | |
140 | } | |
141 | ||
142 | ||
143 | static int | |
6b98979f UD |
144 | find_module_idx (const char *str, size_t *idxp) |
145 | { | |
146 | unsigned int idx; | |
147 | unsigned int hval; | |
148 | unsigned int hval2; | |
149 | const struct gconvcache_header *header; | |
150 | const char *strtab; | |
151 | const struct hash_entry *hashtab; | |
152 | unsigned int limit; | |
153 | ||
230491f0 UD |
154 | header = (const struct gconvcache_header *) gconv_cache; |
155 | strtab = (char *) gconv_cache + header->string_offset; | |
156 | hashtab = (struct hash_entry *) ((char *) gconv_cache | |
9a1f71a7 | 157 | + header->hash_offset); |
6b98979f | 158 | |
dd9423a6 | 159 | hval = __hash_string (str); |
6b98979f UD |
160 | idx = hval % header->hash_size; |
161 | hval2 = 1 + hval % (header->hash_size - 2); | |
162 | ||
163 | limit = cache_size - header->string_offset; | |
164 | while (hashtab[idx].string_offset != 0) | |
165 | if (hashtab[idx].string_offset < limit | |
166 | && strcmp (str, strtab + hashtab[idx].string_offset) == 0) | |
167 | { | |
168 | *idxp = hashtab[idx].module_idx; | |
169 | return 0; | |
170 | } | |
171 | else | |
172 | if ((idx += hval2) >= header->hash_size) | |
173 | idx -= header->hash_size; | |
174 | ||
175 | /* Nothing found. */ | |
176 | return -1; | |
177 | } | |
178 | ||
179 | ||
8e294940 | 180 | #ifndef STATIC_GCONV |
6b98979f | 181 | static int |
6b98979f UD |
182 | find_module (const char *directory, const char *filename, |
183 | struct __gconv_step *result) | |
184 | { | |
185 | size_t dirlen = strlen (directory); | |
186 | size_t fnamelen = strlen (filename) + 1; | |
0db59742 | 187 | char fullname[dirlen + fnamelen]; |
b2e3d177 | 188 | int status = __GCONV_NOCONV; |
6b98979f | 189 | |
6b98979f UD |
190 | memcpy (__mempcpy (fullname, directory, dirlen), filename, fnamelen); |
191 | ||
192 | result->__shlib_handle = __gconv_find_shlib (fullname); | |
b2e3d177 UD |
193 | if (result->__shlib_handle != NULL) |
194 | { | |
195 | status = __GCONV_OK; | |
196 | ||
0db59742 | 197 | result->__modname = NULL; |
b2e3d177 UD |
198 | result->__fct = result->__shlib_handle->fct; |
199 | result->__init_fct = result->__shlib_handle->init_fct; | |
200 | result->__end_fct = result->__shlib_handle->end_fct; | |
6b98979f | 201 | |
f9ad060c UD |
202 | /* These settings can be overridden by the init function. */ |
203 | result->__btowc_fct = NULL; | |
b2e3d177 | 204 | result->__data = NULL; |
f9ad060c UD |
205 | |
206 | /* Call the init function. */ | |
1cf1232c | 207 | __gconv_init_fct init_fct = result->__init_fct; |
aa87e915 | 208 | #ifdef PTR_DEMANGLE |
1cf1232c | 209 | PTR_DEMANGLE (init_fct); |
aa87e915 | 210 | #endif |
1cf1232c PF |
211 | if (init_fct != NULL) |
212 | { | |
aa87e915 UD |
213 | status = DL_CALL_FCT (init_fct, (result)); |
214 | ||
215 | #ifdef PTR_MANGLE | |
1cf1232c | 216 | PTR_MANGLE (result->__btowc_fct); |
aa87e915 UD |
217 | #endif |
218 | } | |
b2e3d177 | 219 | } |
6b98979f | 220 | |
6b98979f UD |
221 | return status; |
222 | } | |
8e294940 | 223 | #endif |
6b98979f UD |
224 | |
225 | ||
97b0f3d3 | 226 | int |
97b0f3d3 UD |
227 | __gconv_compare_alias_cache (const char *name1, const char *name2, int *result) |
228 | { | |
229 | size_t name1_idx; | |
230 | size_t name2_idx; | |
231 | ||
230491f0 | 232 | if (gconv_cache == NULL) |
97b0f3d3 UD |
233 | return -1; |
234 | ||
235 | if (find_module_idx (name1, &name1_idx) != 0 | |
236 | || find_module_idx (name2, &name2_idx) != 0) | |
237 | *result = strcmp (name1, name2); | |
238 | else | |
239 | *result = (int) (name1_idx - name2_idx); | |
240 | ||
241 | return 0; | |
242 | } | |
243 | ||
244 | ||
6b98979f | 245 | int |
6b98979f UD |
246 | __gconv_lookup_cache (const char *toset, const char *fromset, |
247 | struct __gconv_step **handle, size_t *nsteps, int flags) | |
248 | { | |
249 | const struct gconvcache_header *header; | |
250 | const char *strtab; | |
251 | size_t fromidx; | |
252 | size_t toidx; | |
253 | const struct module_entry *modtab; | |
254 | const struct module_entry *from_module; | |
255 | const struct module_entry *to_module; | |
256 | struct __gconv_step *result; | |
257 | ||
230491f0 | 258 | if (gconv_cache == NULL) |
6b98979f UD |
259 | /* We have no cache available. */ |
260 | return __GCONV_NODB; | |
261 | ||
230491f0 UD |
262 | header = (const struct gconvcache_header *) gconv_cache; |
263 | strtab = (char *) gconv_cache + header->string_offset; | |
264 | modtab = (const struct module_entry *) ((char *) gconv_cache | |
6b98979f UD |
265 | + header->module_offset); |
266 | ||
267 | if (find_module_idx (fromset, &fromidx) != 0 | |
268 | || (header->module_offset + (fromidx + 1) * sizeof (struct module_entry) | |
269 | > cache_size)) | |
270 | return __GCONV_NOCONV; | |
271 | from_module = &modtab[fromidx]; | |
272 | ||
273 | if (find_module_idx (toset, &toidx) != 0 | |
274 | || (header->module_offset + (toidx + 1) * sizeof (struct module_entry) | |
275 | > cache_size)) | |
d2a630c3 | 276 | return __GCONV_NOCONV; |
6b98979f UD |
277 | to_module = &modtab[toidx]; |
278 | ||
279 | /* Avoid copy-only transformations if the user requests. */ | |
280 | if (__builtin_expect (flags & GCONV_AVOID_NOCONV, 0) && fromidx == toidx) | |
7b503bcc | 281 | return __GCONV_NULCONV; |
6b98979f UD |
282 | |
283 | /* If there are special conversions available examine them first. */ | |
b2e3d177 UD |
284 | if (fromidx != 0 && toidx != 0 |
285 | && __builtin_expect (from_module->extra_offset, 0) != 0) | |
6b98979f UD |
286 | { |
287 | /* Search through the list to see whether there is a module | |
288 | matching the destination character set. */ | |
289 | const struct extra_entry *extra; | |
290 | ||
291 | /* Note the -1. This is due to the offset added in iconvconfig. | |
292 | See there for more explanations. */ | |
230491f0 | 293 | extra = (const struct extra_entry *) ((char *) gconv_cache |
6b98979f UD |
294 | + header->otherconv_offset |
295 | + from_module->extra_offset - 1); | |
296 | while (extra->module_cnt != 0 | |
297 | && extra->module[extra->module_cnt - 1].outname_offset != toidx) | |
298 | extra = (const struct extra_entry *) ((char *) extra | |
299 | + sizeof (struct extra_entry) | |
300 | + (extra->module_cnt | |
301 | * sizeof (struct extra_entry_module))); | |
302 | ||
303 | if (extra->module_cnt != 0) | |
304 | { | |
305 | /* Use the extra module. First determine how many steps. */ | |
306 | char *fromname; | |
307 | int idx; | |
308 | ||
309 | *nsteps = extra->module_cnt; | |
310 | *handle = result = | |
311 | (struct __gconv_step *) malloc (extra->module_cnt | |
312 | * sizeof (struct __gconv_step)); | |
313 | if (result == NULL) | |
314 | return __GCONV_NOMEM; | |
315 | ||
316 | fromname = (char *) strtab + from_module->canonname_offset; | |
317 | idx = 0; | |
318 | do | |
319 | { | |
320 | result[idx].__from_name = fromname; | |
321 | fromname = result[idx].__to_name = | |
322 | (char *) strtab + modtab[extra->module[idx].outname_offset].canonname_offset; | |
323 | ||
281ebe7c UD |
324 | result[idx].__counter = 1; |
325 | result[idx].__data = NULL; | |
326 | ||
6b98979f UD |
327 | #ifndef STATIC_GCONV |
328 | if (strtab[extra->module[idx].dir_offset] != '\0') | |
329 | { | |
330 | /* Load the module, return handle for it. */ | |
331 | int res; | |
332 | ||
333 | res = find_module (strtab + extra->module[idx].dir_offset, | |
334 | strtab + extra->module[idx].name_offset, | |
335 | &result[idx]); | |
336 | if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK) | |
337 | { | |
338 | /* Something went wrong. */ | |
339 | free (result); | |
340 | goto try_internal; | |
341 | } | |
342 | } | |
343 | else | |
344 | #endif | |
345 | /* It's a builtin transformation. */ | |
346 | __gconv_get_builtin_trans (strtab | |
347 | + extra->module[idx].name_offset, | |
348 | &result[idx]); | |
349 | ||
350 | } | |
351 | while (++idx < extra->module_cnt); | |
352 | ||
353 | return __GCONV_OK; | |
354 | } | |
355 | } | |
356 | ||
357 | try_internal: | |
358 | /* See whether we can convert via the INTERNAL charset. */ | |
b2e3d177 | 359 | if ((fromidx != 0 && __builtin_expect (from_module->fromname_offset, 1) == 0) |
ee6a43e5 UD |
360 | || (toidx != 0 && __builtin_expect (to_module->toname_offset, 1) == 0) |
361 | || (fromidx == 0 && toidx == 0)) | |
6b98979f UD |
362 | /* Not possible. Nothing we can do. */ |
363 | return __GCONV_NOCONV; | |
364 | ||
ee6a43e5 | 365 | /* We will use up to two modules. Always allocate room for two. */ |
6b98979f UD |
366 | result = (struct __gconv_step *) malloc (2 * sizeof (struct __gconv_step)); |
367 | if (result == NULL) | |
368 | return __GCONV_NOMEM; | |
369 | ||
370 | *handle = result; | |
b2e3d177 | 371 | *nsteps = 0; |
6b98979f UD |
372 | |
373 | /* Generate data structure for conversion to INTERNAL. */ | |
b2e3d177 UD |
374 | if (fromidx != 0) |
375 | { | |
376 | result[0].__from_name = (char *) strtab + from_module->canonname_offset; | |
377 | result[0].__to_name = (char *) "INTERNAL"; | |
6b98979f | 378 | |
b2e3d177 UD |
379 | result[0].__counter = 1; |
380 | result[0].__data = NULL; | |
281ebe7c | 381 | |
6b98979f | 382 | #ifndef STATIC_GCONV |
b2e3d177 | 383 | if (strtab[from_module->todir_offset] != '\0') |
6b98979f | 384 | { |
b2e3d177 UD |
385 | /* Load the module, return handle for it. */ |
386 | int res = find_module (strtab + from_module->todir_offset, | |
387 | strtab + from_module->toname_offset, | |
388 | &result[0]); | |
389 | if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK) | |
390 | { | |
391 | /* Something went wrong. */ | |
392 | free (result); | |
393 | return res; | |
394 | } | |
6b98979f | 395 | } |
b2e3d177 | 396 | else |
6b98979f | 397 | #endif |
b2e3d177 UD |
398 | /* It's a builtin transformation. */ |
399 | __gconv_get_builtin_trans (strtab + from_module->toname_offset, | |
400 | &result[0]); | |
401 | ||
402 | ++*nsteps; | |
403 | } | |
6b98979f UD |
404 | |
405 | /* Generate data structure for conversion from INTERNAL. */ | |
b2e3d177 UD |
406 | if (toidx != 0) |
407 | { | |
408 | int idx = *nsteps; | |
6b98979f | 409 | |
b2e3d177 UD |
410 | result[idx].__from_name = (char *) "INTERNAL"; |
411 | result[idx].__to_name = (char *) strtab + to_module->canonname_offset; | |
412 | ||
413 | result[idx].__counter = 1; | |
414 | result[idx].__data = NULL; | |
281ebe7c | 415 | |
6b98979f | 416 | #ifndef STATIC_GCONV |
b2e3d177 | 417 | if (strtab[to_module->fromdir_offset] != '\0') |
6b98979f | 418 | { |
b2e3d177 UD |
419 | /* Load the module, return handle for it. */ |
420 | int res = find_module (strtab + to_module->fromdir_offset, | |
421 | strtab + to_module->fromname_offset, | |
422 | &result[idx]); | |
423 | if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK) | |
424 | { | |
425 | /* Something went wrong. */ | |
426 | if (idx != 0) | |
427 | __gconv_release_step (&result[0]); | |
428 | free (result); | |
429 | return res; | |
430 | } | |
6b98979f | 431 | } |
b2e3d177 | 432 | else |
6b98979f | 433 | #endif |
b2e3d177 UD |
434 | /* It's a builtin transformation. */ |
435 | __gconv_get_builtin_trans (strtab + to_module->fromname_offset, | |
436 | &result[idx]); | |
437 | ||
438 | ++*nsteps; | |
439 | } | |
6b98979f UD |
440 | |
441 | return __GCONV_OK; | |
442 | } | |
443 | ||
444 | ||
0db59742 UD |
445 | /* Free memory allocated for the transformation record. */ |
446 | void | |
0db59742 UD |
447 | __gconv_release_cache (struct __gconv_step *steps, size_t nsteps) |
448 | { | |
0bfddfc9 FW |
449 | if (gconv_cache != NULL) |
450 | /* The only thing we have to deallocate is the record with the | |
451 | steps. */ | |
0db59742 UD |
452 | free (steps); |
453 | } | |
454 | ||
455 | ||
6b98979f | 456 | /* Free all resources if necessary. */ |
c877418f | 457 | libc_freeres_fn (free_mem) |
6b98979f UD |
458 | { |
459 | if (cache_malloced) | |
230491f0 | 460 | free (gconv_cache); |
6b98979f | 461 | #ifdef _POSIX_MAPPED_FILES |
b33026a8 | 462 | else if (gconv_cache != NULL) |
230491f0 | 463 | __munmap (gconv_cache, cache_size); |
6b98979f UD |
464 | #endif |
465 | } |