Commit 63c7eefe9db8d8e87d07198355627af01cc1814d

Authored by Jay Berkenbilt
1 parent 56f1b411

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
  1 +2022-05-17 Jay Berkenbilt <ejb@ql.org>
  2 +
  3 + * Allow passing *uninitialized* (not null) objects to
  4 + replaceStreamData as filter and/or decode_parms to leave any
  5 + existing values for /Filter and /DecodeParms untouched.
  6 +
1 2022-05-15 Jay Berkenbilt <ejb@ql.org> 7 2022-05-15 Jay Berkenbilt <ejb@ql.org>
2 8
3 * Add QUtil::is_long_long to test whether a string can be 9 * Add QUtil::is_long_long to test whether a string can be
include/qpdf/QPDFObjectHandle.hh
@@ -1199,13 +1199,21 @@ class QPDFObjectHandle @@ -1199,13 +1199,21 @@ class QPDFObjectHandle
1199 QPDF_DLL 1199 QPDF_DLL
1200 void replaceDict(QPDFObjectHandle const&); 1200 void replaceDict(QPDFObjectHandle const&);
1201 1201
1202 - // Replace this stream's stream data with the given data buffer,  
1203 - // and replace the /Filter and /DecodeParms keys in the stream  
1204 - // dictionary with the given values. (If either value is empty,  
1205 - // the corresponding key is removed.) The stream's /Length key is  
1206 - // replaced with the length of the data buffer. The stream is  
1207 - // interpreted as if the data read from the file, after any  
1208 - // decryption filters have been applied, is as presented. 1202 + // REPLACING STREAM DATA
  1203 +
  1204 + // Note about all replaceStreamData methods: whatever values are
  1205 + // passed as filter and decode_parms will overwrite /Filter and
  1206 + // /DecodeParms in the stream. Passing a null object
  1207 + // (QPDFObjectHandle::newNull()) will remove those values from the
  1208 + // stream dictionary. From qpdf 11, passing an *uninitialized*
  1209 + // QPDFObjectHandle (QPDFObjectHandle()) will leave any existing
  1210 + // values untouched.
  1211 +
  1212 + // Replace this stream's stream data with the given data buffer.
  1213 + // The stream's /Length key is replaced with the length of the
  1214 + // data buffer. The stream is interpreted as if the data read from
  1215 + // the file, after any decryption filters have been applied, is as
  1216 + // presented.
1209 QPDF_DLL 1217 QPDF_DLL
1210 void replaceStreamData( 1218 void replaceStreamData(
1211 std::shared_ptr<Buffer> data, 1219 std::shared_ptr<Buffer> data,
libqpdf/QPDF_Stream.cc
@@ -725,8 +725,12 @@ QPDF_Stream::replaceFilterData( @@ -725,8 +725,12 @@ QPDF_Stream::replaceFilterData(
725 QPDFObjectHandle const& decode_parms, 725 QPDFObjectHandle const& decode_parms,
726 size_t length) 726 size_t length)
727 { 727 {
728 - this->stream_dict.replaceKey("/Filter", filter);  
729 - this->stream_dict.replaceKey("/DecodeParms", decode_parms); 728 + if (filter.isInitialized()) {
  729 + this->stream_dict.replaceKey("/Filter", filter);
  730 + }
  731 + if (decode_parms.isInitialized()) {
  732 + this->stream_dict.replaceKey("/DecodeParms", decode_parms);
  733 + }
730 if (length == 0) { 734 if (length == 0) {
731 QTC::TC("qpdf", "QPDF_Stream unknown stream length"); 735 QTC::TC("qpdf", "QPDF_Stream unknown stream length");
732 this->stream_dict.removeKey("/Length"); 736 this->stream_dict.removeKey("/Length");