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