1995-10-23 01:34:17 +00:00
|
|
|
/*-
|
2017-11-20 19:49:47 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1995-10-23 01:34:17 +00:00
|
|
|
* Copyright (c) 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Paul Borman at Krystal Technologies.
|
|
|
|
*
|
2011-11-20 14:45:42 +00:00
|
|
|
* Copyright (c) 2011 The FreeBSD Foundation
|
|
|
|
* All rights reserved.
|
|
|
|
* Portions of this software were developed by David Chisnall
|
|
|
|
* under sponsorship from the FreeBSD Foundation.
|
|
|
|
*
|
1995-10-23 01:34:17 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1995-10-23 01:34:17 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2002-03-22 21:53:29 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2017-08-26 16:11:21 +00:00
|
|
|
#define __RUNETYPE_INTERNAL 1
|
2012-03-04 15:31:13 +00:00
|
|
|
|
2004-10-17 06:51:50 +00:00
|
|
|
#include <runetype.h>
|
1995-10-23 01:34:17 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1997-04-07 08:54:38 +00:00
|
|
|
#include <unistd.h>
|
2003-11-01 05:13:13 +00:00
|
|
|
#include <wchar.h>
|
2003-07-06 02:03:37 +00:00
|
|
|
#include "ldpart.h"
|
2004-05-12 14:09:04 +00:00
|
|
|
#include "mblocal.h"
|
1997-02-06 09:11:06 +00:00
|
|
|
#include "setlocale.h"
|
1995-10-23 01:34:17 +00:00
|
|
|
|
2012-03-04 15:31:13 +00:00
|
|
|
#undef _CurrentRuneLocale
|
|
|
|
extern _RuneLocale const *_CurrentRuneLocale;
|
|
|
|
/*
|
|
|
|
* A cached version of the runes for this thread. Used by ctype.h
|
|
|
|
*/
|
|
|
|
_Thread_local const _RuneLocale *_ThreadRuneLocale;
|
|
|
|
|
2007-10-13 16:28:22 +00:00
|
|
|
extern int __mb_sb_limit;
|
|
|
|
|
2015-08-08 18:22:14 +00:00
|
|
|
extern _RuneLocale *_Read_RuneMagi(const char *);
|
1995-10-23 01:34:17 +00:00
|
|
|
|
2011-11-20 14:45:42 +00:00
|
|
|
static int __setrunelocale(struct xlocale_ctype *l, const char *);
|
|
|
|
|
2012-12-10 17:34:33 +00:00
|
|
|
static void
|
|
|
|
destruct_ctype(void *v)
|
2011-11-20 14:45:42 +00:00
|
|
|
{
|
|
|
|
struct xlocale_ctype *l = v;
|
2012-12-10 17:34:33 +00:00
|
|
|
|
2017-08-26 16:11:21 +00:00
|
|
|
if (&_DefaultRuneLocale != l->runes)
|
2011-11-20 14:45:42 +00:00
|
|
|
free(l->runes);
|
|
|
|
free(l);
|
|
|
|
}
|
2012-03-04 15:31:13 +00:00
|
|
|
|
2012-12-10 17:34:33 +00:00
|
|
|
const _RuneLocale *
|
|
|
|
__getCurrentRuneLocale(void)
|
2011-11-20 14:45:42 +00:00
|
|
|
{
|
2012-12-10 17:34:33 +00:00
|
|
|
|
2017-08-26 16:11:21 +00:00
|
|
|
return (XLOCALE_CTYPE(__get_locale())->runes);
|
2011-11-20 14:45:42 +00:00
|
|
|
}
|
2003-07-06 02:03:37 +00:00
|
|
|
|
2012-12-10 17:34:33 +00:00
|
|
|
static void
|
|
|
|
free_runes(_RuneLocale *rl)
|
2012-06-11 14:02:02 +00:00
|
|
|
{
|
|
|
|
if ((rl != &_DefaultRuneLocale) && (rl)) {
|
|
|
|
free(rl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-06 02:03:37 +00:00
|
|
|
static int
|
2011-11-20 14:45:42 +00:00
|
|
|
__setrunelocale(struct xlocale_ctype *l, const char *encoding)
|
2003-07-06 02:03:37 +00:00
|
|
|
{
|
|
|
|
_RuneLocale *rl;
|
2015-08-08 18:22:14 +00:00
|
|
|
int ret;
|
2015-08-09 12:13:30 +00:00
|
|
|
char *path;
|
2011-11-20 14:45:42 +00:00
|
|
|
struct xlocale_ctype saved = *l;
|
2003-07-06 02:03:37 +00:00
|
|
|
|
1995-10-23 01:34:17 +00:00
|
|
|
/*
|
|
|
|
* The "C" and "POSIX" locale are always here.
|
|
|
|
*/
|
2002-08-08 05:51:54 +00:00
|
|
|
if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
|
2012-06-11 14:02:02 +00:00
|
|
|
free_runes(saved.runes);
|
2011-11-20 14:45:42 +00:00
|
|
|
(void) _none_init(l, (_RuneLocale*)&_DefaultRuneLocale);
|
2002-08-03 11:55:19 +00:00
|
|
|
return (0);
|
1995-10-23 01:34:17 +00:00
|
|
|
}
|
|
|
|
|
2003-07-06 02:03:37 +00:00
|
|
|
/* Range checking not needed, encoding length already checked before */
|
2017-09-29 16:30:50 +00:00
|
|
|
if (asprintf(&path, "%s/%s/LC_CTYPE", _PathLocale, encoding) == -1)
|
|
|
|
return (errno);
|
2015-08-08 18:22:14 +00:00
|
|
|
|
|
|
|
if ((rl = _Read_RuneMagi(path)) == NULL) {
|
2015-08-09 12:13:30 +00:00
|
|
|
free(path);
|
2015-08-08 18:22:14 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
return (errno);
|
1995-10-23 01:34:17 +00:00
|
|
|
}
|
2015-08-09 12:13:30 +00:00
|
|
|
free(path);
|
1995-10-23 01:34:17 +00:00
|
|
|
|
2011-11-20 14:45:42 +00:00
|
|
|
l->__mbrtowc = NULL;
|
|
|
|
l->__mbsinit = NULL;
|
2015-08-08 18:22:14 +00:00
|
|
|
l->__mbsnrtowcs = NULL;
|
2011-11-20 14:45:42 +00:00
|
|
|
l->__wcrtomb = NULL;
|
2015-08-08 18:22:14 +00:00
|
|
|
l->__wcsnrtombs = NULL;
|
2008-01-23 03:05:35 +00:00
|
|
|
|
2004-10-17 06:51:50 +00:00
|
|
|
rl->__sputrune = NULL;
|
|
|
|
rl->__sgetrune = NULL;
|
2015-11-09 22:06:22 +00:00
|
|
|
if (strcmp(rl->__encoding, "NONE:US-ASCII") == 0)
|
|
|
|
ret = _ascii_init(l, rl);
|
|
|
|
else if (strncmp(rl->__encoding, "NONE", 4) == 0)
|
2011-11-20 14:45:42 +00:00
|
|
|
ret = _none_init(l, rl);
|
2008-01-23 03:05:35 +00:00
|
|
|
else if (strcmp(rl->__encoding, "UTF-8") == 0)
|
2011-11-20 14:45:42 +00:00
|
|
|
ret = _UTF8_init(l, rl);
|
2015-08-08 18:22:14 +00:00
|
|
|
else if (strcmp(rl->__encoding, "EUC-CN") == 0)
|
|
|
|
ret = _EUC_CN_init(l, rl);
|
|
|
|
else if (strcmp(rl->__encoding, "EUC-JP") == 0)
|
|
|
|
ret = _EUC_JP_init(l, rl);
|
|
|
|
else if (strcmp(rl->__encoding, "EUC-KR") == 0)
|
|
|
|
ret = _EUC_KR_init(l, rl);
|
|
|
|
else if (strcmp(rl->__encoding, "EUC-TW") == 0)
|
|
|
|
ret = _EUC_TW_init(l, rl);
|
2008-01-23 03:05:35 +00:00
|
|
|
else if (strcmp(rl->__encoding, "GB18030") == 0)
|
2017-08-26 16:11:21 +00:00
|
|
|
ret = _GB18030_init(l, rl);
|
2008-01-23 03:05:35 +00:00
|
|
|
else if (strcmp(rl->__encoding, "GB2312") == 0)
|
2011-11-20 14:45:42 +00:00
|
|
|
ret = _GB2312_init(l, rl);
|
2008-01-23 03:05:35 +00:00
|
|
|
else if (strcmp(rl->__encoding, "GBK") == 0)
|
2011-11-20 14:45:42 +00:00
|
|
|
ret = _GBK_init(l, rl);
|
2008-01-23 03:05:35 +00:00
|
|
|
else if (strcmp(rl->__encoding, "BIG5") == 0)
|
2011-11-20 14:45:42 +00:00
|
|
|
ret = _BIG5_init(l, rl);
|
2008-01-23 03:05:35 +00:00
|
|
|
else if (strcmp(rl->__encoding, "MSKanji") == 0)
|
2011-11-20 14:45:42 +00:00
|
|
|
ret = _MSKanji_init(l, rl);
|
2008-01-23 03:05:35 +00:00
|
|
|
else
|
2002-08-08 05:51:54 +00:00
|
|
|
ret = EFTYPE;
|
2008-01-23 02:17:27 +00:00
|
|
|
|
2002-08-08 05:51:54 +00:00
|
|
|
if (ret == 0) {
|
2011-11-20 14:45:42 +00:00
|
|
|
/* Free the old runes if it exists. */
|
2012-06-11 14:02:02 +00:00
|
|
|
free_runes(saved.runes);
|
Reset persistent mbstates when rune locale encoding changes.
This was shown to be a problem by side effect of now-enabled test case,
which was going through C, en_US.UTF-8, ja_JP.SJIS, and ja_JP.eucJP,
and failing eventually as data in mbrtowc's mbstate, that was
perfectly correct for en_US.UTF-8 was treated as incorrect for
ja_JP.SJIS, failing the entire test case.
This makes the persistent mbstates to be per ctype-component,
and not per-locale so we could easily reset the mbstates when
only LC_CTYPE is changed.
Reviewed by: bapt, pfg
Approved by: kib (mentor, implicit)
Differential Revision: https://reviews.freebsd.org/D17796
2018-11-09 03:32:53 +00:00
|
|
|
/* Reset the mbstates */
|
|
|
|
memset(&l->c16rtomb, 0, sizeof(l->c16rtomb));
|
|
|
|
memset(&l->c32rtomb, 0, sizeof(l->c32rtomb));
|
|
|
|
memset(&l->mblen, 0, sizeof(l->mblen));
|
|
|
|
memset(&l->mbrlen, 0, sizeof(l->mbrlen));
|
|
|
|
memset(&l->mbrtoc16, 0, sizeof(l->mbrtoc16));
|
|
|
|
memset(&l->mbrtoc32, 0, sizeof(l->mbrtoc32));
|
|
|
|
memset(&l->mbrtowc, 0, sizeof(l->mbrtowc));
|
|
|
|
memset(&l->mbsnrtowcs, 0, sizeof(l->mbsnrtowcs));
|
|
|
|
memset(&l->mbsrtowcs, 0, sizeof(l->mbsrtowcs));
|
|
|
|
memset(&l->mbtowc, 0, sizeof(l->mbtowc));
|
|
|
|
memset(&l->wcrtomb, 0, sizeof(l->wcrtomb));
|
|
|
|
memset(&l->wcsnrtombs, 0, sizeof(l->wcsnrtombs));
|
|
|
|
memset(&l->wcsrtombs, 0, sizeof(l->wcsrtombs));
|
|
|
|
memset(&l->wctomb, 0, sizeof(l->wctomb));
|
2008-01-23 03:05:35 +00:00
|
|
|
} else {
|
2011-11-20 14:45:42 +00:00
|
|
|
/* Restore the saved version if this failed. */
|
|
|
|
memcpy(l, &saved, sizeof(struct xlocale_ctype));
|
2002-08-08 05:51:54 +00:00
|
|
|
free(rl);
|
2008-01-23 03:05:35 +00:00
|
|
|
}
|
2002-08-08 05:51:54 +00:00
|
|
|
|
|
|
|
return (ret);
|
1995-10-23 01:34:17 +00:00
|
|
|
}
|
|
|
|
|
2003-07-06 02:03:37 +00:00
|
|
|
int
|
|
|
|
__wrap_setrunelocale(const char *locale)
|
|
|
|
{
|
2011-11-20 14:45:42 +00:00
|
|
|
int ret = __setrunelocale(&__xlocale_global_ctype, locale);
|
2003-07-06 02:03:37 +00:00
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
errno = ret;
|
|
|
|
return (_LDP_ERROR);
|
|
|
|
}
|
2011-11-20 14:45:42 +00:00
|
|
|
__mb_cur_max = __xlocale_global_ctype.__mb_cur_max;
|
|
|
|
__mb_sb_limit = __xlocale_global_ctype.__mb_sb_limit;
|
2012-03-04 15:31:13 +00:00
|
|
|
_CurrentRuneLocale = __xlocale_global_ctype.runes;
|
2003-07-06 02:03:37 +00:00
|
|
|
return (_LDP_LOADED);
|
|
|
|
}
|
2012-03-04 15:31:13 +00:00
|
|
|
|
|
|
|
void
|
2012-12-10 17:34:33 +00:00
|
|
|
__set_thread_rune_locale(locale_t loc)
|
|
|
|
{
|
2012-03-04 15:31:13 +00:00
|
|
|
|
|
|
|
if (loc == NULL) {
|
|
|
|
_ThreadRuneLocale = &_DefaultRuneLocale;
|
2014-04-02 11:10:46 +00:00
|
|
|
} else if (loc == LC_GLOBAL_LOCALE) {
|
|
|
|
_ThreadRuneLocale = 0;
|
2012-03-04 15:31:13 +00:00
|
|
|
} else {
|
|
|
|
_ThreadRuneLocale = XLOCALE_CTYPE(loc)->runes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2015-08-08 18:22:14 +00:00
|
|
|
__ctype_load(const char *locale, locale_t unused __unused)
|
2011-11-20 14:45:42 +00:00
|
|
|
{
|
|
|
|
struct xlocale_ctype *l = calloc(sizeof(struct xlocale_ctype), 1);
|
2012-12-10 17:34:33 +00:00
|
|
|
|
2011-11-20 14:45:42 +00:00
|
|
|
l->header.header.destructor = destruct_ctype;
|
2017-08-26 16:11:21 +00:00
|
|
|
if (__setrunelocale(l, locale)) {
|
2011-11-20 14:45:42 +00:00
|
|
|
free(l);
|
2017-08-26 16:11:21 +00:00
|
|
|
return (NULL);
|
2011-11-20 14:45:42 +00:00
|
|
|
}
|
2017-08-26 16:11:21 +00:00
|
|
|
return (l);
|
2011-11-20 14:45:42 +00:00
|
|
|
}
|