1 /* Cache handling for iconv modules.
2 Copyright (C) 2001, 2002, 2003 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
)
212 status
= DL_CALL_FCT (result
->__init_fct
, (result
));
222 __gconv_compare_alias_cache (const char *name1
, const char *name2
, int *result
)
227 if (gconv_cache
== NULL
)
230 if (find_module_idx (name1
, &name1_idx
) != 0
231 || find_module_idx (name2
, &name2_idx
) != 0)
232 *result
= strcmp (name1
, name2
);
234 *result
= (int) (name1_idx
- name2_idx
);
242 __gconv_lookup_cache (const char *toset
, const char *fromset
,
243 struct __gconv_step
**handle
, size_t *nsteps
, int flags
)
245 const struct gconvcache_header
*header
;
249 const struct module_entry
*modtab
;
250 const struct module_entry
*from_module
;
251 const struct module_entry
*to_module
;
252 struct __gconv_step
*result
;
254 if (gconv_cache
== NULL
)
255 /* We have no cache available. */
258 header
= (const struct gconvcache_header
*) gconv_cache
;
259 strtab
= (char *) gconv_cache
+ header
->string_offset
;
260 modtab
= (const struct module_entry
*) ((char *) gconv_cache
261 + header
->module_offset
);
263 if (find_module_idx (fromset
, &fromidx
) != 0
264 || (header
->module_offset
+ (fromidx
+ 1) * sizeof (struct module_entry
)
266 return __GCONV_NOCONV
;
267 from_module
= &modtab
[fromidx
];
269 if (find_module_idx (toset
, &toidx
) != 0
270 || (header
->module_offset
+ (toidx
+ 1) * sizeof (struct module_entry
)
272 return __GCONV_NOCONV
;
273 to_module
= &modtab
[toidx
];
275 /* Avoid copy-only transformations if the user requests. */
276 if (__builtin_expect (flags
& GCONV_AVOID_NOCONV
, 0) && fromidx
== toidx
)
277 return __GCONV_NOCONV
;
279 /* If there are special conversions available examine them first. */
280 if (fromidx
!= 0 && toidx
!= 0
281 && __builtin_expect (from_module
->extra_offset
, 0) != 0)
283 /* Search through the list to see whether there is a module
284 matching the destination character set. */
285 const struct extra_entry
*extra
;
287 /* Note the -1. This is due to the offset added in iconvconfig.
288 See there for more explanations. */
289 extra
= (const struct extra_entry
*) ((char *) gconv_cache
290 + header
->otherconv_offset
291 + from_module
->extra_offset
- 1);
292 while (extra
->module_cnt
!= 0
293 && extra
->module
[extra
->module_cnt
- 1].outname_offset
!= toidx
)
294 extra
= (const struct extra_entry
*) ((char *) extra
295 + sizeof (struct extra_entry
)
297 * sizeof (struct extra_entry_module
)));
299 if (extra
->module_cnt
!= 0)
301 /* Use the extra module. First determine how many steps. */
305 *nsteps
= extra
->module_cnt
;
307 (struct __gconv_step
*) malloc (extra
->module_cnt
308 * sizeof (struct __gconv_step
));
310 return __GCONV_NOMEM
;
312 fromname
= (char *) strtab
+ from_module
->canonname_offset
;
316 result
[idx
].__from_name
= fromname
;
317 fromname
= result
[idx
].__to_name
=
318 (char *) strtab
+ modtab
[extra
->module
[idx
].outname_offset
].canonname_offset
;
320 result
[idx
].__counter
= 1;
321 result
[idx
].__data
= NULL
;
324 if (strtab
[extra
->module
[idx
].dir_offset
] != '\0')
326 /* Load the module, return handle for it. */
329 res
= find_module (strtab
+ extra
->module
[idx
].dir_offset
,
330 strtab
+ extra
->module
[idx
].name_offset
,
332 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
334 /* Something went wrong. */
341 /* It's a builtin transformation. */
342 __gconv_get_builtin_trans (strtab
343 + extra
->module
[idx
].name_offset
,
347 while (++idx
< extra
->module_cnt
);
354 /* See whether we can convert via the INTERNAL charset. */
355 if ((fromidx
!= 0 && __builtin_expect (from_module
->fromname_offset
, 1) == 0)
356 || (toidx
!= 0 && __builtin_expect (to_module
->toname_offset
, 1) == 0)
357 || (fromidx
== 0 && toidx
== 0))
358 /* Not possible. Nothing we can do. */
359 return __GCONV_NOCONV
;
361 /* We will use up to two modules. Always allocate room for two. */
362 result
= (struct __gconv_step
*) malloc (2 * sizeof (struct __gconv_step
));
364 return __GCONV_NOMEM
;
369 /* Generate data structure for conversion to INTERNAL. */
372 result
[0].__from_name
= (char *) strtab
+ from_module
->canonname_offset
;
373 result
[0].__to_name
= (char *) "INTERNAL";
375 result
[0].__counter
= 1;
376 result
[0].__data
= NULL
;
379 if (strtab
[from_module
->todir_offset
] != '\0')
381 /* Load the module, return handle for it. */
382 int res
= find_module (strtab
+ from_module
->todir_offset
,
383 strtab
+ from_module
->toname_offset
,
385 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
387 /* Something went wrong. */
394 /* It's a builtin transformation. */
395 __gconv_get_builtin_trans (strtab
+ from_module
->toname_offset
,
401 /* Generate data structure for conversion from INTERNAL. */
406 result
[idx
].__from_name
= (char *) "INTERNAL";
407 result
[idx
].__to_name
= (char *) strtab
+ to_module
->canonname_offset
;
409 result
[idx
].__counter
= 1;
410 result
[idx
].__data
= NULL
;
413 if (strtab
[to_module
->fromdir_offset
] != '\0')
415 /* Load the module, return handle for it. */
416 int res
= find_module (strtab
+ to_module
->fromdir_offset
,
417 strtab
+ to_module
->fromname_offset
,
419 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
421 /* Something went wrong. */
423 __gconv_release_step (&result
[0]);
430 /* It's a builtin transformation. */
431 __gconv_get_builtin_trans (strtab
+ to_module
->fromname_offset
,
441 /* Free memory allocated for the transformation record. */
444 __gconv_release_cache (struct __gconv_step
*steps
, size_t nsteps
)
446 if (gconv_cache
!= NULL
)
447 /* The only thing we have to deallocate is the record with the
453 /* Free all resources if necessary. */
454 libc_freeres_fn (free_mem
)
458 #ifdef _POSIX_MAPPED_FILES
460 __munmap (gconv_cache
, cache_size
);