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