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