xref: /freebsd/lib/libc/locale/setrunelocale.c (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
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  * 3. 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$");
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
58 extern _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 
66 extern int __mb_sb_limit;
67 
68 extern _RuneLocale	*_Read_RuneMagi(const char *);
69 
70 static int		__setrunelocale(struct xlocale_ctype *l, const char *);
71 
72 static void
73 destruct_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 
82 const _RuneLocale *
83 __getCurrentRuneLocale(void)
84 {
85 
86 	return (XLOCALE_CTYPE(__get_locale())->runes);
87 }
88 
89 static void
90 free_runes(_RuneLocale *rl)
91 {
92 	if ((rl != &_DefaultRuneLocale) && (rl)) {
93 		free(rl);
94 	}
95 }
96 
97 static 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 	if (asprintf(&path, "%s/%s/LC_CTYPE", _PathLocale, encoding) == -1)
116 		return (errno);
117 
118 	if ((rl = _Read_RuneMagi(path)) == NULL) {
119 		free(path);
120 		errno = EINVAL;
121 		return (errno);
122 	}
123 	free(path);
124 
125 	l->__mbrtowc = NULL;
126 	l->__mbsinit = NULL;
127 	l->__mbsnrtowcs = NULL;
128 	l->__wcrtomb = NULL;
129 	l->__wcsnrtombs = NULL;
130 
131 	rl->__sputrune = NULL;
132 	rl->__sgetrune = NULL;
133 	if (strcmp(rl->__encoding, "NONE:US-ASCII") == 0)
134 		ret = _ascii_init(l, rl);
135 	else if (strncmp(rl->__encoding, "NONE", 4) == 0)
136 		ret = _none_init(l, rl);
137 	else if (strcmp(rl->__encoding, "UTF-8") == 0)
138 		ret = _UTF8_init(l, rl);
139 	else if (strcmp(rl->__encoding, "EUC-CN") == 0)
140 		ret = _EUC_CN_init(l, rl);
141 	else if (strcmp(rl->__encoding, "EUC-JP") == 0)
142 		ret = _EUC_JP_init(l, rl);
143 	else if (strcmp(rl->__encoding, "EUC-KR") == 0)
144 		ret = _EUC_KR_init(l, rl);
145 	else if (strcmp(rl->__encoding, "EUC-TW") == 0)
146 		ret = _EUC_TW_init(l, rl);
147 	else if (strcmp(rl->__encoding, "GB18030") == 0)
148 		ret = _GB18030_init(l, rl);
149 	else if (strcmp(rl->__encoding, "GB2312") == 0)
150 		ret = _GB2312_init(l, rl);
151 	else if (strcmp(rl->__encoding, "GBK") == 0)
152 		ret = _GBK_init(l, rl);
153 	else if (strcmp(rl->__encoding, "BIG5") == 0)
154 		ret = _BIG5_init(l, rl);
155 	else if (strcmp(rl->__encoding, "MSKanji") == 0)
156 		ret = _MSKanji_init(l, rl);
157 	else
158 		ret = EFTYPE;
159 
160 	if (ret == 0) {
161 		/* Free the old runes if it exists. */
162 		free_runes(saved.runes);
163 		/* Reset the mbstates */
164 		memset(&l->c16rtomb, 0, sizeof(l->c16rtomb));
165 		memset(&l->c32rtomb, 0, sizeof(l->c32rtomb));
166 		memset(&l->mblen, 0, sizeof(l->mblen));
167 		memset(&l->mbrlen, 0, sizeof(l->mbrlen));
168 		memset(&l->mbrtoc16, 0, sizeof(l->mbrtoc16));
169 		memset(&l->mbrtoc32, 0, sizeof(l->mbrtoc32));
170 		memset(&l->mbrtowc, 0, sizeof(l->mbrtowc));
171 		memset(&l->mbsnrtowcs, 0, sizeof(l->mbsnrtowcs));
172 		memset(&l->mbsrtowcs, 0, sizeof(l->mbsrtowcs));
173 		memset(&l->mbtowc, 0, sizeof(l->mbtowc));
174 		memset(&l->wcrtomb, 0, sizeof(l->wcrtomb));
175 		memset(&l->wcsnrtombs, 0, sizeof(l->wcsnrtombs));
176 		memset(&l->wcsrtombs, 0, sizeof(l->wcsrtombs));
177 		memset(&l->wctomb, 0, sizeof(l->wctomb));
178 	} else {
179 		/* Restore the saved version if this failed. */
180 		memcpy(l, &saved, sizeof(struct xlocale_ctype));
181 		free(rl);
182 	}
183 
184 	return (ret);
185 }
186 
187 int
188 __wrap_setrunelocale(const char *locale)
189 {
190 	int ret = __setrunelocale(&__xlocale_global_ctype, locale);
191 
192 	if (ret != 0) {
193 		errno = ret;
194 		return (_LDP_ERROR);
195 	}
196 	__mb_cur_max = __xlocale_global_ctype.__mb_cur_max;
197 	__mb_sb_limit = __xlocale_global_ctype.__mb_sb_limit;
198 	_CurrentRuneLocale = __xlocale_global_ctype.runes;
199 	return (_LDP_LOADED);
200 }
201 
202 #ifndef __NO_TLS
203 void
204 __set_thread_rune_locale(locale_t loc)
205 {
206 
207 	if (loc == NULL) {
208 		_ThreadRuneLocale = &_DefaultRuneLocale;
209 	} else if (loc == LC_GLOBAL_LOCALE) {
210 		_ThreadRuneLocale = 0;
211 	} else {
212 		_ThreadRuneLocale = XLOCALE_CTYPE(loc)->runes;
213 	}
214 }
215 #endif
216 
217 void *
218 __ctype_load(const char *locale, locale_t unused __unused)
219 {
220 	struct xlocale_ctype *l = calloc(sizeof(struct xlocale_ctype), 1);
221 
222 	l->header.header.destructor = destruct_ctype;
223 	if (__setrunelocale(l, locale)) {
224 		free(l);
225 		return (NULL);
226 	}
227 	return (l);
228 }
229