xref: /PHP-7.3/ext/mbstring/oniguruma/src/regerror.c (revision 1979c5d1)
1 /**********************************************************************
2   regerror.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 #include "regint.h"
31 #include <stdio.h> /* for vsnprintf() */
32 
33 #include <stdarg.h>
34 
35 extern UChar*
onig_error_code_to_format(int code)36 onig_error_code_to_format(int code)
37 {
38   char *p;
39 
40   switch (code) {
41   case ONIG_MISMATCH:
42     p = "mismatch"; break;
43   case ONIG_NO_SUPPORT_CONFIG:
44     p = "no support in this configuration"; break;
45   case ONIG_ABORT:
46     p = "abort"; break;
47   case ONIGERR_MEMORY:
48     p = "fail to memory allocation"; break;
49   case ONIGERR_MATCH_STACK_LIMIT_OVER:
50     p = "match-stack limit over"; break;
51   case ONIGERR_PARSE_DEPTH_LIMIT_OVER:
52     p = "parse depth limit over"; break;
53   case ONIGERR_RETRY_LIMIT_IN_MATCH_OVER:
54     p = "retry-limit-in-match over"; break;
55   case ONIGERR_TYPE_BUG:
56     p = "undefined type (bug)"; break;
57   case ONIGERR_PARSER_BUG:
58     p = "internal parser error (bug)"; break;
59   case ONIGERR_STACK_BUG:
60     p = "stack error (bug)"; break;
61   case ONIGERR_UNDEFINED_BYTECODE:
62     p = "undefined bytecode (bug)"; break;
63   case ONIGERR_UNEXPECTED_BYTECODE:
64     p = "unexpected bytecode (bug)"; break;
65   case ONIGERR_DEFAULT_ENCODING_IS_NOT_SETTED:
66     p = "default multibyte-encoding is not setted"; break;
67   case ONIGERR_SPECIFIED_ENCODING_CANT_CONVERT_TO_WIDE_CHAR:
68     p = "can't convert to wide-char on specified multibyte-encoding"; break;
69   case ONIGERR_FAIL_TO_INITIALIZE:
70     p = "fail to initialize"; break;
71   case ONIGERR_INVALID_ARGUMENT:
72     p = "invalid argument"; break;
73   case ONIGERR_END_PATTERN_AT_LEFT_BRACE:
74     p = "end pattern at left brace"; break;
75   case ONIGERR_END_PATTERN_AT_LEFT_BRACKET:
76     p = "end pattern at left bracket"; break;
77   case ONIGERR_EMPTY_CHAR_CLASS:
78     p = "empty char-class"; break;
79   case ONIGERR_PREMATURE_END_OF_CHAR_CLASS:
80     p = "premature end of char-class"; break;
81   case ONIGERR_END_PATTERN_AT_ESCAPE:
82     p = "end pattern at escape"; break;
83   case ONIGERR_END_PATTERN_AT_META:
84     p = "end pattern at meta"; break;
85   case ONIGERR_END_PATTERN_AT_CONTROL:
86     p = "end pattern at control"; break;
87   case ONIGERR_META_CODE_SYNTAX:
88     p = "invalid meta-code syntax"; break;
89   case ONIGERR_CONTROL_CODE_SYNTAX:
90     p = "invalid control-code syntax"; break;
91   case ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE:
92     p = "char-class value at end of range"; break;
93   case ONIGERR_CHAR_CLASS_VALUE_AT_START_OF_RANGE:
94     p = "char-class value at start of range"; break;
95   case ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS:
96     p = "unmatched range specifier in char-class"; break;
97   case ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED:
98     p = "target of repeat operator is not specified"; break;
99   case ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID:
100     p = "target of repeat operator is invalid"; break;
101   case ONIGERR_NESTED_REPEAT_OPERATOR:
102     p = "nested repeat operator"; break;
103   case ONIGERR_UNMATCHED_CLOSE_PARENTHESIS:
104     p = "unmatched close parenthesis"; break;
105   case ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS:
106     p = "end pattern with unmatched parenthesis"; break;
107   case ONIGERR_END_PATTERN_IN_GROUP:
108     p = "end pattern in group"; break;
109   case ONIGERR_UNDEFINED_GROUP_OPTION:
110     p = "undefined group option"; break;
111   case ONIGERR_INVALID_POSIX_BRACKET_TYPE:
112     p = "invalid POSIX bracket type"; break;
113   case ONIGERR_INVALID_LOOK_BEHIND_PATTERN:
114     p = "invalid pattern in look-behind"; break;
115   case ONIGERR_INVALID_REPEAT_RANGE_PATTERN:
116     p = "invalid repeat range {lower,upper}"; break;
117   case ONIGERR_TOO_BIG_NUMBER:
118     p = "too big number"; break;
119   case ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE:
120     p = "too big number for repeat range"; break;
121   case ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE:
122     p = "upper is smaller than lower in repeat range"; break;
123   case ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS:
124     p = "empty range in char class"; break;
125   case ONIGERR_MISMATCH_CODE_LENGTH_IN_CLASS_RANGE:
126     p = "mismatch multibyte code length in char-class range"; break;
127   case ONIGERR_TOO_MANY_MULTI_BYTE_RANGES:
128     p = "too many multibyte code ranges are specified"; break;
129   case ONIGERR_TOO_SHORT_MULTI_BYTE_STRING:
130     p = "too short multibyte code string"; break;
131   case ONIGERR_TOO_BIG_BACKREF_NUMBER:
132     p = "too big backref number"; break;
133   case ONIGERR_INVALID_BACKREF:
134     p = "invalid backref number/name"; break;
135   case ONIGERR_NUMBERED_BACKREF_OR_CALL_NOT_ALLOWED:
136     p = "numbered backref/call is not allowed. (use name)"; break;
137   case ONIGERR_TOO_MANY_CAPTURES:
138     p = "too many captures"; break;
139   case ONIGERR_TOO_BIG_WIDE_CHAR_VALUE:
140     p = "too big wide-char value"; break;
141   case ONIGERR_TOO_LONG_WIDE_CHAR_VALUE:
142     p = "too long wide-char value"; break;
143   case ONIGERR_INVALID_CODE_POINT_VALUE:
144     p = "invalid code point value"; break;
145   case ONIGERR_EMPTY_GROUP_NAME:
146     p = "group name is empty"; break;
147   case ONIGERR_INVALID_GROUP_NAME:
148     p = "invalid group name <%n>"; break;
149   case ONIGERR_INVALID_CHAR_IN_GROUP_NAME:
150     p = "invalid char in group name <%n>"; break;
151   case ONIGERR_UNDEFINED_NAME_REFERENCE:
152     p = "undefined name <%n> reference"; break;
153   case ONIGERR_UNDEFINED_GROUP_REFERENCE:
154     p = "undefined group <%n> reference"; break;
155   case ONIGERR_MULTIPLEX_DEFINED_NAME:
156     p = "multiplex defined name <%n>"; break;
157   case ONIGERR_MULTIPLEX_DEFINITION_NAME_CALL:
158     p = "multiplex definition name <%n> call"; break;
159   case ONIGERR_NEVER_ENDING_RECURSION:
160     p = "never ending recursion"; break;
161   case ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY:
162     p = "group number is too big for capture history"; break;
163   case ONIGERR_INVALID_CHAR_PROPERTY_NAME:
164     p = "invalid character property name {%n}"; break;
165   case ONIGERR_INVALID_IF_ELSE_SYNTAX:
166     p = "invalid if-else syntax"; break;
167   case ONIGERR_INVALID_ABSENT_GROUP_PATTERN:
168     p = "invalid absent group pattern"; break;
169   case ONIGERR_INVALID_ABSENT_GROUP_GENERATOR_PATTERN:
170     p = "invalid absent group generator pattern"; break;
171   case ONIGERR_INVALID_CALLOUT_PATTERN:
172     p = "invalid callout pattern"; break;
173   case ONIGERR_INVALID_CALLOUT_NAME:
174     p = "invalid callout name"; break;
175   case ONIGERR_UNDEFINED_CALLOUT_NAME:
176     p = "undefined callout name"; break;
177   case ONIGERR_INVALID_CALLOUT_BODY:
178     p = "invalid callout body"; break;
179   case ONIGERR_INVALID_CALLOUT_TAG_NAME:
180     p = "invalid callout tag name"; break;
181   case ONIGERR_INVALID_CALLOUT_ARG:
182     p = "invalid callout arg"; break;
183   case ONIGERR_NOT_SUPPORTED_ENCODING_COMBINATION:
184     p = "not supported encoding combination"; break;
185   case ONIGERR_INVALID_COMBINATION_OF_OPTIONS:
186     p = "invalid combination of options"; break;
187   case ONIGERR_LIBRARY_IS_NOT_INITIALIZED:
188     p = "library is not initialized"; break;
189 
190   default:
191     p = "undefined error code"; break;
192   }
193 
194   return (UChar* )p;
195 }
196 
sprint_byte(char * s,unsigned int v)197 static void sprint_byte(char* s, unsigned int v)
198 {
199   xsnprintf(s, 3, "%02x", (v & 0377));
200 }
201 
sprint_byte_with_x(char * s,unsigned int v)202 static void sprint_byte_with_x(char* s, unsigned int v)
203 {
204   xsnprintf(s, 5, "\\x%02x", (v & 0377));
205 }
206 
to_ascii(OnigEncoding enc,UChar * s,UChar * end,UChar buf[],int buf_size,int * is_over)207 static int to_ascii(OnigEncoding enc, UChar *s, UChar *end,
208                     UChar buf[], int buf_size, int *is_over)
209 {
210   int len;
211   UChar *p;
212   OnigCodePoint code;
213 
214   if (!s) {
215     len = 0;
216     *is_over = 0;
217   }
218   else if (ONIGENC_MBC_MINLEN(enc) > 1) {
219     p = s;
220     len = 0;
221     while (p < end) {
222       code = ONIGENC_MBC_TO_CODE(enc, p, end);
223       if (code >= 0x80) {
224         if (code > 0xffff && len + 10 <= buf_size) {
225           sprint_byte_with_x((char*)(&(buf[len])), (unsigned int)(code >> 24));
226           sprint_byte((char*)(&(buf[len+4])),      (unsigned int)(code >> 16));
227           sprint_byte((char*)(&(buf[len+6])),      (unsigned int)(code >>  8));
228           sprint_byte((char*)(&(buf[len+8])),      (unsigned int)code);
229           len += 10;
230         }
231         else if (len + 6 <= buf_size) {
232           sprint_byte_with_x((char*)(&(buf[len])), (unsigned int)(code >> 8));
233           sprint_byte((char*)(&(buf[len+4])),      (unsigned int)code);
234           len += 6;
235         }
236         else {
237           break;
238         }
239       }
240       else {
241         buf[len++] = (UChar )code;
242       }
243 
244       p += enclen(enc, p);
245       if (len >= buf_size) break;
246     }
247 
248     *is_over = p < end;
249   }
250   else {
251     len = MIN((int )(end - s), buf_size);
252     xmemcpy(buf, s, (size_t )len);
253     *is_over = ((buf_size < (end - s)) ? 1 : 0);
254   }
255 
256   return len;
257 }
258 
259 
260 extern int
onig_is_error_code_needs_param(int code)261 onig_is_error_code_needs_param(int code)
262 {
263   switch (code) {
264   case ONIGERR_UNDEFINED_NAME_REFERENCE:
265   case ONIGERR_UNDEFINED_GROUP_REFERENCE:
266   case ONIGERR_MULTIPLEX_DEFINED_NAME:
267   case ONIGERR_MULTIPLEX_DEFINITION_NAME_CALL:
268   case ONIGERR_INVALID_GROUP_NAME:
269   case ONIGERR_INVALID_CHAR_IN_GROUP_NAME:
270   case ONIGERR_INVALID_CHAR_PROPERTY_NAME:
271     return 1;
272   default:
273     return 0;
274   }
275 }
276 
277 /* for ONIG_MAX_ERROR_MESSAGE_LEN */
278 #define MAX_ERROR_PAR_LEN   30
279 
onig_error_code_to_str(UChar * s,int code,...)280 extern int onig_error_code_to_str(UChar* s, int code, ...)
281 {
282   UChar *p, *q;
283   OnigErrorInfo* einfo;
284   int len, is_over;
285   UChar parbuf[MAX_ERROR_PAR_LEN];
286   va_list vargs;
287 
288   va_start(vargs, code);
289 
290   switch (code) {
291   case ONIGERR_UNDEFINED_NAME_REFERENCE:
292   case ONIGERR_UNDEFINED_GROUP_REFERENCE:
293   case ONIGERR_MULTIPLEX_DEFINED_NAME:
294   case ONIGERR_MULTIPLEX_DEFINITION_NAME_CALL:
295   case ONIGERR_INVALID_GROUP_NAME:
296   case ONIGERR_INVALID_CHAR_IN_GROUP_NAME:
297   case ONIGERR_INVALID_CHAR_PROPERTY_NAME:
298     einfo = va_arg(vargs, OnigErrorInfo*);
299     len = to_ascii(einfo->enc, einfo->par, einfo->par_end,
300                    parbuf, MAX_ERROR_PAR_LEN - 3, &is_over);
301     q = onig_error_code_to_format(code);
302     p = s;
303     while (*q != '\0') {
304       if (*q == '%') {
305         q++;
306         if (*q == 'n') { /* '%n': name */
307           xmemcpy(p, parbuf, len);
308           p += len;
309           if (is_over != 0) {
310             xmemcpy(p, "...", 3);
311             p += 3;
312           }
313           q++;
314         }
315         else
316           goto normal_char;
317       }
318       else {
319       normal_char:
320         *p++ = *q++;
321       }
322     }
323     *p = '\0';
324     len = (int )(p - s);
325     break;
326 
327   default:
328     q = onig_error_code_to_format(code);
329     len = onigenc_str_bytelen_null(ONIG_ENCODING_ASCII, q);
330     xmemcpy(s, q, len);
331     s[len] = '\0';
332     break;
333   }
334 
335   va_end(vargs);
336   return len;
337 }
338 
339 
onig_snprintf_with_pattern(UChar buf[],int bufsize,OnigEncoding enc,UChar * pat,UChar * pat_end,const UChar * fmt,...)340 void onig_snprintf_with_pattern(UChar buf[], int bufsize, OnigEncoding enc,
341                                 UChar* pat, UChar* pat_end, const UChar *fmt, ...)
342 {
343   int n, need, len;
344   UChar *p, *s, *bp;
345   UChar bs[6];
346   va_list args;
347 
348   va_start(args, fmt);
349   n = xvsnprintf((char* )buf, bufsize, (const char* )fmt, args);
350   va_end(args);
351 
352   need = (int )(pat_end - pat) * 4 + 4;
353 
354   if (n + need < bufsize) {
355     xstrcat((char* )buf, ": /", bufsize);
356     s = buf + onigenc_str_bytelen_null(ONIG_ENCODING_ASCII, buf);
357 
358     p = pat;
359     while (p < pat_end) {
360       if (ONIGENC_IS_MBC_HEAD(enc, p)) {
361         len = enclen(enc, p);
362         if (ONIGENC_MBC_MINLEN(enc) == 1) {
363           while (len-- > 0) *s++ = *p++;
364         }
365         else { /* for UTF16/32 */
366           int blen;
367 
368           while (len-- > 0) {
369             sprint_byte_with_x((char* )bs, (unsigned int )(*p++));
370             blen = onigenc_str_bytelen_null(ONIG_ENCODING_ASCII, bs);
371             bp = bs;
372             while (blen-- > 0) *s++ = *bp++;
373           }
374         }
375       }
376       else if (*p == '\\') {
377         *s++ = *p++;
378         len = enclen(enc, p);
379         while (len-- > 0) *s++ = *p++;
380       }
381       else if (*p == '/') {
382         *s++ = (unsigned char )'\\';
383         *s++ = *p++;
384       }
385       else if (!ONIGENC_IS_CODE_PRINT(enc, *p) &&
386                !ONIGENC_IS_CODE_SPACE(enc, *p)) {
387         sprint_byte_with_x((char* )bs, (unsigned int )(*p++));
388         len = onigenc_str_bytelen_null(ONIG_ENCODING_ASCII, bs);
389         bp = bs;
390         while (len-- > 0) *s++ = *bp++;
391       }
392       else {
393         *s++ = *p++;
394       }
395     }
396 
397     *s++ = '/';
398     *s   = '\0';
399   }
400 }
401