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