Coverage Report

Created: 2025-07-11 06:58

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