1 /* 2 * Copyright (c) 2002-2004 Tim J. Robbins 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * Copyright 2010 Nexenta Systems, Inc. All rights reserved. 29 * Use is subject to license terms. 30 */ 31 32 /* 33 * PRC National Standard GB 18030-2000 encoding of Chinese text. 34 * 35 * See gb18030(5) for details. 36 */ 37 38 #include "lint.h" 39 #include <sys/types.h> 40 #include <errno.h> 41 #include "runetype.h" 42 #include <stdlib.h> 43 #include <string.h> 44 #include <wchar.h> 45 #include "mblocal.h" 46 47 48 static size_t _GB18030_mbrtowc(wchar_t *_RESTRICT_KYWD, 49 const char *_RESTRICT_KYWD, 50 size_t, mbstate_t *_RESTRICT_KYWD); 51 static int _GB18030_mbsinit(const mbstate_t *); 52 static size_t _GB18030_wcrtomb(char *_RESTRICT_KYWD, wchar_t, 53 mbstate_t *_RESTRICT_KYWD); 54 55 typedef struct { 56 int count; 57 uchar_t bytes[4]; 58 } _GB18030State; 59 60 int 61 _GB18030_init(_RuneLocale *rl) 62 { 63 64 __mbrtowc = _GB18030_mbrtowc; 65 __wcrtomb = _GB18030_wcrtomb; 66 __mbsinit = _GB18030_mbsinit; 67 _CurrentRuneLocale = rl; 68 __ctype[520] = 4; 69 charset_is_ascii = 0; 70 71 return (0); 72 } 73 74 static int 75 _GB18030_mbsinit(const mbstate_t *ps) 76 { 77 78 return (ps == NULL || ((const _GB18030State *)ps)->count == 0); 79 } 80 81 static size_t 82 _GB18030_mbrtowc(wchar_t *_RESTRICT_KYWD pwc, const char *_RESTRICT_KYWD s, 83 size_t n, mbstate_t *_RESTRICT_KYWD ps) 84 { 85 _GB18030State *gs; 86 wchar_t wch; 87 int ch, len, ocount; 88 size_t ncopy; 89 90 gs = (_GB18030State *)ps; 91 92 if (gs->count < 0 || gs->count > sizeof (gs->bytes)) { 93 errno = EINVAL; 94 return ((size_t)-1); 95 } 96 97 if (s == NULL) { 98 s = ""; 99 n = 1; 100 pwc = NULL; 101 } 102 103 ncopy = MIN(MIN(n, MB_CUR_MAX), sizeof (gs->bytes) - gs->count); 104 (void) memcpy(gs->bytes + gs->count, s, ncopy); 105 ocount = gs->count; 106 gs->count += ncopy; 107 s = (char *)gs->bytes; 108 n = gs->count; 109 110 if (n == 0) 111 /* Incomplete multibyte sequence */ 112 return ((size_t)-2); 113 114 /* 115 * Single byte: [00-7f] 116 * Two byte: [81-fe][40-7e,80-fe] 117 * Four byte: [81-fe][30-39][81-fe][30-39] 118 */ 119 ch = (unsigned char)*s++; 120 if (ch <= 0x7f) { 121 len = 1; 122 wch = ch; 123 } else if (ch >= 0x81 && ch <= 0xfe) { 124 wch = ch; 125 if (n < 2) 126 return ((size_t)-2); 127 ch = (unsigned char)*s++; 128 if ((ch >= 0x40 && ch <= 0x7e) || (ch >= 0x80 && ch <= 0xfe)) { 129 wch = (wch << 8) | ch; 130 len = 2; 131 } else if (ch >= 0x30 && ch <= 0x39) { 132 /* 133 * Strip high bit off the wide character we will 134 * eventually output so that it is positive when 135 * cast to wint_t on 32-bit twos-complement machines. 136 */ 137 wch = ((wch & 0x7f) << 8) | ch; 138 if (n < 3) 139 return ((size_t)-2); 140 ch = (unsigned char)*s++; 141 if (ch < 0x81 || ch > 0xfe) 142 goto ilseq; 143 wch = (wch << 8) | ch; 144 if (n < 4) 145 return ((size_t)-2); 146 ch = (unsigned char)*s++; 147 if (ch < 0x30 || ch > 0x39) 148 goto ilseq; 149 wch = (wch << 8) | ch; 150 len = 4; 151 } else 152 goto ilseq; 153 } else 154 goto ilseq; 155 156 if (pwc != NULL) 157 *pwc = wch; 158 gs->count = 0; 159 return (wch == L'\0' ? 0 : len - ocount); 160 ilseq: 161 errno = EILSEQ; 162 return ((size_t)-1); 163 } 164 165 static size_t 166 _GB18030_wcrtomb(char *_RESTRICT_KYWD s, wchar_t wc, 167 mbstate_t *_RESTRICT_KYWD ps) 168 { 169 _GB18030State *gs; 170 size_t len; 171 int c; 172 173 gs = (_GB18030State *)ps; 174 175 if (gs->count != 0) { 176 errno = EINVAL; 177 return ((size_t)-1); 178 } 179 180 if (s == NULL) 181 /* Reset to initial shift state (no-op) */ 182 return (1); 183 if ((wc & ~0x7fffffff) != 0) 184 goto ilseq; 185 if (wc & 0x7f000000) { 186 /* Replace high bit that mbrtowc() removed. */ 187 wc |= 0x80000000; 188 c = (wc >> 24) & 0xff; 189 if (c < 0x81 || c > 0xfe) 190 goto ilseq; 191 *s++ = c; 192 c = (wc >> 16) & 0xff; 193 if (c < 0x30 || c > 0x39) 194 goto ilseq; 195 *s++ = c; 196 c = (wc >> 8) & 0xff; 197 if (c < 0x81 || c > 0xfe) 198 goto ilseq; 199 *s++ = c; 200 c = wc & 0xff; 201 if (c < 0x30 || c > 0x39) 202 goto ilseq; 203 *s++ = c; 204 len = 4; 205 } else if (wc & 0x00ff0000) 206 goto ilseq; 207 else if (wc & 0x0000ff00) { 208 c = (wc >> 8) & 0xff; 209 if (c < 0x81 || c > 0xfe) 210 goto ilseq; 211 *s++ = c; 212 c = wc & 0xff; 213 if (c < 0x40 || c == 0x7f || c == 0xff) 214 goto ilseq; 215 *s++ = c; 216 len = 2; 217 } else if (wc <= 0x7f) { 218 *s++ = wc; 219 len = 1; 220 } else 221 goto ilseq; 222 223 return (len); 224 ilseq: 225 errno = EILSEQ; 226 return ((size_t)-1); 227 } 228