Commit c99a4eaf14bf382a2b7dea9ffc33e1798a891ff3
1 parent
c59c6a7d
Refactor `Streams::Copier`: replace `copied_streams` with `copied_providers`, st…
…reamline stream registration, and improve encapsulation.
Showing
1 changed file
with
12 additions
and
19 deletions
libqpdf/QPDF_Stream.cc
| ... | ... | @@ -40,7 +40,7 @@ class Streams::Copier final: public QPDFObjectHandle::StreamDataProvider |
| 40 | 40 | friend class Streams; |
| 41 | 41 | |
| 42 | 42 | public: |
| 43 | - Data(Stream& source, QPDFObjectHandle const& dest_dict) : | |
| 43 | + Data(Stream& source, Dictionary const& dest_dict) : | |
| 44 | 44 | encp(source.qpdf()->m->encp), |
| 45 | 45 | file(source.qpdf()->m->file), |
| 46 | 46 | source_og(source.id_gen()), |
| ... | ... | @@ -101,6 +101,7 @@ class Streams::Copier final: public QPDFObjectHandle::StreamDataProvider |
| 101 | 101 | } |
| 102 | 102 | } |
| 103 | 103 | auto stream = copied_streams.find(og); |
| 104 | + qpdf_invariant(stream == copied_streams.end() || stream->second); | |
| 104 | 105 | if (stream != copied_streams.end() && |
| 105 | 106 | stream->second.pipeStreamData( |
| 106 | 107 | pipeline, nullptr, 0, qpdf_dl_none, suppress_warnings, will_retry)) { |
| ... | ... | @@ -110,24 +111,20 @@ class Streams::Copier final: public QPDFObjectHandle::StreamDataProvider |
| 110 | 111 | } |
| 111 | 112 | |
| 112 | 113 | void |
| 113 | - register_copy(QPDFObjGen local_og, QPDFObjectHandle const& foreign_stream) | |
| 114 | + register_copy(Stream& dest, Stream& source, bool provider) | |
| 114 | 115 | { |
| 115 | - copied_streams.insert_or_assign(local_og, foreign_stream); | |
| 116 | - } | |
| 117 | - | |
| 118 | - void | |
| 119 | - register_copy( | |
| 120 | - QPDFObjGen local_og, | |
| 121 | - Stream& foreign, | |
| 122 | - qpdf_offset_t offset, | |
| 123 | - QPDFObjectHandle const& local_dict) | |
| 124 | - { | |
| 125 | - copied_data.insert_or_assign(local_og, Data(foreign, local_dict)); | |
| 116 | + qpdf_expect(source); | |
| 117 | + qpdf_expect(dest); | |
| 118 | + if (provider) { | |
| 119 | + copied_streams.insert_or_assign(dest, source); | |
| 120 | + } else { | |
| 121 | + copied_data.insert_or_assign(dest, Data(source, dest.getDict())); | |
| 122 | + } | |
| 126 | 123 | } |
| 127 | 124 | |
| 128 | 125 | private: |
| 129 | 126 | Streams& streams; |
| 130 | - std::map<QPDFObjGen, QPDFObjectHandle> copied_streams; | |
| 127 | + std::map<QPDFObjGen, Stream> copied_streams; | |
| 131 | 128 | std::map<QPDFObjGen, Data> copied_data; |
| 132 | 129 | }; |
| 133 | 130 | |
| ... | ... | @@ -327,12 +324,8 @@ Stream::copy_data_to(Stream& dest) |
| 327 | 324 | } |
| 328 | 325 | if (s->stream_data) { |
| 329 | 326 | dest.replaceStreamData(s->stream_data, dict["/Filter"], dict["/DecodeParms"]); |
| 330 | - } else if (s->stream_provider) { | |
| 331 | - // In this case, the source stream's QPDF must stay in scope. | |
| 332 | - d_streams.copier()->register_copy(dest, *this); | |
| 333 | - dest.replaceStreamData(d_streams.copier(), dict["/Filter"], dict["/DecodeParms"]); | |
| 334 | 327 | } else { |
| 335 | - d_streams.copier()->register_copy(dest, *this, offset(), dict); | |
| 328 | + d_streams.copier()->register_copy(dest, *this, s->stream_provider.get()); | |
| 336 | 329 | dest.replaceStreamData(d_streams.copier(), dict["/Filter"], dict["/DecodeParms"]); |
| 337 | 330 | } |
| 338 | 331 | } | ... | ... |