1 /**********************************************************************
2 euc_jp.c - Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5 * Copyright (c) 2002-2016 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 "regint.h"
31
32 #define eucjp_islead(c) ((UChar )((c) - 0xa1) > 0xfe - 0xa1)
33
34 static const int EncLen_EUCJP[] = {
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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3,
44 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
45 1, 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, 2,
49 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
50 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
51 };
52
53 static int
mbc_enc_len(const UChar * p)54 mbc_enc_len(const UChar* p)
55 {
56 return EncLen_EUCJP[*p];
57 }
58
59 static int
is_valid_mbc_string(const UChar * p,const UChar * end)60 is_valid_mbc_string(const UChar* p, const UChar* end)
61 {
62 while (p < end) {
63 if (*p < 0x80) {
64 p++;
65 }
66 else if (*p > 0xa0) {
67 if (*p == 0xff) return FALSE;
68 p++;
69 if (p >= end) return FALSE;
70 if (*p < 0xa1 || *p == 0xff) return FALSE;
71 p++;
72 }
73 else if (*p == 0x8e) {
74 p++;
75 if (p >= end) return FALSE;
76 if (*p < 0xa1 || *p > 0xdf) return FALSE;
77 p++;
78 }
79 else if (*p == 0x8f) {
80 p++;
81 if (p >= end) return FALSE;
82 if (*p < 0xa1 || *p == 0xff) return FALSE;
83 p++;
84 if (p >= end) return FALSE;
85 if (*p < 0xa1 || *p == 0xff) return FALSE;
86 p++;
87 }
88 else
89 return FALSE;
90 }
91
92 return TRUE;
93 }
94
95 static OnigCodePoint
mbc_to_code(const UChar * p,const UChar * end)96 mbc_to_code(const UChar* p, const UChar* end)
97 {
98 int c, i, len;
99 OnigCodePoint n;
100
101 len = enclen(ONIG_ENCODING_EUC_JP, p);
102 n = (OnigCodePoint )*p++;
103 if (len == 1) return n;
104
105 for (i = 1; i < len; i++) {
106 if (p >= end) break;
107 c = *p++;
108 n <<= 8; n += c;
109 }
110 return n;
111 }
112
113 static int
code_to_mbclen(OnigCodePoint code)114 code_to_mbclen(OnigCodePoint code)
115 {
116 if (ONIGENC_IS_CODE_ASCII(code)) return 1;
117 else if ((code & 0xff0000) != 0) return 3;
118 else if ((code & 0xff00) != 0) return 2;
119 else
120 return ONIGERR_INVALID_CODE_POINT_VALUE;
121 }
122
123 #if 0
124 static int
125 code_to_mbc_first(OnigCodePoint code)
126 {
127 int first;
128
129 if ((code & 0xff0000) != 0) {
130 first = (code >> 16) & 0xff;
131 }
132 else if ((code & 0xff00) != 0) {
133 first = (code >> 8) & 0xff;
134 }
135 else {
136 return (int )code;
137 }
138 return first;
139 }
140 #endif
141
142 static int
code_to_mbc(OnigCodePoint code,UChar * buf)143 code_to_mbc(OnigCodePoint code, UChar *buf)
144 {
145 UChar *p = buf;
146
147 if ((code & 0xff0000) != 0) *p++ = (UChar )(((code >> 16) & 0xff));
148 if ((code & 0xff00) != 0) *p++ = (UChar )(((code >> 8) & 0xff));
149 *p++ = (UChar )(code & 0xff);
150
151 #if 1
152 if (enclen(ONIG_ENCODING_EUC_JP, buf) != (p - buf))
153 return ONIGERR_INVALID_CODE_POINT_VALUE;
154 #endif
155 return p - buf;
156 }
157
158 static int
mbc_case_fold(OnigCaseFoldType flag ARG_UNUSED,const UChar ** pp,const UChar * end ARG_UNUSED,UChar * lower)159 mbc_case_fold(OnigCaseFoldType flag ARG_UNUSED,
160 const UChar** pp, const UChar* end ARG_UNUSED, UChar* lower)
161 {
162 int len;
163 const UChar* p = *pp;
164
165 if (ONIGENC_IS_MBC_ASCII(p)) {
166 *lower = ONIGENC_ASCII_CODE_TO_LOWER_CASE(*p);
167 (*pp)++;
168 return 1;
169 }
170 else {
171 int i;
172
173 len = enclen(ONIG_ENCODING_EUC_JP, p);
174 for (i = 0; i < len; i++) {
175 *lower++ = *p++;
176 }
177 (*pp) += len;
178 return len; /* return byte length of converted char to lower */
179 }
180 }
181
182 static UChar*
left_adjust_char_head(const UChar * start,const UChar * s)183 left_adjust_char_head(const UChar* start, const UChar* s)
184 {
185 /* In this encoding
186 mb-trail bytes doesn't mix with single bytes.
187 */
188 const UChar *p;
189 int len;
190
191 if (s <= start) return (UChar* )s;
192 p = s;
193
194 while (!eucjp_islead(*p) && p > start) p--;
195 len = enclen(ONIG_ENCODING_EUC_JP, p);
196 if (p + len > s) return (UChar* )p;
197 p += len;
198 return (UChar* )(p + ((s - p) & ~1));
199 }
200
201 static int
is_allowed_reverse_match(const UChar * s,const UChar * end ARG_UNUSED)202 is_allowed_reverse_match(const UChar* s, const UChar* end ARG_UNUSED)
203 {
204 const UChar c = *s;
205 if (c <= 0x7e || c == 0x8e || c == 0x8f)
206 return TRUE;
207 else
208 return FALSE;
209 }
210
211
212 static const OnigCodePoint CR_Hiragana[] = {
213 1,
214 0xa4a1, 0xa4f3
215 }; /* CR_Hiragana */
216
217 static const OnigCodePoint CR_Katakana[] = {
218 3,
219 0xa5a1, 0xa5f6,
220 0xaaa6, 0xaaaf,
221 0xaab1, 0xaadd
222 }; /* CR_Katakana */
223
224 static const OnigCodePoint* PropertyList[] = {
225 CR_Hiragana,
226 CR_Katakana
227 };
228
229 static int
property_name_to_ctype(OnigEncoding enc,UChar * p,UChar * end)230 property_name_to_ctype(OnigEncoding enc, UChar* p, UChar* end)
231 {
232 struct PropertyNameCtype* pc;
233 int len = end - p;
234 char q[32];
235
236 if (len < sizeof(q) - 1) {
237 xmemcpy(q, p, (size_t )len);
238 q[len] = '\0';
239 pc = euc_jp_lookup_property_name(q, len);
240 if (pc != 0)
241 return pc->ctype;
242 }
243
244 return ONIGERR_INVALID_CHAR_PROPERTY_NAME;
245 }
246
247 static int
is_code_ctype(OnigCodePoint code,unsigned int ctype)248 is_code_ctype(OnigCodePoint code, unsigned int ctype)
249 {
250 if (ctype <= ONIGENC_MAX_STD_CTYPE) {
251 if (code < 128)
252 return ONIGENC_IS_ASCII_CODE_CTYPE(code, ctype);
253 else {
254 if (CTYPE_IS_WORD_GRAPH_PRINT(ctype)) {
255 return (code_to_mbclen(code) > 1 ? TRUE : FALSE);
256 }
257 }
258 }
259 else {
260 ctype -= (ONIGENC_MAX_STD_CTYPE + 1);
261 if (ctype >= (unsigned int )(sizeof(PropertyList)/sizeof(PropertyList[0])))
262 return ONIGERR_TYPE_BUG;
263
264 return onig_is_in_code_range((UChar* )PropertyList[ctype], code);
265 }
266
267 return FALSE;
268 }
269
270 static int
get_ctype_code_range(OnigCtype ctype,OnigCodePoint * sb_out,const OnigCodePoint * ranges[])271 get_ctype_code_range(OnigCtype ctype, OnigCodePoint* sb_out,
272 const OnigCodePoint* ranges[])
273 {
274 if (ctype <= ONIGENC_MAX_STD_CTYPE) {
275 return ONIG_NO_SUPPORT_CONFIG;
276 }
277 else {
278 *sb_out = 0x80;
279
280 ctype -= (ONIGENC_MAX_STD_CTYPE + 1);
281 if (ctype >= (OnigCtype )sizeof(PropertyList)/sizeof(PropertyList[0]))
282 return ONIGERR_TYPE_BUG;
283
284 *ranges = PropertyList[ctype];
285 return 0;
286 }
287 }
288
289
290 OnigEncodingType OnigEncodingEUC_JP = {
291 mbc_enc_len,
292 "EUC-JP", /* name */
293 3, /* max enc length */
294 1, /* min enc length */
295 onigenc_is_mbc_newline_0x0a,
296 mbc_to_code,
297 code_to_mbclen,
298 code_to_mbc,
299 mbc_case_fold,
300 onigenc_ascii_apply_all_case_fold,
301 onigenc_ascii_get_case_fold_codes_by_str,
302 property_name_to_ctype,
303 is_code_ctype,
304 get_ctype_code_range,
305 left_adjust_char_head,
306 is_allowed_reverse_match,
307 NULL, /* init */
308 NULL, /* is_initialized */
309 is_valid_mbc_string
310 };
311