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