1 /* Cache handling for iconv modules.
2 Copyright (C) 2001-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
28 #include <gconv_int.h>
29 #include <iconvconfig.h>
30 #include <not-cancel.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)
47 __gconv_load_cache (void)
50 struct __stat64_t64 st
;
51 struct gconvcache_header
*header
;
53 /* We cannot use the cache if the GCONV_PATH environment variable is
55 __gconv_path_envvar
= getenv ("GCONV_PATH");
56 if (__gconv_path_envvar
!= NULL
)
59 /* See whether the cache file exists. */
60 fd
= __open_nocancel (GCONV_MODULES_CACHE
, O_RDONLY
, 0);
61 if (__builtin_expect (fd
, 0) == -1)
65 /* Get information about the file. */
66 if (__glibc_unlikely (__fstat64_time64 (fd
, &st
) < 0)
67 /* We do not have to start looking at the file if it cannot contain
68 at least the cache header. */
69 || (size_t) st
.st_size
< sizeof (struct gconvcache_header
))
72 __close_nocancel_nostatus (fd
);
76 /* Make the file content available. */
77 cache_size
= st
.st_size
;
78 #ifdef _POSIX_MAPPED_FILES
79 gconv_cache
= __mmap (NULL
, cache_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
80 if (__glibc_unlikely (gconv_cache
== MAP_FAILED
))
85 gconv_cache
= malloc (cache_size
);
86 if (gconv_cache
== NULL
)
92 ssize_t n
= __read (fd
, (char *) gconv_cache
+ already_read
,
93 cache_size
- already_read
);
94 if (__builtin_expect (n
, 0) == -1)
103 while (already_read
< cache_size
);
108 /* We don't need the file descriptor anymore. */
109 __close_nocancel_nostatus (fd
);
111 /* Check the consistency. */
112 header
= (struct gconvcache_header
*) gconv_cache
;
113 if (__builtin_expect (header
->magic
, GCONVCACHE_MAGIC
) != GCONVCACHE_MAGIC
114 || __builtin_expect (header
->string_offset
>= cache_size
, 0)
115 || __builtin_expect (header
->hash_offset
>= cache_size
, 0)
116 || __builtin_expect (header
->hash_size
== 0, 0)
117 || __builtin_expect ((header
->hash_offset
118 + header
->hash_size
* sizeof (struct hash_entry
))
120 || __builtin_expect (header
->module_offset
>= cache_size
, 0)
121 || __builtin_expect (header
->otherconv_offset
> cache_size
, 0))
128 #ifdef _POSIX_MAPPED_FILES
130 __munmap (gconv_cache
, cache_size
);
143 find_module_idx (const char *str
, size_t *idxp
)
148 const struct gconvcache_header
*header
;
150 const struct hash_entry
*hashtab
;
153 header
= (const struct gconvcache_header
*) gconv_cache
;
154 strtab
= (char *) gconv_cache
+ header
->string_offset
;
155 hashtab
= (struct hash_entry
*) ((char *) gconv_cache
156 + header
->hash_offset
);
158 hval
= __hash_string (str
);
159 idx
= hval
% header
->hash_size
;
160 hval2
= 1 + hval
% (header
->hash_size
- 2);
162 limit
= cache_size
- header
->string_offset
;
163 while (hashtab
[idx
].string_offset
!= 0)
164 if (hashtab
[idx
].string_offset
< limit
165 && strcmp (str
, strtab
+ hashtab
[idx
].string_offset
) == 0)
167 *idxp
= hashtab
[idx
].module_idx
;
171 if ((idx
+= hval2
) >= header
->hash_size
)
172 idx
-= header
->hash_size
;
181 find_module (const char *directory
, const char *filename
,
182 struct __gconv_step
*result
)
184 size_t dirlen
= strlen (directory
);
185 size_t fnamelen
= strlen (filename
) + 1;
186 char fullname
[dirlen
+ fnamelen
];
187 int status
= __GCONV_NOCONV
;
189 memcpy (__mempcpy (fullname
, directory
, dirlen
), filename
, fnamelen
);
191 result
->__shlib_handle
= __gconv_find_shlib (fullname
);
192 if (result
->__shlib_handle
!= NULL
)
196 result
->__modname
= NULL
;
197 result
->__fct
= result
->__shlib_handle
->fct
;
198 result
->__init_fct
= result
->__shlib_handle
->init_fct
;
199 result
->__end_fct
= result
->__shlib_handle
->end_fct
;
201 /* These settings can be overridden by the init function. */
202 result
->__btowc_fct
= NULL
;
203 result
->__data
= NULL
;
205 /* Call the init function. */
206 __gconv_init_fct init_fct
= result
->__init_fct
;
208 PTR_DEMANGLE (init_fct
);
210 if (init_fct
!= NULL
)
212 status
= DL_CALL_FCT (init_fct
, (result
));
215 PTR_MANGLE (result
->__btowc_fct
);
226 __gconv_compare_alias_cache (const char *name1
, const char *name2
, int *result
)
231 if (gconv_cache
== NULL
)
234 if (find_module_idx (name1
, &name1_idx
) != 0
235 || find_module_idx (name2
, &name2_idx
) != 0)
236 *result
= strcmp (name1
, name2
);
238 *result
= (int) (name1_idx
- name2_idx
);
245 __gconv_lookup_cache (const char *toset
, const char *fromset
,
246 struct __gconv_step
**handle
, size_t *nsteps
, int flags
)
248 const struct gconvcache_header
*header
;
252 const struct module_entry
*modtab
;
253 const struct module_entry
*from_module
;
254 const struct module_entry
*to_module
;
255 struct __gconv_step
*result
;
257 if (gconv_cache
== NULL
)
258 /* We have no cache available. */
261 header
= (const struct gconvcache_header
*) gconv_cache
;
262 strtab
= (char *) gconv_cache
+ header
->string_offset
;
263 modtab
= (const struct module_entry
*) ((char *) gconv_cache
264 + header
->module_offset
);
266 if (find_module_idx (fromset
, &fromidx
) != 0
267 || (header
->module_offset
+ (fromidx
+ 1) * sizeof (struct module_entry
)
269 return __GCONV_NOCONV
;
270 from_module
= &modtab
[fromidx
];
272 if (find_module_idx (toset
, &toidx
) != 0
273 || (header
->module_offset
+ (toidx
+ 1) * sizeof (struct module_entry
)
275 return __GCONV_NOCONV
;
276 to_module
= &modtab
[toidx
];
278 /* Avoid copy-only transformations if the user requests. */
279 if (__builtin_expect (flags
& GCONV_AVOID_NOCONV
, 0) && fromidx
== toidx
)
280 return __GCONV_NULCONV
;
282 /* If there are special conversions available examine them first. */
283 if (fromidx
!= 0 && toidx
!= 0
284 && __builtin_expect (from_module
->extra_offset
, 0) != 0)
286 /* Search through the list to see whether there is a module
287 matching the destination character set. */
288 const struct extra_entry
*extra
;
290 /* Note the -1. This is due to the offset added in iconvconfig.
291 See there for more explanations. */
292 extra
= (const struct extra_entry
*) ((char *) gconv_cache
293 + header
->otherconv_offset
294 + from_module
->extra_offset
- 1);
295 while (extra
->module_cnt
!= 0
296 && extra
->module
[extra
->module_cnt
- 1].outname_offset
!= toidx
)
297 extra
= (const struct extra_entry
*) ((char *) extra
298 + sizeof (struct extra_entry
)
300 * sizeof (struct extra_entry_module
)));
302 if (extra
->module_cnt
!= 0)
304 /* Use the extra module. First determine how many steps. */
308 *nsteps
= extra
->module_cnt
;
310 (struct __gconv_step
*) malloc (extra
->module_cnt
311 * sizeof (struct __gconv_step
));
313 return __GCONV_NOMEM
;
315 fromname
= (char *) strtab
+ from_module
->canonname_offset
;
319 result
[idx
].__from_name
= fromname
;
320 fromname
= result
[idx
].__to_name
=
321 (char *) strtab
+ modtab
[extra
->module
[idx
].outname_offset
].canonname_offset
;
323 result
[idx
].__counter
= 1;
324 result
[idx
].__data
= NULL
;
327 if (strtab
[extra
->module
[idx
].dir_offset
] != '\0')
329 /* Load the module, return handle for it. */
332 res
= find_module (strtab
+ extra
->module
[idx
].dir_offset
,
333 strtab
+ extra
->module
[idx
].name_offset
,
335 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
337 /* Something went wrong. */
344 /* It's a builtin transformation. */
345 __gconv_get_builtin_trans (strtab
346 + extra
->module
[idx
].name_offset
,
350 while (++idx
< extra
->module_cnt
);
357 /* See whether we can convert via the INTERNAL charset. */
358 if ((fromidx
!= 0 && __builtin_expect (from_module
->fromname_offset
, 1) == 0)
359 || (toidx
!= 0 && __builtin_expect (to_module
->toname_offset
, 1) == 0)
360 || (fromidx
== 0 && toidx
== 0))
361 /* Not possible. Nothing we can do. */
362 return __GCONV_NOCONV
;
364 /* We will use up to two modules. Always allocate room for two. */
365 result
= (struct __gconv_step
*) malloc (2 * sizeof (struct __gconv_step
));
367 return __GCONV_NOMEM
;
372 /* Generate data structure for conversion to INTERNAL. */
375 result
[0].__from_name
= (char *) strtab
+ from_module
->canonname_offset
;
376 result
[0].__to_name
= (char *) "INTERNAL";
378 result
[0].__counter
= 1;
379 result
[0].__data
= NULL
;
382 if (strtab
[from_module
->todir_offset
] != '\0')
384 /* Load the module, return handle for it. */
385 int res
= find_module (strtab
+ from_module
->todir_offset
,
386 strtab
+ from_module
->toname_offset
,
388 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
390 /* Something went wrong. */
397 /* It's a builtin transformation. */
398 __gconv_get_builtin_trans (strtab
+ from_module
->toname_offset
,
404 /* Generate data structure for conversion from INTERNAL. */
409 result
[idx
].__from_name
= (char *) "INTERNAL";
410 result
[idx
].__to_name
= (char *) strtab
+ to_module
->canonname_offset
;
412 result
[idx
].__counter
= 1;
413 result
[idx
].__data
= NULL
;
416 if (strtab
[to_module
->fromdir_offset
] != '\0')
418 /* Load the module, return handle for it. */
419 int res
= find_module (strtab
+ to_module
->fromdir_offset
,
420 strtab
+ to_module
->fromname_offset
,
422 if (__builtin_expect (res
, __GCONV_OK
) != __GCONV_OK
)
424 /* Something went wrong. */
426 __gconv_release_step (&result
[0]);
433 /* It's a builtin transformation. */
434 __gconv_get_builtin_trans (strtab
+ to_module
->fromname_offset
,
444 /* Free memory allocated for the transformation record. */
446 __gconv_release_cache (struct __gconv_step
*steps
, size_t nsteps
)
448 if (gconv_cache
!= NULL
)
449 /* The only thing we have to deallocate is the record with the
455 /* Free all resources if necessary. */
456 libc_freeres_fn (free_mem
)
460 #ifdef _POSIX_MAPPED_FILES
461 else if (gconv_cache
!= NULL
)
462 __munmap (gconv_cache
, cache_size
);