]> git.ipfire.org Git - thirdparty/glibc.git/blame - locale/loadlocale.c
2002-08-08 Roland McGrath <roland@redhat.com>
[thirdparty/glibc.git] / locale / loadlocale.c
CommitLineData
ce4d8b66 1/* Functions to read locale data files.
3e154a6f 2 Copyright (C) 1996,1997,1998,1999,2000,2001 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
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
54static const enum value_type *_nl_value_types[] =
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
7a12c6bb
RM
63void
64_nl_load_locale (struct loaded_l10nfile *file, int category)
933e73fa 65{
933e73fa
RM
66 int fd;
67 struct
68 {
69 unsigned int magic;
70 unsigned int nstrings;
71 unsigned int strindex[0];
72 } *filedata;
8edf6e0d 73 struct stat64 st;
933e73fa 74 struct locale_data *newdata;
7a12c6bb 75 int save_err;
c84142e8 76 int mmaped = 1;
7a12c6bb 77 size_t cnt;
933e73fa 78
7a12c6bb
RM
79 file->decided = 1;
80 file->data = NULL;
81
82 fd = __open (file->filename, O_RDONLY);
3a31f6f4 83 if (__builtin_expect (fd, 0) < 0)
7a12c6bb
RM
84 /* Cannot open the file. */
85 return;
86
8edf6e0d 87 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
7a12c6bb 88 goto puntfd;
3a31f6f4 89 if (__builtin_expect (S_ISDIR (st.st_mode), 0))
933e73fa 90 {
7a12c6bb
RM
91 /* LOCALE/LC_foo is a directory; open LOCALE/LC_foo/SYS_LC_foo
92 instead. */
93 char *newp;
b6aa34eb 94 size_t filenamelen;
036cc82f 95
7a12c6bb
RM
96 __close (fd);
97
b6aa34eb
UD
98 filenamelen = strlen (file->filename);
99 newp = (char *) alloca (filenamelen
7a12c6bb 100 + 5 + _nl_category_name_sizes[category] + 1);
b6aa34eb
UD
101 __mempcpy (__mempcpy (__mempcpy (newp, file->filename, filenamelen),
102 "/SYS_", 5),
103 _nl_category_names[category],
104 _nl_category_name_sizes[category] + 1);
7a12c6bb
RM
105
106 fd = __open (newp, O_RDONLY);
3a31f6f4 107 if (__builtin_expect (fd, 0) < 0)
7a12c6bb
RM
108 return;
109
8edf6e0d 110 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
7a12c6bb 111 goto puntfd;
933e73fa
RM
112 }
113
7a12c6bb
RM
114 /* Map in the file's data. */
115 save_err = errno;
b6aa34eb
UD
116#ifdef _POSIX_MAPPED_FILES
117# ifndef MAP_COPY
7a12c6bb 118 /* Linux seems to lack read-only copy-on-write. */
b6aa34eb
UD
119# define MAP_COPY MAP_PRIVATE
120# endif
121# ifndef MAP_FILE
7a12c6bb 122 /* Some systems do not have this flag; it is superfluous. */
b6aa34eb
UD
123# define MAP_FILE 0
124# endif
125# ifndef MAP_INHERIT
7a12c6bb 126 /* Some systems might lack this; they lose. */
b6aa34eb
UD
127# define MAP_INHERIT 0
128# endif
7a12c6bb
RM
129 filedata = (void *) __mmap ((caddr_t) 0, st.st_size, PROT_READ,
130 MAP_FILE|MAP_COPY|MAP_INHERIT, fd, 0);
3a31f6f4 131 if (__builtin_expect ((void *) filedata != MAP_FAILED, 1))
b6aa34eb 132 {
3a31f6f4 133 if (__builtin_expect (st.st_size < sizeof (*filedata), 0))
b6aa34eb
UD
134 /* This cannot be a locale data file since it's too small. */
135 goto puntfd;
136 }
137 else
7a12c6bb 138 {
3a31f6f4 139 if (__builtin_expect (errno, ENOSYS) == ENOSYS)
7a12c6bb 140 {
b6aa34eb 141#endif /* _POSIX_MAPPED_FILES */
7a12c6bb 142 /* No mmap; allocate a buffer and read from the file. */
c84142e8 143 mmaped = 0;
7a12c6bb
RM
144 filedata = malloc (st.st_size);
145 if (filedata != NULL)
146 {
147 off_t to_read = st.st_size;
148 ssize_t nread;
149 char *p = (char *) filedata;
150 while (to_read > 0)
151 {
152 nread = __read (fd, p, to_read);
3a31f6f4 153 if (__builtin_expect (nread, 1) <= 0)
7a12c6bb
RM
154 {
155 free (filedata);
156 if (nread == 0)
c4029823 157 __set_errno (EINVAL); /* Bizarreness going on. */
7a12c6bb
RM
158 goto puntfd;
159 }
160 p += nread;
161 to_read -= nread;
162 }
163 }
164 else
165 goto puntfd;
c4029823 166 __set_errno (save_err);
b6aa34eb 167#ifdef _POSIX_MAPPED_FILES
7a12c6bb
RM
168 }
169 else
170 goto puntfd;
171 }
b6aa34eb 172#endif /* _POSIX_MAPPED_FILES */
933e73fa 173
3a31f6f4 174 if (__builtin_expect (filedata->magic != LIMAGIC (category), 0))
4a33c2f5 175 /* Bad data file in either byte order. */
933e73fa 176 {
4a33c2f5 177 puntmap:
b6aa34eb 178#ifdef _POSIX_MAPPED_FILES
4a33c2f5 179 __munmap ((caddr_t) filedata, st.st_size);
b6aa34eb 180#endif
4a33c2f5
UD
181 puntfd:
182 __close (fd);
183 return;
933e73fa
RM
184 }
185
3a31f6f4
UD
186 if (__builtin_expect (filedata->nstrings < _nl_category_num_items[category],
187 0)
188 || (__builtin_expect (sizeof *filedata
189 + filedata->nstrings * sizeof (unsigned int)
190 >= (size_t) st.st_size, 0)))
933e73fa
RM
191 {
192 /* Insufficient data. */
c4029823 193 __set_errno (EINVAL);
933e73fa
RM
194 goto puntmap;
195 }
196
4b10dd6c 197 newdata = malloc (sizeof *newdata
bece1bc4 198 + filedata->nstrings * sizeof (union locale_data_value));
3a31f6f4 199 if (newdata == NULL)
933e73fa
RM
200 goto puntmap;
201
7a12c6bb 202 newdata->name = NULL; /* This will be filled if necessary in findlocale.c. */
933e73fa
RM
203 newdata->filedata = (void *) filedata;
204 newdata->filesize = st.st_size;
c84142e8
UD
205 newdata->mmaped = mmaped;
206 newdata->usage_count = 0;
323fb88d
UD
207 newdata->use_translit = 0;
208 newdata->options = NULL;
bece1bc4 209 newdata->nstrings = filedata->nstrings;
7a12c6bb 210 for (cnt = 0; cnt < newdata->nstrings; ++cnt)
933e73fa 211 {
4a33c2f5 212 off_t idx = filedata->strindex[cnt];
3a31f6f4 213 if (__builtin_expect (idx > newdata->filesize, 0))
933e73fa
RM
214 {
215 free (newdata);
c4029823 216 __set_errno (EINVAL);
933e73fa
RM
217 goto puntmap;
218 }
3a31f6f4 219 if (__builtin_expect (_nl_value_types[category][cnt] == word, 0))
e43e0dd6 220 {
09bf6406 221 assert (idx % __alignof__ (u_int32_t) == 0);
e43e0dd6
UD
222 newdata->values[cnt].word =
223 *((u_int32_t *) (newdata->filedata + idx));
224 }
19bc17a9 225 else
7a12c6bb 226 newdata->values[cnt].string = newdata->filedata + idx;
933e73fa
RM
227 }
228
f0bf9cb9 229 __close (fd);
7a12c6bb 230 file->data = newdata;
933e73fa 231}
a5a0310d
UD
232
233void
234_nl_unload_locale (struct locale_data *locale)
235{
b6aa34eb 236#ifdef _POSIX_MAPPED_FILES
3a31f6f4 237 if (__builtin_expect (locale->mmaped, 1))
a5a0310d
UD
238 __munmap ((caddr_t) locale->filedata, locale->filesize);
239 else
b6aa34eb 240#endif
a5a0310d
UD
241 free ((void *) locale->filedata);
242
6e5f8285 243 free ((char *) locale->options);
f1ba489e 244 free ((char *) locale->name);
a5a0310d
UD
245 free (locale);
246}