Coverage Report

Created: 2025-10-12 07:05

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