1 /**********************************************************************
2   big5.c -  Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5  * Copyright (c) 2002-2018  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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
36   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
37   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
38   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
39   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
40   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
41   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
42   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
43   1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
44   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
45   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
46   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
47   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
48   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
49 };
50 
51 static int
big5_mbc_enc_len(const UChar * p)52 big5_mbc_enc_len(const UChar* p)
53 {
54   return EncLen_BIG5[*p];
55 }
56 
57 static int
is_valid_mbc_string(const UChar * p,const UChar * end)58 is_valid_mbc_string(const UChar* p, const UChar* end)
59 {
60   while (p < end) {
61     if (*p < 0x80) {
62       p++;
63     }
64     else if (*p < 0xa1) {
65       return FALSE;
66     }
67     else if (*p < 0xff) {
68       p++;
69       if (p >= end)  return FALSE;
70       if (*p < 0x40) return FALSE;
71       if (*p > 0x7e && *p < 0xa1) return FALSE;
72       if (*p == 0xff) return FALSE;
73       p++;
74     }
75     else
76       return FALSE;
77   }
78 
79   return TRUE;
80 }
81 
82 static OnigCodePoint
big5_mbc_to_code(const UChar * p,const UChar * end)83 big5_mbc_to_code(const UChar* p, const UChar* end)
84 {
85   return onigenc_mbn_mbc_to_code(ONIG_ENCODING_BIG5, p, end);
86 }
87 
88 static int
big5_code_to_mbc(OnigCodePoint code,UChar * buf)89 big5_code_to_mbc(OnigCodePoint code, UChar *buf)
90 {
91   return onigenc_mb2_code_to_mbc(ONIG_ENCODING_BIG5, code, buf);
92 }
93 
94 static int
big5_mbc_case_fold(OnigCaseFoldType flag,const UChar ** pp,const UChar * end,UChar * lower)95 big5_mbc_case_fold(OnigCaseFoldType flag, const UChar** pp, const UChar* end,
96                    UChar* lower)
97 {
98   return onigenc_mbn_mbc_case_fold(ONIG_ENCODING_BIG5, flag,
99                                    pp, end, lower);
100 }
101 
102 #if 0
103 static int
104 big5_is_mbc_ambiguous(OnigCaseFoldType flag,
105 		      const UChar** pp, const UChar* end)
106 {
107   return onigenc_mbn_is_mbc_ambiguous(ONIG_ENCODING_BIG5, flag, pp, end);
108 }
109 #endif
110 
111 static int
big5_is_code_ctype(OnigCodePoint code,unsigned int ctype)112 big5_is_code_ctype(OnigCodePoint code, unsigned int ctype)
113 {
114   return onigenc_mb2_is_code_ctype(ONIG_ENCODING_BIG5, code, ctype);
115 }
116 
117 static const char BIG5_CAN_BE_TRAIL_TABLE[256] = {
118   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
119   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
120   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
122   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
123   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
124   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
125   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
126   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
127   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
128   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
129   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
130   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
131   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
132   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
133   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0
134 };
135 
136 #define BIG5_ISMB_FIRST(byte)  (EncLen_BIG5[byte] > 1)
137 #define BIG5_ISMB_TRAIL(byte)  BIG5_CAN_BE_TRAIL_TABLE[(byte)]
138 
139 static UChar*
big5_left_adjust_char_head(const UChar * start,const UChar * s)140 big5_left_adjust_char_head(const UChar* start, const UChar* s)
141 {
142   const UChar *p;
143   int len;
144 
145   if (s <= start) return (UChar* )s;
146   p = s;
147 
148   if (BIG5_ISMB_TRAIL(*p)) {
149     while (p > start) {
150       if (! BIG5_ISMB_FIRST(*--p)) {
151 	p++;
152 	break;
153       }
154     }
155   }
156   len = enclen(ONIG_ENCODING_BIG5, p);
157   if (p + len > s) return (UChar* )p;
158   p += len;
159   return (UChar* )(p + ((s - p) & ~1));
160 }
161 
162 static int
big5_is_allowed_reverse_match(const UChar * s,const UChar * end ARG_UNUSED)163 big5_is_allowed_reverse_match(const UChar* s, const UChar* end ARG_UNUSED)
164 {
165   const UChar c = *s;
166 
167   return (BIG5_ISMB_TRAIL(c) ? FALSE : TRUE);
168 }
169 
170 OnigEncodingType OnigEncodingBIG5 = {
171   big5_mbc_enc_len,
172   "Big5",     /* name */
173   2,          /* max enc length */
174   1,          /* min enc length */
175   onigenc_is_mbc_newline_0x0a,
176   big5_mbc_to_code,
177   onigenc_mb2_code_to_mbclen,
178   big5_code_to_mbc,
179   big5_mbc_case_fold,
180   onigenc_ascii_apply_all_case_fold,
181   onigenc_ascii_get_case_fold_codes_by_str,
182   onigenc_minimum_property_name_to_ctype,
183   big5_is_code_ctype,
184   onigenc_not_support_get_ctype_code_range,
185   big5_left_adjust_char_head,
186   big5_is_allowed_reverse_match,
187   NULL, /* init */
188   NULL, /* is_initialized */
189   is_valid_mbc_string,
190   ENC_FLAG_ASCII_COMPATIBLE|ENC_FLAG_SKIP_OFFSET_1,
191   0, 0
192 };
193