Commit f126bfa7443410f7b4ae8a03eaf1a62d1cc63252
1 parent
96228864
Refactor filter name expansion logic in QPDF_Stream
Removed `expand_filter_name` method and integrated its functionality directly into `filter_factory` for improved clarity and maintainability. Consolidated filter handling logic to eliminate redundant operations and streamline code execution.
Showing
2 changed files
with
32 additions
and
48 deletions
libqpdf/QPDF_Stream.cc
| ... | ... | @@ -83,72 +83,57 @@ namespace |
| 83 | 83 | std::map<std::string, std::function<std::shared_ptr<QPDFStreamFilter>()>> filter_factories; |
| 84 | 84 | } // namespace |
| 85 | 85 | |
| 86 | -std::string | |
| 87 | -QPDF_Stream::Members::expand_filter_name(std::string const& name) const | |
| 86 | +std::function<std::shared_ptr<QPDFStreamFilter>()> | |
| 87 | +QPDF_Stream::Members::filter_factory(std::string const& name) const | |
| 88 | 88 | { |
| 89 | - // The PDF specification provides these filter abbreviations for use in inline images, but | |
| 90 | - // according to table H.1 in the pre-ISO versions of the PDF specification, Adobe Reader also | |
| 91 | - // accepts them for stream filters. | |
| 92 | - if (name == "/AHx") { | |
| 93 | - return "/ASCIIHexDecode"; | |
| 89 | + if (name == "/FlateDecode") { | |
| 90 | + return SF_FlateLzwDecode::flate_factory; | |
| 94 | 91 | } |
| 95 | - if (name == "/A85") { | |
| 96 | - return "/ASCII85Decode"; | |
| 92 | + if (name == "/Crypt") { | |
| 93 | + return []() { return std::make_shared<SF_Crypt>(); }; | |
| 97 | 94 | } |
| 98 | - if (name == "/LZW") { | |
| 99 | - return "/LZWDecode"; | |
| 95 | + if (name == "/LZWDecode") { | |
| 96 | + return SF_FlateLzwDecode::lzw_factory; | |
| 100 | 97 | } |
| 101 | - if (name == "/Fl") { | |
| 102 | - return "/FlateDecode"; | |
| 98 | + if (name == "/RunLengthDecode") { | |
| 99 | + return SF_RunLengthDecode::factory; | |
| 103 | 100 | } |
| 104 | - if (name == "/RL") { | |
| 105 | - return "/RunLengthDecode"; | |
| 101 | + if (name == "/DCTDecode") { | |
| 102 | + return SF_DCTDecode::factory; | |
| 106 | 103 | } |
| 107 | - if (name == "/CCF") { | |
| 108 | - return "/CCITTFaxDecode"; | |
| 104 | + if (name == "/ASCII85Decode") { | |
| 105 | + return SF_ASCII85Decode::factory; | |
| 109 | 106 | } |
| 110 | - if (name == "/DCT") { | |
| 111 | - return "/DCTDecode"; | |
| 107 | + if (name == "/ASCIIHexDecode") { | |
| 108 | + return SF_ASCIIHexDecode::factory; | |
| 112 | 109 | } |
| 113 | - return name; | |
| 114 | -}; | |
| 110 | + // The PDF specification provides these filter abbreviations for use in inline images, but | |
| 111 | + // according to table H.1 in the pre-ISO versions of the PDF specification, Adobe Reader also | |
| 112 | + // accepts them for stream filters. | |
| 115 | 113 | |
| 116 | -std::function<std::shared_ptr<QPDFStreamFilter>()> | |
| 117 | -QPDF_Stream::Members::filter_factory(std::string const& name) const | |
| 118 | -{ | |
| 119 | - auto e_name = expand_filter_name(name); | |
| 120 | - if (e_name == "/Crypt") { | |
| 121 | - return []() { return std::make_shared<SF_Crypt>(); }; | |
| 122 | - } | |
| 123 | - if (e_name == "/FlateDecode") { | |
| 114 | + if (name == "/Fl") { | |
| 124 | 115 | return SF_FlateLzwDecode::flate_factory; |
| 125 | 116 | } |
| 126 | - if (e_name == "/LZWDecode") { | |
| 127 | - return SF_FlateLzwDecode::lzw_factory; | |
| 128 | - } | |
| 129 | - if (e_name == "/RunLengthDecode") { | |
| 130 | - return SF_RunLengthDecode::factory; | |
| 131 | - } | |
| 132 | - if (e_name == "/DCTDecode") { | |
| 133 | - return SF_DCTDecode::factory; | |
| 117 | + if (name == "/AHx") { | |
| 118 | + return SF_ASCIIHexDecode::factory; | |
| 134 | 119 | } |
| 135 | - if (e_name == "/ASCII85Decode") { | |
| 120 | + if (name == "/A85") { | |
| 136 | 121 | return SF_ASCII85Decode::factory; |
| 137 | 122 | } |
| 138 | - if (e_name == "/RunLengthDecode") { | |
| 123 | + if (name == "/LZW") { | |
| 124 | + return SF_FlateLzwDecode::lzw_factory; | |
| 125 | + } | |
| 126 | + if (name == "/RL") { | |
| 139 | 127 | return SF_RunLengthDecode::factory; |
| 140 | 128 | } |
| 141 | - if (e_name == "/DCTDecode") { | |
| 129 | + if (name == "/DCT") { | |
| 142 | 130 | return SF_DCTDecode::factory; |
| 143 | 131 | } |
| 144 | - if (e_name == "/ASCII85Decode") { | |
| 145 | - return SF_ASCII85Decode::factory; | |
| 146 | - } | |
| 147 | - if (e_name == "/ASCIIHexDecode") { | |
| 148 | - return SF_ASCIIHexDecode::factory; | |
| 132 | + if (filter_factories.empty()) { | |
| 133 | + return nullptr; | |
| 149 | 134 | } |
| 150 | - | |
| 151 | - auto ff = filter_factories.find(e_name); | |
| 135 | + auto ff = | |
| 136 | + name == "/CCF" ? filter_factories.find("/CCITTFaxDecode") : filter_factories.find(name); | |
| 152 | 137 | if (ff == filter_factories.end()) { |
| 153 | 138 | return nullptr; |
| 154 | 139 | } | ... | ... |
libqpdf/qpdf/QPDFObject_private.hh
| ... | ... | @@ -226,7 +226,6 @@ class QPDF_Stream final |
| 226 | 226 | std::shared_ptr<Buffer> stream_data; |
| 227 | 227 | std::shared_ptr<QPDFObjectHandle::StreamDataProvider> stream_provider; |
| 228 | 228 | std::vector<std::shared_ptr<QPDFObjectHandle::TokenFilter>> token_filters; |
| 229 | - std::string expand_filter_name(std::string const& name) const; | |
| 230 | 229 | std::function<std::shared_ptr<QPDFStreamFilter>()> |
| 231 | 230 | filter_factory(std::string const& name) const; |
| 232 | 231 | }; | ... | ... |