1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_DER_INPUT_H_
6 #define NET_DER_INPUT_H_
11 #include "base/compiler_specific.h"
12 #include "net/base/net_export.h"
20 // An opaque class that represents a fixed buffer of data of a fixed length,
21 // to be used as an input to other operations. An Input object does not own
22 // the data it references, so callers are responsible for making sure that
23 // the data outlives the Input object and any other associated objects.
25 // All data access for an Input should be done through the ByteReader and Mark
26 // classes. This class and associated classes are designed with safety in mind
27 // to make it difficult to read memory outside of an Input. ByteReader provides
28 // a simple API for reading through the Input sequentially. For more
29 // complicated uses, multiple instances of a ByteReader for a particular Input
30 // can be created, and instances of Mark can be used to coordinate between the
33 // One such use case of multiple ByteReaders would be looking ahead in an
34 // input: A ByteReader is copied and then is used to read some number of
35 // bytes into the input, based on the content it is reading. A Mark can then be
36 // set using the temporary ByteReader to indicate how far it read into the
37 // Input. The original ByteReader can then be synchronized with how far the
38 // temporary ByteReader read, by using either AdvanceToMark() or ReadToMark().
39 class NET_EXPORT_PRIVATE Input
{
41 // Creates an empty Input, one from which no data can be read.
44 // Creates an Input from a constant array |data|.
46 explicit Input(const uint8_t(&data
)[N
])
47 : data_(data
), len_(N
) {}
49 // Creates an Input from the given |data| and |len|.
50 Input(const uint8_t* data
, size_t len
);
52 // Returns the length in bytes of an Input's data.
53 size_t Length() const { return len_
; }
55 // Return true if the Input's data and |other|'s data are byte-wise equal.
56 bool Equals(const Input
& other
) const;
58 // Returns a pointer to the Input's data. This method is marked as "unsafe"
59 // because access to the Input's data should be done through ByteReader
60 // instead. This method should only be used where using a ByteReader truly
62 const uint8_t* UnsafeData() const { return data_
; }
64 // Returns a copy of the data represented by this object as a std::string.
65 std::string
AsString() const;
72 // This class provides ways to read data from an Input in a bounds-checked way.
73 // The ByteReader is designed to read through the input sequentially. Once a
74 // byte has been read with a ByteReader, the caller can't go back and re-read
75 // that byte with the same reader. Of course, the caller can create multiple
76 // ByteReaders for the same input (or copy an existing ByteReader).
78 // For something simple like a single byte lookahead, the easiest way to do
79 // that is to copy the ByteReader and call ReadByte() on the copy - the original
80 // ByteReader will be unaffected and the peeked byte will be read through
81 // ReadByte(). For other read patterns, it can be useful to mark where one is
82 // in a ByteReader to be able to return to that spot.
84 // Some operations using Mark can also be done by creating a copy of the
85 // ByteReader. By using a Mark instead, you use less memory, but more
86 // importantly, you end up with an immutable object that matches the semantics
87 // of what is intended.
88 class NET_EXPORT_PRIVATE ByteReader
{
90 // Creates a ByteReader to read the data represented by an Input.
91 explicit ByteReader(const Input
& in
);
93 // Reads a single byte from the input source, putting the byte read in
94 // |*byte_p|. If a byte cannot be read from the input (because there is
95 // no input left), then this method returns false.
96 bool ReadByte(uint8_t* out
) WARN_UNUSED_RESULT
;
98 // Reads |len| bytes from the input source, and initializes an Input to
99 // point to that data. If there aren't enough bytes left in the input source,
100 // then this method returns false.
101 bool ReadBytes(size_t len
, Input
* out
) WARN_UNUSED_RESULT
;
103 // Returns how many bytes are left to read.
104 size_t BytesLeft() const { return len_
; }
106 // Returns whether there is any more data to be read.
109 // Creates a new Mark at the current position of this ByteReader. If another
110 // ByteReader is advanced to this mark, the next byte read by the other
111 // ByteReader will be the same as the next byte read by this ByteReader.
114 // Advances this ByteReader to the position marked by |mark|. Note that
115 // a ByteReader can only advance forward - it is not possible to "rewind"
116 // to a previous mark. To do this, one would need to create a new ByteReader
117 // (from the same input) and AdvanceToMark() on the new ByteReader.
119 // If it is not possible to advance this ByteReader to the mark, this method
120 // returns false and does nothing.
121 bool AdvanceToMark(Mark mark
) WARN_UNUSED_RESULT
;
123 // Reads all data from the cursor of this ByteReader up to the mark, and
124 // initializes an Input to point to that data. If the Mark is not valid for
125 // this ByteReader, then this method returns false and does not modify |*out|.
126 bool ReadToMark(Mark mark
, Input
* out
) WARN_UNUSED_RESULT
;
129 void Advance(size_t len
);
131 const uint8_t* data_
;
135 // An immutable opaque pointer into the data represented by an Input. A Mark
136 // object is used to save and restore the state (position) of a ByteReader to
137 // allow for lookahead or backtracking. All interaction with a Mark object is
138 // done through the ByteReader class.
139 class NET_EXPORT_PRIVATE Mark
{
141 // Creates a null Mark. This Mark will not be usable with any ByteReader.
142 // This only exists so that a class can have a Mark member which may or may
143 // not be a valid Mark at any given time.
144 static Mark
NullMark();
146 // Checks whether a given Mark is an empty Mark.
148 friend class ByteReader
;
151 explicit Mark(const uint8_t* ptr
);
160 #endif // NET_DER_INPUT_H_