1/*
2 * Copyright (C) 1999-2002, 2006 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 * BIG5-HKSCS:2001
23 */
24
25/*
26 * BIG5-HKSCS:2001 can be downloaded from
27 *   http://www.info.gov.hk/digital21/eng/hkscs/download.html
28 *   http://www.info.gov.hk/digital21/eng/hkscs/index.html
29 *
30 * It extends BIG5-HKSCS:1999 through 116 characters.
31 *
32 * It extends BIG5 (without the rows 0xC6..0xC7) through the ranges
33 *
34 *   0x{88..8D}{40..7E,A1..FE}      757 characters
35 *   0x{8E..A0}{40..7E,A1..FE}     2898 characters
36 *   0x{C6..C8}{40..7E,A1..FE}      359 characters
37 *   0xF9{D6..FE}                    41 characters
38 *   0x{FA..FE}{40..7E,A1..FE}      763 characters
39 *
40 * Note that some HKSCS characters are not contained in Unicode 3.2
41 * and are therefore best represented as sequences of Unicode characters:
42 *   0x8862  U+00CA U+0304  LATIN CAPITAL LETTER E WITH CIRCUMFLEX AND MACRON
43 *   0x8864  U+00CA U+030C  LATIN CAPITAL LETTER E WITH CIRCUMFLEX AND CARON
44 *   0x88A3  U+00EA U+0304  LATIN SMALL LETTER E WITH CIRCUMFLEX AND MACRON
45 *   0x88A5  U+00EA U+030C  LATIN SMALL LETTER E WITH CIRCUMFLEX AND CARON
46 */
47
48#include "hkscs2001.h"
49#include "flushwc.h"
50
51static int
52big5hkscs2001_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
53{
54  ucs4_t last_wc = conv->istate;
55  if (last_wc) {
56    /* Output the buffered character. */
57    conv->istate = 0;
58    *pwc = last_wc;
59    return 0; /* Don't advance the input pointer. */
60  } else {
61    unsigned char c = *s;
62    /* Code set 0 (ASCII) */
63    if (c < 0x80)
64      return ascii_mbtowc(conv,pwc,s,n);
65    /* Code set 1 (BIG5 extended) */
66    if (c >= 0xa1 && c < 0xff) {
67      if (n < 2)
68        return RET_TOOFEW(0);
69      {
70        unsigned char c2 = s[1];
71        if ((c2 >= 0x40 && c2 < 0x7f) || (c2 >= 0xa1 && c2 < 0xff)) {
72          if (!((c == 0xc6 && c2 >= 0xa1) || c == 0xc7)) {
73            int ret = big5_mbtowc(conv,pwc,s,2);
74            if (ret != RET_ILSEQ)
75              return ret;
76          }
77        }
78      }
79    }
80    {
81      int ret = hkscs1999_mbtowc(conv,pwc,s,n);
82      if (ret != RET_ILSEQ)
83        return ret;
84    }
85    {
86      int ret = hkscs2001_mbtowc(conv,pwc,s,n);
87      if (ret != RET_ILSEQ)
88        return ret;
89    }
90    if (c == 0x88) {
91      if (n < 2)
92        return RET_TOOFEW(0);
93      {
94        unsigned char c2 = s[1];
95        if (c2 == 0x62 || c2 == 0x64 || c2 == 0xa3 || c2 == 0xa5) {
96          /* It's a composed character. */
97          ucs4_t wc1 = ((c2 >> 3) << 2) + 0x009a; /* = 0x00ca or 0x00ea */
98          ucs4_t wc2 = ((c2 & 6) << 2) + 0x02fc; /* = 0x0304 or 0x030c */
99          /* We cannot output two Unicode characters at once. So,
100             output the first character and buffer the second one. */
101          *pwc = wc1;
102          conv->istate = wc2;
103          return 2;
104        }
105      }
106    }
107    return RET_ILSEQ;
108  }
109}
110
111#define big5hkscs2001_flushwc normal_flushwc
112
113static int
114big5hkscs2001_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
115{
116  int count = 0;
117  unsigned char last = conv->ostate;
118
119  if (last) {
120    /* last is = 0x66 or = 0xa7. */
121    if (wc == 0x0304 || wc == 0x030c) {
122      /* Output the combined character. */
123      if (n >= 2) {
124        r[0] = 0x88;
125        r[1] = last + ((wc & 24) >> 2) - 4; /* = 0x62 or 0x64 or 0xa3 or 0xa5 */
126        conv->ostate = 0;
127        return 2;
128      } else
129        return RET_TOOSMALL;
130    }
131
132    /* Output the buffered character. */
133    if (n < 2)
134      return RET_TOOSMALL;
135    r[0] = 0x88;
136    r[1] = last;
137    r += 2;
138    count = 2;
139  }
140
141  /* Code set 0 (ASCII) */
142  if (wc < 0x0080) {
143    /* Plain ASCII character. */
144    if (n > count) {
145      r[0] = (unsigned char) wc;
146      conv->ostate = 0;
147      return count+1;
148    } else
149      return RET_TOOSMALL;
150  } else {
151    unsigned char buf[2];
152    int ret;
153
154    /* Code set 1 (BIG5 extended) */
155    ret = big5_wctomb(conv,buf,wc,2);
156    if (ret != RET_ILUNI) {
157      if (ret != 2) abort();
158      if (!((buf[0] == 0xc6 && buf[1] >= 0xa1) || buf[0] == 0xc7)) {
159        if (n >= count+2) {
160          r[0] = buf[0];
161          r[1] = buf[1];
162          conv->ostate = 0;
163          return count+2;
164        } else
165          return RET_TOOSMALL;
166      }
167    }
168    ret = hkscs1999_wctomb(conv,buf,wc,2);
169    if (ret != RET_ILUNI) {
170      if (ret != 2) abort();
171      if ((wc & ~0x0020) == 0x00ca) {
172        /* A possible first character of a multi-character sequence. We have to
173           buffer it. */
174        if (!(buf[0] == 0x88 && (buf[1] == 0x66 || buf[1] == 0xa7))) abort();
175        conv->ostate = buf[1]; /* = 0x66 or = 0xa7 */
176        return count+0;
177      }
178      if (n >= count+2) {
179        r[0] = buf[0];
180        r[1] = buf[1];
181        conv->ostate = 0;
182        return count+2;
183      } else
184        return RET_TOOSMALL;
185    }
186    ret = hkscs2001_wctomb(conv,buf,wc,2);
187    if (ret != RET_ILUNI) {
188      if (ret != 2) abort();
189      if (n >= count+2) {
190        r[0] = buf[0];
191        r[1] = buf[1];
192        conv->ostate = 0;
193        return count+2;
194      } else
195        return RET_TOOSMALL;
196    }
197    return RET_ILUNI;
198  }
199}
200
201static int
202big5hkscs2001_reset (conv_t conv, unsigned char *r, int n)
203{
204  unsigned char last = conv->ostate;
205
206  if (last) {
207    if (n < 2)
208      return RET_TOOSMALL;
209    r[0] = 0x88;
210    r[1] = last;
211    /* conv->ostate = 0; will be done by the caller */
212    return 2;
213  } else
214    return 0;
215}
216