1 /*
   2  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
   3  * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
   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  * Redistribution and use in source and binary forms, with or without
  11  * modification, are permitted provided that the following conditions
  12  * are met:
  13  * 1. Redistributions of source code must retain the above copyright
  14  *    notice, this list of conditions and the following disclaimer.
  15  * 2. Redistributions in binary form must reproduce the above copyright
  16  *    notice, this list of conditions and the following disclaimer in the
  17  *    documentation and/or other materials provided with the distribution.
  18  * 4. Neither the name of the University nor the names of its contributors
  19  *    may be used to endorse or promote products derived from this software
  20  *    without specific prior written permission.
  21  *
  22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32  * SUCH DAMAGE.
  33  */
  34 
  35 #include "lint.h"
  36 #include <sys/types.h>
  37 #include <errno.h>
  38 #include "runetype.h"
  39 #include <stdlib.h>
  40 #include <string.h>
  41 #include <wchar.h>
  42 #include "mblocal.h"
  43 
  44 static size_t   _BIG5_mbrtowc(wchar_t *_RESTRICT_KYWD,
  45                     const char *_RESTRICT_KYWD,
  46                     size_t, mbstate_t *RESTRICT_KYWD);
  47 static int      _BIG5_mbsinit(const mbstate_t *);
  48 static size_t   _BIG5_wcrtomb(char *_RESTRICT_KYWD, wchar_t,
  49                     mbstate_t *_RESTRICT_KYWD);
  50 
  51 typedef struct {
  52         wchar_t ch;
  53 } _BIG5State;
  54 
  55 int
  56 _BIG5_init(_RuneLocale *rl)
  57 {
  58 
  59         __mbrtowc = _BIG5_mbrtowc;
  60         __wcrtomb = _BIG5_wcrtomb;
  61         __mbsinit = _BIG5_mbsinit;
  62         _CurrentRuneLocale = rl;
  63         __ctype[520] = 2;
  64         charset_is_ascii = 0;
  65         return (0);
  66 }
  67 
  68 static int
  69 _BIG5_mbsinit(const mbstate_t *ps)
  70 {
  71 
  72         return (ps == NULL || ((const _BIG5State *)ps)->ch == 0);
  73 }
  74 
  75 static int
  76 _big5_check(uint_t c)
  77 {
  78 
  79         c &= 0xff;
  80         return ((c >= 0xa1 && c <= 0xfe) ? 2 : 1);
  81 }
  82 
  83 static size_t
  84 _BIG5_mbrtowc(wchar_t *_RESTRICT_KYWD pwc, const char *_RESTRICT_KYWD s,
  85     size_t n, mbstate_t *_RESTRICT_KYWD ps)
  86 {
  87         _BIG5State *bs;
  88         wchar_t wc;
  89         size_t len;
  90 
  91         bs = (_BIG5State *)ps;
  92 
  93         if ((bs->ch & ~0xFF) != 0) {
  94                 /* Bad conversion state. */
  95                 errno = EINVAL;
  96                 return ((size_t)-1);
  97         }
  98 
  99         if (s == NULL) {
 100                 s = "";
 101                 n = 1;
 102                 pwc = NULL;
 103         }
 104 
 105         if (n == 0)
 106                 /* Incomplete multibyte sequence */
 107                 return ((size_t)-2);
 108 
 109         if (bs->ch != 0) {
 110                 if (*s == '\0') {
 111                         errno = EILSEQ;
 112                         return ((size_t)-1);
 113                 }
 114                 wc = (bs->ch << 8) | (*s & 0xFF);
 115                 if (pwc != NULL)
 116                         *pwc = wc;
 117                 bs->ch = 0;
 118                 return (1);
 119         }
 120 
 121         len = (size_t)_big5_check(*s);
 122         wc = *s++ & 0xff;
 123         if (len == 2) {
 124                 if (n < 2) {
 125                         /* Incomplete multibyte sequence */
 126                         bs->ch = wc;
 127                         return ((size_t)-2);
 128                 }
 129                 if (*s == '\0') {
 130                         errno = EILSEQ;
 131                         return ((size_t)-1);
 132                 }
 133                 wc = (wc << 8) | (*s++ & 0xff);
 134                 if (pwc != NULL)
 135                         *pwc = wc;
 136                 return (2);
 137         } else {
 138                 if (pwc != NULL)
 139                         *pwc = wc;
 140                 return (wc == L'\0' ? 0 : 1);
 141         }
 142 }
 143 
 144 static size_t
 145 _BIG5_wcrtomb(char *_RESTRICT_KYWD s, wchar_t wc, mbstate_t *_RESTRICT_KYWD ps)
 146 {
 147         _BIG5State *bs;
 148 
 149         bs = (_BIG5State *)ps;
 150 
 151         if (bs->ch != 0) {
 152                 errno = EINVAL;
 153                 return ((size_t)-1);
 154         }
 155 
 156         if (s == NULL)
 157                 /* Reset to initial shift state (no-op) */
 158                 return (1);
 159         if (wc & 0x8000) {
 160                 *s++ = (wc >> 8) & 0xff;
 161                 *s = wc & 0xff;
 162                 return (2);
 163         }
 164         *s = wc & 0xff;
 165         return (1);
 166 }