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