blob: a0d8af617a898fc5e9bb81ea54bff9b2e46ad1c1 [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 Beck5c7a2a02023-11-20 17:28:21 -070014 return std::string(reinterpret_cast<const char *>(data_.data()),
15 data_.size());
Bob Beckbc97b7a2023-04-18 08:35:15 -060016}
17
18std::string_view Input::AsStringView() const {
Bob Beck5c7a2a02023-11-20 17:28:21 -070019 return std::string_view(reinterpret_cast<const char *>(data_.data()),
Bob Beck2e119172023-08-14 11:06:38 -060020 data_.size());
Bob Beckbc97b7a2023-04-18 08:35:15 -060021}
22
Bob Beck5c7a2a02023-11-20 17:28:21 -070023bssl::Span<const uint8_t> Input::AsSpan() const { return data_; }
Bob Beckbc97b7a2023-04-18 08:35:15 -060024
Bob Beck5c7a2a02023-11-20 17:28:21 -070025bool operator==(const Input &lhs, const Input &rhs) {
Bob Beckbc97b7a2023-04-18 08:35:15 -060026 return lhs.Length() == rhs.Length() &&
27 std::equal(lhs.UnsafeData(), lhs.UnsafeData() + lhs.Length(),
28 rhs.UnsafeData());
29}
30
Bob Beck5c7a2a02023-11-20 17:28:21 -070031bool operator!=(const Input &lhs, const Input &rhs) { return !(lhs == rhs); }
Bob Beckbc97b7a2023-04-18 08:35:15 -060032
Bob Beck5c7a2a02023-11-20 17:28:21 -070033ByteReader::ByteReader(const Input &in)
34 : data_(in.UnsafeData()), len_(in.Length()) {}
Bob Beckbc97b7a2023-04-18 08:35:15 -060035
Bob Beck5c7a2a02023-11-20 17:28:21 -070036bool ByteReader::ReadByte(uint8_t *byte_p) {
Bob Beck6beabf32023-11-21 09:43:52 -070037 if (!HasMore()) {
Bob Beckbc97b7a2023-04-18 08:35:15 -060038 return false;
Bob Beck6beabf32023-11-21 09:43:52 -070039 }
Bob Beckbc97b7a2023-04-18 08:35:15 -060040 *byte_p = *data_;
41 Advance(1);
42 return true;
43}
44
Bob Beck5c7a2a02023-11-20 17:28:21 -070045bool ByteReader::ReadBytes(size_t len, Input *out) {
Bob Beck6beabf32023-11-21 09:43:52 -070046 if (len > len_) {
Bob Beckbc97b7a2023-04-18 08:35:15 -060047 return false;
Bob Beck6beabf32023-11-21 09:43:52 -070048 }
Bob Beckbc97b7a2023-04-18 08:35:15 -060049 *out = Input(data_, len);
50 Advance(len);
51 return true;
52}
53
54// Returns whether there is any more data to be read.
Bob Beck5c7a2a02023-11-20 17:28:21 -070055bool ByteReader::HasMore() { return len_ > 0; }
Bob Beckbc97b7a2023-04-18 08:35:15 -060056
57void ByteReader::Advance(size_t len) {
Bob Beck05007562023-08-17 20:22:17 +000058 BSSL_CHECK(len <= len_);
Bob Beckbc97b7a2023-04-18 08:35:15 -060059 data_ += len;
60 len_ -= len;
61}
62
63} // namespace bssl::der