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.
5 
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.
10 
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.
15 
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
19    02111-1307 USA.  */
20 
21 #include <dlfcn.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/stat.h>
28 
29 #include <gconv_int.h>
30 #include <iconvconfig.h>
31 
32 #include "hash-string.h"
33 
34 void *__gconv_cache;
35 static size_t cache_size;
36 static int cache_malloced;
37 
38 
39 int
40 internal_function
__gconv_load_cache(void)41 __gconv_load_cache (void)
42 {
43   int fd;
44   struct stat64 st;
45   struct gconvcache_header *header;
46 
47   /* We cannot use the cache if the GCONV_PATH environment variable is
48      set.  */
49   __gconv_path_envvar = getenv ("GCONV_PATH");
50   if (__gconv_path_envvar != NULL)
51     return -1;
52 
53   /* See whether the cache file exists.  */
54   fd = open (GCONV_MODULES_CACHE, O_RDONLY);
55   if (__builtin_expect (fd, 0) == -1)
56     /* Not available.  */
57     return -1;
58 
59 #ifdef	_POSIX_ASYNC_IO
60   /* Get information about the file.  */
61   if (__builtin_expect (fstat64 (fd, &st), 0) < 0
62       /* We do not have to start looking at the file if it cannot contain
63 	 at least the cache header.  */
64       || st.st_size < sizeof (struct gconvcache_header))
65     {
66 #endif
67     close_and_exit:
68       close (fd);
69       return -1;
70 #ifdef	_POSIX_ASYNC_IO
71     }
72 #endif
73 
74   /* Make the file content available.  */
75   cache_size = st.st_size;
76 #ifdef _POSIX_MAPPED_FILES
77   __gconv_cache = mmap (NULL, cache_size, PROT_READ, MAP_SHARED, fd, 0);
78   if (__builtin_expect (__gconv_cache == MAP_FAILED, 0))
79 #endif
80     {
81       size_t already_read;
82 
83       __gconv_cache = malloc (cache_size);
84       if (__gconv_cache == NULL)
85 	goto close_and_exit;
86 
87       already_read = 0;
88       do
89 	{
90 	  ssize_t n = read (fd, (char *) __gconv_cache + already_read,
91 			      cache_size - already_read);
92 	  if (__builtin_expect (n, 0) == -1)
93 	    {
94 	      free (__gconv_cache);
95 	      __gconv_cache = NULL;
96 	      goto close_and_exit;
97 	    }
98 
99 	  already_read += n;
100 	}
101       while (already_read < cache_size);
102 
103       cache_malloced = 1;
104     }
105 
106   /* We don't need the file descriptor anymore.  */
107   close (fd);
108 
109   /* Check the consistency.  */
110   header = (struct gconvcache_header *) __gconv_cache;
111   if (__builtin_expect (header->magic, GCONVCACHE_MAGIC) != GCONVCACHE_MAGIC
112       || __builtin_expect (header->string_offset >= cache_size, 0)
113       || __builtin_expect (header->hash_offset >= cache_size, 0)
114       || __builtin_expect (header->hash_size == 0, 0)
115       || __builtin_expect ((header->hash_offset
116 			    + header->hash_size * sizeof (struct hash_entry))
117 			   > cache_size, 0)
118       || __builtin_expect (header->module_offset >= cache_size, 0)
119       || __builtin_expect (header->otherconv_offset > cache_size, 0))
120     {
121       if (cache_malloced)
122 	{
123 	  free (__gconv_cache);
124 	  cache_malloced = 0;
125 	}
126 #ifdef _POSIX_MAPPED_FILES
127       else
128 	__munmap (__gconv_cache, cache_size);
129 #endif
130       __gconv_cache = NULL;
131 
132       return -1;
133     }
134 
135   /* That worked.  */
136   return 0;
137 }
138 
139 
140 static int
141 internal_function
find_module_idx(const char * str,size_t * idxp)142 find_module_idx (const char *str, size_t *idxp)
143 {
144   unsigned int idx;
145   unsigned int hval;
146   unsigned int hval2;
147   const struct gconvcache_header *header;
148   const char *strtab;
149   const struct hash_entry *hashtab;
150   unsigned int limit;
151 
152   header = (const struct gconvcache_header *) __gconv_cache;
153   strtab = (char *) __gconv_cache + header->string_offset;
154   hashtab = (struct hash_entry *) ((char *) __gconv_cache
155 				   + header->hash_offset);
156 
157   hval = hash_string (str);
158   idx = hval % header->hash_size;
159   hval2 = 1 + hval % (header->hash_size - 2);
160 
161   limit = cache_size - header->string_offset;
162   while (hashtab[idx].string_offset != 0)
163     if (hashtab[idx].string_offset < limit
164 	&& strcmp (str, strtab + hashtab[idx].string_offset) == 0)
165       {
166 	*idxp = hashtab[idx].module_idx;
167 	return 0;
168       }
169     else
170       if ((idx += hval2) >= header->hash_size)
171 	idx -= header->hash_size;
172 
173   /* Nothing found.  */
174   return -1;
175 }
176 
177 
178 #ifndef STATIC_GCONV
179 static int
180 internal_function
find_module(const char * directory,const char * filename,struct __gconv_step * result)181 find_module (const char *directory, const char *filename,
182 	     struct __gconv_step *result)
183 {
184   size_t dirlen = strlen (directory);
185   size_t fnamelen = strlen (filename) + 1;
186   char fullname[dirlen + fnamelen];
187   int status = __GCONV_NOCONV;
188   char *tmp;
189 
190   tmp = mempcpy (fullname, directory, dirlen);
191   tmp += dirlen;
192   memcpy (tmp, filename, fnamelen);
193 
194   result->__shlib_handle = __gconv_find_shlib (fullname);
195   if (result->__shlib_handle != NULL)
196     {
197       status = __GCONV_OK;
198 
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;
203 
204       result->__data = NULL;
205       if (result->__init_fct != NULL)
206 	status = result->__init_fct (result);
207     }
208 
209   return status;
210 }
211 #endif
212 
213 
214 int
215 internal_function
__gconv_compare_alias_cache(const char * name1,const char * name2,int * result)216 __gconv_compare_alias_cache (const char *name1, const char *name2, int *result)
217 {
218   size_t name1_idx;
219   size_t name2_idx;
220 
221   if (__gconv_cache == NULL)
222     return -1;
223 
224   if (find_module_idx (name1, &name1_idx) != 0
225       || find_module_idx (name2, &name2_idx) != 0)
226     *result = strcmp (name1, name2);
227   else
228     *result = (int) (name1_idx - name2_idx);
229 
230   return 0;
231 }
232 
233 
234 int
235 internal_function
__gconv_lookup_cache(const char * toset,const char * fromset,struct __gconv_step ** handle,size_t * nsteps,int flags)236 __gconv_lookup_cache (const char *toset, const char *fromset,
237 		      struct __gconv_step **handle, size_t *nsteps, int flags)
238 {
239   const struct gconvcache_header *header;
240   const char *strtab;
241   size_t fromidx;
242   size_t toidx;
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;
247 
248   if (__gconv_cache == NULL)
249     /* We have no cache available.  */
250     return __GCONV_NODB;
251 
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);
256 
257   if (find_module_idx (fromset, &fromidx) != 0
258       || (header->module_offset + (fromidx + 1) * sizeof (struct module_entry)
259 	  > cache_size))
260     return __GCONV_NOCONV;
261   from_module = &modtab[fromidx];
262 
263   if (find_module_idx (toset, &toidx) != 0
264       || (header->module_offset + (toidx + 1) * sizeof (struct module_entry)
265 	  > cache_size))
266     return __GCONV_NOCONV;
267   to_module = &modtab[toidx];
268 
269   /* Avoid copy-only transformations if the user requests.   */
270   if (__builtin_expect (flags & GCONV_AVOID_NOCONV, 0) && fromidx == toidx)
271     return __GCONV_NOCONV;
272 
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)
276     {
277       /* Search through the list to see whether there is a module
278 	 matching the destination character set.  */
279       const struct extra_entry *extra;
280 
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)
290 					      + (extra->module_cnt
291 						 * sizeof (struct extra_entry_module)));
292 
293       if (extra->module_cnt != 0)
294 	{
295 	  /* Use the extra module.  First determine how many steps.  */
296 	  char *fromname;
297 	  int idx;
298 
299 	  *nsteps = extra->module_cnt;
300 	  *handle = result =
301 	    (struct __gconv_step *) malloc (extra->module_cnt
302 					    * sizeof (struct __gconv_step));
303 	  if (result == NULL)
304 	    return __GCONV_NOMEM;
305 
306 	  fromname = (char *) strtab + from_module->canonname_offset;
307 	  idx = 0;
308 	  do
309 	    {
310 	      result[idx].__from_name = fromname;
311 	      fromname = result[idx].__to_name =
312 		(char *) strtab + modtab[extra->module[idx].outname_offset].canonname_offset;
313 
314 	      result[idx].__counter = 1;
315 	      result[idx].__data = NULL;
316 
317 #ifndef STATIC_GCONV
318 	      if (strtab[extra->module[idx].dir_offset] != '\0')
319 		{
320 		  /* Load the module, return handle for it.  */
321 		  int res;
322 
323 		  res = find_module (strtab + extra->module[idx].dir_offset,
324 				     strtab + extra->module[idx].name_offset,
325 				     &result[idx]);
326 		  if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
327 		    {
328 		      /* Something went wrong.  */
329 		      free (result);
330 		      goto try_internal;
331 		    }
332 		}
333 	      else
334 #endif
335 		/* It's a builtin transformation.  */
336 		__gconv_get_builtin_trans (strtab
337 					   + extra->module[idx].name_offset,
338 					   &result[idx]);
339 
340 	    }
341 	  while (++idx < extra->module_cnt);
342 
343 	  return __GCONV_OK;
344 	}
345     }
346 
347  try_internal:
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;
354 
355   /* We will use up to two modules.  Always allocate room for two.  */
356   result = (struct __gconv_step *) malloc (2 * sizeof (struct __gconv_step));
357   if (result == NULL)
358     return __GCONV_NOMEM;
359 
360   *handle = result;
361   *nsteps = 0;
362 
363   /* Generate data structure for conversion to INTERNAL.  */
364   if (fromidx != 0)
365     {
366       result[0].__from_name = (char *) strtab + from_module->canonname_offset;
367       result[0].__to_name = (char *) "INTERNAL";
368 
369       result[0].__counter = 1;
370       result[0].__data = NULL;
371 
372 #ifndef STATIC_GCONV
373       if (strtab[from_module->todir_offset] != '\0')
374 	{
375 	  /* Load the module, return handle for it.  */
376 	  int res = find_module (strtab + from_module->todir_offset,
377 				 strtab + from_module->toname_offset,
378 				 &result[0]);
379 	  if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
380 	    {
381 	      /* Something went wrong.  */
382 	      free (result);
383 	      return res;
384 	    }
385 	}
386       else
387 #endif
388 	/* It's a builtin transformation.  */
389 	__gconv_get_builtin_trans (strtab + from_module->toname_offset,
390 				   &result[0]);
391 
392       ++*nsteps;
393     }
394 
395   /* Generate data structure for conversion from INTERNAL.  */
396   if (toidx != 0)
397     {
398       int idx = *nsteps;
399 
400       result[idx].__from_name = (char *) "INTERNAL";
401       result[idx].__to_name = (char *) strtab + to_module->canonname_offset;
402 
403       result[idx].__counter = 1;
404       result[idx].__data = NULL;
405 
406 #ifndef STATIC_GCONV
407       if (strtab[to_module->fromdir_offset] != '\0')
408 	{
409 	  /* Load the module, return handle for it.  */
410 	  int res = find_module (strtab + to_module->fromdir_offset,
411 				 strtab + to_module->fromname_offset,
412 				 &result[idx]);
413 	  if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
414 	    {
415 	      /* Something went wrong.  */
416 	      if (idx != 0)
417 		__gconv_release_step (&result[0]);
418 	      free (result);
419 	      return res;
420 	    }
421 	}
422       else
423 #endif
424 	/* It's a builtin transformation.  */
425 	__gconv_get_builtin_trans (strtab + to_module->fromname_offset,
426 				   &result[idx]);
427 
428       ++*nsteps;
429     }
430 
431   return __GCONV_OK;
432 }
433 
434 
435 /* Free memory allocated for the transformation record.  */
436 void
437 internal_function
__gconv_release_cache(struct __gconv_step * steps,size_t nsteps)438 __gconv_release_cache (struct __gconv_step *steps, size_t nsteps)
439 {
440   if (__gconv_cache != NULL)
441     /* The only thing we have to deallocate is the record with the
442        steps.  */
443     free (steps);
444 }
445 
446 
447 /* Free all resources if necessary.  */
448 static void __attribute__ ((unused))
free_mem(void)449 free_mem (void)
450 {
451   if (cache_malloced)
452     free (__gconv_cache);
453 #ifdef _POSIX_MAPPED_FILES
454   else
455     __munmap (__gconv_cache, cache_size);
456 #endif
457 }
458 
459 text_set_element (__libc_subfreeres, free_mem);
460