/src/qpdf/libqpdf/SF_FlateLzwDecode.cc
Line | Count | Source |
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/QPDFObjectHandle_private.hh> |
9 | | #include <qpdf/QTC.hh> |
10 | | |
11 | | bool |
12 | | SF_FlateLzwDecode::setDecodeParms(QPDFObjectHandle decode_parms) |
13 | 21.4k | { |
14 | 21.4k | if (decode_parms.null()) { |
15 | 17.0k | return true; |
16 | 17.0k | } |
17 | | |
18 | 4.47k | auto memory_limit = Pl_Flate::memory_limit(); |
19 | | |
20 | 4.47k | std::set<std::string> keys = decode_parms.getKeys(); |
21 | 12.0k | for (auto const& key: keys) { |
22 | 12.0k | QPDFObjectHandle value = decode_parms.getKey(key); |
23 | 12.0k | if (key == "/Predictor") { |
24 | 2.16k | if (value.isInteger()) { |
25 | 2.15k | predictor = value.getIntValueAsInt(); |
26 | 2.15k | if (!(predictor == 1 || predictor == 2 || (predictor >= 10 && predictor <= 15))) { |
27 | 80 | return false; |
28 | 80 | } |
29 | 2.15k | } else { |
30 | 10 | return false; |
31 | 10 | } |
32 | 9.87k | } else if (key == "/Columns" || key == "/Colors" || key == "/BitsPerComponent") { |
33 | 2.11k | if (value.isInteger()) { |
34 | 2.10k | int val = value.getIntValueAsInt(); |
35 | 2.10k | if (memory_limit && static_cast<unsigned int>(val) > memory_limit) { |
36 | 28 | QPDFLogger::defaultLogger()->warn( |
37 | 28 | "SF_FlateLzwDecode parameter exceeds PL_Flate memory limit\n"); |
38 | 28 | return false; |
39 | 28 | } |
40 | 2.07k | if (key == "/Columns") { |
41 | 1.40k | columns = val; |
42 | 1.40k | } else if (key == "/Colors") { |
43 | 351 | colors = val; |
44 | 351 | } else if (key == "/BitsPerComponent") { |
45 | 315 | bits_per_component = val; |
46 | 315 | } |
47 | 2.07k | } else { |
48 | 16 | return false; |
49 | 16 | } |
50 | 7.75k | } else if (lzw && (key == "/EarlyChange")) { |
51 | 116 | if (value.isInteger()) { |
52 | 113 | int earlychange = value.getIntValueAsInt(); |
53 | 113 | early_code_change = (earlychange == 1); |
54 | 113 | if (!(earlychange == 0 || earlychange == 1)) { |
55 | 37 | return false; |
56 | 37 | } |
57 | 113 | } else { |
58 | 3 | return false; |
59 | 3 | } |
60 | 116 | } |
61 | 12.0k | } |
62 | | |
63 | 4.30k | if (predictor > 1 && columns == 0) { |
64 | 3 | return false; |
65 | 3 | } |
66 | | |
67 | 4.29k | return true; |
68 | 4.30k | } |
69 | | |
70 | | Pipeline* |
71 | | SF_FlateLzwDecode::getDecodePipeline(Pipeline* next) |
72 | 21.2k | { |
73 | 21.2k | std::unique_ptr<Pipeline> pipeline; |
74 | 21.2k | if (predictor >= 10 && predictor <= 15) { |
75 | 1.41k | QTC::TC("qpdf", "SF_FlateLzwDecode PNG filter"); |
76 | 1.41k | pipeline = std::make_unique<Pl_PNGFilter>( |
77 | 1.41k | "png decode", |
78 | 1.41k | next, |
79 | 1.41k | Pl_PNGFilter::a_decode, |
80 | 1.41k | QIntC::to_uint(columns), |
81 | 1.41k | QIntC::to_uint(colors), |
82 | 1.41k | QIntC::to_uint(bits_per_component)); |
83 | 1.41k | next = pipeline.get(); |
84 | 1.41k | pipelines.push_back(std::move(pipeline)); |
85 | 19.8k | } else if (predictor == 2) { |
86 | 550 | QTC::TC("qpdf", "SF_FlateLzwDecode TIFF predictor"); |
87 | 550 | pipeline = std::make_unique<Pl_TIFFPredictor>( |
88 | 550 | "tiff decode", |
89 | 550 | next, |
90 | 550 | Pl_TIFFPredictor::a_decode, |
91 | 550 | QIntC::to_uint(columns), |
92 | 550 | QIntC::to_uint(colors), |
93 | 550 | QIntC::to_uint(bits_per_component)); |
94 | 550 | next = pipeline.get(); |
95 | 550 | pipelines.push_back(std::move(pipeline)); |
96 | 550 | } |
97 | | |
98 | 21.2k | if (lzw) { |
99 | 2.67k | pipeline = std::make_unique<Pl_LZWDecoder>("lzw decode", next, early_code_change); |
100 | 18.6k | } else { |
101 | 18.6k | pipeline = std::make_unique<Pl_Flate>("stream inflate", next, Pl_Flate::a_inflate); |
102 | 18.6k | } |
103 | 21.2k | next = pipeline.get(); |
104 | 21.2k | pipelines.push_back(std::move(pipeline)); |
105 | 21.2k | return next; |
106 | 21.2k | } |