Coverage Report

Created: 2025-08-03 06:15

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