]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/loadlocale.c
Update.
[thirdparty/glibc.git] / locale / loadlocale.c
1 /* Functions to read locale data files.
2 Copyright (C) 1996-2001, 2002, 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
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 <assert.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <locale.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #ifdef _POSIX_MAPPED_FILES
29 # include <sys/mman.h>
30 #endif
31 #include <sys/stat.h>
32
33 #include <not-cancel.h>
34 #include "localeinfo.h"
35
36
37 static const size_t _nl_category_num_items[] =
38 {
39 #define DEFINE_CATEGORY(category, category_name, items, a) \
40 [category] = _NL_ITEM_INDEX (_NL_NUM_##category),
41 #include "categories.def"
42 #undef DEFINE_CATEGORY
43 };
44
45
46 #define NO_PAREN(arg, rest...) arg, ##rest
47
48 #define DEFINE_CATEGORY(category, category_name, items, a) \
49 static 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
55 static const enum value_type *_nl_value_types[] =
56 {
57 #define DEFINE_CATEGORY(category, category_name, items, a) \
58 [category] = _nl_value_type_##category,
59 #include "categories.def"
60 #undef DEFINE_CATEGORY
61 };
62
63
64 struct locale_data *
65 internal_function
66 _nl_intern_locale_data (int category, const void *data, size_t datasize)
67 {
68 const struct
69 {
70 unsigned int magic;
71 unsigned int nstrings;
72 unsigned int strindex[0];
73 } *const filedata = data;
74 struct locale_data *newdata;
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;
103 newdata->private.data = NULL;
104 newdata->private.cleanup = NULL;
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];
111 if (__builtin_expect (idx > (size_t) newdata->filesize, 0))
112 {
113 puntdata:
114 free (newdata);
115 __set_errno (EINVAL);
116 return NULL;
117 }
118 if (__builtin_expect (_nl_value_types[category][cnt] == word, 0))
119 {
120 if (idx % __alignof__ (u_int32_t) != 0)
121 goto puntdata;
122 newdata->values[cnt].word =
123 *((const u_int32_t *) (newdata->filedata + idx));
124 }
125 else
126 newdata->values[cnt].string = newdata->filedata + idx;
127 }
128
129 return newdata;
130 }
131
132 void
133 internal_function
134 _nl_load_locale (struct loaded_l10nfile *file, int category)
135 {
136 int fd;
137 void *filedata;
138 struct stat64 st;
139 struct locale_data *newdata;
140 int save_err;
141 int alloc = ld_mapped;
142
143 file->decided = 1;
144 file->data = NULL;
145
146 fd = open_not_cancel_2 (file->filename, O_RDONLY);
147 if (__builtin_expect (fd, 0) < 0)
148 /* Cannot open the file. */
149 return;
150
151 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
152 {
153 puntfd:
154 close_not_cancel_no_status (fd);
155 return;
156 }
157 if (__builtin_expect (S_ISDIR (st.st_mode), 0))
158 {
159 /* LOCALE/LC_foo is a directory; open LOCALE/LC_foo/SYS_LC_foo
160 instead. */
161 char *newp;
162 size_t filenamelen;
163
164 close_not_cancel_no_status (fd);
165
166 filenamelen = strlen (file->filename);
167 newp = (char *) alloca (filenamelen
168 + 5 + _nl_category_name_sizes[category] + 1);
169 __mempcpy (__mempcpy (__mempcpy (newp, file->filename, filenamelen),
170 "/SYS_", 5),
171 _nl_category_names[category],
172 _nl_category_name_sizes[category] + 1);
173
174 fd = open_not_cancel_2 (newp, O_RDONLY);
175 if (__builtin_expect (fd, 0) < 0)
176 return;
177
178 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
179 goto puntfd;
180 }
181
182 /* Map in the file's data. */
183 save_err = errno;
184 #ifdef _POSIX_MAPPED_FILES
185 # ifndef MAP_COPY
186 /* Linux seems to lack read-only copy-on-write. */
187 # define MAP_COPY MAP_PRIVATE
188 # endif
189 # ifndef MAP_FILE
190 /* Some systems do not have this flag; it is superfluous. */
191 # define MAP_FILE 0
192 # endif
193 filedata = __mmap ((caddr_t) 0, st.st_size,
194 PROT_READ, MAP_FILE|MAP_COPY, fd, 0);
195 if (__builtin_expect (filedata == MAP_FAILED, 0))
196 {
197 if (__builtin_expect (errno, ENOSYS) == ENOSYS)
198 {
199 #endif /* _POSIX_MAPPED_FILES */
200 /* No mmap; allocate a buffer and read from the file. */
201 alloc = ld_malloced;
202 filedata = malloc (st.st_size);
203 if (filedata != NULL)
204 {
205 off_t to_read = st.st_size;
206 ssize_t nread;
207 char *p = (char *) filedata;
208 while (to_read > 0)
209 {
210 nread = read_not_cancel (fd, p, to_read);
211 if (__builtin_expect (nread, 1) <= 0)
212 {
213 free (filedata);
214 if (nread == 0)
215 __set_errno (EINVAL); /* Bizarreness going on. */
216 goto puntfd;
217 }
218 p += nread;
219 to_read -= nread;
220 }
221 __set_errno (save_err);
222 }
223 #ifdef _POSIX_MAPPED_FILES
224 }
225 }
226 #endif /* _POSIX_MAPPED_FILES */
227
228 /* We have mapped the data, so we no longer need the descriptor. */
229 close_not_cancel_no_status (fd);
230
231 if (__builtin_expect (filedata == NULL, 0))
232 /* We failed to map or read the data. */
233 return;
234
235 newdata = _nl_intern_locale_data (category, filedata, st.st_size);
236 if (__builtin_expect (newdata == NULL, 0))
237 /* Bad data. */
238 {
239 #ifdef _POSIX_MAPPED_FILES
240 if (alloc == ld_mapped)
241 __munmap ((caddr_t) filedata, st.st_size);
242 #endif
243 return;
244 }
245
246 /* _nl_intern_locale_data leaves us these fields to initialize. */
247 newdata->name = NULL; /* This will be filled if necessary in findlocale.c. */
248 newdata->alloc = alloc;
249
250 file->data = newdata;
251 }
252
253 void
254 internal_function
255 _nl_unload_locale (struct locale_data *locale)
256 {
257 if (locale->private.cleanup)
258 (*locale->private.cleanup) (locale);
259
260 switch (__builtin_expect (locale->alloc, ld_mapped))
261 {
262 case ld_malloced:
263 free ((void *) locale->filedata);
264 break;
265 case ld_mapped:
266 #ifdef _POSIX_MAPPED_FILES
267 __munmap ((caddr_t) locale->filedata, locale->filesize);
268 break;
269 #endif
270 case ld_archive: /* Nothing to do. */
271 break;
272 }
273
274 if (__builtin_expect (locale->alloc, ld_mapped) != ld_archive)
275 free ((char *) locale->name);
276
277 free (locale);
278 }