Coverage Report

Created: 2026-08-31 06:57

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