]> git.ipfire.org Git - thirdparty/glibc.git/blame - locale/loadlocale.c
Merge branch 'master' of ssh://sourceware.org/git/glibc
[thirdparty/glibc.git] / locale / loadlocale.c
CommitLineData
ce4d8b66 1/* Functions to read locale data files.
f095bb72 2 Copyright (C) 1996-2004, 2005, 2006, 2010 Free Software Foundation, Inc.
c84142e8 3 This file is part of the GNU C Library.
4b10dd6c 4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
933e73fa 5
c84142e8 6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
933e73fa 10
c84142e8
UD
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
41bdb6e2 14 Lesser General Public License for more details.
933e73fa 15
41bdb6e2
AJ
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. */
933e73fa 20
e43e0dd6 21#include <assert.h>
933e73fa
RM
22#include <errno.h>
23#include <fcntl.h>
3e154a6f 24#include <locale.h>
933e73fa
RM
25#include <stdlib.h>
26#include <string.h>
7a12c6bb 27#include <unistd.h>
b6aa34eb
UD
28#ifdef _POSIX_MAPPED_FILES
29# include <sys/mman.h>
30#endif
7a12c6bb
RM
31#include <sys/stat.h>
32
72ef277e 33#include <not-cancel.h>
933e73fa
RM
34#include "localeinfo.h"
35
7a12c6bb
RM
36
37static const size_t _nl_category_num_items[] =
19bc17a9 38{
4b10dd6c 39#define DEFINE_CATEGORY(category, category_name, items, a) \
19bc17a9 40 [category] = _NL_ITEM_INDEX (_NL_NUM_##category),
933e73fa
RM
41#include "categories.def"
42#undef DEFINE_CATEGORY
19bc17a9
RM
43};
44
45
46#define NO_PAREN(arg, rest...) arg, ##rest
47
4b10dd6c 48#define DEFINE_CATEGORY(category, category_name, items, a) \
19bc17a9
RM
49static const enum value_type _nl_value_type_##category[] = { NO_PAREN items };
50#define DEFINE_ELEMENT(element, element_name, optstd, type, rest...) \
51 [_NL_ITEM_INDEX (element)] = type,
52#include "categories.def"
53#undef DEFINE_CATEGORY
54
aa87e915 55static const enum value_type *const _nl_value_types[] =
19bc17a9 56{
4b10dd6c 57#define DEFINE_CATEGORY(category, category_name, items, a) \
19bc17a9
RM
58 [category] = _nl_value_type_##category,
59#include "categories.def"
60#undef DEFINE_CATEGORY
61};
62
933e73fa 63
f095bb72 64struct __locale_data *
cb09a2cd
RM
65internal_function
66_nl_intern_locale_data (int category, const void *data, size_t datasize)
933e73fa 67{
cb09a2cd 68 const struct
933e73fa
RM
69 {
70 unsigned int magic;
71 unsigned int nstrings;
72 unsigned int strindex[0];
cb09a2cd 73 } *const filedata = data;
f095bb72 74 struct __locale_data *newdata;
cb09a2cd
RM
75 size_t cnt;
76
77 if (__builtin_expect (datasize < sizeof *filedata, 0)
78 || __builtin_expect (filedata->magic != LIMAGIC (category), 0))
79 {
80 /* Bad data file. */
81 __set_errno (EINVAL);
82 return NULL;
83 }
84
85 if (__builtin_expect (filedata->nstrings < _nl_category_num_items[category],
86 0)
87 || (__builtin_expect (sizeof *filedata
88 + filedata->nstrings * sizeof (unsigned int)
89 >= datasize, 0)))
90 {
91 /* Insufficient data. */
92 __set_errno (EINVAL);
93 return NULL;
94 }
95
96 newdata = malloc (sizeof *newdata
97 + filedata->nstrings * sizeof (union locale_data_value));
98 if (newdata == NULL)
99 return NULL;
100
101 newdata->filedata = (void *) filedata;
102 newdata->filesize = datasize;
df9f41c9
RM
103 newdata->private.data = NULL;
104 newdata->private.cleanup = NULL;
cb09a2cd
RM
105 newdata->usage_count = 0;
106 newdata->use_translit = 0;
107 newdata->nstrings = filedata->nstrings;
108 for (cnt = 0; cnt < newdata->nstrings; ++cnt)
109 {
110 size_t idx = filedata->strindex[cnt];
6dd67bd5 111 if (__builtin_expect (idx > (size_t) newdata->filesize, 0))
cb09a2cd
RM
112 {
113 puntdata:
114 free (newdata);
115 __set_errno (EINVAL);
116 return NULL;
117 }
fe6ce170
UD
118
119 /* Determine the type. There is one special case: the LC_CTYPE
120 category can have more elements than there are in the
121 _nl_value_type_LC_XYZ array. There are all pointers. */
122 switch (category)
123 {
124#define CATTEST(cat) \
125 case LC_##cat: \
126 assert (cnt < (sizeof (_nl_value_type_LC_##cat) \
127 / sizeof (_nl_value_type_LC_##cat[0]))); \
128 break
129 CATTEST (NUMERIC);
130 CATTEST (TIME);
131 CATTEST (COLLATE);
132 CATTEST (MONETARY);
133 CATTEST (MESSAGES);
134 CATTEST (PAPER);
135 CATTEST (NAME);
136 CATTEST (ADDRESS);
137 CATTEST (TELEPHONE);
138 CATTEST (MEASUREMENT);
139 CATTEST (IDENTIFICATION);
140 default:
141 assert (category == LC_CTYPE);
142 break;
143 }
144
145 if ((category == LC_CTYPE
146 && cnt >= (sizeof (_nl_value_type_LC_CTYPE)
147 / sizeof (_nl_value_type_LC_CTYPE[0])))
148 || __builtin_expect (_nl_value_types[category][cnt] != word, 1))
149 newdata->values[cnt].string = newdata->filedata + idx;
150 else
cb09a2cd
RM
151 {
152 if (idx % __alignof__ (u_int32_t) != 0)
153 goto puntdata;
154 newdata->values[cnt].word =
155 *((const u_int32_t *) (newdata->filedata + idx));
156 }
cb09a2cd
RM
157 }
158
159 return newdata;
160}
161
162void
163internal_function
164_nl_load_locale (struct loaded_l10nfile *file, int category)
165{
166 int fd;
167 void *filedata;
8edf6e0d 168 struct stat64 st;
f095bb72 169 struct __locale_data *newdata;
7a12c6bb 170 int save_err;
cb09a2cd 171 int alloc = ld_mapped;
933e73fa 172
7a12c6bb
RM
173 file->decided = 1;
174 file->data = NULL;
175
72ef277e 176 fd = open_not_cancel_2 (file->filename, O_RDONLY);
3a31f6f4 177 if (__builtin_expect (fd, 0) < 0)
7a12c6bb
RM
178 /* Cannot open the file. */
179 return;
180
8edf6e0d 181 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
cb09a2cd
RM
182 {
183 puntfd:
72ef277e 184 close_not_cancel_no_status (fd);
cb09a2cd
RM
185 return;
186 }
3a31f6f4 187 if (__builtin_expect (S_ISDIR (st.st_mode), 0))
933e73fa 188 {
7a12c6bb 189 /* LOCALE/LC_foo is a directory; open LOCALE/LC_foo/SYS_LC_foo
f095bb72 190 instead. */
7a12c6bb 191 char *newp;
b6aa34eb 192 size_t filenamelen;
036cc82f 193
72ef277e 194 close_not_cancel_no_status (fd);
7a12c6bb 195
b6aa34eb
UD
196 filenamelen = strlen (file->filename);
197 newp = (char *) alloca (filenamelen
7a12c6bb 198 + 5 + _nl_category_name_sizes[category] + 1);
b6aa34eb
UD
199 __mempcpy (__mempcpy (__mempcpy (newp, file->filename, filenamelen),
200 "/SYS_", 5),
9446614c 201 _nl_category_names.str + _nl_category_name_idxs[category],
b6aa34eb 202 _nl_category_name_sizes[category] + 1);
7a12c6bb 203
72ef277e 204 fd = open_not_cancel_2 (newp, O_RDONLY);
3a31f6f4 205 if (__builtin_expect (fd, 0) < 0)
7a12c6bb
RM
206 return;
207
8edf6e0d 208 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
7a12c6bb 209 goto puntfd;
933e73fa
RM
210 }
211
7a12c6bb
RM
212 /* Map in the file's data. */
213 save_err = errno;
b6aa34eb
UD
214#ifdef _POSIX_MAPPED_FILES
215# ifndef MAP_COPY
7a12c6bb 216 /* Linux seems to lack read-only copy-on-write. */
b6aa34eb
UD
217# define MAP_COPY MAP_PRIVATE
218# endif
219# ifndef MAP_FILE
7a12c6bb 220 /* Some systems do not have this flag; it is superfluous. */
b6aa34eb
UD
221# define MAP_FILE 0
222# endif
cb09a2cd
RM
223 filedata = __mmap ((caddr_t) 0, st.st_size,
224 PROT_READ, MAP_FILE|MAP_COPY, fd, 0);
225 if (__builtin_expect (filedata == MAP_FAILED, 0))
7a12c6bb 226 {
8b2f25c2 227 filedata = NULL;
3a31f6f4 228 if (__builtin_expect (errno, ENOSYS) == ENOSYS)
7a12c6bb 229 {
b6aa34eb 230#endif /* _POSIX_MAPPED_FILES */
7a12c6bb 231 /* No mmap; allocate a buffer and read from the file. */
cb09a2cd 232 alloc = ld_malloced;
7a12c6bb
RM
233 filedata = malloc (st.st_size);
234 if (filedata != NULL)
235 {
236 off_t to_read = st.st_size;
237 ssize_t nread;
238 char *p = (char *) filedata;
239 while (to_read > 0)
240 {
72ef277e 241 nread = read_not_cancel (fd, p, to_read);
3a31f6f4 242 if (__builtin_expect (nread, 1) <= 0)
7a12c6bb
RM
243 {
244 free (filedata);
245 if (nread == 0)
c4029823 246 __set_errno (EINVAL); /* Bizarreness going on. */
7a12c6bb
RM
247 goto puntfd;
248 }
249 p += nread;
250 to_read -= nread;
251 }
cb09a2cd 252 __set_errno (save_err);
7a12c6bb 253 }
b6aa34eb 254#ifdef _POSIX_MAPPED_FILES
7a12c6bb 255 }
7a12c6bb 256 }
b6aa34eb 257#endif /* _POSIX_MAPPED_FILES */
933e73fa 258
cb09a2cd 259 /* We have mapped the data, so we no longer need the descriptor. */
72ef277e 260 close_not_cancel_no_status (fd);
cb09a2cd
RM
261
262 if (__builtin_expect (filedata == NULL, 0))
263 /* We failed to map or read the data. */
264 return;
265
266 newdata = _nl_intern_locale_data (category, filedata, st.st_size);
267 if (__builtin_expect (newdata == NULL, 0))
268 /* Bad data. */
933e73fa 269 {
b6aa34eb 270#ifdef _POSIX_MAPPED_FILES
cb09a2cd
RM
271 if (alloc == ld_mapped)
272 __munmap ((caddr_t) filedata, st.st_size);
b6aa34eb 273#endif
4a33c2f5 274 return;
933e73fa
RM
275 }
276
cb09a2cd 277 /* _nl_intern_locale_data leaves us these fields to initialize. */
7a12c6bb 278 newdata->name = NULL; /* This will be filled if necessary in findlocale.c. */
cb09a2cd 279 newdata->alloc = alloc;
933e73fa 280
7a12c6bb 281 file->data = newdata;
933e73fa 282}
a5a0310d
UD
283
284void
cb09a2cd 285internal_function
f095bb72 286_nl_unload_locale (struct __locale_data *locale)
a5a0310d 287{
df9f41c9
RM
288 if (locale->private.cleanup)
289 (*locale->private.cleanup) (locale);
290
cb09a2cd
RM
291 switch (__builtin_expect (locale->alloc, ld_mapped))
292 {
293 case ld_malloced:
294 free ((void *) locale->filedata);
295 break;
296 case ld_mapped:
b6aa34eb 297#ifdef _POSIX_MAPPED_FILES
cb09a2cd
RM
298 __munmap ((caddr_t) locale->filedata, locale->filesize);
299 break;
b6aa34eb 300#endif
cb09a2cd
RM
301 case ld_archive: /* Nothing to do. */
302 break;
303 }
304
305 if (__builtin_expect (locale->alloc, ld_mapped) != ld_archive)
306 free ((char *) locale->name);
a5a0310d
UD
307
308 free (locale);
309}