1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright (C) 2012-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <exceptions/isc_assert.h>
#include <dns/master_lexer_inputsource.h>
#include <dns/master_lexer.h>

#include <istream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iostream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cerrno><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstring><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace dns {
namespace master_lexer_internal {

namespace { // unnamed namespace

std::string
createStreamName(const std::istream& input_stream) {
     std::stringstream ss;
     ss << "stream-" << &input_stream;
     return (ss.str());
}

size_t
getStreamSize(std::istream& is) {
    errno = 0;                  // see below
    is.seekg(0, std::ios_base::end);
    if (is.bad()) {
        // This means the istream has an integrity error.  It doesn't make
        // sense to continue from this point, so we treat it as a fatal error.
        isc_throw(InputSource::OpenError,
                  "failed to seek end of input source");
    } else if (is.fail() || errno != 0) {
        // This is an error specific to seekg().  There can be several
        // reasons, but the most likely cause in this context is that the
        // stream is associated with a special type of file such as a pipe.
        // In this case, it's more likely that other main operations of
        // the input source work fine, so we continue with just setting
        // the stream size to "unknown".
        //
        // (At least some versions of) Solaris + SunStudio shows deviant
        // behavior here: seekg() apparently calls lseek(2) internally, but
        // even if it fails it doesn't set the error bits of istream. That will
        // confuse the rest of this function, so, as a heuristic workaround
        // we check errno and handle any non 0 value as fail().
        is.clear();   // clear this error not to confuse later ops.
        return (MasterLexer::SOURCE_SIZE_UNKNOWN);
    }
    const std::streampos len = is.tellg();
    size_t ret = len;
    if (len == static_cast<std::streampos>(-1)) { // cast for some compilers
        if (!is.fail()) {
            // tellg() returns -1 if istream::fail() would be true, but it's
            // not guaranteed that it shouldn't be returned in other cases.
            // In fact, with the combination of SunStudio and stlport,
            // a stringstream created by the default constructor showed that
            // behavior.  We treat such cases as an unknown size.
            ret = MasterLexer::SOURCE_SIZE_UNKNOWN;
        } else {
            isc_throw(InputSource::OpenError, "failed to get input size");
        }
    }
    is.seekg(0, std::ios::beg);
    if (is.fail()) {
        isc_throw(InputSource::OpenError,
                  "failed to seek beginning of input source");
    }
    isc_throw_assert(len >= 0 || ret == MasterLexer::SOURCE_SIZE_UNKNOWN);
    return (ret);
}

} // end of unnamed namespace

// Explicit definition of class static constant.  The value is given in the
// declaration so it's not needed here.
const int InputSource::END_OF_STREAM;

InputSource::InputSource(std::istream& input_stream) :
    at_eof_(false),
    line_(1),
    saved_line_(line_),
    buffer_pos_(0),
    total_pos_(0),
    name_(createStreamName(input_stream)),
    input_(input_stream),
    input_size_(getStreamSize(input_)) {
}

namespace {
// A helper to initialize InputSource::input_ in the member initialization
// list.
std::istream&
openFileStream(std::ifstream& file_stream, const char* filename) {
    errno = 0;
    file_stream.open(filename);
    if (file_stream.fail()) {
        std::string error_txt("Error opening the input source file: ");
        error_txt += filename;
        if (errno != 0) {
            error_txt += "; possible cause: ";
            error_txt += std::strerror(errno);
        }
        isc_throw(InputSource::OpenError, error_txt);
    }

    return (file_stream);
}
}

InputSource::InputSource(const char* filename) :
    at_eof_(false),
    line_(1),
    saved_line_(line_),
    buffer_pos_(0),
    total_pos_(0),
    name_(filename),
    input_(openFileStream(file_stream_, filename)),
    input_size_(getStreamSize(input_)) {
}

InputSource::~InputSource() {
    if (file_stream_.is_open()) {
        file_stream_.close();
    }
}

int
InputSource::getChar() {
    if (buffer_pos_ == buffer_.size()) {
        // We may have reached EOF at the last call to
        // getChar(). at_eof_ will be set then. We then simply return
        // early.
        if (at_eof_) {
            return (END_OF_STREAM);
        }
        // We are not yet at EOF. Read from the stream.
        const int c = input_.get();
        // Have we reached EOF now? If so, set at_eof_ and return early,
        // but don't modify buffer_pos_ (which should still be equal to
        // the size of buffer_).
        if (input_.eof()) {
            at_eof_ = true;
            return (END_OF_STREAM);
        }
        // This has to come after the .eof() check as some
        // implementations seem to check the eofbit also in .fail().
        if (input_.fail()) {
            isc_throw(MasterLexer::ReadError,
                      "Error reading from the input stream: " << getName());
        }
        buffer_.push_back(c);
    }

    const int c = buffer_[buffer_pos_];
    ++buffer_pos_;
    ++total_pos_;
    if (c == '\n') {
        ++line_;
    }

    return (c);
}

void
InputSource::ungetChar() {
    if (at_eof_) {
        at_eof_ = false;
    } else if (buffer_pos_ == 0) {
        isc_throw(UngetBeforeBeginning,
                  "Cannot skip before the start of buffer");
    } else {
        --buffer_pos_;
        --total_pos_;
        if (buffer_[buffer_pos_] == '\n') {
            --line_;
        }
    }
}

void
InputSource::ungetAll() {
    isc_throw_assert(total_pos_ >= buffer_pos_);
    total_pos_ -= buffer_pos_;
    buffer_pos_ = 0;
    line_ = saved_line_;
    at_eof_ = false;
}

void
InputSource::saveLine() {
    saved_line_ = line_;
}

void
InputSource::compact() {
    if (buffer_pos_ == buffer_.size()) {
        buffer_.clear();
    } else {
        buffer_.erase(buffer_.begin(), buffer_.begin() + buffer_pos_);
    }

    buffer_pos_ = 0;
}

void
InputSource::mark() {
    saveLine();
    compact();
}

} // namespace master_lexer_internal
} // namespace dns
} // namespace isc