2 * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
3 * at Electronni Visti IA, Kiev, Ukraine.
6 * Copyright (c) 2011 The FreeBSD Foundation
8 * Portions of this software were developed by David Chisnall
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "namespace.h"
37 #include <arpa/inet.h>
44 #include "un-namespace.h"
47 #include "setlocale.h"
50 #include "libc_private.h"
53 * To avoid modifying the original (single-threaded) code too much, we'll just
54 * define the old globals as fields inside the table.
56 * We also modify the collation table test functions to search the thread-local
57 * table first and the global table second.
59 #define __collate_substitute_nontrivial (table->__collate_substitute_nontrivial)
60 #define __collate_substitute_table_ptr (table->__collate_substitute_table_ptr)
61 #define __collate_char_pri_table_ptr (table->__collate_char_pri_table_ptr)
62 #define __collate_chain_pri_table (table->__collate_chain_pri_table)
63 int __collate_load_error;
66 struct xlocale_collate __xlocale_global_collate = {
70 struct xlocale_collate __xlocale_C_collate = {
74 void __collate_err(int ex, const char *f) __dead2;
77 __collate_load_tables_l(const char *encoding, struct xlocale_collate *table);
80 destruct_collate(void *t)
82 struct xlocale_collate *table = t;
83 if (__collate_chain_pri_table) {
84 free(__collate_chain_pri_table);
90 __collate_load(const char *encoding, locale_t unused)
92 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
93 return &__xlocale_C_collate;
95 struct xlocale_collate *table = calloc(sizeof(struct xlocale_collate), 1);
96 table->header.header.destructor = destruct_collate;
97 // FIXME: Make sure that _LDP_CACHE is never returned. We should be doing
98 // the caching outside of this section
99 if (__collate_load_tables_l(encoding, table) != _LDP_LOADED) {
100 xlocale_release(table);
107 * Load the collation tables for the specified encoding into the global table.
110 __collate_load_tables(const char *encoding)
112 int ret = __collate_load_tables_l(encoding, &__xlocale_global_collate);
113 __collate_load_error = __xlocale_global_collate.__collate_load_error;
118 __collate_load_tables_l(const char *encoding, struct xlocale_collate *table)
121 int i, saverr, chains;
123 char strbuf[STR_LEN], buf[PATH_MAX];
124 void *TMP_substitute_table, *TMP_char_pri_table, *TMP_chain_pri_table;
126 /* 'encoding' must be already checked. */
127 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
128 table->__collate_load_error = 1;
132 /* 'PathLocale' must be already set & checked. */
133 /* Range checking not needed, encoding has fixed size */
134 (void)strcpy(buf, _PathLocale);
135 (void)strcat(buf, "/");
136 (void)strcat(buf, encoding);
137 (void)strcat(buf, "/LC_COLLATE");
138 if ((fp = fopen(buf, "re")) == NULL)
141 if (fread(strbuf, sizeof(strbuf), 1, fp) != 1) {
148 if (strcmp(strbuf, COLLATE_VERSION) == 0)
150 else if (strcmp(strbuf, COLLATE_VERSION1_2) == 0)
158 if (fread(&u32, sizeof(u32), 1, fp) != 1) {
164 if ((chains = (int)ntohl(u32)) < 1) {
172 if ((TMP_substitute_table =
173 malloc(sizeof(__collate_substitute_table))) == NULL) {
179 if ((TMP_char_pri_table =
180 malloc(sizeof(__collate_char_pri_table))) == NULL) {
182 free(TMP_substitute_table);
187 if ((TMP_chain_pri_table =
188 malloc(sizeof(*__collate_chain_pri_table) * chains)) == NULL) {
190 free(TMP_substitute_table);
191 free(TMP_char_pri_table);
197 #define FREAD(a, b, c, d) \
199 if (fread(a, b, c, d) != c) { \
201 free(TMP_substitute_table); \
202 free(TMP_char_pri_table); \
203 free(TMP_chain_pri_table); \
206 return (_LDP_ERROR); \
210 FREAD(TMP_substitute_table, sizeof(__collate_substitute_table), 1, fp);
211 FREAD(TMP_char_pri_table, sizeof(__collate_char_pri_table), 1, fp);
212 FREAD(TMP_chain_pri_table,
213 sizeof(*__collate_chain_pri_table), chains, fp);
216 if (__collate_substitute_table_ptr != NULL)
217 free(__collate_substitute_table_ptr);
218 __collate_substitute_table_ptr = TMP_substitute_table;
219 if (__collate_char_pri_table_ptr != NULL)
220 free(__collate_char_pri_table_ptr);
221 __collate_char_pri_table_ptr = TMP_char_pri_table;
222 for (i = 0; i < UCHAR_MAX + 1; i++) {
223 __collate_char_pri_table[i].prim =
224 ntohl(__collate_char_pri_table[i].prim);
225 __collate_char_pri_table[i].sec =
226 ntohl(__collate_char_pri_table[i].sec);
228 if (__collate_chain_pri_table != NULL)
229 free(__collate_chain_pri_table);
230 __collate_chain_pri_table = TMP_chain_pri_table;
231 for (i = 0; i < chains; i++) {
232 __collate_chain_pri_table[i].prim =
233 ntohl(__collate_chain_pri_table[i].prim);
234 __collate_chain_pri_table[i].sec =
235 ntohl(__collate_chain_pri_table[i].sec);
237 __collate_substitute_nontrivial = 0;
238 for (i = 0; i < UCHAR_MAX + 1; i++) {
239 if (__collate_substitute_table[i][0] != i ||
240 __collate_substitute_table[i][1] != 0) {
241 __collate_substitute_nontrivial = 1;
245 table->__collate_load_error = 0;
247 return (_LDP_LOADED);
251 __collate_substitute(struct xlocale_collate *table, const u_char *s)
253 int dest_len, len, nlen;
254 int delta = strlen(s);
255 u_char *dest_str = NULL;
257 if (s == NULL || *s == '\0')
258 return (__collate_strdup(""));
260 dest_str = malloc(dest_len = delta);
261 if (dest_str == NULL)
262 __collate_err(EX_OSERR, __func__);
265 nlen = len + strlen(__collate_substitute_table[*s]);
266 if (dest_len <= nlen) {
267 dest_str = reallocf(dest_str, dest_len = nlen + delta);
268 if (dest_str == NULL)
269 __collate_err(EX_OSERR, __func__);
271 (void)strcpy(dest_str + len, __collate_substitute_table[*s++]);
278 __collate_lookup(struct xlocale_collate *table, const u_char *t, int *len, int *prim, int *sec)
280 struct __collate_st_chain_pri *p2;
284 for (p2 = __collate_chain_pri_table; p2->str[0] != '\0'; p2++) {
285 if (*t == p2->str[0] &&
286 strncmp(t, p2->str, strlen(p2->str)) == 0) {
287 *len = strlen(p2->str);
293 *prim = __collate_char_pri_table[*t].prim;
294 *sec = __collate_char_pri_table[*t].sec;
298 __collate_strdup(u_char *s)
300 u_char *t = strdup(s);
303 __collate_err(EX_OSERR, __func__);
308 __collate_err(int ex, const char *f)
314 _write(STDERR_FILENO, s, strlen(s));
315 _write(STDERR_FILENO, ": ", 2);
317 _write(STDERR_FILENO, s, strlen(s));
318 _write(STDERR_FILENO, ": ", 2);
319 s = strerror(serrno);
320 _write(STDERR_FILENO, s, strlen(s));
321 _write(STDERR_FILENO, "\n", 1);
327 __collate_print_tables()
330 struct __collate_st_chain_pri *p2;
332 printf("Substitute table:\n");
333 for (i = 0; i < UCHAR_MAX + 1; i++)
334 if (i != *__collate_substitute_table[i])
335 printf("\t'%c' --> \"%s\"\n", i,
336 __collate_substitute_table[i]);
337 printf("Chain priority table:\n");
338 for (p2 = __collate_chain_pri_table; p2->str[0] != '\0'; p2++)
339 printf("\t\"%s\" : %d %d\n", p2->str, p2->prim, p2->sec);
340 printf("Char priority table:\n");
341 for (i = 0; i < UCHAR_MAX + 1; i++)
342 printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim,
343 __collate_char_pri_table[i].sec);