Commit a15ec6967dd3312223a6ab7d4198655234e1a4bf

Authored by Jay Berkenbilt
1 parent 1bb209a9

Enhancements to ParserCallbacks

ChangeLog
1 1 2021-03-01 Jay Berkenbilt <ejb@ql.org>
2 2  
  3 + * QPDFObjectHandle::ParserCallbacks: add virtual handleWarning
  4 + method, and provide default (empty) implementation of it and
  5 + handleEOF().
  6 +
3 7 * Add QPDF::numWarnings() -- useful to tell whether any warnings
4 8 were issued by a specific bit of code.
5 9  
... ...
include/qpdf/QPDFObjectHandle.hh
... ... @@ -224,7 +224,16 @@ class QPDFObjectHandle
224 224 virtual void handleObject(
225 225 QPDFObjectHandle, size_t offset, size_t length);
226 226  
227   - virtual void handleEOF() = 0;
  227 + // handleWarning is called if a warning was issued during the
  228 + // parsing of the most recent object. There's no good way to
  229 + // get information about the warning, but implementors can use
  230 + // this to become aware that there was some problem while
  231 + // parsing this content stream.
  232 + QPDF_DLL
  233 + virtual void handleWarning();
  234 +
  235 + QPDF_DLL
  236 + virtual void handleEOF();
228 237  
229 238 // Override this if you want to know the full size of the
230 239 // contents, possibly after concatenation of multiple streams.
... ...
libqpdf/QPDFObjectHandle.cc
... ... @@ -152,6 +152,16 @@ QPDFObjectHandle::ParserCallbacks::handleObject(
152 152 }
153 153  
154 154 void
  155 +QPDFObjectHandle::ParserCallbacks::handleWarning()
  156 +{
  157 +}
  158 +
  159 +void
  160 +QPDFObjectHandle::ParserCallbacks::handleEOF()
  161 +{
  162 +}
  163 +
  164 +void
155 165 QPDFObjectHandle::ParserCallbacks::contentSize(size_t)
156 166 {
157 167 // Ignore by default; overriding this is optional.
... ... @@ -1847,10 +1857,15 @@ QPDFObjectHandle::parseContentStream_internal(
1847 1857 pipeContentStreams(&buf, description, all_description);
1848 1858 PointerHolder<Buffer> stream_data = buf.getBuffer();
1849 1859 callbacks->contentSize(stream_data->getSize());
  1860 + QPDF* context = getOwningQPDF();
  1861 + if ((! context) && isArray() && (getArrayNItems() > 0))
  1862 + {
  1863 + context = getArrayItem(0).getOwningQPDF();
  1864 + }
1850 1865 try
1851 1866 {
1852 1867 parseContentStream_data(stream_data, all_description,
1853   - callbacks, getOwningQPDF());
  1868 + callbacks, context);
1854 1869 }
1855 1870 catch (TerminateParsing&)
1856 1871 {
... ... @@ -1881,9 +1896,15 @@ QPDFObjectHandle::parseContentStream_data(
1881 1896 tokenizer.readToken(input, "content", true);
1882 1897 qpdf_offset_t offset = input->getLastOffset();
1883 1898 input->seek(offset, SEEK_SET);
  1899 + size_t before_nwarnings = (context ? context->numWarnings() : 0);
1884 1900 QPDFObjectHandle obj =
1885 1901 parseInternal(input, "content", tokenizer,
1886 1902 empty, 0, context, true);
  1903 + size_t after_nwarnings = (context ? context->numWarnings() : 0);
  1904 + if (after_nwarnings > before_nwarnings)
  1905 + {
  1906 + callbacks->handleWarning();
  1907 + }
1887 1908 if (! obj.isInitialized())
1888 1909 {
1889 1910 // EOF
... ... @@ -1910,6 +1931,7 @@ QPDFObjectHandle::parseContentStream_data(
1910 1931 QPDFExc(qpdf_e_damaged_pdf, input->getName(),
1911 1932 "stream data", input->tell(),
1912 1933 "EOF found while reading inline image"));
  1934 + callbacks->handleWarning();
1913 1935 }
1914 1936 else
1915 1937 {
... ...