1 /**********************************************************************
2 iso8859_1.c - Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5 * Copyright (c) 2002-2007 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 "regenc.h"
31
32 #define ENC_IS_ISO_8859_1_CTYPE(code,ctype) \
33 ((EncISO_8859_1_CtypeTable[code] & CTYPE_TO_BIT(ctype)) != 0)
34
35 static const unsigned short EncISO_8859_1_CtypeTable[256] = {
36 0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008,
37 0x4008, 0x420c, 0x4209, 0x4208, 0x4208, 0x4208, 0x4008, 0x4008,
38 0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008,
39 0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008, 0x4008,
40 0x4284, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0,
41 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0,
42 0x78b0, 0x78b0, 0x78b0, 0x78b0, 0x78b0, 0x78b0, 0x78b0, 0x78b0,
43 0x78b0, 0x78b0, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x41a0,
44 0x41a0, 0x7ca2, 0x7ca2, 0x7ca2, 0x7ca2, 0x7ca2, 0x7ca2, 0x74a2,
45 0x74a2, 0x74a2, 0x74a2, 0x74a2, 0x74a2, 0x74a2, 0x74a2, 0x74a2,
46 0x74a2, 0x74a2, 0x74a2, 0x74a2, 0x74a2, 0x74a2, 0x74a2, 0x74a2,
47 0x74a2, 0x74a2, 0x74a2, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x51a0,
48 0x41a0, 0x78e2, 0x78e2, 0x78e2, 0x78e2, 0x78e2, 0x78e2, 0x70e2,
49 0x70e2, 0x70e2, 0x70e2, 0x70e2, 0x70e2, 0x70e2, 0x70e2, 0x70e2,
50 0x70e2, 0x70e2, 0x70e2, 0x70e2, 0x70e2, 0x70e2, 0x70e2, 0x70e2,
51 0x70e2, 0x70e2, 0x70e2, 0x41a0, 0x41a0, 0x41a0, 0x41a0, 0x4008,
52 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008,
53 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008,
54 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008,
55 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008,
56 0x0284, 0x01a0, 0x00a0, 0x00a0, 0x00a0, 0x00a0, 0x00a0, 0x00a0,
57 0x00a0, 0x00a0, 0x30e2, 0x01a0, 0x00a0, 0x01a0, 0x00a0, 0x00a0,
58 0x00a0, 0x00a0, 0x10a0, 0x10a0, 0x00a0, 0x30e2, 0x00a0, 0x01a0,
59 0x00a0, 0x10a0, 0x30e2, 0x01a0, 0x10a0, 0x10a0, 0x10a0, 0x01a0,
60 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2,
61 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2,
62 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x00a0,
63 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x34a2, 0x30e2,
64 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2,
65 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2,
66 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x00a0,
67 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2, 0x30e2
68 };
69
70 static const OnigPairCaseFoldCodes CaseFoldMap[] = {
71 { 0xc0, 0xe0 },
72 { 0xc1, 0xe1 },
73 { 0xc2, 0xe2 },
74 { 0xc3, 0xe3 },
75 { 0xc4, 0xe4 },
76 { 0xc5, 0xe5 },
77 { 0xc6, 0xe6 },
78 { 0xc7, 0xe7 },
79 { 0xc8, 0xe8 },
80 { 0xc9, 0xe9 },
81 { 0xca, 0xea },
82 { 0xcb, 0xeb },
83 { 0xcc, 0xec },
84 { 0xcd, 0xed },
85 { 0xce, 0xee },
86 { 0xcf, 0xef },
87
88 { 0xd0, 0xf0 },
89 { 0xd1, 0xf1 },
90 { 0xd2, 0xf2 },
91 { 0xd3, 0xf3 },
92 { 0xd4, 0xf4 },
93 { 0xd5, 0xf5 },
94 { 0xd6, 0xf6 },
95 { 0xd8, 0xf8 },
96 { 0xd9, 0xf9 },
97 { 0xda, 0xfa },
98 { 0xdb, 0xfb },
99 { 0xdc, 0xfc },
100 { 0xdd, 0xfd },
101 { 0xde, 0xfe }
102 };
103
104 static int
apply_all_case_fold(OnigCaseFoldType flag,OnigApplyAllCaseFoldFunc f,void * arg)105 apply_all_case_fold(OnigCaseFoldType flag,
106 OnigApplyAllCaseFoldFunc f, void* arg)
107 {
108 return onigenc_apply_all_case_fold_with_map(
109 sizeof(CaseFoldMap)/sizeof(OnigPairCaseFoldCodes), CaseFoldMap, 1,
110 flag, f, arg);
111 }
112
113 static int
get_case_fold_codes_by_str(OnigCaseFoldType flag ARG_UNUSED,const OnigUChar * p,const OnigUChar * end,OnigCaseFoldCodeItem items[])114 get_case_fold_codes_by_str(OnigCaseFoldType flag ARG_UNUSED,
115 const OnigUChar* p, const OnigUChar* end,
116 OnigCaseFoldCodeItem items[])
117 {
118 if (0x41 <= *p && *p <= 0x5a) {
119 items[0].byte_len = 1;
120 items[0].code_len = 1;
121 items[0].code[0] = (OnigCodePoint )(*p + 0x20);
122 if (*p == 0x53 && end > p + 1
123 && (*(p+1) == 0x53 || *(p+1) == 0x73)) { /* SS */
124 items[1].byte_len = 2;
125 items[1].code_len = 1;
126 items[1].code[0] = (OnigCodePoint )0xdf;
127 return 2;
128 }
129 else
130 return 1;
131 }
132 else if (0x61 <= *p && *p <= 0x7a) {
133 items[0].byte_len = 1;
134 items[0].code_len = 1;
135 items[0].code[0] = (OnigCodePoint )(*p - 0x20);
136 if (*p == 0x73 && end > p + 1
137 && (*(p+1) == 0x73 || *(p+1) == 0x53)) { /* ss */
138 items[1].byte_len = 2;
139 items[1].code_len = 1;
140 items[1].code[0] = (OnigCodePoint )0xdf;
141 return 2;
142 }
143 else
144 return 1;
145 }
146 else if (0xc0 <= *p && *p <= 0xcf) {
147 items[0].byte_len = 1;
148 items[0].code_len = 1;
149 items[0].code[0] = (OnigCodePoint )(*p + 0x20);
150 return 1;
151 }
152 else if (0xd0 <= *p && *p <= 0xdf) {
153 if (*p == 0xdf) {
154 items[0].byte_len = 1;
155 items[0].code_len = 2;
156 items[0].code[0] = (OnigCodePoint )'s';
157 items[0].code[1] = (OnigCodePoint )'s';
158
159 items[1].byte_len = 1;
160 items[1].code_len = 2;
161 items[1].code[0] = (OnigCodePoint )'S';
162 items[1].code[1] = (OnigCodePoint )'S';
163
164 items[2].byte_len = 1;
165 items[2].code_len = 2;
166 items[2].code[0] = (OnigCodePoint )'s';
167 items[2].code[1] = (OnigCodePoint )'S';
168
169 items[3].byte_len = 1;
170 items[3].code_len = 2;
171 items[3].code[0] = (OnigCodePoint )'S';
172 items[3].code[1] = (OnigCodePoint )'s';
173
174 return 4;
175 }
176 else if (*p != 0xd7) {
177 items[0].byte_len = 1;
178 items[0].code_len = 1;
179 items[0].code[0] = (OnigCodePoint )(*p + 0x20);
180 return 1;
181 }
182 }
183 else if (0xe0 <= *p && *p <= 0xef) {
184 items[0].byte_len = 1;
185 items[0].code_len = 1;
186 items[0].code[0] = (OnigCodePoint )(*p - 0x20);
187 return 1;
188 }
189 else if (0xf0 <= *p && *p <= 0xfe) {
190 if (*p != 0xf7) {
191 items[0].byte_len = 1;
192 items[0].code_len = 1;
193 items[0].code[0] = (OnigCodePoint )(*p - 0x20);
194 return 1;
195 }
196 }
197
198 return 0;
199 }
200
201 static int
mbc_case_fold(OnigCaseFoldType flag,const UChar ** pp,const UChar * end ARG_UNUSED,UChar * lower)202 mbc_case_fold(OnigCaseFoldType flag, const UChar** pp,
203 const UChar* end ARG_UNUSED, UChar* lower)
204 {
205 const UChar* p = *pp;
206
207 if (*p == 0xdf && (flag & INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR) != 0) {
208 *lower++ = 's';
209 *lower = 's';
210 (*pp)++;
211 return 2;
212 }
213
214 *lower = ONIGENC_ISO_8859_1_TO_LOWER_CASE(*p);
215 (*pp)++;
216 return 1;
217 }
218
219 #if 0
220 static int
221 is_mbc_ambiguous(OnigCaseFoldType flag,
222 const UChar** pp, const UChar* end)
223 {
224 int v;
225 const UChar* p = *pp;
226
227 if (*p == 0xdf && (flag & INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR) != 0) {
228 (*pp)++;
229 return TRUE;
230 }
231
232 (*pp)++;
233 v = (EncISO_8859_1_CtypeTable[*p] & (BIT_CTYPE_UPPER | BIT_CTYPE_LOWER));
234 if ((v | BIT_CTYPE_LOWER) != 0) {
235 /* 0xdf, 0xaa, 0xb5, 0xba are lower case letter, but can't convert. */
236 if (*p >= 0xaa && *p <= 0xba)
237 return FALSE;
238 else
239 return TRUE;
240 }
241
242 return (v != 0 ? TRUE : FALSE);
243 }
244 #endif
245
246 static int
is_code_ctype(OnigCodePoint code,unsigned int ctype)247 is_code_ctype(OnigCodePoint code, unsigned int ctype)
248 {
249 if (code < 256)
250 return ENC_IS_ISO_8859_1_CTYPE(code, ctype);
251 else
252 return FALSE;
253 }
254
255 OnigEncodingType OnigEncodingISO_8859_1 = {
256 onigenc_single_byte_mbc_enc_len,
257 "ISO-8859-1", /* name */
258 1, /* max enc length */
259 1, /* min enc length */
260 onigenc_is_mbc_newline_0x0a,
261 onigenc_single_byte_mbc_to_code,
262 onigenc_single_byte_code_to_mbclen,
263 onigenc_single_byte_code_to_mbc,
264 mbc_case_fold,
265 apply_all_case_fold,
266 get_case_fold_codes_by_str,
267 onigenc_minimum_property_name_to_ctype,
268 is_code_ctype,
269 onigenc_not_support_get_ctype_code_range,
270 onigenc_single_byte_left_adjust_char_head,
271 onigenc_always_true_is_allowed_reverse_match
272 };
273