From 9a96e233b051b31289c84f90a321583887b1400a Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 10 Aug 2017 20:24:16 -0400 Subject: Remove PCRE --- libqpdf/PCRE.cc | 354 --------------------------------------------------- libqpdf/build.mk | 1 - libqpdf/qpdf/PCRE.hh | 117 ----------------- 3 files changed, 472 deletions(-) delete mode 100644 libqpdf/PCRE.cc delete mode 100644 libqpdf/qpdf/PCRE.hh (limited to 'libqpdf') diff --git a/libqpdf/PCRE.cc b/libqpdf/PCRE.cc deleted file mode 100644 index 2e808aa7..00000000 --- a/libqpdf/PCRE.cc +++ /dev/null @@ -1,354 +0,0 @@ -#include -#include - -#include -#include -#include - -PCRE::NoBackref::NoBackref() : - std::logic_error("PCRE error: no match") -{ -} - -PCRE::Match::Match(int nbackrefs, char const* subject) -{ - this->init(-1, nbackrefs, subject); -} - -PCRE::Match::~Match() -{ - this->destroy(); -} - -PCRE::Match::Match(Match const& rhs) -{ - this->copy(rhs); -} - -PCRE::Match& -PCRE::Match::operator=(Match const& rhs) -{ - if (this != &rhs) - { - this->destroy(); - this->copy(rhs); - } - return *this; -} - -void -PCRE::Match::init(int nmatches, int nbackrefs, char const* subject) -{ - this->nmatches = nmatches; - this->nbackrefs = nbackrefs; - this->subject = subject; - this->ovecsize = 3 * (1 + nbackrefs); - this->ovector = 0; - if (this->ovecsize) - { - this->ovector = new int[this->ovecsize]; - } -} - -void -PCRE::Match::copy(Match const& rhs) -{ - this->init(rhs.nmatches, rhs.nbackrefs, rhs.subject); - int i; - for (i = 0; i < this->ovecsize; ++i) - { - this->ovector[i] = rhs.ovector[i]; - } -} - -void -PCRE::Match::destroy() -{ - delete [] this->ovector; -} - -PCRE::Match::operator bool() -{ - return (this->nmatches >= 0); -} - -std::string -PCRE::Match::getMatch(int n, int flags) -{ - // This method used to be implemented in terms of - // pcre_get_substring, but that function gives you an empty string - // for an unmatched backreference that is in range. - - int offset; - int length; - try - { - getOffsetLength(n, offset, length); - } - catch (NoBackref&) - { - if (flags & gm_no_substring_returns_empty) - { - return ""; - } - else - { - throw; - } - } - - return std::string(this->subject).substr(offset, length); -} - -void -PCRE::Match::getOffsetLength(int n, int& offset, int& length) -{ - if ((this->nmatches < 0) || - (n > this->nmatches - 1) || - (this->ovector[n * 2] == -1)) - { - throw NoBackref(); - } - offset = this->ovector[n * 2]; - length = this->ovector[n * 2 + 1] - offset; -} - -int -PCRE::Match::getOffset(int n) -{ - int offset; - int length; - this->getOffsetLength(n, offset, length); - return offset; -} - -int -PCRE::Match::getLength(int n) -{ - int offset; - int length; - this->getOffsetLength(n, offset, length); - return length; -} - -int -PCRE::Match::nMatches() const -{ - return this->nmatches; -} - -PCRE::PCRE(char const* pattern, int options) -{ - char const *errptr; - int erroffset; - this->code = pcre_compile(pattern, options, &errptr, &erroffset, 0); - if (this->code) - { - pcre_fullinfo(this->code, 0, PCRE_INFO_CAPTURECOUNT, &(this->nbackrefs)); - } - else - { - std::string message = (std::string("compilation of ") + pattern + - " failed at offset " + - QUtil::int_to_string(erroffset) + ": " + - errptr); - throw std::runtime_error("PCRE error: " + message); - } -} - -PCRE::~PCRE() -{ - pcre_free(this->code); -} - -PCRE::Match -PCRE::match(char const* subject, int options, int startoffset, int size) -{ - if (size == -1) - { - size = strlen(subject); - } - - Match result(this->nbackrefs, subject); - int status = pcre_exec(this->code, 0, subject, size, - startoffset, options, - result.ovector, result.ovecsize); - if (status >= 0) - { - result.nmatches = status; - } - else - { - std::string message; - - switch (status) - { - case PCRE_ERROR_NOMATCH: - break; - - case PCRE_ERROR_BADOPTION: - message = "bad option passed to PCRE::match()"; - throw std::logic_error(message); - break; - - case PCRE_ERROR_NOMEMORY: - message = "insufficient memory"; - throw std::runtime_error(message); - break; - - case PCRE_ERROR_NULL: - case PCRE_ERROR_BADMAGIC: - case PCRE_ERROR_UNKNOWN_NODE: - default: - message = "pcre_exec returned " + QUtil::int_to_string(status); - throw std::logic_error(message); - } - } - - return result; -} - -void -PCRE::test(int n) -{ - try - { - if (n == 1) - { - static char const* utf8 = "abπdefq"; - PCRE u1("^([[:alpha:]]+)"); - PCRE u2("^([\\p{L}]+)", PCRE_UTF8); - PCRE::Match m1 = u1.match(utf8); - if (m1) - { - std::cout << "no utf8: " << m1.getMatch(1) << std::endl; - } - PCRE::Match m2 = u2.match(utf8); - if (m2) - { - std::cout << "utf8: " << m2.getMatch(1) << std::endl; - } - return; - } - - try - { - PCRE pcre1("a**"); - } - catch (std::exception& e) - { - std::cout << e.what() << std::endl; - } - - PCRE pcre2("^([^\\s:]*)\\s*:\\s*(.*?)\\s*$"); - PCRE::Match m2 = pcre2.match("key: value one two three "); - if (m2) - { - std::cout << m2.nMatches() << std::endl; - std::cout << m2.getMatch(0) << std::endl; - std::cout << m2.getOffset(0) << std::endl; - std::cout << m2.getLength(0) << std::endl; - std::cout << m2.getMatch(1) << std::endl; - std::cout << m2.getOffset(1) << std::endl; - std::cout << m2.getLength(1) << std::endl; - std::cout << m2.getMatch(2) << std::endl; - std::cout << m2.getOffset(2) << std::endl; - std::cout << m2.getLength(2) << std::endl; - try - { - std::cout << m2.getMatch(3) << std::endl; - } - catch (std::exception& e) - { - std::cout << e.what() << std::endl; - } - try - { - std::cout << m2.getOffset(3) << std::endl; - } - catch (std::exception& e) - { - std::cout << e.what() << std::endl; - } - } - PCRE pcre3("^(a+)(b+)?$"); - PCRE::Match m3 = pcre3.match("aaa"); - try - { - if (m3) - { - std::cout << m3.nMatches() << std::endl; - std::cout << m3.getMatch(0) << std::endl; - std::cout << m3.getMatch(1) << std::endl; - std::cout << "-" - << m3.getMatch( - 2, Match::gm_no_substring_returns_empty) - << "-" << std::endl; - std::cout << "hello" << std::endl; - std::cout << m3.getMatch(2) << std::endl; - std::cout << "can't see this" << std::endl; - } - } - catch (std::exception& e) - { - std::cout << e.what() << std::endl; - } - - // backref: 1 2 3 4 5 - PCRE pcre4("^((?:(a(b)?)(?:,(c))?)|(c))?$"); - static char const* candidates[] = { - "qqqcqqq", // no match - "ab,c", // backrefs: 0, 1, 2, 3, 4 - "ab", // backrefs: 0, 1, 2, 3 - "a", // backrefs: 0, 1, 2 - "a,c", // backrefs: 0, 1, 2, 4 - "c", // backrefs: 0, 1, 5 - "", // backrefs: 0 - 0 - }; - for (char const** p = candidates; *p; ++p) - { - PCRE::Match m(pcre4.match(*p)); - if (m) - { - int nmatches = m.nMatches(); - for (int i = 0; i < nmatches; ++i) - { - std::cout << *p << ": " << i << ": "; - try - { - std::string match = m.getMatch(i); - std::cout << match; - } - catch (NoBackref&) - { - std::cout << "no backref (getMatch)"; - } - std::cout << std::endl; - - std::cout << *p << ": " << i << ": "; - try - { - int offset; - int length; - m.getOffsetLength(i, offset, length); - std::cout << offset << ", " << length; - } - catch (NoBackref&) - { - std::cout << "no backref (getOffsetLength)"; - } - std:: cout << std::endl; - } - } - else - { - std::cout << *p << ": no match" << std::endl; - } - } - } - catch (std::exception& e) - { - std::cout << "unexpected exception: " << e.what() << std::endl; - } -} diff --git a/libqpdf/build.mk b/libqpdf/build.mk index 4963027e..001d36c8 100644 --- a/libqpdf/build.mk +++ b/libqpdf/build.mk @@ -14,7 +14,6 @@ SRCS_libqpdf = \ libqpdf/InsecureRandomDataProvider.cc \ libqpdf/MD5.cc \ libqpdf/OffsetInputSource.cc \ - libqpdf/PCRE.cc \ libqpdf/Pipeline.cc \ libqpdf/Pl_AES_PDF.cc \ libqpdf/Pl_ASCII85Decoder.cc \ diff --git a/libqpdf/qpdf/PCRE.hh b/libqpdf/qpdf/PCRE.hh deleted file mode 100644 index 8f200655..00000000 --- a/libqpdf/qpdf/PCRE.hh +++ /dev/null @@ -1,117 +0,0 @@ -// This is a C++ wrapper class around Philip Hazel's perl-compatible -// regular expressions library. -// - -#ifndef __PCRE_HH__ -#define __PCRE_HH__ - -#include - -#ifdef _WIN32 -# define PCRE_STATIC -#endif -#include -#include -#include - -// Note: this class does not encapsulate all features of the PCRE -// package -- only those that I actually need right now are here. - -class PCRE -{ - public: - // This is thrown when an attempt is made to access a non-existent - // back reference. - class NoBackref: public std::logic_error - { - public: - QPDF_DLL - NoBackref(); - virtual ~NoBackref() throw() {} - }; - - class Match - { - friend class PCRE; - public: - QPDF_DLL - Match(int nbackrefs, char const* subject); - QPDF_DLL - Match(Match const&); - QPDF_DLL - Match& operator=(Match const&); - QPDF_DLL - ~Match(); - QPDF_DLL - operator bool(); - - // All the back reference accessing routines may throw the - // special exception NoBackref (derived from Exception) if the - // back reference does not exist. Exception will be thrown - // for other error conditions. This allows callers to trap - // this condition explicitly when they care about the - // difference between a backreference matching an empty string - // and not matching at all. - - // see getMatch flags below - QPDF_DLL - std::string getMatch(int n, int flags = 0); - QPDF_DLL - void getOffsetLength(int n, int& offset, int& length); - QPDF_DLL - int getOffset(int n); - QPDF_DLL - int getLength(int n); - - // nMatches returns the number of available matches including - // match 0 which is the whole string. In other words, if you - // have one backreference in your expression and the - // expression matches, nMatches() will return 2, getMatch(0) - // will return the whole string, getMatch(1) will return the - // text that matched the backreference, and getMatch(2) will - // throw an exception because it is out of range. - QPDF_DLL - int nMatches() const; - - // Flags for getMatch - - // getMatch on a substring that didn't match should return - // empty string instead of throwing an exception - static int const gm_no_substring_returns_empty = (1 << 0); - - private: - void init(int nmatches, int nbackrefs, char const* subject); - void copy(Match const&); - void destroy(); - - int nbackrefs; - char const* subject; - int* ovector; - int ovecsize; - int nmatches; - }; - - // The value passed in as options is passed to pcre_exec. See man - // pcreapi for details. - QPDF_DLL - PCRE(char const* pattern, int options = 0); - QPDF_DLL - ~PCRE(); - - QPDF_DLL - Match match(char const* subject, int options = 0, int startoffset = 0, - int size = -1); - - QPDF_DLL - static void test(int n = 0); - - private: - // prohibit copying and assignment - PCRE(PCRE const&); - PCRE& operator=(PCRE const&); - - pcre* code; - int nbackrefs; -}; - -#endif // __PCRE_HH__ -- cgit v1.2.3-54-g00ecf