341N/A/*
341N/A * Copyright The Open Group
341N/A * Permission to use, copy, modify, distribute, and sell this software and its
341N/A * documentation for any purpose is hereby granted without fee, provided that the
341N/A * above copyright notice appear in all copies and that both that copyright notice
341N/A * and this permission notice appear in supporting documentation.
341N/A
341N/A * The above copyright notice and this permission notice shall be included in all
341N/A * copies or substantial portions of the Software.
341N/A
341N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
341N/A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
341N/A * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE OPEN GROUP
341N/A * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
341N/A * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
341N/A * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
341N/A
341N/A * Except as contained in this notice, the name of The Open Group shall not be used
341N/A * in advertising or otherwise to promote the sale, use or other dealings in this
341N/A * Software without prior written authorization from The Open Group.
341N/A
341N/A * Portions also covered by other licenses as noted in the above URL.
341N/A */
341N/A
341N/A/*
341N/A * GB18030
341N/A */
341N/A
341N/A/*
341N/A * GB18030, as implemented in glibc-2.2, is an extension of GBK (= CP936).
341N/A * It adds the following ranges:
341N/A * 1. Two-byte range
341N/A * 0xA2E3, 0xA8BF, 0xA98A..0xA995, 0xFE50..0xFE9F
341N/A * 2. Four-byte range
341N/A * 0x{81..84}{30..39}{81..FE}{30..39}
341N/A * Most of Unicode plane 1 in Unicode order.
341N/A * Start: 0x81308130 = 0x0080
341N/A * End: 0x8431A439 = 0xFFFF
341N/A * 3. Four-byte range
341N/A * 0x{90..E3}{30..39}{81..FE}{30..39}
341N/A * Unicode plane 2..16 in Unicode order.
341N/A * Start: 0x90308130 = 0x010000
341N/A * End: 0xE3329A35 = 0x10FFFF
341N/A */
341N/A
341N/A#include "gb18030ext.h"
341N/A#include "gb18030uni.h"
341N/A
341N/A#define sun
341N/A
341N/Astatic int
341N/Agb18030_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
341N/A{
341N/A int ret;
341N/A
341N/A#ifdef sun
341N/A int v = (s[0] << 8 | s[1]);
341N/A unsigned char buf[4];
341N/A
341N/A buf[3] = 0x30 + v%10; v/=10;
341N/A buf[2] = 0x81 + v%126; v/=126;
341N/A buf[1] = 0x30 + v%10; v/=10;
341N/A buf[0] = 0x81 + v;
341N/A
341N/A /* Code set 2 (remainder of Unicode U+0000..U+FFFF) */
341N/A ret = gb18030uni_mbtowc(conv,pwc,buf,4);
341N/A if (ret != RET_ILSEQ)
341N/A ret = 2;
341N/A
341N/A return ret;
341N/A#else
341N/A /* Code set 0 (ASCII) */
341N/A if (*s < 0x80)
341N/A return ascii_mbtowc(conv,pwc,s,n);
341N/A
341N/A /* Code set 1 (GBK extended) */
341N/A ret = gbk_mbtowc(conv,pwc,s,n);
341N/A if (ret != RET_ILSEQ)
341N/A return ret;
341N/A
341N/A ret = gb18030ext_mbtowc(conv,pwc,s,n);
341N/A if (ret != RET_ILSEQ)
341N/A return ret;
341N/A
341N/A /* Code set 2 (remainder of Unicode U+0000..U+FFFF) */
341N/A ret = gb18030uni_mbtowc(conv,pwc,s,n);
341N/A if (ret != RET_ILSEQ)
341N/A return ret;
341N/A
341N/A /* Code set 3 (Unicode U+10000..U+10FFFF) */
341N/A {
341N/A unsigned char c1 = s[0];
341N/A if (c1 >= 0x90 && c1 <= 0xe3) {
341N/A if (n >= 2) {
341N/A unsigned char c2 = s[1];
341N/A if (c2 >= 0x30 && c2 <= 0x39) {
341N/A if (n >= 3) {
341N/A unsigned char c3 = s[2];
341N/A if (c3 >= 0x81 && c3 <= 0xfe) {
341N/A if (n >= 4) {
341N/A unsigned char c4 = s[3];
341N/A if (c4 >= 0x30 && c4 <= 0x39) {
341N/A unsigned int i = (((c1 - 0x90) * 10 + (c2 - 0x30)) * 126 + (c3 - 0x81)) * 10 + (c4 - 0x30);
341N/A if (i >= 0 && i < 0x100000) {
341N/A *pwc = (ucs4_t) (0x10000 + i);
341N/A return 4;
341N/A }
341N/A }
341N/A return RET_ILSEQ;
341N/A }
341N/A return RET_TOOFEW(0);
341N/A }
341N/A return RET_ILSEQ;
341N/A }
341N/A return RET_TOOFEW(0);
341N/A }
341N/A return RET_ILSEQ;
341N/A }
341N/A return RET_TOOFEW(0);
341N/A }
341N/A return RET_ILSEQ;
341N/A }
341N/A#endif
341N/A}
341N/A
341N/Astatic int
341N/Agb18030_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
341N/A{
341N/A int ret;
341N/A
341N/A#ifndef sun
341N/A /* Code set 0 (ASCII) */
341N/A ret = ascii_wctomb(conv,r,wc,n);
341N/A if (ret != RET_ILUNI)
341N/A return ret;
341N/A
341N/A /* Code set 1 (GBK extended) */
341N/A ret = gbk_wctomb(conv,r,wc,n);
341N/A if (ret != RET_ILUNI)
341N/A return ret;
341N/A
341N/A ret = gb18030ext_wctomb(conv,r,wc,n);
341N/A if (ret != RET_ILUNI)
341N/A return ret;
341N/A#endif
341N/A
341N/A /* Code set 2 (remainder of Unicode U+0000..U+FFFF) */
341N/A ret = gb18030uni_wctomb(conv,r,wc,n);
341N/A if (ret != RET_ILSEQ)
341N/A#ifdef sun
341N/A {
341N/A int v = 12600 * (r[0] - 0x81) + 1260 * (r[1] - 0x30) + 10 * (r[2] - 0x81) + (r[3] - 0x30);
341N/A
341N/A r[0] = (v & 0xff00) >> 8;
341N/A r[1] = v & 0xff;
341N/A ret = 2;
341N/A }
341N/A#endif
341N/A return ret;
341N/A
341N/A#ifndef sun
341N/A /* Code set 3 (Unicode U+10000..U+10FFFF) */
341N/A if (n >= 4) {
341N/A if (wc >= 0x10000 && wc < 0x110000) {
341N/A unsigned int i = wc - 0x10000;
341N/A r[3] = (i % 10) + 0x30; i = i / 10;
341N/A r[2] = (i % 126) + 0x81; i = i / 126;
341N/A r[1] = (i % 10) + 0x30; i = i / 10;
341N/A r[0] = i + 0x90;
341N/A return 4;
341N/A }
341N/A return RET_ILUNI;
341N/A }
341N/A return RET_TOOSMALL;
341N/A#endif
341N/A}