Coverage Report

Created: 2025-08-26 07:13

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