1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : #include "syzygy/pdb/pdb_stream_reader.h"
15 :
16 :
17 : namespace pdb {
18 :
19 : PdbStreamReaderWithPosition::PdbStreamReaderWithPosition(PdbStream* stream)
20 E : : start_offset_(0), pos_(0), length_(stream->length()), stream_(stream) {
21 E : DCHECK_NE(static_cast<PdbStream*>(nullptr), stream_);
22 E : }
23 :
24 : PdbStreamReaderWithPosition::PdbStreamReaderWithPosition(size_t start_offset,
25 : size_t len,
26 : PdbStream* stream)
27 E : : start_offset_(start_offset), pos_(0), length_(len), stream_(stream) {
28 E : DCHECK_NE(static_cast<PdbStream*>(nullptr), stream_);
29 E : DCHECK_GE(stream_->length(), start_offset_ + length_);
30 E : DCHECK_LE(start_offset_, start_offset_ + length_);
31 E : }
32 :
33 : PdbStreamReaderWithPosition::PdbStreamReaderWithPosition()
34 E : : start_offset_(0), pos_(0), length_(0), stream_(nullptr) {
35 E : }
36 :
37 : void PdbStreamReaderWithPosition::SetStream(size_t start_offset,
38 : size_t len,
39 E : PdbStream* stream) {
40 E : DCHECK_EQ(static_cast<PdbStream*>(nullptr), stream_.get());
41 E : DCHECK_NE(static_cast<PdbStream*>(nullptr), stream);
42 E : DCHECK_GE(stream->length(), start_offset + len);
43 :
44 E : start_offset_ = start_offset;
45 E : length_ = len;
46 E : stream_ = stream;
47 E : }
48 :
49 E : bool PdbStreamReaderWithPosition::Read(size_t len, void* out) {
50 E : DCHECK(stream_);
51 E : if (pos_ + len > length_)
52 E : return false;
53 :
54 E : if (!stream_->ReadBytesAt(start_offset_ + pos_, len, out))
55 i : return false;
56 :
57 E : pos_ += len;
58 E : DCHECK_LE(pos_, length_);
59 :
60 E : return true;
61 E : }
62 :
63 E : size_t PdbStreamReaderWithPosition::Position() const {
64 E : DCHECK(stream_);
65 E : return pos_;
66 E : }
67 :
68 E : bool PdbStreamReaderWithPosition::AtEnd() const {
69 E : DCHECK(stream_);
70 E : DCHECK_LE(pos_, length_);
71 E : return pos_ == length_;
72 E : }
73 :
74 E : bool PdbStreamReaderWithPosition::Consume(size_t len) {
75 E : DCHECK(stream_);
76 E : if (pos_ + len > length_)
77 E : return false;
78 :
79 E : pos_ += len;
80 E : DCHECK_LE(pos_, length_);
81 :
82 E : return true;
83 E : }
84 :
85 : } // namespace pdb
|