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 
146 /* for encoding system implementation (internal) */
147 extern int onigenc_end(void);
148 extern int onigenc_ascii_apply_all_case_fold P_((OnigCaseFoldType flag, OnigApplyAllCaseFoldFunc f, void* arg));
149 extern int onigenc_ascii_get_case_fold_codes_by_str P_((OnigCaseFoldType flag, const OnigUChar* p, const OnigUChar* end, OnigCaseFoldCodeItem items[]));
150 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));
151 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[]));
152 extern int onigenc_not_support_get_ctype_code_range P_((OnigCtype ctype, OnigCodePoint* sb_out, const OnigCodePoint* ranges[]));
153 extern int onigenc_is_mbc_newline_0x0a P_((const UChar* p, const UChar* end));
154 
155 
156 /* methods for single byte encoding */
157 extern int onigenc_ascii_mbc_case_fold P_((OnigCaseFoldType flag, const UChar** p, const UChar* end, UChar* lower));
158 extern int onigenc_single_byte_mbc_enc_len P_((const UChar* p));
159 extern OnigCodePoint onigenc_single_byte_mbc_to_code P_((const UChar* p, const UChar* end));
160 extern int onigenc_single_byte_code_to_mbclen P_((OnigCodePoint code));
161 extern int onigenc_single_byte_code_to_mbc P_((OnigCodePoint code, UChar *buf));
162 extern UChar* onigenc_single_byte_left_adjust_char_head P_((const UChar* start, const UChar* s));
163 extern int onigenc_always_true_is_allowed_reverse_match P_((const UChar* s, const UChar* end));
164 extern int onigenc_always_false_is_allowed_reverse_match P_((const UChar* s, const UChar* end));
165 extern int onigenc_always_true_is_valid_mbc_string P_((const UChar* s, const UChar* end));
166 extern int onigenc_length_check_is_valid_mbc_string P_((OnigEncoding enc, const UChar* s, const UChar* end));
167 
168 /* methods for multi byte encoding */
169 extern OnigCodePoint onigenc_mbn_mbc_to_code P_((OnigEncoding enc, const UChar* p, const UChar* end));
170 extern int onigenc_mbn_mbc_case_fold P_((OnigEncoding enc, OnigCaseFoldType flag, const UChar** p, const UChar* end, UChar* lower));
171 extern int onigenc_mb2_code_to_mbc P_((OnigEncoding enc, OnigCodePoint code, UChar *buf));
172 extern int onigenc_minimum_property_name_to_ctype P_((OnigEncoding enc, UChar* p, UChar* end));
173 extern int onigenc_unicode_property_name_to_ctype P_((OnigEncoding enc, UChar* p, UChar* end));
174 extern int onigenc_is_mbc_word_ascii P_((OnigEncoding enc, UChar* s, const UChar* end));
175 extern int onigenc_mb2_is_code_ctype P_((OnigEncoding enc, OnigCodePoint code, unsigned int ctype));
176 extern int onigenc_mb4_code_to_mbc P_((OnigEncoding enc, OnigCodePoint code, UChar *buf));
177 extern int onigenc_mb4_is_code_ctype P_((OnigEncoding enc, OnigCodePoint code, unsigned int ctype));
178 extern struct PropertyNameCtype* onigenc_euc_jp_lookup_property_name P_((register const char *str, register size_t len));
179 extern struct PropertyNameCtype* onigenc_sjis_lookup_property_name P_((register const char *str, register size_t len));
180 
181 /* in unicode.c */
182 extern int onigenc_unicode_is_code_ctype P_((OnigCodePoint code, unsigned int ctype));
183 extern int onigenc_utf16_32_get_ctype_code_range P_((OnigCtype ctype, OnigCodePoint *sb_out, const OnigCodePoint* ranges[]));
184 extern int onigenc_unicode_ctype_code_range P_((OnigCtype ctype, const OnigCodePoint* ranges[]));
185 extern int onigenc_unicode_get_case_fold_codes_by_str P_((OnigEncoding enc, OnigCaseFoldType flag, const OnigUChar* p, const OnigUChar* end, OnigCaseFoldCodeItem items[]));
186 extern int onigenc_unicode_mbc_case_fold P_((OnigEncoding enc, OnigCaseFoldType flag, const UChar** pp, const UChar* end, UChar* fold));
187 extern int onigenc_unicode_apply_all_case_fold P_((OnigCaseFoldType flag, OnigApplyAllCaseFoldFunc f, void* arg));
188 
189 extern int onigenc_egcb_is_break_position P_((OnigEncoding enc, UChar* p, UChar* prev, const UChar* start, const UChar* end));
190 
191 #ifdef USE_UNICODE_WORD_BREAK
192 extern int onigenc_wb_is_break_position P_((OnigEncoding enc, UChar* p, UChar* prev, const UChar* start, const UChar* end));
193 #endif
194 
195 #define UTF16_IS_SURROGATE_FIRST(c)    (((c) & 0xfc) == 0xd8)
196 #define UTF16_IS_SURROGATE_SECOND(c)   (((c) & 0xfc) == 0xdc)
197 
198 /* from unicode generated codes */
199 #define FOLDS1_FOLD(i)         (OnigUnicodeFolds1 + (i))
200 #define FOLDS2_FOLD(i)         (OnigUnicodeFolds2 + (i))
201 #define FOLDS3_FOLD(i)         (OnigUnicodeFolds3 + (i))
202 #define FOLDS1_UNFOLDS_NUM(i)  (OnigUnicodeFolds1[(i)+1])
203 #define FOLDS2_UNFOLDS_NUM(i)  (OnigUnicodeFolds2[(i)+2])
204 #define FOLDS3_UNFOLDS_NUM(i)  (OnigUnicodeFolds3[(i)+3])
205 #define FOLDS1_UNFOLDS(i)      (OnigUnicodeFolds1 + (i) + 2)
206 #define FOLDS2_UNFOLDS(i)      (OnigUnicodeFolds2 + (i) + 3)
207 #define FOLDS3_UNFOLDS(i)      (OnigUnicodeFolds3 + (i) + 4)
208 #define FOLDS1_NEXT_INDEX(i)   ((i) + 2 + OnigUnicodeFolds1[(i)+1])
209 #define FOLDS2_NEXT_INDEX(i)   ((i) + 3 + OnigUnicodeFolds2[(i)+2])
210 #define FOLDS3_NEXT_INDEX(i)   ((i) + 4 + OnigUnicodeFolds3[(i)+3])
211 
212 #define FOLDS_FOLD_ADDR_BUK(buk, addr) do {\
213   if ((buk)->fold_len == 1)\
214     addr = OnigUnicodeFolds1 + (buk)->index;\
215   else if ((buk)->fold_len == 2)\
216     addr = OnigUnicodeFolds2 + (buk)->index;\
217   else if ((buk)->fold_len == 3)\
218     addr = OnigUnicodeFolds3 + (buk)->index;\
219   else\
220     return ONIGERR_INVALID_CODE_POINT_VALUE;\
221 } while (0)
222 
223 extern OnigCodePoint OnigUnicodeFolds1[];
224 extern OnigCodePoint OnigUnicodeFolds2[];
225 extern OnigCodePoint OnigUnicodeFolds3[];
226 
227 struct ByUnfoldKey {
228   OnigCodePoint code;
229   short int     index;
230   short int     fold_len;
231 };
232 
233 extern const struct ByUnfoldKey* onigenc_unicode_unfold_key(OnigCodePoint code);
234 extern int onigenc_unicode_fold1_key(OnigCodePoint code[]);
235 extern int onigenc_unicode_fold2_key(OnigCodePoint code[]);
236 extern int onigenc_unicode_fold3_key(OnigCodePoint code[]);
237 
238 extern int onig_codes_cmp(OnigCodePoint a[], OnigCodePoint b[], int n);
239 extern int onig_codes_byte_at(OnigCodePoint code[], int at);
240 
241 
242 
243 #define ONIGENC_ISO_8859_1_TO_LOWER_CASE(c) \
244   OnigEncISO_8859_1_ToLowerCaseTable[c]
245 #define ONIGENC_ISO_8859_1_TO_UPPER_CASE(c) \
246   OnigEncISO_8859_1_ToUpperCaseTable[c]
247 
248 extern const UChar OnigEncISO_8859_1_ToLowerCaseTable[];
249 extern const UChar OnigEncISO_8859_1_ToUpperCaseTable[];
250 
251 extern int
252 onigenc_with_ascii_strncmp P_((OnigEncoding enc, const UChar* p, const UChar* end, const UChar* sascii /* ascii */, int n));
253 extern UChar*
254 onigenc_step P_((OnigEncoding enc, const UChar* p, const UChar* end, int n));
255 
256 /* defined in regexec.c, but used in enc/xxx.c */
257 extern int  onig_is_in_code_range P_((const UChar* p, OnigCodePoint code));
258 
259 extern OnigEncoding  OnigEncDefaultCharEncoding;
260 extern const UChar  OnigEncAsciiToLowerCaseTable[];
261 extern const UChar  OnigEncAsciiToUpperCaseTable[];
262 extern const unsigned short OnigEncAsciiCtypeTable[];
263 
264 
265 #define ONIGENC_IS_ASCII_CODE(code)  ((code) < 0x80)
266 #define ONIGENC_ASCII_CODE_TO_LOWER_CASE(c) OnigEncAsciiToLowerCaseTable[c]
267 #define ONIGENC_ASCII_CODE_TO_UPPER_CASE(c) OnigEncAsciiToUpperCaseTable[c]
268 #define ONIGENC_IS_ASCII_CODE_CTYPE(code,ctype) \
269   ((OnigEncAsciiCtypeTable[code] & CTYPE_TO_BIT(ctype)) != 0)
270 #define ONIGENC_IS_ASCII_CODE_WORD(code) \
271   ((OnigEncAsciiCtypeTable[code] & CTYPE_TO_BIT(ONIGENC_CTYPE_WORD)) != 0)
272 #define ONIGENC_IS_ASCII_CODE_CASE_AMBIG(code) \
273  (ONIGENC_IS_ASCII_CODE_CTYPE(code, ONIGENC_CTYPE_UPPER) ||\
274   ONIGENC_IS_ASCII_CODE_CTYPE(code, ONIGENC_CTYPE_LOWER))
275 
276 #define ONIGENC_IS_UNICODE_ENCODING(enc) \
277   (((enc)->flag & ENC_FLAG_UNICODE) != 0)
278 
279 #define ONIGENC_IS_ASCII_COMPATIBLE_ENCODING(enc)  \
280   (((enc)->flag & ENC_FLAG_ASCII_COMPATIBLE) != 0)
281 
282 #endif /* REGENC_H */
283