/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 | 222k | { |
14 | 222k | if (decode_parms.null()) { |
15 | 159k | return true; |
16 | 159k | } |
17 | | |
18 | 63.0k | auto memory_limit = Pl_Flate::memory_limit(); |
19 | | |
20 | 63.0k | std::set<std::string> keys = decode_parms.getKeys(); |
21 | 154k | for (auto const& key: keys) { |
22 | 154k | QPDFObjectHandle value = decode_parms.getKey(key); |
23 | 154k | if (key == "/Predictor") { |
24 | 38.9k | if (value.isInteger()) { |
25 | 38.7k | predictor = value.getIntValueAsInt(); |
26 | 38.7k | if (!(predictor == 1 || predictor == 2 || (predictor >= 10 && predictor <= 15))) { |
27 | 972 | return false; |
28 | 972 | } |
29 | 38.7k | } else { |
30 | 166 | return false; |
31 | 166 | } |
32 | 115k | } else if (key == "/Columns" || key == "/Colors" || key == "/BitsPerComponent") { |
33 | 40.4k | if (value.isInteger()) { |
34 | 40.2k | int val = value.getIntValueAsInt(); |
35 | 40.2k | if (memory_limit && static_cast<unsigned int>(val) > memory_limit) { |
36 | 306 | QPDFLogger::defaultLogger()->warn( |
37 | 306 | "SF_FlateLzwDecode parameter exceeds PL_Flate memory limit\n"); |
38 | 306 | return false; |
39 | 306 | } |
40 | 39.9k | if (key == "/Columns") { |
41 | 24.3k | columns = val; |
42 | 24.3k | } else if (key == "/Colors") { |
43 | 7.50k | colors = val; |
44 | 8.15k | } else if (key == "/BitsPerComponent") { |
45 | 8.14k | bits_per_component = val; |
46 | 8.14k | } |
47 | 39.9k | } else { |
48 | 136 | return false; |
49 | 136 | } |
50 | 74.9k | } else if (lzw && (key == "/EarlyChange")) { |
51 | 2.09k | if (value.isInteger()) { |
52 | 2.01k | int earlychange = value.getIntValueAsInt(); |
53 | 2.01k | early_code_change = (earlychange == 1); |
54 | 2.01k | if (!(earlychange == 0 || earlychange == 1)) { |
55 | 533 | return false; |
56 | 533 | } |
57 | 2.01k | } else { |
58 | 78 | return false; |
59 | 78 | } |
60 | 2.09k | } |
61 | 154k | } |
62 | | |
63 | 60.8k | if (predictor > 1 && columns == 0) { |
64 | 91 | return false; |
65 | 91 | } |
66 | | |
67 | 60.7k | return true; |
68 | 60.8k | } |
69 | | |
70 | | Pipeline* |
71 | | SF_FlateLzwDecode::getDecodePipeline(Pipeline* next) |
72 | 219k | { |
73 | 219k | std::unique_ptr<Pipeline> pipeline; |
74 | 219k | if (predictor >= 10 && predictor <= 15) { |
75 | 25.3k | QTC::TC("qpdf", "SF_FlateLzwDecode PNG filter"); |
76 | 25.3k | pipeline = std::make_unique<Pl_PNGFilter>( |
77 | 25.3k | "png decode", |
78 | 25.3k | next, |
79 | 25.3k | Pl_PNGFilter::a_decode, |
80 | 25.3k | QIntC::to_uint(columns), |
81 | 25.3k | QIntC::to_uint(colors), |
82 | 25.3k | QIntC::to_uint(bits_per_component)); |
83 | 25.3k | next = pipeline.get(); |
84 | 25.3k | pipelines.push_back(std::move(pipeline)); |
85 | 193k | } else if (predictor == 2) { |
86 | 10.5k | QTC::TC("qpdf", "SF_FlateLzwDecode TIFF predictor"); |
87 | 10.5k | pipeline = std::make_unique<Pl_TIFFPredictor>( |
88 | 10.5k | "tiff decode", |
89 | 10.5k | next, |
90 | 10.5k | Pl_TIFFPredictor::a_decode, |
91 | 10.5k | QIntC::to_uint(columns), |
92 | 10.5k | QIntC::to_uint(colors), |
93 | 10.5k | QIntC::to_uint(bits_per_component)); |
94 | 10.5k | next = pipeline.get(); |
95 | 10.5k | pipelines.push_back(std::move(pipeline)); |
96 | 10.5k | } |
97 | | |
98 | 219k | if (lzw) { |
99 | 44.1k | pipeline = std::make_unique<Pl_LZWDecoder>("lzw decode", next, early_code_change); |
100 | 174k | } else { |
101 | 174k | pipeline = std::make_unique<Pl_Flate>("stream inflate", next, Pl_Flate::a_inflate); |
102 | 174k | } |
103 | 219k | next = pipeline.get(); |
104 | 219k | pipelines.push_back(std::move(pipeline)); |
105 | 219k | return next; |
106 | 219k | } |