Commit 1527bf296a47237cd09329e5fe5a95372374d690
1 parent
98925989
Refactor stream logic for clarity and reliability
Stream provider logic has been streamlined by removing unnecessary variables and conditional checks. Adjusted handling of mismatched or missing stream lengths to improve code readability and maintainability.
Showing
2 changed files
with
9 additions
and
16 deletions
libqpdf/QPDF_Stream.cc
| @@ -519,24 +519,20 @@ Stream::pipeStreamData( | @@ -519,24 +519,20 @@ Stream::pipeStreamData( | ||
| 519 | pipeline->write(s->stream_data->getBuffer(), s->stream_data->getSize()); | 519 | pipeline->write(s->stream_data->getBuffer(), s->stream_data->getSize()); |
| 520 | pipeline->finish(); | 520 | pipeline->finish(); |
| 521 | } else if (s->stream_provider.get()) { | 521 | } else if (s->stream_provider.get()) { |
| 522 | - bool success = true; | ||
| 523 | Pl_Count count("stream provider count", pipeline); | 522 | Pl_Count count("stream provider count", pipeline); |
| 524 | if (s->stream_provider->supportsRetry()) { | 523 | if (s->stream_provider->supportsRetry()) { |
| 525 | if (!s->stream_provider->provideStreamData( | 524 | if (!s->stream_provider->provideStreamData( |
| 526 | obj->getObjGen(), &count, suppress_warnings, will_retry)) { | 525 | obj->getObjGen(), &count, suppress_warnings, will_retry)) { |
| 527 | filter = false; | 526 | filter = false; |
| 528 | - success = false; | 527 | + return false; |
| 529 | } | 528 | } |
| 530 | } else { | 529 | } else { |
| 531 | s->stream_provider->provideStreamData(obj->getObjGen(), &count); | 530 | s->stream_provider->provideStreamData(obj->getObjGen(), &count); |
| 532 | } | 531 | } |
| 533 | qpdf_offset_t actual_length = count.getCount(); | 532 | qpdf_offset_t actual_length = count.getCount(); |
| 534 | - qpdf_offset_t desired_length = 0; | ||
| 535 | - if (success && s->stream_dict.hasKey("/Length")) { | ||
| 536 | - desired_length = s->stream_dict.getKey("/Length").getIntValue(); | ||
| 537 | - if (actual_length == desired_length) { | ||
| 538 | - QTC::TC("qpdf", "QPDF_Stream pipe use stream provider"); | ||
| 539 | - } else { | 533 | + if (s->stream_dict.hasKey("/Length")) { |
| 534 | + auto desired_length = s->stream_dict.getKey("/Length").getIntValue(); | ||
| 535 | + if (actual_length != desired_length) { | ||
| 540 | QTC::TC("qpdf", "QPDF_Stream provider length mismatch"); | 536 | QTC::TC("qpdf", "QPDF_Stream provider length mismatch"); |
| 541 | // This would be caused by programmer error on the part of a library user, not by | 537 | // This would be caused by programmer error on the part of a library user, not by |
| 542 | // invalid input data. | 538 | // invalid input data. |
| @@ -545,17 +541,15 @@ Stream::pipeStreamData( | @@ -545,17 +541,15 @@ Stream::pipeStreamData( | ||
| 545 | std::to_string(actual_length) + " bytes instead of expected " + | 541 | std::to_string(actual_length) + " bytes instead of expected " + |
| 546 | std::to_string(desired_length) + " bytes"); | 542 | std::to_string(desired_length) + " bytes"); |
| 547 | } | 543 | } |
| 548 | - } else if (success) { | 544 | + } else { |
| 549 | QTC::TC("qpdf", "QPDF_Stream provider length not provided"); | 545 | QTC::TC("qpdf", "QPDF_Stream provider length not provided"); |
| 550 | s->stream_dict.replaceKey("/Length", QPDFObjectHandle::newInteger(actual_length)); | 546 | s->stream_dict.replaceKey("/Length", QPDFObjectHandle::newInteger(actual_length)); |
| 551 | } | 547 | } |
| 552 | - if (!success) { | ||
| 553 | - return false; | ||
| 554 | - } | ||
| 555 | - } else if (obj->getParsedOffset() == 0) { | ||
| 556 | - QTC::TC("qpdf", "QPDF_Stream pipe no stream data"); | ||
| 557 | - throw std::logic_error("pipeStreamData called for stream with no data"); | ||
| 558 | } else { | 548 | } else { |
| 549 | + if (obj->getParsedOffset() == 0) { | ||
| 550 | + QTC::TC("qpdf", "QPDF_Stream pipe no stream data"); | ||
| 551 | + throw std::logic_error("pipeStreamData called for stream with no data"); | ||
| 552 | + } | ||
| 559 | QTC::TC("qpdf", "QPDF_Stream pipe original stream data"); | 553 | QTC::TC("qpdf", "QPDF_Stream pipe original stream data"); |
| 560 | if (!QPDF::Pipe::pipeStreamData( | 554 | if (!QPDF::Pipe::pipeStreamData( |
| 561 | obj->getQPDF(), | 555 | obj->getQPDF(), |
qpdf/qpdf.testcov
| @@ -167,7 +167,6 @@ qpdf-c called qpdf_has_error 0 | @@ -167,7 +167,6 @@ qpdf-c called qpdf_has_error 0 | ||
| 167 | qpdf-c called qpdf_get_qpdf_version 0 | 167 | qpdf-c called qpdf_get_qpdf_version 0 |
| 168 | QPDF_Stream pipe original stream data 0 | 168 | QPDF_Stream pipe original stream data 0 |
| 169 | QPDF_Stream pipe replaced stream data 0 | 169 | QPDF_Stream pipe replaced stream data 0 |
| 170 | -QPDF_Stream pipe use stream provider 0 | ||
| 171 | QPDF_Stream provider length mismatch 0 | 170 | QPDF_Stream provider length mismatch 0 |
| 172 | QPDFObjectHandle newStream 0 | 171 | QPDFObjectHandle newStream 0 |
| 173 | QPDFObjectHandle newStream with data 0 | 172 | QPDFObjectHandle newStream with data 0 |