xref: /freebsd/lib/libc/locale/collate.c (revision ee2ea5ceafed78a5bd9810beb9e3ca927180c226)
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 
44 #include "libc_private.h"
45 
46 int __collate_load_error = 1;
47 int __collate_substitute_nontrivial;
48 char __collate_version[STR_LEN];
49 u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN];
50 struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1];
51 struct __collate_st_chain_pri __collate_chain_pri_table[TABLE_SIZE];
52 
53 #define FREAD(a, b, c, d) \
54 	do { \
55 		if (fread(a, b, c, d) != c) { \
56 			fclose(d); \
57 			return -1; \
58 		} \
59 	} while(0)
60 
61 void __collate_err(int ex, const char *f) __dead2;
62 
63 int
64 __collate_load_tables(encoding)
65 	char *encoding;
66 {
67 	char buf[PATH_MAX];
68 	FILE *fp;
69 	int i, save_load_error;
70 
71 	save_load_error = __collate_load_error;
72 	__collate_load_error = 1;
73 	if (!encoding) {
74 		__collate_load_error = save_load_error;
75 		return -1;
76 	}
77 	if (!strcmp(encoding, "C") || !strcmp(encoding, "POSIX"))
78 		return 0;
79 	if (!_PathLocale) {
80 		__collate_load_error = save_load_error;
81 		return -1;
82 	}
83 	/* Range checking not needed, encoding has fixed size */
84 	(void) strcpy(buf, _PathLocale);
85 	(void) strcat(buf, "/");
86 	(void) strcat(buf, encoding);
87 	(void) strcat(buf, "/LC_COLLATE");
88 	if ((fp = fopen(buf, "r")) == NULL) {
89 		__collate_load_error = save_load_error;
90 		return -1;
91 	}
92 	FREAD(__collate_version, sizeof(__collate_version), 1, fp);
93 	if (strcmp(__collate_version, COLLATE_VERSION) != 0) {
94 		fclose(fp);
95 		return -1;
96 	}
97 	FREAD(__collate_substitute_table, sizeof(__collate_substitute_table),
98 	      1, fp);
99 	FREAD(__collate_char_pri_table, sizeof(__collate_char_pri_table), 1,
100 	      fp);
101 	FREAD(__collate_chain_pri_table, sizeof(__collate_chain_pri_table), 1,
102 	      fp);
103 	fclose(fp);
104 	__collate_load_error = 0;
105 
106 	__collate_substitute_nontrivial = 0;
107 	for (i = 0; i < UCHAR_MAX + 1; i++) {
108 		if (__collate_substitute_table[i][0] != i ||
109 		    __collate_substitute_table[i][1] != 0) {
110 			__collate_substitute_nontrivial = 1;
111 			break;
112 		}
113 	}
114 
115 	return 0;
116 }
117 
118 u_char *
119 __collate_substitute(s)
120 	const u_char *s;
121 {
122 	int dest_len, len, nlen;
123 	int delta = strlen(s);
124 	u_char *dest_str = NULL;
125 
126 	if(s == NULL || *s == '\0')
127 		return __collate_strdup("");
128 	delta += delta / 8;
129 	dest_str = malloc(dest_len = delta);
130 	if(dest_str == NULL)
131 		__collate_err(EX_OSERR, __FUNCTION__);
132 	len = 0;
133 	while(*s) {
134 		nlen = len + strlen(__collate_substitute_table[*s]);
135 		if (dest_len <= nlen) {
136 			dest_str = reallocf(dest_str, dest_len = nlen + delta);
137 			if(dest_str == NULL)
138 				__collate_err(EX_OSERR, __FUNCTION__);
139 		}
140 		strcpy(dest_str + len, __collate_substitute_table[*s++]);
141 		len = nlen;
142 	}
143 	return dest_str;
144 }
145 
146 void
147 __collate_lookup(t, len, prim, sec)
148 	const u_char *t;
149 	int *len, *prim, *sec;
150 {
151 	struct __collate_st_chain_pri *p2;
152 
153 	*len = 1;
154 	*prim = *sec = 0;
155 	for(p2 = __collate_chain_pri_table; p2->str[0]; p2++) {
156 		if(strncmp(t, p2->str, strlen(p2->str)) == 0) {
157 			*len = strlen(p2->str);
158 			*prim = p2->prim;
159 			*sec = p2->sec;
160 			return;
161 		}
162 	}
163 	*prim = __collate_char_pri_table[*t].prim;
164 	*sec = __collate_char_pri_table[*t].sec;
165 }
166 
167 u_char *
168 __collate_strdup(s)
169 	u_char *s;
170 {
171 	u_char *t = strdup(s);
172 
173 	if (t == NULL)
174 		__collate_err(EX_OSERR, __FUNCTION__);
175 	return t;
176 }
177 
178 void
179 __collate_err(int ex, const char *f)
180 {
181 	const char *s;
182 	int serrno = errno;
183 
184 	s = _getprogname();
185 	_write(STDERR_FILENO, s, strlen(s));
186 	_write(STDERR_FILENO, ": ", 2);
187 	s = f;
188 	_write(STDERR_FILENO, s, strlen(s));
189 	_write(STDERR_FILENO, ": ", 2);
190 	s = strerror(serrno);
191 	_write(STDERR_FILENO, s, strlen(s));
192 	_write(STDERR_FILENO, "\n", 1);
193 	exit(ex);
194 }
195 
196 #ifdef COLLATE_DEBUG
197 void
198 __collate_print_tables()
199 {
200 	int i;
201 	struct __collate_st_chain_pri *p2;
202 
203 	printf("Substitute table:\n");
204 	for (i = 0; i < UCHAR_MAX + 1; i++)
205 	    if (i != *__collate_substitute_table[i])
206 		printf("\t'%c' --> \"%s\"\n", i,
207 		       __collate_substitute_table[i]);
208 	printf("Chain priority table:\n");
209 	for (p2 = __collate_chain_pri_table; p2->str[0]; p2++)
210 		printf("\t\"%s\" : %d %d\n\n", p2->str, p2->prim, p2->sec);
211 	printf("Char priority table:\n");
212 	for (i = 0; i < UCHAR_MAX + 1; i++)
213 		printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim,
214 		       __collate_char_pri_table[i].sec);
215 }
216 #endif
217