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