1 /* 2 * Copyright (c) 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Paul Borman at Krystal Technologies. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright 2010 Nexenta Systems, Inc. All rights reserved. 35 * Use is subject to license terms. 36 */ 37 38 #include "lint.h" 39 #include "file64.h" 40 #include <errno.h> 41 #include <limits.h> 42 #include <string.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <unistd.h> 46 #include <wchar.h> 47 #include "runetype.h" 48 #include "ldpart.h" 49 #include "mblocal.h" 50 #include "setlocale.h" 51 #include "_ctype.h" 52 53 extern _RuneLocale *_Read_RuneMagi(FILE *); 54 55 static int __setrunelocale(const char *); 56 57 static int 58 __setrunelocale(const char *encoding) 59 { 60 FILE *fp; 61 char name[PATH_MAX]; 62 _RuneLocale *rl; 63 int saverr, ret; 64 size_t (*old__mbrtowc)(wchar_t *_RESTRICT_KYWD, 65 const char *_RESTRICT_KYWD, size_t, mbstate_t *_RESTRICT_KYWD); 66 size_t (*old__wcrtomb)(char *_RESTRICT_KYWD, wchar_t, 67 mbstate_t *_RESTRICT_KYWD); 68 int (*old__mbsinit)(const mbstate_t *); 69 size_t (*old__mbsnrtowcs)(wchar_t *_RESTRICT_KYWD, 70 const char **_RESTRICT_KYWD, size_t, size_t, 71 mbstate_t *_RESTRICT_KYWD); 72 size_t (*old__wcsnrtombs)(char *_RESTRICT_KYWD, 73 const wchar_t **_RESTRICT_KYWD, size_t, size_t, 74 mbstate_t *_RESTRICT_KYWD); 75 static char ctype_encoding[ENCODING_LEN + 1]; 76 static _RuneLocale *CachedRuneLocale; 77 static size_t (*Cached__mbrtowc)(wchar_t *_RESTRICT_KYWD, 78 const char *_RESTRICT_KYWD, size_t, mbstate_t *_RESTRICT_KYWD); 79 static size_t (*Cached__wcrtomb)(char *_RESTRICT_KYWD, wchar_t, 80 mbstate_t *_RESTRICT_KYWD); 81 static int (*Cached__mbsinit)(const mbstate_t *); 82 static size_t (*Cached__mbsnrtowcs)(wchar_t *_RESTRICT_KYWD, 83 const char **_RESTRICT_KYWD, size_t, size_t, 84 mbstate_t *_RESTRICT_KYWD); 85 static size_t (*Cached__wcsnrtombs)(char *_RESTRICT_KYWD, 86 const wchar_t **_RESTRICT_KYWD, size_t, size_t, 87 mbstate_t *_RESTRICT_KYWD); 88 89 /* 90 * The "C" and "POSIX" locale are always here. 91 */ 92 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) { 93 (void) _none_init(&_DefaultRuneLocale); 94 return (0); 95 } 96 97 /* 98 * If the locale name is the same as our cache, use the cache. 99 */ 100 if (CachedRuneLocale != NULL && 101 strcmp(encoding, ctype_encoding) == 0) { 102 _CurrentRuneLocale = CachedRuneLocale; 103 __mbrtowc = Cached__mbrtowc; 104 __mbsinit = Cached__mbsinit; 105 __mbsnrtowcs = Cached__mbsnrtowcs; 106 __wcrtomb = Cached__wcrtomb; 107 __wcsnrtombs = Cached__wcsnrtombs; 108 return (0); 109 } 110 111 /* 112 * Slurp the locale file into the cache. 113 */ 114 115 /* Range checking not needed, encoding length already checked before */ 116 (void) strcpy(name, _PathLocale); 117 (void) strcat(name, "/"); 118 (void) strcat(name, encoding); 119 (void) strcat(name, "/LC_CTYPE"); 120 121 if ((fp = fopen(name, "r")) == NULL) 122 return (errno == 0 ? ENOENT : errno); 123 124 if ((rl = _Read_RuneMagi(fp)) == NULL) { 125 saverr = (errno == 0 ? EINVAL : errno); 126 (void) fclose(fp); 127 return (saverr); 128 } 129 (void) fclose(fp); 130 131 old__mbrtowc = __mbrtowc; 132 old__mbsinit = __mbsinit; 133 old__mbsnrtowcs = __mbsnrtowcs; 134 old__wcrtomb = __wcrtomb; 135 old__wcsnrtombs = __wcsnrtombs; 136 137 __mbrtowc = NULL; 138 __mbsinit = NULL; 139 __mbsnrtowcs = __mbsnrtowcs_std; 140 __wcrtomb = NULL; 141 __wcsnrtombs = __wcsnrtombs_std; 142 143 if (strcmp(rl->__encoding, "NONE") == 0) 144 ret = _none_init(rl); 145 else if (strcmp(rl->__encoding, "ASCII") == 0) 146 ret = _ascii_init(rl); 147 else if (strcmp(rl->__encoding, "UTF-8") == 0) 148 ret = _UTF8_init(rl); 149 else if (strcmp(rl->__encoding, "EUC") == 0) 150 ret = _EUC_init(rl); 151 else if (strcmp(rl->__encoding, "GB18030") == 0) 152 ret = _GB18030_init(rl); 153 else if (strcmp(rl->__encoding, "GB2312") == 0) 154 ret = _GB2312_init(rl); 155 else if (strcmp(rl->__encoding, "GBK") == 0) 156 ret = _GBK_init(rl); 157 else if (strcmp(rl->__encoding, "BIG5") == 0) 158 ret = _BIG5_init(rl); 159 else if (strcmp(rl->__encoding, "MSKanji") == 0) 160 ret = _MSKanji_init(rl); 161 else 162 ret = EINVAL; 163 164 if (ret == 0) { 165 if (CachedRuneLocale != NULL) { 166 /* See euc.c */ 167 if (strcmp(CachedRuneLocale->__encoding, "EUC") == 0) 168 free(CachedRuneLocale->__variable); 169 free(CachedRuneLocale); 170 } 171 CachedRuneLocale = _CurrentRuneLocale; 172 Cached__mbrtowc = __mbrtowc; 173 Cached__mbsinit = __mbsinit; 174 Cached__mbsnrtowcs = __mbsnrtowcs; 175 Cached__wcrtomb = __wcrtomb; 176 Cached__wcsnrtombs = __wcsnrtombs; 177 (void) strcpy(ctype_encoding, encoding); 178 179 /* 180 * We need to overwrite the _ctype array. This requires 181 * some finagling. This is because references to it may 182 * have been baked into applications. 183 * 184 * Note that it is interesting that toupper/tolower only 185 * produce defined results when the input is representable 186 * as a byte. 187 */ 188 189 /* 190 * The top half is the type mask array. Because we 191 * want to support both legacy Solaris code (which have 192 * mask valeus baked in to them), and we want to be able 193 * to import locale files from other sources (FreeBSD) 194 * which probably uses different masks, we have to perform 195 * a conversion here. Ugh. Note that the _CTYPE definitions 196 * we use from FreeBSD are richer than the Solaris legacy. 197 * 198 * We have to cope with these limitations though, because the 199 * inadequate Solaris definitions were baked into binaries. 200 */ 201 for (int i = 0; i < _CACHED_RUNES; i++) { 202 /* ctype can only encode the lower 8 bits. */ 203 __ctype[i+1] = rl->__runetype[i] & 0xff; 204 __ctype_mask[i] = rl->__runetype[i]; 205 } 206 207 /* The bottom half is the toupper/lower array */ 208 for (int i = 0; i < _CACHED_RUNES; i++) { 209 __ctype[258 + i] = i; 210 if (rl->__mapupper[i] && rl->__mapupper[i] != i) 211 __ctype[258+i] = rl->__mapupper[i]; 212 if (rl->__maplower[i] && rl->__maplower[i] != i) 213 __ctype[258+i] = rl->__maplower[i]; 214 215 /* Don't forget these annoyances either! */ 216 __trans_upper[i] = rl->__mapupper[i]; 217 __trans_lower[i] = rl->__maplower[i]; 218 } 219 220 /* 221 * Note that we expect the init code will have populated 222 * the CSWIDTH array (__ctype[514-520]) properly. 223 */ 224 } else { 225 __mbrtowc = old__mbrtowc; 226 __mbsinit = old__mbsinit; 227 __mbsnrtowcs = old__mbsnrtowcs; 228 __wcrtomb = old__wcrtomb; 229 __wcsnrtombs = old__wcsnrtombs; 230 free(rl); 231 } 232 233 return (ret); 234 } 235 236 int 237 __wrap_setrunelocale(const char *locale) 238 { 239 int ret = __setrunelocale(locale); 240 241 if (ret != 0) { 242 errno = ret; 243 return (_LDP_ERROR); 244 } 245 return (_LDP_LOADED); 246 } 247