Debian Codegraph @

src/OVAL/oval_glob_to_regex.c

raw symbols SCIP
1 /**
2 * @file oval_glob_to_regex.c
3 * \brief Open Vulnerability and Assessment Language
4 *
5 * See more details at http://oval.mitre.org/
6 */
7
8 /*
9 * Copyright 2015 Red Hat Inc., Durham, North Carolina.
10 * All Rights Reserved.
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 *
26 * Authors:
27 * "Jan Černý" <[email protected]>
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #include <stdlib.h>
35 #include "oval_glob_to_regex.h"
36 #include "common/oscap_string.h"
37 #include "common/debug_priv.h"
38
39 typedef enum {
40 START,
41 NORMAL,
42 LEFT_BRACKET,
43 CLASS,
44 ESCAPE,
45 SLASH
46 } states;
47
48 char *oval_glob_to_regex (const char *glob, int noescape)
49 {
50 struct oscap_string *regex;
51 char * result;
52 char c;
53 int i = 0;
54 int state = START;
55 regex = oscap_string_new();
56 if (regex == NULL) {
57 return NULL;
58 }
59 oscap_string_append_char(regex, '^'); // regex must match whole string
60 while(1) {
61 c = glob[i++];
62 switch (state) {
63 case START:
64 if (c == '\0') {
65 goto finish;
66 } else if (c== '/') {
67 oscap_string_append_char(regex, c);
68 state = SLASH;
69 } else if (c == '?') {
70 // a ? matches any single character, but
71 // a ? at the begining of glob pattern can't match a .
72 // a ? never matches a / (see man 7 glob - Pathnames)
73 oscap_string_append_string(regex, "[^./]");
74 state = NORMAL;
75 } else if (c == '*') {
76 // a * matches any string, but
77 // a * at the begining of glob pattern can't match a .
78 // a * never matches a / (see man 7 glob - Pathnames)
79 oscap_string_append_string(regex, "(?=[^.])[^/]*");
80 state = NORMAL;
81 } else if (c == '.' || c == '|' || c == '^' || c == '(' || c == ')'
82 || c == '{' || c == '}' || c == '+' || c == '$') {
83 oscap_string_append_char(regex, '\\');
84 oscap_string_append_char(regex, c);
85 state = NORMAL;
86 } else if (c == '[') {
87 oscap_string_append_char(regex,'[');
88 state = LEFT_BRACKET;
89 } else if (c == '\\') {
90 if (noescape) {
91 oscap_string_append_char(regex, '\\');
92 oscap_string_append_char(regex, '\\');
93 state = NORMAL;
94 } else {
95 state = ESCAPE;
96 }
97 } else {
98 oscap_string_append_char(regex, c);
99 state = NORMAL;
100 }
101 break;
102 case NORMAL:
103 if (c == '\0') {
104 goto finish;
105 } else if (c== '/') {
106 oscap_string_append_char(regex, c);
107 state = SLASH;
108 } else if (c == '?') {
109 // a ? matches any single character, but
110 // it can never match a /
111 oscap_string_append_string(regex, "[^/]");
112 } else if (c == '*') {
113 // a * matches any string, but
114 // it can never match a /
115 oscap_string_append_string(regex, "[^/]*");
116 } else if (c == '.' || c == '|' || c == '^' || c == '(' || c == ')'
117 || c == '{' || c == '}' || c == '+' || c == '$' ) {
118 oscap_string_append_char(regex, '\\');
119 oscap_string_append_char(regex, c);
120 } else if (c == '[') {
121 oscap_string_append_char(regex,'[');
122 state = LEFT_BRACKET;
123 } else if (c == '\\') {
124 if (noescape) {
125 oscap_string_append_char(regex, '\\');
126 oscap_string_append_char(regex, '\\');
127 } else {
128 state = ESCAPE;
129 }
130 } else {
131 oscap_string_append_char(regex, c);
132 }
133 break;
134 case LEFT_BRACKET:
135 if (c == '!') {
136 oscap_string_append_char(regex, '^');
137 } else if (c == '\0') {
138 dE("Can't convert glob '%s' to regular expression. Expecting ']' at the end of glob.", glob);
139 oscap_string_free(regex);
140 return NULL;
141 } else {
142 oscap_string_append_char(regex, c);
143 }
144 state = CLASS;
145 break;
146 case CLASS:
147 if (c == '\\') {
148 if (noescape) {
149 oscap_string_append_char(regex, '\\');
150 }
151 } else if (c == ']') {
152 state = NORMAL;
153 } else if (c == '\0') {
154 dE("Can't convert glob '%s' to regular expression. Expecting ']' at the end of glob.", glob);
155 oscap_string_free(regex);
156 return NULL;
157 }
158 oscap_string_append_char(regex, c);
159 break;
160 case ESCAPE:
161 // ?, *, [ and ] are special characters, they must be escaped in glob.
162 // A backslash is treated as an escape character only for these characters.
163 // For all other characters the backslash is just an ordinary character.
164 // Other characters, that are special in perl's regex,
165 // are not special in a glob.
166 if (c == '?' || c == '*' || c == '[' || c == ']') {
167 oscap_string_append_char(regex, '\\');
168 oscap_string_append_char(regex, c);
169 } else if (c == '\0') {
170 oscap_string_append_char(regex, '\\');
171 oscap_string_append_char(regex, '\\');
172 goto finish;
173 } else {
174 oscap_string_append_char(regex, '\\');
175 oscap_string_append_char(regex, '\\');
176 oscap_string_append_char(regex, c);
177 }
178 state = NORMAL;
179 break;
180 case SLASH:
181 if (c == '\0') {
182 goto finish;
183 } else if (c == '?') {
184 // a ? matches any single character, but
185 // a ? at the begining of glob pattern can't match a .
186 // a ? never matches a / (see man 7 glob - Pathnames)
187 oscap_string_append_string(regex, "[^./]");
188 state = NORMAL;
189 } else if (c == '*') {
190 // a * matches any string, but
191 // a * at the begining of glob pattern can't match a .
192 // a * never matches a / (see man 7 glob - Pathnames)
193 oscap_string_append_string(regex, "(?=[^.])[^/]*");
194 state = NORMAL;
195 } else if (c == '.' || c == '|' || c == '^' || c == '(' || c == ')'
196 || c == '{' || c == '}' || c == '+' || c == '$' ) {
197 oscap_string_append_char(regex, '\\');
198 oscap_string_append_char(regex, c);
199 state = NORMAL;
200 } else if (c == '[') {
201 state = LEFT_BRACKET;
202 } else if (c == '\\') {
203 if (noescape) {
204 oscap_string_append_char(regex, '\\');
205 oscap_string_append_char(regex, '\\');
206 state = NORMAL;
207 } else {
208 state = ESCAPE;
209 }
210 } else {
211 oscap_string_append_char(regex, c);
212 state = NORMAL;
213 }
214 break;
215 default:
216 break;
217 }
218 }
219 finish:
220 oscap_string_append_char(regex, '$'); // regex must match only whole string
221 result = oscap_strdup(oscap_string_get_cstr(regex));
222 oscap_string_free(regex);
223 return result;
224 }