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 */ 36 37 #include "lint.h" 38 #include "file64.h" 39 #include <errno.h> 40 #include <limits.h> 41 #include <string.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <unistd.h> 45 #include <wchar.h> 46 #include "runetype.h" 47 #include "ldpart.h" 48 #include "mblocal.h" 49 #include "setlocale.h" 50 #include "_ctype.h" 51 52 extern _RuneLocale *_Read_RuneMagi(FILE *); 53 54 static int __setrunelocale(const char *); 55 56 static int 57 __setrunelocale(const char *encoding) 58 { 59 FILE *fp; 60 char name[PATH_MAX]; 61 _RuneLocale *rl; 62 int saverr, ret; 63 size_t (*old__mbrtowc)(wchar_t *_RESTRICT_KYWD, 64 const char *_RESTRICT_KYWD, size_t, mbstate_t *_RESTRICT_KYWD); 65 size_t (*old__wcrtomb)(char *_RESTRICT_KYWD, wchar_t, 66 mbstate_t *_RESTRICT_KYWD); 67 int (*old__mbsinit)(const mbstate_t *); 68 size_t (*old__mbsnrtowcs)(wchar_t *_RESTRICT_KYWD, 69 const char **_RESTRICT_KYWD, size_t, size_t, 70 mbstate_t *_RESTRICT_KYWD); 71 size_t (*old__wcsnrtombs)(char *_RESTRICT_KYWD, 72 const wchar_t **_RESTRICT_KYWD, size_t, size_t, 73 mbstate_t *_RESTRICT_KYWD); 74 static char ctype_encoding[ENCODING_LEN + 1]; 75 static _RuneLocale *CachedRuneLocale; 76 static size_t (*Cached__mbrtowc)(wchar_t *_RESTRICT_KYWD, 77 const char *_RESTRICT_KYWD, size_t, mbstate_t *_RESTRICT_KYWD); 78 static size_t (*Cached__wcrtomb)(char *_RESTRICT_KYWD, wchar_t, 79 mbstate_t *_RESTRICT_KYWD); 80 static int (*Cached__mbsinit)(const mbstate_t *); 81 static size_t (*Cached__mbsnrtowcs)(wchar_t *_RESTRICT_KYWD, 82 const char **_RESTRICT_KYWD, size_t, size_t, 83 mbstate_t *_RESTRICT_KYWD); 84 static size_t (*Cached__wcsnrtombs)(char *_RESTRICT_KYWD, 85 const wchar_t **_RESTRICT_KYWD, size_t, size_t, 86 mbstate_t *_RESTRICT_KYWD); 87 88 /* 89 * The "C" and "POSIX" locale are always here. 90 */ 91 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) { 92 (void) _none_init(&_DefaultRuneLocale); 93 return (0); 94 } 95 96 /* 97 * If the locale name is the same as our cache, use the cache. 98 */ 99 if (CachedRuneLocale != NULL && 100 strcmp(encoding, ctype_encoding) == 0) { 101 _CurrentRuneLocale = CachedRuneLocale; 102 __mbrtowc = Cached__mbrtowc; 103 __mbsinit = Cached__mbsinit; 104 __mbsnrtowcs = Cached__mbsnrtowcs; 105 __wcrtomb = Cached__wcrtomb; 106 __wcsnrtombs = Cached__wcsnrtombs; 107 return (0); 108 } 109 110 /* 111 * Slurp the locale file into the cache. 112 */ 113 114 (void) snprintf(name, sizeof (name), "%s/%s/LC_CTYPE/LCL_DATA", 115 _PathLocale, encoding); 116 117 if ((fp = fopen(name, "r")) == NULL) 118 return (errno == 0 ? ENOENT : errno); 119 120 if ((rl = _Read_RuneMagi(fp)) == NULL) { 121 saverr = (errno == 0 ? EINVAL : errno); 122 (void) fclose(fp); 123 return (saverr); 124 } 125 (void) fclose(fp); 126 127 old__mbrtowc = __mbrtowc; 128 old__mbsinit = __mbsinit; 129 old__mbsnrtowcs = __mbsnrtowcs; 130 old__wcrtomb = __wcrtomb; 131 old__wcsnrtombs = __wcsnrtombs; 132 133 __mbrtowc = NULL; 134 __mbsinit = NULL; 135 __mbsnrtowcs = __mbsnrtowcs_std; 136 __wcrtomb = NULL; 137 __wcsnrtombs = __wcsnrtombs_std; 138 139 if (strcmp(rl->__encoding, "NONE") == 0) 140 ret = _none_init(rl); 141 else if (strcmp(rl->__encoding, "ASCII") == 0) 142 ret = _ascii_init(rl); 143 else if (strcmp(rl->__encoding, "UTF-8") == 0) 144 ret = _UTF8_init(rl); 145 else if (strcmp(rl->__encoding, "EUC") == 0) 146 ret = _EUC_init(rl); 147 else if (strcmp(rl->__encoding, "GB18030") == 0) 148 ret = _GB18030_init(rl); 149 else if (strcmp(rl->__encoding, "GB2312") == 0) 150 ret = _GB2312_init(rl); 151 else if (strcmp(rl->__encoding, "GBK") == 0) 152 ret = _GBK_init(rl); 153 else if (strcmp(rl->__encoding, "BIG5") == 0) 154 ret = _BIG5_init(rl); 155 else if (strcmp(rl->__encoding, "MSKanji") == 0) 156 ret = _MSKanji_init(rl); 157 else 158 ret = EINVAL; 159 160 if (ret == 0) { 161 if (CachedRuneLocale != NULL) { 162 /* See euc.c */ 163 if (strcmp(CachedRuneLocale->__encoding, "EUC") == 0) 164 free(CachedRuneLocale->__variable); 165 free(CachedRuneLocale); 166 } 167 CachedRuneLocale = _CurrentRuneLocale; 168 Cached__mbrtowc = __mbrtowc; 169 Cached__mbsinit = __mbsinit; 170 Cached__mbsnrtowcs = __mbsnrtowcs; 171 Cached__wcrtomb = __wcrtomb; 172 Cached__wcsnrtombs = __wcsnrtombs; 173 (void) strcpy(ctype_encoding, encoding); 174 175 /* 176 * We need to overwrite the _ctype array. This requires 177 * some finagling. This is because references to it may 178 * have been baked into applications. 179 * 180 * Note that it is interesting that toupper/tolower only 181 * produce defined results when the input is representable 182 * as a byte. 183 */ 184 185 /* 186 * The top half is the type mask array. Because we 187 * want to support both legacy Solaris code (which have 188 * mask valeus baked in to them), and we want to be able 189 * to import locale files from other sources (FreeBSD) 190 * which probably uses different masks, we have to perform 191 * a conversion here. Ugh. Note that the _CTYPE definitions 192 * we use from FreeBSD are richer than the Solaris legacy. 193 * 194 * We have to cope with these limitations though, because the 195 * inadequate Solaris definitions were baked into binaries. 196 */ 197 for (int i = 0; i < _CACHED_RUNES; i++) { 198 /* ctype can only encode the lower 8 bits. */ 199 __ctype[i+1] = rl->__runetype[i] & 0xff; 200 __ctype_mask[i] = rl->__runetype[i]; 201 } 202 203 /* The bottom half is the toupper/lower array */ 204 for (int i = 0; i < _CACHED_RUNES; i++) { 205 __ctype[258 + i] = i; 206 if (rl->__mapupper[i] && rl->__mapupper[i] != i) 207 __ctype[258+i] = rl->__mapupper[i]; 208 if (rl->__maplower[i] && rl->__maplower[i] != i) 209 __ctype[258+i] = rl->__maplower[i]; 210 211 /* Don't forget these annoyances either! */ 212 __trans_upper[i] = rl->__mapupper[i]; 213 __trans_lower[i] = rl->__maplower[i]; 214 } 215 216 /* 217 * Note that we expect the init code will have populated 218 * the CSWIDTH array (__ctype[514-520]) properly. 219 */ 220 } else { 221 __mbrtowc = old__mbrtowc; 222 __mbsinit = old__mbsinit; 223 __mbsnrtowcs = old__mbsnrtowcs; 224 __wcrtomb = old__wcrtomb; 225 __wcsnrtombs = old__wcsnrtombs; 226 free(rl); 227 } 228 229 return (ret); 230 } 231 232 int 233 __wrap_setrunelocale(const char *locale) 234 { 235 int ret = __setrunelocale(locale); 236 237 if (ret != 0) { 238 errno = ret; 239 return (_LDP_ERROR); 240 } 241 return (_LDP_LOADED); 242 } 243