| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2015-2023 by Hans-Peter Deifel * |
| 3 | * [email protected] * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 2 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * |
| 18 | * Boston, MA 02110-1301 USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #include "regengine.h" |
| 22 | |
| 23 | #include <regex.h> |
| 24 | #include <cstdint> |
| 25 | #include <cstdio> |
| 26 | #include <cstdlib> |
| 27 | #include <cstring> |
| 28 | #include <iostream> |
| 29 | #include <iomanip> |
| 30 | #include <string> |
| 31 | #include <sstream> |
| 32 | |
| 33 | #include "output.h" |
| 34 | #include "pdfgrep.h" |
| 35 | |
| 36 | using namespace std; |
| 37 | |
| 38 | bool PatternList::exec(const string &str, size_t offset, struct match &m) const |
| 39 | { |
| 40 | struct match m_copy = m; |
| 41 | |
| 42 | for (auto &r : patterns) { |
| 43 | if (r->exec(str, offset, m_copy)) { |
| 44 | m.start = m_copy.start; |
| 45 | m.end = m_copy.end; |
| 46 | return true; |
| 47 | } |
| 48 | } |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | void PatternList::add_pattern(unique_ptr<Regengine> pattern) { |
| 54 | patterns.push_back(move(pattern)); |
| 55 | } |
| 56 | |
| 57 | // regex(3) |
| 58 | |
| 59 | PosixRegex::PosixRegex(const string &pattern, bool case_insensitive) |
| 60 | { |
| 61 | int regex_flags = REG_EXTENDED | (case_insensitive ? REG_ICASE : 0); |
| 62 | |
| 63 | // The regcomp/regexec implementation of OpenBSD doesn't like empty |
| 64 | // patterns. Thus we just replace empty patterns by "()", which does |
| 65 | // have the same semantics. |
| 66 | const char *c_str_pattern; |
| 67 | if (pattern.empty()) { |
| 68 | c_str_pattern = "()"; |
| 69 | } else { |
| 70 | c_str_pattern = pattern.c_str(); |
| 71 | } |
| 72 | |
| 73 | int ret = regcomp(&this->regex, c_str_pattern, regex_flags); |
| 74 | if (ret != 0) { |
| 75 | char err_msg[256]; |
| 76 | regerror(ret, &this->regex, err_msg, 256); |
| 77 | err() << err_msg << endl; |
| 78 | exit(EXIT_ERROR); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | bool PosixRegex::exec(const string &str, size_t offset, struct match &m) const |
| 83 | { |
| 84 | regmatch_t match[] = {{0, 0}}; |
| 85 | const int nmatch = 1; |
| 86 | |
| 87 | // If we aren't at the beginning of the page, ^ should not match. |
| 88 | int flags = offset == 0 ? 0 : REG_NOTBOL; |
| 89 | |
| 90 | int ret = regexec(&this->regex, &str[offset], nmatch, match, flags); |
| 91 | |
| 92 | if (ret != 0) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | m.start = offset + match[0].rm_so; |
| 97 | m.end = offset + match[0].rm_eo; |
| 98 | |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | PosixRegex::~PosixRegex() |
| 103 | { |
| 104 | regfree(&this->regex); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | // pcre2(3) |
| 109 | |
| 110 | #ifdef HAVE_LIBPCRE |
| 111 | |
| 112 | PCRERegex::PCRERegex(const string &pattern, bool case_insensitive) |
| 113 | { |
| 114 | int pcre_err; |
| 115 | PCRE2_SIZE pcre_err_ofs; |
| 116 | const uint32_t pcre_options = PCRE2_UTF | (case_insensitive ? PCRE2_CASELESS : 0); |
| 117 | |
| 118 | this->regex = pcre2_compile(reinterpret_cast<PCRE2_SPTR>(pattern.data()), pattern.size(), |
| 119 | pcre_options, &pcre_err, &pcre_err_ofs, nullptr); |
| 120 | |
| 121 | if (this->regex == nullptr) { |
| 122 | PCRE2_UCHAR message[512]; // Actual size unknowable, longer messages get truncated |
| 123 | pcre2_get_error_message(pcre_err, message, sizeof message / sizeof *message); |
| 124 | err() << pattern << endl; |
| 125 | err() << setw(pcre_err_ofs+1) << "^" << endl; |
| 126 | err() << "Error compiling PCRE pattern: " << message << endl; |
| 127 | exit(EXIT_ERROR); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | PCRERegex::~PCRERegex() |
| 132 | { |
| 133 | pcre2_code_free(this->regex); |
| 134 | } |
| 135 | |
| 136 | bool PCRERegex::exec(const string &str, size_t offset, struct match &m) const |
| 137 | { |
| 138 | pcre2_match_data *data; |
| 139 | PCRE2_SIZE *ov; |
| 140 | |
| 141 | data = pcre2_match_data_create(1, nullptr); |
| 142 | const int ret = pcre2_match(this->regex, |
| 143 | reinterpret_cast<PCRE2_SPTR>(str.data()), str.size(), |
| 144 | offset, 0, data, nullptr); |
| 145 | |
| 146 | // TODO: Print human readable error |
| 147 | if (ret < 0 || pcre2_get_ovector_count(data) != 1) { |
| 148 | pcre2_match_data_free(data); |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | ov = pcre2_get_ovector_pointer(data); |
| 153 | m.start = ov[0]; |
| 154 | m.end = ov[1]; |
| 155 | |
| 156 | pcre2_match_data_free(data); |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | #endif // HAVE_LIBPCRE |
| 161 | |
| 162 | FixedString::FixedString(const string &pattern, bool case_insensitive) |
| 163 | : case_insensitive(case_insensitive) |
| 164 | { |
| 165 | istringstream str { pattern }; |
| 166 | string line; |
| 167 | |
| 168 | if (pattern.empty()) { |
| 169 | // special case for the empty pattern. In this case we _do_ want |
| 170 | // matches, but getline returns false leaving our patterns array |
| 171 | // empty. Thus we add the whole pattern explicitly. |
| 172 | patterns.push_back(pattern); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | // split pattern at newlines |
| 177 | while (getline(str, line)) { |
| 178 | patterns.push_back(line); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | bool FixedString::exec(const string &str, size_t offset, struct match &m) const |
| 183 | { |
| 184 | // We use C-style strings here, because of strcasestr |
| 185 | const char *str_begin = &str[offset]; |
| 186 | |
| 187 | // FIXME Searching for multiple patterns is very inefficient, because we |
| 188 | // search the same thing over and over, until it becomes the next match. |
| 189 | // We should introduce some kind of caching here |
| 190 | |
| 191 | const char *min_result = nullptr; |
| 192 | const string *min_pattern; |
| 193 | |
| 194 | for (const string &pattern : patterns) { |
| 195 | const char *result; |
| 196 | if (this->case_insensitive) { |
| 197 | result = strcasestr(str_begin, pattern.c_str()); |
| 198 | } else { |
| 199 | result = strstr(str_begin, pattern.c_str()); |
| 200 | } |
| 201 | |
| 202 | if (result != nullptr) { |
| 203 | if (min_result == nullptr || result < min_result) { |
| 204 | min_result = result; |
| 205 | min_pattern = &pattern; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (min_result != nullptr) { |
| 211 | m.start = offset + (min_result - str_begin); |
| 212 | m.end = m.start + (*min_pattern).size(); |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | return false; |
| 217 | } |