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(FILE *); 67 68 static int __setrunelocale(struct xlocale_ctype *l, const char *); 69 70 #define __collate_substitute_nontrivial (table->__collate_substitute_nontrivial) 71 #define __collate_substitute_table_ptr (table->__collate_substitute_table_ptr) 72 #define __collate_char_pri_table_ptr (table->__collate_char_pri_table_ptr) 73 #define __collate_chain_pri_table (table->__collate_chain_pri_table) 74 75 76 static void destruct_ctype(void *v) 77 { 78 struct xlocale_ctype *l = v; 79 if (strcmp(l->runes->__encoding, "EUC") == 0) 80 free(l->runes->__variable); 81 if (&_DefaultRuneLocale != l->runes) 82 free(l->runes); 83 free(l); 84 } 85 86 const _RuneLocale *__getCurrentRuneLocale(void) 87 { 88 return XLOCALE_CTYPE(__get_locale())->runes; 89 } 90 91 static void free_runes(_RuneLocale *rl) 92 { 93 /* FIXME: The "EUC" check here is a hideous abstraction violation. */ 94 if ((rl != &_DefaultRuneLocale) && (rl)) { 95 if (strcmp(rl->__encoding, "EUC") == 0) { 96 free(rl->__variable); 97 } 98 free(rl); 99 } 100 } 101 102 static int 103 __setrunelocale(struct xlocale_ctype *l, const char *encoding) 104 { 105 FILE *fp; 106 char name[PATH_MAX]; 107 _RuneLocale *rl; 108 int saverr, ret; 109 struct xlocale_ctype saved = *l; 110 111 /* 112 * The "C" and "POSIX" locale are always here. 113 */ 114 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) { 115 free_runes(saved.runes); 116 (void) _none_init(l, (_RuneLocale*)&_DefaultRuneLocale); 117 return (0); 118 } 119 120 /* Range checking not needed, encoding length already checked before */ 121 (void) strcpy(name, _PathLocale); 122 (void) strcat(name, "/"); 123 (void) strcat(name, encoding); 124 (void) strcat(name, "/LC_CTYPE"); 125 126 if ((fp = fopen(name, "r")) == NULL) 127 return (errno == 0 ? ENOENT : errno); 128 129 if ((rl = _Read_RuneMagi(fp)) == NULL) { 130 saverr = (errno == 0 ? EFTYPE : errno); 131 (void)fclose(fp); 132 return (saverr); 133 } 134 (void)fclose(fp); 135 136 l->__mbrtowc = NULL; 137 l->__mbsinit = NULL; 138 l->__mbsnrtowcs = __mbsnrtowcs_std; 139 l->__wcrtomb = NULL; 140 l->__wcsnrtombs = __wcsnrtombs_std; 141 142 rl->__sputrune = NULL; 143 rl->__sgetrune = NULL; 144 if (strcmp(rl->__encoding, "NONE") == 0) 145 ret = _none_init(l, rl); 146 else if (strcmp(rl->__encoding, "ASCII") == 0) 147 ret = _ascii_init(l, rl); 148 else if (strcmp(rl->__encoding, "UTF-8") == 0) 149 ret = _UTF8_init(l, rl); 150 else if (strcmp(rl->__encoding, "EUC") == 0) 151 ret = _EUC_init(l, rl); 152 else if (strcmp(rl->__encoding, "GB18030") == 0) 153 ret = _GB18030_init(l, rl); 154 else if (strcmp(rl->__encoding, "GB2312") == 0) 155 ret = _GB2312_init(l, rl); 156 else if (strcmp(rl->__encoding, "GBK") == 0) 157 ret = _GBK_init(l, rl); 158 else if (strcmp(rl->__encoding, "BIG5") == 0) 159 ret = _BIG5_init(l, rl); 160 else if (strcmp(rl->__encoding, "MSKanji") == 0) 161 ret = _MSKanji_init(l, rl); 162 else 163 ret = EFTYPE; 164 165 if (ret == 0) { 166 /* Free the old runes if it exists. */ 167 free_runes(saved.runes); 168 } else { 169 /* Restore the saved version if this failed. */ 170 memcpy(l, &saved, sizeof(struct xlocale_ctype)); 171 free(rl); 172 } 173 174 return (ret); 175 } 176 177 int 178 __wrap_setrunelocale(const char *locale) 179 { 180 int ret = __setrunelocale(&__xlocale_global_ctype, locale); 181 182 if (ret != 0) { 183 errno = ret; 184 return (_LDP_ERROR); 185 } 186 __mb_cur_max = __xlocale_global_ctype.__mb_cur_max; 187 __mb_sb_limit = __xlocale_global_ctype.__mb_sb_limit; 188 _CurrentRuneLocale = __xlocale_global_ctype.runes; 189 return (_LDP_LOADED); 190 } 191 192 #ifndef __NO_TLS 193 void 194 __set_thread_rune_locale(locale_t loc) { 195 196 if (loc == NULL) { 197 _ThreadRuneLocale = &_DefaultRuneLocale; 198 } else { 199 _ThreadRuneLocale = XLOCALE_CTYPE(loc)->runes; 200 } 201 } 202 #endif 203 204 void * 205 __ctype_load(const char *locale, locale_t unused) 206 { 207 struct xlocale_ctype *l = calloc(sizeof(struct xlocale_ctype), 1); 208 l->header.header.destructor = destruct_ctype; 209 if (__setrunelocale(l, locale)) 210 { 211 free(l); 212 return NULL; 213 } 214 return l; 215 } 216