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
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
;
40 __gconv_get_cache (void)
48 __gconv_load_cache (void)
52 struct gconvcache_header
*header
;
54 /* We cannot use the cache if the GCONV_PATH environment variable is
56 __gconv_path_envvar
= getenv ("GCONV_PATH");
57 if (__gconv_path_envvar
!= NULL
)
60 /* See whether the cache file exists. */
61 fd
= __open (GCONV_MODULES_CACHE
, O_RDONLY
);
62 if (__builtin_expect (fd
, 0) == -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 || st
.st_size
< sizeof (struct gconvcache_header
))
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))
86 gconv_cache
= malloc (cache_size
);
87 if (gconv_cache
== NULL
)
93 ssize_t n
= __read (fd
, (char *) gconv_cache
+ already_read
,
94 cache_size
- already_read
);
95 if (__builtin_expect (n
, 0) == -1)
104 while (already_read
< cache_size
);
109 /* We don't need the file descriptor anymore. */
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
))
121 || __builtin_expect (header
->module_offset
>= cache_size
, 0)
122 || __builtin_expect (header
->otherconv_offset
> cache_size
, 0))
129 #ifdef _POSIX_MAPPED_FILES
131 __munmap (gconv_cache
, cache_size
);
145 find_module_idx (const char *str
, size_t *idxp
)
150 const struct gconvcache_header
*header
;
152 const struct hash_entry
*hashtab
;
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
;
173 if ((idx
+= hval2
) >= header
->hash_size
)
174 idx
-= header
->hash_size
;
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
)
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
));
216 __gconv_compare_alias_cache (const char *name1
, const char *name2
, int *result
)
221 if (gconv_cache
== NULL
)
224 if (find_module_idx (name1
, &name1_idx
) != 0
225 || find_module_idx (name2
, &name2_idx
) != 0)
226 *result
= strcmp (name1
, name2
);
228 *result
= (int) (name1_idx
- name2_idx
);
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
;
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. */
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
)
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
)
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
)
291 * sizeof (struct extra_entry_module
)));
293 if (extra
->module_cnt
!= 0)
295 /* Use the extra module. First determine how many steps. */
299 *nsteps
= extra
->module_cnt
;
301 (struct __gconv_step
*) malloc (extra
->module_cnt
302 * sizeof (struct __gconv_step
));
304 return __GCONV_NOMEM
;
306 fromname
= (char *) strtab
+ from_module
->canonname_offset
;
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
;
318 if (strtab
[extra
->module
[idx
].dir_offset
] != '\0')
320 /* Load the module, return handle for it. */
323 res
= find_module (strtab
+ extra
->module
[idx
].dir_offset
,
324 strtab
+ extra
->module
[idx
].name_offset
,
326 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
328 /* Something went wrong. */
335 /* It's a builtin transformation. */
336 __gconv_get_builtin_trans (strtab
337 + extra
->module
[idx
].name_offset
,
341 while (++idx
< extra
->module_cnt
);
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
));
358 return __GCONV_NOMEM
;
363 /* Generate data structure for conversion to INTERNAL. */
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
;
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
,
379 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
381 /* Something went wrong. */
388 /* It's a builtin transformation. */
389 __gconv_get_builtin_trans (strtab
+ from_module
->toname_offset
,
395 /* Generate data structure for conversion from INTERNAL. */
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
;
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
,
413 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
415 /* Something went wrong. */
417 __gconv_release_step (&result
[0]);
424 /* It's a builtin transformation. */
425 __gconv_get_builtin_trans (strtab
+ to_module
->fromname_offset
,
435 /* Free memory allocated for the transformation record. */
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
447 /* Free all resources if necessary. */
448 static void __attribute__ ((unused
))
453 #ifdef _POSIX_MAPPED_FILES
455 __munmap (gconv_cache
, cache_size
);
459 text_set_element (__libc_subfreeres
, free_mem
);