xref: /freebsd/lib/libc/locale/collate.c (revision c4f6a2a9e1b1879b618c436ab4f56ff75c73a0f5)
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 <rune.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #include <sysexits.h>
39 #include "un-namespace.h"
40 
41 #include "collate.h"
42 #include "setlocale.h"
43 #include "ldpart.h"
44 
45 #include "libc_private.h"
46 
47 int __collate_load_error = 1;
48 int __collate_substitute_nontrivial;
49 
50 u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN];
51 struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1];
52 struct __collate_st_chain_pri __collate_chain_pri_table[TABLE_SIZE];
53 
54 void __collate_err(int ex, const char *f) __dead2;
55 
56 int
57 __collate_load_tables(const char *encoding)
58 {
59 	FILE *fp;
60 	int i, saverr;
61 	char collate_version[STR_LEN];
62 	char buf[PATH_MAX];
63 	void *TMP_substitute_table, *TMP_char_pri_table, *TMP_chain_pri_table;
64 	static char collate_encoding[ENCODING_LEN + 1];
65 
66 	/* 'encoding' must be already checked. */
67 	if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
68 		__collate_load_error = 1;
69 		return (_LDP_CACHE);
70 	}
71 
72 	/*
73 	 * If the locale name is the same as our cache, use the cache.
74 	 */
75 	if (strcmp(encoding, collate_encoding) == 0) {
76 		__collate_load_error = 0;
77 		return (_LDP_CACHE);
78 	}
79 
80 	/*
81 	 * Slurp the locale file into the cache.
82 	 */
83 
84 	/* 'PathLocale' must be already set & checked. */
85 	/* Range checking not needed, encoding has fixed size */
86 	(void)strcpy(buf, _PathLocale);
87 	(void)strcat(buf, "/");
88 	(void)strcat(buf, encoding);
89 	(void)strcat(buf, "/LC_COLLATE");
90 	if ((fp = fopen(buf, "r")) == NULL)
91 		return (_LDP_ERROR);
92 
93 	if ((TMP_substitute_table =
94 	     malloc(sizeof(__collate_substitute_table))) == NULL) {
95 		saverr = errno;
96 		(void)fclose(fp);
97 		errno = saverr;
98 		return (_LDP_ERROR);
99 	}
100 	if ((TMP_char_pri_table =
101 	     malloc(sizeof(__collate_char_pri_table))) == NULL) {
102 		saverr = errno;
103 		free(TMP_substitute_table);
104 		(void)fclose(fp);
105 		errno = saverr;
106 		return (_LDP_ERROR);
107 	}
108 	if ((TMP_chain_pri_table =
109 	     malloc(sizeof(__collate_chain_pri_table))) == NULL) {
110 		saverr = errno;
111 		free(TMP_substitute_table);
112 		free(TMP_char_pri_table);
113 		(void)fclose(fp);
114 		errno = saverr;
115 		return (_LDP_ERROR);
116 	}
117 
118 #define FREAD(a, b, c, d) \
119 { \
120 	if (fread(a, b, c, d) != c) { \
121 		saverr = errno; \
122 		free(TMP_substitute_table); \
123 		free(TMP_char_pri_table); \
124 		free(TMP_chain_pri_table); \
125 		(void)fclose(d); \
126 		errno = saverr; \
127 		return (_LDP_ERROR); \
128 	} \
129 }
130 
131 	FREAD(collate_version, sizeof(collate_version), 1, fp);
132 	if (strcmp(collate_version, COLLATE_VERSION) != 0) {
133 		free(TMP_substitute_table);
134 		free(TMP_char_pri_table);
135 		free(TMP_chain_pri_table);
136 		(void)fclose(fp);
137 		errno = EFTYPE;
138 		return (_LDP_ERROR);
139 	}
140 	FREAD(TMP_substitute_table, sizeof(__collate_substitute_table), 1, fp);
141 	FREAD(TMP_char_pri_table, sizeof(__collate_char_pri_table), 1, fp);
142 	FREAD(TMP_chain_pri_table, sizeof(__collate_chain_pri_table), 1, fp);
143 	(void)fclose(fp);
144 
145 	(void)strcpy(collate_encoding, encoding);
146 	if (__collate_substitute_table_ptr != NULL)
147 		free(__collate_substitute_table_ptr);
148 	__collate_substitute_table_ptr = TMP_substitute_table;
149 	if (__collate_char_pri_table_ptr != NULL)
150 		free(__collate_char_pri_table_ptr);
151 	__collate_char_pri_table_ptr = TMP_char_pri_table;
152 	if (__collate_chain_pri_table_ptr != NULL)
153 		free(__collate_chain_pri_table_ptr);
154 	__collate_chain_pri_table_ptr = TMP_chain_pri_table;
155 
156 	__collate_substitute_nontrivial = 0;
157 	for (i = 0; i < UCHAR_MAX + 1; i++) {
158 		if (__collate_substitute_table[i][0] != i ||
159 		    __collate_substitute_table[i][1] != 0) {
160 			__collate_substitute_nontrivial = 1;
161 			break;
162 		}
163 	}
164 	__collate_load_error = 0;
165 
166 	return (_LDP_LOADED);
167 }
168 
169 u_char *
170 __collate_substitute(s)
171 	const u_char *s;
172 {
173 	int dest_len, len, nlen;
174 	int delta = strlen(s);
175 	u_char *dest_str = NULL;
176 
177 	if (s == NULL || *s == '\0')
178 		return (__collate_strdup(""));
179 	delta += delta / 8;
180 	dest_str = malloc(dest_len = delta);
181 	if (dest_str == NULL)
182 		__collate_err(EX_OSERR, __FUNCTION__);
183 	len = 0;
184 	while (*s) {
185 		nlen = len + strlen(__collate_substitute_table[*s]);
186 		if (dest_len <= nlen) {
187 			dest_str = reallocf(dest_str, dest_len = nlen + delta);
188 			if (dest_str == NULL)
189 				__collate_err(EX_OSERR, __FUNCTION__);
190 		}
191 		(void)strcpy(dest_str + len, __collate_substitute_table[*s++]);
192 		len = nlen;
193 	}
194 	return (dest_str);
195 }
196 
197 void
198 __collate_lookup(t, len, prim, sec)
199 	const u_char *t;
200 	int *len, *prim, *sec;
201 {
202 	struct __collate_st_chain_pri *p2;
203 
204 	*len = 1;
205 	*prim = *sec = 0;
206 	for (p2 = __collate_chain_pri_table; p2->str[0]; p2++) {
207 		if (strncmp(t, p2->str, strlen(p2->str)) == 0) {
208 			*len = strlen(p2->str);
209 			*prim = p2->prim;
210 			*sec = p2->sec;
211 			return;
212 		}
213 	}
214 	*prim = __collate_char_pri_table[*t].prim;
215 	*sec = __collate_char_pri_table[*t].sec;
216 }
217 
218 u_char *
219 __collate_strdup(s)
220 	u_char *s;
221 {
222 	u_char *t = strdup(s);
223 
224 	if (t == NULL)
225 		__collate_err(EX_OSERR, __FUNCTION__);
226 	return (t);
227 }
228 
229 void
230 __collate_err(int ex, const char *f)
231 {
232 	const char *s;
233 	int serrno = errno;
234 
235 	s = _getprogname();
236 	_write(STDERR_FILENO, s, strlen(s));
237 	_write(STDERR_FILENO, ": ", 2);
238 	s = f;
239 	_write(STDERR_FILENO, s, strlen(s));
240 	_write(STDERR_FILENO, ": ", 2);
241 	s = strerror(serrno);
242 	_write(STDERR_FILENO, s, strlen(s));
243 	_write(STDERR_FILENO, "\n", 1);
244 	exit(ex);
245 }
246 
247 #ifdef COLLATE_DEBUG
248 void
249 __collate_print_tables()
250 {
251 	int i;
252 	struct __collate_st_chain_pri *p2;
253 
254 	printf("Substitute table:\n");
255 	for (i = 0; i < UCHAR_MAX + 1; i++)
256 	    if (i != *__collate_substitute_table[i])
257 		printf("\t'%c' --> \"%s\"\n", i,
258 		       __collate_substitute_table[i]);
259 	printf("Chain priority table:\n");
260 	for (p2 = __collate_chain_pri_table; p2->str[0]; p2++)
261 		printf("\t\"%s\" : %d %d\n\n", p2->str, p2->prim, p2->sec);
262 	printf("Char priority table:\n");
263 	for (i = 0; i < UCHAR_MAX + 1; i++)
264 		printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim,
265 		       __collate_char_pri_table[i].sec);
266 }
267 #endif
268