OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_DER_INPUT_H_ | 5 #ifndef NET_DER_INPUT_H_ |
6 #define NET_DER_INPUT_H_ | 6 #define NET_DER_INPUT_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/strings/string_piece.h" |
14 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
15 | 16 |
16 namespace net { | 17 namespace net { |
17 | 18 |
18 namespace der { | 19 namespace der { |
19 | 20 |
20 class Mark; | 21 class Mark; |
21 | 22 |
22 // An opaque class that represents a fixed buffer of data of a fixed length, | 23 // An opaque class that represents a fixed buffer of data of a fixed length, |
23 // to be used as an input to other operations. An Input object does not own | 24 // to be used as an input to other operations. An Input object does not own |
(...skipping 18 matching lines...) Expand all Loading... |
42 public: | 43 public: |
43 // Creates an empty Input, one from which no data can be read. | 44 // Creates an empty Input, one from which no data can be read. |
44 Input(); | 45 Input(); |
45 | 46 |
46 // Creates an Input from a constant array |data|. | 47 // Creates an Input from a constant array |data|. |
47 template <size_t N> | 48 template <size_t N> |
48 explicit Input(const uint8_t(&data)[N]) | 49 explicit Input(const uint8_t(&data)[N]) |
49 : data_(data), len_(N) {} | 50 : data_(data), len_(N) {} |
50 | 51 |
51 // Creates an Input from the given |data| and |len|. | 52 // Creates an Input from the given |data| and |len|. |
52 Input(const uint8_t* data, size_t len); | 53 explicit Input(const uint8_t* data, size_t len); |
| 54 |
| 55 // Creates an Input from a base::StringPiece. |
| 56 explicit Input(const base::StringPiece& sp); |
| 57 |
| 58 // Creates an Input from a std::string. The lifetimes are a bit subtle when |
| 59 // using this function: The constructed Input is only valid so long as |s| is |
| 60 // still alive and not mutated. |
| 61 Input(const std::string* s); |
53 | 62 |
54 // Returns the length in bytes of an Input's data. | 63 // Returns the length in bytes of an Input's data. |
55 size_t Length() const { return len_; } | 64 size_t Length() const { return len_; } |
56 | 65 |
57 // Return true if the Input's data and |other|'s data are byte-wise equal. | 66 // Return true if the Input's data and |other|'s data are byte-wise equal. |
58 bool Equals(const Input& other) const; | 67 bool Equals(const Input& other) const; |
59 | 68 |
60 // Returns a pointer to the Input's data. This method is marked as "unsafe" | 69 // Returns a pointer to the Input's data. This method is marked as "unsafe" |
61 // because access to the Input's data should be done through ByteReader | 70 // because access to the Input's data should be done through ByteReader |
62 // instead. This method should only be used where using a ByteReader truly | 71 // instead. This method should only be used where using a ByteReader truly |
63 // is not an option. | 72 // is not an option. |
64 const uint8_t* UnsafeData() const { return data_; } | 73 const uint8_t* UnsafeData() const { return data_; } |
65 | 74 |
66 // Returns a copy of the data represented by this object as a std::string. | 75 // Returns a copy of the data represented by this object as a std::string. |
67 std::string AsString() const; | 76 std::string AsString() const; |
68 | 77 |
| 78 // Returns a StringPiece pointing to the same data as the Input. The resulting |
| 79 // StringPiece must not outlive the data that was used to construct this |
| 80 // Input. |
| 81 base::StringPiece AsStringPiece() const; |
| 82 |
69 private: | 83 private: |
| 84 // This constructor is deleted to prevent constructing an Input from a |
| 85 // std::string r-value. Since the Input points to memory owned by another |
| 86 // object, such an Input would point to invalid memory. Without this deleted |
| 87 // constructor, a std::string could be passed in to the base::StringPiece |
| 88 // constructor because of StringPiece's implicit constructor. |
| 89 Input(std::string) = delete; |
70 const uint8_t* data_; | 90 const uint8_t* data_; |
71 size_t len_; | 91 size_t len_; |
72 }; | 92 }; |
73 | 93 |
74 // Returns true if |lhs|'s data is lexicographically less than |rhs|'s data. | 94 // Returns true if |lhs|'s data is lexicographically less than |rhs|'s data. |
75 NET_EXPORT_PRIVATE bool operator<(const Input& lhs, const Input& rhs); | 95 NET_EXPORT_PRIVATE bool operator<(const Input& lhs, const Input& rhs); |
76 | 96 |
77 // This class provides ways to read data from an Input in a bounds-checked way. | 97 // This class provides ways to read data from an Input in a bounds-checked way. |
78 // The ByteReader is designed to read through the input sequentially. Once a | 98 // The ByteReader is designed to read through the input sequentially. Once a |
79 // byte has been read with a ByteReader, the caller can't go back and re-read | 99 // byte has been read with a ByteReader, the caller can't go back and re-read |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 explicit Mark(const uint8_t* ptr); | 176 explicit Mark(const uint8_t* ptr); |
157 Mark(); | 177 Mark(); |
158 const uint8_t* ptr_; | 178 const uint8_t* ptr_; |
159 }; | 179 }; |
160 | 180 |
161 } // namespace der | 181 } // namespace der |
162 | 182 |
163 } // namespace net | 183 } // namespace net |
164 | 184 |
165 #endif // NET_DER_INPUT_H_ | 185 #endif // NET_DER_INPUT_H_ |
OLD | NEW |