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