2N/A/*
2N/A * Copyright 2013 Garrett D'Amore <garrett@damore.org>
2N/A * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
2N/A * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
2N/A * Copyright (c) 1993
2N/A * The Regents of the University of California. All rights reserved.
2N/A *
2N/A * This code is derived from software contributed to Berkeley by
2N/A * Paul Borman at Krystal Technologies.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted provided that the following conditions
2N/A * are met:
2N/A * 1. Redistributions of source code must retain the above copyright
2N/A * notice, this list of conditions and the following disclaimer.
2N/A * 2. Redistributions in binary form must reproduce the above copyright
2N/A * notice, this list of conditions and the following disclaimer in the
2N/A * documentation and/or other materials provided with the distribution.
2N/A * 4. Neither the name of the University nor the names of its contributors
2N/A * may be used to endorse or promote products derived from this software
2N/A * without specific prior written permission.
2N/A *
2N/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2N/A * SUCH DAMAGE.
2N/A */
2N/A
2N/A#include "lint.h"
2N/A#include <sys/types.h>
2N/A#include <errno.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <wchar.h>
2N/A#include "mblocal.h"
2N/A#include "lctype.h"
2N/A
2N/Astatic size_t _GBK_mbrtowc(wchar_t *_RESTRICT_KYWD,
2N/A const char *_RESTRICT_KYWD,
2N/A size_t, mbstate_t *_RESTRICT_KYWD);
2N/Astatic int _GBK_mbsinit(const mbstate_t *);
2N/Astatic size_t _GBK_wcrtomb(char *_RESTRICT_KYWD, wchar_t,
2N/A mbstate_t *_RESTRICT_KYWD);
2N/Astatic size_t _GBK_mbsnrtowcs(wchar_t *_RESTRICT_KYWD,
2N/A const char **_RESTRICT_KYWD, size_t, size_t,
2N/A mbstate_t *_RESTRICT_KYWD);
2N/Astatic size_t _GBK_wcsnrtombs(char *_RESTRICT_KYWD,
2N/A const wchar_t **_RESTRICT_KYWD, size_t, size_t,
2N/A mbstate_t *_RESTRICT_KYWD);
2N/A
2N/Atypedef struct {
2N/A wchar_t ch;
2N/A} _GBKState;
2N/A
2N/Avoid
2N/A_GBK_init(struct lc_ctype *lct)
2N/A{
2N/A lct->lc_mbrtowc = _GBK_mbrtowc;
2N/A lct->lc_wcrtomb = _GBK_wcrtomb;
2N/A lct->lc_mbsinit = _GBK_mbsinit;
2N/A lct->lc_mbsnrtowcs = _GBK_mbsnrtowcs;
2N/A lct->lc_wcsnrtombs = _GBK_wcsnrtombs;
2N/A lct->lc_max_mblen = 2;
2N/A lct->lc_is_ascii = 0;
2N/A}
2N/A
2N/Astatic int
2N/A_GBK_mbsinit(const mbstate_t *ps)
2N/A{
2N/A
2N/A return (ps == NULL || ((const _GBKState *)ps)->ch == 0);
2N/A}
2N/A
2N/Astatic int
2N/A_gbk_check(uint_t c)
2N/A{
2N/A
2N/A c &= 0xff;
2N/A return ((c >= 0x81 && c <= 0xfe) ? 2 : 1);
2N/A}
2N/A
2N/Astatic size_t
2N/A_GBK_mbrtowc(wchar_t *_RESTRICT_KYWD pwc, const char *_RESTRICT_KYWD s,
2N/A size_t n, mbstate_t *_RESTRICT_KYWD ps)
2N/A{
2N/A _GBKState *gs;
2N/A wchar_t wc;
2N/A size_t len;
2N/A
2N/A gs = (_GBKState *)ps;
2N/A
2N/A if ((gs->ch & ~0xFF) != 0) {
2N/A /* Bad conversion state. */
2N/A errno = EINVAL;
2N/A return ((size_t)-1);
2N/A }
2N/A
2N/A if (s == NULL) {
2N/A s = "";
2N/A n = 1;
2N/A pwc = NULL;
2N/A }
2N/A
2N/A if (n == 0)
2N/A /* Incomplete multibyte sequence */
2N/A return ((size_t)-2);
2N/A
2N/A if (gs->ch != 0) {
2N/A if (*s == '\0') {
2N/A errno = EILSEQ;
2N/A return ((size_t)-1);
2N/A }
2N/A wc = (gs->ch << 8) | (*s & 0xFF);
2N/A if (pwc != NULL)
2N/A *pwc = wc;
2N/A gs->ch = 0;
2N/A return (1);
2N/A }
2N/A
2N/A len = (size_t)_gbk_check(*s);
2N/A wc = *s++ & 0xff;
2N/A if (len == 2) {
2N/A if (n < 2) {
2N/A /* Incomplete multibyte sequence */
2N/A gs->ch = wc;
2N/A return ((size_t)-2);
2N/A }
2N/A if (*s == '\0') {
2N/A errno = EILSEQ;
2N/A return ((size_t)-1);
2N/A }
2N/A wc = (wc << 8) | (*s++ & 0xff);
2N/A if (pwc != NULL)
2N/A *pwc = wc;
2N/A return (2);
2N/A } else {
2N/A if (pwc != NULL)
2N/A *pwc = wc;
2N/A return (wc == L'\0' ? 0 : 1);
2N/A }
2N/A}
2N/A
2N/Astatic size_t
2N/A_GBK_wcrtomb(char *_RESTRICT_KYWD s, wchar_t wc, mbstate_t *_RESTRICT_KYWD ps)
2N/A{
2N/A _GBKState *gs;
2N/A
2N/A gs = (_GBKState *)ps;
2N/A
2N/A if (gs->ch != 0) {
2N/A errno = EINVAL;
2N/A return ((size_t)-1);
2N/A }
2N/A
2N/A if (s == NULL)
2N/A /* Reset to initial shift state (no-op) */
2N/A return (1);
2N/A if (wc & 0x8000) {
2N/A *s++ = (wc >> 8) & 0xff;
2N/A *s = wc & 0xff;
2N/A return (2);
2N/A }
2N/A *s = wc & 0xff;
2N/A return (1);
2N/A}
2N/A
2N/Astatic size_t
2N/A_GBK_mbsnrtowcs(wchar_t *_RESTRICT_KYWD dst, const char **_RESTRICT_KYWD src,
2N/A size_t nms, size_t len, mbstate_t *_RESTRICT_KYWD ps)
2N/A{
2N/A return (__mbsnrtowcs_std(dst, src, nms, len, ps, _GBK_mbrtowc));
2N/A}
2N/A
2N/Astatic size_t
2N/A_GBK_wcsnrtombs(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
2N/A size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD ps)
2N/A{
2N/A return (__wcsnrtombs_std(dst, src, nwc, len, ps, _GBK_wcrtomb));
2N/A}
2N/A