/src/qpdf/libqpdf/SF_FlateLzwDecode.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include <qpdf/SF_FlateLzwDecode.hh> |
2 | | |
3 | | #include <qpdf/Pl_Flate.hh> |
4 | | #include <qpdf/Pl_LZWDecoder.hh> |
5 | | #include <qpdf/Pl_PNGFilter.hh> |
6 | | #include <qpdf/Pl_TIFFPredictor.hh> |
7 | | #include <qpdf/QIntC.hh> |
8 | | #include <qpdf/QTC.hh> |
9 | | |
10 | | SF_FlateLzwDecode::SF_FlateLzwDecode(bool lzw) : |
11 | | lzw(lzw), |
12 | | // Initialize values to their defaults as per the PDF spec |
13 | | predictor(1), |
14 | | columns(1), |
15 | | colors(1), |
16 | | bits_per_component(8), |
17 | | early_code_change(true) |
18 | 40.6k | { |
19 | 40.6k | } |
20 | | |
21 | | bool |
22 | | SF_FlateLzwDecode::setDecodeParms(QPDFObjectHandle decode_parms) |
23 | 40.5k | { |
24 | 40.5k | if (decode_parms.isNull()) { |
25 | 38.2k | return true; |
26 | 38.2k | } |
27 | | |
28 | 2.30k | bool filterable = true; |
29 | 2.30k | std::set<std::string> keys = decode_parms.getKeys(); |
30 | 6.26k | for (auto const& key: keys) { |
31 | 6.26k | QPDFObjectHandle value = decode_parms.getKey(key); |
32 | 6.26k | if (key == "/Predictor") { |
33 | 2.20k | if (value.isInteger()) { |
34 | 2.20k | this->predictor = value.getIntValueAsInt(); |
35 | 2.20k | if (!((this->predictor == 1) || (this->predictor == 2) || |
36 | 2.20k | ((this->predictor >= 10) && (this->predictor <= 15)))) { |
37 | 15 | filterable = false; |
38 | 15 | } |
39 | 2.20k | } else { |
40 | 6 | filterable = false; |
41 | 6 | } |
42 | 4.05k | } else if ((key == "/Columns") || (key == "/Colors") || (key == "/BitsPerComponent")) { |
43 | 3.64k | if (value.isInteger()) { |
44 | 3.63k | int val = value.getIntValueAsInt(); |
45 | 3.63k | if (key == "/Columns") { |
46 | 2.20k | this->columns = val; |
47 | 2.20k | } else if (key == "/Colors") { |
48 | 732 | this->colors = val; |
49 | 732 | } else if (key == "/BitsPerComponent") { |
50 | 701 | this->bits_per_component = val; |
51 | 701 | } |
52 | 3.63k | } else { |
53 | 8 | filterable = false; |
54 | 8 | } |
55 | 3.64k | } else if (lzw && (key == "/EarlyChange")) { |
56 | 0 | if (value.isInteger()) { |
57 | 0 | int earlychange = value.getIntValueAsInt(); |
58 | 0 | this->early_code_change = (earlychange == 1); |
59 | 0 | if (!((earlychange == 0) || (earlychange == 1))) { |
60 | 0 | filterable = false; |
61 | 0 | } |
62 | 0 | } else { |
63 | 0 | filterable = false; |
64 | 0 | } |
65 | 0 | } |
66 | 6.26k | } |
67 | | |
68 | 2.30k | if ((this->predictor > 1) && (this->columns == 0)) { |
69 | 4 | filterable = false; |
70 | 4 | } |
71 | | |
72 | 2.30k | return filterable; |
73 | 40.5k | } |
74 | | |
75 | | Pipeline* |
76 | | SF_FlateLzwDecode::getDecodePipeline(Pipeline* next) |
77 | 40.5k | { |
78 | 40.5k | std::shared_ptr<Pipeline> pipeline; |
79 | 40.5k | if ((this->predictor >= 10) && (this->predictor <= 15)) { |
80 | 2.07k | QTC::TC("qpdf", "SF_FlateLzwDecode PNG filter"); |
81 | 2.07k | pipeline = std::make_shared<Pl_PNGFilter>( |
82 | 2.07k | "png decode", |
83 | 2.07k | next, |
84 | 2.07k | Pl_PNGFilter::a_decode, |
85 | 2.07k | QIntC::to_uint(this->columns), |
86 | 2.07k | QIntC::to_uint(this->colors), |
87 | 2.07k | QIntC::to_uint(this->bits_per_component)); |
88 | 2.07k | this->pipelines.push_back(pipeline); |
89 | 2.07k | next = pipeline.get(); |
90 | 38.4k | } else if (this->predictor == 2) { |
91 | 99 | QTC::TC("qpdf", "SF_FlateLzwDecode TIFF predictor"); |
92 | 99 | pipeline = std::make_shared<Pl_TIFFPredictor>( |
93 | 99 | "tiff decode", |
94 | 99 | next, |
95 | 99 | Pl_TIFFPredictor::a_decode, |
96 | 99 | QIntC::to_uint(this->columns), |
97 | 99 | QIntC::to_uint(this->colors), |
98 | 99 | QIntC::to_uint(this->bits_per_component)); |
99 | 99 | this->pipelines.push_back(pipeline); |
100 | 99 | next = pipeline.get(); |
101 | 99 | } |
102 | | |
103 | 40.5k | if (lzw) { |
104 | 659 | pipeline = std::make_shared<Pl_LZWDecoder>("lzw decode", next, early_code_change); |
105 | 39.8k | } else { |
106 | 39.8k | pipeline = std::make_shared<Pl_Flate>("stream inflate", next, Pl_Flate::a_inflate); |
107 | 39.8k | } |
108 | 40.5k | this->pipelines.push_back(pipeline); |
109 | 40.5k | return pipeline.get(); |
110 | 40.5k | } |
111 | | |
112 | | std::shared_ptr<QPDFStreamFilter> |
113 | | SF_FlateLzwDecode::flate_factory() |
114 | 39.9k | { |
115 | 39.9k | return std::make_shared<SF_FlateLzwDecode>(false); |
116 | 39.9k | } |
117 | | |
118 | | std::shared_ptr<QPDFStreamFilter> |
119 | | SF_FlateLzwDecode::lzw_factory() |
120 | 662 | { |
121 | 662 | return std::make_shared<SF_FlateLzwDecode>(true); |
122 | 662 | } |