Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 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 | |
Bob Beck | 0500756 | 2023-08-17 20:22:17 +0000 | [diff] [blame^] | 9 | #include <openssl/base.h> |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 10 | |
| 11 | namespace bssl::der { |
| 12 | |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 13 | std::string Input::AsString() const { |
Bob Beck | 2e11917 | 2023-08-14 11:06:38 -0600 | [diff] [blame] | 14 | return std::string(reinterpret_cast<const char*>(data_.data()), data_.size()); |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | std::string_view Input::AsStringView() const { |
Bob Beck | 2e11917 | 2023-08-14 11:06:38 -0600 | [diff] [blame] | 18 | return std::string_view(reinterpret_cast<const char*>(data_.data()), |
| 19 | data_.size()); |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | bssl::Span<const uint8_t> Input::AsSpan() const { |
Bob Beck | 2e11917 | 2023-08-14 11:06:38 -0600 | [diff] [blame] | 23 | return data_; |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 24 | } |
| 25 | |
| 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 | |
| 32 | bool operator!=(const Input& lhs, const Input& rhs) { |
| 33 | return !(lhs == rhs); |
| 34 | } |
| 35 | |
| 36 | ByteReader::ByteReader(const Input& in) |
| 37 | : data_(in.UnsafeData()), len_(in.Length()) { |
| 38 | } |
| 39 | |
| 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 | |
| 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. |
| 57 | bool ByteReader::HasMore() { |
| 58 | return len_ > 0; |
| 59 | } |
| 60 | |
| 61 | void ByteReader::Advance(size_t len) { |
Bob Beck | 0500756 | 2023-08-17 20:22:17 +0000 | [diff] [blame^] | 62 | BSSL_CHECK(len <= len_); |
Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame] | 63 | data_ += len; |
| 64 | len_ -= len; |
| 65 | } |
| 66 | |
| 67 | } // namespace bssl::der |