Commit c82c6d0ab8eadac730c72d65677e9af28148b4be

Authored by m-holger
1 parent e9359c72

Refactor filter name expansion logic into a dedicated method

Moved the filter abbreviation expansion logic from a static map to a new `expand_filter_name` method in `QPDF_Stream::Members`. This simplifies the code, improves maintainability, and consolidates the logic for easier testing and updates.
libqpdf/QPDF_Stream.cc
... ... @@ -80,17 +80,34 @@ namespace
80 80 };
81 81 } // namespace
82 82  
83   -std::map<std::string, std::string> Stream::filter_abbreviations = {
  83 +std::string
  84 +QPDF_Stream::Members::expand_filter_name(std::string const& name) const
  85 +{
84 86 // The PDF specification provides these filter abbreviations for use in inline images, but
85 87 // according to table H.1 in the pre-ISO versions of the PDF specification, Adobe Reader also
86 88 // accepts them for stream filters.
87   - {"/AHx", "/ASCIIHexDecode"},
88   - {"/A85", "/ASCII85Decode"},
89   - {"/LZW", "/LZWDecode"},
90   - {"/Fl", "/FlateDecode"},
91   - {"/RL", "/RunLengthDecode"},
92   - {"/CCF", "/CCITTFaxDecode"},
93   - {"/DCT", "/DCTDecode"},
  89 + if (name == "/AHx") {
  90 + return "/ASCIIHexDecode";
  91 + }
  92 + if (name == "/A85") {
  93 + return "/ASCII85Decode";
  94 + }
  95 + if (name == "/LZW") {
  96 + return "/LZWDecode";
  97 + }
  98 + if (name == "/Fl") {
  99 + return "/FlateDecode";
  100 + }
  101 + if (name == "/RL") {
  102 + return "/RunLengthDecode";
  103 + }
  104 + if (name == "/CCF") {
  105 + return "/CCITTFaxDecode";
  106 + }
  107 + if (name == "/DCT") {
  108 + return "/DCTDecode";
  109 + }
  110 + return name;
94 111 };
95 112  
96 113 std::map<std::string, std::function<std::shared_ptr<QPDFStreamFilter>()>> Stream::filter_factories =
... ... @@ -333,10 +350,7 @@ Stream::filterable(
333 350 bool filterable = true;
334 351  
335 352 for (auto& filter_name: filter_names) {
336   - if (filter_abbreviations.count(filter_name)) {
337   - QTC::TC("qpdf", "QPDF_Stream expand filter abbreviation");
338   - filter_name = filter_abbreviations[filter_name];
339   - }
  353 + filter_name = s->expand_filter_name(filter_name);
340 354  
341 355 auto ff = filter_factories.find(filter_name);
342 356 if (ff == filter_factories.end()) {
... ...
libqpdf/qpdf/QPDFObject_private.hh
... ... @@ -226,6 +226,7 @@ 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;
229 230 };
230 231  
231 232 friend class QPDFObject;
... ...
qpdf/qpdf.testcov
... ... @@ -177,7 +177,6 @@ QPDFObjectHandle prepend page contents 0
177 177 QPDFObjectHandle append page contents 0
178 178 QPDF_Stream getRawStreamData 0
179 179 QPDF_Stream getStreamData 0
180   -QPDF_Stream expand filter abbreviation 0
181 180 qpdf-c called qpdf_read_memory 0
182 181 QPDF stream without newline 0
183 182 QPDF stream with CR only 0
... ...