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