Coverage Report

Created: 2026-02-26 06:38

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