Annotation of embedaddon/libiconv/lib/gb18030.h, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 1999-2001, 2005 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., 51 Franklin Street,
        !            18:  * Fifth Floor, Boston, MA 02110-1301, USA.
        !            19:  */
        !            20: 
        !            21: /*
        !            22:  * GB18030
        !            23:  */
        !            24: 
        !            25: /*
        !            26:  * GB18030, as specified in the GB18030 standard, is an extension of GBK.
        !            27:  *
        !            28:  * In what follows, page numbers refer to the GB18030 standard (second
        !            29:  * printing).
        !            30:  *
        !            31:  *
        !            32:  * It consists of the following parts:
        !            33:  *
        !            34:  * One-byte range:
        !            35:  *   ASCII        p. 2         0x{00..7F}
        !            36:  *
        !            37:  * Two-byte range:
        !            38:  *   GBK part 1   p. 10..12    0x{A1..A9}{A1..FE}
        !            39:  *   GBK part 2   p. 13..36    0x{B0..F7}{A1..FE}
        !            40:  *   GBK part 3   p. 37..52    0x{81..A0}{40..7E,80..FE}
        !            41:  *   GBK part 4   p. 53..81    0x{AA..FE}{40..7E,80..A0}
        !            42:  *   GBK part 5   p. 82        0x{A8..A9}{40..7E,80..A0}
        !            43:  *   UDA part 1   p. 83..84    0x{AA..AF}{A1..FE}              U+E000..U+E233
        !            44:  *   UDA part 2   p. 85..87    0x{F8..FE}{A1..FE}              U+E234..U+E4C5
        !            45:  *   UDA part 3   p. 88..90    0x{A1..A7}{40..7E,80..A0}       U+E4C6..U+E765
        !            46:  *
        !            47:  * Four-byte range:
        !            48:  *   BMP rest     p. 94..283   0x{81..84}{30..39}{81..FE}{30..39}
        !            49:  *                                                     rest of U+0080..U+FFFF
        !            50:  *   Planes 1-16  p. 5         0x{90..FE}{30..39}{81..FE}{30..39}
        !            51:  *                                                            U+10000..U+10FFFF
        !            52:  *
        !            53:  * To GBK part 1 were added:
        !            54:  * 1. 0xA2E3, 0xA8BF.
        !            55:  * 2. Characters mapped to the Unicode PUA
        !            56:  *      0xA2AB..0xA2B0                      U+E766..U+E76B
        !            57:  *      0xA2E4                              U+E76D
        !            58:  *      0xA2EF..0xA2F0                      U+E76E..U+E76F
        !            59:  *      0xA2FD..0xA2FE                      U+E770..U+E771
        !            60:  *      0xA4F4..0xA4FE                      U+E772..U+E77C
        !            61:  *      0xA5F7..0xA5FE                      U+E77D..U+E784
        !            62:  *      0xA6B9..0xA6C0                      U+E785..U+E78C
        !            63:  *      0xA6D9..0xA6DF [glyphs here!!]      U+E78D..U+E793
        !            64:  *      0xA6EC..0xA6ED [glyphs here!!]      U+E794..U+E795
        !            65:  *      0xA6F3         [glyphs here!!]      U+E796
        !            66:  *      0xA6F6..0xA6FE                      U+E797..U+E79F
        !            67:  *      0xA7C2..0xA7D0                      U+E7A0..U+E7AE
        !            68:  *      0xA7F2..0xA7FE                      U+E7AF..U+E7BB
        !            69:  *      0xA8BC         [glyphs here!!]      U+E7C7
        !            70:  *      0xA8C1..0xA8C4                      U+E7C9..U+E7CC
        !            71:  *      0xA8EA..0xA8FE                      U+E7CD..U+E7E1
        !            72:  *      0xA9A1..0xA9A3                      U+E7FE..U+E800
        !            73:  *      0xA9F0..0xA9FE                      U+E801..U+E80F
        !            74:  *
        !            75:  * To GBK part 2 were added:
        !            76:  * 3. Characters mapped to the Unicode PUA
        !            77:  *      0xD7FA..0xD7FE                      U+E810..0xE814
        !            78:  *
        !            79:  * To GBK part 3 nothing was added.
        !            80:  *
        !            81:  * To GBK part 4 were added:
        !            82:  * 4. 0xFE{50,54..58,5A..60,62..65,68..6B,6E..75,77..7D,80..8F,92..9F}.
        !            83:  * 5. Characters mapped to the Unicode PUA
        !            84:  *      0xFE51..0xFE53 [glyphs here!!]      U+E816..U+E818
        !            85:  *      0xFE59         [glyphs here!!]      U+E81E
        !            86:  *      0xFE61         [glyphs here!!]      U+E826
        !            87:  *      0xFE66..0xFE67 [glyphs here!!]      U+E82B..U+E82C
        !            88:  *      0xFE6C..0xFE6D [glyphs here!!]      U+E831..U+E832
        !            89:  *      0xFE76         [glyphs here!!]      U+E83B
        !            90:  *      0xFE7E         [glyphs here!!]      U+E843
        !            91:  *      0xFE90..0xFE91 [glyphs here!!]      U+E854..U+E855
        !            92:  *      0xFEA0         [glyphs here!!]      U+E864
        !            93:  *
        !            94:  * To GBK part 5 were added:
        !            95:  * 6. 0xA98A..0xA995.
        !            96:  * 7. Characters mapped to the Unicode PUA
        !            97:  *      0xA896..0xA8A0                      U+E7BC..U+E7C6
        !            98:  *      0xA958                              U+E7E2
        !            99:  *      0xA95B                              U+E7E3
        !           100:  *      0xA95D..0xA95F                      U+E7E4..U+E7E6
        !           101:  *      0xA997..0xA9A0                      U+E7F4..U+E7FD
        !           102:  *
        !           103:  * UDA part 1 contains the user-defined characters, mapped to the Unicode PUA
        !           104:  * U+E000..U+E233 in ascending order.
        !           105:  *
        !           106:  * UDA part 2 contains the user-defined characters, mapped to the Unicode PUA
        !           107:  * U+E234..U+E4C5 in ascending order.
        !           108:  *
        !           109:  * UDA part 3 contains the user-defined characters, mapped to the Unicode PUA
        !           110:  * U+E4C6..U+E765 in ascending order.
        !           111:  *
        !           112:  * The four-byte range 0x{81..84}{30..39}{81..FE}{30..39}
        !           113:  * contains the rest of the Unicode BMP in ascending order.
        !           114:  *    Start: 0x81308130 = 0x0080
        !           115:  *    End:   0x8431A439 = 0xFFFF
        !           116:  *
        !           117:  * The four-byte range 0x{90..E3}{30..39}{81..FE}{30..39}
        !           118:  * contains the remaining 16 Unicode planes in Unicode order.
        !           119:  *    Start: 0x90308130 = 0x010000
        !           120:  *    End:   0xE3329A35 = 0x10FFFF
        !           121:  *
        !           122:  *
        !           123:  * Unassigned Unicode characters are mapped. For example,
        !           124:  *   U+173F = 0x8134BF35   (p. 120)
        !           125:  *   U+2EFF = 0x81398B31   (p. 148)
        !           126:  *   U+FFFE = 0x8431A438   (p. 283)
        !           127:  *
        !           128:  *
        !           129:  * The Unicode PUA (U+E000..U+F8FF) is mapped as follows:
        !           130:  *   p. 83..84    0x{AA..AF}{A1..FE}                  U+E000..U+E233
        !           131:  *   p. 85..87    0x{F8..FE}{A1..FE}                  U+E234..U+E4C5
        !           132:  *   p. 88..90    0x{A1..A7}{40..7E,80..A0}           U+E4C6..U+E765
        !           133:  *   p. 10        0xA2AB..0xA2B0                      U+E766..U+E76B
        !           134:  *   p. 255       0x8336C739                          U+E76C
        !           135:  *   p. 10        0xA2E4                              U+E76D
        !           136:  *   p. 10        0xA2EF..0xA2F0                      U+E76E..U+E76F
        !           137:  *   p. 10        0xA2FD..0xA2FE                      U+E770..U+E771
        !           138:  *   p. 11        0xA4F4..0xA4FE                      U+E772..U+E77C
        !           139:  *   p. 11        0xA5F7..0xA5FE                      U+E77D..U+E784
        !           140:  *   p. 11        0xA6B9..0xA6C0                      U+E785..U+E78C
        !           141:  *   p. 11        0xA6D9..0xA6DF [glyphs here!!]      U+E78D..U+E793
        !           142:  *   p. 11        0xA6EC..0xA6ED [glyphs here!!]      U+E794..U+E795
        !           143:  *   p. 11        0xA6F3 [glyphs here!!]              U+E796
        !           144:  *   p. 11        0xA6F6..0xA6FE                      U+E797..U+E79F
        !           145:  *   p. 12        0xA7C2..0xA7D0                      U+E7A0..U+E7AE
        !           146:  *   p. 12        0xA7F2..0xA7FE                      U+E7AF..U+E7BB
        !           147:  *   p. 82        0xA896..0xA8A0                      U+E7BC..U+E7C6
        !           148:  *   p. 12        0xA8BC [glyphs here!!]              U+E7C7
        !           149:  *   p. 255       0x8336C830                          U+E7C8
        !           150:  *   p. 12        0xA8C1..0xA8C4                      U+E7C9..U+E7CC
        !           151:  *   p. 12        0xA8EA..0xA8FE                      U+E7CD..U+E7E1
        !           152:  *   p. 82        0xA958                              U+E7E2
        !           153:  *   p. 82        0xA95B                              U+E7E3
        !           154:  *   p. 82        0xA95D..0xA95F                      U+E7E4..U+E7E6
        !           155:  *   p. 255       0x8336C831..0x8336C933              U+E7E7..U+E7F3
        !           156:  *   p. 82        0xA997..0xA9A0                      U+E7F4..U+E7FD
        !           157:  *   p. 12        0xA9A1..0xA9A3                      U+E7FE..U+E800
        !           158:  *   p. 12        0xA9F0..0xA9FE                      U+E801..U+E80F
        !           159:  *   p. 26        0xD7FA..0xD7FE                      U+E810..0xE814
        !           160:  *   p. 255       0x8336C934                          U+E815
        !           161:  *   p. 81        0xFE51..0xFE53 [glyphs here!!]      U+E816..U+E818
        !           162:  *   p. 255       0x8336C935..0x8336C939              U+E819..U+E81D
        !           163:  *   p. 81        0xFE59 [glyphs here!!]              U+E81E
        !           164:  *   p. 255       0x8336CA30..0x8336CA36              U+E81F..U+E825
        !           165:  *   p. 81        0xFE61 [glyphs here!!]              U+E826
        !           166:  *   p. 255       0x8336CA37..0x8336CB30              U+E827..U+E82A
        !           167:  *   p. 81        0xFE66..0xFE67 [glyphs here!!]      U+E82B..U+E82C
        !           168:  *   p. 255       0x8336CB31..0x8336CB34              U+E82D..U+E830
        !           169:  *   p. 81        0xFE6C..0xFE6D [glyphs here!!]      U+E831..U+E832
        !           170:  *   p. 255       0x8336CB35..0x8336CC32              U+E833..U+E83A
        !           171:  *   p. 81        0xFE76 [glyphs here!!]              U+E83B
        !           172:  *   p. 255       0x8336CC33..0x8336CC39              U+E83C..U+E842
        !           173:  *   p. 81        0xFE7E [glyphs here!!]              U+E843
        !           174:  *   p. 255       0x8336CD30..0x8336CE35              U+E844..U+E853
        !           175:  *   p. 81        0xFE90..0xFE91 [glyphs here!!]      U+E854..U+E855
        !           176:  *   p. 255       0x8336CE36..0x8336CF39              U+E856..U+E863
        !           177:  *   p. 81        0xFEA0 [glyphs here!!]              U+E864
        !           178:  *   p. 255..276  0x8336D030..0x84308130              U+E865..U+F8FF
        !           179:  *
        !           180:  *
        !           181:  * The Unicode surrogate area (U+D800..U+DFFF) is not mapped. (p. 255)
        !           182:  *
        !           183:  */
        !           184: 
        !           185: #include "gb18030ext.h"
        !           186: #include "gb18030uni.h"
        !           187: 
        !           188: static int
        !           189: gb18030_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
        !           190: {
        !           191:   int ret;
        !           192: 
        !           193:   /* Code set 0 (ASCII) */
        !           194:   if (*s < 0x80)
        !           195:     return ascii_mbtowc(conv,pwc,s,n);
        !           196: 
        !           197:   /* Code set 1 (GBK extended) */
        !           198:   ret = gbk_mbtowc(conv,pwc,s,n);
        !           199:   if (ret != RET_ILSEQ)
        !           200:     return ret;
        !           201: 
        !           202:   ret = gb18030ext_mbtowc(conv,pwc,s,n);
        !           203:   if (ret != RET_ILSEQ)
        !           204:     return ret;
        !           205: 
        !           206:   /* Code set 2 (remainder of Unicode U+0000..U+FFFF), including
        !           207:      User-defined characters, two-byte part of range U+E766..U+E864 */
        !           208:   ret = gb18030uni_mbtowc(conv,pwc,s,n);
        !           209:   if (ret != RET_ILSEQ)
        !           210:     return ret;
        !           211:   /* User-defined characters range U+E000..U+E765 */
        !           212:   {
        !           213:     unsigned char c1 = s[0];
        !           214:     if ((c1 >= 0xaa && c1 <= 0xaf) || (c1 >= 0xf8 && c1 <= 0xfe)) {
        !           215:       if (n >= 2) {
        !           216:         unsigned char c2 = s[1];
        !           217:         if (c2 >= 0xa1 && c2 <= 0xfe) {
        !           218:           *pwc = 0xe000 + 94 * (c1 >= 0xf8 ? c1 - 0xf2 : c1 - 0xaa) + (c2 - 0xa1);
        !           219:           return 2;
        !           220:         }
        !           221:       } else
        !           222:         return RET_TOOFEW(0);
        !           223:     } else if (c1 >= 0xa1 && c1 <= 0xa7) {
        !           224:       if (n >= 2) {
        !           225:         unsigned char c2 = s[1];
        !           226:         if (c2 >= 0x40 && c2 <= 0xa1 && c2 != 0x7f) {
        !           227:           *pwc = 0xe4c6 + 96 * (c1 - 0xa1) + c2 - (c2 >= 0x80 ? 0x41 : 0x40);
        !           228:           return 2;
        !           229:         }
        !           230:       } else
        !           231:         return RET_TOOFEW(0);
        !           232:     }
        !           233:   }
        !           234: 
        !           235:   /* Code set 3 (Unicode U+10000..U+10FFFF) */
        !           236:   {
        !           237:     unsigned char c1 = s[0];
        !           238:     if (c1 >= 0x90 && c1 <= 0xe3) {
        !           239:       if (n >= 2) {
        !           240:         unsigned char c2 = s[1];
        !           241:         if (c2 >= 0x30 && c2 <= 0x39) {
        !           242:           if (n >= 3) {
        !           243:             unsigned char c3 = s[2];
        !           244:             if (c3 >= 0x81 && c3 <= 0xfe) {
        !           245:               if (n >= 4) {
        !           246:                 unsigned char c4 = s[3];
        !           247:                 if (c4 >= 0x30 && c4 <= 0x39) {
        !           248:                   unsigned int i = (((c1 - 0x90) * 10 + (c2 - 0x30)) * 126 + (c3 - 0x81)) * 10 + (c4 - 0x30);
        !           249:                   if (i >= 0 && i < 0x100000) {
        !           250:                     *pwc = (ucs4_t) (0x10000 + i);
        !           251:                     return 4;
        !           252:                   }
        !           253:                 }
        !           254:                 return RET_ILSEQ;
        !           255:               }
        !           256:               return RET_TOOFEW(0);
        !           257:             }
        !           258:             return RET_ILSEQ;
        !           259:           }
        !           260:           return RET_TOOFEW(0);
        !           261:         }
        !           262:         return RET_ILSEQ;
        !           263:       }
        !           264:       return RET_TOOFEW(0);
        !           265:     }
        !           266:     return RET_ILSEQ;
        !           267:   }
        !           268: }
        !           269: 
        !           270: static const unsigned short gb18030_pua2charset[32*3] = {
        !           271: /* Unicode range   GB18030 range */
        !           272:   0xe766, 0xe76b,  0xa2ab, /*.. 0xa2b0, */
        !           273:   0xe76d, 0xe76d,  0xa2e4,
        !           274:   0xe76e, 0xe76f,  0xa2ef, /*.. 0xa2f0, */
        !           275:   0xe770, 0xe771,  0xa2fd, /*.. 0xa2fe, */
        !           276:   0xe772, 0xe77c,  0xa4f4, /*.. 0xa4fe, */
        !           277:   0xe77d, 0xe784,  0xa5f7, /*.. 0xa5fe, */
        !           278:   0xe785, 0xe78c,  0xa6b9, /*.. 0xa6c0, */
        !           279:   0xe78d, 0xe793,  0xa6d9, /*.. 0xa6df, */
        !           280:   0xe794, 0xe795,  0xa6ec, /*.. 0xa6ed, */
        !           281:   0xe796, 0xe796,  0xa6f3,
        !           282:   0xe797, 0xe79f,  0xa6f6, /*.. 0xa6fe, */
        !           283:   0xe7a0, 0xe7ae,  0xa7c2, /*.. 0xa7d0, */
        !           284:   0xe7af, 0xe7bb,  0xa7f2, /*.. 0xa7fe, */
        !           285:   0xe7bc, 0xe7c6,  0xa896, /*.. 0xa8a0, */
        !           286:   0xe7c7, 0xe7c7,  0xa8bc,
        !           287:   0xe7c9, 0xe7cc,  0xa8c1, /*.. 0xa8c4, */
        !           288:   0xe7cd, 0xe7e1,  0xa8ea, /*.. 0xa8fe, */
        !           289:   0xe7e2, 0xe7e2,  0xa958,
        !           290:   0xe7e3, 0xe7e3,  0xa95b,
        !           291:   0xe7e4, 0xe7e6,  0xa95d, /*.. 0xa95f, */
        !           292:   0xe7f4, 0xe800,  0xa997, /*.. 0xa9a3, */
        !           293:   0xe801, 0xe80f,  0xa9f0, /*.. 0xa9fe, */
        !           294:   0xe810, 0xe814,  0xd7fa, /*.. 0xd7fe, */
        !           295:   0xe816, 0xe818,  0xfe51, /*.. 0xfe53, */
        !           296:   0xe81e, 0xe81e,  0xfe59,
        !           297:   0xe826, 0xe826,  0xfe61,
        !           298:   0xe82b, 0xe82c,  0xfe66, /*.. 0xfe67, */
        !           299:   0xe831, 0xe832,  0xfe6c, /*.. 0xfe6d, */
        !           300:   0xe83b, 0xe83b,  0xfe76,
        !           301:   0xe843, 0xe843,  0xfe7e,
        !           302:   0xe854, 0xe855,  0xfe90, /*.. 0xfe91, */
        !           303:   0xe864, 0xe864,  0xfea0,
        !           304: };
        !           305: 
        !           306: static int
        !           307: gb18030_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
        !           308: {
        !           309:   int ret;
        !           310: 
        !           311:   /* Code set 0 (ASCII) */
        !           312:   ret = ascii_wctomb(conv,r,wc,n);
        !           313:   if (ret != RET_ILUNI)
        !           314:     return ret;
        !           315: 
        !           316:   /* Code set 1 (GBK extended) */
        !           317:   ret = gbk_wctomb(conv,r,wc,n);
        !           318:   if (ret != RET_ILUNI)
        !           319:     return ret;
        !           320: 
        !           321:   ret = gb18030ext_wctomb(conv,r,wc,n);
        !           322:   if (ret != RET_ILUNI)
        !           323:     return ret;
        !           324: 
        !           325:   /* Code set 2 (remainder of Unicode U+0000..U+FFFF) */
        !           326:   if (wc >= 0xe000 && wc <= 0xe864) {
        !           327:     if (n >= 2) {
        !           328:       if (wc < 0xe766) {
        !           329:         /* User-defined characters range U+E000..U+E765 */
        !           330:         if (wc < 0xe4c6) {
        !           331:           unsigned int i = wc - 0xe000;
        !           332:           r[1] = (i % 94) + 0xa1; i = i / 94;
        !           333:           r[0] = (i < 6 ? i + 0xaa : i + 0xf2);
        !           334:           return 2;
        !           335:         } else {
        !           336:           unsigned int i = wc - 0xe4c6;
        !           337:           r[0] = (i / 96) + 0xa1; i = i % 96;
        !           338:           r[1] = i + (i >= 0x3f ? 0x41 : 0x40);
        !           339:           return 2;
        !           340:         }
        !           341:       } else {
        !           342:         /* User-defined characters, two-byte part of range U+E766..U+E864 */
        !           343:         unsigned int k1 = 0;
        !           344:         unsigned int k2 = 32;
        !           345:         /* Invariant: We know that if wc occurs in Unicode interval in
        !           346:            gb18030_pua2charset, it does so at a k with  k1 <= k < k2. */
        !           347:         while (k1 < k2) {
        !           348:           unsigned int k = (k1 + k2) / 2;
        !           349:           if (wc < gb18030_pua2charset[k*3+0])
        !           350:             k2 = k;
        !           351:           else if (wc > gb18030_pua2charset[k*3+1])
        !           352:             k1 = k + 1;
        !           353:           else {
        !           354:             unsigned short c =
        !           355:               gb18030_pua2charset[k*3+2] + (wc - gb18030_pua2charset[k*3+0]);
        !           356:             r[0] = (c >> 8);
        !           357:             r[1] = (c & 0xff);
        !           358:             return 2;
        !           359:           }
        !           360:         }
        !           361:       }
        !           362:     } else
        !           363:       return RET_TOOSMALL;
        !           364:   }
        !           365:   ret = gb18030uni_wctomb(conv,r,wc,n);
        !           366:   if (ret != RET_ILUNI)
        !           367:     return ret;
        !           368: 
        !           369:   /* Code set 3 (Unicode U+10000..U+10FFFF) */
        !           370:   if (n >= 4) {
        !           371:     if (wc >= 0x10000 && wc < 0x110000) {
        !           372:       unsigned int i = wc - 0x10000;
        !           373:       r[3] = (i % 10) + 0x30; i = i / 10;
        !           374:       r[2] = (i % 126) + 0x81; i = i / 126;
        !           375:       r[1] = (i % 10) + 0x30; i = i / 10;
        !           376:       r[0] = i + 0x90;
        !           377:       return 4;
        !           378:     }
        !           379:     return RET_ILUNI;
        !           380:   }
        !           381:   return RET_TOOSMALL;
        !           382: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>