Coverage Report

Created: 2025-08-28 06:33

/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/QTC.hh>
9
10
bool
11
SF_FlateLzwDecode::setDecodeParms(QPDFObjectHandle decode_parms)
12
19.8k
{
13
19.8k
    if (decode_parms.isNull()) {
14
13.3k
        return true;
15
13.3k
    }
16
17
6.45k
    auto memory_limit = Pl_Flate::memory_limit();
18
19
6.45k
    std::set<std::string> keys = decode_parms.getKeys();
20
14.1k
    for (auto const& key: keys) {
21
14.1k
        QPDFObjectHandle value = decode_parms.getKey(key);
22
14.1k
        if (key == "/Predictor") {
23
3.76k
            if (value.isInteger()) {
24
3.75k
                predictor = value.getIntValueAsInt();
25
3.75k
                if (!(predictor == 1 || predictor == 2 || (predictor >= 10 && predictor <= 15))) {
26
100
                    return false;
27
100
                }
28
3.75k
            } else {
29
15
                return false;
30
15
            }
31
10.3k
        } else if (key == "/Columns" || key == "/Colors" || key == "/BitsPerComponent") {
32
3.31k
            if (value.isInteger()) {
33
3.29k
                int val = value.getIntValueAsInt();
34
3.29k
                if (memory_limit && static_cast<unsigned int>(val) > memory_limit) {
35
10
                    QPDFLogger::defaultLogger()->warn(
36
10
                        "SF_FlateLzwDecode parameter exceeds PL_Flate memory limit\n");
37
10
                    return false;
38
10
                }
39
3.28k
                if (key == "/Columns") {
40
1.60k
                    columns = val;
41
1.67k
                } else if (key == "/Colors") {
42
1.04k
                    colors = val;
43
1.04k
                } else if (key == "/BitsPerComponent") {
44
633
                    bits_per_component = val;
45
633
                }
46
3.28k
            } else {
47
14
                return false;
48
14
            }
49
7.04k
        } else if (lzw && (key == "/EarlyChange")) {
50
637
            if (value.isInteger()) {
51
634
                int earlychange = value.getIntValueAsInt();
52
634
                early_code_change = (earlychange == 1);
53
634
                if (!(earlychange == 0 || earlychange == 1)) {
54
46
                    return false;
55
46
                }
56
634
            } else {
57
3
                return false;
58
3
            }
59
637
        }
60
14.1k
    }
61
62
6.26k
    if (predictor > 1 && columns == 0) {
63
3
        return false;
64
3
    }
65
66
6.26k
    return true;
67
6.26k
}
68
69
Pipeline*
70
SF_FlateLzwDecode::getDecodePipeline(Pipeline* next)
71
19.5k
{
72
19.5k
    std::unique_ptr<Pipeline> pipeline;
73
19.5k
    if (predictor >= 10 && predictor <= 15) {
74
2.21k
        QTC::TC("qpdf", "SF_FlateLzwDecode PNG filter");
75
2.21k
        pipeline = std::make_unique<Pl_PNGFilter>(
76
2.21k
            "png decode",
77
2.21k
            next,
78
2.21k
            Pl_PNGFilter::a_decode,
79
2.21k
            QIntC::to_uint(columns),
80
2.21k
            QIntC::to_uint(colors),
81
2.21k
            QIntC::to_uint(bits_per_component));
82
2.21k
        next = pipeline.get();
83
2.21k
        pipelines.push_back(std::move(pipeline));
84
17.3k
    } else if (predictor == 2) {
85
1.18k
        QTC::TC("qpdf", "SF_FlateLzwDecode TIFF predictor");
86
1.18k
        pipeline = std::make_unique<Pl_TIFFPredictor>(
87
1.18k
            "tiff decode",
88
1.18k
            next,
89
1.18k
            Pl_TIFFPredictor::a_decode,
90
1.18k
            QIntC::to_uint(columns),
91
1.18k
            QIntC::to_uint(colors),
92
1.18k
            QIntC::to_uint(bits_per_component));
93
1.18k
        next = pipeline.get();
94
1.18k
        pipelines.push_back(std::move(pipeline));
95
1.18k
    }
96
97
19.5k
    if (lzw) {
98
5.30k
        pipeline = std::make_unique<Pl_LZWDecoder>("lzw decode", next, early_code_change);
99
14.2k
    } else {
100
14.2k
        pipeline = std::make_unique<Pl_Flate>("stream inflate", next, Pl_Flate::a_inflate);
101
14.2k
    }
102
19.5k
    next = pipeline.get();
103
19.5k
    pipelines.push_back(std::move(pipeline));
104
19.5k
    return next;
105
19.5k
}