]> 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, 1997, 1998, 1999 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include <byteswap.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/mman.h>
28 #include <sys/stat.h>
29
30 #include "localeinfo.h"
31
32
33 static const size_t _nl_category_num_items[] =
34 {
35 #define DEFINE_CATEGORY(category, category_name, items, a) \
36 [category] = _NL_ITEM_INDEX (_NL_NUM_##category),
37 #include "categories.def"
38 #undef DEFINE_CATEGORY
39 };
40
41
42 #define NO_PAREN(arg, rest...) arg, ##rest
43
44 #define DEFINE_CATEGORY(category, category_name, items, a) \
45 static const enum value_type _nl_value_type_##category[] = { NO_PAREN items };
46 #define DEFINE_ELEMENT(element, element_name, optstd, type, rest...) \
47 [_NL_ITEM_INDEX (element)] = type,
48 #include "categories.def"
49 #undef DEFINE_CATEGORY
50
51 static const enum value_type *_nl_value_types[] =
52 {
53 #define DEFINE_CATEGORY(category, category_name, items, a) \
54 [category] = _nl_value_type_##category,
55 #include "categories.def"
56 #undef DEFINE_CATEGORY
57 };
58
59
60 void
61 _nl_load_locale (struct loaded_l10nfile *file, int category)
62 {
63 int fd;
64 struct
65 {
66 unsigned int magic;
67 unsigned int nstrings;
68 unsigned int strindex[0];
69 } *filedata;
70 struct stat st;
71 struct locale_data *newdata;
72 int save_err;
73 int swap = 0;
74 int mmaped = 1;
75 size_t cnt;
76 inline unsigned int SWAP (const unsigned int *inw)
77 {
78 if (!swap)
79 return *inw;
80 return bswap_32 (*inw);
81 }
82
83 file->decided = 1;
84 file->data = NULL;
85
86 fd = __open (file->filename, O_RDONLY);
87 if (fd < 0)
88 /* Cannot open the file. */
89 return;
90
91 if (__fstat (fd, &st) < 0)
92 goto puntfd;
93 if (S_ISDIR (st.st_mode))
94 {
95 /* LOCALE/LC_foo is a directory; open LOCALE/LC_foo/SYS_LC_foo
96 instead. */
97 char *newp;
98
99 __close (fd);
100
101 newp = (char *) alloca (strlen (file->filename)
102 + 5 + _nl_category_name_sizes[category] + 1);
103 __stpcpy (__stpcpy (__stpcpy (newp, file->filename), "/SYS_"),
104 _nl_category_names[category]);
105
106 fd = __open (newp, O_RDONLY);
107 if (fd < 0)
108 return;
109
110 if (__fstat (fd, &st) < 0)
111 goto puntfd;
112 }
113
114 /* Map in the file's data. */
115 save_err = errno;
116 #ifndef MAP_COPY
117 /* Linux seems to lack read-only copy-on-write. */
118 #define MAP_COPY MAP_PRIVATE
119 #endif
120 #ifndef MAP_FILE
121 /* Some systems do not have this flag; it is superfluous. */
122 #define MAP_FILE 0
123 #endif
124 #ifndef MAP_INHERIT
125 /* Some systems might lack this; they lose. */
126 #define MAP_INHERIT 0
127 #endif
128 filedata = (void *) __mmap ((caddr_t) 0, st.st_size, PROT_READ,
129 MAP_FILE|MAP_COPY|MAP_INHERIT, fd, 0);
130 if ((void *) filedata == MAP_FAILED)
131 {
132 if (errno == ENOSYS)
133 {
134 /* No mmap; allocate a buffer and read from the file. */
135 mmaped = 0;
136 filedata = malloc (st.st_size);
137 if (filedata != NULL)
138 {
139 off_t to_read = st.st_size;
140 ssize_t nread;
141 char *p = (char *) filedata;
142 while (to_read > 0)
143 {
144 nread = __read (fd, p, to_read);
145 if (nread <= 0)
146 {
147 free (filedata);
148 if (nread == 0)
149 __set_errno (EINVAL); /* Bizarreness going on. */
150 goto puntfd;
151 }
152 p += nread;
153 to_read -= nread;
154 }
155 }
156 else
157 goto puntfd;
158 __set_errno (save_err);
159 }
160 else
161 goto puntfd;
162 }
163 else if (st.st_size < sizeof (*filedata))
164 /* This cannot be a locale data file since it's too small. */
165 goto puntfd;
166
167 if (filedata->magic == LIMAGIC (category))
168 /* Good data file in our byte order. */
169 swap = 0;
170 else
171 {
172 /* Try the other byte order. */
173 swap = 1;
174 if (SWAP (&filedata->magic) != LIMAGIC (category))
175 /* Bad data file in either byte order. */
176 {
177 puntmap:
178 __munmap ((caddr_t) filedata, st.st_size);
179 puntfd:
180 __close (fd);
181 return;
182 }
183 }
184
185 #define W(word) SWAP (&(word))
186
187 if (W (filedata->nstrings) < _nl_category_num_items[category] ||
188 (sizeof *filedata + W (filedata->nstrings) * sizeof (unsigned int)
189 >= (size_t) st.st_size))
190 {
191 /* Insufficient data. */
192 __set_errno (EINVAL);
193 goto puntmap;
194 }
195
196 newdata = malloc (sizeof *newdata
197 + (_nl_category_num_items[category]
198 * sizeof (union locale_data_value)));
199 if (! newdata)
200 goto puntmap;
201
202 newdata->name = NULL; /* This will be filled if necessary in findlocale.c. */
203 newdata->filedata = (void *) filedata;
204 newdata->filesize = st.st_size;
205 newdata->mmaped = mmaped;
206 newdata->usage_count = 0;
207 newdata->nstrings = _nl_category_num_items[category];
208 for (cnt = 0; cnt < newdata->nstrings; ++cnt)
209 {
210 off_t idx = W (filedata->strindex[cnt]);
211 if (idx >= newdata->filesize)
212 {
213 free (newdata);
214 __set_errno (EINVAL);
215 goto puntmap;
216 }
217 if (_nl_value_types[category][cnt] == word)
218 newdata->values[cnt].word = W (*((u_int32_t *) (newdata->filedata
219 + idx)));
220 else
221 newdata->values[cnt].string = newdata->filedata + idx;
222 }
223
224 __close (fd);
225 file->data = newdata;
226 }
227
228 void
229 _nl_unload_locale (struct locale_data *locale)
230 {
231 if (locale->mmaped)
232 __munmap ((caddr_t) locale->filedata, locale->filesize);
233 else
234 free ((void *) locale->filedata);
235
236 free (locale);
237 }