(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / iconv / gconv_cache.c
blob9b695c377d7380176f1aafb24059c0b035430ad7
1 /* Cache handling for iconv modules.
2 Copyright (C) 2001, 2002, 2003 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)
212 status = DL_CALL_FCT (result->__init_fct, (result));
215 return status;
217 #endif
221 internal_function
222 __gconv_compare_alias_cache (const char *name1, const char *name2, int *result)
224 size_t name1_idx;
225 size_t name2_idx;
227 if (gconv_cache == NULL)
228 return -1;
230 if (find_module_idx (name1, &name1_idx) != 0
231 || find_module_idx (name2, &name2_idx) != 0)
232 *result = strcmp (name1, name2);
233 else
234 *result = (int) (name1_idx - name2_idx);
236 return 0;
241 internal_function
242 __gconv_lookup_cache (const char *toset, const char *fromset,
243 struct __gconv_step **handle, size_t *nsteps, int flags)
245 const struct gconvcache_header *header;
246 const char *strtab;
247 size_t fromidx;
248 size_t toidx;
249 const struct module_entry *modtab;
250 const struct module_entry *from_module;
251 const struct module_entry *to_module;
252 struct __gconv_step *result;
254 if (gconv_cache == NULL)
255 /* We have no cache available. */
256 return __GCONV_NODB;
258 header = (const struct gconvcache_header *) gconv_cache;
259 strtab = (char *) gconv_cache + header->string_offset;
260 modtab = (const struct module_entry *) ((char *) gconv_cache
261 + header->module_offset);
263 if (find_module_idx (fromset, &fromidx) != 0
264 || (header->module_offset + (fromidx + 1) * sizeof (struct module_entry)
265 > cache_size))
266 return __GCONV_NOCONV;
267 from_module = &modtab[fromidx];
269 if (find_module_idx (toset, &toidx) != 0
270 || (header->module_offset + (toidx + 1) * sizeof (struct module_entry)
271 > cache_size))
272 return __GCONV_NOCONV;
273 to_module = &modtab[toidx];
275 /* Avoid copy-only transformations if the user requests. */
276 if (__builtin_expect (flags & GCONV_AVOID_NOCONV, 0) && fromidx == toidx)
277 return __GCONV_NOCONV;
279 /* If there are special conversions available examine them first. */
280 if (fromidx != 0 && toidx != 0
281 && __builtin_expect (from_module->extra_offset, 0) != 0)
283 /* Search through the list to see whether there is a module
284 matching the destination character set. */
285 const struct extra_entry *extra;
287 /* Note the -1. This is due to the offset added in iconvconfig.
288 See there for more explanations. */
289 extra = (const struct extra_entry *) ((char *) gconv_cache
290 + header->otherconv_offset
291 + from_module->extra_offset - 1);
292 while (extra->module_cnt != 0
293 && extra->module[extra->module_cnt - 1].outname_offset != toidx)
294 extra = (const struct extra_entry *) ((char *) extra
295 + sizeof (struct extra_entry)
296 + (extra->module_cnt
297 * sizeof (struct extra_entry_module)));
299 if (extra->module_cnt != 0)
301 /* Use the extra module. First determine how many steps. */
302 char *fromname;
303 int idx;
305 *nsteps = extra->module_cnt;
306 *handle = result =
307 (struct __gconv_step *) malloc (extra->module_cnt
308 * sizeof (struct __gconv_step));
309 if (result == NULL)
310 return __GCONV_NOMEM;
312 fromname = (char *) strtab + from_module->canonname_offset;
313 idx = 0;
316 result[idx].__from_name = fromname;
317 fromname = result[idx].__to_name =
318 (char *) strtab + modtab[extra->module[idx].outname_offset].canonname_offset;
320 result[idx].__counter = 1;
321 result[idx].__data = NULL;
323 #ifndef STATIC_GCONV
324 if (strtab[extra->module[idx].dir_offset] != '\0')
326 /* Load the module, return handle for it. */
327 int res;
329 res = find_module (strtab + extra->module[idx].dir_offset,
330 strtab + extra->module[idx].name_offset,
331 &result[idx]);
332 if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
334 /* Something went wrong. */
335 free (result);
336 goto try_internal;
339 else
340 #endif
341 /* It's a builtin transformation. */
342 __gconv_get_builtin_trans (strtab
343 + extra->module[idx].name_offset,
344 &result[idx]);
347 while (++idx < extra->module_cnt);
349 return __GCONV_OK;
353 try_internal:
354 /* See whether we can convert via the INTERNAL charset. */
355 if ((fromidx != 0 && __builtin_expect (from_module->fromname_offset, 1) == 0)
356 || (toidx != 0 && __builtin_expect (to_module->toname_offset, 1) == 0)
357 || (fromidx == 0 && toidx == 0))
358 /* Not possible. Nothing we can do. */
359 return __GCONV_NOCONV;
361 /* We will use up to two modules. Always allocate room for two. */
362 result = (struct __gconv_step *) malloc (2 * sizeof (struct __gconv_step));
363 if (result == NULL)
364 return __GCONV_NOMEM;
366 *handle = result;
367 *nsteps = 0;
369 /* Generate data structure for conversion to INTERNAL. */
370 if (fromidx != 0)
372 result[0].__from_name = (char *) strtab + from_module->canonname_offset;
373 result[0].__to_name = (char *) "INTERNAL";
375 result[0].__counter = 1;
376 result[0].__data = NULL;
378 #ifndef STATIC_GCONV
379 if (strtab[from_module->todir_offset] != '\0')
381 /* Load the module, return handle for it. */
382 int res = find_module (strtab + from_module->todir_offset,
383 strtab + from_module->toname_offset,
384 &result[0]);
385 if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
387 /* Something went wrong. */
388 free (result);
389 return res;
392 else
393 #endif
394 /* It's a builtin transformation. */
395 __gconv_get_builtin_trans (strtab + from_module->toname_offset,
396 &result[0]);
398 ++*nsteps;
401 /* Generate data structure for conversion from INTERNAL. */
402 if (toidx != 0)
404 int idx = *nsteps;
406 result[idx].__from_name = (char *) "INTERNAL";
407 result[idx].__to_name = (char *) strtab + to_module->canonname_offset;
409 result[idx].__counter = 1;
410 result[idx].__data = NULL;
412 #ifndef STATIC_GCONV
413 if (strtab[to_module->fromdir_offset] != '\0')
415 /* Load the module, return handle for it. */
416 int res = find_module (strtab + to_module->fromdir_offset,
417 strtab + to_module->fromname_offset,
418 &result[idx]);
419 if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
421 /* Something went wrong. */
422 if (idx != 0)
423 __gconv_release_step (&result[0]);
424 free (result);
425 return res;
428 else
429 #endif
430 /* It's a builtin transformation. */
431 __gconv_get_builtin_trans (strtab + to_module->fromname_offset,
432 &result[idx]);
434 ++*nsteps;
437 return __GCONV_OK;
441 /* Free memory allocated for the transformation record. */
442 void
443 internal_function
444 __gconv_release_cache (struct __gconv_step *steps, size_t nsteps)
446 if (gconv_cache != NULL)
447 /* The only thing we have to deallocate is the record with the
448 steps. */
449 free (steps);
453 /* Free all resources if necessary. */
454 libc_freeres_fn (free_mem)
456 if (cache_malloced)
457 free (gconv_cache);
458 #ifdef _POSIX_MAPPED_FILES
459 else
460 __munmap (gconv_cache, cache_size);
461 #endif