1 /**********************************************************************
2   euc_jp.c -  Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5  * Copyright (c) 2002-2020  K.Kosako
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) {
118     if (EncLen_EUCJP[(int )(code >> 16) & 0xff] == 3)
119       return 3;
120   }
121   else if ((code & 0xff00) != 0) {
122     if (EncLen_EUCJP[(int )(code >> 8) & 0xff] == 2)
123       return 2;
124   }
125   else if (code < 256) {
126     if (EncLen_EUCJP[(int )(code & 0xff)] == 1)
127       return 1;
128   }
129 
130   return ONIGERR_INVALID_CODE_POINT_VALUE;
131 }
132 
133 static int
code_to_mbc(OnigCodePoint code,UChar * buf)134 code_to_mbc(OnigCodePoint code, UChar *buf)
135 {
136   UChar *p = buf;
137 
138   if ((code & 0xff0000) != 0) {
139     *p++ = (UChar )(((code >> 16) & 0xff));
140     *p++ = (UChar )(((code >>  8) & 0xff));
141   }
142   else if ((code & 0xff00) != 0)
143     *p++ = (UChar )(((code >>  8) & 0xff));
144 
145   *p++ = (UChar )(code & 0xff);
146 
147 #if 1
148   if (enclen(ONIG_ENCODING_EUC_JP, buf) != (p - buf))
149     return ONIGERR_INVALID_CODE_POINT_VALUE;
150 #endif
151   return (int )(p - buf);
152 }
153 
154 static int
mbc_case_fold(OnigCaseFoldType flag ARG_UNUSED,const UChar ** pp,const UChar * end ARG_UNUSED,UChar * lower)155 mbc_case_fold(OnigCaseFoldType flag ARG_UNUSED,
156               const UChar** pp, const UChar* end ARG_UNUSED, UChar* lower)
157 {
158   int len;
159   const UChar* p = *pp;
160 
161   if (ONIGENC_IS_MBC_ASCII(p)) {
162     *lower = ONIGENC_ASCII_CODE_TO_LOWER_CASE(*p);
163     (*pp)++;
164     return 1;
165   }
166   else {
167     int i;
168 
169     len = enclen(ONIG_ENCODING_EUC_JP, p);
170     for (i = 0; i < len; i++) {
171       *lower++ = *p++;
172     }
173     (*pp) += len;
174     return len; /* return byte length of converted char to lower */
175   }
176 }
177 
178 static UChar*
left_adjust_char_head(const UChar * start,const UChar * s)179 left_adjust_char_head(const UChar* start, const UChar* s)
180 {
181   /* In this encoding
182      mb-trail bytes doesn't mix with single bytes.
183   */
184   const UChar *p;
185   int len;
186 
187   if (s <= start) return (UChar* )s;
188   p = s;
189 
190   while (!eucjp_islead(*p) && p > start) p--;
191   len = enclen(ONIG_ENCODING_EUC_JP, p);
192   if (p + len > s) return (UChar* )p;
193   p += len;
194   return (UChar* )(p + ((s - p) & ~1));
195 }
196 
197 static int
is_allowed_reverse_match(const UChar * s,const UChar * end ARG_UNUSED)198 is_allowed_reverse_match(const UChar* s, const UChar* end ARG_UNUSED)
199 {
200   const UChar c = *s;
201   if (c <= 0x7e || c == 0x8e || c == 0x8f)
202     return TRUE;
203   else
204     return FALSE;
205 }
206 
207 
208 static const OnigCodePoint CR_Hiragana[] = {
209   1,
210   0xa4a1, 0xa4f3
211 }; /* CR_Hiragana */
212 
213 static const OnigCodePoint CR_Katakana[] = {
214   3,
215   0xa5a1, 0xa5f6,
216   0xaaa6, 0xaaaf,
217   0xaab1, 0xaadd
218 }; /* CR_Katakana */
219 
220 static const OnigCodePoint* PropertyList[] = {
221   CR_Hiragana,
222   CR_Katakana
223 };
224 
225 static int
property_name_to_ctype(OnigEncoding enc,UChar * p,UChar * end)226 property_name_to_ctype(OnigEncoding enc, UChar* p, UChar* end)
227 {
228   struct PropertyNameCtype* pc;
229   int len = (int )(end - p);
230   char q[32];
231 
232   if (len < sizeof(q) - 1) {
233     xmemcpy(q, p, (size_t )len);
234     q[len] = '\0';
235     pc = onigenc_euc_jp_lookup_property_name(q, len);
236     if (pc != 0)
237       return pc->ctype;
238   }
239 
240   return ONIGERR_INVALID_CHAR_PROPERTY_NAME;
241 }
242 
243 static int
is_code_ctype(OnigCodePoint code,unsigned int ctype)244 is_code_ctype(OnigCodePoint code, unsigned int ctype)
245 {
246   if (ctype <= ONIGENC_MAX_STD_CTYPE) {
247     if (code < 128)
248       return ONIGENC_IS_ASCII_CODE_CTYPE(code, ctype);
249     else {
250       if (CTYPE_IS_WORD_GRAPH_PRINT(ctype)) {
251         return (code_to_mbclen(code) > 1 ? TRUE : FALSE);
252       }
253     }
254   }
255   else {
256     ctype -= (ONIGENC_MAX_STD_CTYPE + 1);
257     if (ctype >= (unsigned int )(sizeof(PropertyList)/sizeof(PropertyList[0])))
258       return ONIGERR_TYPE_BUG;
259 
260     return onig_is_in_code_range((UChar* )PropertyList[ctype], code);
261   }
262 
263   return FALSE;
264 }
265 
266 static int
get_ctype_code_range(OnigCtype ctype,OnigCodePoint * sb_out,const OnigCodePoint * ranges[])267 get_ctype_code_range(OnigCtype ctype, OnigCodePoint* sb_out,
268                      const OnigCodePoint* ranges[])
269 {
270   if (ctype <= ONIGENC_MAX_STD_CTYPE) {
271     return ONIG_NO_SUPPORT_CONFIG;
272   }
273   else {
274     *sb_out = 0x80;
275 
276     ctype -= (ONIGENC_MAX_STD_CTYPE + 1);
277     if (ctype >= (OnigCtype )sizeof(PropertyList)/sizeof(PropertyList[0]))
278       return ONIGERR_TYPE_BUG;
279 
280     *ranges = PropertyList[ctype];
281     return 0;
282   }
283 }
284 
285 
286 OnigEncodingType OnigEncodingEUC_JP = {
287   mbc_enc_len,
288   "EUC-JP",   /* name */
289   3,          /* max enc length */
290   1,          /* min enc length */
291   onigenc_is_mbc_newline_0x0a,
292   mbc_to_code,
293   code_to_mbclen,
294   code_to_mbc,
295   mbc_case_fold,
296   onigenc_ascii_apply_all_case_fold,
297   onigenc_ascii_get_case_fold_codes_by_str,
298   property_name_to_ctype,
299   is_code_ctype,
300   get_ctype_code_range,
301   left_adjust_char_head,
302   is_allowed_reverse_match,
303   NULL, /* init */
304   NULL, /* is_initialized */
305   is_valid_mbc_string,
306   ENC_FLAG_ASCII_COMPATIBLE|ENC_FLAG_SKIP_OFFSET_1_OR_0,
307   0, 0
308 };
309