From 5b8b1de2dbe272751e8e1c2d40714f857788a846 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Fri, 26 Feb 2021 19:21:27 +0100 Subject: [PATCH] Bugfix: Invalid parsing of postscript function --- Pdf4QtLib/sources/pdffunction.cpp | 1 + Pdf4QtLib/sources/pdfparser.cpp | 13 ++++++++++++- Pdf4QtLib/sources/pdfparser.h | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Pdf4QtLib/sources/pdffunction.cpp b/Pdf4QtLib/sources/pdffunction.cpp index 341083b..7b6b34d 100644 --- a/Pdf4QtLib/sources/pdffunction.cpp +++ b/Pdf4QtLib/sources/pdffunction.cpp @@ -1663,6 +1663,7 @@ PDFPostScriptFunction::Program PDFPostScriptFunction::parseProgram(const QByteAr Program result; PDFLexicalAnalyzer parser(adjustedArray.constBegin(), adjustedArray.constEnd()); + parser.setTokenizingPostScriptFunction(); std::stack blockCallStack; while (true) diff --git a/Pdf4QtLib/sources/pdfparser.cpp b/Pdf4QtLib/sources/pdfparser.cpp index f30abdd..7442cfa 100644 --- a/Pdf4QtLib/sources/pdfparser.cpp +++ b/Pdf4QtLib/sources/pdfparser.cpp @@ -32,7 +32,8 @@ namespace pdf PDFLexicalAnalyzer::PDFLexicalAnalyzer(const char* begin, const char* end) : m_begin(begin), m_current(begin), - m_end(end) + m_end(end), + m_tokenizingPostScriptFunction(false) { } @@ -454,6 +455,16 @@ PDFLexicalAnalyzer::Token PDFLexicalAnalyzer::fetch() return Token(TokenType::Command, std::move(command)); } } + else if (m_tokenizingPostScriptFunction) + { + const char currentChar = lookChar(); + if (currentChar == CHAR_LEFT_CURLY_BRACKET || currentChar == CHAR_RIGHT_CURLY_BRACKET) + { + return Token(TokenType::Command, QByteArray(1, fetchChar())); + } + + error(tr("Unexpected character '%1' in the stream.").arg(currentChar)); + } else { error(tr("Unexpected character '%1' in the stream.").arg(lookChar())); diff --git a/Pdf4QtLib/sources/pdfparser.h b/Pdf4QtLib/sources/pdfparser.h index f890b8c..d1c4d3e 100644 --- a/Pdf4QtLib/sources/pdfparser.h +++ b/Pdf4QtLib/sources/pdfparser.h @@ -153,6 +153,9 @@ public: /// \param position Position, from which the search is started PDFInteger findSubstring(const char* str, PDFInteger position) const; + /// Switch parser mode for tokenizing PostScript function + void setTokenizingPostScriptFunction() { m_tokenizingPostScriptFunction = true; } + /// Returns true, if character is a whitespace character according to the PDF 1.7 specification /// \param character Character to be tested static constexpr bool isWhitespace(char character); @@ -198,6 +201,7 @@ private: const char* m_begin; const char* m_current; const char* m_end; + bool m_tokenizingPostScriptFunction; }; /// Parsing context. Used for example to detect cyclic reference errors.