/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 | 33.9k | { |
14 | 33.9k | if (decode_parms.null()) { |
15 | 5.75k | return true; |
16 | 5.75k | } |
17 | | |
18 | 28.2k | auto memory_limit = Pl_Flate::memory_limit(); |
19 | | |
20 | 28.2k | std::set<std::string> keys = decode_parms.getKeys(); |
21 | 37.3k | for (auto const& key: keys) { |
22 | 37.3k | QPDFObjectHandle value = decode_parms.getKey(key); |
23 | 37.3k | if (key == "/Predictor") { |
24 | 25.2k | if (value.isInteger()) { |
25 | 25.2k | predictor = value.getIntValueAsInt(); |
26 | 25.2k | if (!(predictor == 1 || predictor == 2 || (predictor >= 10 && predictor <= 15))) { |
27 | 64 | return false; |
28 | 64 | } |
29 | 25.2k | } else { |
30 | 3 | return false; |
31 | 3 | } |
32 | 25.2k | } else if (key == "/Columns" || key == "/Colors" || key == "/BitsPerComponent") { |
33 | 3.24k | if (value.isInteger()) { |
34 | 3.23k | int val = value.getIntValueAsInt(); |
35 | 3.23k | if (memory_limit && static_cast<unsigned int>(val) > memory_limit) { |
36 | 3 | QPDFLogger::defaultLogger()->warn( |
37 | 3 | "SF_FlateLzwDecode parameter exceeds PL_Flate memory limit\n"); |
38 | 3 | return false; |
39 | 3 | } |
40 | 3.23k | if (key == "/Columns") { |
41 | 1.68k | columns = val; |
42 | 1.68k | } else if (key == "/Colors") { |
43 | 615 | colors = val; |
44 | 935 | } else if (key == "/BitsPerComponent") { |
45 | 934 | bits_per_component = val; |
46 | 934 | } |
47 | 3.23k | } else { |
48 | 3 | return false; |
49 | 3 | } |
50 | 8.84k | } else if (lzw && (key == "/EarlyChange")) { |
51 | 429 | if (value.isInteger()) { |
52 | 426 | int earlychange = value.getIntValueAsInt(); |
53 | 426 | early_code_change = (earlychange == 1); |
54 | 426 | if (!(earlychange == 0 || earlychange == 1)) { |
55 | 28 | return false; |
56 | 28 | } |
57 | 426 | } else { |
58 | 3 | return false; |
59 | 3 | } |
60 | 429 | } |
61 | 37.3k | } |
62 | | |
63 | 28.1k | if (predictor > 1 && columns == 0) { |
64 | 3 | return false; |
65 | 3 | } |
66 | | |
67 | 28.1k | return true; |
68 | 28.1k | } |
69 | | |
70 | | Pipeline* |
71 | | SF_FlateLzwDecode::getDecodePipeline(Pipeline* next) |
72 | 33.8k | { |
73 | 33.8k | std::unique_ptr<Pipeline> pipeline; |
74 | 33.8k | if (predictor >= 10 && predictor <= 15) { |
75 | 23.7k | QTC::TC("qpdf", "SF_FlateLzwDecode PNG filter"); |
76 | 23.7k | pipeline = std::make_unique<Pl_PNGFilter>( |
77 | 23.7k | "png decode", |
78 | 23.7k | next, |
79 | 23.7k | Pl_PNGFilter::a_decode, |
80 | 23.7k | QIntC::to_uint(columns), |
81 | 23.7k | QIntC::to_uint(colors), |
82 | 23.7k | QIntC::to_uint(bits_per_component)); |
83 | 23.7k | next = pipeline.get(); |
84 | 23.7k | pipelines.push_back(std::move(pipeline)); |
85 | 23.7k | } else if (predictor == 2) { |
86 | 1.25k | QTC::TC("qpdf", "SF_FlateLzwDecode TIFF predictor"); |
87 | 1.25k | pipeline = std::make_unique<Pl_TIFFPredictor>( |
88 | 1.25k | "tiff decode", |
89 | 1.25k | next, |
90 | 1.25k | Pl_TIFFPredictor::a_decode, |
91 | 1.25k | QIntC::to_uint(columns), |
92 | 1.25k | QIntC::to_uint(colors), |
93 | 1.25k | QIntC::to_uint(bits_per_component)); |
94 | 1.25k | next = pipeline.get(); |
95 | 1.25k | pipelines.push_back(std::move(pipeline)); |
96 | 1.25k | } |
97 | | |
98 | 33.8k | if (lzw) { |
99 | 28.4k | pipeline = std::make_unique<Pl_LZWDecoder>("lzw decode", next, early_code_change); |
100 | 28.4k | } else { |
101 | 5.37k | pipeline = std::make_unique<Pl_Flate>("stream inflate", next, Pl_Flate::a_inflate); |
102 | 5.37k | } |
103 | 33.8k | next = pipeline.get(); |
104 | 33.8k | pipelines.push_back(std::move(pipeline)); |
105 | 33.8k | return next; |
106 | 33.8k | } |