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