]> git.lyx.org Git - lyx.git/blob - 3rdparty/libiconv/1.15/lib/euc_jisx0213.h
Implement auto-nesting.
[lyx.git] / 3rdparty / libiconv / 1.15 / lib / euc_jisx0213.h
1 /*
2  * Copyright (C) 1999-2002, 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  * EUC-JISX0213
22  */
23
24 /* The structure of EUC-JISX0213 is as follows:
25
26    0x00..0x7F: ASCII
27
28    0x8E{A1..FE}: JISX0201 Katakana, with prefix 0x8E, offset by +0x80.
29
30    0x8F{A1..FE}{A1..FE}: JISX0213 plane 2, with prefix 0x8F, offset by +0x8080.
31
32    0x{A1..FE}{A1..FE}: JISX0213 plane 1, offset by +0x8080.
33
34    Note that some JISX0213 characters are not contained in Unicode 3.2
35    and are therefore best represented as sequences of Unicode characters.
36 */
37
38 #include "jisx0213.h"
39 #include "flushwc.h"
40
41 static int
42 euc_jisx0213_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
43 {
44   ucs4_t last_wc = conv->istate;
45   if (last_wc) {
46     /* Output the buffered character. */
47     conv->istate = 0;
48     *pwc = last_wc;
49     return 0; /* Don't advance the input pointer. */
50   } else {
51     unsigned char c = *s;
52     if (c < 0x80) {
53       /* Plain ASCII character. */
54       *pwc = (ucs4_t) c;
55       return 1;
56     } else {
57       if ((c >= 0xa1 && c <= 0xfe) || c == 0x8e || c == 0x8f) {
58         /* Two or three byte character. */
59         if (n >= 2) {
60           unsigned char c2 = s[1];
61           if (c2 >= 0xa1 && c2 <= 0xfe) {
62             if (c == 0x8e) {
63               /* Half-width katakana. */
64               if (c2 <= 0xdf) {
65                 *pwc = c2 + 0xfec0;
66                 return 2;
67               }
68             } else {
69               ucs4_t wc;
70               if (c == 0x8f) {
71                 /* JISX 0213 plane 2. */
72                 if (n >= 3) {
73                   unsigned char c3 = s[2];
74                   wc = jisx0213_to_ucs4(0x200-0x80+c2,c3^0x80);
75                 } else
76                   return RET_TOOFEW(0);
77               } else {
78                 /* JISX 0213 plane 1. */
79                 wc = jisx0213_to_ucs4(0x100-0x80+c,c2^0x80);
80               }
81               if (wc) {
82                 if (wc < 0x80) {
83                   /* It's a combining character. */
84                   ucs4_t wc1 = jisx0213_to_ucs_combining[wc - 1][0];
85                   ucs4_t wc2 = jisx0213_to_ucs_combining[wc - 1][1];
86                   /* We cannot output two Unicode characters at once. So,
87                      output the first character and buffer the second one. */
88                   *pwc = wc1;
89                   conv->istate = wc2;
90                 } else
91                   *pwc = wc;
92                 return (c == 0x8f ? 3 : 2);
93               }
94             }
95           }
96         } else
97           return RET_TOOFEW(0);
98       }
99       return RET_ILSEQ;
100     }
101   }
102 }
103
104 #define euc_jisx0213_flushwc normal_flushwc
105
106 /* Composition tables for each of the relevant combining characters.  */
107 static const struct { unsigned short base; unsigned short composed; } euc_jisx0213_comp_table_data[] = {
108 #define euc_jisx0213_comp_table02e5_idx 0
109 #define euc_jisx0213_comp_table02e5_len 1
110   { 0xabe4, 0xabe5 }, /* 0x12B65 = 0x12B64 U+02E5 */
111 #define euc_jisx0213_comp_table02e9_idx (euc_jisx0213_comp_table02e5_idx+euc_jisx0213_comp_table02e5_len)
112 #define euc_jisx0213_comp_table02e9_len 1
113   { 0xabe0, 0xabe6 }, /* 0x12B66 = 0x12B60 U+02E9 */
114 #define euc_jisx0213_comp_table0300_idx (euc_jisx0213_comp_table02e9_idx+euc_jisx0213_comp_table02e9_len)
115 #define euc_jisx0213_comp_table0300_len 5
116   { 0xa9dc, 0xabc4 }, /* 0x12B44 = 0x1295C U+0300 */
117   { 0xabb8, 0xabc8 }, /* 0x12B48 = 0x12B38 U+0300 */
118   { 0xabb7, 0xabca }, /* 0x12B4A = 0x12B37 U+0300 */
119   { 0xabb0, 0xabcc }, /* 0x12B4C = 0x12B30 U+0300 */
120   { 0xabc3, 0xabce }, /* 0x12B4E = 0x12B43 U+0300 */
121 #define euc_jisx0213_comp_table0301_idx (euc_jisx0213_comp_table0300_idx+euc_jisx0213_comp_table0300_len)
122 #define euc_jisx0213_comp_table0301_len 4
123   { 0xabb8, 0xabc9 }, /* 0x12B49 = 0x12B38 U+0301 */
124   { 0xabb7, 0xabcb }, /* 0x12B4B = 0x12B37 U+0301 */
125   { 0xabb0, 0xabcd }, /* 0x12B4D = 0x12B30 U+0301 */
126   { 0xabc3, 0xabcf }, /* 0x12B4F = 0x12B43 U+0301 */
127 #define euc_jisx0213_comp_table309a_idx (euc_jisx0213_comp_table0301_idx+euc_jisx0213_comp_table0301_len)
128 #define euc_jisx0213_comp_table309a_len 14
129   { 0xa4ab, 0xa4f7 }, /* 0x12477 = 0x1242B U+309A */
130   { 0xa4ad, 0xa4f8 }, /* 0x12478 = 0x1242D U+309A */
131   { 0xa4af, 0xa4f9 }, /* 0x12479 = 0x1242F U+309A */
132   { 0xa4b1, 0xa4fa }, /* 0x1247A = 0x12431 U+309A */
133   { 0xa4b3, 0xa4fb }, /* 0x1247B = 0x12433 U+309A */
134   { 0xa5ab, 0xa5f7 }, /* 0x12577 = 0x1252B U+309A */
135   { 0xa5ad, 0xa5f8 }, /* 0x12578 = 0x1252D U+309A */
136   { 0xa5af, 0xa5f9 }, /* 0x12579 = 0x1252F U+309A */
137   { 0xa5b1, 0xa5fa }, /* 0x1257A = 0x12531 U+309A */
138   { 0xa5b3, 0xa5fb }, /* 0x1257B = 0x12533 U+309A */
139   { 0xa5bb, 0xa5fc }, /* 0x1257C = 0x1253B U+309A */
140   { 0xa5c4, 0xa5fd }, /* 0x1257D = 0x12544 U+309A */
141   { 0xa5c8, 0xa5fe }, /* 0x1257E = 0x12548 U+309A */
142   { 0xa6f5, 0xa6f8 }, /* 0x12678 = 0x12675 U+309A */
143 };
144
145 static int
146 euc_jisx0213_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
147 {
148   int count = 0;
149   unsigned short lasttwo = conv->ostate;
150
151   if (lasttwo) {
152     /* Attempt to combine the last character with this one. */
153     unsigned int idx;
154     unsigned int len;
155
156     if (wc == 0x02e5)
157       idx = euc_jisx0213_comp_table02e5_idx,
158       len = euc_jisx0213_comp_table02e5_len;
159     else if (wc == 0x02e9)
160       idx = euc_jisx0213_comp_table02e9_idx,
161       len = euc_jisx0213_comp_table02e9_len;
162     else if (wc == 0x0300)
163       idx = euc_jisx0213_comp_table0300_idx,
164       len = euc_jisx0213_comp_table0300_len;
165     else if (wc == 0x0301)
166       idx = euc_jisx0213_comp_table0301_idx,
167       len = euc_jisx0213_comp_table0301_len;
168     else if (wc == 0x309a)
169       idx = euc_jisx0213_comp_table309a_idx,
170       len = euc_jisx0213_comp_table309a_len;
171     else
172       goto not_combining;
173
174     do
175       if (euc_jisx0213_comp_table_data[idx].base == lasttwo)
176         break;
177     while (++idx, --len > 0);
178
179     if (len > 0) {
180       /* Output the combined character. */
181       if (n >= 2) {
182         lasttwo = euc_jisx0213_comp_table_data[idx].composed;
183         r[0] = (lasttwo >> 8) & 0xff;
184         r[1] = lasttwo & 0xff;
185         conv->ostate = 0;
186         return 2;
187       } else
188         return RET_TOOSMALL;
189     }
190
191   not_combining:
192     /* Output the buffered character. */
193     if (n < 2)
194       return RET_TOOSMALL;
195     r[0] = (lasttwo >> 8) & 0xff;
196     r[1] = lasttwo & 0xff;
197     r += 2;
198     count = 2;
199   }
200
201   if (wc < 0x80) {
202     /* Plain ASCII character. */
203     if (n > count) {
204       r[0] = (unsigned char) wc;
205       conv->ostate = 0;
206       return count+1;
207     } else
208       return RET_TOOSMALL;
209   } else if (wc >= 0xff61 && wc <= 0xff9f) {
210     /* Half-width katakana. */
211     if (n >= count+2) {
212       r[0] = 0x8e;
213       r[1] = wc - 0xfec0;
214       conv->ostate = 0;
215       return count+2;
216     } else
217       return RET_TOOSMALL;
218   } else {
219     unsigned short jch = ucs4_to_jisx0213(wc);
220     if (jch != 0) {
221       if (jch & 0x0080) {
222         /* A possible match in comp_table_data. We have to buffer it. */
223         /* We know it's a JISX 0213 plane 1 character. */
224         if (jch & 0x8000) abort();
225         conv->ostate = jch | 0x8080;
226         return count+0;
227       }
228       if (jch & 0x8000) {
229         /* JISX 0213 plane 2. */
230         if (n >= count+3) {
231           r[0] = 0x8f;
232           r[1] = (jch >> 8) | 0x80;
233           r[2] = (jch & 0xff) | 0x80;
234           conv->ostate = 0;
235           return count+3;
236         } else
237           return RET_TOOSMALL;
238       } else {
239         /* JISX 0213 plane 1. */
240         if (n >= count+2) {
241           r[0] = (jch >> 8) | 0x80;
242           r[1] = (jch & 0xff) | 0x80;
243           conv->ostate = 0;
244           return count+2;
245         } else
246           return RET_TOOSMALL;
247       }
248     }
249     return RET_ILUNI;
250   }
251 }
252
253 static int
254 euc_jisx0213_reset (conv_t conv, unsigned char *r, size_t n)
255 {
256   state_t lasttwo = conv->ostate;
257
258   if (lasttwo) {
259     if (n < 2)
260       return RET_TOOSMALL;
261     r[0] = (lasttwo >> 8) & 0xff;
262     r[1] = lasttwo & 0xff;
263     /* conv->ostate = 0; will be done by the caller */
264     return 2;
265   } else
266     return 0;
267 }