1 /* Functions to read locale data files.
2 Copyright (C) 1996-2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
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.
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.
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
28 #ifdef _POSIX_MAPPED_FILES
29 # include <sys/mman.h>
33 #include <not-cancel.h>
34 #include "localeinfo.h"
37 static const size_t _nl_category_num_items
[] =
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
46 #define NO_PAREN(arg, rest...) arg, ##rest
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
55 static const enum value_type
*_nl_value_types
[] =
57 #define DEFINE_CATEGORY(category, category_name, items, a) \
58 [category] = _nl_value_type_##category,
59 #include "categories.def"
60 #undef DEFINE_CATEGORY
66 _nl_intern_locale_data (int category
, const void *data
, size_t datasize
)
71 unsigned int nstrings
;
72 unsigned int strindex
[0];
73 } *const filedata
= data
;
74 struct locale_data
*newdata
;
77 if (__builtin_expect (datasize
< sizeof *filedata
, 0)
78 || __builtin_expect (filedata
->magic
!= LIMAGIC (category
), 0))
85 if (__builtin_expect (filedata
->nstrings
< _nl_category_num_items
[category
],
87 || (__builtin_expect (sizeof *filedata
88 + filedata
->nstrings
* sizeof (unsigned int)
91 /* Insufficient data. */
96 newdata
= malloc (sizeof *newdata
97 + filedata
->nstrings
* sizeof (union locale_data_value
));
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
)
110 size_t idx
= filedata
->strindex
[cnt
];
111 if (__builtin_expect (idx
> (size_t) newdata
->filesize
, 0))
115 __set_errno (EINVAL
);
119 /* Determine the type. There is one special case: the LC_CTYPE
120 category can have more elements than there are in the
121 _nl_value_type_LC_XYZ array. There are all pointers. */
124 #define CATTEST(cat) \
126 assert (cnt < (sizeof (_nl_value_type_LC_##cat) \
127 / sizeof (_nl_value_type_LC_##cat[0]))); \
138 CATTEST (MEASUREMENT
);
139 CATTEST (IDENTIFICATION
);
141 assert (category
== LC_CTYPE
);
145 if ((category
== LC_CTYPE
146 && cnt
>= (sizeof (_nl_value_type_LC_CTYPE
)
147 / sizeof (_nl_value_type_LC_CTYPE
[0])))
148 || __builtin_expect (_nl_value_types
[category
][cnt
] != word
, 1))
149 newdata
->values
[cnt
].string
= newdata
->filedata
+ idx
;
152 if (idx
% __alignof__ (u_int32_t
) != 0)
154 newdata
->values
[cnt
].word
=
155 *((const u_int32_t
*) (newdata
->filedata
+ idx
));
164 _nl_load_locale (struct loaded_l10nfile
*file
, int category
)
169 struct locale_data
*newdata
;
171 int alloc
= ld_mapped
;
176 fd
= open_not_cancel_2 (file
->filename
, O_RDONLY
);
177 if (__builtin_expect (fd
, 0) < 0)
178 /* Cannot open the file. */
181 if (__builtin_expect (__fxstat64 (_STAT_VER
, fd
, &st
), 0) < 0)
184 close_not_cancel_no_status (fd
);
187 if (__builtin_expect (S_ISDIR (st
.st_mode
), 0))
189 /* LOCALE/LC_foo is a directory; open LOCALE/LC_foo/SYS_LC_foo
194 close_not_cancel_no_status (fd
);
196 filenamelen
= strlen (file
->filename
);
197 newp
= (char *) alloca (filenamelen
198 + 5 + _nl_category_name_sizes
[category
] + 1);
199 __mempcpy (__mempcpy (__mempcpy (newp
, file
->filename
, filenamelen
),
201 _nl_category_names
[category
],
202 _nl_category_name_sizes
[category
] + 1);
204 fd
= open_not_cancel_2 (newp
, O_RDONLY
);
205 if (__builtin_expect (fd
, 0) < 0)
208 if (__builtin_expect (__fxstat64 (_STAT_VER
, fd
, &st
), 0) < 0)
212 /* Map in the file's data. */
214 #ifdef _POSIX_MAPPED_FILES
216 /* Linux seems to lack read-only copy-on-write. */
217 # define MAP_COPY MAP_PRIVATE
220 /* Some systems do not have this flag; it is superfluous. */
223 filedata
= __mmap ((caddr_t
) 0, st
.st_size
,
224 PROT_READ
, MAP_FILE
|MAP_COPY
, fd
, 0);
225 if (__builtin_expect (filedata
== MAP_FAILED
, 0))
227 if (__builtin_expect (errno
, ENOSYS
) == ENOSYS
)
229 #endif /* _POSIX_MAPPED_FILES */
230 /* No mmap; allocate a buffer and read from the file. */
232 filedata
= malloc (st
.st_size
);
233 if (filedata
!= NULL
)
235 off_t to_read
= st
.st_size
;
237 char *p
= (char *) filedata
;
240 nread
= read_not_cancel (fd
, p
, to_read
);
241 if (__builtin_expect (nread
, 1) <= 0)
245 __set_errno (EINVAL
); /* Bizarreness going on. */
251 __set_errno (save_err
);
253 #ifdef _POSIX_MAPPED_FILES
256 #endif /* _POSIX_MAPPED_FILES */
258 /* We have mapped the data, so we no longer need the descriptor. */
259 close_not_cancel_no_status (fd
);
261 if (__builtin_expect (filedata
== NULL
, 0))
262 /* We failed to map or read the data. */
265 newdata
= _nl_intern_locale_data (category
, filedata
, st
.st_size
);
266 if (__builtin_expect (newdata
== NULL
, 0))
269 #ifdef _POSIX_MAPPED_FILES
270 if (alloc
== ld_mapped
)
271 __munmap ((caddr_t
) filedata
, st
.st_size
);
276 /* _nl_intern_locale_data leaves us these fields to initialize. */
277 newdata
->name
= NULL
; /* This will be filled if necessary in findlocale.c. */
278 newdata
->alloc
= alloc
;
280 file
->data
= newdata
;
285 _nl_unload_locale (struct locale_data
*locale
)
287 if (locale
->private.cleanup
)
288 (*locale
->private.cleanup
) (locale
);
290 switch (__builtin_expect (locale
->alloc
, ld_mapped
))
293 free ((void *) locale
->filedata
);
296 #ifdef _POSIX_MAPPED_FILES
297 __munmap ((caddr_t
) locale
->filedata
, locale
->filesize
);
300 case ld_archive
: /* Nothing to do. */
304 if (__builtin_expect (locale
->alloc
, ld_mapped
) != ld_archive
)
305 free ((char *) locale
->name
);