Commit 171027b1c73506008bd56fc286ff289d2975b94a

Authored by m-holger
1 parent 9ad84bae

Refactor `copyStreamData`: rename parameters for clarity, streamline stream data…

… handling, and enhance encapsulation.
Showing 1 changed file with 18 additions and 22 deletions
libqpdf/QPDF_Stream.cc
@@ -197,44 +197,40 @@ Stream::copy() const @@ -197,44 +197,40 @@ Stream::copy() const
197 } 197 }
198 198
199 void 199 void
200 -QPDF::copyStreamData(QPDFObjectHandle result, QPDFObjectHandle foreign_oh) 200 +QPDF::copyStreamData(QPDFObjectHandle dest, QPDFObjectHandle source_oh)
201 { 201 {
202 - // This method was originally written for copying foreign streams, but it is used by  
203 - // Stream::copy to copy streams from the same QPDF object as well.  
204 -  
205 - Dictionary dict = result.getDict();  
206 - Dictionary old_dict = foreign_oh.getDict();  
207 - QPDFObjGen local_og(result.getObjGen()); 202 + Dictionary dict = dest.getDict();
  203 + Dictionary old_dict = source_oh.getDict();
  204 + QPDFObjGen local_og(dest.getObjGen());
208 // Copy information from the foreign stream so we can pipe its data later without keeping the 205 // Copy information from the foreign stream so we can pipe its data later without keeping the
209 // original QPDF object around. 206 // original QPDF object around.
210 207
211 - QPDF& foreign_stream_qpdf =  
212 - foreign_oh.getQPDF("unable to retrieve owning qpdf from foreign stream"); 208 + QPDF& source_qpdf = source_oh.getQPDF("unable to retrieve owning qpdf from foreign stream");
213 209
214 - Stream foreign = foreign_oh;  
215 - if (!foreign) { 210 + Stream source = source_oh;
  211 + if (!source) {
216 throw std::logic_error("unable to retrieve underlying stream object from foreign stream"); 212 throw std::logic_error("unable to retrieve underlying stream object from foreign stream");
217 } 213 }
218 - std::shared_ptr<Buffer> stream_buffer = foreign.getStreamDataBuffer();  
219 - if (foreign_stream_qpdf.m->immediate_copy_from && !stream_buffer) { 214 + std::shared_ptr<Buffer> stream_buffer = source.getStreamDataBuffer();
  215 + if (source_qpdf.m->immediate_copy_from && !stream_buffer) {
220 // Pull the stream data into a buffer before attempting the copy operation. Do it on the 216 // Pull the stream data into a buffer before attempting the copy operation. Do it on the
221 // source stream so that if the source stream is copied multiple times, we don't have to 217 // source stream so that if the source stream is copied multiple times, we don't have to
222 // keep duplicating the memory. 218 // keep duplicating the memory.
223 - foreign.replaceStreamData(  
224 - foreign.getRawStreamData(), old_dict["/Filter"], old_dict["/DecodeParms"]);  
225 - stream_buffer = foreign.getStreamDataBuffer(); 219 + source.replaceStreamData(
  220 + source.getRawStreamData(), old_dict["/Filter"], old_dict["/DecodeParms"]);
  221 + stream_buffer = source.getStreamDataBuffer();
226 } 222 }
227 - auto stream_provider = foreign.getStreamDataProvider(); 223 + auto stream_provider = source.getStreamDataProvider();
228 if (stream_buffer) { 224 if (stream_buffer) {
229 - result.replaceStreamData(stream_buffer, dict["/Filter"], dict["/DecodeParms"]); 225 + dest.replaceStreamData(stream_buffer, dict["/Filter"], dict["/DecodeParms"]);
230 } else if (stream_provider) { 226 } else if (stream_provider) {
231 // In this case, the remote stream's QPDF must stay in scope. 227 // In this case, the remote stream's QPDF must stay in scope.
232 - m->objects.streams().copier()->register_copy(local_og, foreign_oh);  
233 - result.replaceStreamData( 228 + m->objects.streams().copier()->register_copy(local_og, source_oh);
  229 + dest.replaceStreamData(
234 m->objects.streams().copier(), dict["/Filter"], dict["/DecodeParms"]); 230 m->objects.streams().copier(), dict["/Filter"], dict["/DecodeParms"]);
235 } else { 231 } else {
236 - m->objects.streams().copier()->register_copy(local_og, foreign, foreign_oh.offset(), dict);  
237 - result.replaceStreamData( 232 + m->objects.streams().copier()->register_copy(local_og, source, source.offset(), dict);
  233 + dest.replaceStreamData(
238 m->objects.streams().copier(), dict["/Filter"], dict["/DecodeParms"]); 234 m->objects.streams().copier(), dict["/Filter"], dict["/DecodeParms"]);
239 } 235 }
240 } 236 }