1 /* Cache handling for iconv modules.
2 Copyright (C) 2001-2014 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, see
18 <http://www.gnu.org/licenses/>. */
29 #include <gconv_int.h>
30 #include <iconvconfig.h>
31 #include <not-cancel.h>
33 #include "../intl/hash-string.h"
35 static void *gconv_cache
;
36 static size_t cache_size
;
37 static int cache_malloced
;
41 __gconv_get_cache (void)
49 __gconv_load_cache (void)
53 struct gconvcache_header
*header
;
55 /* We cannot use the cache if the GCONV_PATH environment variable is
57 __gconv_path_envvar
= getenv ("GCONV_PATH");
58 if (__gconv_path_envvar
!= NULL
)
61 /* See whether the cache file exists. */
62 fd
= open_not_cancel (GCONV_MODULES_CACHE
, O_RDONLY
, 0);
63 if (__builtin_expect (fd
, 0) == -1)
67 /* Get information about the file. */
68 if (__builtin_expect (__fxstat64 (_STAT_VER
, fd
, &st
), 0) < 0
69 /* We do not have to start looking at the file if it cannot contain
70 at least the cache header. */
71 || (size_t) st
.st_size
< sizeof (struct gconvcache_header
))
74 close_not_cancel_no_status (fd
);
78 /* Make the file content available. */
79 cache_size
= st
.st_size
;
80 #ifdef _POSIX_MAPPED_FILES
81 gconv_cache
= __mmap (NULL
, cache_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
82 if (__glibc_unlikely (gconv_cache
== MAP_FAILED
))
87 gconv_cache
= malloc (cache_size
);
88 if (gconv_cache
== NULL
)
94 ssize_t n
= __read (fd
, (char *) gconv_cache
+ already_read
,
95 cache_size
- already_read
);
96 if (__builtin_expect (n
, 0) == -1)
105 while (already_read
< cache_size
);
110 /* We don't need the file descriptor anymore. */
111 close_not_cancel_no_status (fd
);
113 /* Check the consistency. */
114 header
= (struct gconvcache_header
*) gconv_cache
;
115 if (__builtin_expect (header
->magic
, GCONVCACHE_MAGIC
) != GCONVCACHE_MAGIC
116 || __builtin_expect (header
->string_offset
>= cache_size
, 0)
117 || __builtin_expect (header
->hash_offset
>= cache_size
, 0)
118 || __builtin_expect (header
->hash_size
== 0, 0)
119 || __builtin_expect ((header
->hash_offset
120 + header
->hash_size
* sizeof (struct hash_entry
))
122 || __builtin_expect (header
->module_offset
>= cache_size
, 0)
123 || __builtin_expect (header
->otherconv_offset
> cache_size
, 0))
130 #ifdef _POSIX_MAPPED_FILES
132 __munmap (gconv_cache
, cache_size
);
146 find_module_idx (const char *str
, size_t *idxp
)
151 const struct gconvcache_header
*header
;
153 const struct hash_entry
*hashtab
;
156 header
= (const struct gconvcache_header
*) gconv_cache
;
157 strtab
= (char *) gconv_cache
+ header
->string_offset
;
158 hashtab
= (struct hash_entry
*) ((char *) gconv_cache
159 + header
->hash_offset
);
161 hval
= __hash_string (str
);
162 idx
= hval
% header
->hash_size
;
163 hval2
= 1 + hval
% (header
->hash_size
- 2);
165 limit
= cache_size
- header
->string_offset
;
166 while (hashtab
[idx
].string_offset
!= 0)
167 if (hashtab
[idx
].string_offset
< limit
168 && strcmp (str
, strtab
+ hashtab
[idx
].string_offset
) == 0)
170 *idxp
= hashtab
[idx
].module_idx
;
174 if ((idx
+= hval2
) >= header
->hash_size
)
175 idx
-= header
->hash_size
;
185 find_module (const char *directory
, const char *filename
,
186 struct __gconv_step
*result
)
188 size_t dirlen
= strlen (directory
);
189 size_t fnamelen
= strlen (filename
) + 1;
190 char fullname
[dirlen
+ fnamelen
];
191 int status
= __GCONV_NOCONV
;
193 memcpy (__mempcpy (fullname
, directory
, dirlen
), filename
, fnamelen
);
195 result
->__shlib_handle
= __gconv_find_shlib (fullname
);
196 if (result
->__shlib_handle
!= NULL
)
200 result
->__modname
= NULL
;
201 result
->__fct
= result
->__shlib_handle
->fct
;
202 result
->__init_fct
= result
->__shlib_handle
->init_fct
;
203 result
->__end_fct
= result
->__shlib_handle
->end_fct
;
205 /* These settings can be overridden by the init function. */
206 result
->__btowc_fct
= NULL
;
207 result
->__data
= NULL
;
209 /* Call the init function. */
210 if (result
->__init_fct
!= NULL
)
212 __gconv_init_fct init_fct
= result
->__init_fct
;
214 PTR_DEMANGLE (init_fct
);
216 status
= DL_CALL_FCT (init_fct
, (result
));
219 if (result
->__btowc_fct
!= NULL
)
220 PTR_MANGLE (result
->__btowc_fct
);
232 __gconv_compare_alias_cache (const char *name1
, const char *name2
, int *result
)
237 if (gconv_cache
== NULL
)
240 if (find_module_idx (name1
, &name1_idx
) != 0
241 || find_module_idx (name2
, &name2_idx
) != 0)
242 *result
= strcmp (name1
, name2
);
244 *result
= (int) (name1_idx
- name2_idx
);
252 __gconv_lookup_cache (const char *toset
, const char *fromset
,
253 struct __gconv_step
**handle
, size_t *nsteps
, int flags
)
255 const struct gconvcache_header
*header
;
259 const struct module_entry
*modtab
;
260 const struct module_entry
*from_module
;
261 const struct module_entry
*to_module
;
262 struct __gconv_step
*result
;
264 if (gconv_cache
== NULL
)
265 /* We have no cache available. */
268 header
= (const struct gconvcache_header
*) gconv_cache
;
269 strtab
= (char *) gconv_cache
+ header
->string_offset
;
270 modtab
= (const struct module_entry
*) ((char *) gconv_cache
271 + header
->module_offset
);
273 if (find_module_idx (fromset
, &fromidx
) != 0
274 || (header
->module_offset
+ (fromidx
+ 1) * sizeof (struct module_entry
)
276 return __GCONV_NOCONV
;
277 from_module
= &modtab
[fromidx
];
279 if (find_module_idx (toset
, &toidx
) != 0
280 || (header
->module_offset
+ (toidx
+ 1) * sizeof (struct module_entry
)
282 return __GCONV_NOCONV
;
283 to_module
= &modtab
[toidx
];
285 /* Avoid copy-only transformations if the user requests. */
286 if (__builtin_expect (flags
& GCONV_AVOID_NOCONV
, 0) && fromidx
== toidx
)
287 return __GCONV_NULCONV
;
289 /* If there are special conversions available examine them first. */
290 if (fromidx
!= 0 && toidx
!= 0
291 && __builtin_expect (from_module
->extra_offset
, 0) != 0)
293 /* Search through the list to see whether there is a module
294 matching the destination character set. */
295 const struct extra_entry
*extra
;
297 /* Note the -1. This is due to the offset added in iconvconfig.
298 See there for more explanations. */
299 extra
= (const struct extra_entry
*) ((char *) gconv_cache
300 + header
->otherconv_offset
301 + from_module
->extra_offset
- 1);
302 while (extra
->module_cnt
!= 0
303 && extra
->module
[extra
->module_cnt
- 1].outname_offset
!= toidx
)
304 extra
= (const struct extra_entry
*) ((char *) extra
305 + sizeof (struct extra_entry
)
307 * sizeof (struct extra_entry_module
)));
309 if (extra
->module_cnt
!= 0)
311 /* Use the extra module. First determine how many steps. */
315 *nsteps
= extra
->module_cnt
;
317 (struct __gconv_step
*) malloc (extra
->module_cnt
318 * sizeof (struct __gconv_step
));
320 return __GCONV_NOMEM
;
322 fromname
= (char *) strtab
+ from_module
->canonname_offset
;
326 result
[idx
].__from_name
= fromname
;
327 fromname
= result
[idx
].__to_name
=
328 (char *) strtab
+ modtab
[extra
->module
[idx
].outname_offset
].canonname_offset
;
330 result
[idx
].__counter
= 1;
331 result
[idx
].__data
= NULL
;
334 if (strtab
[extra
->module
[idx
].dir_offset
] != '\0')
336 /* Load the module, return handle for it. */
339 res
= find_module (strtab
+ extra
->module
[idx
].dir_offset
,
340 strtab
+ extra
->module
[idx
].name_offset
,
342 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
344 /* Something went wrong. */
351 /* It's a builtin transformation. */
352 __gconv_get_builtin_trans (strtab
353 + extra
->module
[idx
].name_offset
,
357 while (++idx
< extra
->module_cnt
);
364 /* See whether we can convert via the INTERNAL charset. */
365 if ((fromidx
!= 0 && __builtin_expect (from_module
->fromname_offset
, 1) == 0)
366 || (toidx
!= 0 && __builtin_expect (to_module
->toname_offset
, 1) == 0)
367 || (fromidx
== 0 && toidx
== 0))
368 /* Not possible. Nothing we can do. */
369 return __GCONV_NOCONV
;
371 /* We will use up to two modules. Always allocate room for two. */
372 result
= (struct __gconv_step
*) malloc (2 * sizeof (struct __gconv_step
));
374 return __GCONV_NOMEM
;
379 /* Generate data structure for conversion to INTERNAL. */
382 result
[0].__from_name
= (char *) strtab
+ from_module
->canonname_offset
;
383 result
[0].__to_name
= (char *) "INTERNAL";
385 result
[0].__counter
= 1;
386 result
[0].__data
= NULL
;
389 if (strtab
[from_module
->todir_offset
] != '\0')
391 /* Load the module, return handle for it. */
392 int res
= find_module (strtab
+ from_module
->todir_offset
,
393 strtab
+ from_module
->toname_offset
,
395 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
397 /* Something went wrong. */
404 /* It's a builtin transformation. */
405 __gconv_get_builtin_trans (strtab
+ from_module
->toname_offset
,
411 /* Generate data structure for conversion from INTERNAL. */
416 result
[idx
].__from_name
= (char *) "INTERNAL";
417 result
[idx
].__to_name
= (char *) strtab
+ to_module
->canonname_offset
;
419 result
[idx
].__counter
= 1;
420 result
[idx
].__data
= NULL
;
423 if (strtab
[to_module
->fromdir_offset
] != '\0')
425 /* Load the module, return handle for it. */
426 int res
= find_module (strtab
+ to_module
->fromdir_offset
,
427 strtab
+ to_module
->fromname_offset
,
429 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
431 /* Something went wrong. */
433 __gconv_release_step (&result
[0]);
440 /* It's a builtin transformation. */
441 __gconv_get_builtin_trans (strtab
+ to_module
->fromname_offset
,
451 /* Free memory allocated for the transformation record. */
454 __gconv_release_cache (struct __gconv_step
*steps
, size_t nsteps
)
456 if (gconv_cache
!= NULL
)
457 /* The only thing we have to deallocate is the record with the
463 /* Free all resources if necessary. */
464 libc_freeres_fn (free_mem
)
468 #ifdef _POSIX_MAPPED_FILES
469 else if (gconv_cache
!= NULL
)
470 __munmap (gconv_cache
, cache_size
);