Coverage Report

Created: 2025-12-05 06:54

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
19.7k
{
14
19.7k
    if (decode_parms.null()) {
15
16.1k
        return true;
16
16.1k
    }
17
18
3.66k
    auto memory_limit = Pl_Flate::memory_limit();
19
20
3.66k
    std::set<std::string> keys = decode_parms.getKeys();
21
9.80k
    for (auto const& key: keys) {
22
9.80k
        QPDFObjectHandle value = decode_parms.getKey(key);
23
9.80k
        if (key == "/Predictor") {
24
2.07k
            if (value.isInteger()) {
25
2.06k
                predictor = value.getIntValueAsInt();
26
2.06k
                if (!(predictor == 1 || predictor == 2 || (predictor >= 10 && predictor <= 15))) {
27
78
                    return false;
28
78
                }
29
2.06k
            } else {
30
10
                return false;
31
10
            }
32
7.73k
        } else if (key == "/Columns" || key == "/Colors" || key == "/BitsPerComponent") {
33
2.22k
            if (value.isInteger()) {
34
2.20k
                int val = value.getIntValueAsInt();
35
2.20k
                if (memory_limit && static_cast<unsigned int>(val) > memory_limit) {
36
39
                    QPDFLogger::defaultLogger()->warn(
37
39
                        "SF_FlateLzwDecode parameter exceeds PL_Flate memory limit\n");
38
39
                    return false;
39
39
                }
40
2.17k
                if (key == "/Columns") {
41
1.35k
                    columns = val;
42
1.35k
                } else if (key == "/Colors") {
43
455
                    colors = val;
44
455
                } else if (key == "/BitsPerComponent") {
45
359
                    bits_per_component = val;
46
359
                }
47
2.17k
            } else {
48
14
                return false;
49
14
            }
50
5.50k
        } else if (lzw && (key == "/EarlyChange")) {
51
168
            if (value.isInteger()) {
52
165
                int earlychange = value.getIntValueAsInt();
53
165
                early_code_change = (earlychange == 1);
54
165
                if (!(earlychange == 0 || earlychange == 1)) {
55
31
                    return false;
56
31
                }
57
165
            } else {
58
3
                return false;
59
3
            }
60
168
        }
61
9.80k
    }
62
63
3.49k
    if (predictor > 1 && columns == 0) {
64
3
        return false;
65
3
    }
66
67
3.48k
    return true;
68
3.49k
}
69
70
Pipeline*
71
SF_FlateLzwDecode::getDecodePipeline(Pipeline* next)
72
19.5k
{
73
19.5k
    std::unique_ptr<Pipeline> pipeline;
74
19.5k
    if (predictor >= 10 && predictor <= 15) {
75
1.39k
        QTC::TC("qpdf", "SF_FlateLzwDecode PNG filter");
76
1.39k
        pipeline = std::make_unique<Pl_PNGFilter>(
77
1.39k
            "png decode",
78
1.39k
            next,
79
1.39k
            Pl_PNGFilter::a_decode,
80
1.39k
            QIntC::to_uint(columns),
81
1.39k
            QIntC::to_uint(colors),
82
1.39k
            QIntC::to_uint(bits_per_component));
83
1.39k
        next = pipeline.get();
84
1.39k
        pipelines.push_back(std::move(pipeline));
85
18.2k
    } else if (predictor == 2) {
86
492
        QTC::TC("qpdf", "SF_FlateLzwDecode TIFF predictor");
87
492
        pipeline = std::make_unique<Pl_TIFFPredictor>(
88
492
            "tiff decode",
89
492
            next,
90
492
            Pl_TIFFPredictor::a_decode,
91
492
            QIntC::to_uint(columns),
92
492
            QIntC::to_uint(colors),
93
492
            QIntC::to_uint(bits_per_component));
94
492
        next = pipeline.get();
95
492
        pipelines.push_back(std::move(pipeline));
96
492
    }
97
98
19.5k
    if (lzw) {
99
1.99k
        pipeline = std::make_unique<Pl_LZWDecoder>("lzw decode", next, early_code_change);
100
17.6k
    } else {
101
17.6k
        pipeline = std::make_unique<Pl_Flate>("stream inflate", next, Pl_Flate::a_inflate);
102
17.6k
    }
103
19.5k
    next = pipeline.get();
104
19.5k
    pipelines.push_back(std::move(pipeline));
105
19.5k
    return next;
106
19.5k
}