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