Use native radiobutton for Default Search Engine picker dialog.
[chromium-blink-merge.git] / net / der / input.h
blob5814375a2e0dc032b12d5b30217a8648b563a705
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_
8 #include <stdint.h>
9 #include <string>
11 #include "base/compiler_specific.h"
12 #include "net/base/net_export.h"
14 namespace net {
16 namespace der {
18 class Mark;
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
31 // ByteReaders.
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 {
40 public:
41 // Creates an empty Input, one from which no data can be read.
42 Input();
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 // Return true if the Input's data and |other|'s data are byte-wise equal.
54 bool Equals(const Input& other) const;
56 // Returns a pointer to the Input's data. This method is marked as "unsafe"
57 // because access to the Input's data should be done through ByteReader
58 // instead. This method should only be used where using a ByteReader truly
59 // is not an option.
60 const uint8_t* UnsafeData() const { return data_; }
62 private:
63 const uint8_t* data_;
64 size_t len_;
67 // This class provides ways to read data from an Input in a bounds-checked way.
68 // The ByteReader is designed to read through the input sequentially. Once a
69 // byte has been read with a ByteReader, the caller can't go back and re-read
70 // that byte with the same reader. Of course, the caller can create multiple
71 // ByteReaders for the same input (or copy an existing ByteReader).
73 // For something simple like a single byte lookahead, the easiest way to do
74 // that is to copy the ByteReader and call ReadByte() on the copy - the original
75 // ByteReader will be unaffected and the peeked byte will be read through
76 // ReadByte(). For other read patterns, it can be useful to mark where one is
77 // in a ByteReader to be able to return to that spot.
79 // Some operations using Mark can also be done by creating a copy of the
80 // ByteReader. By using a Mark instead, you use less memory, but more
81 // importantly, you end up with an immutable object that matches the semantics
82 // of what is intended.
83 class NET_EXPORT_PRIVATE ByteReader {
84 public:
85 // Creates a ByteReader to read the data represented by an Input.
86 explicit ByteReader(const Input& in);
88 // Reads a single byte from the input source, putting the byte read in
89 // |*byte_p|. If a byte cannot be read from the input (because there is
90 // no input left), then this method returns false.
91 bool ReadByte(uint8_t* out) WARN_UNUSED_RESULT;
93 // Reads |len| bytes from the input source, and initializes an Input to
94 // point to that data. If there aren't enough bytes left in the input source,
95 // then this method returns false.
96 bool ReadBytes(size_t len, Input* out) WARN_UNUSED_RESULT;
98 // Returns how many bytes are left to read.
99 size_t BytesLeft() const { return len_; }
101 // Returns whether there is any more data to be read.
102 bool HasMore();
104 // Creates a new Mark at the current position of this ByteReader. If another
105 // ByteReader is advanced to this mark, the next byte read by the other
106 // ByteReader will be the same as the next byte read by this ByteReader.
107 Mark NewMark();
109 // Advances this ByteReader to the position marked by |mark|. Note that
110 // a ByteReader can only advance forward - it is not possible to "rewind"
111 // to a previous mark. To do this, one would need to create a new ByteReader
112 // (from the same input) and AdvanceToMark() on the new ByteReader.
114 // If it is not possible to advance this ByteReader to the mark, this method
115 // returns false and does nothing.
116 bool AdvanceToMark(Mark mark) WARN_UNUSED_RESULT;
118 // Reads all data from the cursor of this ByteReader up to the mark, and
119 // initializes an Input to point to that data. If the Mark is not valid for
120 // this ByteReader, then this method returns false and does not modify |*out|.
121 bool ReadToMark(Mark mark, Input* out) WARN_UNUSED_RESULT;
123 private:
124 void Advance(size_t len);
126 const uint8_t* data_;
127 size_t len_;
130 // An immutable opaque pointer into the data represented by an Input. A Mark
131 // object is used to save and restore the state (position) of a ByteReader to
132 // allow for lookahead or backtracking. All interaction with a Mark object is
133 // done through the ByteReader class.
134 class NET_EXPORT_PRIVATE Mark {
135 public:
136 // Creates a null Mark. This Mark will not be usable with any ByteReader.
137 // This only exists so that a class can have a Mark member which may or may
138 // not be a valid Mark at any given time.
139 static Mark NullMark();
141 // Checks whether a given Mark is an empty Mark.
142 bool IsEmpty();
143 friend class ByteReader;
145 private:
146 explicit Mark(const uint8_t* ptr);
147 Mark();
148 const uint8_t* ptr_;
151 } // namespace der
153 } // namespace net
155 #endif // NET_DER_INPUT_H_