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