/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 | 233k | { |
14 | 233k | if (decode_parms.null()) { |
15 | 166k | return true; |
16 | 166k | } |
17 | | |
18 | 66.5k | auto memory_limit = Pl_Flate::memory_limit(); |
19 | | |
20 | 66.5k | std::set<std::string> keys = decode_parms.getKeys(); |
21 | 162k | for (auto const& key: keys) { |
22 | 162k | QPDFObjectHandle value = decode_parms.getKey(key); |
23 | 162k | if (key == "/Predictor") { |
24 | 41.1k | if (value.isInteger()) { |
25 | 41.0k | predictor = value.getIntValueAsInt(); |
26 | 41.0k | if (!(predictor == 1 || predictor == 2 || (predictor >= 10 && predictor <= 15))) { |
27 | 985 | return false; |
28 | 985 | } |
29 | 41.0k | } else { |
30 | 150 | return false; |
31 | 150 | } |
32 | 121k | } else if (key == "/Columns" || key == "/Colors" || key == "/BitsPerComponent") { |
33 | 43.1k | if (value.isInteger()) { |
34 | 42.9k | int val = value.getIntValueAsInt(); |
35 | 42.9k | if (memory_limit && static_cast<unsigned int>(val) > memory_limit) { |
36 | 297 | QPDFLogger::defaultLogger()->warn( |
37 | 297 | "SF_FlateLzwDecode parameter exceeds PL_Flate memory limit\n"); |
38 | 297 | return false; |
39 | 297 | } |
40 | 42.7k | if (key == "/Columns") { |
41 | 26.1k | columns = val; |
42 | 26.1k | } else if (key == "/Colors") { |
43 | 8.03k | colors = val; |
44 | 8.56k | } else if (key == "/BitsPerComponent") { |
45 | 8.55k | bits_per_component = val; |
46 | 8.55k | } |
47 | 42.7k | } else { |
48 | 138 | return false; |
49 | 138 | } |
50 | 78.3k | } else if (lzw && (key == "/EarlyChange")) { |
51 | 2.21k | if (value.isInteger()) { |
52 | 2.12k | int earlychange = value.getIntValueAsInt(); |
53 | 2.12k | early_code_change = (earlychange == 1); |
54 | 2.12k | if (!(earlychange == 0 || earlychange == 1)) { |
55 | 592 | return false; |
56 | 592 | } |
57 | 2.12k | } else { |
58 | 86 | return false; |
59 | 86 | } |
60 | 2.21k | } |
61 | 162k | } |
62 | | |
63 | 64.2k | if (predictor > 1 && columns == 0) { |
64 | 97 | return false; |
65 | 97 | } |
66 | | |
67 | 64.1k | return true; |
68 | 64.2k | } |
69 | | |
70 | | Pipeline* |
71 | | SF_FlateLzwDecode::getDecodePipeline(Pipeline* next) |
72 | 229k | { |
73 | 229k | std::unique_ptr<Pipeline> pipeline; |
74 | 229k | if (predictor >= 10 && predictor <= 15) { |
75 | 26.6k | QTC::TC("qpdf", "SF_FlateLzwDecode PNG filter"); |
76 | 26.6k | pipeline = std::make_unique<Pl_PNGFilter>( |
77 | 26.6k | "png decode", |
78 | 26.6k | next, |
79 | 26.6k | Pl_PNGFilter::a_decode, |
80 | 26.6k | QIntC::to_uint(columns), |
81 | 26.6k | QIntC::to_uint(colors), |
82 | 26.6k | QIntC::to_uint(bits_per_component)); |
83 | 26.6k | next = pipeline.get(); |
84 | 26.6k | pipelines.push_back(std::move(pipeline)); |
85 | 202k | } else if (predictor == 2) { |
86 | 11.6k | QTC::TC("qpdf", "SF_FlateLzwDecode TIFF predictor"); |
87 | 11.6k | pipeline = std::make_unique<Pl_TIFFPredictor>( |
88 | 11.6k | "tiff decode", |
89 | 11.6k | next, |
90 | 11.6k | Pl_TIFFPredictor::a_decode, |
91 | 11.6k | QIntC::to_uint(columns), |
92 | 11.6k | QIntC::to_uint(colors), |
93 | 11.6k | QIntC::to_uint(bits_per_component)); |
94 | 11.6k | next = pipeline.get(); |
95 | 11.6k | pipelines.push_back(std::move(pipeline)); |
96 | 11.6k | } |
97 | | |
98 | 229k | if (lzw) { |
99 | 46.6k | pipeline = std::make_unique<Pl_LZWDecoder>("lzw decode", next, early_code_change); |
100 | 182k | } else { |
101 | 182k | pipeline = std::make_unique<Pl_Flate>("stream inflate", next, Pl_Flate::a_inflate); |
102 | 182k | } |
103 | 229k | next = pipeline.get(); |
104 | 229k | pipelines.push_back(std::move(pipeline)); |
105 | 229k | return next; |
106 | 229k | } |