Enhancements to ParserCallbacks

This commit is contained in:
Jay Berkenbilt 2021-03-01 17:34:47 -05:00
parent 1bb209a9bf
commit a15ec6967d
3 changed files with 37 additions and 2 deletions

View File

@ -1,5 +1,9 @@
2021-03-01 Jay Berkenbilt <ejb@ql.org>
* QPDFObjectHandle::ParserCallbacks: add virtual handleWarning
method, and provide default (empty) implementation of it and
handleEOF().
* Add QPDF::numWarnings() -- useful to tell whether any warnings
were issued by a specific bit of code.

View File

@ -224,7 +224,16 @@ class QPDFObjectHandle
virtual void handleObject(
QPDFObjectHandle, size_t offset, size_t length);
virtual void handleEOF() = 0;
// handleWarning is called if a warning was issued during the
// parsing of the most recent object. There's no good way to
// get information about the warning, but implementors can use
// this to become aware that there was some problem while
// parsing this content stream.
QPDF_DLL
virtual void handleWarning();
QPDF_DLL
virtual void handleEOF();
// Override this if you want to know the full size of the
// contents, possibly after concatenation of multiple streams.

View File

@ -151,6 +151,16 @@ QPDFObjectHandle::ParserCallbacks::handleObject(
handleObject(oh);
}
void
QPDFObjectHandle::ParserCallbacks::handleWarning()
{
}
void
QPDFObjectHandle::ParserCallbacks::handleEOF()
{
}
void
QPDFObjectHandle::ParserCallbacks::contentSize(size_t)
{
@ -1847,10 +1857,15 @@ QPDFObjectHandle::parseContentStream_internal(
pipeContentStreams(&buf, description, all_description);
PointerHolder<Buffer> stream_data = buf.getBuffer();
callbacks->contentSize(stream_data->getSize());
QPDF* context = getOwningQPDF();
if ((! context) && isArray() && (getArrayNItems() > 0))
{
context = getArrayItem(0).getOwningQPDF();
}
try
{
parseContentStream_data(stream_data, all_description,
callbacks, getOwningQPDF());
callbacks, context);
}
catch (TerminateParsing&)
{
@ -1881,9 +1896,15 @@ QPDFObjectHandle::parseContentStream_data(
tokenizer.readToken(input, "content", true);
qpdf_offset_t offset = input->getLastOffset();
input->seek(offset, SEEK_SET);
size_t before_nwarnings = (context ? context->numWarnings() : 0);
QPDFObjectHandle obj =
parseInternal(input, "content", tokenizer,
empty, 0, context, true);
size_t after_nwarnings = (context ? context->numWarnings() : 0);
if (after_nwarnings > before_nwarnings)
{
callbacks->handleWarning();
}
if (! obj.isInitialized())
{
// EOF
@ -1910,6 +1931,7 @@ QPDFObjectHandle::parseContentStream_data(
QPDFExc(qpdf_e_damaged_pdf, input->getName(),
"stream data", input->tell(),
"EOF found while reading inline image"));
callbacks->handleWarning();
}
else
{