Coverage Report

Created: 2025-08-26 07:10

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