xref: /freebsd/lib/libc/locale/collate.c (revision a3e8fd0b7f663db7eafff527d5c3ca3bcfa8a537)
1 /*-
2  * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
3  *		at Electronni Visti IA, Kiev, Ukraine.
4  *			All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "namespace.h"
32 #include <arpa/inet.h>
33 #include <rune.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <sysexits.h>
40 #include "un-namespace.h"
41 
42 #include "collate.h"
43 #include "setlocale.h"
44 #include "ldpart.h"
45 
46 #include "libc_private.h"
47 
48 int __collate_load_error = 1;
49 int __collate_substitute_nontrivial;
50 
51 u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN];
52 struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1];
53 struct __collate_st_chain_pri *__collate_chain_pri_table;
54 
55 void __collate_err(int ex, const char *f) __dead2;
56 
57 int
58 __collate_load_tables(const char *encoding)
59 {
60 	FILE *fp;
61 	int i, saverr, chains;
62 	uint32_t u32;
63 	char strbuf[STR_LEN], buf[PATH_MAX];
64 	void *TMP_substitute_table, *TMP_char_pri_table, *TMP_chain_pri_table;
65 	static char collate_encoding[ENCODING_LEN + 1];
66 
67 	/* 'encoding' must be already checked. */
68 	if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
69 		__collate_load_error = 1;
70 		return (_LDP_CACHE);
71 	}
72 
73 	/*
74 	 * If the locale name is the same as our cache, use the cache.
75 	 */
76 	if (strcmp(encoding, collate_encoding) == 0) {
77 		__collate_load_error = 0;
78 		return (_LDP_CACHE);
79 	}
80 
81 	/*
82 	 * Slurp the locale file into the cache.
83 	 */
84 
85 	/* 'PathLocale' must be already set & checked. */
86 	/* Range checking not needed, encoding has fixed size */
87 	(void)strcpy(buf, _PathLocale);
88 	(void)strcat(buf, "/");
89 	(void)strcat(buf, encoding);
90 	(void)strcat(buf, "/LC_COLLATE");
91 	if ((fp = fopen(buf, "r")) == NULL)
92 		return (_LDP_ERROR);
93 
94 	if (fread(strbuf, sizeof(strbuf), 1, fp) != 1) {
95 		saverr = errno;
96 		(void)fclose(fp);
97 		errno = saverr;
98 		return (_LDP_ERROR);
99 	}
100 	chains = -1;
101 	if (strcmp(strbuf, COLLATE_VERSION) == 0)
102 		chains = 0;
103 	else if (strcmp(strbuf, COLLATE_VERSION1_1) == 0)
104 		chains = 1;
105 	if (chains < 0) {
106 		(void)fclose(fp);
107 		errno = EFTYPE;
108 		return (_LDP_ERROR);
109 	}
110 	if (chains) {
111 		if (fread(&u32, sizeof(u32), 1, fp) != 1) {
112 			saverr = errno;
113 			(void)fclose(fp);
114 			errno = saverr;
115 			return (_LDP_ERROR);
116 		}
117 		if ((chains = (int)ntohl(u32)) < 1) {
118 			(void)fclose(fp);
119 			errno = EFTYPE;
120 			return (_LDP_ERROR);
121 		}
122 	} else
123 		chains = TABLE_SIZE;
124 
125 	if ((TMP_substitute_table =
126 	     malloc(sizeof(__collate_substitute_table))) == NULL) {
127 		saverr = errno;
128 		(void)fclose(fp);
129 		errno = saverr;
130 		return (_LDP_ERROR);
131 	}
132 	if ((TMP_char_pri_table =
133 	     malloc(sizeof(__collate_char_pri_table))) == NULL) {
134 		saverr = errno;
135 		free(TMP_substitute_table);
136 		(void)fclose(fp);
137 		errno = saverr;
138 		return (_LDP_ERROR);
139 	}
140 	if ((TMP_chain_pri_table =
141 	     malloc(sizeof(*__collate_chain_pri_table) * chains)) == NULL) {
142 		saverr = errno;
143 		free(TMP_substitute_table);
144 		free(TMP_char_pri_table);
145 		(void)fclose(fp);
146 		errno = saverr;
147 		return (_LDP_ERROR);
148 	}
149 
150 #define FREAD(a, b, c, d) \
151 { \
152 	if (fread(a, b, c, d) != c) { \
153 		saverr = errno; \
154 		free(TMP_substitute_table); \
155 		free(TMP_char_pri_table); \
156 		free(TMP_chain_pri_table); \
157 		(void)fclose(d); \
158 		errno = saverr; \
159 		return (_LDP_ERROR); \
160 	} \
161 }
162 
163 	FREAD(TMP_substitute_table, sizeof(__collate_substitute_table), 1, fp);
164 	FREAD(TMP_char_pri_table, sizeof(__collate_char_pri_table), 1, fp);
165 	FREAD(TMP_chain_pri_table,
166 	      sizeof(*__collate_chain_pri_table), chains, fp);
167 	(void)fclose(fp);
168 
169 	(void)strcpy(collate_encoding, encoding);
170 	if (__collate_substitute_table_ptr != NULL)
171 		free(__collate_substitute_table_ptr);
172 	__collate_substitute_table_ptr = TMP_substitute_table;
173 	if (__collate_char_pri_table_ptr != NULL)
174 		free(__collate_char_pri_table_ptr);
175 	__collate_char_pri_table_ptr = TMP_char_pri_table;
176 	if (__collate_chain_pri_table != NULL)
177 		free(__collate_chain_pri_table);
178 	__collate_chain_pri_table = TMP_chain_pri_table;
179 
180 	__collate_substitute_nontrivial = 0;
181 	for (i = 0; i < UCHAR_MAX + 1; i++) {
182 		if (__collate_substitute_table[i][0] != i ||
183 		    __collate_substitute_table[i][1] != 0) {
184 			__collate_substitute_nontrivial = 1;
185 			break;
186 		}
187 	}
188 	__collate_load_error = 0;
189 
190 	return (_LDP_LOADED);
191 }
192 
193 u_char *
194 __collate_substitute(s)
195 	const u_char *s;
196 {
197 	int dest_len, len, nlen;
198 	int delta = strlen(s);
199 	u_char *dest_str = NULL;
200 
201 	if (s == NULL || *s == '\0')
202 		return (__collate_strdup(""));
203 	delta += delta / 8;
204 	dest_str = malloc(dest_len = delta);
205 	if (dest_str == NULL)
206 		__collate_err(EX_OSERR, __FUNCTION__);
207 	len = 0;
208 	while (*s) {
209 		nlen = len + strlen(__collate_substitute_table[*s]);
210 		if (dest_len <= nlen) {
211 			dest_str = reallocf(dest_str, dest_len = nlen + delta);
212 			if (dest_str == NULL)
213 				__collate_err(EX_OSERR, __FUNCTION__);
214 		}
215 		(void)strcpy(dest_str + len, __collate_substitute_table[*s++]);
216 		len = nlen;
217 	}
218 	return (dest_str);
219 }
220 
221 void
222 __collate_lookup(t, len, prim, sec)
223 	const u_char *t;
224 	int *len, *prim, *sec;
225 {
226 	struct __collate_st_chain_pri *p2;
227 
228 	*len = 1;
229 	*prim = *sec = 0;
230 	for (p2 = __collate_chain_pri_table; p2->str[0] != '\0'; p2++) {
231 		if (*t == p2->str[0] &&
232 		    strncmp(t, p2->str, strlen(p2->str)) == 0) {
233 			*len = strlen(p2->str);
234 			*prim = p2->prim;
235 			*sec = p2->sec;
236 			return;
237 		}
238 	}
239 	*prim = __collate_char_pri_table[*t].prim;
240 	*sec = __collate_char_pri_table[*t].sec;
241 }
242 
243 u_char *
244 __collate_strdup(s)
245 	u_char *s;
246 {
247 	u_char *t = strdup(s);
248 
249 	if (t == NULL)
250 		__collate_err(EX_OSERR, __FUNCTION__);
251 	return (t);
252 }
253 
254 void
255 __collate_err(int ex, const char *f)
256 {
257 	const char *s;
258 	int serrno = errno;
259 
260 	s = _getprogname();
261 	_write(STDERR_FILENO, s, strlen(s));
262 	_write(STDERR_FILENO, ": ", 2);
263 	s = f;
264 	_write(STDERR_FILENO, s, strlen(s));
265 	_write(STDERR_FILENO, ": ", 2);
266 	s = strerror(serrno);
267 	_write(STDERR_FILENO, s, strlen(s));
268 	_write(STDERR_FILENO, "\n", 1);
269 	exit(ex);
270 }
271 
272 #ifdef COLLATE_DEBUG
273 void
274 __collate_print_tables()
275 {
276 	int i;
277 	struct __collate_st_chain_pri *p2;
278 
279 	printf("Substitute table:\n");
280 	for (i = 0; i < UCHAR_MAX + 1; i++)
281 	    if (i != *__collate_substitute_table[i])
282 		printf("\t'%c' --> \"%s\"\n", i,
283 		       __collate_substitute_table[i]);
284 	printf("Chain priority table:\n");
285 	for (p2 = __collate_chain_pri_table; p2->str[0] != '\0'; p2++)
286 		printf("\t\"%s\" : %d %d\n", p2->str, p2->prim, p2->sec);
287 	printf("Char priority table:\n");
288 	for (i = 0; i < UCHAR_MAX + 1; i++)
289 		printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim,
290 		       __collate_char_pri_table[i].sec);
291 }
292 #endif
293