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