1/* 2 * Copyright (C) 1999-2001 Free Software Foundation, Inc. 3 * This file is part of the GNU LIBICONV Library. 4 * 5 * The GNU LIBICONV Library is free software; you can redistribute it 6 * and/or modify it under the terms of the GNU Library General Public 7 * License as published by the Free Software Foundation; either version 2 8 * of the License, or (at your option) any later version. 9 * 10 * The GNU LIBICONV Library is distributed in the hope that it will be 11 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Library General Public License for more details. 14 * 15 * You should have received a copy of the GNU Library General Public 16 * License along with the GNU LIBICONV Library; see the file COPYING.LIB. 17 * If not, write to the Free Software Foundation, Inc., 59 Temple Place - 18 * Suite 330, Boston, MA 02111-1307, USA. 19 */ 20 21/* 22 * GBK 23 */ 24 25/* 26 * GBK, as described in Ken Lunde's book, is an extension of GB 2312-1980 27 * (shifted by adding 0x8080 to the range 0xA1A1..0xFEFE, as used in EUC-CN). 28 * It adds the following ranges: 29 * 30 * (part of GBK/1) 0xA2A1-0xA2AA Small Roman numerals 31 * GBK/3 0x{81-A0}{40-7E,80-FE} 6080 new characters, all in Unicode 32 * GBK/4 0x{AA-FE}{40-7E,80-A0} 8160 new characters, 8080 in Unicode 33 * GBK/5 0x{A8-A9}{40-7E,80-A0} 166 new characters, 153 in Unicode 34 */ 35 36/* 37 * CP936 is nearly identical to GBK. It differs as follows: 38 * 39 * 1. Some characters in the GB2312 range are defined differently: 40 * 41 * code GB2312 CP936.TXT 42 * 0xA1A4 0x30FB # KATAKANA MIDDLE DOT 0x00B7 # MIDDLE DOT 43 * 0xA1AA 0x2015 # HORIZONTAL BAR 0x2014 # EM DASH 44 * 45 * 2. 19 characters added in the range 0xA6E0-0xA6F5. 46 * 47 * 3. 4 characters added in the range 0xA8BB-0xA8C0. 48 */ 49 50/* 51 * Since all three tables I have looked at 52 * - the CP936 table by Microsoft, found on ftp.unicode.org, 53 * - the GBK table by Sun, investigated on a Solaris 2.7 machine, 54 * - the GBK tables by CWEX, found in the Big5+ package, 55 * all include these CP936 extensions (the CWEX tables have additional 56 * differences), I conclude that either Ken Lunde has overlooked some of 57 * the differences between GB2312 and GBK, or he is right but the major 58 * vendors don't care about it. In either case, CP936 is the de facto 59 * standard under the name "GBK", and we should better support it. 60 * 61 * So in what follows, when we write "GBK" we always mean "CP936". 62 */ 63 64#include "gbkext1.h" 65#include "gbkext2.h" 66#include "gbkext_inv.h" 67#include "cp936ext.h" 68 69static int 70gbk_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n) 71{ 72 unsigned char c = *s; 73 74 if (c >= 0x81 && c < 0xff) { 75 if (n < 2) 76 return RET_TOOFEW(0); 77 if (c >= 0xa1 && c <= 0xf7) { 78 unsigned char c2 = s[1]; 79 if (c == 0xa1) { 80 if (c2 == 0xa4) { 81 *pwc = 0x00b7; 82 return 2; 83 } 84 if (c2 == 0xaa) { 85 *pwc = 0x2014; 86 return 2; 87 } 88 } 89 if (c2 >= 0xa1 && c2 < 0xff) { 90 unsigned char buf[2]; 91 int ret; 92 buf[0] = c-0x80; buf[1] = c2-0x80; 93 ret = gb2312_mbtowc(conv,pwc,buf,2); 94 if (ret != RET_ILSEQ) 95 return ret; 96 buf[0] = c; buf[1] = c2; 97 ret = cp936ext_mbtowc(conv,pwc,buf,2); 98 if (ret != RET_ILSEQ) 99 return ret; 100 } 101 } 102 if (c >= 0x81 && c <= 0xa0) 103 return gbkext1_mbtowc(conv,pwc,s,2); 104 if (c >= 0xa8 && c <= 0xfe) 105 return gbkext2_mbtowc(conv,pwc,s,2); 106 if (c == 0xa2) { 107 unsigned char c2 = s[1]; 108 if (c2 >= 0xa1 && c2 <= 0xaa) { 109 *pwc = 0x2170+(c2-0xa1); 110 return 2; 111 } 112 } 113 } 114 return RET_ILSEQ; 115} 116 117static int 118gbk_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n) 119{ 120 unsigned char buf[2]; 121 int ret; 122 123 if (wc != 0x30fb && wc != 0x2015) { 124 ret = gb2312_wctomb(conv,buf,wc,2); 125 if (ret != RET_ILUNI) { 126 if (ret != 2) abort(); 127 if (n < 2) 128 return RET_TOOSMALL; 129 r[0] = buf[0]+0x80; 130 r[1] = buf[1]+0x80; 131 return 2; 132 } 133 } 134 ret = gbkext_inv_wctomb(conv,buf,wc,2); 135 if (ret != RET_ILUNI) { 136 if (ret != 2) abort(); 137 if (n < 2) 138 return RET_TOOSMALL; 139 r[0] = buf[0]; 140 r[1] = buf[1]; 141 return 2; 142 } 143 if (wc >= 0x2170 && wc <= 0x2179) { 144 r[0] = 0xa2; 145 r[1] = 0xa1 + (wc-0x2170); 146 return 2; 147 } 148 ret = cp936ext_wctomb(conv,buf,wc,2); 149 if (ret != RET_ILUNI) { 150 if (ret != 2) abort(); 151 if (n < 2) 152 return RET_TOOSMALL; 153 r[0] = buf[0]; 154 r[1] = buf[1]; 155 return 2; 156 } 157 if (wc == 0x00b7) { 158 if (n < 2) 159 return RET_TOOSMALL; 160 r[0] = 0xa1; 161 r[1] = 0xa4; 162 return 2; 163 } 164 if (wc == 0x2014) { 165 if (n < 2) 166 return RET_TOOSMALL; 167 r[0] = 0xa1; 168 r[1] = 0xaa; 169 return 2; 170 } 171 172 return RET_ILUNI; 173} 174