Ruby  1.9.3p392(2013-02-22revision39386)
big5.c
Go to the documentation of this file.
1 /**********************************************************************
2  big5.c - Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5  * Copyright (c) 2002-2007 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include "regenc.h"
31 
32 static const int EncLen_BIG5[] = {
33  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
34  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
35  1, 1, 1,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1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
78  /* 6 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
79  /* 7 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
80  /* 8 */ 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
81  /* 9 */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
82  /* A */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
83  /* B */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
84  /* C */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
85  /* D */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
86  /* E */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
87  /* F */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1,
88 };
89 
90 typedef enum { FAILURE = -2, ACCEPT = -1, S0 = 0, S1 } state_t;
91 #define A ACCEPT
92 #define F FAILURE
93 static const signed char trans[][0x100] = {
94  { /* S0 0 1 2 3 4 5 6 7 8 9 a b c d e f */
95  /* 0 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
96  /* 1 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
97  /* 2 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
98  /* 3 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
99  /* 4 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
100  /* 5 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
101  /* 6 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
102  /* 7 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
103  /* 8 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
104  /* 9 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
105  /* a */ F, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
106  /* b */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
107  /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
108  /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
109  /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
110  /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, F
111  },
112  { /* S1 0 1 2 3 4 5 6 7 8 9 a b c d e f */
113  /* 0 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
114  /* 1 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
115  /* 2 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
116  /* 3 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
117  /* 4 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
118  /* 5 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
119  /* 6 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
120  /* 7 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, F,
121  /* 8 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
122  /* 9 */ F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
123  /* a */ F, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
124  /* b */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
125  /* c */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
126  /* d */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
127  /* e */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
128  /* f */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, F
129  },
130  { /* S2 0 1 2 3 4 5 6 7 8 9 a b c d e f */
131  /* 0 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
132  /* 1 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
133  /* 2 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
134  /* 3 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
135  /* 4 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
136  /* 5 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
137  /* 6 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
138  /* 7 */ A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A,
139  /* 8 */ F, F, F, F, F, F, F, 1, 1, 1, 1, 1, 1, 1, 1, 1,
140  /* 9 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
141  /* a */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
142  /* b */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
143  /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
144  /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
145  /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
146  /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, F
147  }
148 };
149 #undef A
150 #undef F
151 
152 static int
153 big5_mbc_enc_len0(const UChar* p, const UChar* e, int tridx, const int tbl[])
154 {
155  int firstbyte = *p++;
156  state_t s = trans[tridx][firstbyte];
157 #define RETURN(n) \
158  return s == ACCEPT ? ONIGENC_CONSTRUCT_MBCLEN_CHARFOUND(n) : \
159  ONIGENC_CONSTRUCT_MBCLEN_INVALID()
160  if (s < 0) RETURN(1);
161  if (p == e) return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(tbl[firstbyte]-1);
162  s = trans[s][*p++];
163  RETURN(2);
164 #undef RETURN
165 }
166 
167 static int
168 big5_mbc_enc_len(const UChar* p, const UChar* e, OnigEncoding enc ARG_UNUSED)
169 {
170  return big5_mbc_enc_len0(p, e, 0, EncLen_BIG5);
171 }
172 
173 static int
174 big5_hkscs_mbc_enc_len(const UChar* p, const UChar* e, OnigEncoding enc ARG_UNUSED)
175 {
176  return big5_mbc_enc_len0(p, e, 2, EncLen_BIG5_HKSCS);
177 }
178 
179 static int
180 big5_uao_mbc_enc_len(const UChar* p, const UChar* e, OnigEncoding enc ARG_UNUSED)
181 {
182  return big5_mbc_enc_len0(p, e, 2, EncLen_BIG5_UAO);
183 }
184 
185 static OnigCodePoint
186 big5_mbc_to_code(const UChar* p, const UChar* end, OnigEncoding enc)
187 {
188  return onigenc_mbn_mbc_to_code(enc, p, end);
189 }
190 
191 static int
193 {
194  return onigenc_mb2_code_to_mbc(enc, code, buf);
195 }
196 
197 static int
198 big5_mbc_case_fold(OnigCaseFoldType flag, const UChar** pp, const UChar* end,
199  UChar* lower, OnigEncoding enc)
200 {
201  return onigenc_mbn_mbc_case_fold(enc, flag,
202  pp, end, lower);
203 }
204 
205 #if 0
206 static int
207 big5_is_mbc_ambiguous(OnigCaseFoldType flag,
208  const UChar** pp, const UChar* end, OnigEncoding enc)
209 {
210  return onigenc_mbn_is_mbc_ambiguous(enc, flag, pp, end);
211 }
212 #endif
213 
214 static int
215 big5_is_code_ctype(OnigCodePoint code, unsigned int ctype, OnigEncoding enc)
216 {
217  return onigenc_mb2_is_code_ctype(enc, code, ctype);
218 }
219 
220 static const char BIG5_CAN_BE_TRAIL_TABLE[256] = {
221  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
222  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
223  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
224  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
225  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
226  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
227  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
228  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
229  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
230  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
231  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
232  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
233  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
234  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
235  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
236  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0
237 };
238 
239 #define BIG5_HKSCS_P(enc) ((enc)->precise_mbc_enc_len == big5_hkscs_mbc_enc_len)
240 #define BIG5_UAO_P(enc) ((enc)->precise_mbc_enc_len == big5_uao_mbc_enc_len)
241 
242 #define BIG5_ISMB_FIRST(byte) ( \
243  BIG5_HKSCS_P(enc) ? EncLen_BIG5_HKSCS[byte] > 1 : \
244  EncLen_BIG5[byte] > 1 \
245  )
246 #define BIG5_ISMB_TRAIL(byte) BIG5_CAN_BE_TRAIL_TABLE[(byte)]
247 
248 static UChar*
249 big5_left_adjust_char_head(const UChar* start, const UChar* s, const UChar* end, OnigEncoding enc)
250 {
251  const UChar *p;
252  int len;
253 
254  if (s <= start) return (UChar* )s;
255  p = s;
256 
257  if (BIG5_ISMB_TRAIL(*p)) {
258  while (p > start) {
259  if (! BIG5_ISMB_FIRST(*--p)) {
260  p++;
261  break;
262  }
263  }
264  }
265  len = enclen(enc, p, end);
266  if (p + len > s) return (UChar* )p;
267  p += len;
268  return (UChar* )(p + ((s - p) & ~1));
269 }
270 
271 static int
272 big5_is_allowed_reverse_match(const UChar* s, const UChar* end ARG_UNUSED, OnigEncoding enc ARG_UNUSED)
273 {
274  const UChar c = *s;
275 
276  return (BIG5_ISMB_TRAIL(c) ? FALSE : TRUE);
277 }
278 
279 /*
280  * Name: Big5 (preferred MIME name)
281  * MIBenum: 2026
282  * Source: Chinese for Taiwan Multi-byte set.
283  * PCL Symbol Set Id: 18T
284  * Alias: csBig5
285  */
286 OnigEncodingDefine(big5, BIG5) = {
288  "Big5", /* name */
289  2, /* max enc length */
290  1, /* min enc length */
303 };
304 
305 /*
306  * Name: CP950
307  * Source: http://msdn.microsoft.com/en-us/goglobal/cc305155.aspx
308  */
309 ENC_REPLICATE("CP950", "Big5")
310 
311 /*
312  * Name: Big5-HKSCS
313  * MIBenum: 2101
314  * Source: http://www.iana.org/assignments/charset-reg/Big5-HKSCS
315  * Source: http://www.ogcio.gov.hk/ccli/eng/hkscs/mapping_table_2008.html
316  * Alias: None
317  */
318 OnigEncodingDefine(big5_hkscs, BIG5_HKSCS) = {
320  "Big5-HKSCS", /* name */
321  2, /* max enc length */
322  1, /* min enc length */
335 };
336 ENC_ALIAS("Big5-HKSCS:2008", "Big5-HKSCS")
337 
338 /*
339  * Name: CP951
340  * Source: http://www.microsoft.com/hk/hkscs/default.aspx
341  * Source: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=0e6f5ac8-7baa-4571-b8e8-78b3b776afd7&DisplayLang=en
342  * Source: http://blogs.msdn.com/b/shawnste/archive/2007/03/12/cp-951-hkscs.aspx
343  */
344 ENC_REPLICATE("CP951", "Big5-HKSCS")
345 
346 /*
347  * Name: Big5-UAO [NOT registered by IANA!]
348  * Source: http://moztw.org/docs/big5/table/big5_2003-b2u.txt
349  */
350 OnigEncodingDefine(big5_uao, BIG5_UAO) = {
352  "Big5-UAO", /* name */
353  2, /* max enc length */
354  1, /* min enc length */
367 };
368