1 // Copyright 2015 The Chromium Authors
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 // https://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
15 #include "input.h"
16
17 BSSL_NAMESPACE_BEGIN
18 namespace der {
19
AsString() const20 std::string Input::AsString() const { return std::string(AsStringView()); }
21
operator ==(Input lhs,Input rhs)22 bool operator==(Input lhs, Input rhs) { return Span(lhs) == Span(rhs); }
23
operator !=(Input lhs,Input rhs)24 bool operator!=(Input lhs, Input rhs) { return !(lhs == rhs); }
25
ByteReader(Input in)26 ByteReader::ByteReader(Input in) : data_(in) {}
27
ReadByte(uint8_t * byte_p)28 bool ByteReader::ReadByte(uint8_t *byte_p) {
29 if (!HasMore()) {
30 return false;
31 }
32 *byte_p = data_[0];
33 Advance(1);
34 return true;
35 }
36
ReadBytes(size_t len,Input * out)37 bool ByteReader::ReadBytes(size_t len, Input *out) {
38 if (len > data_.size()) {
39 return false;
40 }
41 *out = Input(data_.first(len));
42 Advance(len);
43 return true;
44 }
45
46 // Returns whether there is any more data to be read.
HasMore()47 bool ByteReader::HasMore() { return !data_.empty(); }
48
Advance(size_t len)49 void ByteReader::Advance(size_t len) {
50 BSSL_CHECK(len <= data_.size());
51 data_ = data_.subspan(len);
52 }
53
54 } // namespace der
55 BSSL_NAMESPACE_END
56