Regenerated.
[glibc.git] / iconv / gconv_cache.c
blob882acc6fa46c0362dde9f499a93951ff7f721136
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 /* These settings can be overridden by the init function. */
205 result->__btowc_fct = NULL;
206 result->__data = NULL;
208 /* Call the init function. */
209 if (result->__init_fct != NULL)
210 status = DL_CALL_FCT (result->__init_fct, (result));
213 return status;
215 #endif
219 internal_function
220 __gconv_compare_alias_cache (const char *name1, const char *name2, int *result)
222 size_t name1_idx;
223 size_t name2_idx;
225 if (gconv_cache == NULL)
226 return -1;
228 if (find_module_idx (name1, &name1_idx) != 0
229 || find_module_idx (name2, &name2_idx) != 0)
230 *result = strcmp (name1, name2);
231 else
232 *result = (int) (name1_idx - name2_idx);
234 return 0;
239 internal_function
240 __gconv_lookup_cache (const char *toset, const char *fromset,
241 struct __gconv_step **handle, size_t *nsteps, int flags)
243 const struct gconvcache_header *header;
244 const char *strtab;
245 size_t fromidx;
246 size_t toidx;
247 const struct module_entry *modtab;
248 const struct module_entry *from_module;
249 const struct module_entry *to_module;
250 struct __gconv_step *result;
252 if (gconv_cache == NULL)
253 /* We have no cache available. */
254 return __GCONV_NODB;
256 header = (const struct gconvcache_header *) gconv_cache;
257 strtab = (char *) gconv_cache + header->string_offset;
258 modtab = (const struct module_entry *) ((char *) gconv_cache
259 + header->module_offset);
261 if (find_module_idx (fromset, &fromidx) != 0
262 || (header->module_offset + (fromidx + 1) * sizeof (struct module_entry)
263 > cache_size))
264 return __GCONV_NOCONV;
265 from_module = &modtab[fromidx];
267 if (find_module_idx (toset, &toidx) != 0
268 || (header->module_offset + (toidx + 1) * sizeof (struct module_entry)
269 > cache_size))
270 return __GCONV_NOCONV;
271 to_module = &modtab[toidx];
273 /* Avoid copy-only transformations if the user requests. */
274 if (__builtin_expect (flags & GCONV_AVOID_NOCONV, 0) && fromidx == toidx)
275 return __GCONV_NOCONV;
277 /* If there are special conversions available examine them first. */
278 if (fromidx != 0 && toidx != 0
279 && __builtin_expect (from_module->extra_offset, 0) != 0)
281 /* Search through the list to see whether there is a module
282 matching the destination character set. */
283 const struct extra_entry *extra;
285 /* Note the -1. This is due to the offset added in iconvconfig.
286 See there for more explanations. */
287 extra = (const struct extra_entry *) ((char *) gconv_cache
288 + header->otherconv_offset
289 + from_module->extra_offset - 1);
290 while (extra->module_cnt != 0
291 && extra->module[extra->module_cnt - 1].outname_offset != toidx)
292 extra = (const struct extra_entry *) ((char *) extra
293 + sizeof (struct extra_entry)
294 + (extra->module_cnt
295 * sizeof (struct extra_entry_module)));
297 if (extra->module_cnt != 0)
299 /* Use the extra module. First determine how many steps. */
300 char *fromname;
301 int idx;
303 *nsteps = extra->module_cnt;
304 *handle = result =
305 (struct __gconv_step *) malloc (extra->module_cnt
306 * sizeof (struct __gconv_step));
307 if (result == NULL)
308 return __GCONV_NOMEM;
310 fromname = (char *) strtab + from_module->canonname_offset;
311 idx = 0;
314 result[idx].__from_name = fromname;
315 fromname = result[idx].__to_name =
316 (char *) strtab + modtab[extra->module[idx].outname_offset].canonname_offset;
318 result[idx].__counter = 1;
319 result[idx].__data = NULL;
321 #ifndef STATIC_GCONV
322 if (strtab[extra->module[idx].dir_offset] != '\0')
324 /* Load the module, return handle for it. */
325 int res;
327 res = find_module (strtab + extra->module[idx].dir_offset,
328 strtab + extra->module[idx].name_offset,
329 &result[idx]);
330 if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
332 /* Something went wrong. */
333 free (result);
334 goto try_internal;
337 else
338 #endif
339 /* It's a builtin transformation. */
340 __gconv_get_builtin_trans (strtab
341 + extra->module[idx].name_offset,
342 &result[idx]);
345 while (++idx < extra->module_cnt);
347 return __GCONV_OK;
351 try_internal:
352 /* See whether we can convert via the INTERNAL charset. */
353 if ((fromidx != 0 && __builtin_expect (from_module->fromname_offset, 1) == 0)
354 || (toidx != 0 && __builtin_expect (to_module->toname_offset, 1) == 0)
355 || (fromidx == 0 && toidx == 0))
356 /* Not possible. Nothing we can do. */
357 return __GCONV_NOCONV;
359 /* We will use up to two modules. Always allocate room for two. */
360 result = (struct __gconv_step *) malloc (2 * sizeof (struct __gconv_step));
361 if (result == NULL)
362 return __GCONV_NOMEM;
364 *handle = result;
365 *nsteps = 0;
367 /* Generate data structure for conversion to INTERNAL. */
368 if (fromidx != 0)
370 result[0].__from_name = (char *) strtab + from_module->canonname_offset;
371 result[0].__to_name = (char *) "INTERNAL";
373 result[0].__counter = 1;
374 result[0].__data = NULL;
376 #ifndef STATIC_GCONV
377 if (strtab[from_module->todir_offset] != '\0')
379 /* Load the module, return handle for it. */
380 int res = find_module (strtab + from_module->todir_offset,
381 strtab + from_module->toname_offset,
382 &result[0]);
383 if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
385 /* Something went wrong. */
386 free (result);
387 return res;
390 else
391 #endif
392 /* It's a builtin transformation. */
393 __gconv_get_builtin_trans (strtab + from_module->toname_offset,
394 &result[0]);
396 ++*nsteps;
399 /* Generate data structure for conversion from INTERNAL. */
400 if (toidx != 0)
402 int idx = *nsteps;
404 result[idx].__from_name = (char *) "INTERNAL";
405 result[idx].__to_name = (char *) strtab + to_module->canonname_offset;
407 result[idx].__counter = 1;
408 result[idx].__data = NULL;
410 #ifndef STATIC_GCONV
411 if (strtab[to_module->fromdir_offset] != '\0')
413 /* Load the module, return handle for it. */
414 int res = find_module (strtab + to_module->fromdir_offset,
415 strtab + to_module->fromname_offset,
416 &result[idx]);
417 if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
419 /* Something went wrong. */
420 if (idx != 0)
421 __gconv_release_step (&result[0]);
422 free (result);
423 return res;
426 else
427 #endif
428 /* It's a builtin transformation. */
429 __gconv_get_builtin_trans (strtab + to_module->fromname_offset,
430 &result[idx]);
432 ++*nsteps;
435 return __GCONV_OK;
439 /* Free memory allocated for the transformation record. */
440 void
441 internal_function
442 __gconv_release_cache (struct __gconv_step *steps, size_t nsteps)
444 if (gconv_cache != NULL)
445 /* The only thing we have to deallocate is the record with the
446 steps. */
447 free (steps);
451 /* Free all resources if necessary. */
452 libc_freeres_fn (free_mem)
454 if (cache_malloced)
455 free (gconv_cache);
456 #ifdef _POSIX_MAPPED_FILES
457 else
458 __munmap (gconv_cache, cache_size);
459 #endif