Commit 989259892774095f387678ddcf7b5f540ef1f427

Authored by m-holger
1 parent 1a05b099

Refactor stream handling for clarity and efficiency

Replaced shared_ptr with unique_ptr for pipeline management, reducing overhead and improving ownership semantics. Simplified conditional checks and modernized code to enhance readability by removing redundant variables and adjusting pointer usages. This refactor ensures better maintainability and minor performance gains in stream processing.
Showing 1 changed file with 25 additions and 25 deletions
libqpdf/QPDF_Stream.cc
@@ -447,18 +447,17 @@ Stream::pipeStreamData( @@ -447,18 +447,17 @@ Stream::pipeStreamData(
447 bool specialized_compression = false; 447 bool specialized_compression = false;
448 bool lossy_compression = false; 448 bool lossy_compression = false;
449 bool ignored; 449 bool ignored;
450 - if (filterp == nullptr) { 450 + if (!filterp) {
451 filterp = &ignored; 451 filterp = &ignored;
452 } 452 }
453 bool& filter = *filterp; 453 bool& filter = *filterp;
454 - filter = (!((encode_flags == 0) && (decode_level == qpdf_dl_none)));  
455 - bool success = true; 454 + filter = encode_flags || decode_level != qpdf_dl_none;
456 if (filter) { 455 if (filter) {
457 filter = filterable(filters, specialized_compression, lossy_compression); 456 filter = filterable(filters, specialized_compression, lossy_compression);
458 if ((decode_level < qpdf_dl_all) && lossy_compression) { 457 if ((decode_level < qpdf_dl_all) && lossy_compression) {
459 filter = false; 458 filter = false;
460 } 459 }
461 - if ((decode_level < qpdf_dl_specialized) && specialized_compression) { 460 + if (decode_level < qpdf_dl_specialized && specialized_compression) {
462 filter = false; 461 filter = false;
463 } 462 }
464 QTC::TC( 463 QTC::TC(
@@ -470,7 +469,7 @@ Stream::pipeStreamData( @@ -470,7 +469,7 @@ Stream::pipeStreamData(
470 : 3); 469 : 3);
471 } 470 }
472 471
473 - if (pipeline == nullptr) { 472 + if (!pipeline) {
474 QTC::TC("qpdf", "QPDF_Stream pipeStreamData with null pipeline"); 473 QTC::TC("qpdf", "QPDF_Stream pipeStreamData with null pipeline");
475 // Return value is whether we can filter in this case. 474 // Return value is whether we can filter in this case.
476 return filter; 475 return filter;
@@ -479,40 +478,37 @@ Stream::pipeStreamData( @@ -479,40 +478,37 @@ Stream::pipeStreamData(
479 // Construct the pipeline in reverse order. Force pipelines we create to be deleted when this 478 // Construct the pipeline in reverse order. Force pipelines we create to be deleted when this
480 // function finishes. Pipelines created by QPDFStreamFilter objects will be deleted by those 479 // function finishes. Pipelines created by QPDFStreamFilter objects will be deleted by those
481 // objects. 480 // objects.
482 - std::vector<std::shared_ptr<Pipeline>> to_delete; 481 + std::vector<std::unique_ptr<Pipeline>> to_delete;
483 482
484 - std::shared_ptr<ContentNormalizer> normalizer;  
485 - std::shared_ptr<Pipeline> new_pipeline; 483 + ContentNormalizer normalizer;
486 if (filter) { 484 if (filter) {
487 if (encode_flags & qpdf_ef_compress) { 485 if (encode_flags & qpdf_ef_compress) {
488 - new_pipeline =  
489 - std::make_shared<Pl_Flate>("compress stream", pipeline, Pl_Flate::a_deflate);  
490 - to_delete.push_back(new_pipeline); 486 + auto new_pipeline =
  487 + std::make_unique<Pl_Flate>("compress stream", pipeline, Pl_Flate::a_deflate);
491 pipeline = new_pipeline.get(); 488 pipeline = new_pipeline.get();
  489 + to_delete.push_back(std::move(new_pipeline));
492 } 490 }
493 491
494 if (encode_flags & qpdf_ef_normalize) { 492 if (encode_flags & qpdf_ef_normalize) {
495 - normalizer = std::make_shared<ContentNormalizer>();  
496 - new_pipeline =  
497 - std::make_shared<Pl_QPDFTokenizer>("normalizer", normalizer.get(), pipeline);  
498 - to_delete.push_back(new_pipeline); 493 + auto new_pipeline =
  494 + std::make_unique<Pl_QPDFTokenizer>("normalizer", &normalizer, pipeline);
499 pipeline = new_pipeline.get(); 495 pipeline = new_pipeline.get();
  496 + to_delete.push_back(std::move(new_pipeline));
500 } 497 }
501 498
502 for (auto iter = s->token_filters.rbegin(); iter != s->token_filters.rend(); ++iter) { 499 for (auto iter = s->token_filters.rbegin(); iter != s->token_filters.rend(); ++iter) {
503 - new_pipeline =  
504 - std::make_shared<Pl_QPDFTokenizer>("token filter", (*iter).get(), pipeline);  
505 - to_delete.push_back(new_pipeline); 500 + auto new_pipeline =
  501 + std::make_unique<Pl_QPDFTokenizer>("token filter", (*iter).get(), pipeline);
506 pipeline = new_pipeline.get(); 502 pipeline = new_pipeline.get();
  503 + to_delete.push_back(std::move(new_pipeline));
507 } 504 }
508 505
509 for (auto f_iter = filters.rbegin(); f_iter != filters.rend(); ++f_iter) { 506 for (auto f_iter = filters.rbegin(); f_iter != filters.rend(); ++f_iter) {
510 - auto decode_pipeline = (*f_iter)->getDecodePipeline(pipeline);  
511 - if (decode_pipeline) { 507 + if (auto decode_pipeline = (*f_iter)->getDecodePipeline(pipeline)) {
512 pipeline = decode_pipeline; 508 pipeline = decode_pipeline;
513 } 509 }
514 auto* flate = dynamic_cast<Pl_Flate*>(pipeline); 510 auto* flate = dynamic_cast<Pl_Flate*>(pipeline);
515 - if (flate != nullptr) { 511 + if (flate) {
516 flate->setWarnCallback([this](char const* msg, int code) { warn(msg); }); 512 flate->setWarnCallback([this](char const* msg, int code) { warn(msg); });
517 } 513 }
518 } 514 }
@@ -523,6 +519,7 @@ Stream::pipeStreamData( @@ -523,6 +519,7 @@ Stream::pipeStreamData(
523 pipeline->write(s->stream_data->getBuffer(), s->stream_data->getSize()); 519 pipeline->write(s->stream_data->getBuffer(), s->stream_data->getSize());
524 pipeline->finish(); 520 pipeline->finish();
525 } else if (s->stream_provider.get()) { 521 } else if (s->stream_provider.get()) {
  522 + bool success = true;
526 Pl_Count count("stream provider count", pipeline); 523 Pl_Count count("stream provider count", pipeline);
527 if (s->stream_provider->supportsRetry()) { 524 if (s->stream_provider->supportsRetry()) {
528 if (!s->stream_provider->provideStreamData( 525 if (!s->stream_provider->provideStreamData(
@@ -552,6 +549,9 @@ Stream::pipeStreamData( @@ -552,6 +549,9 @@ Stream::pipeStreamData(
552 QTC::TC("qpdf", "QPDF_Stream provider length not provided"); 549 QTC::TC("qpdf", "QPDF_Stream provider length not provided");
553 s->stream_dict.replaceKey("/Length", QPDFObjectHandle::newInteger(actual_length)); 550 s->stream_dict.replaceKey("/Length", QPDFObjectHandle::newInteger(actual_length));
554 } 551 }
  552 + if (!success) {
  553 + return false;
  554 + }
555 } else if (obj->getParsedOffset() == 0) { 555 } else if (obj->getParsedOffset() == 0) {
556 QTC::TC("qpdf", "QPDF_Stream pipe no stream data"); 556 QTC::TC("qpdf", "QPDF_Stream pipe no stream data");
557 throw std::logic_error("pipeStreamData called for stream with no data"); 557 throw std::logic_error("pipeStreamData called for stream with no data");
@@ -568,13 +568,13 @@ Stream::pipeStreamData( @@ -568,13 +568,13 @@ Stream::pipeStreamData(
568 suppress_warnings, 568 suppress_warnings,
569 will_retry)) { 569 will_retry)) {
570 filter = false; 570 filter = false;
571 - success = false; 571 + return false;
572 } 572 }
573 } 573 }
574 574
575 - if (filter && (!suppress_warnings) && normalizer.get() && normalizer->anyBadTokens()) { 575 + if (filter && !suppress_warnings && normalizer.anyBadTokens()) {
576 warn("content normalization encountered bad tokens"); 576 warn("content normalization encountered bad tokens");
577 - if (normalizer->lastTokenWasBad()) { 577 + if (normalizer.lastTokenWasBad()) {
578 QTC::TC("qpdf", "QPDF_Stream bad token at end during normalize"); 578 QTC::TC("qpdf", "QPDF_Stream bad token at end during normalize");
579 warn( 579 warn(
580 "normalized content ended with a bad token; you may be able to resolve this by " 580 "normalized content ended with a bad token; you may be able to resolve this by "
@@ -587,7 +587,7 @@ Stream::pipeStreamData( @@ -587,7 +587,7 @@ Stream::pipeStreamData(
587 "in the manual."); 587 "in the manual.");
588 } 588 }
589 589
590 - return success; 590 + return true;
591 } 591 }
592 592
593 void 593 void