Coverage Report

Created: 2026-03-07 06:25

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