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 the given |data| and |len|.
45 Input(const uint8_t* data
, size_t len
);
47 // Creates an Input from the given string |s|.
48 explicit Input(const std::string
& s
);
50 // Returns the length in bytes of an Input's data.
51 size_t Length() const { return len_
; }
53 // Returns a pointer to the Input's data. This method is marked as "unsafe"
54 // because access to the Input's data should be done through ByteReader
55 // instead. This method should only be used where using a ByteReader truly
57 const uint8_t* UnsafeData() const { return data_
; }
64 // This class provides ways to read data from an Input in a bounds-checked way.
65 // The ByteReader is designed to read through the input sequentially. Once a
66 // byte has been read with a ByteReader, the caller can't go back and re-read
67 // that byte with the same reader. Of course, the caller can create multiple
68 // ByteReaders for the same input (or copy an existing ByteReader).
70 // For something simple like a single byte lookahead, the easiest way to do
71 // that is to copy the ByteReader and call ReadByte() on the copy - the original
72 // ByteReader will be unaffected and the peeked byte will be read through
73 // ReadByte(). For other read patterns, it can be useful to mark where one is
74 // in a ByteReader to be able to return to that spot.
76 // Some operations using Mark can also be done by creating a copy of the
77 // ByteReader. By using a Mark instead, you use less memory, but more
78 // importantly, you end up with an immutable object that matches the semantics
79 // of what is intended.
80 class NET_EXPORT_PRIVATE ByteReader
{
82 // Creates a ByteReader to read the data represented by an Input.
83 explicit ByteReader(const Input
& in
);
85 // Reads a single byte from the input source, putting the byte read in
86 // |*byte_p|. If a byte cannot be read from the input (because there is
87 // no input left), then this method returns false.
88 bool ReadByte(uint8_t* out
) WARN_UNUSED_RESULT
;
90 // Reads |len| bytes from the input source, and initializes an Input to
91 // point to that data. If there aren't enough bytes left in the input source,
92 // then this method returns false.
93 bool ReadBytes(size_t len
, Input
* out
) WARN_UNUSED_RESULT
;
95 // Returns how many bytes are left to read.
96 size_t BytesLeft() const { return len_
; }
98 // Returns whether there is any more data to be read.
101 // Creates a new Mark at the current position of this ByteReader. If another
102 // ByteReader is advanced to this mark, the next byte read by the other
103 // ByteReader will be the same as the next byte read by this ByteReader.
106 // Advances this ByteReader to the position marked by |mark|. Note that
107 // a ByteReader can only advance forward - it is not possible to "rewind"
108 // to a previous mark. To do this, one would need to create a new ByteReader
109 // (from the same input) and AdvanceToMark() on the new ByteReader.
111 // If it is not possible to advance this ByteReader to the mark, this method
112 // returns false and does nothing.
113 bool AdvanceToMark(Mark mark
) WARN_UNUSED_RESULT
;
115 // Reads all data from the cursor of this ByteReader up to the mark, and
116 // initializes an Input to point to that data. If the Mark is not valid for
117 // this ByteReader, then this method returns false and does not modify |*out|.
118 bool ReadToMark(Mark mark
, Input
* out
) WARN_UNUSED_RESULT
;
121 void Advance(size_t len
);
123 const uint8_t* data_
;
127 // An immutable opaque pointer into the data represented by an Input. A Mark
128 // object is used to save and restore the state (position) of a ByteReader to
129 // allow for lookahead or backtracking. All interaction with a Mark object is
130 // done through the ByteReader class.
131 class NET_EXPORT_PRIVATE Mark
{
133 // Creates a null Mark. This Mark will not be usable with any ByteReader.
134 // This only exists so that a class can have a Mark member which may or may
135 // not be a valid Mark at any given time.
136 static Mark
NullMark();
138 // Checks whether a given Mark is an empty Mark.
140 friend class ByteReader
;
143 explicit Mark(const uint8_t* ptr
);
152 #endif // NET_DER_INPUT_H_