Coverage Report

Created: 2026-03-07 06:24

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