| 1 |
/* |
| 2 |
* Copyright (C) 2005, 2016 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, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* CP936 |
| 22 |
*/ |
| 23 |
|
| 24 |
/* |
| 25 |
* The IANA has CP936 as an alias of GBK. But GBK is an official Chinese |
| 26 |
* specification, whereas CP936 is de-facto maintained by Microsoft. And, |
| 27 |
* of course, Microsoft modified CP936 since 1999. |
| 28 |
* |
| 29 |
* The differences from GBK are: |
| 30 |
* |
| 31 |
* 1. A single character: |
| 32 |
* |
| 33 |
* code CP936.TXT |
| 34 |
* 0x80 0x20AC # EURO SIGN |
| 35 |
* |
| 36 |
* Some variants of CP936 (in JDK, Windows-2000, ICU) also add: |
| 37 |
* |
| 38 |
* 2. Private area mappings: |
| 39 |
* |
| 40 |
* code Unicode |
| 41 |
* 0x{A1..A2}{40..7E,80..A0} U+E4C6..U+E585 |
| 42 |
* 0x{AA..AF,F8..FE}{A1..FE} U+E000..U+E4C5 |
| 43 |
* |
| 44 |
* We add them too because, although there are backward compatibility problems |
| 45 |
* when a character from a private area is moved to an official Unicode code |
| 46 |
* point, they are useful for some people in practice. |
| 47 |
*/ |
| 48 |
|
| 49 |
static int |
| 50 |
cp936_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n) |
| 51 |
{ |
| 52 |
/* Try GBK first. */ |
| 53 |
{ |
| 54 |
int ret = ces_gbk_mbtowc(conv,pwc,s,n); |
| 55 |
if (ret != RET_ILSEQ) |
| 56 |
return ret; |
| 57 |
} |
| 58 |
/* Then handle the additional mappings. */ |
| 59 |
{ |
| 60 |
unsigned char c = *s; |
| 61 |
if (c == 0x80) { |
| 62 |
*pwc = 0x20ac; |
| 63 |
return 1; |
| 64 |
} |
| 65 |
/* User-defined characters */ |
| 66 |
if (c >= 0xa1 && c <= 0xa2) { |
| 67 |
if (n < 2) |
| 68 |
return RET_TOOFEW(0); |
| 69 |
{ |
| 70 |
unsigned char c2 = s[1]; |
| 71 |
if ((c2 >= 0x40 && c2 < 0x7f) || (c2 >= 0x80 && c2 < 0xa1)) { |
| 72 |
*pwc = 0xe4c6 + 96 * (c - 0xa1) + (c2 - (c2 >= 0x80 ? 0x41 : 0x40)); |
| 73 |
return 2; |
| 74 |
} |
| 75 |
} |
| 76 |
} else if ((c >= 0xaa && c < 0xb0) || (c >= 0xf8 && c < 0xff)) { |
| 77 |
if (n < 2) |
| 78 |
return RET_TOOFEW(0); |
| 79 |
{ |
| 80 |
unsigned char c2 = s[1]; |
| 81 |
if (c2 >= 0xa1 && c2 < 0xff) { |
| 82 |
*pwc = 0xe000 + 94 * (c - (c >= 0xf8 ? 0xf2 : 0xaa)) + (c2 - 0xa1); |
| 83 |
return 2; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
return RET_ILSEQ; |
| 89 |
} |
| 90 |
|
| 91 |
static int |
| 92 |
cp936_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n) |
| 93 |
{ |
| 94 |
/* Try GBK first. */ |
| 95 |
{ |
| 96 |
int ret = ces_gbk_wctomb(conv,r,wc,n); |
| 97 |
if (ret != RET_ILUNI) |
| 98 |
return ret; |
| 99 |
} |
| 100 |
/* Then handle the additional mappings. */ |
| 101 |
if (wc >= 0xe000 && wc < 0xe586) { |
| 102 |
/* User-defined characters */ |
| 103 |
if (n < 2) |
| 104 |
return RET_TOOFEW(0); |
| 105 |
if (wc < 0xe4c6) { |
| 106 |
unsigned int i = wc - 0xe000; |
| 107 |
unsigned int c1 = i / 94; |
| 108 |
unsigned int c2 = i % 94; |
| 109 |
r[0] = c1 + (c1 < 6 ? 0xaa : 0xf2); |
| 110 |
r[1] = c2 + 0xa1; |
| 111 |
return 2; |
| 112 |
} else { |
| 113 |
unsigned int i = wc - 0xe4c6; |
| 114 |
unsigned int c1 = i / 96; |
| 115 |
unsigned int c2 = i % 96; |
| 116 |
r[0] = c1 + 0xa1; |
| 117 |
r[1] = c2 + (c2 < 0x3f ? 0x40 : 0x41); |
| 118 |
return 2; |
| 119 |
} |
| 120 |
} else if (wc == 0x20ac) { |
| 121 |
r[0] = 0x80; |
| 122 |
return 1; |
| 123 |
} |
| 124 |
return RET_ILUNI; |
| 125 |
} |