summaryrefslogtreecommitdiff
path: root/jni/iconv/lib/cp1258.h
blob: 578340483735ad4b87ec83b5c4b910c5a59536a7 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright (C) 1999-2001, 2004 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.
 */

/*
 * CP1258
 */

#include "flushwc.h"
#include "vietcomb.h"

static const unsigned char cp1258_comb_table[] = {
  0xcc0xec0xde0xd20xf2,
};

/* The possible bases in viet_comp_table_data:
   0x0041..0x0045, 0x0047..0x0049, 0x004B..0x0050, 0x0052..0x0057,
   0x0059..0x005A, 0x0061..0x0065, 0x0067..0x0069, 0x006B..0x0070,
   0x0072..0x0077, 0x0079..0x007A, 0x00A5, 0x00A8, 0x00C2, 0x00C5..0x00C7,
   0x00CA, 0x00CF, 0x00D3..0x00D4, 0x00D6, 0x00D8, 0x00DA, 0x00DC, 0x00E2,
   0x00E5..0x00E7, 0x00EA, 0x00EF, 0x00F3..0x00F4, 0x00F6, 0x00F8, 0x00FA,
   0x00FC, 0x0102..0x0103, 0x01A0..0x01A1, 0x01AF..0x01B0. */
static const unsigned int cp1258_comp_bases[] = {
  0x06fdfbbe0x06fdfbbe0x000000000x000001200x155884e40x155884e4,
  0x0000000c0x000000000x000000000x000000000x000000000x00018003
};

static const unsigned short cp1258_2uni[128] = {
  /* 0x80 */
  0x20ac0xfffd0x201a0x01920x201e0x20260x20200x2021,
  0x02c60x20300xfffd0x20390x01520xfffd0xfffd0xfffd,
  /* 0x90 */
  0xfffd0x20180x20190x201c0x201d0x20220x20130x2014,
  0x02dc0x21220xfffd0x203a0x01530xfffd0xfffd0x0178,
  /* 0xa0 */
  0x00a00x00a10x00a20x00a30x00a40x00a50x00a60x00a7,
  0x00a80x00a90x00aa0x00ab0x00ac0x00ad0x00ae0x00af,
  /* 0xb0 */
  0x00b00x00b10x00b20x00b30x00b40x00b50x00b60x00b7,
  0x00b80x00b90x00ba0x00bb0x00bc0x00bd0x00be0x00bf,
  /* 0xc0 */
  0x00c00x00c10x00c20x01020x00c40x00c50x00c60x00c7,
  0x00c80x00c90x00ca0x00cb0x03000x00cd0x00ce0x00cf,
  /* 0xd0 */
  0x01100x00d10x03090x00d30x00d40x01a00x00d60x00d7,
  0x00d80x00d90x00da0x00db0x00dc0x01af0x03030x00df,
  /* 0xe0 */
  0x00e00x00e10x00e20x01030x00e40x00e50x00e60x00e7,
  0x00e80x00e90x00ea0x00eb0x03010x00ed0x00ee0x00ef,
  /* 0xf0 */
  0x01110x00f10x03230x00f30x00f40x01a10x00f60x00f7,
  0x00f80x00f90x00fa0x00fb0x00fc0x01b00x20ab0x00ff,
};

/* In the CP1258 to Unicode direction, the state contains a buffered
   character, or 0 if none. */

static int
cp1258_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
{
  unsigned char c = *s;
  unsigned short wc;
  unsigned short last_wc;
  if (c < 0x80) {
    wc = c;
  } else {
    wc = cp1258_2uni[c-0x80];
    if (wc == 0xfffd)
      return RET_ILSEQ;
  }
  last_wc = conv->istate;
  if (last_wc) {
    if (wc >= 0x0300 && wc < 0x0340) {
      /* See whether last_wc and wc can be combined. */
      unsigned int k;
      unsigned int i1, i2;
      switch (wc) {
        case 0x0300: k = 0break;
        case 0x0301: k = 1break;
        case 0x0303: k = 2break;
        case 0x0309: k = 3break;
        case 0x0323: k = 4break;
        default: abort();
      }
      i1 = viet_comp_table[k].idx;
      i2 = i1 + viet_comp_table[k].len-1;
      if (last_wc >= viet_comp_table_data[i1].base
          && last_wc <= viet_comp_table_data[i2].base) {
        unsigned int i;
        for (;;) {
          i = (i1+i2)>>1;
          if (last_wc == viet_comp_table_data[i].base)
            break;
          if (last_wc < viet_comp_table_data[i].base) {
            if (i1 == i)
              goto not_combining;
            i2 = i;
          } else {
            if (i1 != i)
              i1 = i;
            else {
              i = i2;
              if (last_wc == viet_comp_table_data[i].base)
                break;
              goto not_combining;
            }
          }
        }
        last_wc = viet_comp_table_data[i].composed;
        /* Output the combined character. */
        conv->istate = 0;
        *pwc = (ucs4_t) last_wc;
        return 1;
      }
    }
  not_combining:
    /* Output the buffered character. */
    conv->istate = 0;
    *pwc = (ucs4_t) last_wc;
    return 0/* Don't advance the input pointer. */
  }
  if (wc >= 0x0041 && wc <= 0x01b0
      && ((cp1258_comp_bases[(wc - 0x0040) >> 5] >> (wc & 0x1f)) & 1)) {
    /* wc is a possible match in viet_comp_table_data. Buffer it. */
    conv->istate = wc;
    return RET_TOOFEW(1);
  } else {
    /* Output wc immediately. */
    *pwc = (ucs4_t) wc;
    return 1;
  }
}

#define cp1258_flushwc normal_flushwc

static const unsigned char cp1258_page00[88] = {
  0xc00xc10xc20x000xc40xc50xc60xc7/* 0xc0-0xc7 */
  0xc80xc90xca0xcb0x000xcd0xce0xcf/* 0xc8-0xcf */
  0x000xd10x000xd30xd40x000xd60xd7/* 0xd0-0xd7 */
  0xd80xd90xda0xdb0xdc0x000x000xdf/* 0xd8-0xdf */
  0xe00xe10xe20x000xe40xe50xe60xe7/* 0xe0-0xe7 */
  0xe80xe90xea0xeb0x000xed0xee0xef/* 0xe8-0xef */
  0x000xf10x000xf30xf40x000xf60xf7/* 0xf0-0xf7 */
  0xf80xf90xfa0xfb0xfc0x000x000xff/* 0xf8-0xff */
  /* 0x0100 */
  0x000x000xc30xe30x000x000x000x00/* 0x00-0x07 */
  0x000x000x000x000x000x000x000x00/* 0x08-0x0f */
  0xd00xf00x000x000x000x000x000x00/* 0x10-0x17 */
};
static const unsigned char cp1258_page01[104] = {
  0x000x000x8c0x9c0x000x000x000x00/* 0x50-0x57 */
  0x000x000x000x000x000x000x000x00/* 0x58-0x5f */
  0x000x000x000x000x000x000x000x00/* 0x60-0x67 */
  0x000x000x000x000x000x000x000x00/* 0x68-0x6f */
  0x000x000x000x000x000x000x000x00/* 0x70-0x77 */
  0x9f0x000x000x000x000x000x000x00/* 0x78-0x7f */
  0x000x000x000x000x000x000x000x00/* 0x80-0x87 */
  0x000x000x000x000x000x000x000x00/* 0x88-0x8f */
  0x000x000x830x000x000x000x000x00/* 0x90-0x97 */
  0x000x000x000x000x000x000x000x00/* 0x98-0x9f */
  0xd50xf50x000x000x000x000x000x00/* 0xa0-0xa7 */
  0x000x000x000x000x000x000x000xdd/* 0xa8-0xaf */
  0xfd0x000x000x000x000x000x000x00/* 0xb0-0xb7 */
};
static const unsigned char cp1258_page02[32] = {
  0x000x000x000x000x000x000x880x00/* 0xc0-0xc7 */
  0x000x000x000x000x000x000x000x00/* 0xc8-0xcf */
  0x000x000x000x000x000x000x000x00/* 0xd0-0xd7 */
  0x000x000x000x000x980x000x000x00/* 0xd8-0xdf */
};
static const unsigned char cp1258_page03[40] = {
  0xcc0xec0x000xde0x000x000x000x00/* 0x00-0x07 */
  0x000xd20x000x000x000x000x000x00/* 0x08-0x0f */
  0x000x000x000x000x000x000x000x00/* 0x10-0x17 */
  0x000x000x000x000x000x000x000x00/* 0x18-0x1f */
  0x000x000x000xf20x000x000x000x00/* 0x20-0x27 */
};
static const unsigned char cp1258_page20[48] = {
  0x000x000x000x960x970x000x000x00/* 0x10-0x17 */
  0x910x920x820x000x930x940x840x00/* 0x18-0x1f */
  0x860x870x950x000x000x000x850x00/* 0x20-0x27 */
  0x000x000x000x000x000x000x000x00/* 0x28-0x2f */
  0x890x000x000x000x000x000x000x00/* 0x30-0x37 */
  0x000x8b0x9b0x000x000x000x000x00/* 0x38-0x3f */
};

static int
cp1258_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
{
  unsigned char c = 0;
  if (wc < 0x0080) {
    *r = wc;
    return 1;
  }
  else if (wc >= 0x00a0 && wc < 0x00c0)
    c = wc;
  else if (wc >= 0x00c0 && wc < 0x0118)
    c = cp1258_page00[wc-0x00c0];
  else if (wc >= 0x0150 && wc < 0x01b8)
    c = cp1258_page01[wc-0x0150];
  else if (wc >= 0x02c0 && wc < 0x02e0)
    c = cp1258_page02[wc-0x02c0];
  else if (wc >= 0x0300 && wc < 0x0328)
    c = cp1258_page03[wc-0x0300];
  else if (wc >= 0x0340 && wc < 0x0342/* deprecated Vietnamese tone marks */
    c = cp1258_page03[wc-0x0340];
  else if (wc >= 0x2010 && wc < 0x2040)
    c = cp1258_page20[wc-0x2010];
  else if (wc == 0x20ab)
    c = 0xfe;
  else if (wc == 0x20ac)
    c = 0x80;
  else if (wc == 0x2122)
    c = 0x99;
  if (c != 0) {
    *r = c;
    return 1;
  }
  /* Try canonical decomposition. */
  {
    /* Binary search through viet_decomp_table. */
    unsigned int i1 = 0;
    unsigned int i2 = sizeof(viet_decomp_table)/sizeof(viet_decomp_table[0])-1;
    if (wc >= viet_decomp_table[i1].composed
        && wc <= viet_decomp_table[i2].composed) {
      unsigned int i;
      for (;;) {
        /* Here i2 - i1 > 0. */
        i = (i1+i2)>>1;
        if (wc == viet_decomp_table[i].composed)
          break;
        if (wc < viet_decomp_table[i].composed) {
          if (i1 == i)
            return RET_ILUNI;
          /* Here i1 < i < i2. */
          i2 = i;
        } else {
          /* Here i1 <= i < i2. */
          if (i1 != i)
            i1 = i;
          else {
            /* Here i2 - i1 = 1. */
            i = i2;
            if (wc == viet_decomp_table[i].composed)
              break;
            else
              return RET_ILUNI;
          }
        }
      }
      /* Found a canonical decomposition. */
      wc = viet_decomp_table[i].base;
      /* wc is one of 0x0020, 0x0041..0x005a, 0x0061..0x007a, 0x00a5, 0x00a8,
         0x00c2, 0x00c5..0x00c7, 0x00ca, 0x00cf, 0x00d3, 0x00d4, 0x00d6,
         0x00d8, 0x00da, 0x00dc, 0x00e2, 0x00e5..0x00e7, 0x00ea, 0x00ef,
         0x00f3, 0x00f4, 0x00f6, 0x00f8, 0x00fc, 0x0102, 0x0103, 0x01a0,
         0x01a1, 0x01af, 0x01b0. */
      if (wc < 0x0100)
        c = wc;
      else if (wc < 0x0118)
        c = cp1258_page00[wc-0x00c0];
      else
        c = cp1258_page01[wc-0x0150];
      if (n < 2)
        return RET_TOOSMALL;
      r[0] = c;
      r[1] = cp1258_comb_table[viet_decomp_table[i].comb1];
      return 2;
    }
  }
  return RET_ILUNI;
}