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