1 /**********************************************************************
2 euc_tw.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 "regenc.h"
31
32 static const int EncLen_EUCTW[] = {
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, 4, 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
euctw_mbc_enc_len(const UChar * p)52 euctw_mbc_enc_len(const UChar* p)
53 {
54 return EncLen_EUCTW[*p];
55 }
56
57 static int
euctw_code_to_mbclen(OnigCodePoint code)58 euctw_code_to_mbclen(OnigCodePoint code)
59 {
60 if ((code & 0xff000000) != 0) {
61 if (EncLen_EUCTW[(int )(code >> 24) & 0xff] == 4)
62 return 4;
63 }
64 else if ((code & 0xff0000) != 0)
65 return ONIGERR_INVALID_CODE_POINT_VALUE;
66 else if ((code & 0xff00) != 0) {
67 if (EncLen_EUCTW[(int )(code >> 8) & 0xff] == 2)
68 return 2;
69 }
70 else {
71 if (EncLen_EUCTW[(int )(code & 0xff)] == 1)
72 return 1;
73 }
74
75 return ONIGERR_INVALID_CODE_POINT_VALUE;
76 }
77
78 static int
is_valid_mbc_string(const UChar * p,const UChar * end)79 is_valid_mbc_string(const UChar* p, const UChar* end)
80 {
81 while (p < end) {
82 if (*p < 0x80) {
83 p++;
84 }
85 else if (*p < 0xa1) {
86 if (*p == 0x8e) {
87 p++;
88 if (p >= end) return FALSE;
89 if (*p < 0xa1 || *p > 0xb0) return FALSE;
90 p++;
91 if (p >= end) return FALSE;
92 if (*p < 0xa1 || *p == 0xff)
93 return FALSE;
94 p++;
95 if (p >= end) return FALSE;
96 if (*p < 0xa1 || *p == 0xff)
97 return FALSE;
98 p++;
99 }
100 else
101 return FALSE;
102 }
103 else if (*p < 0xff) {
104 p++;
105 if (p >= end) return FALSE;
106 if (*p < 0xa1 || *p == 0xff)
107 return FALSE;
108 p++;
109 }
110 else
111 return FALSE;
112 }
113
114 return TRUE;
115 }
116
117 static OnigCodePoint
euctw_mbc_to_code(const UChar * p,const UChar * end)118 euctw_mbc_to_code(const UChar* p, const UChar* end)
119 {
120 return onigenc_mbn_mbc_to_code(ONIG_ENCODING_EUC_TW, p, end);
121 }
122
123 static int
euctw_code_to_mbc(OnigCodePoint code,UChar * buf)124 euctw_code_to_mbc(OnigCodePoint code, UChar *buf)
125 {
126 return onigenc_mb4_code_to_mbc(ONIG_ENCODING_EUC_TW, code, buf);
127 }
128
129 static int
euctw_mbc_case_fold(OnigCaseFoldType flag,const UChar ** pp,const UChar * end,UChar * lower)130 euctw_mbc_case_fold(OnigCaseFoldType flag, const UChar** pp, const UChar* end,
131 UChar* lower)
132 {
133 return onigenc_mbn_mbc_case_fold(ONIG_ENCODING_EUC_TW, flag,
134 pp, end, lower);
135 }
136
137 static int
euctw_is_code_ctype(OnigCodePoint code,unsigned int ctype)138 euctw_is_code_ctype(OnigCodePoint code, unsigned int ctype)
139 {
140 return onigenc_mb4_is_code_ctype(ONIG_ENCODING_EUC_TW, code, ctype);
141 }
142
143 #define euctw_islead(c) ((UChar )((c) - 0xa1) > 0xfe - 0xa1)
144
145 static UChar*
euctw_left_adjust_char_head(const UChar * start,const UChar * s)146 euctw_left_adjust_char_head(const UChar* start, const UChar* s)
147 {
148 /* Assumed in this encoding,
149 mb-trail bytes don't mix with single bytes.
150 */
151 const UChar *p;
152 int len;
153
154 if (s <= start) return (UChar* )s;
155 p = s;
156
157 while (!euctw_islead(*p) && p > start) p--;
158 len = enclen(ONIG_ENCODING_EUC_TW, p);
159 if (p + len > s) return (UChar* )p;
160 p += len;
161 return (UChar* )(p + ((s - p) & ~1));
162 }
163
164 static int
euctw_is_allowed_reverse_match(const UChar * s,const UChar * end ARG_UNUSED)165 euctw_is_allowed_reverse_match(const UChar* s, const UChar* end ARG_UNUSED)
166 {
167 const UChar c = *s;
168 if (c <= 0x7e) return TRUE;
169 else return FALSE;
170 }
171
172 OnigEncodingType OnigEncodingEUC_TW = {
173 euctw_mbc_enc_len,
174 "EUC-TW", /* name */
175 4, /* max enc length */
176 1, /* min enc length */
177 onigenc_is_mbc_newline_0x0a,
178 euctw_mbc_to_code,
179 euctw_code_to_mbclen,
180 euctw_code_to_mbc,
181 euctw_mbc_case_fold,
182 onigenc_ascii_apply_all_case_fold,
183 onigenc_ascii_get_case_fold_codes_by_str,
184 onigenc_minimum_property_name_to_ctype,
185 euctw_is_code_ctype,
186 onigenc_not_support_get_ctype_code_range,
187 euctw_left_adjust_char_head,
188 euctw_is_allowed_reverse_match,
189 NULL, /* init */
190 NULL, /* is_initialized */
191 is_valid_mbc_string,
192 ENC_FLAG_ASCII_COMPATIBLE|ENC_FLAG_SKIP_OFFSET_1,
193 0, 0
194 };
195