1 /* Cache handling for iconv modules.
2 Copyright (C) 2001, 2002, 2003, 2005, 2007 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
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
;
42 __gconv_get_cache (void)
50 __gconv_load_cache (void)
54 struct gconvcache_header
*header
;
56 /* We cannot use the cache if the GCONV_PATH environment variable is
58 __gconv_path_envvar
= getenv ("GCONV_PATH");
59 if (__gconv_path_envvar
!= NULL
)
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)
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
))
75 close_not_cancel_no_status (fd
);
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))
88 gconv_cache
= malloc (cache_size
);
89 if (gconv_cache
== NULL
)
95 ssize_t n
= __read (fd
, (char *) gconv_cache
+ already_read
,
96 cache_size
- already_read
);
97 if (__builtin_expect (n
, 0) == -1)
106 while (already_read
< cache_size
);
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
))
123 || __builtin_expect (header
->module_offset
>= cache_size
, 0)
124 || __builtin_expect (header
->otherconv_offset
> cache_size
, 0))
131 #ifdef _POSIX_MAPPED_FILES
133 __munmap (gconv_cache
, cache_size
);
147 find_module_idx (const char *str
, size_t *idxp
)
152 const struct gconvcache_header
*header
;
154 const struct hash_entry
*hashtab
;
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
;
175 if ((idx
+= hval2
) >= header
->hash_size
)
176 idx
-= header
->hash_size
;
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
)
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
;
215 PTR_DEMANGLE (init_fct
);
217 status
= DL_CALL_FCT (init_fct
, (result
));
220 if (result
->__btowc_fct
!= NULL
)
221 PTR_MANGLE (result
->__btowc_fct
);
233 __gconv_compare_alias_cache (const char *name1
, const char *name2
, int *result
)
238 if (gconv_cache
== NULL
)
241 if (find_module_idx (name1
, &name1_idx
) != 0
242 || find_module_idx (name2
, &name2_idx
) != 0)
243 *result
= strcmp (name1
, name2
);
245 *result
= (int) (name1_idx
- name2_idx
);
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
;
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. */
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
)
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
)
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_NULCONV
;
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
)
308 * sizeof (struct extra_entry_module
)));
310 if (extra
->module_cnt
!= 0)
312 /* Use the extra module. First determine how many steps. */
316 *nsteps
= extra
->module_cnt
;
318 (struct __gconv_step
*) malloc (extra
->module_cnt
319 * sizeof (struct __gconv_step
));
321 return __GCONV_NOMEM
;
323 fromname
= (char *) strtab
+ from_module
->canonname_offset
;
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
;
335 if (strtab
[extra
->module
[idx
].dir_offset
] != '\0')
337 /* Load the module, return handle for it. */
340 res
= find_module (strtab
+ extra
->module
[idx
].dir_offset
,
341 strtab
+ extra
->module
[idx
].name_offset
,
343 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
345 /* Something went wrong. */
352 /* It's a builtin transformation. */
353 __gconv_get_builtin_trans (strtab
354 + extra
->module
[idx
].name_offset
,
358 while (++idx
< extra
->module_cnt
);
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
));
375 return __GCONV_NOMEM
;
380 /* Generate data structure for conversion to INTERNAL. */
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
;
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
,
396 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
398 /* Something went wrong. */
405 /* It's a builtin transformation. */
406 __gconv_get_builtin_trans (strtab
+ from_module
->toname_offset
,
412 /* Generate data structure for conversion from INTERNAL. */
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
;
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
,
430 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
432 /* Something went wrong. */
434 __gconv_release_step (&result
[0]);
441 /* It's a builtin transformation. */
442 __gconv_get_builtin_trans (strtab
+ to_module
->fromname_offset
,
452 /* Free memory allocated for the transformation record. */
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
464 /* Free all resources if necessary. */
465 libc_freeres_fn (free_mem
)
469 #ifdef _POSIX_MAPPED_FILES
470 else if (gconv_cache
!= NULL
)
471 __munmap (gconv_cache
, cache_size
);