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 * 3. 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 * $FreeBSD: head/lib/libc/locale/setrunelocale.c 264038 2014-04-02 11:10:46Z theraven $ 38 */ 39 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 /* 58 * A cached version of the runes for this thread. Used by ctype.h 59 */ 60 __thread const _RuneLocale *_ThreadRuneLocale; 61 62 extern _RuneLocale *_Read_RuneMagi(const char *); 63 64 static int __setrunelocale(struct xlocale_ctype *l, const char *); 65 66 static void 67 destruct_ctype(void *v) 68 { 69 struct xlocale_ctype *l = v; 70 71 if (&_DefaultRuneLocale != l->runes) 72 free(l->runes); 73 free(l); 74 } 75 76 const _RuneLocale * 77 __getCurrentRuneLocale(void) 78 { 79 80 return (XLOCALE_CTYPE(__get_locale())->runes); 81 } 82 83 static void 84 free_runes(_RuneLocale *rl) 85 { 86 if ((rl != &_DefaultRuneLocale) && (rl)) { 87 free(rl); 88 } 89 } 90 91 static int 92 __setrunelocale(struct xlocale_ctype *l, const char *encoding) 93 { 94 _RuneLocale *rl; 95 int ret; 96 char path[PATH_MAX]; 97 struct xlocale_ctype saved = *l; 98 99 /* 100 * The "C" and "POSIX" locale are always here. 101 */ 102 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) { 103 free_runes(saved.runes); 104 (void) _none_init(l, (_RuneLocale*)&_DefaultRuneLocale); 105 return (0); 106 } 107 108 /* Range checking not needed, encoding length already checked before */ 109 (void) snprintf(path, sizeof (path), "%s/%s/LC_CTYPE", 110 _PathLocale, encoding); 111 112 if ((rl = _Read_RuneMagi(path)) == NULL) { 113 errno = EINVAL; 114 return (errno); 115 } 116 117 l->__mbrtowc = NULL; 118 l->__mbsinit = NULL; 119 l->__mbsnrtowcs = NULL; 120 l->__wcrtomb = NULL; 121 l->__wcsnrtombs = NULL; 122 123 rl->__sputrune = NULL; 124 rl->__sgetrune = NULL; 125 if (strcmp(rl->__encoding, "NONE:US-ASCII") == 0) 126 ret = _ascii_init(l, rl); 127 else if (strncmp(rl->__encoding, "NONE", 4) == 0) 128 ret = _none_init(l, rl); 129 else if (strcmp(rl->__encoding, "UTF-8") == 0) 130 ret = _UTF8_init(l, rl); 131 else if (strcmp(rl->__encoding, "EUC-CN") == 0) 132 ret = _EUC_CN_init(l, rl); 133 else if (strcmp(rl->__encoding, "EUC-JP") == 0) 134 ret = _EUC_JP_init(l, rl); 135 else if (strcmp(rl->__encoding, "EUC-KR") == 0) 136 ret = _EUC_KR_init(l, rl); 137 else if (strcmp(rl->__encoding, "EUC-TW") == 0) 138 ret = _EUC_TW_init(l, rl); 139 else if (strcmp(rl->__encoding, "GB18030") == 0) 140 ret = _GB18030_init(l, rl); 141 else if (strcmp(rl->__encoding, "GB2312") == 0) 142 ret = _GB2312_init(l, rl); 143 else if (strcmp(rl->__encoding, "GBK") == 0) 144 ret = _GBK_init(l, rl); 145 else if (strcmp(rl->__encoding, "BIG5") == 0) 146 ret = _BIG5_init(l, rl); 147 else if (strcmp(rl->__encoding, "MSKanji") == 0) 148 ret = _MSKanji_init(l, rl); 149 else 150 ret = EFTYPE; 151 152 if (ret == 0) { 153 /* Free the old runes if it exists. */ 154 free_runes(saved.runes); 155 } else { 156 /* Restore the saved version if this failed. */ 157 memcpy(l, &saved, sizeof(struct xlocale_ctype)); 158 free(rl); 159 } 160 161 return (ret); 162 } 163 164 int 165 __wrap_setrunelocale(const char *locale) 166 { 167 int ret = __setrunelocale(&__xlocale_global_ctype, locale); 168 169 if (ret != 0) { 170 errno = ret; 171 return (_LDP_ERROR); 172 } 173 __mb_cur_max = __xlocale_global_ctype.__mb_cur_max; 174 __mb_sb_limit = __xlocale_global_ctype.__mb_sb_limit; 175 _CurrentRuneLocale = __xlocale_global_ctype.runes; 176 return (_LDP_LOADED); 177 } 178 179 void 180 __set_thread_rune_locale(locale_t loc) 181 { 182 183 if (loc == NULL) { 184 _ThreadRuneLocale = &_DefaultRuneLocale; 185 } else if (loc == LC_GLOBAL_LOCALE) { 186 _ThreadRuneLocale = NULL; 187 } else { 188 _ThreadRuneLocale = XLOCALE_CTYPE(loc)->runes; 189 } 190 } 191 192 void * 193 __ctype_load(const char *locale, locale_t unused __unused) 194 { 195 struct xlocale_ctype *l = calloc(sizeof(struct xlocale_ctype), 1); 196 197 l->header.header.destructor = destruct_ctype; 198 if (__setrunelocale(l, locale)) { 199 free(l); 200 return (NULL); 201 } 202 return (l); 203 } 204