Back to index

glibc  2.9
loadlocale.c
Go to the documentation of this file.
00001 /* Functions to read locale data files.
00002    Copyright (C) 1996-2004, 2005, 2006 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <assert.h>
00022 #include <errno.h>
00023 #include <fcntl.h>
00024 #include <locale.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <unistd.h>
00028 #ifdef _POSIX_MAPPED_FILES
00029 # include <sys/mman.h>
00030 #endif
00031 #include <sys/stat.h>
00032 
00033 #include <not-cancel.h>
00034 #include "localeinfo.h"
00035 
00036 
00037 static const size_t _nl_category_num_items[] =
00038 {
00039 #define DEFINE_CATEGORY(category, category_name, items, a) \
00040   [category] = _NL_ITEM_INDEX (_NL_NUM_##category),
00041 #include "categories.def"
00042 #undef DEFINE_CATEGORY
00043 };
00044 
00045 
00046 #define NO_PAREN(arg, rest...) arg, ##rest
00047 
00048 #define DEFINE_CATEGORY(category, category_name, items, a) \
00049 static const enum value_type _nl_value_type_##category[] = { NO_PAREN items };
00050 #define DEFINE_ELEMENT(element, element_name, optstd, type, rest...) \
00051   [_NL_ITEM_INDEX (element)] = type,
00052 #include "categories.def"
00053 #undef DEFINE_CATEGORY
00054 
00055 static const enum value_type *const _nl_value_types[] =
00056 {
00057 #define DEFINE_CATEGORY(category, category_name, items, a) \
00058   [category] = _nl_value_type_##category,
00059 #include "categories.def"
00060 #undef DEFINE_CATEGORY
00061 };
00062 
00063 
00064 struct locale_data *
00065 internal_function
00066 _nl_intern_locale_data (int category, const void *data, size_t datasize)
00067 {
00068   const struct
00069     {
00070       unsigned int magic;
00071       unsigned int nstrings;
00072       unsigned int strindex[0];
00073     } *const filedata = data;
00074   struct locale_data *newdata;
00075   size_t cnt;
00076 
00077   if (__builtin_expect (datasize < sizeof *filedata, 0)
00078       || __builtin_expect (filedata->magic != LIMAGIC (category), 0))
00079     {
00080       /* Bad data file.  */
00081       __set_errno (EINVAL);
00082       return NULL;
00083     }
00084 
00085   if (__builtin_expect (filedata->nstrings < _nl_category_num_items[category],
00086                      0)
00087       || (__builtin_expect (sizeof *filedata
00088                          + filedata->nstrings * sizeof (unsigned int)
00089                          >= datasize, 0)))
00090     {
00091       /* Insufficient data.  */
00092       __set_errno (EINVAL);
00093       return NULL;
00094     }
00095 
00096   newdata = malloc (sizeof *newdata
00097                   + filedata->nstrings * sizeof (union locale_data_value));
00098   if (newdata == NULL)
00099     return NULL;
00100 
00101   newdata->filedata = (void *) filedata;
00102   newdata->filesize = datasize;
00103   newdata->private.data = NULL;
00104   newdata->private.cleanup = NULL;
00105   newdata->usage_count = 0;
00106   newdata->use_translit = 0;
00107   newdata->nstrings = filedata->nstrings;
00108   for (cnt = 0; cnt < newdata->nstrings; ++cnt)
00109     {
00110       size_t idx = filedata->strindex[cnt];
00111       if (__builtin_expect (idx > (size_t) newdata->filesize, 0))
00112        {
00113        puntdata:
00114          free (newdata);
00115          __set_errno (EINVAL);
00116          return NULL;
00117        }
00118 
00119       /* Determine the type.  There is one special case: the LC_CTYPE
00120         category can have more elements than there are in the
00121         _nl_value_type_LC_XYZ array.  There are all pointers.  */
00122       switch (category)
00123        {
00124 #define CATTEST(cat) \
00125        case LC_##cat:                                                       \
00126          assert (cnt < (sizeof (_nl_value_type_LC_##cat)                    \
00127                       / sizeof (_nl_value_type_LC_##cat[0])));       \
00128          break
00129          CATTEST (NUMERIC);
00130          CATTEST (TIME);
00131          CATTEST (COLLATE);
00132          CATTEST (MONETARY);
00133          CATTEST (MESSAGES);
00134          CATTEST (PAPER);
00135          CATTEST (NAME);
00136          CATTEST (ADDRESS);
00137          CATTEST (TELEPHONE);
00138          CATTEST (MEASUREMENT);
00139          CATTEST (IDENTIFICATION);
00140        default:
00141          assert (category == LC_CTYPE);
00142          break;
00143        }
00144 
00145       if ((category == LC_CTYPE
00146           && cnt >= (sizeof (_nl_value_type_LC_CTYPE)
00147                     / sizeof (_nl_value_type_LC_CTYPE[0])))
00148          || __builtin_expect (_nl_value_types[category][cnt] != word, 1))
00149        newdata->values[cnt].string = newdata->filedata + idx;
00150       else
00151        {
00152          if (idx % __alignof__ (u_int32_t) != 0)
00153            goto puntdata;
00154          newdata->values[cnt].word =
00155            *((const u_int32_t *) (newdata->filedata + idx));
00156        }
00157     }
00158 
00159   return newdata;
00160 }
00161 
00162 void
00163 internal_function
00164 _nl_load_locale (struct loaded_l10nfile *file, int category)
00165 {
00166   int fd;
00167   void *filedata;
00168   struct stat64 st;
00169   struct locale_data *newdata;
00170   int save_err;
00171   int alloc = ld_mapped;
00172 
00173   file->decided = 1;
00174   file->data = NULL;
00175 
00176   fd = open_not_cancel_2 (file->filename, O_RDONLY);
00177   if (__builtin_expect (fd, 0) < 0)
00178     /* Cannot open the file.  */
00179     return;
00180 
00181   if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
00182     {
00183     puntfd:
00184       close_not_cancel_no_status (fd);
00185       return;
00186     }
00187   if (__builtin_expect (S_ISDIR (st.st_mode), 0))
00188     {
00189       /* LOCALE/LC_foo is a directory; open LOCALE/LC_foo/SYS_LC_foo
00190            instead.  */
00191       char *newp;
00192       size_t filenamelen;
00193 
00194       close_not_cancel_no_status (fd);
00195 
00196       filenamelen = strlen (file->filename);
00197       newp = (char *) alloca (filenamelen
00198                            + 5 + _nl_category_name_sizes[category] + 1);
00199       __mempcpy (__mempcpy (__mempcpy (newp, file->filename, filenamelen),
00200                          "/SYS_", 5),
00201                _nl_category_names.str + _nl_category_name_idxs[category],
00202                _nl_category_name_sizes[category] + 1);
00203 
00204       fd = open_not_cancel_2 (newp, O_RDONLY);
00205       if (__builtin_expect (fd, 0) < 0)
00206        return;
00207 
00208       if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
00209        goto puntfd;
00210     }
00211 
00212   /* Map in the file's data.  */
00213   save_err = errno;
00214 #ifdef _POSIX_MAPPED_FILES
00215 # ifndef MAP_COPY
00216   /* Linux seems to lack read-only copy-on-write.  */
00217 #  define MAP_COPY MAP_PRIVATE
00218 # endif
00219 # ifndef MAP_FILE
00220   /* Some systems do not have this flag; it is superfluous.  */
00221 #  define MAP_FILE 0
00222 # endif
00223   filedata = __mmap ((caddr_t) 0, st.st_size,
00224                    PROT_READ, MAP_FILE|MAP_COPY, fd, 0);
00225   if (__builtin_expect (filedata == MAP_FAILED, 0))
00226     {
00227       if (__builtin_expect (errno, ENOSYS) == ENOSYS)
00228        {
00229 #endif /* _POSIX_MAPPED_FILES */
00230          /* No mmap; allocate a buffer and read from the file.  */
00231          alloc = ld_malloced;
00232          filedata = malloc (st.st_size);
00233          if (filedata != NULL)
00234            {
00235              off_t to_read = st.st_size;
00236              ssize_t nread;
00237              char *p = (char *) filedata;
00238              while (to_read > 0)
00239               {
00240                 nread = read_not_cancel (fd, p, to_read);
00241                 if (__builtin_expect (nread, 1) <= 0)
00242                   {
00243                     free (filedata);
00244                     if (nread == 0)
00245                      __set_errno (EINVAL); /* Bizarreness going on.  */
00246                     goto puntfd;
00247                   }
00248                 p += nread;
00249                 to_read -= nread;
00250               }
00251              __set_errno (save_err);
00252            }
00253 #ifdef _POSIX_MAPPED_FILES
00254        }
00255     }
00256 #endif /* _POSIX_MAPPED_FILES */
00257 
00258   /* We have mapped the data, so we no longer need the descriptor.  */
00259   close_not_cancel_no_status (fd);
00260 
00261   if (__builtin_expect (filedata == NULL, 0))
00262     /* We failed to map or read the data.  */
00263     return;
00264 
00265   newdata = _nl_intern_locale_data (category, filedata, st.st_size);
00266   if (__builtin_expect (newdata == NULL, 0))
00267     /* Bad data.  */
00268     {
00269 #ifdef _POSIX_MAPPED_FILES
00270       if (alloc == ld_mapped)
00271        __munmap ((caddr_t) filedata, st.st_size);
00272 #endif
00273       return;
00274     }
00275 
00276   /* _nl_intern_locale_data leaves us these fields to initialize.  */
00277   newdata->name = NULL;     /* This will be filled if necessary in findlocale.c. */
00278   newdata->alloc = alloc;
00279 
00280   file->data = newdata;
00281 }
00282 
00283 void
00284 internal_function
00285 _nl_unload_locale (struct locale_data *locale)
00286 {
00287   if (locale->private.cleanup)
00288     (*locale->private.cleanup) (locale);
00289 
00290   switch (__builtin_expect (locale->alloc, ld_mapped))
00291     {
00292     case ld_malloced:
00293       free ((void *) locale->filedata);
00294       break;
00295     case ld_mapped:
00296 #ifdef _POSIX_MAPPED_FILES
00297       __munmap ((caddr_t) locale->filedata, locale->filesize);
00298       break;
00299 #endif
00300     case ld_archive:        /* Nothing to do.  */
00301       break;
00302     }
00303 
00304   if (__builtin_expect (locale->alloc, ld_mapped) != ld_archive)
00305     free ((char *) locale->name);
00306 
00307   free (locale);
00308 }