Ruby: regerror.c Source File

Ruby 3.4.9p82 (2026-03-11 revision 76cca827ab52ab1d346a728f068d5b8da3e2952b)

regerror.c
1/**********************************************************************
2 regerror.c - Onigmo (Oniguruma-mod) (regular expression library)
3**********************************************************************/
4/*-
5 * Copyright (c) 2002-2007 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
6 * Copyright (c) 2011-2016 K.Takata <kentkt AT csc DOT jp>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include "regint.h"
32#include <stdio.h> /* for vsnprintf() */
33
34#include <stdarg.h>
35
36extern UChar*
37onig_error_code_to_format(OnigPosition code)
38{
39 const char *p;
40
41 if (code >= 0) return (UChar* )0;
42
43 switch (code) {
44 case ONIG_MISMATCH:
45 p = "mismatch"; break;
46 case ONIG_NO_SUPPORT_CONFIG:
47 p = "no support in this configuration"; break;
48 case ONIGERR_MEMORY:
49 p = "failed to allocate memory"; break;
50 case ONIGERR_TYPE_BUG:
51 p = "undefined type (bug)"; break;
52 case ONIGERR_PARSER_BUG:
53 p = "internal parser error (bug)"; break;
54 case ONIGERR_STACK_BUG:
55 p = "stack error (bug)"; break;
56 case ONIGERR_UNDEFINED_BYTECODE:
57 p = "undefined bytecode (bug)"; break;
58 case ONIGERR_UNEXPECTED_BYTECODE:
59 p = "unexpected bytecode (bug)"; break;
60 case ONIGERR_MATCH_STACK_LIMIT_OVER:
61 p = "match-stack limit over"; break;
62 case ONIGERR_PARSE_DEPTH_LIMIT_OVER:
63 p = "parse depth limit over"; break;
64 case ONIGERR_DEFAULT_ENCODING_IS_NOT_SET:
65 p = "default multibyte-encoding is not set"; break;
66 case ONIGERR_SPECIFIED_ENCODING_CANT_CONVERT_TO_WIDE_CHAR:
67 p = "can't convert to wide-char on specified multibyte-encoding"; break;
68 case ONIGERR_INVALID_ARGUMENT:
69 p = "invalid argument"; break;
70 case ONIGERR_END_PATTERN_AT_LEFT_BRACE:
71 p = "end pattern at left brace"; break;
72 case ONIGERR_END_PATTERN_AT_LEFT_BRACKET:
73 p = "end pattern at left bracket"; break;
74 case ONIGERR_EMPTY_CHAR_CLASS:
75 p = "empty char-class"; break;
76 case ONIGERR_PREMATURE_END_OF_CHAR_CLASS:
77 p = "premature end of char-class"; break;
78 case ONIGERR_END_PATTERN_AT_ESCAPE:
79 p = "end pattern at escape"; break;
80 case ONIGERR_END_PATTERN_AT_META:
81 p = "end pattern at meta"; break;
82 case ONIGERR_END_PATTERN_AT_CONTROL:
83 p = "end pattern at control"; break;
84 case ONIGERR_META_CODE_SYNTAX:
85 p = "invalid meta-code syntax"; break;
86 case ONIGERR_CONTROL_CODE_SYNTAX:
87 p = "invalid control-code syntax"; break;
88 case ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE:
89 p = "char-class value at end of range"; break;
90 case ONIGERR_CHAR_CLASS_VALUE_AT_START_OF_RANGE:
91 p = "char-class value at start of range"; break;
92 case ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS:
93 p = "unmatched range specifier in char-class"; break;
94 case ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED:
95 p = "target of repeat operator is not specified"; break;
96 case ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID:
97 p = "target of repeat operator is invalid"; break;
98 case ONIGERR_NESTED_REPEAT_OPERATOR:
99 p = "nested repeat operator"; break;
100 case ONIGERR_UNMATCHED_CLOSE_PARENTHESIS:
101 p = "unmatched close parenthesis"; break;
102 case ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS:
103 p = "end pattern with unmatched parenthesis"; break;
104 case ONIGERR_END_PATTERN_IN_GROUP:
105 p = "end pattern in group"; break;
106 case ONIGERR_UNDEFINED_GROUP_OPTION:
107 p = "undefined group option"; break;
108 case ONIGERR_INVALID_POSIX_BRACKET_TYPE:
109 p = "invalid POSIX bracket type"; break;
110 case ONIGERR_INVALID_LOOK_BEHIND_PATTERN:
111 p = "invalid pattern in look-behind"; break;
112 case ONIGERR_INVALID_REPEAT_RANGE_PATTERN:
113 p = "invalid repeat range {lower,upper}"; break;
114 case ONIGERR_INVALID_CONDITION_PATTERN:
115 p = "invalid conditional pattern"; break;
116 case ONIGERR_TOO_BIG_NUMBER:
117 p = "too big number"; break;
118 case ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE:
119 p = "too big number for repeat range"; break;
120 case ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE:
121 p = "upper is smaller than lower in repeat range"; break;
122 case ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS:
123 p = "empty range in char class"; break;
124 case ONIGERR_MISMATCH_CODE_LENGTH_IN_CLASS_RANGE:
125 p = "mismatch multibyte code length in char-class range"; break;
126 case ONIGERR_TOO_MANY_MULTI_BYTE_RANGES:
127 p = "too many multibyte code ranges are specified"; break;
128 case ONIGERR_TOO_SHORT_MULTI_BYTE_STRING:
129 p = "too short multibyte code string"; break;
130 case ONIGERR_TOO_BIG_BACKREF_NUMBER:
131 p = "too big backref number"; break;
132 case ONIGERR_INVALID_BACKREF:
133#ifdef USE_NAMED_GROUP
134 p = "invalid backref number/name"; break;
135#else
136 p = "invalid backref number"; break;
137#endif
138 case ONIGERR_NUMBERED_BACKREF_OR_CALL_NOT_ALLOWED:
139 p = "numbered backref/call is not allowed. (use name)"; break;
140 case ONIGERR_TOO_SHORT_DIGITS:
141 p = "too short digits"; break;
142 case ONIGERR_TOO_LONG_WIDE_CHAR_VALUE:
143 p = "too long wide-char value"; break;
144 case ONIGERR_EMPTY_GROUP_NAME:
145 p = "group name is empty"; break;
146 case ONIGERR_INVALID_GROUP_NAME:
147 p = "invalid group name <%n>"; break;
148 case ONIGERR_INVALID_CHAR_IN_GROUP_NAME:
149#ifdef USE_NAMED_GROUP
150 p = "invalid char in group name <%n>"; break;
151#else
152 p = "invalid char in group number <%n>"; break;
153#endif
154 case ONIGERR_UNDEFINED_NAME_REFERENCE:
155 p = "undefined name <%n> reference"; break;
156 case ONIGERR_UNDEFINED_GROUP_REFERENCE:
157 p = "undefined group <%n> reference"; break;
158 case ONIGERR_MULTIPLEX_DEFINED_NAME:
159 p = "multiplex defined name <%n>"; break;
160 case ONIGERR_MULTIPLEX_DEFINITION_NAME_CALL:
161 p = "multiplex definition name <%n> call"; break;
162 case ONIGERR_NEVER_ENDING_RECURSION:
163 p = "never ending recursion"; break;
164 case ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY:
165 p = "group number is too big for capture history"; break;
166 case ONIGERR_INVALID_CHAR_PROPERTY_NAME:
167 p = "invalid character property name {%n}"; break;
168 case ONIGERR_TOO_MANY_CAPTURE_GROUPS:
169 p = "too many capture groups are specified"; break;
170 case ONIGERR_INVALID_CODE_POINT_VALUE:
171 p = "invalid code point value"; break;
172 case ONIGERR_TOO_BIG_WIDE_CHAR_VALUE:
173 p = "too big wide-char value"; break;
174 case ONIGERR_NOT_SUPPORTED_ENCODING_COMBINATION:
175 p = "not supported encoding combination"; break;
176 case ONIGERR_INVALID_COMBINATION_OF_OPTIONS:
177 p = "invalid combination of options"; break;
178
179 default:
180 p = "undefined error code"; break;
181 }
182
183 return (UChar* )p;
184}
185
186static void sprint_byte(char* s, unsigned int v)
187{
188 xsnprintf(s, 3, "%02x", (v & 0377));
189}
190
191static void sprint_byte_with_x(char* s, unsigned int v)
192{
193 xsnprintf(s, 5, "\\x%02x", (v & 0377));
194}
195
196static int to_ascii(OnigEncoding enc, UChar *s, UChar *end,
197 UChar buf[], int buf_size, int *is_over)
198{
199 int len;
200 UChar *p;
201 OnigCodePoint code;
202
203 if (ONIGENC_MBC_MINLEN(enc) > 1) {
204 p = s;
205 len = 0;
206 while (p < end) {
207 code = ONIGENC_MBC_TO_CODE(enc, p, end);
208 if (code >= 0x80) {
209 if (code > 0xffff && len + 10 <= buf_size) {
210 sprint_byte_with_x((char*)(&(buf[len])), (unsigned int)(code >> 24));
211 sprint_byte((char*)(&(buf[len+4])), (unsigned int)(code >> 16));
212 sprint_byte((char*)(&(buf[len+6])), (unsigned int)(code >> 8));
213 sprint_byte((char*)(&(buf[len+8])), (unsigned int)code);
214 len += 10;
215 }
216 else if (len + 6 <= buf_size) {
217 sprint_byte_with_x((char*)(&(buf[len])), (unsigned int)(code >> 8));
218 sprint_byte((char*)(&(buf[len+4])), (unsigned int)code);
219 len += 6;
220 }
221 else {
222 break;
223 }
224 }
225 else {
226 buf[len++] = (UChar )code;
227 }
228
229 p += enclen(enc, p, end);
230 if (len >= buf_size) break;
231 }
232
233 *is_over = ((p < end) ? 1 : 0);
234 }
235 else {
236 len = (int )MIN((end - s), buf_size);
237 xmemcpy(buf, s, (size_t )len);
238 *is_over = ((buf_size < (end - s)) ? 1 : 0);
239 }
240
241 return len;
242}
243
244
245/* < ONIG_MAX_ERROR_MESSAGE_LEN - max length of messages with %n */
246#define MAX_ERROR_PAR_LEN 50
247
248extern int
249onig_error_code_to_str(UChar* s, OnigPosition code, ...)
250{
251 UChar *p, *q;
252 OnigErrorInfo* einfo;
253 size_t len;
254 int is_over;
255 UChar parbuf[MAX_ERROR_PAR_LEN];
256 va_list vargs;
257
258 va_start(vargs, code);
259
260 switch (code) {
261 case ONIGERR_UNDEFINED_NAME_REFERENCE:
262 case ONIGERR_UNDEFINED_GROUP_REFERENCE:
263 case ONIGERR_MULTIPLEX_DEFINED_NAME:
264 case ONIGERR_MULTIPLEX_DEFINITION_NAME_CALL:
265 case ONIGERR_INVALID_GROUP_NAME:
266 case ONIGERR_INVALID_CHAR_IN_GROUP_NAME:
267 case ONIGERR_INVALID_CHAR_PROPERTY_NAME:
268 einfo = va_arg(vargs, OnigErrorInfo*);
269 len = to_ascii(einfo->enc, einfo->par, einfo->par_end,
270 parbuf, MAX_ERROR_PAR_LEN - 3, &is_over);
271 q = onig_error_code_to_format(code);
272 p = s;
273 while (*q != '\0') {
274 if (*q == '%') {
275 q++;
276 if (*q == 'n') { /* '%n': name */
277 xmemcpy(p, parbuf, len);
278 p += len;
279 if (is_over != 0) {
280 xmemcpy(p, "...", 3);
281 p += 3;
282 }
283 q++;
284 }
285 else
286 goto normal_char;
287 }
288 else {
289 normal_char:
290 *p++ = *q++;
291 }
292 }
293 *p = '\0';
294 len = p - s;
295 break;
296
297 default:
298 q = onig_error_code_to_format(code);
299 if (q) {
300 len = onigenc_str_bytelen_null(ONIG_ENCODING_ASCII, q);
301 xmemcpy(s, q, len);
302 } else {
303 len = 0;
304 }
305 s[len] = '\0';
306 break;
307 }
308
309 va_end(vargs);
310 return (int )len;
311}
312
313void
314onig_vsnprintf_with_pattern(UChar buf[], int bufsize, OnigEncoding enc,
315 UChar* pat, UChar* pat_end, const char *fmt, va_list args)
316{
317 size_t need;
318 int n, len;
319 UChar *p, *s, *bp;
320 UChar bs[6];
321
322 n = xvsnprintf((char* )buf, bufsize, (const char* )fmt, args);
323
324 need = (pat_end - pat) * 4 + 4;
325
326 if (n + need < (size_t )bufsize) {
327 static const char sep[] = ": /";
328 memcpy((char* )buf + n, sep, sizeof(sep));
329 s = buf + onigenc_str_bytelen_null(ONIG_ENCODING_ASCII, buf);
330
331 p = pat;
332 while (p < pat_end) {
333 if (ONIGENC_IS_MBC_HEAD(enc, p, pat_end)) {
334 len = enclen(enc, p, pat_end);
335 if (ONIGENC_MBC_MINLEN(enc) == 1) {
336 while (len-- > 0) *s++ = *p++;
337 }
338 else { /* for UTF16/32 */
339 int blen;
340
341 while (len-- > 0) {
342 sprint_byte_with_x((char* )bs, (unsigned int )(*p++));
343 blen = onigenc_str_bytelen_null(ONIG_ENCODING_ASCII, bs);
344 bp = bs;
345 while (blen-- > 0) *s++ = *bp++;
346 }
347 }
348 }
349 else if (*p == '\\') {
350 *s++ = *p++;
351 len = enclen(enc, p, pat_end);
352 while (len-- > 0) *s++ = *p++;
353 }
354 else if (*p == '/') {
355 *s++ = (unsigned char )'\\';
356 *s++ = *p++;
357 }
358 else if (!ONIGENC_IS_CODE_PRINT(enc, *p) &&
359 (!ONIGENC_IS_CODE_SPACE(enc, *p) ||
360 ONIGENC_IS_CODE_CNTRL(enc, *p))) {
361 sprint_byte_with_x((char* )bs, (unsigned int )(*p++));
362 len = onigenc_str_bytelen_null(ONIG_ENCODING_ASCII, bs);
363 bp = bs;
364 while (len-- > 0) *s++ = *bp++;
365 }
366 else {
367 *s++ = *p++;
368 }
369 }
370
371 *s++ = '/';
372 *s = '\0';
373 }
374}
375
376#if 0 /* unused */
377void
378onig_snprintf_with_pattern(UChar buf[], int bufsize, OnigEncoding enc,
379 UChar* pat, UChar* pat_end, const char *fmt, ...)
380{
381 va_list args;
382 va_start(args, fmt);
383 onig_vsnprintf_with_pattern(buf, bufsize, enc,
384 pat, pat_end, fmt, args);
385 va_end(args);
386}
387#endif
len
int len
Length of the buffer.
Definition io.h:8
stdarg.h
Defines old _.
OnigErrorInfo
Definition onigmo.h:739