1 /**********************************************************************
2   sjis.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 "regint.h"
31 
32 static const int EncLen_SJIS[] = {
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,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 1, 1, 1, 1, 1, 1,
59   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
60   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
61   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
62   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
63   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
64   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
65   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
66   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
67   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0
68 };
69 
70 #define SJIS_ISMB_FIRST(byte)  (EncLen_SJIS[byte] > 1)
71 #define SJIS_ISMB_TRAIL(byte)  SJIS_CAN_BE_TRAIL_TABLE[(byte)]
72 
73 static int
mbc_enc_len(const UChar * p)74 mbc_enc_len(const UChar* p)
75 {
76   return EncLen_SJIS[*p];
77 }
78 
79 static int
is_valid_mbc_string(const UChar * p,const UChar * end)80 is_valid_mbc_string(const UChar* p, const UChar* end)
81 {
82   while (p < end) {
83     if (*p < 0x80) {
84       p++;
85     }
86     else if (*p < 0xa1) {
87       if (*p == 0xa0 || *p == 0x80)
88 	return FALSE;
89       p++;
90       if (p >= end) return FALSE;
91       if (*p < 0x40 || *p > 0xfc || *p == 0x7f)
92 	return FALSE;
93       p++;
94     }
95     else if (*p < 0xe0) {
96       p++;
97     }
98     else if (*p < 0xfd) {
99       p++;
100       if (p >= end) return FALSE;
101       if (*p < 0x40 || *p > 0xfc || *p == 0x7f)
102 	return FALSE;
103       p++;
104     }
105     else
106       return FALSE;
107   }
108 
109   return TRUE;
110 }
111 
112 static int
code_to_mbclen(OnigCodePoint code)113 code_to_mbclen(OnigCodePoint code)
114 {
115   if (code < 256) {
116     return EncLen_SJIS[(int )code] == 1;
117   }
118   else if (code <= 0xffff) {
119     return 2;
120   }
121   else
122     return ONIGERR_INVALID_CODE_POINT_VALUE;
123 }
124 
125 static OnigCodePoint
mbc_to_code(const UChar * p,const UChar * end)126 mbc_to_code(const UChar* p, const UChar* end)
127 {
128   int c, i, len;
129   OnigCodePoint n;
130 
131   len = enclen(ONIG_ENCODING_SJIS, p);
132   c = *p++;
133   n = c;
134   if (len == 1) return n;
135 
136   for (i = 1; i < len; i++) {
137     if (p >= end) break;
138     c = *p++;
139     n <<= 8;  n += c;
140   }
141   return n;
142 }
143 
144 static int
code_to_mbc(OnigCodePoint code,UChar * buf)145 code_to_mbc(OnigCodePoint code, UChar *buf)
146 {
147   UChar *p = buf;
148 
149   if ((code & 0xff00) != 0) *p++ = (UChar )(((code >>  8) & 0xff));
150   *p++ = (UChar )(code & 0xff);
151 
152 #if 0
153   if (enclen(ONIG_ENCODING_SJIS, buf) != (p - buf))
154     return REGERR_INVALID_CODE_POINT_VALUE;
155 #endif
156   return (int )(p - buf);
157 }
158 
159 static int
mbc_case_fold(OnigCaseFoldType flag ARG_UNUSED,const UChar ** pp,const UChar * end ARG_UNUSED,UChar * lower)160 mbc_case_fold(OnigCaseFoldType flag ARG_UNUSED,
161 	      const UChar** pp, const UChar* end ARG_UNUSED, UChar* lower)
162 {
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     int len = enclen(ONIG_ENCODING_SJIS, p);
173 
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 #if 0
183 static int
184 is_mbc_ambiguous(OnigCaseFoldType flag,
185 		 const UChar** pp, const UChar* end)
186 {
187   return onigenc_mbn_is_mbc_ambiguous(ONIG_ENCODING_SJIS, flag, pp, end);
188 
189 }
190 #endif
191 
192 #if 0
193 static int
194 is_code_ctype(OnigCodePoint code, unsigned int ctype)
195 {
196   if (code < 128)
197     return ONIGENC_IS_ASCII_CODE_CTYPE(code, ctype);
198   else {
199     if (CTYPE_IS_WORD_GRAPH_PRINT(ctype)) {
200       return (code_to_mbclen(code) > 1 ? TRUE : FALSE);
201     }
202   }
203 
204   return FALSE;
205 }
206 #endif
207 
208 static UChar*
left_adjust_char_head(const UChar * start,const UChar * s)209 left_adjust_char_head(const UChar* start, const UChar* s)
210 {
211   const UChar *p;
212   int len;
213 
214   if (s <= start) return (UChar* )s;
215   p = s;
216 
217   if (SJIS_ISMB_TRAIL(*p)) {
218     while (p > start) {
219       if (! SJIS_ISMB_FIRST(*--p)) {
220 	p++;
221 	break;
222       }
223     }
224   }
225   len = enclen(ONIG_ENCODING_SJIS, p);
226   if (p + len > s) return (UChar* )p;
227   p += len;
228   return (UChar* )(p + ((s - p) & ~1));
229 }
230 
231 static int
is_allowed_reverse_match(const UChar * s,const UChar * end ARG_UNUSED)232 is_allowed_reverse_match(const UChar* s, const UChar* end ARG_UNUSED)
233 {
234   const UChar c = *s;
235   return (SJIS_ISMB_TRAIL(c) ? FALSE : TRUE);
236 }
237 
238 
239 static const OnigCodePoint CR_Hiragana[] = {
240   1,
241   0x829f, 0x82f1
242 }; /* CR_Hiragana */
243 
244 static const OnigCodePoint CR_Katakana[] = {
245   4,
246   0x00a6, 0x00af,
247   0x00b1, 0x00dd,
248   0x8340, 0x837e,
249   0x8380, 0x8396,
250 }; /* CR_Katakana */
251 
252 static const OnigCodePoint* PropertyList[] = {
253   CR_Hiragana,
254   CR_Katakana
255 };
256 
257 
258 static int
property_name_to_ctype(OnigEncoding enc,UChar * p,UChar * end)259 property_name_to_ctype(OnigEncoding enc, UChar* p, UChar* end)
260 {
261   struct PropertyNameCtype* pc;
262   int len = (int )(end - p);
263   char q[32];
264 
265   if (len < sizeof(q) - 1) {
266     xmemcpy(q, p, (size_t )len);
267     q[len] = '\0';
268     pc = onigenc_sjis_lookup_property_name(q, len);
269     if (pc != 0)
270       return pc->ctype;
271   }
272 
273   return ONIGERR_INVALID_CHAR_PROPERTY_NAME;
274 }
275 
276 static int
is_code_ctype(OnigCodePoint code,unsigned int ctype)277 is_code_ctype(OnigCodePoint code, unsigned int ctype)
278 {
279   if (ctype <= ONIGENC_MAX_STD_CTYPE) {
280     if (code < 128)
281       return ONIGENC_IS_ASCII_CODE_CTYPE(code, ctype);
282     else {
283       if (CTYPE_IS_WORD_GRAPH_PRINT(ctype)) {
284 	return (code_to_mbclen(code) > 1 ? TRUE : FALSE);
285       }
286     }
287   }
288   else {
289     ctype -= (ONIGENC_MAX_STD_CTYPE + 1);
290     if (ctype >= (unsigned int )(sizeof(PropertyList)/sizeof(PropertyList[0])))
291       return ONIGERR_TYPE_BUG;
292 
293     return onig_is_in_code_range((UChar* )PropertyList[ctype], code);
294   }
295 
296   return FALSE;
297 }
298 
299 static int
get_ctype_code_range(OnigCtype ctype,OnigCodePoint * sb_out,const OnigCodePoint * ranges[])300 get_ctype_code_range(OnigCtype ctype, OnigCodePoint* sb_out,
301 		     const OnigCodePoint* ranges[])
302 {
303   if (ctype <= ONIGENC_MAX_STD_CTYPE) {
304     return ONIG_NO_SUPPORT_CONFIG;
305   }
306   else {
307     *sb_out = 0x80;
308 
309     ctype -= (ONIGENC_MAX_STD_CTYPE + 1);
310     if (ctype >= (OnigCtype )(sizeof(PropertyList)/sizeof(PropertyList[0])))
311       return ONIGERR_TYPE_BUG;
312 
313     *ranges = PropertyList[ctype];
314     return 0;
315   }
316 }
317 
318 OnigEncodingType OnigEncodingSJIS = {
319   mbc_enc_len,
320   "Shift_JIS",   /* name */
321   2,             /* max enc length */
322   1,             /* min enc length */
323   onigenc_is_mbc_newline_0x0a,
324   mbc_to_code,
325   code_to_mbclen,
326   code_to_mbc,
327   mbc_case_fold,
328   onigenc_ascii_apply_all_case_fold,
329   onigenc_ascii_get_case_fold_codes_by_str,
330   property_name_to_ctype,
331   is_code_ctype,
332   get_ctype_code_range,
333   left_adjust_char_head,
334   is_allowed_reverse_match,
335   NULL, /* init */
336   NULL, /* is_initialized */
337   is_valid_mbc_string,
338   ENC_FLAG_ASCII_COMPATIBLE|ENC_FLAG_SKIP_OFFSET_1_OR_0,
339   0, 0
340 };
341