xref: /PHP-7.3/ext/mbstring/oniguruma/src/regposix.c (revision 1979c5d1)
1 /**********************************************************************
2   regposix.c - Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5  * Copyright (c) 2002-2019  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 #define regex_t   onig_regex_t
31 #include "regint.h"
32 #undef regex_t
33 
34 #include "onigposix.h"
35 
36 #define ONIG_C(reg)    ((onig_regex_t* )((reg)->onig))
37 #define PONIG_C(reg)   ((onig_regex_t** )(&(reg)->onig))
38 
39 /* #define ENC_STRING_LEN(enc,s,len)    len = strlen(s) */
40 #define ENC_STRING_LEN(enc,s,len) do { \
41   if (ONIGENC_MBC_MINLEN(enc) == 1) { \
42     UChar* tmps = (UChar* )(s); \
43     while (*tmps != 0) tmps++; \
44     len = (int )(tmps - (UChar* )(s));\
45   } \
46   else { \
47     len = onigenc_str_bytelen_null(enc, (UChar* )s); \
48   } \
49 } while(0)
50 
51 typedef struct {
52   int onig_err;
53   int posix_err;
54 } O2PERR;
55 
56 static int
onig2posix_error_code(int code)57 onig2posix_error_code(int code)
58 {
59   static const O2PERR o2p[] = {
60     { ONIG_MISMATCH,                                      REG_NOMATCH },
61     { ONIG_NO_SUPPORT_CONFIG,                             REG_EONIG_INTERNAL },
62     { ONIG_ABORT,                                         REG_EONIG_INTERNAL },
63     { ONIGERR_MEMORY,                                     REG_ESPACE  },
64     { ONIGERR_MATCH_STACK_LIMIT_OVER,                     REG_EONIG_INTERNAL },
65     { ONIGERR_RETRY_LIMIT_IN_MATCH_OVER,                  REG_EONIG_INTERNAL },
66     { ONIGERR_TYPE_BUG,                                   REG_EONIG_INTERNAL },
67     { ONIGERR_PARSER_BUG,                                 REG_EONIG_INTERNAL },
68     { ONIGERR_STACK_BUG,                                  REG_EONIG_INTERNAL },
69     { ONIGERR_UNDEFINED_BYTECODE,                         REG_EONIG_INTERNAL },
70     { ONIGERR_UNEXPECTED_BYTECODE,                        REG_EONIG_INTERNAL },
71     { ONIGERR_DEFAULT_ENCODING_IS_NOT_SETTED,             REG_EONIG_BADARG },
72     { ONIGERR_SPECIFIED_ENCODING_CANT_CONVERT_TO_WIDE_CHAR, REG_EONIG_BADARG },
73     { ONIGERR_FAIL_TO_INITIALIZE,                         REG_EONIG_INTERNAL },
74     { ONIGERR_INVALID_ARGUMENT,                           REG_EONIG_BADARG },
75     { ONIGERR_END_PATTERN_AT_LEFT_BRACE,                  REG_EBRACE  },
76     { ONIGERR_END_PATTERN_AT_LEFT_BRACKET,                REG_EBRACK  },
77     { ONIGERR_EMPTY_CHAR_CLASS,                           REG_ECTYPE  },
78     { ONIGERR_PREMATURE_END_OF_CHAR_CLASS,                REG_ECTYPE  },
79     { ONIGERR_END_PATTERN_AT_ESCAPE,                      REG_EESCAPE },
80     { ONIGERR_END_PATTERN_AT_META,                        REG_EESCAPE },
81     { ONIGERR_END_PATTERN_AT_CONTROL,                     REG_EESCAPE },
82     { ONIGERR_META_CODE_SYNTAX,                           REG_BADPAT  },
83     { ONIGERR_CONTROL_CODE_SYNTAX,                        REG_BADPAT  },
84     { ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE,           REG_ECTYPE  },
85     { ONIGERR_CHAR_CLASS_VALUE_AT_START_OF_RANGE,         REG_ECTYPE  },
86     { ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS,    REG_ECTYPE  },
87     { ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED,    REG_BADRPT  },
88     { ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID,          REG_BADRPT  },
89     { ONIGERR_NESTED_REPEAT_OPERATOR,                     REG_BADRPT  },
90     { ONIGERR_UNMATCHED_CLOSE_PARENTHESIS,                REG_EPAREN  },
91     { ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS,     REG_EPAREN  },
92     { ONIGERR_END_PATTERN_IN_GROUP,                       REG_BADPAT  },
93     { ONIGERR_UNDEFINED_GROUP_OPTION,                     REG_BADPAT  },
94     { ONIGERR_INVALID_POSIX_BRACKET_TYPE,                 REG_BADPAT  },
95     { ONIGERR_INVALID_LOOK_BEHIND_PATTERN,                REG_BADPAT  },
96     { ONIGERR_INVALID_REPEAT_RANGE_PATTERN,               REG_BADPAT  },
97     { ONIGERR_TOO_BIG_NUMBER,                             REG_BADPAT  },
98     { ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE,            REG_BADBR   },
99     { ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE,   REG_BADBR   },
100     { ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS,                  REG_ECTYPE  },
101     { ONIGERR_MISMATCH_CODE_LENGTH_IN_CLASS_RANGE,        REG_ECTYPE  },
102     { ONIGERR_TOO_MANY_MULTI_BYTE_RANGES,                 REG_ECTYPE  },
103     { ONIGERR_TOO_SHORT_MULTI_BYTE_STRING,                REG_BADPAT  },
104     { ONIGERR_TOO_BIG_BACKREF_NUMBER,                     REG_ESUBREG },
105     { ONIGERR_INVALID_BACKREF,                            REG_ESUBREG },
106     { ONIGERR_NUMBERED_BACKREF_OR_CALL_NOT_ALLOWED,       REG_BADPAT  },
107     { ONIGERR_TOO_BIG_WIDE_CHAR_VALUE,                    REG_EONIG_BADWC },
108     { ONIGERR_TOO_LONG_WIDE_CHAR_VALUE,                   REG_EONIG_BADWC },
109     { ONIGERR_INVALID_CODE_POINT_VALUE,                   REG_EONIG_BADWC },
110     { ONIGERR_EMPTY_GROUP_NAME,                           REG_BADPAT },
111     { ONIGERR_INVALID_GROUP_NAME,                         REG_BADPAT },
112     { ONIGERR_INVALID_CHAR_IN_GROUP_NAME,                 REG_BADPAT },
113     { ONIGERR_UNDEFINED_NAME_REFERENCE,                   REG_BADPAT },
114     { ONIGERR_UNDEFINED_GROUP_REFERENCE,                  REG_BADPAT },
115     { ONIGERR_MULTIPLEX_DEFINED_NAME,                     REG_BADPAT },
116     { ONIGERR_MULTIPLEX_DEFINITION_NAME_CALL,             REG_BADPAT },
117     { ONIGERR_NEVER_ENDING_RECURSION,                     REG_BADPAT },
118     { ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY,      REG_BADPAT },
119     { ONIGERR_INVALID_CHAR_PROPERTY_NAME,                 REG_BADPAT },
120     { ONIGERR_INVALID_IF_ELSE_SYNTAX,                     REG_BADPAT },
121     { ONIGERR_INVALID_ABSENT_GROUP_PATTERN,               REG_BADPAT },
122     { ONIGERR_INVALID_ABSENT_GROUP_GENERATOR_PATTERN,     REG_BADPAT },
123     { ONIGERR_INVALID_CALLOUT_PATTERN,                    REG_BADPAT },
124     { ONIGERR_INVALID_CALLOUT_NAME,                       REG_BADPAT },
125     { ONIGERR_UNDEFINED_CALLOUT_NAME,                     REG_BADPAT },
126     { ONIGERR_INVALID_CALLOUT_BODY,                       REG_BADPAT },
127     { ONIGERR_INVALID_CALLOUT_TAG_NAME,                   REG_BADPAT },
128     { ONIGERR_INVALID_CALLOUT_ARG,                        REG_BADPAT },
129     { ONIGERR_NOT_SUPPORTED_ENCODING_COMBINATION,         REG_EONIG_BADARG },
130     { ONIGERR_LIBRARY_IS_NOT_INITIALIZED,                 REG_EONIG_INTERNAL }
131   };
132 
133   int i;
134 
135   if (code >= 0) return 0;
136 
137   for (i = 0; i < (int )(sizeof(o2p) / sizeof(o2p[0])); i++) {
138     if (code == o2p[i].onig_err)
139       return o2p[i].posix_err;
140   }
141 
142   return REG_EONIG_INTERNAL;  /* but, unknown error code */
143 }
144 
145 extern int
regcomp(regex_t * reg,const char * pattern,int posix_options)146 regcomp(regex_t* reg, const char* pattern, int posix_options)
147 {
148   int r, len;
149   OnigSyntaxType* syntax = OnigDefaultSyntax;
150   OnigOptionType options;
151 
152   reg->onig = (void* )0;
153 
154   if ((posix_options & REG_EXTENDED) == 0)
155     syntax = ONIG_SYNTAX_POSIX_BASIC;
156 
157   options = syntax->options;
158   if ((posix_options & REG_ICASE)   != 0)
159     ONIG_OPTION_ON(options, ONIG_OPTION_IGNORECASE);
160   if ((posix_options & REG_NEWLINE) != 0) {
161     ONIG_OPTION_ON( options, ONIG_OPTION_NEGATE_SINGLELINE);
162     ONIG_OPTION_OFF(options, ONIG_OPTION_SINGLELINE);
163   }
164 
165   reg->comp_options = posix_options;
166 
167   ENC_STRING_LEN(OnigEncDefaultCharEncoding, pattern, len);
168   r = onig_new(PONIG_C(reg), (UChar* )pattern, (UChar* )(pattern + len),
169                options, OnigEncDefaultCharEncoding, syntax,
170                (OnigErrorInfo* )NULL);
171   if (r != ONIG_NORMAL) {
172     return onig2posix_error_code(r);
173   }
174 
175   reg->re_nsub = ONIG_C(reg)->num_mem;
176   return 0;
177 }
178 
179 extern int
regexec(regex_t * reg,const char * str,size_t nmatch,regmatch_t pmatch[],int posix_options)180 regexec(regex_t* reg, const char* str, size_t nmatch,
181         regmatch_t pmatch[], int posix_options)
182 {
183   int r, i, len;
184   UChar* end;
185   regmatch_t* pm;
186   OnigOptionType options;
187 
188   options = ONIG_OPTION_POSIX_REGION;
189   if ((posix_options & REG_NOTBOL) != 0) options |= ONIG_OPTION_NOTBOL;
190   if ((posix_options & REG_NOTEOL) != 0) options |= ONIG_OPTION_NOTEOL;
191 
192   if (nmatch == 0 || (reg->comp_options & REG_NOSUB) != 0) {
193     pm = (regmatch_t* )NULL;
194     nmatch = 0;
195   }
196   else if ((int )nmatch < ONIG_C(reg)->num_mem + 1) {
197     pm = (regmatch_t* )xmalloc(sizeof(regmatch_t)
198                                * (ONIG_C(reg)->num_mem + 1));
199     if (pm == NULL)
200       return REG_ESPACE;
201   }
202   else {
203     pm = pmatch;
204   }
205 
206   ENC_STRING_LEN(ONIG_C(reg)->enc, str, len);
207   end = (UChar* )(str + len);
208   r = onig_search(ONIG_C(reg), (UChar* )str, end, (UChar* )str, end,
209                   (OnigRegion* )pm, options);
210 
211   if (r >= 0) {
212     r = 0; /* Match */
213     if (pm != pmatch && pm != NULL) {
214       xmemcpy(pmatch, pm, sizeof(regmatch_t) * nmatch);
215     }
216   }
217   else if (r == ONIG_MISMATCH) {
218     r = REG_NOMATCH;
219     for (i = 0; i < (int )nmatch; i++)
220       pmatch[i].rm_so = pmatch[i].rm_eo = ONIG_REGION_NOTPOS;
221   }
222   else {
223     r = onig2posix_error_code(r);
224   }
225 
226   if (pm != pmatch && pm != NULL)
227     xfree(pm);
228 
229 #if 0
230   if (reg->re_nsub > nmatch - 1)
231     reg->re_nsub = (nmatch <= 1 ? 0 : nmatch - 1);
232 #endif
233 
234   return r;
235 }
236 
237 extern void
regfree(regex_t * reg)238 regfree(regex_t* reg)
239 {
240   onig_free(ONIG_C(reg));
241   reg->onig = (void* )0;
242 }
243 
244 
245 extern void
reg_set_encoding(int mb_code)246 reg_set_encoding(int mb_code)
247 {
248   OnigEncoding enc;
249 
250   switch (mb_code) {
251   case REG_POSIX_ENCODING_ASCII:
252     enc = ONIG_ENCODING_ASCII;
253     break;
254   case REG_POSIX_ENCODING_EUC_JP:
255     enc = ONIG_ENCODING_EUC_JP;
256     break;
257   case REG_POSIX_ENCODING_SJIS:
258     enc = ONIG_ENCODING_SJIS;
259     break;
260   case REG_POSIX_ENCODING_UTF8:
261     enc = ONIG_ENCODING_UTF8;
262     break;
263   case REG_POSIX_ENCODING_UTF16_BE:
264     enc = ONIG_ENCODING_UTF16_BE;
265     break;
266   case REG_POSIX_ENCODING_UTF16_LE:
267     enc = ONIG_ENCODING_UTF16_LE;
268     break;
269 
270   default:
271     return ;
272     break;
273   }
274 
275   onig_initialize(&enc, 1);
276 
277   onigenc_set_default_encoding(enc);
278 }
279 
280 extern int
reg_name_to_group_numbers(regex_t * reg,const unsigned char * name,const unsigned char * name_end,int ** nums)281 reg_name_to_group_numbers(regex_t* reg,
282   const unsigned char* name, const unsigned char* name_end, int** nums)
283 {
284   return onig_name_to_group_numbers(ONIG_C(reg), name, name_end, nums);
285 }
286 
287 typedef struct {
288   int (*func)(const unsigned char*, const unsigned char*,int,int*,regex_t*,void*);
289   regex_t* reg;
290   void* arg;
291 } i_wrap;
292 
293 static int
i_wrapper(const UChar * name,const UChar * name_end,int ng,int * gs,onig_regex_t * reg ARG_UNUSED,void * arg)294 i_wrapper(const UChar* name, const UChar* name_end, int ng, int* gs,
295           onig_regex_t* reg ARG_UNUSED, void* arg)
296 {
297   i_wrap* warg = (i_wrap* )arg;
298 
299   return (*warg->func)(name, name_end, ng, gs, warg->reg, warg->arg);
300 }
301 
302 extern int
reg_foreach_name(regex_t * reg,int (* func)(const unsigned char *,const unsigned char *,int,int *,regex_t *,void *),void * arg)303 reg_foreach_name(regex_t* reg,
304  int (*func)(const unsigned char*, const unsigned char*,int,int*,regex_t*,void*),
305  void* arg)
306 {
307   i_wrap warg;
308 
309   warg.func = func;
310   warg.reg  = reg;
311   warg.arg  = arg;
312 
313   return onig_foreach_name(ONIG_C(reg), i_wrapper, &warg);
314 }
315 
316 extern int
reg_number_of_names(regex_t * reg)317 reg_number_of_names(regex_t* reg)
318 {
319   return onig_number_of_names(ONIG_C(reg));
320 }
321