xref: /freebsd/lib/libc/locale/setrunelocale.c (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
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  *
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 #define	__RUNETYPE_INTERNAL 1
41 
42 #include <runetype.h>
43 #include <errno.h>
44 #include <limits.h>
45 #include <string.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <wchar.h>
50 #include "ldpart.h"
51 #include "mblocal.h"
52 #include "setlocale.h"
53 
54 #undef _CurrentRuneLocale
55 extern _RuneLocale const *_CurrentRuneLocale;
56 /*
57  * A cached version of the runes for this thread.  Used by ctype.h
58  */
59 _Thread_local const _RuneLocale *_ThreadRuneLocale;
60 
61 extern int __mb_sb_limit;
62 
63 extern _RuneLocale	*_Read_RuneMagi(const char *);
64 
65 static int		__setrunelocale(struct xlocale_ctype *l, const char *);
66 
67 static void
68 destruct_ctype(void *v)
69 {
70 	struct xlocale_ctype *l = v;
71 
72 	if (&_DefaultRuneLocale != l->runes)
73 		free(l->runes);
74 	free(l);
75 }
76 
77 const _RuneLocale *
78 __getCurrentRuneLocale(void)
79 {
80 
81 	return (XLOCALE_CTYPE(__get_locale())->runes);
82 }
83 
84 static void
85 free_runes(_RuneLocale *rl)
86 {
87 	if ((rl != &_DefaultRuneLocale) && (rl)) {
88 		free(rl);
89 	}
90 }
91 
92 static int
93 __setrunelocale(struct xlocale_ctype *l, const char *encoding)
94 {
95 	_RuneLocale *rl;
96 	int ret;
97 	char *path;
98 	struct xlocale_ctype saved = *l;
99 
100 	/*
101 	 * The "C" and "POSIX" locale are always here.
102 	 */
103 	if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
104 		free_runes(saved.runes);
105 		(void) _none_init(l, (_RuneLocale*)&_DefaultRuneLocale);
106 		return (0);
107 	}
108 
109 	/* Range checking not needed, encoding length already checked before */
110 	if (asprintf(&path, "%s/%s/LC_CTYPE", _PathLocale, encoding) == -1)
111 		return (errno);
112 
113 	if ((rl = _Read_RuneMagi(path)) == NULL) {
114 		free(path);
115 		errno = EINVAL;
116 		return (errno);
117 	}
118 	free(path);
119 
120 	l->__mbrtowc = NULL;
121 	l->__mbsinit = NULL;
122 	l->__mbsnrtowcs = NULL;
123 	l->__wcrtomb = NULL;
124 	l->__wcsnrtombs = NULL;
125 
126 	rl->__sputrune = NULL;
127 	rl->__sgetrune = NULL;
128 	if (strcmp(rl->__encoding, "NONE:US-ASCII") == 0)
129 		ret = _ascii_init(l, rl);
130 	else if (strncmp(rl->__encoding, "NONE", 4) == 0)
131 		ret = _none_init(l, rl);
132 	else if (strcmp(rl->__encoding, "UTF-8") == 0)
133 		ret = _UTF8_init(l, rl);
134 	else if (strcmp(rl->__encoding, "EUC-CN") == 0)
135 		ret = _EUC_CN_init(l, rl);
136 	else if (strcmp(rl->__encoding, "EUC-JP") == 0)
137 		ret = _EUC_JP_init(l, rl);
138 	else if (strcmp(rl->__encoding, "EUC-KR") == 0)
139 		ret = _EUC_KR_init(l, rl);
140 	else if (strcmp(rl->__encoding, "EUC-TW") == 0)
141 		ret = _EUC_TW_init(l, rl);
142 	else if (strcmp(rl->__encoding, "GB18030") == 0)
143 		ret = _GB18030_init(l, rl);
144 	else if (strcmp(rl->__encoding, "GB2312") == 0)
145 		ret = _GB2312_init(l, rl);
146 	else if (strcmp(rl->__encoding, "GBK") == 0)
147 		ret = _GBK_init(l, rl);
148 	else if (strcmp(rl->__encoding, "BIG5") == 0)
149 		ret = _BIG5_init(l, rl);
150 	else if (strcmp(rl->__encoding, "MSKanji") == 0)
151 		ret = _MSKanji_init(l, rl);
152 	else
153 		ret = EFTYPE;
154 
155 	if (ret == 0) {
156 		/* Free the old runes if it exists. */
157 		free_runes(saved.runes);
158 		/* Reset the mbstates */
159 		memset(&l->c16rtomb, 0, sizeof(l->c16rtomb));
160 		memset(&l->c32rtomb, 0, sizeof(l->c32rtomb));
161 		memset(&l->mblen, 0, sizeof(l->mblen));
162 		memset(&l->mbrlen, 0, sizeof(l->mbrlen));
163 		memset(&l->mbrtoc16, 0, sizeof(l->mbrtoc16));
164 		memset(&l->mbrtoc32, 0, sizeof(l->mbrtoc32));
165 		memset(&l->mbrtowc, 0, sizeof(l->mbrtowc));
166 		memset(&l->mbsnrtowcs, 0, sizeof(l->mbsnrtowcs));
167 		memset(&l->mbsrtowcs, 0, sizeof(l->mbsrtowcs));
168 		memset(&l->mbtowc, 0, sizeof(l->mbtowc));
169 		memset(&l->wcrtomb, 0, sizeof(l->wcrtomb));
170 		memset(&l->wcsnrtombs, 0, sizeof(l->wcsnrtombs));
171 		memset(&l->wcsrtombs, 0, sizeof(l->wcsrtombs));
172 		memset(&l->wctomb, 0, sizeof(l->wctomb));
173 	} else {
174 		/* Restore the saved version if this failed. */
175 		memcpy(l, &saved, sizeof(struct xlocale_ctype));
176 		free(rl);
177 	}
178 
179 	return (ret);
180 }
181 
182 int
183 __wrap_setrunelocale(const char *locale)
184 {
185 	int ret = __setrunelocale(&__xlocale_global_ctype, locale);
186 
187 	if (ret != 0) {
188 		errno = ret;
189 		return (_LDP_ERROR);
190 	}
191 	__mb_cur_max = __xlocale_global_ctype.__mb_cur_max;
192 	__mb_sb_limit = __xlocale_global_ctype.__mb_sb_limit;
193 	_CurrentRuneLocale = __xlocale_global_ctype.runes;
194 	return (_LDP_LOADED);
195 }
196 
197 void
198 __set_thread_rune_locale(locale_t loc)
199 {
200 
201 	if (loc == NULL) {
202 		_ThreadRuneLocale = &_DefaultRuneLocale;
203 	} else if (loc == LC_GLOBAL_LOCALE) {
204 		_ThreadRuneLocale = 0;
205 	} else {
206 		_ThreadRuneLocale = XLOCALE_CTYPE(loc)->runes;
207 	}
208 }
209 
210 void *
211 __ctype_load(const char *locale, locale_t unused __unused)
212 {
213 	struct xlocale_ctype *l = calloc(sizeof(struct xlocale_ctype), 1);
214 	if (l == NULL)
215 		return (NULL);
216 
217 	l->header.header.destructor = destruct_ctype;
218 	if (__setrunelocale(l, locale)) {
219 		free(l);
220 		return (NULL);
221 	}
222 	return (l);
223 }
224