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