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