Ruby  2.5.0dev(2017-10-22revision60238)
regerror.c
Go to the documentation of this file.
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 
36 extern UChar*
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;
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;
57  p = "undefined bytecode (bug)"; break;
59  p = "unexpected bytecode (bug)"; break;
61  p = "match-stack limit over"; break;
63  p = "parse depth limit over"; break;
65  p = "default multibyte-encoding is not set"; break;
67  p = "can't convert to wide-char on specified multibyte-encoding"; break;
69  p = "invalid argument"; break;
71  p = "end pattern at left brace"; break;
73  p = "end pattern at left bracket"; break;
75  p = "empty char-class"; break;
77  p = "premature end of char-class"; break;
79  p = "end pattern at escape"; break;
81  p = "end pattern at meta"; break;
83  p = "end pattern at control"; break;
85  p = "invalid meta-code syntax"; break;
87  p = "invalid control-code syntax"; break;
89  p = "char-class value at end of range"; break;
91  p = "char-class value at start of range"; break;
93  p = "unmatched range specifier in char-class"; break;
95  p = "target of repeat operator is not specified"; break;
97  p = "target of repeat operator is invalid"; break;
99  p = "nested repeat operator"; break;
101  p = "unmatched close parenthesis"; break;
103  p = "end pattern with unmatched parenthesis"; break;
105  p = "end pattern in group"; break;
107  p = "undefined group option"; break;
109  p = "invalid POSIX bracket type"; break;
111  p = "invalid pattern in look-behind"; break;
113  p = "invalid repeat range {lower,upper}"; break;
115  p = "invalid conditional pattern"; break;
117  p = "too big number"; break;
119  p = "too big number for repeat range"; break;
121  p = "upper is smaller than lower in repeat range"; break;
123  p = "empty range in char class"; break;
125  p = "mismatch multibyte code length in char-class range"; break;
127  p = "too many multibyte code ranges are specified"; break;
129  p = "too short multibyte code string"; break;
131  p = "too big backref number"; break;
133 #ifdef USE_NAMED_GROUP
134  p = "invalid backref number/name"; break;
135 #else
136  p = "invalid backref number"; break;
137 #endif
139  p = "numbered backref/call is not allowed. (use name)"; break;
141  p = "too short digits"; break;
143  p = "too long wide-char value"; break;
145  p = "group name is empty"; break;
147  p = "invalid group name <%n>"; break;
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
155  p = "undefined name <%n> reference"; break;
157  p = "undefined group <%n> reference"; break;
159  p = "multiplex defined name <%n>"; break;
161  p = "multiplex definition name <%n> call"; break;
163  p = "never ending recursion"; break;
165  p = "group number is too big for capture history"; break;
167  p = "invalid character property name {%n}"; break;
169  p = "too many capture groups are specified"; break;
171  p = "invalid code point value"; break;
173  p = "too big wide-char value"; break;
175  p = "not supported encoding combination"; break;
177  p = "invalid combination of options"; break;
178 
179  default:
180  p = "undefined error code"; break;
181  }
182 
183  return (UChar* )p;
184 }
185 
186 static void sprint_byte(char* s, unsigned int v)
187 {
188  xsnprintf(s, 3, "%02x", (v & 0377));
189 }
190 
191 static void sprint_byte_with_x(char* s, unsigned int v)
192 {
193  xsnprintf(s, 5, "\\x%02x", (v & 0377));
194 }
195 
196 static 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 /* for ONIG_MAX_ERROR_MESSAGE_LEN */
246 #define MAX_ERROR_PAR_LEN 30
247 
248 extern int
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) {
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) {
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 
313 void
315  UChar* pat, UChar* pat_end, const UChar *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));
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++));
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  sprint_byte_with_x((char* )bs, (unsigned int )(*p++));
362  bp = bs;
363  while (len-- > 0) *s++ = *bp++;
364  }
365  else {
366  *s++ = *p++;
367  }
368  }
369 
370  *s++ = '/';
371  *s = '\0';
372  }
373 }
374 
375 #if 0 /* unused */
376 void
378  UChar* pat, UChar* pat_end, const UChar *fmt, ...)
379 {
380  va_list args;
381  va_start(args, fmt);
382  onig_vsnprintf_with_pattern(buf, bufsize, enc,
383  pat, pat_end, fmt, args);
384  va_end(args);
385 }
386 #endif
#define ONIGERR_MATCH_STACK_LIMIT_OVER
Definition: onigmo.h:635
#define xsnprintf
Definition: regint.h:215
unsigned int OnigCodePoint
Definition: onigmo.h:80
UChar * onig_error_code_to_format(OnigPosition code)
Definition: regerror.c:37
#define ONIG_MISMATCH
Definition: onigmo.h:625
#define ONIGERR_TOO_MANY_CAPTURE_GROUPS
Definition: onigmo.h:676
#define ONIGENC_IS_MBC_HEAD(enc, p, e)
Definition: onigmo.h:319
#define ONIGERR_NOT_SUPPORTED_ENCODING_COMBINATION
Definition: onigmo.h:692
#define ONIGERR_INVALID_COMBINATION_OF_OPTIONS
Definition: onigmo.h:693
#define ONIGERR_META_CODE_SYNTAX
Definition: onigmo.h:649
#define ONIGERR_INVALID_LOOK_BEHIND_PATTERN
Definition: onigmo.h:662
#define ONIGERR_INVALID_BACKREF
Definition: onigmo.h:674
#define ONIGERR_END_PATTERN_AT_LEFT_BRACKET
Definition: onigmo.h:643
OnigUChar * par_end
Definition: onigmo.h:737
void onig_snprintf_with_pattern(UChar buf[], int bufsize, OnigEncoding enc, UChar *pat, UChar *pat_end, const UChar *fmt,...)
#define ONIGERR_UNDEFINED_GROUP_OPTION
Definition: onigmo.h:660
#define ONIGERR_INVALID_CODE_POINT_VALUE
Definition: onigmo.h:689
#define ONIG_ENCODING_ASCII
Definition: onigmo.h:225
#define ONIGERR_NUMBERED_BACKREF_OR_CALL_NOT_ALLOWED
Definition: onigmo.h:675
#define ONIGERR_TOO_BIG_NUMBER
Definition: onigmo.h:666
#define ONIGERR_INVALID_GROUP_NAME
Definition: onigmo.h:680
ptrdiff_t OnigPosition
Definition: onigmo.h:83
#define ONIGERR_MULTIPLEX_DEFINED_NAME
Definition: onigmo.h:684
#define ONIGERR_END_PATTERN_AT_ESCAPE
Definition: onigmo.h:646
#define ONIGERR_CONTROL_CODE_SYNTAX
Definition: onigmo.h:650
#define ONIGERR_MULTIPLEX_DEFINITION_NAME_CALL
Definition: onigmo.h:685
#define ONIGERR_MISMATCH_CODE_LENGTH_IN_CLASS_RANGE
Definition: onigmo.h:670
#define ONIGERR_END_PATTERN_AT_META
Definition: onigmo.h:647
#define ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS
Definition: onigmo.h:669
#define ONIGERR_NESTED_REPEAT_OPERATOR
Definition: onigmo.h:656
int onig_error_code_to_str(UChar *s, OnigPosition code,...)
Definition: regerror.c:249
#define ONIGERR_INVALID_CHAR_IN_GROUP_NAME
Definition: onigmo.h:681
#define ONIGERR_INVALID_ARGUMENT
Definition: onigmo.h:640
#define xvsnprintf
Definition: regint.h:214
#define ONIGERR_END_PATTERN_AT_CONTROL
Definition: onigmo.h:648
#define ONIGENC_IS_CODE_SPACE(enc, code)
Definition: onigmo.h:392
#define ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE
Definition: onigmo.h:651
#define ONIGERR_END_PATTERN_IN_GROUP
Definition: onigmo.h:659
#define ONIGERR_TYPE_BUG
Definition: onigmo.h:630
#define ONIGERR_UNDEFINED_NAME_REFERENCE
Definition: onigmo.h:682
#define xmemcpy
Definition: regint.h:202
#define ONIGERR_UNDEFINED_BYTECODE
Definition: onigmo.h:633
#define ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS
Definition: onigmo.h:658
#define ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE
Definition: onigmo.h:668
#define ONIGENC_MBC_MINLEN(enc)
Definition: onigmo.h:364
#define ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED
Definition: onigmo.h:654
#define UChar
Definition: onigmo.h:76
#define ONIGERR_UNDEFINED_GROUP_REFERENCE
Definition: onigmo.h:683
#define ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS
Definition: onigmo.h:653
#define ONIGENC_MBC_TO_CODE(enc, p, end)
Definition: onigmo.h:366
#define MAX_ERROR_PAR_LEN
Definition: regerror.c:246
void onig_vsnprintf_with_pattern(UChar buf[], int bufsize, OnigEncoding enc, UChar *pat, UChar *pat_end, const UChar *fmt, va_list args)
Definition: regerror.c:314
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4309
#define ONIGERR_TOO_SHORT_MULTI_BYTE_STRING
Definition: onigmo.h:672
#define ONIGERR_SPECIFIED_ENCODING_CANT_CONVERT_TO_WIDE_CHAR
Definition: onigmo.h:638
OnigEncoding enc
Definition: onigmo.h:735
#define ONIGERR_CHAR_CLASS_VALUE_AT_START_OF_RANGE
Definition: onigmo.h:652
#define ONIGERR_INVALID_CHAR_PROPERTY_NAME
Definition: onigmo.h:688
#define ONIGERR_EMPTY_GROUP_NAME
Definition: onigmo.h:679
#define ONIGERR_STACK_BUG
Definition: onigmo.h:632
#define MIN(a, b)
Definition: regint.h:295
register unsigned int len
Definition: zonetab.h:51
#define ONIGERR_END_PATTERN_AT_LEFT_BRACE
Definition: onigmo.h:642
#define ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID
Definition: onigmo.h:655
#define ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY
Definition: onigmo.h:687
#define ONIGERR_PREMATURE_END_OF_CHAR_CLASS
Definition: onigmo.h:645
#define ONIGERR_PARSE_DEPTH_LIMIT_OVER
Definition: onigmo.h:636
ONIG_EXTERN int onigenc_str_bytelen_null(OnigEncoding enc, const OnigUChar *p)
#define ONIGERR_TOO_LONG_WIDE_CHAR_VALUE
Definition: onigmo.h:678
#define ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
Definition: onigmo.h:691
#define ONIGERR_INVALID_REPEAT_RANGE_PATTERN
Definition: onigmo.h:663
#define ONIGERR_NEVER_ENDING_RECURSION
Definition: onigmo.h:686
#define ONIGERR_UNEXPECTED_BYTECODE
Definition: onigmo.h:634
#define ONIGERR_INVALID_POSIX_BRACKET_TYPE
Definition: onigmo.h:661
#define ONIGERR_TOO_BIG_BACKREF_NUMBER
Definition: onigmo.h:673
#define ONIGERR_MEMORY
Definition: onigmo.h:629
#define ONIGENC_IS_CODE_PRINT(enc, code)
Definition: onigmo.h:378
#define ONIGERR_DEFAULT_ENCODING_IS_NOT_SET
Definition: onigmo.h:637
#define ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE
Definition: onigmo.h:667
#define ONIGERR_UNMATCHED_CLOSE_PARENTHESIS
Definition: onigmo.h:657
#define ONIGERR_EMPTY_CHAR_CLASS
Definition: onigmo.h:644
#define ONIGERR_TOO_SHORT_DIGITS
Definition: onigmo.h:677
#define bp()
Definition: vm_debug.h:25
#define ONIG_NO_SUPPORT_CONFIG
Definition: onigmo.h:626
#define ONIGERR_PARSER_BUG
Definition: onigmo.h:631
#define ONIGERR_INVALID_CONDITION_PATTERN
Definition: onigmo.h:664
#define ONIGERR_TOO_MANY_MULTI_BYTE_RANGES
Definition: onigmo.h:671
OnigUChar * par
Definition: onigmo.h:736
#define enclen(enc, p, e)
Definition: regenc.h:93