Rizin
unix-like reverse engineering framework and cli tools
regerror.c
Go to the documentation of this file.
1 /* $OpenBSD: regerror.c,v 1.13 2005/08/05 13:03:00 espie Exp $ */
2 /*-
3  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
4  * Copyright (c) 1992, 1993, 1994
5  * The Regents of the University of California. All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Henry Spencer.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  * may be used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * @(#)regerror.c 8.4 (Berkeley) 3/20/94
35  */
36 
37 #include <sys/types.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include <limits.h>
42 #include <stdlib.h>
43 #include "rz_regex.h"
44 
45 #include "utils.h"
46 
47 static char *regatoi(const RzRegex *, char *, int);
48 
49 static struct rerr {
50  int code;
51  char *name;
52  char *explain;
53 } rerrs[] = {
54  { RZ_REGEX_NOMATCH, "RZ_REGEX_NOMATCH", "regexec() failed to match" },
55  { RZ_REGEX_BADPAT, "RZ_REGEX_BADPAT", "invalid regular expression" },
56  { RZ_REGEX_ECOLLATE, "RZ_REGEX_ECOLLATE", "invalid collating element" },
57  { RZ_REGEX_ECTYPE, "RZ_REGEX_ECTYPE", "invalid character class" },
58  { RZ_REGEX_EESCAPE, "RZ_REGEX_EESCAPE", "trailing backslash (\\)" },
59  { RZ_REGEX_ESUBREG, "RZ_REGEX_ESUBREG", "invalid backreference number" },
60  { RZ_REGEX_EBRACK, "RZ_REGEX_EBRACK", "brackets ([ ]) not balanced" },
61  { RZ_REGEX_EPAREN, "RZ_REGEX_EPAREN", "parentheses not balanced" },
62  { RZ_REGEX_EBRACE, "RZ_REGEX_EBRACE", "braces not balanced" },
63  { RZ_REGEX_BADBR, "RZ_REGEX_BADBR", "invalid repetition count(s)" },
64  { RZ_REGEX_ERANGE, "RZ_REGEX_ERANGE", "invalid character range" },
65  { RZ_REGEX_ESPACE, "RZ_REGEX_ESPACE", "out of memory" },
66  { RZ_REGEX_BADRPT, "RZ_REGEX_BADRPT", "repetition-operator operand invalid" },
67  { RZ_REGEX_EMPTY, "RZ_REGEX_EMPTY", "empty (sub)expression" },
68  { RZ_REGEX_ASSERT, "RZ_REGEX_ASSERT", "\"can't happen\" -- you found a bug" },
69  { RZ_REGEX_INVARG, "RZ_REGEX_INVARG", "invalid argument to regex routine" },
70  { 0, "", "*** unknown regexp error code ***" }
71 };
72 
73 /*
74  - regerror - the interface to error numbers
75  = extern size_t regerror(int, const regex_t *, char *, size_t);
76  */
77 /* ARGSUSED */
78 RZ_API size_t rz_regex_error(int errcode, const RzRegex *preg, char *errbuf, size_t errbuf_size) {
79  struct rerr *r;
80  size_t len;
81  int target = errcode & ~RZ_REGEX_ITOA;
82  char *s;
83  char convbuf[50];
84 
85  if (errcode == RZ_REGEX_ATOI) {
86  s = regatoi(preg, convbuf, sizeof convbuf);
87  } else {
88  for (r = rerrs; r->code != 0; r++) {
89  if (r->code == target) {
90  break;
91  }
92  }
93 
94  if (errcode & RZ_REGEX_ITOA) {
95  if (r->code != 0) {
96  STRLCPY(convbuf, r->name, sizeof(convbuf) - 1);
97  } else {
98  snprintf(convbuf, sizeof convbuf, "RZ_REGEX_0x%x", target);
99  }
100  s = convbuf;
101  } else {
102  s = r->explain;
103  }
104  }
105 
106  len = strlen(s) + 1;
107  if (errbuf_size > 0) {
108  STRLCPY(errbuf, s, errbuf_size - 1);
109  }
110 
111  return len;
112 }
113 
114 /*
115  - regatoi - internal routine to implement RZ_REGEX_ATOI
116  */
117 static char *
118 regatoi(const RzRegex *preg, char *localbuf, int localbufsize) {
119  struct rerr *r;
120 
121  for (r = rerrs; r->code != 0; r++) {
122  if (strcmp(r->name, preg->re_endp) == 0) {
123  break;
124  }
125  }
126  if (r->code == 0) {
127  return ("0");
128  }
129 
130  (void)snprintf(localbuf, localbufsize, "%d", r->code);
131  return (localbuf);
132 }
size_t len
Definition: 6502dis.c:15
#define RZ_API
#define r
Definition: crypto_rc6.c:12
snprintf
Definition: kernel.h:364
#define STRLCPY(x, y, z)
Definition: utils.h:44
static struct rerr rerrs[]
RZ_API size_t rz_regex_error(int errcode, const RzRegex *preg, char *errbuf, size_t errbuf_size)
Definition: regerror.c:78
static char * regatoi(const RzRegex *, char *, int)
Definition: regerror.c:118
static RzSocket * s
Definition: rtr.c:28
#define RZ_REGEX_ITOA
Definition: rz_regex.h:51
#define RZ_REGEX_ESPACE
Definition: rz_regex.h:44
#define RZ_REGEX_BADPAT
Definition: rz_regex.h:34
#define RZ_REGEX_BADBR
Definition: rz_regex.h:42
#define RZ_REGEX_ECTYPE
Definition: rz_regex.h:36
#define RZ_REGEX_ATOI
Definition: rz_regex.h:50
#define RZ_REGEX_EBRACK
Definition: rz_regex.h:39
#define RZ_REGEX_ECOLLATE
Definition: rz_regex.h:35
#define RZ_REGEX_ERANGE
Definition: rz_regex.h:43
#define RZ_REGEX_ESUBREG
Definition: rz_regex.h:38
#define RZ_REGEX_EBRACE
Definition: rz_regex.h:41
#define RZ_REGEX_EMPTY
Definition: rz_regex.h:46
#define RZ_REGEX_BADRPT
Definition: rz_regex.h:45
#define RZ_REGEX_NOMATCH
Definition: rz_regex.h:33
#define RZ_REGEX_INVARG
Definition: rz_regex.h:48
#define RZ_REGEX_EPAREN
Definition: rz_regex.h:40
#define RZ_REGEX_EESCAPE
Definition: rz_regex.h:37
#define RZ_REGEX_ASSERT
Definition: rz_regex.h:47
Definition: regerror.c:49
int code
Definition: regerror.c:50
char * name
Definition: regerror.c:51
char * explain
Definition: regerror.c:52
const char * re_endp
Definition: rz_regex.h:11