1 /* Cache handling for iconv modules.
2 Copyright (C) 2001 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"
35 static size_t cache_size
;
36 static int cache_malloced
;
41 __gconv_load_cache (void)
45 struct gconvcache_header
*header
;
47 /* We cannot use the cache if the GCONV_PATH environment variable is
49 __gconv_path_envvar
= getenv ("GCONV_PATH");
50 if (__gconv_path_envvar
!= NULL
)
53 /* See whether the cache file exists. */
54 fd
= __open (GCONV_MODULES_CACHE
, O_RDONLY
);
55 if (__builtin_expect (fd
, 0) == -1)
59 /* Get information about the file. */
60 if (__builtin_expect (__fxstat64 (_STAT_VER
, fd
, &st
), 0) < 0
61 /* We do not have to start looking at the file if it cannot contain
62 at least the cache header. */
63 || st
.st_size
< sizeof (struct gconvcache_header
))
70 /* Make the file content available. */
71 cache_size
= st
.st_size
;
72 #ifdef _POSIX_MAPPED_FILES
73 __gconv_cache
= __mmap (NULL
, cache_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
74 if (__builtin_expect (__gconv_cache
== MAP_FAILED
, 0))
79 __gconv_cache
= malloc (cache_size
);
80 if (__gconv_cache
== NULL
)
86 ssize_t n
= __read (fd
, (char *) __gconv_cache
+ already_read
,
87 cache_size
- already_read
);
88 if (__builtin_expect (n
, 0) == -1)
97 while (already_read
< cache_size
);
102 /* We don't need the file descriptor anymore. */
105 /* Check the consistency. */
106 header
= (struct gconvcache_header
*) __gconv_cache
;
107 if (__builtin_expect (header
->magic
, GCONVCACHE_MAGIC
) != GCONVCACHE_MAGIC
108 || __builtin_expect (header
->string_offset
>= cache_size
, 0)
109 || __builtin_expect (header
->hash_offset
>= cache_size
, 0)
110 || __builtin_expect (header
->hash_size
== 0, 0)
111 || __builtin_expect ((header
->hash_offset
112 + header
->hash_size
* sizeof (struct hash_entry
))
114 || __builtin_expect (header
->module_offset
>= cache_size
, 0)
115 || __builtin_expect (header
->otherconv_offset
> cache_size
, 0))
119 free (__gconv_cache
);
122 #ifdef _POSIX_MAPPED_FILES
124 __munmap (__gconv_cache
, cache_size
);
126 __gconv_cache
= NULL
;
138 find_module_idx (const char *str
, size_t *idxp
)
143 const struct gconvcache_header
*header
;
145 const struct hash_entry
*hashtab
;
148 header
= (const struct gconvcache_header
*) __gconv_cache
;
149 strtab
= (char *) __gconv_cache
+ header
->string_offset
;
150 hashtab
= (struct hash_entry
*) ((char *) __gconv_cache
151 + header
->hash_offset
);
153 hval
= hash_string (str
);
154 idx
= hval
% header
->hash_size
;
155 hval2
= 1 + hval
% (header
->hash_size
- 2);
157 limit
= cache_size
- header
->string_offset
;
158 while (hashtab
[idx
].string_offset
!= 0)
159 if (hashtab
[idx
].string_offset
< limit
160 && strcmp (str
, strtab
+ hashtab
[idx
].string_offset
) == 0)
162 *idxp
= hashtab
[idx
].module_idx
;
166 if ((idx
+= hval2
) >= header
->hash_size
)
167 idx
-= header
->hash_size
;
177 find_module (const char *directory
, const char *filename
,
178 struct __gconv_step
*result
)
180 size_t dirlen
= strlen (directory
);
181 size_t fnamelen
= strlen (filename
) + 1;
182 char fullname
[dirlen
+ fnamelen
];
183 int status
= __GCONV_NOCONV
;
185 memcpy (__mempcpy (fullname
, directory
, dirlen
), filename
, fnamelen
);
187 result
->__shlib_handle
= __gconv_find_shlib (fullname
);
188 if (result
->__shlib_handle
!= NULL
)
192 result
->__modname
= NULL
;
193 result
->__fct
= result
->__shlib_handle
->fct
;
194 result
->__init_fct
= result
->__shlib_handle
->init_fct
;
195 result
->__end_fct
= result
->__shlib_handle
->end_fct
;
197 result
->__data
= NULL
;
198 if (result
->__init_fct
!= NULL
)
199 status
= DL_CALL_FCT (result
->__init_fct
, (result
));
209 __gconv_compare_alias_cache (const char *name1
, const char *name2
, int *result
)
214 if (__gconv_cache
== NULL
)
217 if (find_module_idx (name1
, &name1_idx
) != 0
218 || find_module_idx (name2
, &name2_idx
) != 0)
219 *result
= strcmp (name1
, name2
);
221 *result
= (int) (name1_idx
- name2_idx
);
229 __gconv_lookup_cache (const char *toset
, const char *fromset
,
230 struct __gconv_step
**handle
, size_t *nsteps
, int flags
)
232 const struct gconvcache_header
*header
;
236 const struct module_entry
*modtab
;
237 const struct module_entry
*from_module
;
238 const struct module_entry
*to_module
;
239 struct __gconv_step
*result
;
241 if (__gconv_cache
== NULL
)
242 /* We have no cache available. */
245 header
= (const struct gconvcache_header
*) __gconv_cache
;
246 strtab
= (char *) __gconv_cache
+ header
->string_offset
;
247 modtab
= (const struct module_entry
*) ((char *) __gconv_cache
248 + header
->module_offset
);
250 if (find_module_idx (fromset
, &fromidx
) != 0
251 || (header
->module_offset
+ (fromidx
+ 1) * sizeof (struct module_entry
)
253 return __GCONV_NOCONV
;
254 from_module
= &modtab
[fromidx
];
256 if (find_module_idx (toset
, &toidx
) != 0
257 || (header
->module_offset
+ (toidx
+ 1) * sizeof (struct module_entry
)
259 return __GCONV_NOCONV
;
260 to_module
= &modtab
[toidx
];
262 /* Avoid copy-only transformations if the user requests. */
263 if (__builtin_expect (flags
& GCONV_AVOID_NOCONV
, 0) && fromidx
== toidx
)
264 return __GCONV_NOCONV
;
266 /* If there are special conversions available examine them first. */
267 if (fromidx
!= 0 && toidx
!= 0
268 && __builtin_expect (from_module
->extra_offset
, 0) != 0)
270 /* Search through the list to see whether there is a module
271 matching the destination character set. */
272 const struct extra_entry
*extra
;
274 /* Note the -1. This is due to the offset added in iconvconfig.
275 See there for more explanations. */
276 extra
= (const struct extra_entry
*) ((char *) __gconv_cache
277 + header
->otherconv_offset
278 + from_module
->extra_offset
- 1);
279 while (extra
->module_cnt
!= 0
280 && extra
->module
[extra
->module_cnt
- 1].outname_offset
!= toidx
)
281 extra
= (const struct extra_entry
*) ((char *) extra
282 + sizeof (struct extra_entry
)
284 * sizeof (struct extra_entry_module
)));
286 if (extra
->module_cnt
!= 0)
288 /* Use the extra module. First determine how many steps. */
292 *nsteps
= extra
->module_cnt
;
294 (struct __gconv_step
*) malloc (extra
->module_cnt
295 * sizeof (struct __gconv_step
));
297 return __GCONV_NOMEM
;
299 fromname
= (char *) strtab
+ from_module
->canonname_offset
;
303 result
[idx
].__from_name
= fromname
;
304 fromname
= result
[idx
].__to_name
=
305 (char *) strtab
+ modtab
[extra
->module
[idx
].outname_offset
].canonname_offset
;
307 result
[idx
].__counter
= 1;
308 result
[idx
].__data
= NULL
;
311 if (strtab
[extra
->module
[idx
].dir_offset
] != '\0')
313 /* Load the module, return handle for it. */
316 res
= find_module (strtab
+ extra
->module
[idx
].dir_offset
,
317 strtab
+ extra
->module
[idx
].name_offset
,
319 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
321 /* Something went wrong. */
328 /* It's a builtin transformation. */
329 __gconv_get_builtin_trans (strtab
330 + extra
->module
[idx
].name_offset
,
334 while (++idx
< extra
->module_cnt
);
341 /* See whether we can convert via the INTERNAL charset. */
342 if ((fromidx
!= 0 && __builtin_expect (from_module
->fromname_offset
, 1) == 0)
343 || (toidx
!= 0 && __builtin_expect (to_module
->toname_offset
, 1) == 0)
344 || (fromidx
== 0 && toidx
== 0))
345 /* Not possible. Nothing we can do. */
346 return __GCONV_NOCONV
;
348 /* We will use up to two modules. Always allocate room for two. */
349 result
= (struct __gconv_step
*) malloc (2 * sizeof (struct __gconv_step
));
351 return __GCONV_NOMEM
;
356 /* Generate data structure for conversion to INTERNAL. */
359 result
[0].__from_name
= (char *) strtab
+ from_module
->canonname_offset
;
360 result
[0].__to_name
= (char *) "INTERNAL";
362 result
[0].__counter
= 1;
363 result
[0].__data
= NULL
;
366 if (strtab
[from_module
->todir_offset
] != '\0')
368 /* Load the module, return handle for it. */
369 int res
= find_module (strtab
+ from_module
->todir_offset
,
370 strtab
+ from_module
->toname_offset
,
372 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
374 /* Something went wrong. */
381 /* It's a builtin transformation. */
382 __gconv_get_builtin_trans (strtab
+ from_module
->toname_offset
,
388 /* Generate data structure for conversion from INTERNAL. */
393 result
[idx
].__from_name
= (char *) "INTERNAL";
394 result
[idx
].__to_name
= (char *) strtab
+ to_module
->canonname_offset
;
396 result
[idx
].__counter
= 1;
397 result
[idx
].__data
= NULL
;
400 if (strtab
[to_module
->fromdir_offset
] != '\0')
402 /* Load the module, return handle for it. */
403 int res
= find_module (strtab
+ to_module
->fromdir_offset
,
404 strtab
+ to_module
->fromname_offset
,
406 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
408 /* Something went wrong. */
410 __gconv_release_step (&result
[0]);
417 /* It's a builtin transformation. */
418 __gconv_get_builtin_trans (strtab
+ to_module
->fromname_offset
,
428 /* Free memory allocated for the transformation record. */
431 __gconv_release_cache (struct __gconv_step
*steps
, size_t nsteps
)
433 if (__gconv_cache
!= NULL
)
434 /* The only thing we have to deallocate is the record with the
440 /* Free all resources if necessary. */
441 static void __attribute__ ((unused
))
445 free (__gconv_cache
);
446 #ifdef _POSIX_MAPPED_FILES
448 __munmap (__gconv_cache
, cache_size
);
452 text_set_element (__libc_subfreeres
, free_mem
);