1 #ifndef REGENC_H
2 #define REGENC_H
3 /**********************************************************************
4   regenc.h -  Oniguruma (regular expression library)
5 **********************************************************************/
6 /*-
7  * Copyright (c) 2002-2020  K.Kosako
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef ONIGURUMA_EXPORT
33 #define ONIGURUMA_EXPORT
34 #endif
35 
36 #include "config.h"
37 
38 #ifndef ONIG_NO_STANDARD_C_HEADERS
39 #include <stddef.h>
40 #endif
41 
42 #ifdef ONIG_ESCAPE_UCHAR_COLLISION
43 #undef ONIG_ESCAPE_UCHAR_COLLISION
44 #endif
45 
46 #include "oniguruma.h"
47 
48 typedef struct {
49   OnigCodePoint from;
50   OnigCodePoint to;
51 } OnigPairCaseFoldCodes;
52 
53 
54 #ifndef NULL
55 #define NULL   ((void* )0)
56 #endif
57 
58 #ifndef TRUE
59 #define TRUE    1
60 #endif
61 
62 #ifndef FALSE
63 #define FALSE   0
64 #endif
65 
66 #ifndef ARG_UNUSED
67 #if defined(__GNUC__)
68 #  define ARG_UNUSED  __attribute__ ((unused))
69 #else
70 #  define ARG_UNUSED
71 #endif
72 #endif
73 
74 #define ONIG_IS_NULL(p)                    (((void*)(p)) == (void*)0)
75 #define ONIG_IS_NOT_NULL(p)                (((void*)(p)) != (void*)0)
76 #define ONIG_CHECK_NULL_RETURN(p)          if (ONIG_IS_NULL(p)) return NULL
77 #define ONIG_CHECK_NULL_RETURN_VAL(p,val)  if (ONIG_IS_NULL(p)) return (val)
78 
79 #define MAX_CODE_POINT         (~((OnigCodePoint )0))
80 #define ASCII_LIMIT            127
81 #define NEWLINE_CODE           0x0a
82 
83 #define enclen(enc,p)          ONIGENC_MBC_ENC_LEN(enc,p)
84 
85 /* character types bit flag */
86 #define BIT_CTYPE_NEWLINE  (1<< ONIGENC_CTYPE_NEWLINE)
87 #define BIT_CTYPE_ALPHA    (1<< ONIGENC_CTYPE_ALPHA)
88 #define BIT_CTYPE_BLANK    (1<< ONIGENC_CTYPE_BLANK)
89 #define BIT_CTYPE_CNTRL    (1<< ONIGENC_CTYPE_CNTRL)
90 #define BIT_CTYPE_DIGIT    (1<< ONIGENC_CTYPE_DIGIT)
91 #define BIT_CTYPE_GRAPH    (1<< ONIGENC_CTYPE_GRAPH)
92 #define BIT_CTYPE_LOWER    (1<< ONIGENC_CTYPE_LOWER)
93 #define BIT_CTYPE_PRINT    (1<< ONIGENC_CTYPE_PRINT)
94 #define BIT_CTYPE_PUNCT    (1<< ONIGENC_CTYPE_PUNCT)
95 #define BIT_CTYPE_SPACE    (1<< ONIGENC_CTYPE_SPACE)
96 #define BIT_CTYPE_UPPER    (1<< ONIGENC_CTYPE_UPPER)
97 #define BIT_CTYPE_XDIGIT   (1<< ONIGENC_CTYPE_XDIGIT)
98 #define BIT_CTYPE_WORD     (1<< ONIGENC_CTYPE_WORD)
99 #define BIT_CTYPE_ALNUM    (1<< ONIGENC_CTYPE_ALNUM)
100 #define BIT_CTYPE_ASCII    (1<< ONIGENC_CTYPE_ASCII)
101 
102 #define CTYPE_TO_BIT(ctype)  (1<<(ctype))
103 #define CTYPE_IS_WORD_GRAPH_PRINT(ctype) \
104   ((ctype) == ONIGENC_CTYPE_WORD || (ctype) == ONIGENC_CTYPE_GRAPH ||\
105    (ctype) == ONIGENC_CTYPE_PRINT)
106 
107 
108 typedef struct {
109   UChar    *name;
110   int       ctype;
111   short int len;
112 } PosixBracketEntryType;
113 
114 struct PropertyNameCtype {
115   char *name;
116   int ctype;
117 };
118 
119 /* #define USE_CRNL_AS_LINE_TERMINATOR */
120 #define USE_UNICODE_PROPERTIES
121 #define USE_UNICODE_EXTENDED_GRAPHEME_CLUSTER
122 #define USE_UNICODE_WORD_BREAK
123 /* #define USE_UNICODE_CASE_FOLD_TURKISH_AZERI */
124 /* #define USE_UNICODE_ALL_LINE_TERMINATORS */  /* see Unicode.org UTS #18 */
125 
126 
127 #define ONIG_ENCODING_INIT_DEFAULT           ONIG_ENCODING_ASCII
128 
129 
130 #define ENC_SKIP_OFFSET_1_OR_0             7
131 
132 #define ENC_FLAG_ASCII_COMPATIBLE      (1<<0)
133 #define ENC_FLAG_UNICODE               (1<<1)
134 #define ENC_FLAG_SKIP_OFFSET_MASK      (7<<2)
135 #define ENC_FLAG_SKIP_OFFSET_0             0
136 #define ENC_FLAG_SKIP_OFFSET_1         (1<<2)
137 #define ENC_FLAG_SKIP_OFFSET_2         (2<<2)
138 #define ENC_FLAG_SKIP_OFFSET_3         (3<<2)
139 #define ENC_FLAG_SKIP_OFFSET_4         (4<<2)
140 #define ENC_FLAG_SKIP_OFFSET_1_OR_0    (ENC_SKIP_OFFSET_1_OR_0<<2)
141 
142 #define ENC_GET_SKIP_OFFSET(enc) \
143   (((enc)->flag & ENC_FLAG_SKIP_OFFSET_MASK)>>2)
144 
145 #define CASE_FOLD_IS_ASCII_ONLY(flag) \
146   (((flag) & ONIGENC_CASE_FOLD_ASCII_ONLY) != 0)
147 #define CASE_FOLD_IS_NOT_ASCII_ONLY(flag) \
148   (((flag) & ONIGENC_CASE_FOLD_ASCII_ONLY) == 0)
149 
150 /* for encoding system implementation (internal) */
151 extern int onigenc_end(void);
152 extern int onigenc_ascii_apply_all_case_fold P_((OnigCaseFoldType flag, OnigApplyAllCaseFoldFunc f, void* arg));
153 extern int onigenc_ascii_get_case_fold_codes_by_str P_((OnigCaseFoldType flag, const OnigUChar* p, const OnigUChar* end, OnigCaseFoldCodeItem items[]));
154 extern int onigenc_apply_all_case_fold_with_map P_((int map_size, const OnigPairCaseFoldCodes map[], int ess_tsett_flag, OnigCaseFoldType flag, OnigApplyAllCaseFoldFunc f, void* arg));
155 extern int onigenc_get_case_fold_codes_by_str_with_map P_((int map_size, const OnigPairCaseFoldCodes map[], int ess_tsett_flag, OnigCaseFoldType flag, const OnigUChar* p, const OnigUChar* end, OnigCaseFoldCodeItem items[]));
156 extern int onigenc_not_support_get_ctype_code_range P_((OnigCtype ctype, OnigCodePoint* sb_out, const OnigCodePoint* ranges[]));
157 extern int onigenc_is_mbc_newline_0x0a P_((const UChar* p, const UChar* end));
158 
159 
160 /* methods for single byte encoding */
161 extern int onigenc_ascii_mbc_case_fold P_((OnigCaseFoldType flag, const UChar** p, const UChar* end, UChar* lower));
162 extern int onigenc_single_byte_mbc_enc_len P_((const UChar* p));
163 extern OnigCodePoint onigenc_single_byte_mbc_to_code P_((const UChar* p, const UChar* end));
164 extern int onigenc_single_byte_code_to_mbclen P_((OnigCodePoint code));
165 extern int onigenc_single_byte_code_to_mbc P_((OnigCodePoint code, UChar *buf));
166 extern UChar* onigenc_single_byte_left_adjust_char_head P_((const UChar* start, const UChar* s));
167 extern int onigenc_always_true_is_allowed_reverse_match P_((const UChar* s, const UChar* end));
168 extern int onigenc_always_false_is_allowed_reverse_match P_((const UChar* s, const UChar* end));
169 extern int onigenc_always_true_is_valid_mbc_string P_((const UChar* s, const UChar* end));
170 extern int onigenc_length_check_is_valid_mbc_string P_((OnigEncoding enc, const UChar* s, const UChar* end));
171 
172 /* methods for multi byte encoding */
173 extern OnigCodePoint onigenc_mbn_mbc_to_code P_((OnigEncoding enc, const UChar* p, const UChar* end));
174 extern int onigenc_mbn_mbc_case_fold P_((OnigEncoding enc, OnigCaseFoldType flag, const UChar** p, const UChar* end, UChar* lower));
175 extern int onigenc_mb2_code_to_mbc P_((OnigEncoding enc, OnigCodePoint code, UChar *buf));
176 extern int onigenc_minimum_property_name_to_ctype P_((OnigEncoding enc, UChar* p, UChar* end));
177 extern int onigenc_unicode_property_name_to_ctype P_((OnigEncoding enc, UChar* p, UChar* end));
178 extern int onigenc_is_mbc_word_ascii P_((OnigEncoding enc, UChar* s, const UChar* end));
179 extern int onigenc_mb2_is_code_ctype P_((OnigEncoding enc, OnigCodePoint code, unsigned int ctype));
180 extern int onigenc_mb4_code_to_mbc P_((OnigEncoding enc, OnigCodePoint code, UChar *buf));
181 extern int onigenc_mb4_is_code_ctype P_((OnigEncoding enc, OnigCodePoint code, unsigned int ctype));
182 extern struct PropertyNameCtype* onigenc_euc_jp_lookup_property_name P_((register const char *str, register size_t len));
183 extern struct PropertyNameCtype* onigenc_sjis_lookup_property_name P_((register const char *str, register size_t len));
184 
185 /* in unicode.c */
186 extern int onigenc_unicode_is_code_ctype P_((OnigCodePoint code, unsigned int ctype));
187 extern int onigenc_utf16_32_get_ctype_code_range P_((OnigCtype ctype, OnigCodePoint *sb_out, const OnigCodePoint* ranges[]));
188 extern int onigenc_unicode_ctype_code_range P_((OnigCtype ctype, const OnigCodePoint* ranges[]));
189 extern int onigenc_unicode_get_case_fold_codes_by_str P_((OnigEncoding enc, OnigCaseFoldType flag, const OnigUChar* p, const OnigUChar* end, OnigCaseFoldCodeItem items[]));
190 extern int onigenc_unicode_mbc_case_fold P_((OnigEncoding enc, OnigCaseFoldType flag, const UChar** pp, const UChar* end, UChar* fold));
191 extern int onigenc_unicode_apply_all_case_fold P_((OnigCaseFoldType flag, OnigApplyAllCaseFoldFunc f, void* arg));
192 
193 extern int onigenc_egcb_is_break_position P_((OnigEncoding enc, UChar* p, UChar* prev, const UChar* start, const UChar* end));
194 
195 #ifdef USE_UNICODE_WORD_BREAK
196 extern int onigenc_wb_is_break_position P_((OnigEncoding enc, UChar* p, UChar* prev, const UChar* start, const UChar* end));
197 #endif
198 
199 #define UTF16_IS_SURROGATE_FIRST(c)    (((c) & 0xfc) == 0xd8)
200 #define UTF16_IS_SURROGATE_SECOND(c)   (((c) & 0xfc) == 0xdc)
201 
202 /* from unicode generated codes */
203 #define FOLDS1_FOLD(i)         (OnigUnicodeFolds1 + (i))
204 #define FOLDS2_FOLD(i)         (OnigUnicodeFolds2 + (i))
205 #define FOLDS3_FOLD(i)         (OnigUnicodeFolds3 + (i))
206 #define FOLDS1_UNFOLDS_NUM(i)  (OnigUnicodeFolds1[(i)+1])
207 #define FOLDS2_UNFOLDS_NUM(i)  (OnigUnicodeFolds2[(i)+2])
208 #define FOLDS3_UNFOLDS_NUM(i)  (OnigUnicodeFolds3[(i)+3])
209 #define FOLDS1_UNFOLDS(i)      (FOLDS1_FOLD(i) + 2)
210 #define FOLDS2_UNFOLDS(i)      (FOLDS2_FOLD(i) + 3)
211 #define FOLDS3_UNFOLDS(i)      (FOLDS3_FOLD(i) + 4)
212 #define FOLDS1_NEXT_INDEX(i)   ((i) + 2 + FOLDS1_UNFOLDS_NUM(i))
213 #define FOLDS2_NEXT_INDEX(i)   ((i) + 3 + FOLDS2_UNFOLDS_NUM(i))
214 #define FOLDS3_NEXT_INDEX(i)   ((i) + 4 + FOLDS3_UNFOLDS_NUM(i))
215 
216 #define FOLDS_FOLD_ADDR_BUK(buk, addr) do {\
217   if ((buk)->fold_len == 1)\
218     addr = OnigUnicodeFolds1 + (buk)->index;\
219   else if ((buk)->fold_len == 2)\
220     addr = OnigUnicodeFolds2 + (buk)->index;\
221   else if ((buk)->fold_len == 3)\
222     addr = OnigUnicodeFolds3 + (buk)->index;\
223   else\
224     return ONIGERR_INVALID_CODE_POINT_VALUE;\
225 } while (0)
226 
227 extern OnigCodePoint OnigUnicodeFolds1[];
228 extern OnigCodePoint OnigUnicodeFolds2[];
229 extern OnigCodePoint OnigUnicodeFolds3[];
230 
231 struct ByUnfoldKey {
232   OnigCodePoint code;
233   short int     index;
234   short int     fold_len;
235 };
236 
237 extern const struct ByUnfoldKey* onigenc_unicode_unfold_key(OnigCodePoint code);
238 extern int onigenc_unicode_fold1_key(OnigCodePoint code[]);
239 extern int onigenc_unicode_fold2_key(OnigCodePoint code[]);
240 extern int onigenc_unicode_fold3_key(OnigCodePoint code[]);
241 
242 extern int onig_codes_cmp(OnigCodePoint a[], OnigCodePoint b[], int n);
243 extern int onig_codes_byte_at(OnigCodePoint code[], int at);
244 
245 
246 
247 #define ONIGENC_ISO_8859_1_TO_LOWER_CASE(c) \
248   OnigEncISO_8859_1_ToLowerCaseTable[c]
249 #define ONIGENC_ISO_8859_1_TO_UPPER_CASE(c) \
250   OnigEncISO_8859_1_ToUpperCaseTable[c]
251 
252 extern const UChar OnigEncISO_8859_1_ToLowerCaseTable[];
253 extern const UChar OnigEncISO_8859_1_ToUpperCaseTable[];
254 
255 extern int
256 onigenc_with_ascii_strncmp P_((OnigEncoding enc, const UChar* p, const UChar* end, const UChar* sascii /* ascii */, int n));
257 extern UChar*
258 onigenc_step P_((OnigEncoding enc, const UChar* p, const UChar* end, int n));
259 
260 /* defined in regexec.c, but used in enc/xxx.c */
261 extern int  onig_is_in_code_range P_((const UChar* p, OnigCodePoint code));
262 
263 extern OnigEncoding  OnigEncDefaultCharEncoding;
264 extern const UChar  OnigEncAsciiToLowerCaseTable[];
265 extern const UChar  OnigEncAsciiToUpperCaseTable[];
266 extern const unsigned short OnigEncAsciiCtypeTable[];
267 
268 
269 #define ONIGENC_IS_ASCII_CODE(code)  ((code) < 0x80)
270 #define ONIGENC_ASCII_CODE_TO_LOWER_CASE(c) OnigEncAsciiToLowerCaseTable[c]
271 #define ONIGENC_ASCII_CODE_TO_UPPER_CASE(c) OnigEncAsciiToUpperCaseTable[c]
272 #define ONIGENC_IS_ASCII_CODE_CTYPE(code,ctype) \
273   ((OnigEncAsciiCtypeTable[code] & CTYPE_TO_BIT(ctype)) != 0)
274 #define ONIGENC_IS_ASCII_CODE_WORD(code) \
275   ((OnigEncAsciiCtypeTable[code] & CTYPE_TO_BIT(ONIGENC_CTYPE_WORD)) != 0)
276 #define ONIGENC_IS_ASCII_CODE_CASE_AMBIG(code) \
277  (ONIGENC_IS_ASCII_CODE_CTYPE(code, ONIGENC_CTYPE_UPPER) ||\
278   ONIGENC_IS_ASCII_CODE_CTYPE(code, ONIGENC_CTYPE_LOWER))
279 
280 #define ONIGENC_IS_UNICODE_ENCODING(enc) \
281   (((enc)->flag & ENC_FLAG_UNICODE) != 0)
282 
283 #define ONIGENC_IS_ASCII_COMPATIBLE_ENCODING(enc)  \
284   (((enc)->flag & ENC_FLAG_ASCII_COMPATIBLE) != 0)
285 
286 #endif /* REGENC_H */
287