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