From e2dedde4bdb5fa68c86d412e534a4b2750739988 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 7 Jul 2012 17:33:45 -0400 Subject: Don't require stream data provider to know length in advance Breaking API change: length parameter has disappeared from the StreamDataProvider version of QPDFObjectHandle::replaceStreamData since it is no longer necessary to compute it in advance. This breaking change is justified by the fact that removing the length parameter provides the caller an opportunity to simplify the calling code. --- libqpdf/QPDFObjectHandle.cc | 5 ++-- libqpdf/QPDF_Stream.cc | 62 ++++++++++++++++++++++++++++----------------- libqpdf/qpdf/QPDF_Stream.hh | 3 +-- 3 files changed, 42 insertions(+), 28 deletions(-) (limited to 'libqpdf') diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 4d6c5f79..c8b67416 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -414,12 +414,11 @@ QPDFObjectHandle::replaceStreamData(PointerHolder data, void QPDFObjectHandle::replaceStreamData(PointerHolder provider, QPDFObjectHandle const& filter, - QPDFObjectHandle const& decode_parms, - size_t length) + QPDFObjectHandle const& decode_parms) { assertType("Stream", isStream()); dynamic_cast(obj.getPointer())->replaceStreamData( - provider, filter, decode_parms, length); + provider, filter, decode_parms); } int diff --git a/libqpdf/QPDF_Stream.cc b/libqpdf/QPDF_Stream.cc index ed5fb5be..c089bcc1 100644 --- a/libqpdf/QPDF_Stream.cc +++ b/libqpdf/QPDF_Stream.cc @@ -380,24 +380,33 @@ QPDF_Stream::pipeStreamData(Pipeline* pipeline, bool filter, this->stream_provider->provideStreamData( this->objid, this->generation, &count); qpdf_offset_t actual_length = count.getCount(); - qpdf_offset_t desired_length = - this->stream_dict.getKey("/Length").getIntValue(); - if (actual_length == desired_length) - { - QTC::TC("qpdf", "QPDF_Stream pipe use stream provider"); - } - else - { - QTC::TC("qpdf", "QPDF_Stream provider length mismatch"); - throw std::logic_error( - "stream data provider for " + - QUtil::int_to_string(this->objid) + " " + - QUtil::int_to_string(this->generation) + - " provided " + - QUtil::int_to_string(actual_length) + - " bytes instead of expected " + - QUtil::int_to_string(desired_length) + " bytes"); - } + qpdf_offset_t desired_length = 0; + if (this->stream_dict.hasKey("/Length")) + { + desired_length = this->stream_dict.getKey("/Length").getIntValue(); + if (actual_length == desired_length) + { + QTC::TC("qpdf", "QPDF_Stream pipe use stream provider"); + } + else + { + QTC::TC("qpdf", "QPDF_Stream provider length mismatch"); + throw std::logic_error( + "stream data provider for " + + QUtil::int_to_string(this->objid) + " " + + QUtil::int_to_string(this->generation) + + " provided " + + QUtil::int_to_string(actual_length) + + " bytes instead of expected " + + QUtil::int_to_string(desired_length) + " bytes"); + } + } + else + { + QTC::TC("qpdf", "QPDF_Stream provider length not provided"); + this->stream_dict.replaceKey( + "/Length", QPDFObjectHandle::newInteger(actual_length)); + } } else if (this->offset == 0) { @@ -430,12 +439,11 @@ void QPDF_Stream::replaceStreamData( PointerHolder provider, QPDFObjectHandle const& filter, - QPDFObjectHandle const& decode_parms, - size_t length) + QPDFObjectHandle const& decode_parms) { this->stream_provider = provider; this->stream_data = 0; - replaceFilterData(filter, decode_parms, length); + replaceFilterData(filter, decode_parms, 0); } void @@ -445,6 +453,14 @@ QPDF_Stream::replaceFilterData(QPDFObjectHandle const& filter, { this->stream_dict.replaceOrRemoveKey("/Filter", filter); this->stream_dict.replaceOrRemoveKey("/DecodeParms", decode_parms); - this->stream_dict.replaceKey("/Length", - QPDFObjectHandle::newInteger((int)length)); + if (length == 0) + { + QTC::TC("qpdf", "QPDF_Stream unknown stream length"); + this->stream_dict.removeKey("/Length"); + } + else + { + this->stream_dict.replaceKey( + "/Length", QPDFObjectHandle::newInteger((int)length)); + } } diff --git a/libqpdf/qpdf/QPDF_Stream.hh b/libqpdf/qpdf/QPDF_Stream.hh index e74ae201..ce46d994 100644 --- a/libqpdf/qpdf/QPDF_Stream.hh +++ b/libqpdf/qpdf/QPDF_Stream.hh @@ -30,8 +30,7 @@ class QPDF_Stream: public QPDFObject void replaceStreamData( PointerHolder provider, QPDFObjectHandle const& filter, - QPDFObjectHandle const& decode_parms, - size_t length); + QPDFObjectHandle const& decode_parms); // Replace object ID and generation. This may only be called if // object ID and generation are 0. It is used by QPDFObjectHandle -- cgit v1.2.3-54-g00ecf