1 /* Copyright (C) 1996-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
25 #ifdef _POSIX_MAPPED_FILES
26 # include <sys/mman.h>
29 #include "localeinfo.h"
30 #include "../iconv/gconv_charset.h"
31 #include "../iconv/gconv_int.h"
34 #ifdef NL_CURRENT_INDIRECT
35 # define DEFINE_CATEGORY(category, category_name, items, a) \
36 extern struct __locale_data _nl_C_##category; \
37 weak_extern (_nl_C_##category)
38 # include "categories.def"
39 # undef DEFINE_CATEGORY
41 /* Array indexed by category of pointers to _nl_C_CATEGORY slots.
42 Elements are zero for categories whose data is never used. */
43 struct __locale_data
*const _nl_C
[] attribute_hidden
=
45 # define DEFINE_CATEGORY(category, category_name, items, a) \
46 [category] = &_nl_C_##category,
47 # include "categories.def"
48 # undef DEFINE_CATEGORY
51 # define _nl_C (_nl_C_locobj.__locales)
55 /* For each category we keep a list of records for the locale files
56 which are somehow addressed. */
57 struct loaded_l10nfile
*_nl_locale_file_list
[__LC_LAST
];
59 const char _nl_default_locale_path
[] attribute_hidden
= LOCALEDIR
;
61 /* Checks if the name is actually present, that is, not NULL and not
64 name_present (const char *name
)
66 return name
!= NULL
&& name
[0] != '\0';
69 /* Checks that the locale name neither extremely long, nor contains a
70 ".." path component (to prevent directory traversal). */
72 valid_locale_name (const char *name
)
75 size_t namelen
= strlen (name
);
76 /* Name too long. The limit is arbitrary and prevents stack overflow
78 if (__glibc_unlikely (namelen
> 255))
80 /* Directory traversal attempt. */
81 static const char slashdot
[4] = {'/', '.', '.', '/'};
82 if (__glibc_unlikely (__memmem (name
, namelen
,
83 slashdot
, sizeof (slashdot
)) != NULL
))
85 if (namelen
== 2 && __glibc_unlikely (name
[0] == '.' && name
[1] == '.'))
88 && __glibc_unlikely (((name
[0] == '.'
91 || (name
[namelen
- 3] == '/'
92 && name
[namelen
- 2] == '.'
93 && name
[namelen
- 1] == '.'))))
95 /* If there is a slash in the name, it must start with one. */
96 if (__glibc_unlikely (memchr (name
, '/', namelen
) != NULL
) && name
[0] != '/')
101 struct __locale_data
*
103 _nl_find_locale (const char *locale_path
, size_t locale_path_len
,
104 int category
, const char **name
)
107 /* Name of the locale for this category. */
108 char *loc_name
= (char *) *name
;
109 const char *language
;
110 const char *modifier
;
111 const char *territory
;
113 const char *normalized_codeset
;
114 struct loaded_l10nfile
*locale_file
;
116 if (loc_name
[0] == '\0')
118 /* The user decides which locale to use by setting environment
120 loc_name
= getenv ("LC_ALL");
121 if (!name_present (loc_name
))
122 loc_name
= getenv (_nl_category_names
.str
123 + _nl_category_name_idxs
[category
]);
124 if (!name_present (loc_name
))
125 loc_name
= getenv ("LANG");
126 if (!name_present (loc_name
))
127 loc_name
= (char *) _nl_C_name
;
130 /* We used to fall back to the C locale if the name contains a slash
131 character '/', but we now check for directory traversal in
132 valid_locale_name, so this is no longer necessary. */
134 if (__builtin_expect (strcmp (loc_name
, _nl_C_name
), 1) == 0
135 || __builtin_expect (strcmp (loc_name
, _nl_POSIX_name
), 1) == 0)
137 /* We need not load anything. The needed data is contained in
138 the library itself. */
139 *name
= (char *) _nl_C_name
;
140 return _nl_C
[category
];
142 else if (!valid_locale_name (loc_name
))
144 __set_errno (EINVAL
);
150 /* We really have to load some data. First we try the archive,
151 but only if there was no LOCPATH environment variable specified. */
152 if (__glibc_likely (locale_path
== NULL
))
154 struct __locale_data
*data
155 = _nl_load_locale_from_archive (category
, name
);
156 if (__glibc_likely (data
!= NULL
))
159 /* Nothing in the archive. Set the default path to search below. */
160 locale_path
= _nl_default_locale_path
;
161 locale_path_len
= sizeof _nl_default_locale_path
;
164 /* We really have to load some data. First see whether the name is
165 an alias. Please note that this makes it impossible to have "C"
166 or "POSIX" as aliases. */
167 loc_name
= (char *) _nl_expand_alias (*name
);
168 if (loc_name
== NULL
)
169 /* It is no alias. */
170 loc_name
= (char *) *name
;
172 /* Make a writable copy of the locale name. */
173 loc_name
= strdupa (loc_name
);
175 /* LOCALE can consist of up to four recognized parts for the XPG syntax:
177 language[_territory[.codeset]][@modifier]
179 Beside the first all of them are allowed to be missing. If the
180 full specified locale is not found, the less specific one are
181 looked for. The various part will be stripped off according to
184 (2) normalized codeset
188 mask
= _nl_explode_name (loc_name
, &language
, &modifier
, &territory
,
189 &codeset
, &normalized_codeset
);
191 /* Memory allocate problem. */
194 /* If exactly this locale was already asked for we have an entry with
195 the complete name. */
196 locale_file
= _nl_make_l10nflist (&_nl_locale_file_list
[category
],
197 locale_path
, locale_path_len
, mask
,
198 language
, territory
, codeset
,
199 normalized_codeset
, modifier
,
200 _nl_category_names
.str
201 + _nl_category_name_idxs
[category
], 0);
203 if (locale_file
== NULL
)
205 /* Find status record for addressed locale file. We have to search
206 through all directories in the locale path. */
207 locale_file
= _nl_make_l10nflist (&_nl_locale_file_list
[category
],
208 locale_path
, locale_path_len
, mask
,
209 language
, territory
, codeset
,
210 normalized_codeset
, modifier
,
211 _nl_category_names
.str
212 + _nl_category_name_idxs
[category
], 1);
213 if (locale_file
== NULL
)
214 /* This means we are out of core. */
218 /* The space for normalized_codeset is dynamically allocated. Free it. */
219 if (mask
& XPG_NORM_CODESET
)
220 free ((void *) normalized_codeset
);
222 if (locale_file
->decided
== 0)
223 _nl_load_locale (locale_file
, category
);
225 if (locale_file
->data
== NULL
)
228 for (cnt
= 0; locale_file
->successor
[cnt
] != NULL
; ++cnt
)
230 if (locale_file
->successor
[cnt
]->decided
== 0)
231 _nl_load_locale (locale_file
->successor
[cnt
], category
);
232 if (locale_file
->successor
[cnt
]->data
!= NULL
)
235 /* Move the entry we found (or NULL) to the first place of
237 locale_file
->successor
[0] = locale_file
->successor
[cnt
];
238 locale_file
= locale_file
->successor
[cnt
];
240 if (locale_file
== NULL
)
244 /* The LC_CTYPE category allows to check whether a locale is really
245 usable. If the locale name contains a charset name and the
246 charset name used in the locale (present in the LC_CTYPE data) is
247 not the same (after resolving aliases etc) we reject the locale
248 since using it would irritate users expecting the charset named
249 in the locale name. */
252 /* Get the codeset information from the locale file. */
253 static const int codeset_idx
[] =
255 [__LC_CTYPE
] = _NL_ITEM_INDEX (CODESET
),
256 [__LC_NUMERIC
] = _NL_ITEM_INDEX (_NL_NUMERIC_CODESET
),
257 [__LC_TIME
] = _NL_ITEM_INDEX (_NL_TIME_CODESET
),
258 [__LC_COLLATE
] = _NL_ITEM_INDEX (_NL_COLLATE_CODESET
),
259 [__LC_MONETARY
] = _NL_ITEM_INDEX (_NL_MONETARY_CODESET
),
260 [__LC_MESSAGES
] = _NL_ITEM_INDEX (_NL_MESSAGES_CODESET
),
261 [__LC_PAPER
] = _NL_ITEM_INDEX (_NL_PAPER_CODESET
),
262 [__LC_NAME
] = _NL_ITEM_INDEX (_NL_NAME_CODESET
),
263 [__LC_ADDRESS
] = _NL_ITEM_INDEX (_NL_ADDRESS_CODESET
),
264 [__LC_TELEPHONE
] = _NL_ITEM_INDEX (_NL_TELEPHONE_CODESET
),
265 [__LC_MEASUREMENT
] = _NL_ITEM_INDEX (_NL_MEASUREMENT_CODESET
),
266 [__LC_IDENTIFICATION
] = _NL_ITEM_INDEX (_NL_IDENTIFICATION_CODESET
)
268 const struct __locale_data
*data
;
269 const char *locale_codeset
;
270 char *clocale_codeset
;
273 data
= (const struct __locale_data
*) locale_file
->data
;
275 (const char *) data
->values
[codeset_idx
[category
]].string
;
276 assert (locale_codeset
!= NULL
);
277 /* Note the length of the allocated memory: +3 for up to two slashes
279 clocale_codeset
= (char *) alloca (strlen (locale_codeset
) + 3);
280 strip (clocale_codeset
, locale_codeset
);
282 ccodeset
= (char *) alloca (strlen (codeset
) + 3);
283 strip (ccodeset
, codeset
);
285 if (__gconv_compare_alias (upstr (ccodeset
, ccodeset
),
286 upstr (clocale_codeset
,
287 clocale_codeset
)) != 0)
288 /* The codesets are not identical, don't use the locale. */
292 /* Determine the locale name for which loading succeeded. This
293 information comes from the file name. The form is
294 <path>/<locale>/LC_foo. We must extract the <locale> part. */
295 if (((const struct __locale_data
*) locale_file
->data
)->name
== NULL
)
299 endp
= strrchr (locale_file
->filename
, '/');
301 while (cp
[-1] != '/')
303 ((struct __locale_data
*) locale_file
->data
)->name
304 = __strndup (cp
, endp
- cp
);
307 /* Determine whether the user wants transliteration or not. */
309 && __strcasecmp_l (modifier
, "TRANSLIT", _nl_C_locobj_ptr
) == 0)
310 ((struct __locale_data
*) locale_file
->data
)->use_translit
= 1;
312 /* Increment the usage count. */
313 if (((const struct __locale_data
*) locale_file
->data
)->usage_count
315 ++((struct __locale_data
*) locale_file
->data
)->usage_count
;
317 return (struct __locale_data
*) locale_file
->data
;
321 /* Calling this function assumes the lock for handling global locale data
325 _nl_remove_locale (int locale
, struct __locale_data
*data
)
327 if (--data
->usage_count
== 0)
329 if (data
->alloc
!= ld_archive
)
331 /* First search the entry in the list of loaded files. */
332 struct loaded_l10nfile
*ptr
= _nl_locale_file_list
[locale
];
334 /* Search for the entry. It must be in the list. Otherwise it
335 is a bug and we crash badly. */
336 while ((struct __locale_data
*) ptr
->data
!= data
)
339 /* Mark the data as not available anymore. So when the data has
340 to be used again it is reloaded. */
345 /* This does the real work. */
346 _nl_unload_locale (data
);