xref: /PHP-7.3/ext/mbstring/oniguruma/src/regenc.h (revision 1979c5d1)
1 #ifndef REGENC_H
2 #define REGENC_H
3 /**********************************************************************
4   regenc.h -  Oniguruma (regular expression library)
5 **********************************************************************/
6 /*-
7  * Copyright (c) 2002-2019  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 #include <stddef.h>
38 
39 #ifdef ONIG_ESCAPE_UCHAR_COLLISION
40 #undef ONIG_ESCAPE_UCHAR_COLLISION
41 #endif
42 
43 #include "oniguruma.h"
44 
45 typedef struct {
46   OnigCodePoint from;
47   OnigCodePoint to;
48 } OnigPairCaseFoldCodes;
49 
50 
51 #ifndef NULL
52 #define NULL   ((void* )0)
53 #endif
54 
55 #ifndef TRUE
56 #define TRUE    1
57 #endif
58 
59 #ifndef FALSE
60 #define FALSE   0
61 #endif
62 
63 #ifndef ARG_UNUSED
64 #if defined(__GNUC__)
65 #  define ARG_UNUSED  __attribute__ ((unused))
66 #else
67 #  define ARG_UNUSED
68 #endif
69 #endif
70 
71 #define ONIG_IS_NULL(p)                    (((void*)(p)) == (void*)0)
72 #define ONIG_IS_NOT_NULL(p)                (((void*)(p)) != (void*)0)
73 #define ONIG_CHECK_NULL_RETURN(p)          if (ONIG_IS_NULL(p)) return NULL
74 #define ONIG_CHECK_NULL_RETURN_VAL(p,val)  if (ONIG_IS_NULL(p)) return (val)
75 
76 #define MAX_CODE_POINT         (~((OnigCodePoint )0))
77 
78 #define enclen(enc,p)          ONIGENC_MBC_ENC_LEN(enc,p)
79 
80 /* character types bit flag */
81 #define BIT_CTYPE_NEWLINE  (1<< ONIGENC_CTYPE_NEWLINE)
82 #define BIT_CTYPE_ALPHA    (1<< ONIGENC_CTYPE_ALPHA)
83 #define BIT_CTYPE_BLANK    (1<< ONIGENC_CTYPE_BLANK)
84 #define BIT_CTYPE_CNTRL    (1<< ONIGENC_CTYPE_CNTRL)
85 #define BIT_CTYPE_DIGIT    (1<< ONIGENC_CTYPE_DIGIT)
86 #define BIT_CTYPE_GRAPH    (1<< ONIGENC_CTYPE_GRAPH)
87 #define BIT_CTYPE_LOWER    (1<< ONIGENC_CTYPE_LOWER)
88 #define BIT_CTYPE_PRINT    (1<< ONIGENC_CTYPE_PRINT)
89 #define BIT_CTYPE_PUNCT    (1<< ONIGENC_CTYPE_PUNCT)
90 #define BIT_CTYPE_SPACE    (1<< ONIGENC_CTYPE_SPACE)
91 #define BIT_CTYPE_UPPER    (1<< ONIGENC_CTYPE_UPPER)
92 #define BIT_CTYPE_XDIGIT   (1<< ONIGENC_CTYPE_XDIGIT)
93 #define BIT_CTYPE_WORD     (1<< ONIGENC_CTYPE_WORD)
94 #define BIT_CTYPE_ALNUM    (1<< ONIGENC_CTYPE_ALNUM)
95 #define BIT_CTYPE_ASCII    (1<< ONIGENC_CTYPE_ASCII)
96 
97 #define CTYPE_TO_BIT(ctype)  (1<<(ctype))
98 #define CTYPE_IS_WORD_GRAPH_PRINT(ctype) \
99   ((ctype) == ONIGENC_CTYPE_WORD || (ctype) == ONIGENC_CTYPE_GRAPH ||\
100    (ctype) == ONIGENC_CTYPE_PRINT)
101 
102 
103 typedef struct {
104   UChar    *name;
105   int       ctype;
106   short int len;
107 } PosixBracketEntryType;
108 
109 struct PropertyNameCtype {
110   char *name;
111   int ctype;
112 };
113 
114 /* #define USE_CRNL_AS_LINE_TERMINATOR */
115 #define USE_UNICODE_PROPERTIES
116 #define USE_UNICODE_EXTENDED_GRAPHEME_CLUSTER
117 #define USE_UNICODE_WORD_BREAK
118 /* #define USE_UNICODE_CASE_FOLD_TURKISH_AZERI */
119 /* #define USE_UNICODE_ALL_LINE_TERMINATORS */  /* see Unicode.org UTS #18 */
120 
121 
122 #define ONIG_ENCODING_INIT_DEFAULT           ONIG_ENCODING_ASCII
123 
124 
125 #define ENC_SKIP_OFFSET_1_OR_0             7
126 
127 #define ENC_FLAG_ASCII_COMPATIBLE      (1<<0)
128 #define ENC_FLAG_UNICODE               (1<<1)
129 #define ENC_FLAG_SKIP_OFFSET_MASK      (7<<2)
130 #define ENC_FLAG_SKIP_OFFSET_0             0
131 #define ENC_FLAG_SKIP_OFFSET_1         (1<<2)
132 #define ENC_FLAG_SKIP_OFFSET_2         (2<<2)
133 #define ENC_FLAG_SKIP_OFFSET_3         (3<<2)
134 #define ENC_FLAG_SKIP_OFFSET_4         (4<<2)
135 #define ENC_FLAG_SKIP_OFFSET_1_OR_0    (ENC_SKIP_OFFSET_1_OR_0<<2)
136 
137 #define ENC_GET_SKIP_OFFSET(enc) \
138   (((enc)->flag & ENC_FLAG_SKIP_OFFSET_MASK)>>2)
139 
140 
141 /* for encoding system implementation (internal) */
142 extern int onigenc_end(void);
143 extern int onigenc_ascii_apply_all_case_fold P_((OnigCaseFoldType flag, OnigApplyAllCaseFoldFunc f, void* arg));
144 extern int onigenc_ascii_get_case_fold_codes_by_str P_((OnigCaseFoldType flag, const OnigUChar* p, const OnigUChar* end, OnigCaseFoldCodeItem items[]));
145 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));
146 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[]));
147 extern int onigenc_not_support_get_ctype_code_range P_((OnigCtype ctype, OnigCodePoint* sb_out, const OnigCodePoint* ranges[]));
148 extern int onigenc_is_mbc_newline_0x0a P_((const UChar* p, const UChar* end));
149 
150 
151 /* methods for single byte encoding */
152 extern int onigenc_ascii_mbc_case_fold P_((OnigCaseFoldType flag, const UChar** p, const UChar* end, UChar* lower));
153 extern int onigenc_single_byte_mbc_enc_len P_((const UChar* p));
154 extern OnigCodePoint onigenc_single_byte_mbc_to_code P_((const UChar* p, const UChar* end));
155 extern int onigenc_single_byte_code_to_mbclen P_((OnigCodePoint code));
156 extern int onigenc_single_byte_code_to_mbc P_((OnigCodePoint code, UChar *buf));
157 extern UChar* onigenc_single_byte_left_adjust_char_head P_((const UChar* start, const UChar* s));
158 extern int onigenc_always_true_is_allowed_reverse_match P_((const UChar* s, const UChar* end));
159 extern int onigenc_always_false_is_allowed_reverse_match P_((const UChar* s, const UChar* end));
160 extern int onigenc_always_true_is_valid_mbc_string P_((const UChar* s, const UChar* end));
161 extern int onigenc_length_check_is_valid_mbc_string P_((OnigEncoding enc, const UChar* s, const UChar* end));
162 
163 /* methods for multi byte encoding */
164 extern OnigCodePoint onigenc_mbn_mbc_to_code P_((OnigEncoding enc, const UChar* p, const UChar* end));
165 extern int onigenc_mbn_mbc_case_fold P_((OnigEncoding enc, OnigCaseFoldType flag, const UChar** p, const UChar* end, UChar* lower));
166 extern int onigenc_mb2_code_to_mbc P_((OnigEncoding enc, OnigCodePoint code, UChar *buf));
167 extern int onigenc_minimum_property_name_to_ctype P_((OnigEncoding enc, UChar* p, UChar* end));
168 extern int onigenc_unicode_property_name_to_ctype P_((OnigEncoding enc, UChar* p, UChar* end));
169 extern int onigenc_is_mbc_word_ascii P_((OnigEncoding enc, UChar* s, const UChar* end));
170 extern int onigenc_mb2_is_code_ctype P_((OnigEncoding enc, OnigCodePoint code, unsigned int ctype));
171 extern int onigenc_mb4_code_to_mbc P_((OnigEncoding enc, OnigCodePoint code, UChar *buf));
172 extern int onigenc_mb4_is_code_ctype P_((OnigEncoding enc, OnigCodePoint code, unsigned int ctype));
173 extern struct PropertyNameCtype* onigenc_euc_jp_lookup_property_name P_((register const char *str, register size_t len));
174 extern struct PropertyNameCtype* onigenc_sjis_lookup_property_name P_((register const char *str, register size_t len));
175 
176 /* in unicode.c */
177 extern int onigenc_unicode_is_code_ctype P_((OnigCodePoint code, unsigned int ctype));
178 extern int onigenc_utf16_32_get_ctype_code_range P_((OnigCtype ctype, OnigCodePoint *sb_out, const OnigCodePoint* ranges[]));
179 extern int onigenc_unicode_ctype_code_range P_((OnigCtype ctype, const OnigCodePoint* ranges[]));
180 extern int onigenc_unicode_get_case_fold_codes_by_str P_((OnigEncoding enc, OnigCaseFoldType flag, const OnigUChar* p, const OnigUChar* end, OnigCaseFoldCodeItem items[]));
181 extern int onigenc_unicode_mbc_case_fold P_((OnigEncoding enc, OnigCaseFoldType flag, const UChar** pp, const UChar* end, UChar* fold));
182 extern int onigenc_unicode_apply_all_case_fold P_((OnigCaseFoldType flag, OnigApplyAllCaseFoldFunc f, void* arg));
183 
184 extern int onigenc_egcb_is_break_position P_((OnigEncoding enc, UChar* p, UChar* prev, const UChar* start, const UChar* end));
185 
186 #ifdef USE_UNICODE_WORD_BREAK
187 extern int onigenc_wb_is_break_position P_((OnigEncoding enc, UChar* p, UChar* prev, const UChar* start, const UChar* end));
188 #endif
189 
190 #define UTF16_IS_SURROGATE_FIRST(c)    (((c) & 0xfc) == 0xd8)
191 #define UTF16_IS_SURROGATE_SECOND(c)   (((c) & 0xfc) == 0xdc)
192 
193 /* from unicode generated codes */
194 #define FOLDS1_FOLD(i)         (OnigUnicodeFolds1 + (i))
195 #define FOLDS2_FOLD(i)         (OnigUnicodeFolds2 + (i))
196 #define FOLDS3_FOLD(i)         (OnigUnicodeFolds3 + (i))
197 #define FOLDS1_UNFOLDS_NUM(i)  (OnigUnicodeFolds1[(i)+1])
198 #define FOLDS2_UNFOLDS_NUM(i)  (OnigUnicodeFolds2[(i)+2])
199 #define FOLDS3_UNFOLDS_NUM(i)  (OnigUnicodeFolds3[(i)+3])
200 #define FOLDS1_UNFOLDS(i)      (OnigUnicodeFolds1 + (i) + 2)
201 #define FOLDS2_UNFOLDS(i)      (OnigUnicodeFolds2 + (i) + 3)
202 #define FOLDS3_UNFOLDS(i)      (OnigUnicodeFolds3 + (i) + 4)
203 #define FOLDS1_NEXT_INDEX(i)   ((i) + 2 + OnigUnicodeFolds1[(i)+1])
204 #define FOLDS2_NEXT_INDEX(i)   ((i) + 3 + OnigUnicodeFolds2[(i)+2])
205 #define FOLDS3_NEXT_INDEX(i)   ((i) + 4 + OnigUnicodeFolds3[(i)+3])
206 
207 #define FOLDS_FOLD_ADDR_BUK(buk, addr) do {\
208   if ((buk)->fold_len == 1)\
209     addr = OnigUnicodeFolds1 + (buk)->index;\
210   else if ((buk)->fold_len == 2)\
211     addr = OnigUnicodeFolds2 + (buk)->index;\
212   else if ((buk)->fold_len == 3)\
213     addr = OnigUnicodeFolds3 + (buk)->index;\
214   else\
215     return ONIGERR_INVALID_CODE_POINT_VALUE;\
216 } while (0)
217 
218 extern OnigCodePoint OnigUnicodeFolds1[];
219 extern OnigCodePoint OnigUnicodeFolds2[];
220 extern OnigCodePoint OnigUnicodeFolds3[];
221 
222 struct ByUnfoldKey {
223   OnigCodePoint code;
224   short int     index;
225   short int     fold_len;
226 };
227 
228 extern const struct ByUnfoldKey* onigenc_unicode_unfold_key(OnigCodePoint code);
229 extern int onigenc_unicode_fold1_key(OnigCodePoint code[]);
230 extern int onigenc_unicode_fold2_key(OnigCodePoint code[]);
231 extern int onigenc_unicode_fold3_key(OnigCodePoint code[]);
232 
233 extern int onig_codes_cmp(OnigCodePoint a[], OnigCodePoint b[], int n);
234 extern int onig_codes_byte_at(OnigCodePoint code[], int at);
235 
236 
237 
238 #define ONIGENC_ISO_8859_1_TO_LOWER_CASE(c) \
239   OnigEncISO_8859_1_ToLowerCaseTable[c]
240 #define ONIGENC_ISO_8859_1_TO_UPPER_CASE(c) \
241   OnigEncISO_8859_1_ToUpperCaseTable[c]
242 
243 extern const UChar OnigEncISO_8859_1_ToLowerCaseTable[];
244 extern const UChar OnigEncISO_8859_1_ToUpperCaseTable[];
245 
246 extern int
247 onigenc_with_ascii_strncmp P_((OnigEncoding enc, const UChar* p, const UChar* end, const UChar* sascii /* ascii */, int n));
248 extern UChar*
249 onigenc_step P_((OnigEncoding enc, const UChar* p, const UChar* end, int n));
250 
251 /* defined in regexec.c, but used in enc/xxx.c */
252 extern int  onig_is_in_code_range P_((const UChar* p, OnigCodePoint code));
253 
254 extern OnigEncoding  OnigEncDefaultCharEncoding;
255 extern const UChar  OnigEncAsciiToLowerCaseTable[];
256 extern const UChar  OnigEncAsciiToUpperCaseTable[];
257 extern const unsigned short OnigEncAsciiCtypeTable[];
258 
259 
260 #define ONIGENC_IS_ASCII_CODE(code)  ((code) < 0x80)
261 #define ONIGENC_ASCII_CODE_TO_LOWER_CASE(c) OnigEncAsciiToLowerCaseTable[c]
262 #define ONIGENC_ASCII_CODE_TO_UPPER_CASE(c) OnigEncAsciiToUpperCaseTable[c]
263 #define ONIGENC_IS_ASCII_CODE_CTYPE(code,ctype) \
264   ((OnigEncAsciiCtypeTable[code] & CTYPE_TO_BIT(ctype)) != 0)
265 #define ONIGENC_IS_ASCII_CODE_WORD(code) \
266   ((OnigEncAsciiCtypeTable[code] & CTYPE_TO_BIT(ONIGENC_CTYPE_WORD)) != 0)
267 #define ONIGENC_IS_ASCII_CODE_CASE_AMBIG(code) \
268  (ONIGENC_IS_ASCII_CODE_CTYPE(code, ONIGENC_CTYPE_UPPER) ||\
269   ONIGENC_IS_ASCII_CODE_CTYPE(code, ONIGENC_CTYPE_LOWER))
270 
271 #define ONIGENC_IS_UNICODE_ENCODING(enc) \
272   (((enc)->flag & ENC_FLAG_UNICODE) != 0)
273 
274 #define ONIGENC_IS_ASCII_COMPATIBLE_ENCODING(enc)  \
275   (((enc)->flag & ENC_FLAG_ASCII_COMPATIBLE) != 0)
276 
277 #endif /* REGENC_H */
278