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 * Copyright (c) 2011 The FreeBSD Foundation 9 * All rights reserved. 10 * Portions of this software were developed by David Chisnall 11 * under sponsorship from the FreeBSD Foundation. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #define __RUNETYPE_INTERNAL 1 42 43 #include <runetype.h> 44 #include <errno.h> 45 #include <limits.h> 46 #include <string.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <unistd.h> 50 #include <wchar.h> 51 #include "ldpart.h" 52 #include "mblocal.h" 53 #include "setlocale.h" 54 55 #undef _CurrentRuneLocale 56 extern _RuneLocale const *_CurrentRuneLocale; 57 #ifndef __NO_TLS 58 /* 59 * A cached version of the runes for this thread. Used by ctype.h 60 */ 61 _Thread_local const _RuneLocale *_ThreadRuneLocale; 62 #endif 63 64 extern int __mb_sb_limit; 65 66 extern _RuneLocale *_Read_RuneMagi(const char *); 67 68 static int __setrunelocale(struct xlocale_ctype *l, const char *); 69 70 static void 71 destruct_ctype(void *v) 72 { 73 struct xlocale_ctype *l = v; 74 75 if (&_DefaultRuneLocale != l->runes) 76 free(l->runes); 77 free(l); 78 } 79 80 const _RuneLocale * 81 __getCurrentRuneLocale(void) 82 { 83 84 return XLOCALE_CTYPE(__get_locale())->runes; 85 } 86 87 static void 88 free_runes(_RuneLocale *rl) 89 { 90 if ((rl != &_DefaultRuneLocale) && (rl)) { 91 free(rl); 92 } 93 } 94 95 static int 96 __setrunelocale(struct xlocale_ctype *l, const char *encoding) 97 { 98 _RuneLocale *rl; 99 int ret; 100 char *path; 101 struct xlocale_ctype saved = *l; 102 103 /* 104 * The "C" and "POSIX" locale are always here. 105 */ 106 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) { 107 free_runes(saved.runes); 108 (void) _none_init(l, (_RuneLocale*)&_DefaultRuneLocale); 109 return (0); 110 } 111 112 /* Range checking not needed, encoding length already checked before */ 113 asprintf(&path, "%s/%s/LC_CTYPE", _PathLocale, encoding); 114 if (path == NULL) 115 return (0); 116 117 if ((rl = _Read_RuneMagi(path)) == NULL) { 118 free(path); 119 errno = EINVAL; 120 return (errno); 121 } 122 free(path); 123 124 l->__mbrtowc = NULL; 125 l->__mbsinit = NULL; 126 l->__mbsnrtowcs = NULL; 127 l->__wcrtomb = NULL; 128 l->__wcsnrtombs = NULL; 129 130 rl->__sputrune = NULL; 131 rl->__sgetrune = NULL; 132 if (strcmp(rl->__encoding, "NONE:US-ASCII") == 0) 133 ret = _ascii_init(l, rl); 134 else if (strncmp(rl->__encoding, "NONE", 4) == 0) 135 ret = _none_init(l, rl); 136 else if (strcmp(rl->__encoding, "UTF-8") == 0) 137 ret = _UTF8_init(l, rl); 138 else if (strcmp(rl->__encoding, "EUC-CN") == 0) 139 ret = _EUC_CN_init(l, rl); 140 else if (strcmp(rl->__encoding, "EUC-JP") == 0) 141 ret = _EUC_JP_init(l, rl); 142 else if (strcmp(rl->__encoding, "EUC-KR") == 0) 143 ret = _EUC_KR_init(l, rl); 144 else if (strcmp(rl->__encoding, "EUC-TW") == 0) 145 ret = _EUC_TW_init(l, rl); 146 else if (strcmp(rl->__encoding, "GB18030") == 0) 147 ret = _GB18030_init(l, rl); 148 else if (strcmp(rl->__encoding, "GB2312") == 0) 149 ret = _GB2312_init(l, rl); 150 else if (strcmp(rl->__encoding, "GBK") == 0) 151 ret = _GBK_init(l, rl); 152 else if (strcmp(rl->__encoding, "BIG5") == 0) 153 ret = _BIG5_init(l, rl); 154 else if (strcmp(rl->__encoding, "MSKanji") == 0) 155 ret = _MSKanji_init(l, rl); 156 else 157 ret = EFTYPE; 158 159 if (ret == 0) { 160 /* Free the old runes if it exists. */ 161 free_runes(saved.runes); 162 } else { 163 /* Restore the saved version if this failed. */ 164 memcpy(l, &saved, sizeof(struct xlocale_ctype)); 165 free(rl); 166 } 167 168 return (ret); 169 } 170 171 int 172 __wrap_setrunelocale(const char *locale) 173 { 174 int ret = __setrunelocale(&__xlocale_global_ctype, locale); 175 176 if (ret != 0) { 177 errno = ret; 178 return (_LDP_ERROR); 179 } 180 __mb_cur_max = __xlocale_global_ctype.__mb_cur_max; 181 __mb_sb_limit = __xlocale_global_ctype.__mb_sb_limit; 182 _CurrentRuneLocale = __xlocale_global_ctype.runes; 183 return (_LDP_LOADED); 184 } 185 186 #ifndef __NO_TLS 187 void 188 __set_thread_rune_locale(locale_t loc) 189 { 190 191 if (loc == NULL) { 192 _ThreadRuneLocale = &_DefaultRuneLocale; 193 } else if (loc == LC_GLOBAL_LOCALE) { 194 _ThreadRuneLocale = 0; 195 } else { 196 _ThreadRuneLocale = XLOCALE_CTYPE(loc)->runes; 197 } 198 } 199 #endif 200 201 void * 202 __ctype_load(const char *locale, locale_t unused __unused) 203 { 204 struct xlocale_ctype *l = calloc(sizeof(struct xlocale_ctype), 1); 205 206 l->header.header.destructor = destruct_ctype; 207 if (__setrunelocale(l, locale)) 208 { 209 free(l); 210 return NULL; 211 } 212 return l; 213 } 214