// Copyright 2015 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef BSSL_DER_INPUT_H_ #define BSSL_DER_INPUT_H_ #include <stddef.h> #include <stdint.h> #include <string> #include <string_view> #include <openssl/base.h> #include <openssl/span.h> #if defined(__has_include) #if __has_include(<version>) #include <version> #endif #endif #if defined(__cpp_lib_ranges) && __cpp_lib_ranges >= 201911L #include <ranges> BSSL_NAMESPACE_BEGIN namespace der { class OPENSSL_EXPORT Input; } BSSL_NAMESPACE_END // Mark `Input` as satisfying the `view` and `borrowed_range` concepts. This // should be done before the definition of `Input`, so that any inlined calls to // range functionality use the correct specializations. enable_view; enable_borrowed_range; #endif BSSL_NAMESPACE_BEGIN namespace der { // An opaque class that represents a fixed buffer of data of a fixed length, // to be used as an input to other operations. An Input object does not own // the data it references, so callers are responsible for making sure that // the data outlives the Input object and any other associated objects. // // All data access for an Input should be done through the ByteReader class. // This class and associated classes are designed with safety in mind to make it // difficult to read memory outside of an Input. ByteReader provides a simple // API for reading through the Input sequentially. For more complicated uses, // multiple instances of a ByteReader for a particular Input can be created. // // TODO(crbug.com/boringssl/661): This class will gradually be replaced with // bssl::Span<const uint8_t>. Avoid relying on APIs that are not part of // bssl::Span. class OPENSSL_EXPORT Input { … }; // Return true if |lhs|'s data and |rhs|'s data are byte-wise equal. OPENSSL_EXPORT bool operator==(Input lhs, Input rhs); // Return true if |lhs|'s data and |rhs|'s data are not byte-wise equal. OPENSSL_EXPORT bool operator!=(Input lhs, Input rhs); // Returns true if |lhs|'s data is lexicographically less than |rhs|'s data. OPENSSL_EXPORT constexpr bool operator<(Input lhs, Input rhs) { … } // This class provides ways to read data from an Input in a bounds-checked way. // The ByteReader is designed to read through the input sequentially. Once a // byte has been read with a ByteReader, the caller can't go back and re-read // that byte with the same reader. Of course, the caller can create multiple // ByteReaders for the same input (or copy an existing ByteReader). // // For something simple like a single byte lookahead, the easiest way to do // that is to copy the ByteReader and call ReadByte() on the copy - the original // ByteReader will be unaffected and the peeked byte will be read through // ReadByte(). For other read patterns, it can be useful to mark where one is // in a ByteReader to be able to return to that spot. // // Some operations using Mark can also be done by creating a copy of the // ByteReader. By using a Mark instead, you use less memory, but more // importantly, you end up with an immutable object that matches the semantics // of what is intended. class OPENSSL_EXPORT ByteReader { … }; } // namespace der BSSL_NAMESPACE_END #endif // BSSL_DER_INPUT_H_