summaryrefslogtreecommitdiff
path: root/jni/iconv/lib/euc_jisx0213.h
blob: 4d417ca12e119a6df171facbfe6f141792201d98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * Copyright (C) 1999-2002 Free Software Foundation, Inc.
 * This file is part of the GNU LIBICONV Library.
 *
 * The GNU LIBICONV Library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * The GNU LIBICONV Library is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
 * If not, write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA.
 */

/*
 * EUC-JISX0213
 */

/* The structure of EUC-JISX0213 is as follows:

   0x00..0x7F: ASCII

   0x8E{A1..FE}: JISX0201 Katakana, with prefix 0x8E, offset by +0x80.

   0x8F{A1..FE}{A1..FE}: JISX0213 plane 2, with prefix 0x8F, offset by +0x8080.

   0x{A1..FE}{A1..FE}: JISX0213 plane 1, offset by +0x8080.

   Note that some JISX0213 characters are not contained in Unicode 3.2
   and are therefore best represented as sequences of Unicode characters.
*/

#include "jisx0213.h"
#include "flushwc.h"

static int
euc_jisx0213_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
{
  ucs4_t last_wc = conv->istate;
  if (last_wc) {
    /* Output the buffered character. */
    conv->istate = 0;
    *pwc = last_wc;
    return 0/* Don't advance the input pointer. */
  } else {
    unsigned char c = *s;
    if (c < 0x80) {
      /* Plain ASCII character. */
      *pwc = (ucs4_t) c;
      return 1;
    } else {
      if ((c >= 0xa1 && c <= 0xfe) || c == 0x8e || c == 0x8f) {
        /* Two or three byte character. */
        if (n >= 2) {
          unsigned char c2 = s[1];
          if (c2 >= 0xa1 && c2 <= 0xfe) {
            if (c == 0x8e) {
              /* Half-width katakana. */
              if (c2 <= 0xdf) {
                *pwc = c2 + 0xfec0;
                return 2;
              }
            } else {
              ucs4_t wc;
              if (c == 0x8f) {
                /* JISX 0213 plane 2. */
                if (n >= 3) {
                  unsigned char c3 = s[2];
                  wc = jisx0213_to_ucs4(0x200-0x80+c2,c3^0x80);
                } else
                  return RET_TOOFEW(0);
              } else {
                /* JISX 0213 plane 1. */
                wc = jisx0213_to_ucs4(0x100-0x80+c,c2^0x80);
              }
              if (wc) {
                if (wc < 0x80) {
                  /* It's a combining character. */
                  ucs4_t wc1 = jisx0213_to_ucs_combining[wc - 1][0];
                  ucs4_t wc2 = jisx0213_to_ucs_combining[wc - 1][1];
                  /* We cannot output two Unicode characters at once. So,
                     output the first character and buffer the second one. */
                  *pwc = wc1;
                  conv->istate = wc2;
                } else
                  *pwc = wc;
                return (c == 0x8f ? 3 : 2);
              }
            }
          }
        } else
          return RET_TOOFEW(0);
      }
      return RET_ILSEQ;
    }
  }
}

#define euc_jisx0213_flushwc normal_flushwc

/* Composition tables for each of the relevant combining characters.  */
static const struct { unsigned short base; unsigned short composed; } euc_jisx0213_comp_table_data[] = {
#define euc_jisx0213_comp_table02e5_idx 0
#define euc_jisx0213_comp_table02e5_len 1
  { 0xabe40xabe5 }, /* 0x12B65 = 0x12B64 U+02E5 */
#define euc_jisx0213_comp_table02e9_idx (euc_jisx0213_comp_table02e5_idx+euc_jisx0213_comp_table02e5_len)
#define euc_jisx0213_comp_table02e9_len 1
  { 0xabe00xabe6 }, /* 0x12B66 = 0x12B60 U+02E9 */
#define euc_jisx0213_comp_table0300_idx (euc_jisx0213_comp_table02e9_idx+euc_jisx0213_comp_table02e9_len)
#define euc_jisx0213_comp_table0300_len 5
  { 0xa9dc0xabc4 }, /* 0x12B44 = 0x1295C U+0300 */
  { 0xabb80xabc8 }, /* 0x12B48 = 0x12B38 U+0300 */
  { 0xabb70xabca }, /* 0x12B4A = 0x12B37 U+0300 */
  { 0xabb00xabcc }, /* 0x12B4C = 0x12B30 U+0300 */
  { 0xabc30xabce }, /* 0x12B4E = 0x12B43 U+0300 */
#define euc_jisx0213_comp_table0301_idx (euc_jisx0213_comp_table0300_idx+euc_jisx0213_comp_table0300_len)
#define euc_jisx0213_comp_table0301_len 4
  { 0xabb80xabc9 }, /* 0x12B49 = 0x12B38 U+0301 */
  { 0xabb70xabcb }, /* 0x12B4B = 0x12B37 U+0301 */
  { 0xabb00xabcd }, /* 0x12B4D = 0x12B30 U+0301 */
  { 0xabc30xabcf }, /* 0x12B4F = 0x12B43 U+0301 */
#define euc_jisx0213_comp_table309a_idx (euc_jisx0213_comp_table0301_idx+euc_jisx0213_comp_table0301_len)
#define euc_jisx0213_comp_table309a_len 14
  { 0xa4ab0xa4f7 }, /* 0x12477 = 0x1242B U+309A */
  { 0xa4ad0xa4f8 }, /* 0x12478 = 0x1242D U+309A */
  { 0xa4af0xa4f9 }, /* 0x12479 = 0x1242F U+309A */
  { 0xa4b10xa4fa }, /* 0x1247A = 0x12431 U+309A */
  { 0xa4b30xa4fb }, /* 0x1247B = 0x12433 U+309A */
  { 0xa5ab0xa5f7 }, /* 0x12577 = 0x1252B U+309A */
  { 0xa5ad0xa5f8 }, /* 0x12578 = 0x1252D U+309A */
  { 0xa5af0xa5f9 }, /* 0x12579 = 0x1252F U+309A */
  { 0xa5b10xa5fa }, /* 0x1257A = 0x12531 U+309A */
  { 0xa5b30xa5fb }, /* 0x1257B = 0x12533 U+309A */
  { 0xa5bb0xa5fc }, /* 0x1257C = 0x1253B U+309A */
  { 0xa5c40xa5fd }, /* 0x1257D = 0x12544 U+309A */
  { 0xa5c80xa5fe }, /* 0x1257E = 0x12548 U+309A */
  { 0xa6f50xa6f8 }, /* 0x12678 = 0x12675 U+309A */
};

static int
euc_jisx0213_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
{
  int count = 0;
  unsigned short lasttwo = conv->ostate;

  if (lasttwo) {
    /* Attempt to combine the last character with this one. */
    unsigned int idx;
    unsigned int len;

    if (wc == 0x02e5)
      idx = euc_jisx0213_comp_table02e5_idx,
      len = euc_jisx0213_comp_table02e5_len;
    else if (wc == 0x02e9)
      idx = euc_jisx0213_comp_table02e9_idx,
      len = euc_jisx0213_comp_table02e9_len;
    else if (wc == 0x0300)
      idx = euc_jisx0213_comp_table0300_idx,
      len = euc_jisx0213_comp_table0300_len;
    else if (wc == 0x0301)
      idx = euc_jisx0213_comp_table0301_idx,
      len = euc_jisx0213_comp_table0301_len;
    else if (wc == 0x309a)
      idx = euc_jisx0213_comp_table309a_idx,
      len = euc_jisx0213_comp_table309a_len;
    else
      goto not_combining;

    do
      if (euc_jisx0213_comp_table_data[idx].base == lasttwo)
        break;
    while (++idx, --len > 0);

    if (len > 0) {
      /* Output the combined character. */
      if (n >= 2) {
        lasttwo = euc_jisx0213_comp_table_data[idx].composed;
        r[0] = (lasttwo >> 8) & 0xff;
        r[1] = lasttwo & 0xff;
        conv->ostate = 0;
        return 2;
      } else
        return RET_TOOSMALL;
    }

  not_combining:
    /* Output the buffered character. */
    if (n < 2)
      return RET_TOOSMALL;
    r[0] = (lasttwo >> 8) & 0xff;
    r[1] = lasttwo & 0xff;
    r += 2;
    count = 2;
  }

  if (wc < 0x80) {
    /* Plain ASCII character. */
    if (n > count) {
      r[0] = (unsigned char) wc;
      conv->ostate = 0;
      return count+1;
    } else
      return RET_TOOSMALL;
  } else if (wc >= 0xff61 && wc <= 0xff9f) {
    /* Half-width katakana. */
    if (n >= count+2) {
      r[0] = 0x8e;
      r[1] = wc - 0xfec0;
      conv->ostate = 0;
      return count+2;
    } else
      return RET_TOOSMALL;
  } else {
    unsigned short jch = ucs4_to_jisx0213(wc);
    if (jch != 0) {
      if (jch & 0x0080) {
        /* A possible match in comp_table_data. We have to buffer it. */
        /* We know it's a JISX 0213 plane 1 character. */
        if (jch & 0x8000) abort();
        conv->ostate = jch | 0x8080;
        return count+0;
      }
      if (jch & 0x8000) {
        /* JISX 0213 plane 2. */
        if (n >= count+3) {
          r[0] = 0x8f;
          r[1] = (jch >> 8) | 0x80;
          r[2] = (jch & 0xff) | 0x80;
          conv->ostate = 0;
          return count+3;
        } else
          return RET_TOOSMALL;
      } else {
        /* JISX 0213 plane 1. */
        if (n >= count+2) {
          r[0] = (jch >> 8) | 0x80;
          r[1] = (jch & 0xff) | 0x80;
          conv->ostate = 0;
          return count+2;
        } else
          return RET_TOOSMALL;
      }
    }
    return RET_ILUNI;
  }
}

static int
euc_jisx0213_reset (conv_t conv, unsigned char *r, int n)
{
  state_t lasttwo = conv->ostate;

  if (lasttwo) {
    if (n < 2)
      return RET_TOOSMALL;
    r[0] = (lasttwo >> 8) & 0xff;
    r[1] = lasttwo & 0xff;
    /* conv->ostate = 0; will be done by the caller */
    return 2;
  } else
    return 0;
}