| 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_Hcxx . . $ `<built-in>:409:9`! |
| 31 | #include <config.h>cxx . . $ `<file>/build/config.h`/ |
| 32 | #endif |
| 33 | |
| 34 | #include <stdlib.h> |
| 35 | #include "oval_glob_to_regex.h"cxx . . $ `<file>/src/OVAL/oval_glob_to_regex.h`/ |
| 36 | #include "common/oscap_string.h"cxx . . $ `<file>/src/common/oscap_string.h`/ |
| 37 | #include "common/debug_priv.h"cxx . . $ `<file>/src/common/debug_priv.h`/ |
| 38 | |
| 39 | typedef enumcxx . . $ $anonymous_type_47b83975384773d8_0# { |
| 40 | STARTcxx . . $ START., |
| 41 | NORMALcxx . . $ NORMAL., |
| 42 | LEFT_BRACKETcxx . . $ LEFT_BRACKET., |
| 43 | CLASScxx . . $ CLASS., |
| 44 | ESCAPEcxx . . $ ESCAPE., |
| 45 | SLASHcxx . . $ SLASH. |
| 46 | } statescxx . . $ states#; |
| 47 | |
| 48 | char *oval_glob_to_regexcxx . . $ oval_glob_to_regex(2050848944e00d54). (const char *glob, int noescape) |
| 49 | { |
| 50 | struct oscap_stringcxx . . $ oscap_string# *regex; |
| 51 | char * result; |
| 52 | char c; |
| 53 | int i = 0; |
| 54 | int state = STARTcxx . . $ START.; |
| 55 | regex = oscap_string_newcxx . . $ oscap_string_new(a2a7aea7d1de9315).(); |
| 56 | if (regex == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 57 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 58 | } |
| 59 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '^'); // regex must match whole string |
| 60 | while(1) { |
| 61 | c = glob[i++]; |
| 62 | switch (state) { |
| 63 | case STARTcxx . . $ START.: |
| 64 | if (c == '\0') { |
| 65 | goto finish; |
| 66 | } else if (c== '/') { |
| 67 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, c); |
| 68 | state = SLASHcxx . . $ 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_stringcxx . . $ oscap_string_append_string(bdf4962f194308bb).(regex, "[^./]"); |
| 74 | state = NORMALcxx . . $ 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_stringcxx . . $ oscap_string_append_string(bdf4962f194308bb).(regex, "(?=[^.])[^/]*"); |
| 80 | state = NORMALcxx . . $ NORMAL.; |
| 81 | } else if (c == '.' || c == '|' || c == '^' || c == '(' || c == ')' |
| 82 | || c == '{' || c == '}' || c == '+' || c == '$') { |
| 83 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '\\'); |
| 84 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, c); |
| 85 | state = NORMALcxx . . $ NORMAL.; |
| 86 | } else if (c == '[') { |
| 87 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex,'['); |
| 88 | state = LEFT_BRACKETcxx . . $ LEFT_BRACKET.; |
| 89 | } else if (c == '\\') { |
| 90 | if (noescape) { |
| 91 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '\\'); |
| 92 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '\\'); |
| 93 | state = NORMALcxx . . $ NORMAL.; |
| 94 | } else { |
| 95 | state = ESCAPEcxx . . $ ESCAPE.; |
| 96 | } |
| 97 | } else { |
| 98 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, c); |
| 99 | state = NORMALcxx . . $ NORMAL.; |
| 100 | } |
| 101 | break; |
| 102 | case NORMALcxx . . $ NORMAL.: |
| 103 | if (c == '\0') { |
| 104 | goto finish; |
| 105 | } else if (c== '/') { |
| 106 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, c); |
| 107 | state = SLASHcxx . . $ SLASH.; |
| 108 | } else if (c == '?') { |
| 109 | // a ? matches any single character, but |
| 110 | // it can never match a / |
| 111 | oscap_string_append_stringcxx . . $ oscap_string_append_string(bdf4962f194308bb).(regex, "[^/]"); |
| 112 | } else if (c == '*') { |
| 113 | // a * matches any string, but |
| 114 | // it can never match a / |
| 115 | oscap_string_append_stringcxx . . $ oscap_string_append_string(bdf4962f194308bb).(regex, "[^/]*"); |
| 116 | } else if (c == '.' || c == '|' || c == '^' || c == '(' || c == ')' |
| 117 | || c == '{' || c == '}' || c == '+' || c == '$' ) { |
| 118 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '\\'); |
| 119 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, c); |
| 120 | } else if (c == '[') { |
| 121 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex,'['); |
| 122 | state = LEFT_BRACKETcxx . . $ LEFT_BRACKET.; |
| 123 | } else if (c == '\\') { |
| 124 | if (noescape) { |
| 125 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '\\'); |
| 126 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '\\'); |
| 127 | } else { |
| 128 | state = ESCAPEcxx . . $ ESCAPE.; |
| 129 | } |
| 130 | } else { |
| 131 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, c); |
| 132 | } |
| 133 | break; |
| 134 | case LEFT_BRACKETcxx . . $ LEFT_BRACKET.: |
| 135 | if (c == '!') { |
| 136 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '^'); |
| 137 | } else if (c == '\0') { |
| 138 | dEcxx . . $ `src/common/debug_priv.h:63:9`!("Can't convert glob '%s' to regular expression. Expecting ']' at the end of glob.", glob); |
| 139 | oscap_string_freecxx . . $ oscap_string_free(bf7769a094bde656).(regex); |
| 140 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 141 | } else { |
| 142 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, c); |
| 143 | } |
| 144 | state = CLASScxx . . $ CLASS.; |
| 145 | break; |
| 146 | case CLASScxx . . $ CLASS.: |
| 147 | if (c == '\\') { |
| 148 | if (noescape) { |
| 149 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '\\'); |
| 150 | } |
| 151 | } else if (c == ']') { |
| 152 | state = NORMALcxx . . $ NORMAL.; |
| 153 | } else if (c == '\0') { |
| 154 | dEcxx . . $ `src/common/debug_priv.h:63:9`!("Can't convert glob '%s' to regular expression. Expecting ']' at the end of glob.", glob); |
| 155 | oscap_string_freecxx . . $ oscap_string_free(bf7769a094bde656).(regex); |
| 156 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 157 | } |
| 158 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, c); |
| 159 | break; |
| 160 | case ESCAPEcxx . . $ 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_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '\\'); |
| 168 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, c); |
| 169 | } else if (c == '\0') { |
| 170 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '\\'); |
| 171 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '\\'); |
| 172 | goto finish; |
| 173 | } else { |
| 174 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '\\'); |
| 175 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '\\'); |
| 176 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, c); |
| 177 | } |
| 178 | state = NORMALcxx . . $ NORMAL.; |
| 179 | break; |
| 180 | case SLASHcxx . . $ 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_stringcxx . . $ oscap_string_append_string(bdf4962f194308bb).(regex, "[^./]"); |
| 188 | state = NORMALcxx . . $ 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_stringcxx . . $ oscap_string_append_string(bdf4962f194308bb).(regex, "(?=[^.])[^/]*"); |
| 194 | state = NORMALcxx . . $ NORMAL.; |
| 195 | } else if (c == '.' || c == '|' || c == '^' || c == '(' || c == ')' |
| 196 | || c == '{' || c == '}' || c == '+' || c == '$' ) { |
| 197 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '\\'); |
| 198 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, c); |
| 199 | state = NORMALcxx . . $ NORMAL.; |
| 200 | } else if (c == '[') { |
| 201 | state = LEFT_BRACKETcxx . . $ LEFT_BRACKET.; |
| 202 | } else if (c == '\\') { |
| 203 | if (noescape) { |
| 204 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '\\'); |
| 205 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '\\'); |
| 206 | state = NORMALcxx . . $ NORMAL.; |
| 207 | } else { |
| 208 | state = ESCAPEcxx . . $ ESCAPE.; |
| 209 | } |
| 210 | } else { |
| 211 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, c); |
| 212 | state = NORMALcxx . . $ NORMAL.; |
| 213 | } |
| 214 | break; |
| 215 | default: |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | finish: |
| 220 | oscap_string_append_charcxx . . $ oscap_string_append_char(10528718cb7e122a).(regex, '$'); // regex must match only whole string |
| 221 | result = oscap_strdupcxx . . $ oscap_strdup(a8b8e32347182212).(oscap_string_get_cstrcxx . . $ oscap_string_get_cstr(4dc31ca86595c117).(regex)); |
| 222 | oscap_string_freecxx . . $ oscap_string_free(bf7769a094bde656).(regex); |
| 223 | return result; |
| 224 | } |