Coverage Report

Created: 2025-07-14 06:14

/src/qpdf/include/qpdf/InputSource.hh
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2005-2021 Jay Berkenbilt
2
// Copyright (c) 2022-2025 Jay Berkenbilt and Manfred Holger
3
//
4
// This file is part of qpdf.
5
//
6
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7
// in compliance with the License. You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software distributed under the License
12
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13
// or implied. See the License for the specific language governing permissions and limitations under
14
// the License.
15
//
16
// Versions of qpdf prior to version 7 were released under the terms of version 2.0 of the Artistic
17
// License. At your option, you may continue to consider qpdf to be licensed under those terms.
18
// Please see the manual for additional information.
19
20
#ifndef QPDF_INPUTSOURCE_HH
21
#define QPDF_INPUTSOURCE_HH
22
23
#include <qpdf/DLL.h>
24
#include <qpdf/Types.h>
25
26
#include <cstdio>
27
#include <memory>
28
#include <string>
29
30
// Remember to use QPDF_DLL_CLASS on anything derived from InputSource so it will work with
31
// dynamic_cast across the shared object boundary.
32
class QPDF_DLL_CLASS InputSource
33
{
34
  public:
35
41.9k
    InputSource() = default;
36
37
41.9k
    virtual ~InputSource() = default;
38
39
    class QPDF_DLL_CLASS Finder
40
    {
41
      public:
42
        QPDF_DLL
43
14.8k
        Finder() = default;
44
        QPDF_DLL
45
0
        virtual ~Finder() = default;
46
        virtual bool check() = 0;
47
    };
48
49
    QPDF_DLL
50
    void setLastOffset(qpdf_offset_t);
51
    QPDF_DLL
52
    qpdf_offset_t getLastOffset() const;
53
    QPDF_DLL
54
    std::string readLine(size_t max_line_length);
55
56
    // Find first or last occurrence of a sequence of characters starting within the range defined
57
    // by offset and len such that, when the input source is positioned at the beginning of that
58
    // sequence, finder.check() returns true. If len is 0, the search proceeds until EOF. If a
59
    // qualifying pattern is found, these methods return true and leave the input source positioned
60
    // wherever check() left it at the end of the matching pattern.
61
    QPDF_DLL
62
    bool findFirst(char const* start_chars, qpdf_offset_t offset, size_t len, Finder& finder);
63
    QPDF_DLL
64
    bool findLast(char const* start_chars, qpdf_offset_t offset, size_t len, Finder& finder);
65
66
    virtual qpdf_offset_t findAndSkipNextEOL() = 0;
67
    virtual std::string const& getName() const = 0;
68
    virtual qpdf_offset_t tell() = 0;
69
    virtual void seek(qpdf_offset_t offset, int whence) = 0;
70
    virtual void rewind() = 0;
71
    virtual size_t read(char* buffer, size_t length) = 0;
72
73
    // Note: you can only unread the character you just read. The specific character is ignored by
74
    // some implementations, and the implementation doesn't check this. Use of unreadCh is
75
    // semantically equivalent to seek(-1, SEEK_CUR) but is much more efficient.
76
    virtual void unreadCh(char ch) = 0;
77
78
    // The following methods are for internal use by qpdf only.
79
    inline size_t read(std::string& str, size_t count, qpdf_offset_t at = -1);
80
    inline std::string read(size_t count, qpdf_offset_t at = -1);
81
    size_t read_line(std::string& str, size_t count, qpdf_offset_t at = -1);
82
    std::string read_line(size_t count, qpdf_offset_t at = -1);
83
    inline qpdf_offset_t fastTell();
84
    inline bool fastRead(char&);
85
    inline void fastUnread(bool);
86
    inline void loadBuffer();
87
88
  protected:
89
    qpdf_offset_t last_offset{0};
90
91
  private:
92
    // State for fast... methods
93
    static const qpdf_offset_t buf_size = 128;
94
    char buffer[buf_size];
95
    qpdf_offset_t buf_len = 0;
96
    qpdf_offset_t buf_idx = 0;
97
    qpdf_offset_t buf_start = 0;
98
};
99
100
#endif // QPDF_INPUTSOURCE_HH