From 12f1eb15ca3fed6310402847559a7c99d3c77847 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 2 Apr 2022 17:14:10 -0400 Subject: Programmatically apply new formatting to code Run this: for i in **/*.cc **/*.c **/*.h **/*.hh; do clang-format < $i >| $i.new && mv $i.new $i done --- libqpdf/QPDF_Stream.cc | 450 ++++++++++++++++++++++--------------------------- 1 file changed, 205 insertions(+), 245 deletions(-) (limited to 'libqpdf/QPDF_Stream.cc') diff --git a/libqpdf/QPDF_Stream.cc b/libqpdf/QPDF_Stream.cc index 9e995e1f..1521cc99 100644 --- a/libqpdf/QPDF_Stream.cc +++ b/libqpdf/QPDF_Stream.cc @@ -1,21 +1,21 @@ #include -#include +#include #include -#include #include #include -#include -#include -#include -#include +#include #include #include -#include -#include -#include +#include +#include +#include +#include #include #include +#include +#include +#include #include @@ -25,30 +25,27 @@ class SF_Crypt: public QPDFStreamFilter SF_Crypt() = default; virtual ~SF_Crypt() = default; - virtual bool setDecodeParms(QPDFObjectHandle decode_parms) + virtual bool + setDecodeParms(QPDFObjectHandle decode_parms) { - if (decode_parms.isNull()) - { + if (decode_parms.isNull()) { return true; } bool filterable = true; - for (auto const& key: decode_parms.getKeys()) - { + for (auto const& key : decode_parms.getKeys()) { if (((key == "/Type") || (key == "/Name")) && - ((! decode_parms.hasKey("/Type")) || - decode_parms.isDictionaryOfType("/CryptFilterDecodeParms"))) - { + ((!decode_parms.hasKey("/Type")) || + decode_parms.isDictionaryOfType("/CryptFilterDecodeParms"))) { // we handle this in decryptStream - } - else - { + } else { filterable = false; } } return filterable; } - virtual Pipeline* getDecodePipeline(Pipeline*) + virtual Pipeline* + getDecodePipeline(Pipeline*) { // Not used -- handled by pipeStreamData return nullptr; @@ -69,22 +66,24 @@ std::map QPDF_Stream::filter_abbreviations = { {"/DCT", "/DCTDecode"}, }; -std::map< - std::string, - std::function()>> -QPDF_Stream::filter_factories = { - {"/Crypt", []() { return std::make_shared(); }}, - {"/FlateDecode", SF_FlateLzwDecode::flate_factory}, - {"/LZWDecode", SF_FlateLzwDecode::lzw_factory}, - {"/RunLengthDecode", SF_RunLengthDecode::factory}, - {"/DCTDecode", SF_DCTDecode::factory}, - {"/ASCII85Decode", SF_ASCII85Decode::factory}, - {"/ASCIIHexDecode", SF_ASCIIHexDecode::factory}, +std::map()>> + QPDF_Stream::filter_factories = { + {"/Crypt", []() { return std::make_shared(); }}, + {"/FlateDecode", SF_FlateLzwDecode::flate_factory}, + {"/LZWDecode", SF_FlateLzwDecode::lzw_factory}, + {"/RunLengthDecode", SF_RunLengthDecode::factory}, + {"/DCTDecode", SF_DCTDecode::factory}, + {"/ASCII85Decode", SF_ASCII85Decode::factory}, + {"/ASCIIHexDecode", SF_ASCIIHexDecode::factory}, }; -QPDF_Stream::QPDF_Stream(QPDF* qpdf, int objid, int generation, - QPDFObjectHandle stream_dict, - qpdf_offset_t offset, size_t length) : +QPDF_Stream::QPDF_Stream( + QPDF* qpdf, + int objid, + int generation, + QPDFObjectHandle stream_dict, + qpdf_offset_t offset, + size_t length) : qpdf(qpdf), objid(objid), generation(generation), @@ -93,11 +92,9 @@ QPDF_Stream::QPDF_Stream(QPDF* qpdf, int objid, int generation, offset(offset), length(length) { - if (! stream_dict.isDictionary()) - { - throw std::logic_error( - "stream object instantiated with non-dictionary " - "object for dictionary"); + if (!stream_dict.isDictionary()) { + throw std::logic_error("stream object instantiated with non-dictionary " + "object for dictionary"); } setStreamDescription(); } @@ -136,8 +133,7 @@ QPDF_Stream::releaseResolved() void QPDF_Stream::setObjGen(int objid, int generation) { - if (! ((this->objid == 0) && (this->generation == 0))) - { + if (!((this->objid == 0) && (this->generation == 0))) { throw std::logic_error( "attempt to set object ID and generation of a stream" " that already has them"); @@ -184,9 +180,9 @@ QPDF_Stream::setStreamDescription() { setDescription( this->qpdf, - this->qpdf->getFilename() + - ", stream object " + QUtil::int_to_string(this->objid) + " " + - QUtil::int_to_string(this->generation)); + this->qpdf->getFilename() + ", stream object " + + QUtil::int_to_string(this->objid) + " " + + QUtil::int_to_string(this->generation)); } void @@ -194,9 +190,8 @@ QPDF_Stream::setDictDescription() { QPDF* qpdf = 0; std::string description; - if ((! this->stream_dict.hasObjectDescription()) && - getDescription(qpdf, description)) - { + if ((!this->stream_dict.hasObjectDescription()) && + getDescription(qpdf, description)) { this->stream_dict.setObjectDescription( qpdf, description + " -> stream dictionary"); } @@ -211,7 +206,7 @@ QPDF_Stream::getDict() const bool QPDF_Stream::isDataModified() const { - return (! this->token_filters.empty()); + return (!this->token_filters.empty()); } qpdf_offset_t @@ -244,11 +239,13 @@ QPDF_Stream::getStreamData(qpdf_stream_decode_level_e decode_level) Pl_Buffer buf("stream data buffer"); bool filtered; pipeStreamData(&buf, &filtered, 0, decode_level, false, false); - if (! filtered) - { - throw QPDFExc(qpdf_e_unsupported, qpdf->getFilename(), - "", this->offset, - "getStreamData called on unfilterable stream"); + if (!filtered) { + throw QPDFExc( + qpdf_e_unsupported, + qpdf->getFilename(), + "", + this->offset, + "getStreamData called on unfilterable stream"); } QTC::TC("qpdf", "QPDF_Stream getStreamData"); return buf.getBufferSharedPointer(); @@ -258,11 +255,13 @@ PointerHolder QPDF_Stream::getRawStreamData() { Pl_Buffer buf("stream data buffer"); - if (! pipeStreamData(&buf, nullptr, 0, qpdf_dl_none, false, false)) - { - throw QPDFExc(qpdf_e_unsupported, qpdf->getFilename(), - "", this->offset, - "error getting raw stream data"); + if (!pipeStreamData(&buf, nullptr, 0, qpdf_dl_none, false, false)) { + throw QPDFExc( + qpdf_e_unsupported, + qpdf->getFilename(), + "", + this->offset, + "error getting raw stream data"); } QTC::TC("qpdf", "QPDF_Stream getRawStreamData"); return buf.getBufferSharedPointer(); @@ -281,69 +280,54 @@ QPDF_Stream::filterable( std::vector filter_names; - if (filter_obj.isNull()) - { + if (filter_obj.isNull()) { // No filters - } - else if (filter_obj.isName()) - { + } else if (filter_obj.isName()) { // One filter filter_names.push_back(filter_obj.getName()); - } - else if (filter_obj.isArray()) - { + } else if (filter_obj.isArray()) { // Potentially multiple filters int n = filter_obj.getArrayNItems(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { QPDFObjectHandle item = filter_obj.getArrayItem(i); - if (item.isName()) - { + if (item.isName()) { filter_names.push_back(item.getName()); - } - else - { + } else { filters_okay = false; } } - } - else - { + } else { filters_okay = false; } - if (! filters_okay) - { + if (!filters_okay) { QTC::TC("qpdf", "QPDF_Stream invalid filter"); - warn(QPDFExc(qpdf_e_damaged_pdf, qpdf->getFilename(), - "", this->offset, - "stream filter type is not name or array")); + warn(QPDFExc( + qpdf_e_damaged_pdf, + qpdf->getFilename(), + "", + this->offset, + "stream filter type is not name or array")); return false; } bool filterable = true; - for (auto& filter_name: filter_names) - { - if (filter_abbreviations.count(filter_name)) - { + for (auto& filter_name : filter_names) { + if (filter_abbreviations.count(filter_name)) { QTC::TC("qpdf", "QPDF_Stream expand filter abbreviation"); filter_name = filter_abbreviations[filter_name]; } auto ff = filter_factories.find(filter_name); - if (ff == filter_factories.end()) - { + if (ff == filter_factories.end()) { filterable = false; - } - else - { + } else { filters.push_back((ff->second)()); } } - if (! filterable) - { + if (!filterable) { return false; } @@ -354,21 +338,15 @@ QPDF_Stream::filterable( QPDFObjectHandle decode_obj = this->stream_dict.getKey("/DecodeParms"); std::vector decode_parms; - if (decode_obj.isArray() && (decode_obj.getArrayNItems() == 0)) - { + if (decode_obj.isArray() && (decode_obj.getArrayNItems() == 0)) { decode_obj = QPDFObjectHandle::newNull(); } - if (decode_obj.isArray()) - { - for (int i = 0; i < decode_obj.getArrayNItems(); ++i) - { + if (decode_obj.isArray()) { + for (int i = 0; i < decode_obj.getArrayNItems(); ++i) { decode_parms.push_back(decode_obj.getArrayItem(i)); } - } - else - { - for (unsigned int i = 0; i < filter_names.size(); ++i) - { + } else { + for (unsigned int i = 0; i < filter_names.size(); ++i) { decode_parms.push_back(decode_obj); } } @@ -376,39 +354,34 @@ QPDF_Stream::filterable( // Ignore /DecodeParms entirely if /Filters is empty. At least // one case of a file whose /DecodeParms was [ << >> ] when // /Filters was empty has been seen in the wild. - if ((filters.size() != 0) && (decode_parms.size() != filters.size())) - { - warn(QPDFExc(qpdf_e_damaged_pdf, qpdf->getFilename(), - "", this->offset, - "stream /DecodeParms length is" - " inconsistent with filters")); + if ((filters.size() != 0) && (decode_parms.size() != filters.size())) { + warn(QPDFExc( + qpdf_e_damaged_pdf, + qpdf->getFilename(), + "", + this->offset, + "stream /DecodeParms length is" + " inconsistent with filters")); filterable = false; } - if (! filterable) - { + if (!filterable) { return false; } - for (size_t i = 0; i < filters.size(); ++i) - { + for (size_t i = 0; i < filters.size(); ++i) { auto filter = filters.at(i); auto decode_item = decode_parms.at(i); - if (filter->setDecodeParms(decode_item)) - { - if (filter->isSpecializedCompression()) - { + if (filter->setDecodeParms(decode_item)) { + if (filter->isSpecializedCompression()) { specialized_compression = true; } - if (filter->isLossyCompression()) - { + if (filter->isLossyCompression()) { specialized_compression = true; lossy_compression = true; } - } - else - { + } else { filterable = false; } } @@ -417,43 +390,43 @@ QPDF_Stream::filterable( } bool -QPDF_Stream::pipeStreamData(Pipeline* pipeline, bool* filterp, - int encode_flags, - qpdf_stream_decode_level_e decode_level, - bool suppress_warnings, bool will_retry) +QPDF_Stream::pipeStreamData( + Pipeline* pipeline, + bool* filterp, + int encode_flags, + qpdf_stream_decode_level_e decode_level, + bool suppress_warnings, + bool will_retry) { std::vector> filters; bool specialized_compression = false; bool lossy_compression = false; bool ignored; - if (filterp == nullptr) - { + if (filterp == nullptr) { filterp = &ignored; } bool& filter = *filterp; - filter = (! ((encode_flags == 0) && (decode_level == qpdf_dl_none))); + filter = (!((encode_flags == 0) && (decode_level == qpdf_dl_none))); bool success = true; - if (filter) - { - filter = filterable( - filters, specialized_compression, lossy_compression); - if ((decode_level < qpdf_dl_all) && lossy_compression) - { + if (filter) { + filter = + filterable(filters, specialized_compression, lossy_compression); + if ((decode_level < qpdf_dl_all) && lossy_compression) { filter = false; } - if ((decode_level < qpdf_dl_specialized) && specialized_compression) - { + if ((decode_level < qpdf_dl_specialized) && specialized_compression) { filter = false; } - QTC::TC("qpdf", "QPDF_Stream special filters", - (! filter) ? 0 : - lossy_compression ? 1 : - specialized_compression ? 2 : - 3); + QTC::TC( + "qpdf", + "QPDF_Stream special filters", + (!filter) ? 0 + : lossy_compression ? 1 + : specialized_compression ? 2 + : 3); } - if (pipeline == 0) - { + if (pipeline == 0) { QTC::TC("qpdf", "QPDF_Stream pipeStreamData with null pipeline"); // Return value is whether we can filter in this case. return filter; @@ -467,18 +440,15 @@ QPDF_Stream::pipeStreamData(Pipeline* pipeline, bool* filterp, PointerHolder normalizer; std::shared_ptr new_pipeline; - if (filter) - { - if (encode_flags & qpdf_ef_compress) - { + if (filter) { + if (encode_flags & qpdf_ef_compress) { new_pipeline = std::make_shared( "compress stream", pipeline, Pl_Flate::a_deflate); to_delete.push_back(new_pipeline); pipeline = new_pipeline.get(); } - if (encode_flags & qpdf_ef_normalize) - { + if (encode_flags & qpdf_ef_normalize) { normalizer = make_pointer_holder(); new_pipeline = std::make_shared( "normalizer", normalizer.get(), pipeline); @@ -487,143 +457,138 @@ QPDF_Stream::pipeStreamData(Pipeline* pipeline, bool* filterp, } for (auto iter = this->token_filters.rbegin(); - iter != this->token_filters.rend(); ++iter) - { + iter != this->token_filters.rend(); + ++iter) { new_pipeline = std::make_shared( "token filter", (*iter).get(), pipeline); to_delete.push_back(new_pipeline); pipeline = new_pipeline.get(); } - for (auto f_iter = filters.rbegin(); - f_iter != filters.rend(); ++f_iter) - { + for (auto f_iter = filters.rbegin(); f_iter != filters.rend(); + ++f_iter) { auto decode_pipeline = (*f_iter)->getDecodePipeline(pipeline); - if (decode_pipeline) - { + if (decode_pipeline) { pipeline = decode_pipeline; } Pl_Flate* flate = dynamic_cast(pipeline); - if (flate != nullptr) - { + if (flate != nullptr) { flate->setWarnCallback([this](char const* msg, int code) { - warn(QPDFExc(qpdf_e_damaged_pdf, qpdf->getFilename(), - "", this->offset, msg)); + warn(QPDFExc( + qpdf_e_damaged_pdf, + qpdf->getFilename(), + "", + this->offset, + msg)); }); } } } - if (this->stream_data.get()) - { + if (this->stream_data.get()) { QTC::TC("qpdf", "QPDF_Stream pipe replaced stream data"); - pipeline->write(this->stream_data->getBuffer(), - this->stream_data->getSize()); + pipeline->write( + this->stream_data->getBuffer(), this->stream_data->getSize()); pipeline->finish(); - } - else if (this->stream_provider.get()) - { + } else if (this->stream_provider.get()) { Pl_Count count("stream provider count", pipeline); - if (this->stream_provider->supportsRetry()) - { - if (! this->stream_provider->provideStreamData( - this->objid, this->generation, &count, - suppress_warnings, will_retry)) - { + if (this->stream_provider->supportsRetry()) { + if (!this->stream_provider->provideStreamData( + this->objid, + this->generation, + &count, + suppress_warnings, + will_retry)) { filter = false; success = false; } - } - else - { + } else { this->stream_provider->provideStreamData( this->objid, this->generation, &count); } qpdf_offset_t actual_length = count.getCount(); qpdf_offset_t desired_length = 0; - if (success && this->stream_dict.hasKey("/Length")) - { + if (success && this->stream_dict.hasKey("/Length")) { desired_length = this->stream_dict.getKey("/Length").getIntValue(); - if (actual_length == desired_length) - { + if (actual_length == desired_length) { QTC::TC("qpdf", "QPDF_Stream pipe use stream provider"); - } - else - { + } else { QTC::TC("qpdf", "QPDF_Stream provider length mismatch"); // This would be caused by programmer error on the // part of a library user, not by invalid input data. throw std::runtime_error( "stream data provider for " + QUtil::int_to_string(this->objid) + " " + - QUtil::int_to_string(this->generation) + - " provided " + + QUtil::int_to_string(this->generation) + " provided " + QUtil::int_to_string(actual_length) + " bytes instead of expected " + QUtil::int_to_string(desired_length) + " bytes"); } - } - else if (success) - { + } else if (success) { QTC::TC("qpdf", "QPDF_Stream provider length not provided"); this->stream_dict.replaceKey( "/Length", QPDFObjectHandle::newInteger(actual_length)); } - } - else if (this->offset == 0) - { + } else if (this->offset == 0) { QTC::TC("qpdf", "QPDF_Stream pipe no stream data"); - throw std::logic_error( - "pipeStreamData called for stream with no data"); - } - else - { + throw std::logic_error("pipeStreamData called for stream with no data"); + } else { QTC::TC("qpdf", "QPDF_Stream pipe original stream data"); - if (! QPDF::Pipe::pipeStreamData(this->qpdf, this->objid, this->generation, - this->offset, this->length, - this->stream_dict, pipeline, - suppress_warnings, - will_retry)) - { + if (!QPDF::Pipe::pipeStreamData( + this->qpdf, + this->objid, + this->generation, + this->offset, + this->length, + this->stream_dict, + pipeline, + suppress_warnings, + will_retry)) { filter = false; success = false; } } - if (filter && - (! suppress_warnings) && - normalizer.get() && - normalizer->anyBadTokens()) - { - warn(QPDFExc(qpdf_e_damaged_pdf, qpdf->getFilename(), - "", this->offset, - "content normalization encountered bad tokens")); - if (normalizer->lastTokenWasBad()) - { + if (filter && (!suppress_warnings) && normalizer.get() && + normalizer->anyBadTokens()) { + warn(QPDFExc( + qpdf_e_damaged_pdf, + qpdf->getFilename(), + "", + this->offset, + "content normalization encountered bad tokens")); + if (normalizer->lastTokenWasBad()) { QTC::TC("qpdf", "QPDF_Stream bad token at end during normalize"); - warn(QPDFExc(qpdf_e_damaged_pdf, qpdf->getFilename(), - "", this->offset, - "normalized content ended with a bad token;" - " you may be able to resolve this by" - " coalescing content streams in combination" - " with normalizing content. From the command" - " line, specify --coalesce-contents")); + warn(QPDFExc( + qpdf_e_damaged_pdf, + qpdf->getFilename(), + "", + this->offset, + "normalized content ended with a bad token;" + " you may be able to resolve this by" + " coalescing content streams in combination" + " with normalizing content. From the command" + " line, specify --coalesce-contents")); } - warn(QPDFExc(qpdf_e_damaged_pdf, qpdf->getFilename(), - "", this->offset, - "Resulting stream data may be corrupted but is" - " may still useful for manual inspection." - " For more information on this warning, search" - " for content normalization in the manual.")); + warn(QPDFExc( + qpdf_e_damaged_pdf, + qpdf->getFilename(), + "", + this->offset, + "Resulting stream data may be corrupted but is" + " may still useful for manual inspection." + " For more information on this warning, search" + " for content normalization in the manual.")); } return success; } void -QPDF_Stream::replaceStreamData(PointerHolder data, - QPDFObjectHandle const& filter, - QPDFObjectHandle const& decode_parms) +QPDF_Stream::replaceStreamData( + PointerHolder data, + QPDFObjectHandle const& filter, + QPDFObjectHandle const& decode_parms) { this->stream_data = data; this->stream_provider = 0; @@ -649,22 +614,20 @@ QPDF_Stream::addTokenFilter( } void -QPDF_Stream::replaceFilterData(QPDFObjectHandle const& filter, - QPDFObjectHandle const& decode_parms, - size_t length) +QPDF_Stream::replaceFilterData( + QPDFObjectHandle const& filter, + QPDFObjectHandle const& decode_parms, + size_t length) { this->stream_dict.replaceOrRemoveKey("/Filter", filter); this->stream_dict.replaceOrRemoveKey("/DecodeParms", decode_parms); - if (length == 0) - { + if (length == 0) { QTC::TC("qpdf", "QPDF_Stream unknown stream length"); this->stream_dict.removeKey("/Length"); - } - else - { + } else { this->stream_dict.replaceKey( - "/Length", QPDFObjectHandle::newInteger( - QIntC::to_longlong(length))); + "/Length", + QPDFObjectHandle::newInteger(QIntC::to_longlong(length))); } } @@ -674,12 +637,9 @@ QPDF_Stream::replaceDict(QPDFObjectHandle new_dict) this->stream_dict = new_dict; setDictDescription(); QPDFObjectHandle length_obj = new_dict.getKey("/Length"); - if (length_obj.isInteger()) - { + if (length_obj.isInteger()) { this->length = QIntC::to_size(length_obj.getUIntValue()); - } - else - { + } else { this->length = 0; } } -- cgit v1.2.3-70-g09d2