Update.
[glibc.git] / iconv / gconv_cache.c
bloba2beee06fa5db1c5c01f825c6511a31e283d4e65
1 /* Cache handling for iconv modules.
2 Copyright (C) 2001 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 <fcntl.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/stat.h>
29 #include <gconv_int.h>
30 #include <iconvconfig.h>
32 #include "../intl/hash-string.h"
34 void *__gconv_cache;
35 static size_t cache_size;
36 static int cache_malloced;
39 int
40 internal_function
41 __gconv_load_cache (void)
43 int fd;
44 struct stat64 st;
45 struct gconvcache_header *header;
47 /* We cannot use the cache if the GCONV_PATH environment variable is
48 set. */
49 __gconv_path_envvar = getenv ("GCONV_PATH");
50 if (__gconv_path_envvar != NULL)
51 return -1;
53 /* See whether the cache file exists. */
54 fd = __open (GCONV_MODULES_CACHE, O_RDONLY);
55 if (__builtin_expect (fd, 0) == -1)
56 /* Not available. */
57 return -1;
59 /* Get information about the file. */
60 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0
61 /* We do not have to start looking at the file if it cannot contain
62 at least the cache header. */
63 || st.st_size < sizeof (struct gconvcache_header))
65 close_and_exit:
66 __close (fd);
67 return -1;
70 /* Make the file content available. */
71 cache_size = st.st_size;
72 #ifdef _POSIX_MAPPED_FILES
73 __gconv_cache = __mmap (NULL, cache_size, PROT_READ, MAP_SHARED, fd, 0);
74 if (__builtin_expect (__gconv_cache == MAP_FAILED, 0))
75 #endif
77 size_t already_read;
79 __gconv_cache = malloc (cache_size);
80 if (__gconv_cache == NULL)
81 goto close_and_exit;
83 already_read = 0;
86 ssize_t n = __read (fd, (char *) __gconv_cache + already_read,
87 cache_size - already_read);
88 if (__builtin_expect (n, 0) == -1)
90 free (__gconv_cache);
91 __gconv_cache = NULL;
92 goto close_and_exit;
95 already_read += n;
97 while (already_read < cache_size);
99 cache_malloced = 1;
102 /* We don't need the file descriptor anymore. */
103 __close (fd);
105 /* Check the consistency. */
106 header = (struct gconvcache_header *) __gconv_cache;
107 if (__builtin_expect (header->magic, GCONVCACHE_MAGIC) != GCONVCACHE_MAGIC
108 || __builtin_expect (header->string_offset >= cache_size, 0)
109 || __builtin_expect (header->hash_offset >= cache_size, 0)
110 || __builtin_expect (header->hash_size == 0, 0)
111 || __builtin_expect ((header->hash_offset
112 + header->hash_size * sizeof (struct hash_entry))
113 > cache_size, 0)
114 || __builtin_expect (header->module_offset >= cache_size, 0)
115 || __builtin_expect (header->otherconv_offset > cache_size, 0))
117 if (cache_malloced)
119 free (__gconv_cache);
120 cache_malloced = 0;
122 #ifdef _POSIX_MAPPED_FILES
123 else
124 __munmap (__gconv_cache, cache_size);
125 #endif
126 __gconv_cache = NULL;
128 return -1;
131 /* That worked. */
132 return 0;
136 static int
137 internal_function
138 find_module_idx (const char *str, size_t *idxp)
140 unsigned int idx;
141 unsigned int hval;
142 unsigned int hval2;
143 const struct gconvcache_header *header;
144 const char *strtab;
145 const struct hash_entry *hashtab;
146 unsigned int limit;
148 header = (const struct gconvcache_header *) __gconv_cache;
149 strtab = (char *) __gconv_cache + header->string_offset;
150 hashtab = (struct hash_entry *) ((char *) __gconv_cache
151 + header->hash_offset);
153 hval = hash_string (str);
154 idx = hval % header->hash_size;
155 hval2 = 1 + hval % (header->hash_size - 2);
157 limit = cache_size - header->string_offset;
158 while (hashtab[idx].string_offset != 0)
159 if (hashtab[idx].string_offset < limit
160 && strcmp (str, strtab + hashtab[idx].string_offset) == 0)
162 *idxp = hashtab[idx].module_idx;
163 return 0;
165 else
166 if ((idx += hval2) >= header->hash_size)
167 idx -= header->hash_size;
169 /* Nothing found. */
170 return -1;
174 #ifndef STATIC_GCONV
175 static int
176 internal_function
177 find_module (const char *directory, const char *filename,
178 struct __gconv_step *result)
180 size_t dirlen = strlen (directory);
181 size_t fnamelen = strlen (filename) + 1;
182 char fullname[dirlen + fnamelen];
183 int status = __GCONV_NOCONV;
185 memcpy (__mempcpy (fullname, directory, dirlen), filename, fnamelen);
187 result->__shlib_handle = __gconv_find_shlib (fullname);
188 if (result->__shlib_handle != NULL)
190 status = __GCONV_OK;
192 result->__modname = NULL;
193 result->__fct = result->__shlib_handle->fct;
194 result->__init_fct = result->__shlib_handle->init_fct;
195 result->__end_fct = result->__shlib_handle->end_fct;
197 result->__data = NULL;
198 if (result->__init_fct != NULL)
199 status = DL_CALL_FCT (result->__init_fct, (result));
202 return status;
204 #endif
208 internal_function
209 __gconv_compare_alias_cache (const char *name1, const char *name2, int *result)
211 size_t name1_idx;
212 size_t name2_idx;
214 if (__gconv_cache == NULL)
215 return -1;
217 if (find_module_idx (name1, &name1_idx) != 0
218 || find_module_idx (name2, &name2_idx) != 0)
219 *result = strcmp (name1, name2);
220 else
221 *result = (int) (name1_idx - name2_idx);
223 return 0;
228 internal_function
229 __gconv_lookup_cache (const char *toset, const char *fromset,
230 struct __gconv_step **handle, size_t *nsteps, int flags)
232 const struct gconvcache_header *header;
233 const char *strtab;
234 size_t fromidx;
235 size_t toidx;
236 const struct module_entry *modtab;
237 const struct module_entry *from_module;
238 const struct module_entry *to_module;
239 struct __gconv_step *result;
241 if (__gconv_cache == NULL)
242 /* We have no cache available. */
243 return __GCONV_NODB;
245 header = (const struct gconvcache_header *) __gconv_cache;
246 strtab = (char *) __gconv_cache + header->string_offset;
247 modtab = (const struct module_entry *) ((char *) __gconv_cache
248 + header->module_offset);
250 if (find_module_idx (fromset, &fromidx) != 0
251 || (header->module_offset + (fromidx + 1) * sizeof (struct module_entry)
252 > cache_size))
253 return __GCONV_NOCONV;
254 from_module = &modtab[fromidx];
256 if (find_module_idx (toset, &toidx) != 0
257 || (header->module_offset + (toidx + 1) * sizeof (struct module_entry)
258 > cache_size))
259 return __GCONV_NOCONV;
260 to_module = &modtab[toidx];
262 /* Avoid copy-only transformations if the user requests. */
263 if (__builtin_expect (flags & GCONV_AVOID_NOCONV, 0) && fromidx == toidx)
264 return __GCONV_NOCONV;
266 /* If there are special conversions available examine them first. */
267 if (fromidx != 0 && toidx != 0
268 && __builtin_expect (from_module->extra_offset, 0) != 0)
270 /* Search through the list to see whether there is a module
271 matching the destination character set. */
272 const struct extra_entry *extra;
274 /* Note the -1. This is due to the offset added in iconvconfig.
275 See there for more explanations. */
276 extra = (const struct extra_entry *) ((char *) __gconv_cache
277 + header->otherconv_offset
278 + from_module->extra_offset - 1);
279 while (extra->module_cnt != 0
280 && extra->module[extra->module_cnt - 1].outname_offset != toidx)
281 extra = (const struct extra_entry *) ((char *) extra
282 + sizeof (struct extra_entry)
283 + (extra->module_cnt
284 * sizeof (struct extra_entry_module)));
286 if (extra->module_cnt != 0)
288 /* Use the extra module. First determine how many steps. */
289 char *fromname;
290 int idx;
292 *nsteps = extra->module_cnt;
293 *handle = result =
294 (struct __gconv_step *) malloc (extra->module_cnt
295 * sizeof (struct __gconv_step));
296 if (result == NULL)
297 return __GCONV_NOMEM;
299 fromname = (char *) strtab + from_module->canonname_offset;
300 idx = 0;
303 result[idx].__from_name = fromname;
304 fromname = result[idx].__to_name =
305 (char *) strtab + modtab[extra->module[idx].outname_offset].canonname_offset;
307 result[idx].__counter = 1;
308 result[idx].__data = NULL;
310 #ifndef STATIC_GCONV
311 if (strtab[extra->module[idx].dir_offset] != '\0')
313 /* Load the module, return handle for it. */
314 int res;
316 res = find_module (strtab + extra->module[idx].dir_offset,
317 strtab + extra->module[idx].name_offset,
318 &result[idx]);
319 if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
321 /* Something went wrong. */
322 free (result);
323 goto try_internal;
326 else
327 #endif
328 /* It's a builtin transformation. */
329 __gconv_get_builtin_trans (strtab
330 + extra->module[idx].name_offset,
331 &result[idx]);
334 while (++idx < extra->module_cnt);
336 return __GCONV_OK;
340 try_internal:
341 /* See whether we can convert via the INTERNAL charset. */
342 if ((fromidx != 0 && __builtin_expect (from_module->fromname_offset, 1) == 0)
343 || (toidx != 0 && __builtin_expect (to_module->toname_offset, 1) == 0)
344 || (fromidx == 0 && toidx == 0))
345 /* Not possible. Nothing we can do. */
346 return __GCONV_NOCONV;
348 /* We will use up to two modules. Always allocate room for two. */
349 result = (struct __gconv_step *) malloc (2 * sizeof (struct __gconv_step));
350 if (result == NULL)
351 return __GCONV_NOMEM;
353 *handle = result;
354 *nsteps = 0;
356 /* Generate data structure for conversion to INTERNAL. */
357 if (fromidx != 0)
359 result[0].__from_name = (char *) strtab + from_module->canonname_offset;
360 result[0].__to_name = (char *) "INTERNAL";
362 result[0].__counter = 1;
363 result[0].__data = NULL;
365 #ifndef STATIC_GCONV
366 if (strtab[from_module->todir_offset] != '\0')
368 /* Load the module, return handle for it. */
369 int res = find_module (strtab + from_module->todir_offset,
370 strtab + from_module->toname_offset,
371 &result[0]);
372 if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
374 /* Something went wrong. */
375 free (result);
376 return res;
379 else
380 #endif
381 /* It's a builtin transformation. */
382 __gconv_get_builtin_trans (strtab + from_module->toname_offset,
383 &result[0]);
385 ++*nsteps;
388 /* Generate data structure for conversion from INTERNAL. */
389 if (toidx != 0)
391 int idx = *nsteps;
393 result[idx].__from_name = (char *) "INTERNAL";
394 result[idx].__to_name = (char *) strtab + to_module->canonname_offset;
396 result[idx].__counter = 1;
397 result[idx].__data = NULL;
399 #ifndef STATIC_GCONV
400 if (strtab[to_module->fromdir_offset] != '\0')
402 /* Load the module, return handle for it. */
403 int res = find_module (strtab + to_module->fromdir_offset,
404 strtab + to_module->fromname_offset,
405 &result[idx]);
406 if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
408 /* Something went wrong. */
409 if (idx != 0)
410 __gconv_release_step (&result[0]);
411 free (result);
412 return res;
415 else
416 #endif
417 /* It's a builtin transformation. */
418 __gconv_get_builtin_trans (strtab + to_module->fromname_offset,
419 &result[idx]);
421 ++*nsteps;
424 return __GCONV_OK;
428 /* Free memory allocated for the transformation record. */
429 void
430 internal_function
431 __gconv_release_cache (struct __gconv_step *steps, size_t nsteps)
433 if (__gconv_cache != NULL)
434 /* The only thing we have to deallocate is the record with the
435 steps. */
436 free (steps);
440 /* Free all resources if necessary. */
441 static void __attribute__ ((unused))
442 free_mem (void)
444 if (cache_malloced)
445 free (__gconv_cache);
446 #ifdef _POSIX_MAPPED_FILES
447 else
448 __munmap (__gconv_cache, cache_size);
449 #endif
452 text_set_element (__libc_subfreeres, free_mem);