From 63c7eefe9db8d8e87d07198355627af01cc1814d Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Tue, 17 May 2022 18:35:35 -0400 Subject: [PATCH] replaceStreamData: accept uninitialized filter/decode_parms These mean to leave the original values alone. This is needed for reconstructing streams from JSON given that the stream data and stream dictionary may appear in any order in the JSON. --- ChangeLog | 6 ++++++ include/qpdf/QPDFObjectHandle.hh | 22 +++++++++++++++------- libqpdf/QPDF_Stream.cc | 8 ++++++-- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index 41c4671b..95ed40c7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2022-05-17 Jay Berkenbilt + + * Allow passing *uninitialized* (not null) objects to + replaceStreamData as filter and/or decode_parms to leave any + existing values for /Filter and /DecodeParms untouched. + 2022-05-15 Jay Berkenbilt * Add QUtil::is_long_long to test whether a string can be diff --git a/include/qpdf/QPDFObjectHandle.hh b/include/qpdf/QPDFObjectHandle.hh index 2265459d..557330e5 100644 --- a/include/qpdf/QPDFObjectHandle.hh +++ b/include/qpdf/QPDFObjectHandle.hh @@ -1199,13 +1199,21 @@ class QPDFObjectHandle QPDF_DLL void replaceDict(QPDFObjectHandle const&); - // Replace this stream's stream data with the given data buffer, - // and replace the /Filter and /DecodeParms keys in the stream - // dictionary with the given values. (If either value is empty, - // the corresponding key is removed.) The stream's /Length key is - // replaced with the length of the data buffer. The stream is - // interpreted as if the data read from the file, after any - // decryption filters have been applied, is as presented. + // REPLACING STREAM DATA + + // Note about all replaceStreamData methods: whatever values are + // passed as filter and decode_parms will overwrite /Filter and + // /DecodeParms in the stream. Passing a null object + // (QPDFObjectHandle::newNull()) will remove those values from the + // stream dictionary. From qpdf 11, passing an *uninitialized* + // QPDFObjectHandle (QPDFObjectHandle()) will leave any existing + // values untouched. + + // Replace this stream's stream data with the given data buffer. + // The stream's /Length key is replaced with the length of the + // data buffer. The stream is interpreted as if the data read from + // the file, after any decryption filters have been applied, is as + // presented. QPDF_DLL void replaceStreamData( std::shared_ptr data, diff --git a/libqpdf/QPDF_Stream.cc b/libqpdf/QPDF_Stream.cc index c6f0d873..11dbe7e3 100644 --- a/libqpdf/QPDF_Stream.cc +++ b/libqpdf/QPDF_Stream.cc @@ -725,8 +725,12 @@ QPDF_Stream::replaceFilterData( QPDFObjectHandle const& decode_parms, size_t length) { - this->stream_dict.replaceKey("/Filter", filter); - this->stream_dict.replaceKey("/DecodeParms", decode_parms); + if (filter.isInitialized()) { + this->stream_dict.replaceKey("/Filter", filter); + } + if (decode_parms.isInitialized()) { + this->stream_dict.replaceKey("/DecodeParms", decode_parms); + } if (length == 0) { QTC::TC("qpdf", "QPDF_Stream unknown stream length"); this->stream_dict.removeKey("/Length");