blob: 884c6bc8e5158022844acb3c6b672b80ac392f98 [file] [log] [blame]
Bob Beckbc97b7a2023-04-18 08:35:15 -06001// 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
Bob Beck05007562023-08-17 20:22:17 +00009#include <openssl/base.h>
Bob Beckbc97b7a2023-04-18 08:35:15 -060010
11namespace bssl::der {
12
Bob Beckbc97b7a2023-04-18 08:35:15 -060013std::string Input::AsString() const {
Bob Beck2e119172023-08-14 11:06:38 -060014 return std::string(reinterpret_cast<const char*>(data_.data()), data_.size());
Bob Beckbc97b7a2023-04-18 08:35:15 -060015}
16
17std::string_view Input::AsStringView() const {
Bob Beck2e119172023-08-14 11:06:38 -060018 return std::string_view(reinterpret_cast<const char*>(data_.data()),
19 data_.size());
Bob Beckbc97b7a2023-04-18 08:35:15 -060020}
21
22bssl::Span<const uint8_t> Input::AsSpan() const {
Bob Beck2e119172023-08-14 11:06:38 -060023 return data_;
Bob Beckbc97b7a2023-04-18 08:35:15 -060024}
25
26bool 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
32bool operator!=(const Input& lhs, const Input& rhs) {
33 return !(lhs == rhs);
34}
35
36ByteReader::ByteReader(const Input& in)
37 : data_(in.UnsafeData()), len_(in.Length()) {
38}
39
40bool ByteReader::ReadByte(uint8_t* byte_p) {
41 if (!HasMore())
42 return false;
43 *byte_p = *data_;
44 Advance(1);
45 return true;
46}
47
48bool 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.
57bool ByteReader::HasMore() {
58 return len_ > 0;
59}
60
61void ByteReader::Advance(size_t len) {
Bob Beck05007562023-08-17 20:22:17 +000062 BSSL_CHECK(len <= len_);
Bob Beckbc97b7a2023-04-18 08:35:15 -060063 data_ += len;
64 len_ -= len;
65}
66
67} // namespace bssl::der