* stdlib/tst-setcontext.c (main): If makecontext does nothing,
[glibc.git] / iconv / gconv_cache.c
blobf7dca0285a02da97cf5c3a8d34ab3f3985bac678
1 /* Cache handling for iconv modules.
2 Copyright (C) 2001, 2002 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 static void *gconv_cache;
35 static size_t cache_size;
36 static int cache_malloced;
39 void *
40 __gconv_get_cache (void)
42 return gconv_cache;
46 int
47 internal_function
48 __gconv_load_cache (void)
50 int fd;
51 struct stat64 st;
52 struct gconvcache_header *header;
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;
60 /* See whether the cache file exists. */
61 fd = __open (GCONV_MODULES_CACHE, O_RDONLY);
62 if (__builtin_expect (fd, 0) == -1)
63 /* Not available. */
64 return -1;
66 /* Get information about the file. */
67 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0
68 /* We do not have to start looking at the file if it cannot contain
69 at least the cache header. */
70 || (size_t) st.st_size < sizeof (struct gconvcache_header))
72 close_and_exit:
73 __close (fd);
74 return -1;
77 /* Make the file content available. */
78 cache_size = st.st_size;
79 #ifdef _POSIX_MAPPED_FILES
80 gconv_cache = __mmap (NULL, cache_size, PROT_READ, MAP_SHARED, fd, 0);
81 if (__builtin_expect (gconv_cache == MAP_FAILED, 0))
82 #endif
84 size_t already_read;
86 gconv_cache = malloc (cache_size);
87 if (gconv_cache == NULL)
88 goto close_and_exit;
90 already_read = 0;
93 ssize_t n = __read (fd, (char *) gconv_cache + already_read,
94 cache_size - already_read);
95 if (__builtin_expect (n, 0) == -1)
97 free (gconv_cache);
98 gconv_cache = NULL;
99 goto close_and_exit;
102 already_read += n;
104 while (already_read < cache_size);
106 cache_malloced = 1;
109 /* We don't need the file descriptor anymore. */
110 __close (fd);
112 /* Check the consistency. */
113 header = (struct gconvcache_header *) gconv_cache;
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))
124 if (cache_malloced)
126 free (gconv_cache);
127 cache_malloced = 0;
129 #ifdef _POSIX_MAPPED_FILES
130 else
131 __munmap (gconv_cache, cache_size);
132 #endif
133 gconv_cache = NULL;
135 return -1;
138 /* That worked. */
139 return 0;
143 static int
144 internal_function
145 find_module_idx (const char *str, size_t *idxp)
147 unsigned int idx;
148 unsigned int hval;
149 unsigned int hval2;
150 const struct gconvcache_header *header;
151 const char *strtab;
152 const struct hash_entry *hashtab;
153 unsigned int limit;
155 header = (const struct gconvcache_header *) gconv_cache;
156 strtab = (char *) gconv_cache + header->string_offset;
157 hashtab = (struct hash_entry *) ((char *) gconv_cache
158 + header->hash_offset);
160 hval = hash_string (str);
161 idx = hval % header->hash_size;
162 hval2 = 1 + hval % (header->hash_size - 2);
164 limit = cache_size - header->string_offset;
165 while (hashtab[idx].string_offset != 0)
166 if (hashtab[idx].string_offset < limit
167 && strcmp (str, strtab + hashtab[idx].string_offset) == 0)
169 *idxp = hashtab[idx].module_idx;
170 return 0;
172 else
173 if ((idx += hval2) >= header->hash_size)
174 idx -= header->hash_size;
176 /* Nothing found. */
177 return -1;
181 #ifndef STATIC_GCONV
182 static int
183 internal_function
184 find_module (const char *directory, const char *filename,
185 struct __gconv_step *result)
187 size_t dirlen = strlen (directory);
188 size_t fnamelen = strlen (filename) + 1;
189 char fullname[dirlen + fnamelen];
190 int status = __GCONV_NOCONV;
192 memcpy (__mempcpy (fullname, directory, dirlen), filename, fnamelen);
194 result->__shlib_handle = __gconv_find_shlib (fullname);
195 if (result->__shlib_handle != NULL)
197 status = __GCONV_OK;
199 result->__modname = NULL;
200 result->__fct = result->__shlib_handle->fct;
201 result->__init_fct = result->__shlib_handle->init_fct;
202 result->__end_fct = result->__shlib_handle->end_fct;
204 result->__data = NULL;
205 if (result->__init_fct != NULL)
206 status = DL_CALL_FCT (result->__init_fct, (result));
209 return status;
211 #endif
215 internal_function
216 __gconv_compare_alias_cache (const char *name1, const char *name2, int *result)
218 size_t name1_idx;
219 size_t name2_idx;
221 if (gconv_cache == NULL)
222 return -1;
224 if (find_module_idx (name1, &name1_idx) != 0
225 || find_module_idx (name2, &name2_idx) != 0)
226 *result = strcmp (name1, name2);
227 else
228 *result = (int) (name1_idx - name2_idx);
230 return 0;
235 internal_function
236 __gconv_lookup_cache (const char *toset, const char *fromset,
237 struct __gconv_step **handle, size_t *nsteps, int flags)
239 const struct gconvcache_header *header;
240 const char *strtab;
241 size_t fromidx;
242 size_t toidx;
243 const struct module_entry *modtab;
244 const struct module_entry *from_module;
245 const struct module_entry *to_module;
246 struct __gconv_step *result;
248 if (gconv_cache == NULL)
249 /* We have no cache available. */
250 return __GCONV_NODB;
252 header = (const struct gconvcache_header *) gconv_cache;
253 strtab = (char *) gconv_cache + header->string_offset;
254 modtab = (const struct module_entry *) ((char *) gconv_cache
255 + header->module_offset);
257 if (find_module_idx (fromset, &fromidx) != 0
258 || (header->module_offset + (fromidx + 1) * sizeof (struct module_entry)
259 > cache_size))
260 return __GCONV_NOCONV;
261 from_module = &modtab[fromidx];
263 if (find_module_idx (toset, &toidx) != 0
264 || (header->module_offset + (toidx + 1) * sizeof (struct module_entry)
265 > cache_size))
266 return __GCONV_NOCONV;
267 to_module = &modtab[toidx];
269 /* Avoid copy-only transformations if the user requests. */
270 if (__builtin_expect (flags & GCONV_AVOID_NOCONV, 0) && fromidx == toidx)
271 return __GCONV_NOCONV;
273 /* If there are special conversions available examine them first. */
274 if (fromidx != 0 && toidx != 0
275 && __builtin_expect (from_module->extra_offset, 0) != 0)
277 /* Search through the list to see whether there is a module
278 matching the destination character set. */
279 const struct extra_entry *extra;
281 /* Note the -1. This is due to the offset added in iconvconfig.
282 See there for more explanations. */
283 extra = (const struct extra_entry *) ((char *) gconv_cache
284 + header->otherconv_offset
285 + from_module->extra_offset - 1);
286 while (extra->module_cnt != 0
287 && extra->module[extra->module_cnt - 1].outname_offset != toidx)
288 extra = (const struct extra_entry *) ((char *) extra
289 + sizeof (struct extra_entry)
290 + (extra->module_cnt
291 * sizeof (struct extra_entry_module)));
293 if (extra->module_cnt != 0)
295 /* Use the extra module. First determine how many steps. */
296 char *fromname;
297 int idx;
299 *nsteps = extra->module_cnt;
300 *handle = result =
301 (struct __gconv_step *) malloc (extra->module_cnt
302 * sizeof (struct __gconv_step));
303 if (result == NULL)
304 return __GCONV_NOMEM;
306 fromname = (char *) strtab + from_module->canonname_offset;
307 idx = 0;
310 result[idx].__from_name = fromname;
311 fromname = result[idx].__to_name =
312 (char *) strtab + modtab[extra->module[idx].outname_offset].canonname_offset;
314 result[idx].__counter = 1;
315 result[idx].__data = NULL;
317 #ifndef STATIC_GCONV
318 if (strtab[extra->module[idx].dir_offset] != '\0')
320 /* Load the module, return handle for it. */
321 int res;
323 res = find_module (strtab + extra->module[idx].dir_offset,
324 strtab + extra->module[idx].name_offset,
325 &result[idx]);
326 if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
328 /* Something went wrong. */
329 free (result);
330 goto try_internal;
333 else
334 #endif
335 /* It's a builtin transformation. */
336 __gconv_get_builtin_trans (strtab
337 + extra->module[idx].name_offset,
338 &result[idx]);
341 while (++idx < extra->module_cnt);
343 return __GCONV_OK;
347 try_internal:
348 /* See whether we can convert via the INTERNAL charset. */
349 if ((fromidx != 0 && __builtin_expect (from_module->fromname_offset, 1) == 0)
350 || (toidx != 0 && __builtin_expect (to_module->toname_offset, 1) == 0)
351 || (fromidx == 0 && toidx == 0))
352 /* Not possible. Nothing we can do. */
353 return __GCONV_NOCONV;
355 /* We will use up to two modules. Always allocate room for two. */
356 result = (struct __gconv_step *) malloc (2 * sizeof (struct __gconv_step));
357 if (result == NULL)
358 return __GCONV_NOMEM;
360 *handle = result;
361 *nsteps = 0;
363 /* Generate data structure for conversion to INTERNAL. */
364 if (fromidx != 0)
366 result[0].__from_name = (char *) strtab + from_module->canonname_offset;
367 result[0].__to_name = (char *) "INTERNAL";
369 result[0].__counter = 1;
370 result[0].__data = NULL;
372 #ifndef STATIC_GCONV
373 if (strtab[from_module->todir_offset] != '\0')
375 /* Load the module, return handle for it. */
376 int res = find_module (strtab + from_module->todir_offset,
377 strtab + from_module->toname_offset,
378 &result[0]);
379 if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
381 /* Something went wrong. */
382 free (result);
383 return res;
386 else
387 #endif
388 /* It's a builtin transformation. */
389 __gconv_get_builtin_trans (strtab + from_module->toname_offset,
390 &result[0]);
392 ++*nsteps;
395 /* Generate data structure for conversion from INTERNAL. */
396 if (toidx != 0)
398 int idx = *nsteps;
400 result[idx].__from_name = (char *) "INTERNAL";
401 result[idx].__to_name = (char *) strtab + to_module->canonname_offset;
403 result[idx].__counter = 1;
404 result[idx].__data = NULL;
406 #ifndef STATIC_GCONV
407 if (strtab[to_module->fromdir_offset] != '\0')
409 /* Load the module, return handle for it. */
410 int res = find_module (strtab + to_module->fromdir_offset,
411 strtab + to_module->fromname_offset,
412 &result[idx]);
413 if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
415 /* Something went wrong. */
416 if (idx != 0)
417 __gconv_release_step (&result[0]);
418 free (result);
419 return res;
422 else
423 #endif
424 /* It's a builtin transformation. */
425 __gconv_get_builtin_trans (strtab + to_module->fromname_offset,
426 &result[idx]);
428 ++*nsteps;
431 return __GCONV_OK;
435 /* Free memory allocated for the transformation record. */
436 void
437 internal_function
438 __gconv_release_cache (struct __gconv_step *steps, size_t nsteps)
440 if (gconv_cache != NULL)
441 /* The only thing we have to deallocate is the record with the
442 steps. */
443 free (steps);
447 /* Free all resources if necessary. */
448 static void __attribute__ ((unused))
449 free_mem (void)
451 if (cache_malloced)
452 free (gconv_cache);
453 #ifdef _POSIX_MAPPED_FILES
454 else
455 __munmap (gconv_cache, cache_size);
456 #endif
459 text_set_element (__libc_subfreeres, free_mem);