Coverage Report

Created: 2026-10-17 06:58

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