xref: /dflybsd-src/lib/libc/locale/gbk.c (revision cb40c8cc2df81ae2ad923617b2becc49e89c11de)
14776d4e8SJohn Marino /*
24776d4e8SJohn Marino  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
34776d4e8SJohn Marino  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
40d5acd74SJohn Marino  * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
50d5acd74SJohn Marino  * Copyright (c) 1993
60d5acd74SJohn Marino  *	The Regents of the University of California.  All rights reserved.
70d5acd74SJohn Marino  *
80d5acd74SJohn Marino  * This code is derived from software contributed to Berkeley by
90d5acd74SJohn Marino  * Paul Borman at Krystal Technologies.
100d5acd74SJohn Marino  *
110d5acd74SJohn Marino  * Copyright (c) 2011 The FreeBSD Foundation
120d5acd74SJohn Marino  * All rights reserved.
130d5acd74SJohn Marino  * Portions of this software were developed by David Chisnall
140d5acd74SJohn Marino  * under sponsorship from the FreeBSD Foundation.
150d5acd74SJohn Marino  *
160d5acd74SJohn Marino  * Redistribution and use in source and binary forms, with or without
170d5acd74SJohn Marino  * modification, are permitted provided that the following conditions
180d5acd74SJohn Marino  * are met:
190d5acd74SJohn Marino  * 1. Redistributions of source code must retain the above copyright
200d5acd74SJohn Marino  *    notice, this list of conditions and the following disclaimer.
210d5acd74SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
220d5acd74SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
230d5acd74SJohn Marino  *    documentation and/or other materials provided with the distribution.
24*c66c7e2fSzrj  * 3. Neither the name of the University nor the names of its contributors
250d5acd74SJohn Marino  *    may be used to endorse or promote products derived from this software
260d5acd74SJohn Marino  *    without specific prior written permission.
270d5acd74SJohn Marino  *
280d5acd74SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
290d5acd74SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
300d5acd74SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
310d5acd74SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
320d5acd74SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
330d5acd74SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
340d5acd74SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
350d5acd74SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
360d5acd74SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
370d5acd74SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
380d5acd74SJohn Marino  * SUCH DAMAGE.
390d5acd74SJohn Marino  */
400d5acd74SJohn Marino 
410d5acd74SJohn Marino #include <sys/types.h>
420d5acd74SJohn Marino #include <errno.h>
430d5acd74SJohn Marino #include <runetype.h>
440d5acd74SJohn Marino #include <stdlib.h>
450d5acd74SJohn Marino #include <string.h>
460d5acd74SJohn Marino #include <wchar.h>
470d5acd74SJohn Marino #include "mblocal.h"
480d5acd74SJohn Marino 
490d5acd74SJohn Marino static size_t	_GBK_mbrtowc(wchar_t * __restrict, const char * __restrict,
500d5acd74SJohn Marino 		    size_t, mbstate_t * __restrict);
510d5acd74SJohn Marino static int	_GBK_mbsinit(const mbstate_t *);
520d5acd74SJohn Marino static size_t	_GBK_wcrtomb(char * __restrict, wchar_t,
530d5acd74SJohn Marino 		    mbstate_t * __restrict);
544776d4e8SJohn Marino static size_t	_GBK_mbsnrtowcs(wchar_t * __restrict,
554776d4e8SJohn Marino 		    const char ** __restrict, size_t, size_t,
564776d4e8SJohn Marino 		    mbstate_t * __restrict);
574776d4e8SJohn Marino static size_t	_GBK_wcsnrtombs(char * __restrict,
584776d4e8SJohn Marino 		    const wchar_t ** __restrict, size_t, size_t,
594776d4e8SJohn Marino 		    mbstate_t * __restrict);
600d5acd74SJohn Marino 
610d5acd74SJohn Marino typedef struct {
620d5acd74SJohn Marino 	wchar_t	ch;
630d5acd74SJohn Marino } _GBKState;
640d5acd74SJohn Marino 
650d5acd74SJohn Marino int
_GBK_init(struct xlocale_ctype * l,_RuneLocale * rl)660d5acd74SJohn Marino _GBK_init(struct xlocale_ctype *l, _RuneLocale *rl)
670d5acd74SJohn Marino {
680d5acd74SJohn Marino 
690d5acd74SJohn Marino 	l->__mbrtowc = _GBK_mbrtowc;
700d5acd74SJohn Marino 	l->__wcrtomb = _GBK_wcrtomb;
710d5acd74SJohn Marino 	l->__mbsinit = _GBK_mbsinit;
724776d4e8SJohn Marino 	l->__mbsnrtowcs = _GBK_mbsnrtowcs;
734776d4e8SJohn Marino 	l->__wcsnrtombs = _GBK_wcsnrtombs;
740d5acd74SJohn Marino 	l->runes = rl;
750d5acd74SJohn Marino 	l->__mb_cur_max = 2;
760d5acd74SJohn Marino 	l->__mb_sb_limit = 128;
770d5acd74SJohn Marino 	return (0);
780d5acd74SJohn Marino }
790d5acd74SJohn Marino 
800d5acd74SJohn Marino static int
_GBK_mbsinit(const mbstate_t * ps)810d5acd74SJohn Marino _GBK_mbsinit(const mbstate_t *ps)
820d5acd74SJohn Marino {
830d5acd74SJohn Marino 
840d5acd74SJohn Marino 	return (ps == NULL || ((const _GBKState *)ps)->ch == 0);
850d5acd74SJohn Marino }
860d5acd74SJohn Marino 
874776d4e8SJohn Marino static int
_gbk_check(u_int c)880d5acd74SJohn Marino _gbk_check(u_int c)
890d5acd74SJohn Marino {
900d5acd74SJohn Marino 
910d5acd74SJohn Marino 	c &= 0xff;
920d5acd74SJohn Marino 	return ((c >= 0x81 && c <= 0xfe) ? 2 : 1);
930d5acd74SJohn Marino }
940d5acd74SJohn Marino 
950d5acd74SJohn Marino static size_t
_GBK_mbrtowc(wchar_t * __restrict pwc,const char * __restrict s,size_t n,mbstate_t * __restrict ps)960d5acd74SJohn Marino _GBK_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
970d5acd74SJohn Marino     mbstate_t * __restrict ps)
980d5acd74SJohn Marino {
990d5acd74SJohn Marino 	_GBKState *gs;
1000d5acd74SJohn Marino 	wchar_t wc;
1010d5acd74SJohn Marino 	size_t len;
1020d5acd74SJohn Marino 
1030d5acd74SJohn Marino 	gs = (_GBKState *)ps;
1040d5acd74SJohn Marino 
1050d5acd74SJohn Marino 	if ((gs->ch & ~0xFF) != 0) {
1060d5acd74SJohn Marino 		/* Bad conversion state. */
1070d5acd74SJohn Marino 		errno = EINVAL;
1080d5acd74SJohn Marino 		return ((size_t)-1);
1090d5acd74SJohn Marino 	}
1100d5acd74SJohn Marino 
1110d5acd74SJohn Marino 	if (s == NULL) {
1120d5acd74SJohn Marino 		s = "";
1130d5acd74SJohn Marino 		n = 1;
1140d5acd74SJohn Marino 		pwc = NULL;
1150d5acd74SJohn Marino 	}
1160d5acd74SJohn Marino 
1170d5acd74SJohn Marino 	if (n == 0)
1180d5acd74SJohn Marino 		/* Incomplete multibyte sequence */
1190d5acd74SJohn Marino 		return ((size_t)-2);
1200d5acd74SJohn Marino 
1210d5acd74SJohn Marino 	if (gs->ch != 0) {
1220d5acd74SJohn Marino 		if (*s == '\0') {
1230d5acd74SJohn Marino 			errno = EILSEQ;
1240d5acd74SJohn Marino 			return ((size_t)-1);
1250d5acd74SJohn Marino 		}
1260d5acd74SJohn Marino 		wc = (gs->ch << 8) | (*s & 0xFF);
1270d5acd74SJohn Marino 		if (pwc != NULL)
1280d5acd74SJohn Marino 			*pwc = wc;
1290d5acd74SJohn Marino 		gs->ch = 0;
1300d5acd74SJohn Marino 		return (1);
1310d5acd74SJohn Marino 	}
1320d5acd74SJohn Marino 
1330d5acd74SJohn Marino 	len = (size_t)_gbk_check(*s);
1340d5acd74SJohn Marino 	wc = *s++ & 0xff;
1350d5acd74SJohn Marino 	if (len == 2) {
1360d5acd74SJohn Marino 		if (n < 2) {
1370d5acd74SJohn Marino 			/* Incomplete multibyte sequence */
1380d5acd74SJohn Marino 			gs->ch = wc;
1390d5acd74SJohn Marino 			return ((size_t)-2);
1400d5acd74SJohn Marino 		}
1410d5acd74SJohn Marino 		if (*s == '\0') {
1420d5acd74SJohn Marino 			errno = EILSEQ;
1430d5acd74SJohn Marino 			return ((size_t)-1);
1440d5acd74SJohn Marino 		}
1450d5acd74SJohn Marino 		wc = (wc << 8) | (*s++ & 0xff);
1460d5acd74SJohn Marino 		if (pwc != NULL)
1470d5acd74SJohn Marino 			*pwc = wc;
1480d5acd74SJohn Marino 		return (2);
1490d5acd74SJohn Marino 	} else {
1500d5acd74SJohn Marino 		if (pwc != NULL)
1510d5acd74SJohn Marino 			*pwc = wc;
1520d5acd74SJohn Marino 		return (wc == L'\0' ? 0 : 1);
1530d5acd74SJohn Marino 	}
1540d5acd74SJohn Marino }
1550d5acd74SJohn Marino 
1560d5acd74SJohn Marino static size_t
_GBK_wcrtomb(char * __restrict s,wchar_t wc,mbstate_t * __restrict ps)1570d5acd74SJohn Marino _GBK_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
1580d5acd74SJohn Marino {
1590d5acd74SJohn Marino 	_GBKState *gs;
1600d5acd74SJohn Marino 
1610d5acd74SJohn Marino 	gs = (_GBKState *)ps;
1620d5acd74SJohn Marino 
1630d5acd74SJohn Marino 	if (gs->ch != 0) {
1640d5acd74SJohn Marino 		errno = EINVAL;
1650d5acd74SJohn Marino 		return ((size_t)-1);
1660d5acd74SJohn Marino 	}
1670d5acd74SJohn Marino 
1680d5acd74SJohn Marino 	if (s == NULL)
1690d5acd74SJohn Marino 		/* Reset to initial shift state (no-op) */
1700d5acd74SJohn Marino 		return (1);
1710d5acd74SJohn Marino 	if (wc & 0x8000) {
1720d5acd74SJohn Marino 		*s++ = (wc >> 8) & 0xff;
1730d5acd74SJohn Marino 		*s = wc & 0xff;
1740d5acd74SJohn Marino 		return (2);
1750d5acd74SJohn Marino 	}
1760d5acd74SJohn Marino 	*s = wc & 0xff;
1770d5acd74SJohn Marino 	return (1);
1780d5acd74SJohn Marino }
1794776d4e8SJohn Marino 
1804776d4e8SJohn Marino static size_t
_GBK_mbsnrtowcs(wchar_t * __restrict dst,const char ** __restrict src,size_t nms,size_t len,mbstate_t * __restrict ps)1814776d4e8SJohn Marino _GBK_mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
1824776d4e8SJohn Marino     size_t nms, size_t len, mbstate_t * __restrict ps)
1834776d4e8SJohn Marino {
1844776d4e8SJohn Marino 	return (__mbsnrtowcs_std(dst, src, nms, len, ps, _GBK_mbrtowc));
1854776d4e8SJohn Marino }
1864776d4e8SJohn Marino 
1874776d4e8SJohn Marino static size_t
_GBK_wcsnrtombs(char * __restrict dst,const wchar_t ** __restrict src,size_t nwc,size_t len,mbstate_t * __restrict ps)1884776d4e8SJohn Marino _GBK_wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src,
1894776d4e8SJohn Marino     size_t nwc, size_t len, mbstate_t * __restrict ps)
1904776d4e8SJohn Marino {
1914776d4e8SJohn Marino 	return (__wcsnrtombs_std(dst, src, nwc, len, ps, _GBK_wcrtomb));
1924776d4e8SJohn Marino }
193