.
[glibc/history.git] / iconv / gconv_cache.c
blob716c384de2dd1a835e18f2748af1723906ea9f04
1 /* Cache handling for iconv modules.
2 Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2001.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <dlfcn.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/mman.h>
28 #include <sys/stat.h>
30 #include <gconv_int.h>
31 #include <iconvconfig.h>
32 #include <not-cancel.h>
34 #include "../intl/hash-string.h"
36 static void *gconv_cache;
37 static size_t cache_size;
38 static int cache_malloced;
41 void *
42 __gconv_get_cache (void)
44 return gconv_cache;
48 int
49 internal_function
50 __gconv_load_cache (void)
52 int fd;
53 struct stat64 st;
54 struct gconvcache_header *header;
56 /* We cannot use the cache if the GCONV_PATH environment variable is
57 set. */
58 __gconv_path_envvar = getenv ("GCONV_PATH");
59 if (__gconv_path_envvar != NULL)
60 return -1;
62 /* See whether the cache file exists. */
63 fd = open_not_cancel (GCONV_MODULES_CACHE, O_RDONLY, 0);
64 if (__builtin_expect (fd, 0) == -1)
65 /* Not available. */
66 return -1;
68 /* Get information about the file. */
69 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0
70 /* We do not have to start looking at the file if it cannot contain
71 at least the cache header. */
72 || (size_t) st.st_size < sizeof (struct gconvcache_header))
74 close_and_exit:
75 close_not_cancel_no_status (fd);
76 return -1;
79 /* Make the file content available. */
80 cache_size = st.st_size;
81 #ifdef _POSIX_MAPPED_FILES
82 gconv_cache = __mmap (NULL, cache_size, PROT_READ, MAP_SHARED, fd, 0);
83 if (__builtin_expect (gconv_cache == MAP_FAILED, 0))
84 #endif
86 size_t already_read;
88 gconv_cache = malloc (cache_size);
89 if (gconv_cache == NULL)
90 goto close_and_exit;
92 already_read = 0;
95 ssize_t n = __read (fd, (char *) gconv_cache + already_read,
96 cache_size - already_read);
97 if (__builtin_expect (n, 0) == -1)
99 free (gconv_cache);
100 gconv_cache = NULL;
101 goto close_and_exit;
104 already_read += n;
106 while (already_read < cache_size);
108 cache_malloced = 1;
111 /* We don't need the file descriptor anymore. */
112 close_not_cancel_no_status (fd);
114 /* Check the consistency. */
115 header = (struct gconvcache_header *) gconv_cache;
116 if (__builtin_expect (header->magic, GCONVCACHE_MAGIC) != GCONVCACHE_MAGIC
117 || __builtin_expect (header->string_offset >= cache_size, 0)
118 || __builtin_expect (header->hash_offset >= cache_size, 0)
119 || __builtin_expect (header->hash_size == 0, 0)
120 || __builtin_expect ((header->hash_offset
121 + header->hash_size * sizeof (struct hash_entry))
122 > cache_size, 0)
123 || __builtin_expect (header->module_offset >= cache_size, 0)
124 || __builtin_expect (header->otherconv_offset > cache_size, 0))
126 if (cache_malloced)
128 free (gconv_cache);
129 cache_malloced = 0;
131 #ifdef _POSIX_MAPPED_FILES
132 else
133 __munmap (gconv_cache, cache_size);
134 #endif
135 gconv_cache = NULL;
137 return -1;
140 /* That worked. */
141 return 0;
145 static int
146 internal_function
147 find_module_idx (const char *str, size_t *idxp)
149 unsigned int idx;
150 unsigned int hval;
151 unsigned int hval2;
152 const struct gconvcache_header *header;
153 const char *strtab;
154 const struct hash_entry *hashtab;
155 unsigned int limit;
157 header = (const struct gconvcache_header *) gconv_cache;
158 strtab = (char *) gconv_cache + header->string_offset;
159 hashtab = (struct hash_entry *) ((char *) gconv_cache
160 + header->hash_offset);
162 hval = __hash_string (str);
163 idx = hval % header->hash_size;
164 hval2 = 1 + hval % (header->hash_size - 2);
166 limit = cache_size - header->string_offset;
167 while (hashtab[idx].string_offset != 0)
168 if (hashtab[idx].string_offset < limit
169 && strcmp (str, strtab + hashtab[idx].string_offset) == 0)
171 *idxp = hashtab[idx].module_idx;
172 return 0;
174 else
175 if ((idx += hval2) >= header->hash_size)
176 idx -= header->hash_size;
178 /* Nothing found. */
179 return -1;
183 #ifndef STATIC_GCONV
184 static int
185 internal_function
186 find_module (const char *directory, const char *filename,
187 struct __gconv_step *result)
189 size_t dirlen = strlen (directory);
190 size_t fnamelen = strlen (filename) + 1;
191 char fullname[dirlen + fnamelen];
192 int status = __GCONV_NOCONV;
194 memcpy (__mempcpy (fullname, directory, dirlen), filename, fnamelen);
196 result->__shlib_handle = __gconv_find_shlib (fullname);
197 if (result->__shlib_handle != NULL)
199 status = __GCONV_OK;
201 result->__modname = NULL;
202 result->__fct = result->__shlib_handle->fct;
203 result->__init_fct = result->__shlib_handle->init_fct;
204 result->__end_fct = result->__shlib_handle->end_fct;
206 /* These settings can be overridden by the init function. */
207 result->__btowc_fct = NULL;
208 result->__data = NULL;
210 /* Call the init function. */
211 if (result->__init_fct != NULL)
213 __gconv_init_fct init_fct = result->__init_fct;
214 #ifdef PTR_DEMANGLE
215 PTR_DEMANGLE (init_fct);
216 #endif
217 status = DL_CALL_FCT (init_fct, (result));
219 #ifdef PTR_MANGLE
220 if (result->__btowc_fct != NULL)
221 PTR_MANGLE (result->__btowc_fct);
222 #endif
226 return status;
228 #endif
232 internal_function
233 __gconv_compare_alias_cache (const char *name1, const char *name2, int *result)
235 size_t name1_idx;
236 size_t name2_idx;
238 if (gconv_cache == NULL)
239 return -1;
241 if (find_module_idx (name1, &name1_idx) != 0
242 || find_module_idx (name2, &name2_idx) != 0)
243 *result = strcmp (name1, name2);
244 else
245 *result = (int) (name1_idx - name2_idx);
247 return 0;
252 internal_function
253 __gconv_lookup_cache (const char *toset, const char *fromset,
254 struct __gconv_step **handle, size_t *nsteps, int flags)
256 const struct gconvcache_header *header;
257 const char *strtab;
258 size_t fromidx;
259 size_t toidx;
260 const struct module_entry *modtab;
261 const struct module_entry *from_module;
262 const struct module_entry *to_module;
263 struct __gconv_step *result;
265 if (gconv_cache == NULL)
266 /* We have no cache available. */
267 return __GCONV_NODB;
269 header = (const struct gconvcache_header *) gconv_cache;
270 strtab = (char *) gconv_cache + header->string_offset;
271 modtab = (const struct module_entry *) ((char *) gconv_cache
272 + header->module_offset);
274 if (find_module_idx (fromset, &fromidx) != 0
275 || (header->module_offset + (fromidx + 1) * sizeof (struct module_entry)
276 > cache_size))
277 return __GCONV_NOCONV;
278 from_module = &modtab[fromidx];
280 if (find_module_idx (toset, &toidx) != 0
281 || (header->module_offset + (toidx + 1) * sizeof (struct module_entry)
282 > cache_size))
283 return __GCONV_NOCONV;
284 to_module = &modtab[toidx];
286 /* Avoid copy-only transformations if the user requests. */
287 if (__builtin_expect (flags & GCONV_AVOID_NOCONV, 0) && fromidx == toidx)
288 return __GCONV_NOCONV;
290 /* If there are special conversions available examine them first. */
291 if (fromidx != 0 && toidx != 0
292 && __builtin_expect (from_module->extra_offset, 0) != 0)
294 /* Search through the list to see whether there is a module
295 matching the destination character set. */
296 const struct extra_entry *extra;
298 /* Note the -1. This is due to the offset added in iconvconfig.
299 See there for more explanations. */
300 extra = (const struct extra_entry *) ((char *) gconv_cache
301 + header->otherconv_offset
302 + from_module->extra_offset - 1);
303 while (extra->module_cnt != 0
304 && extra->module[extra->module_cnt - 1].outname_offset != toidx)
305 extra = (const struct extra_entry *) ((char *) extra
306 + sizeof (struct extra_entry)
307 + (extra->module_cnt
308 * sizeof (struct extra_entry_module)));
310 if (extra->module_cnt != 0)
312 /* Use the extra module. First determine how many steps. */
313 char *fromname;
314 int idx;
316 *nsteps = extra->module_cnt;
317 *handle = result =
318 (struct __gconv_step *) malloc (extra->module_cnt
319 * sizeof (struct __gconv_step));
320 if (result == NULL)
321 return __GCONV_NOMEM;
323 fromname = (char *) strtab + from_module->canonname_offset;
324 idx = 0;
327 result[idx].__from_name = fromname;
328 fromname = result[idx].__to_name =
329 (char *) strtab + modtab[extra->module[idx].outname_offset].canonname_offset;
331 result[idx].__counter = 1;
332 result[idx].__data = NULL;
334 #ifndef STATIC_GCONV
335 if (strtab[extra->module[idx].dir_offset] != '\0')
337 /* Load the module, return handle for it. */
338 int res;
340 res = find_module (strtab + extra->module[idx].dir_offset,
341 strtab + extra->module[idx].name_offset,
342 &result[idx]);
343 if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
345 /* Something went wrong. */
346 free (result);
347 goto try_internal;
350 else
351 #endif
352 /* It's a builtin transformation. */
353 __gconv_get_builtin_trans (strtab
354 + extra->module[idx].name_offset,
355 &result[idx]);
358 while (++idx < extra->module_cnt);
360 return __GCONV_OK;
364 try_internal:
365 /* See whether we can convert via the INTERNAL charset. */
366 if ((fromidx != 0 && __builtin_expect (from_module->fromname_offset, 1) == 0)
367 || (toidx != 0 && __builtin_expect (to_module->toname_offset, 1) == 0)
368 || (fromidx == 0 && toidx == 0))
369 /* Not possible. Nothing we can do. */
370 return __GCONV_NOCONV;
372 /* We will use up to two modules. Always allocate room for two. */
373 result = (struct __gconv_step *) malloc (2 * sizeof (struct __gconv_step));
374 if (result == NULL)
375 return __GCONV_NOMEM;
377 *handle = result;
378 *nsteps = 0;
380 /* Generate data structure for conversion to INTERNAL. */
381 if (fromidx != 0)
383 result[0].__from_name = (char *) strtab + from_module->canonname_offset;
384 result[0].__to_name = (char *) "INTERNAL";
386 result[0].__counter = 1;
387 result[0].__data = NULL;
389 #ifndef STATIC_GCONV
390 if (strtab[from_module->todir_offset] != '\0')
392 /* Load the module, return handle for it. */
393 int res = find_module (strtab + from_module->todir_offset,
394 strtab + from_module->toname_offset,
395 &result[0]);
396 if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
398 /* Something went wrong. */
399 free (result);
400 return res;
403 else
404 #endif
405 /* It's a builtin transformation. */
406 __gconv_get_builtin_trans (strtab + from_module->toname_offset,
407 &result[0]);
409 ++*nsteps;
412 /* Generate data structure for conversion from INTERNAL. */
413 if (toidx != 0)
415 int idx = *nsteps;
417 result[idx].__from_name = (char *) "INTERNAL";
418 result[idx].__to_name = (char *) strtab + to_module->canonname_offset;
420 result[idx].__counter = 1;
421 result[idx].__data = NULL;
423 #ifndef STATIC_GCONV
424 if (strtab[to_module->fromdir_offset] != '\0')
426 /* Load the module, return handle for it. */
427 int res = find_module (strtab + to_module->fromdir_offset,
428 strtab + to_module->fromname_offset,
429 &result[idx]);
430 if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
432 /* Something went wrong. */
433 if (idx != 0)
434 __gconv_release_step (&result[0]);
435 free (result);
436 return res;
439 else
440 #endif
441 /* It's a builtin transformation. */
442 __gconv_get_builtin_trans (strtab + to_module->fromname_offset,
443 &result[idx]);
445 ++*nsteps;
448 return __GCONV_OK;
452 /* Free memory allocated for the transformation record. */
453 void
454 internal_function
455 __gconv_release_cache (struct __gconv_step *steps, size_t nsteps)
457 if (gconv_cache != NULL)
458 /* The only thing we have to deallocate is the record with the
459 steps. */
460 free (steps);
464 /* Free all resources if necessary. */
465 libc_freeres_fn (free_mem)
467 if (cache_malloced)
468 free (gconv_cache);
469 #ifdef _POSIX_MAPPED_FILES
470 else if (gconv_cache != NULL)
471 __munmap (gconv_cache, cache_size);
472 #endif