Commit 1527bf296a47237cd09329e5fe5a95372374d690

Authored by m-holger
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.
libqpdf/QPDF_Stream.cc
... ... @@ -519,24 +519,20 @@ Stream::pipeStreamData(
519 519 pipeline->write(s->stream_data->getBuffer(), s->stream_data->getSize());
520 520 pipeline->finish();
521 521 } else if (s->stream_provider.get()) {
522   - bool success = true;
523 522 Pl_Count count("stream provider count", pipeline);
524 523 if (s->stream_provider->supportsRetry()) {
525 524 if (!s->stream_provider->provideStreamData(
526 525 obj->getObjGen(), &count, suppress_warnings, will_retry)) {
527 526 filter = false;
528   - success = false;
  527 + return false;
529 528 }
530 529 } else {
531 530 s->stream_provider->provideStreamData(obj->getObjGen(), &count);
532 531 }
533 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 536 QTC::TC("qpdf", "QPDF_Stream provider length mismatch");
541 537 // This would be caused by programmer error on the part of a library user, not by
542 538 // invalid input data.
... ... @@ -545,17 +541,15 @@ Stream::pipeStreamData(
545 541 std::to_string(actual_length) + " bytes instead of expected " +
546 542 std::to_string(desired_length) + " bytes");
547 543 }
548   - } else if (success) {
  544 + } else {
549 545 QTC::TC("qpdf", "QPDF_Stream provider length not provided");
550 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 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 553 QTC::TC("qpdf", "QPDF_Stream pipe original stream data");
560 554 if (!QPDF::Pipe::pipeStreamData(
561 555 obj->getQPDF(),
... ...
qpdf/qpdf.testcov
... ... @@ -167,7 +167,6 @@ qpdf-c called qpdf_has_error 0
167 167 qpdf-c called qpdf_get_qpdf_version 0
168 168 QPDF_Stream pipe original stream data 0
169 169 QPDF_Stream pipe replaced stream data 0
170   -QPDF_Stream pipe use stream provider 0
171 170 QPDF_Stream provider length mismatch 0
172 171 QPDFObjectHandle newStream 0
173 172 QPDFObjectHandle newStream with data 0
... ...