1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "input.h"
6
7 #include <algorithm>
8
9 #include <openssl/base.h>
10
11 namespace bssl::der {
12
AsString() const13 std::string Input::AsString() const {
14 return std::string(reinterpret_cast<const char*>(data_.data()), data_.size());
15 }
16
AsStringView() const17 std::string_view Input::AsStringView() const {
18 return std::string_view(reinterpret_cast<const char*>(data_.data()),
19 data_.size());
20 }
21
AsSpan() const22 bssl::Span<const uint8_t> Input::AsSpan() const {
23 return data_;
24 }
25
operator ==(const Input & lhs,const Input & rhs)26 bool operator==(const Input& lhs, const Input& rhs) {
27 return lhs.Length() == rhs.Length() &&
28 std::equal(lhs.UnsafeData(), lhs.UnsafeData() + lhs.Length(),
29 rhs.UnsafeData());
30 }
31
operator !=(const Input & lhs,const Input & rhs)32 bool operator!=(const Input& lhs, const Input& rhs) {
33 return !(lhs == rhs);
34 }
35
ByteReader(const Input & in)36 ByteReader::ByteReader(const Input& in)
37 : data_(in.UnsafeData()), len_(in.Length()) {
38 }
39
ReadByte(uint8_t * byte_p)40 bool ByteReader::ReadByte(uint8_t* byte_p) {
41 if (!HasMore())
42 return false;
43 *byte_p = *data_;
44 Advance(1);
45 return true;
46 }
47
ReadBytes(size_t len,Input * out)48 bool ByteReader::ReadBytes(size_t len, Input* out) {
49 if (len > len_)
50 return false;
51 *out = Input(data_, len);
52 Advance(len);
53 return true;
54 }
55
56 // Returns whether there is any more data to be read.
HasMore()57 bool ByteReader::HasMore() {
58 return len_ > 0;
59 }
60
Advance(size_t len)61 void ByteReader::Advance(size_t len) {
62 BSSL_CHECK(len <= len_);
63 data_ += len;
64 len_ -= len;
65 }
66
67 } // namespace bssl::der
68