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