xref: /freebsd/lib/libc/locale/gbk.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
19d793e98SAndrey A. Chernov /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
47b247341SBaptiste Daroussin  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
57b247341SBaptiste Daroussin  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
6ca2dae42STim J. Robbins  * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
79d793e98SAndrey A. Chernov  * Copyright (c) 1993
89d793e98SAndrey A. Chernov  *	The Regents of the University of California.  All rights reserved.
99d793e98SAndrey A. Chernov  *
109d793e98SAndrey A. Chernov  * This code is derived from software contributed to Berkeley by
119d793e98SAndrey A. Chernov  * Paul Borman at Krystal Technologies.
129d793e98SAndrey A. Chernov  *
133c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
14*5b5fa75aSEd Maste  *
153c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
163c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
173c87aa1dSDavid Chisnall  *
189d793e98SAndrey A. Chernov  * Redistribution and use in source and binary forms, with or without
199d793e98SAndrey A. Chernov  * modification, are permitted provided that the following conditions
209d793e98SAndrey A. Chernov  * are met:
219d793e98SAndrey A. Chernov  * 1. Redistributions of source code must retain the above copyright
229d793e98SAndrey A. Chernov  *    notice, this list of conditions and the following disclaimer.
239d793e98SAndrey A. Chernov  * 2. Redistributions in binary form must reproduce the above copyright
249d793e98SAndrey A. Chernov  *    notice, this list of conditions and the following disclaimer in the
259d793e98SAndrey A. Chernov  *    documentation and/or other materials provided with the distribution.
26fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
279d793e98SAndrey A. Chernov  *    may be used to endorse or promote products derived from this software
289d793e98SAndrey A. Chernov  *    without specific prior written permission.
299d793e98SAndrey A. Chernov  *
309d793e98SAndrey A. Chernov  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
319d793e98SAndrey A. Chernov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
329d793e98SAndrey A. Chernov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
339d793e98SAndrey A. Chernov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
349d793e98SAndrey A. Chernov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
359d793e98SAndrey A. Chernov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
369d793e98SAndrey A. Chernov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
379d793e98SAndrey A. Chernov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
389d793e98SAndrey A. Chernov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
399d793e98SAndrey A. Chernov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
409d793e98SAndrey A. Chernov  * SUCH DAMAGE.
419d793e98SAndrey A. Chernov  */
429d793e98SAndrey A. Chernov 
43e94c6cb4SAlexey Zelkin #include <sys/types.h>
44fc813796STim J. Robbins #include <errno.h>
4590c7d99fSTim J. Robbins #include <runetype.h>
469d793e98SAndrey A. Chernov #include <stdlib.h>
47ca2dae42STim J. Robbins #include <string.h>
4890c7d99fSTim J. Robbins #include <wchar.h>
492051a8f2STim J. Robbins #include "mblocal.h"
5090c7d99fSTim J. Robbins 
51367ed4e1SAndrey A. Chernov extern int __mb_sb_limit;
52367ed4e1SAndrey A. Chernov 
53e94c6cb4SAlexey Zelkin static size_t	_GBK_mbrtowc(wchar_t * __restrict, const char * __restrict,
54e94c6cb4SAlexey Zelkin 		    size_t, mbstate_t * __restrict);
55e94c6cb4SAlexey Zelkin static int	_GBK_mbsinit(const mbstate_t *);
56e94c6cb4SAlexey Zelkin static size_t	_GBK_wcrtomb(char * __restrict, wchar_t,
5790c7d99fSTim J. Robbins 		    mbstate_t * __restrict);
587b247341SBaptiste Daroussin static size_t	_GBK_mbsnrtowcs(wchar_t * __restrict,
597b247341SBaptiste Daroussin 		    const char ** __restrict, size_t, size_t,
607b247341SBaptiste Daroussin 		    mbstate_t * __restrict);
617b247341SBaptiste Daroussin static size_t	_GBK_wcsnrtombs(char * __restrict,
627b247341SBaptiste Daroussin 		    const wchar_t ** __restrict, size_t, size_t,
637b247341SBaptiste Daroussin 		    mbstate_t * __restrict);
649d793e98SAndrey A. Chernov 
65ca2dae42STim J. Robbins typedef struct {
6661074767STim J. Robbins 	wchar_t	ch;
67ca2dae42STim J. Robbins } _GBKState;
68ca2dae42STim J. Robbins 
699d793e98SAndrey A. Chernov int
_GBK_init(struct xlocale_ctype * l,_RuneLocale * rl)703c87aa1dSDavid Chisnall _GBK_init(struct xlocale_ctype *l, _RuneLocale *rl)
719d793e98SAndrey A. Chernov {
7290c7d99fSTim J. Robbins 
733c87aa1dSDavid Chisnall 	l->__mbrtowc = _GBK_mbrtowc;
743c87aa1dSDavid Chisnall 	l->__wcrtomb = _GBK_wcrtomb;
753c87aa1dSDavid Chisnall 	l->__mbsinit = _GBK_mbsinit;
767b247341SBaptiste Daroussin 	l->__mbsnrtowcs = _GBK_mbsnrtowcs;
777b247341SBaptiste Daroussin 	l->__wcsnrtombs = _GBK_wcsnrtombs;
783c87aa1dSDavid Chisnall 	l->runes = rl;
793c87aa1dSDavid Chisnall 	l->__mb_cur_max = 2;
803c87aa1dSDavid Chisnall 	l->__mb_sb_limit = 128;
819d793e98SAndrey A. Chernov 	return (0);
829d793e98SAndrey A. Chernov }
839d793e98SAndrey A. Chernov 
84e94c6cb4SAlexey Zelkin static int
_GBK_mbsinit(const mbstate_t * ps)85ca2dae42STim J. Robbins _GBK_mbsinit(const mbstate_t *ps)
86ca2dae42STim J. Robbins {
87ca2dae42STim J. Robbins 
8861074767STim J. Robbins 	return (ps == NULL || ((const _GBKState *)ps)->ch == 0);
89ca2dae42STim J. Robbins }
90ca2dae42STim J. Robbins 
917b247341SBaptiste Daroussin static int
_gbk_check(u_int c)9290c7d99fSTim J. Robbins _gbk_check(u_int c)
939d793e98SAndrey A. Chernov {
9490c7d99fSTim J. Robbins 
959d793e98SAndrey A. Chernov 	c &= 0xff;
966abda1f0SAndrey A. Chernov 	return ((c >= 0x81 && c <= 0xfe) ? 2 : 1);
979d793e98SAndrey A. Chernov }
989d793e98SAndrey A. Chernov 
99e94c6cb4SAlexey Zelkin static size_t
_GBK_mbrtowc(wchar_t * __restrict pwc,const char * __restrict s,size_t n,mbstate_t * __restrict ps)10090c7d99fSTim J. Robbins _GBK_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
101ca2dae42STim J. Robbins     mbstate_t * __restrict ps)
1029d793e98SAndrey A. Chernov {
103ca2dae42STim J. Robbins 	_GBKState *gs;
10490c7d99fSTim J. Robbins 	wchar_t wc;
10561074767STim J. Robbins 	size_t len;
1069d793e98SAndrey A. Chernov 
107ca2dae42STim J. Robbins 	gs = (_GBKState *)ps;
108ca2dae42STim J. Robbins 
10961074767STim J. Robbins 	if ((gs->ch & ~0xFF) != 0) {
11061074767STim J. Robbins 		/* Bad conversion state. */
111fc813796STim J. Robbins 		errno = EINVAL;
112fc813796STim J. Robbins 		return ((size_t)-1);
113fc813796STim J. Robbins 	}
114fc813796STim J. Robbins 
115ca2dae42STim J. Robbins 	if (s == NULL) {
116ca2dae42STim J. Robbins 		s = "";
117ca2dae42STim J. Robbins 		n = 1;
118ca2dae42STim J. Robbins 		pwc = NULL;
119ca2dae42STim J. Robbins 	}
120ca2dae42STim J. Robbins 
12161074767STim J. Robbins 	if (n == 0)
12290c7d99fSTim J. Robbins 		/* Incomplete multibyte sequence */
12390c7d99fSTim J. Robbins 		return ((size_t)-2);
12461074767STim J. Robbins 
12561074767STim J. Robbins 	if (gs->ch != 0) {
12661074767STim J. Robbins 		if (*s == '\0') {
12788af941aSTim J. Robbins 			errno = EILSEQ;
12888af941aSTim J. Robbins 			return ((size_t)-1);
12988af941aSTim J. Robbins 		}
13061074767STim J. Robbins 		wc = (gs->ch << 8) | (*s & 0xFF);
13190c7d99fSTim J. Robbins 		if (pwc != NULL)
13290c7d99fSTim J. Robbins 			*pwc = wc;
13361074767STim J. Robbins 		gs->ch = 0;
13461074767STim J. Robbins 		return (1);
13561074767STim J. Robbins 	}
13661074767STim J. Robbins 
13761074767STim J. Robbins 	len = (size_t)_gbk_check(*s);
13861074767STim J. Robbins 	wc = *s++ & 0xff;
13961074767STim J. Robbins 	if (len == 2) {
14061074767STim J. Robbins 		if (n < 2) {
14161074767STim J. Robbins 			/* Incomplete multibyte sequence */
14261074767STim J. Robbins 			gs->ch = wc;
14361074767STim J. Robbins 			return ((size_t)-2);
14461074767STim J. Robbins 		}
14561074767STim J. Robbins 		if (*s == '\0') {
14661074767STim J. Robbins 			errno = EILSEQ;
14761074767STim J. Robbins 			return ((size_t)-1);
14861074767STim J. Robbins 		}
14961074767STim J. Robbins 		wc = (wc << 8) | (*s++ & 0xff);
15061074767STim J. Robbins 		if (pwc != NULL)
15161074767STim J. Robbins 			*pwc = wc;
15261074767STim J. Robbins 		return (2);
15361074767STim J. Robbins 	} else {
15461074767STim J. Robbins 		if (pwc != NULL)
15561074767STim J. Robbins 			*pwc = wc;
15661074767STim J. Robbins 		return (wc == L'\0' ? 0 : 1);
15761074767STim J. Robbins 	}
1589d793e98SAndrey A. Chernov }
1599d793e98SAndrey A. Chernov 
160e94c6cb4SAlexey Zelkin static size_t
_GBK_wcrtomb(char * __restrict s,wchar_t wc,mbstate_t * __restrict ps)161fc813796STim J. Robbins _GBK_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
1629d793e98SAndrey A. Chernov {
163fc813796STim J. Robbins 	_GBKState *gs;
164fc813796STim J. Robbins 
165fc813796STim J. Robbins 	gs = (_GBKState *)ps;
166fc813796STim J. Robbins 
16761074767STim J. Robbins 	if (gs->ch != 0) {
168fc813796STim J. Robbins 		errno = EINVAL;
169fc813796STim J. Robbins 		return ((size_t)-1);
170fc813796STim J. Robbins 	}
17190c7d99fSTim J. Robbins 
17290c7d99fSTim J. Robbins 	if (s == NULL)
17390c7d99fSTim J. Robbins 		/* Reset to initial shift state (no-op) */
17490c7d99fSTim J. Robbins 		return (1);
17590c7d99fSTim J. Robbins 	if (wc & 0x8000) {
17690c7d99fSTim J. Robbins 		*s++ = (wc >> 8) & 0xff;
17790c7d99fSTim J. Robbins 		*s = wc & 0xff;
1789d793e98SAndrey A. Chernov 		return (2);
1799d793e98SAndrey A. Chernov 	}
18090c7d99fSTim J. Robbins 	*s = wc & 0xff;
1819d793e98SAndrey A. Chernov 	return (1);
1829d793e98SAndrey A. Chernov }
1837b247341SBaptiste Daroussin 
1847b247341SBaptiste Daroussin static size_t
_GBK_mbsnrtowcs(wchar_t * __restrict dst,const char ** __restrict src,size_t nms,size_t len,mbstate_t * __restrict ps)1857b247341SBaptiste Daroussin _GBK_mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
1867b247341SBaptiste Daroussin     size_t nms, size_t len, mbstate_t * __restrict ps)
1877b247341SBaptiste Daroussin {
1887b247341SBaptiste Daroussin 	return (__mbsnrtowcs_std(dst, src, nms, len, ps, _GBK_mbrtowc));
1897b247341SBaptiste Daroussin }
1907b247341SBaptiste Daroussin 
1917b247341SBaptiste Daroussin static size_t
_GBK_wcsnrtombs(char * __restrict dst,const wchar_t ** __restrict src,size_t nwc,size_t len,mbstate_t * __restrict ps)1927b247341SBaptiste Daroussin _GBK_wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src,
1937b247341SBaptiste Daroussin     size_t nwc, size_t len, mbstate_t * __restrict ps)
1947b247341SBaptiste Daroussin {
1957b247341SBaptiste Daroussin 	return (__wcsnrtombs_std(dst, src, nwc, len, ps, _GBK_wcrtomb));
1967b247341SBaptiste Daroussin }
197