Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Adam Langley | e0e46cdf | 2018-10-29 19:23:16 | [diff] [blame] | 5 | #ifndef COMPONENTS_CBOR_VALUES_H_ |
| 6 | #define COMPONENTS_CBOR_VALUES_H_ |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 7 | |
Jun Choi | 06ae32d | 2017-12-21 18:52:39 | [diff] [blame] | 8 | #include <stdint.h> |
Jun Choi | 7b6fbff | 2018-01-22 19:54:01 | [diff] [blame] | 9 | |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 10 | #include <string> |
Adam Langley | 68672fd | 2017-10-17 19:35:27 | [diff] [blame] | 11 | #include <tuple> |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
Hans Wennborg | 29c74d3f | 2020-06-24 13:46:35 | [diff] [blame] | 14 | #include "base/check.h" |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 15 | #include "base/containers/flat_map.h" |
jdoerrie | b538442 | 2018-06-14 21:22:19 | [diff] [blame] | 16 | #include "base/containers/span.h" |
Hans Wennborg | 29c74d3f | 2020-06-24 13:46:35 | [diff] [blame] | 17 | #include "base/notreached.h" |
Adam Langley | df763d79 | 2017-11-28 19:20:15 | [diff] [blame] | 18 | #include "base/strings/string_piece.h" |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 19 | #include "components/cbor/cbor_export.h" |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 20 | |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 21 | namespace cbor { |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 22 | |
| 23 | // A class for Concise Binary Object Representation (CBOR) values. |
| 24 | // This does not support: |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 25 | // * Floating-point numbers. |
| 26 | // * Indefinite-length encodings. |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 27 | class CBOR_EXPORT Value { |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 28 | public: |
Kouhei Ueno | 6d1629e | 2018-02-01 17:28:03 | [diff] [blame] | 29 | struct Less { |
Adam Langley | 68672fd | 2017-10-17 19:35:27 | [diff] [blame] | 30 | // Comparison predicate to order keys in a dictionary as required by the |
Kouhei Ueno | 6d1629e | 2018-02-01 17:28:03 | [diff] [blame] | 31 | // canonical CBOR order defined in |
| 32 | // https://tools.ietf.org/html/rfc7049#section-3.9 |
| 33 | // TODO(808022): Clarify where this stands. |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 34 | bool operator()(const Value& a, const Value& b) const { |
Tsuyoshi Horo | 2a1fd52 | 2020-01-14 09:18:51 | [diff] [blame] | 35 | // The current implementation only supports integer, text string, byte |
| 36 | // string and invalid UTF8 keys. |
| 37 | DCHECK((a.is_integer() || a.is_string() || a.is_bytestring() || |
| 38 | a.is_invalid_utf8()) && |
| 39 | (b.is_integer() || b.is_string() || b.is_bytestring() || |
| 40 | b.is_invalid_utf8())); |
Kouhei Ueno | 6d1629e | 2018-02-01 17:28:03 | [diff] [blame] | 41 | |
| 42 | // Below text from https://tools.ietf.org/html/rfc7049 errata 4409: |
| 43 | // * If the major types are different, the one with the lower value |
| 44 | // in numerical order sorts earlier. |
Jun Choi | 98a59e46 | 2017-12-14 23:04:09 | [diff] [blame] | 45 | if (a.type() != b.type()) |
| 46 | return a.type() < b.type(); |
Kouhei Ueno | 6d1629e | 2018-02-01 17:28:03 | [diff] [blame] | 47 | |
| 48 | // * If two keys have different lengths, the shorter one sorts |
| 49 | // earlier; |
| 50 | // * If two keys have the same length, the one with the lower value |
| 51 | // in (byte-wise) lexical order sorts earlier. |
Jun Choi | 98a59e46 | 2017-12-14 23:04:09 | [diff] [blame] | 52 | switch (a.type()) { |
| 53 | case Type::UNSIGNED: |
Kouhei Ueno | 6d1629e | 2018-02-01 17:28:03 | [diff] [blame] | 54 | // For unsigned integers, the smaller value has shorter length, |
| 55 | // and (byte-wise) lexical representation. |
Jun Choi | 06ae32d | 2017-12-21 18:52:39 | [diff] [blame] | 56 | return a.GetInteger() < b.GetInteger(); |
| 57 | case Type::NEGATIVE: |
Kouhei Ueno | 6d1629e | 2018-02-01 17:28:03 | [diff] [blame] | 58 | // For negative integers, the value closer to zero has shorter length, |
| 59 | // and (byte-wise) lexical representation. |
Jun Choi | 06ae32d | 2017-12-21 18:52:39 | [diff] [blame] | 60 | return a.GetInteger() > b.GetInteger(); |
Jun Choi | 98a59e46 | 2017-12-14 23:04:09 | [diff] [blame] | 61 | case Type::STRING: { |
| 62 | const auto& a_str = a.GetString(); |
| 63 | const size_t a_length = a_str.size(); |
| 64 | const auto& b_str = b.GetString(); |
| 65 | const size_t b_length = b_str.size(); |
| 66 | return std::tie(a_length, a_str) < std::tie(b_length, b_str); |
| 67 | } |
Kouhei Ueno | 6d1629e | 2018-02-01 17:28:03 | [diff] [blame] | 68 | case Type::BYTE_STRING: { |
| 69 | const auto& a_str = a.GetBytestring(); |
| 70 | const size_t a_length = a_str.size(); |
| 71 | const auto& b_str = b.GetBytestring(); |
| 72 | const size_t b_length = b_str.size(); |
| 73 | return std::tie(a_length, a_str) < std::tie(b_length, b_str); |
| 74 | } |
Tsuyoshi Horo | 2a1fd52 | 2020-01-14 09:18:51 | [diff] [blame] | 75 | case Type::INVALID_UTF8: { |
| 76 | const auto& a_str = a.GetInvalidUTF8(); |
| 77 | const size_t a_length = a_str.size(); |
| 78 | const auto& b_str = b.GetInvalidUTF8(); |
| 79 | const size_t b_length = b_str.size(); |
| 80 | return std::tie(a_length, a_str) < std::tie(b_length, b_str); |
| 81 | } |
Jun Choi | 98a59e46 | 2017-12-14 23:04:09 | [diff] [blame] | 82 | default: |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | NOTREACHED(); |
| 87 | return false; |
Adam Langley | 68672fd | 2017-10-17 19:35:27 | [diff] [blame] | 88 | } |
| 89 | |
Adam Langley | 68672fd | 2017-10-17 19:35:27 | [diff] [blame] | 90 | using is_transparent = void; |
| 91 | }; |
| 92 | |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 93 | using BinaryValue = std::vector<uint8_t>; |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 94 | using ArrayValue = std::vector<Value>; |
| 95 | using MapValue = base::flat_map<Value, Value, Less>; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 96 | |
| 97 | enum class Type { |
Jun Choi | 6d30c4a | 2017-12-09 01:10:32 | [diff] [blame] | 98 | UNSIGNED = 0, |
Jun Choi | 06ae32d | 2017-12-21 18:52:39 | [diff] [blame] | 99 | NEGATIVE = 1, |
Jun Choi | 6d30c4a | 2017-12-09 01:10:32 | [diff] [blame] | 100 | BYTE_STRING = 2, |
| 101 | STRING = 3, |
| 102 | ARRAY = 4, |
| 103 | MAP = 5, |
Chris Palmer | be2d8dc | 2018-09-14 00:31:42 | [diff] [blame] | 104 | TAG = 6, |
Jun Choi | 07540c6 | 2017-12-21 02:51:43 | [diff] [blame] | 105 | SIMPLE_VALUE = 7, |
Russ Hamilton | 385541b | 2023-07-06 00:30:38 | [diff] [blame^] | 106 | // In CBOR floating types also have major type 7, but we separate them here |
| 107 | // for simplicity. |
| 108 | FLOAT_VALUE = 70, |
Jun Choi | 6d30c4a | 2017-12-09 01:10:32 | [diff] [blame] | 109 | NONE = -1, |
Adam Langley | 08718f73 | 2019-04-22 22:21:33 | [diff] [blame] | 110 | INVALID_UTF8 = -2, |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 111 | }; |
| 112 | |
Jun Choi | 07540c6 | 2017-12-21 02:51:43 | [diff] [blame] | 113 | enum class SimpleValue { |
| 114 | FALSE_VALUE = 20, |
| 115 | TRUE_VALUE = 21, |
| 116 | NULL_VALUE = 22, |
| 117 | UNDEFINED = 23, |
| 118 | }; |
| 119 | |
Martin Kreichgauer | 2344a64 | 2019-07-15 21:32:50 | [diff] [blame] | 120 | // Returns a Value with Type::INVALID_UTF8. This factory method lets tests |
| 121 | // encode such a value as a CBOR string. It should never be used outside of |
| 122 | // tests since encoding may yield invalid CBOR data. |
| 123 | static Value InvalidUTF8StringValueForTesting(base::StringPiece in_string); |
| 124 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 125 | Value(Value&& that) noexcept; |
| 126 | Value() noexcept; // A NONE value. |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 127 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 128 | explicit Value(Type type); |
Jun Choi | 7b6fbff | 2018-01-22 19:54:01 | [diff] [blame] | 129 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 130 | explicit Value(SimpleValue in_simple); |
| 131 | explicit Value(bool boolean_value); |
Russ Hamilton | 385541b | 2023-07-06 00:30:38 | [diff] [blame^] | 132 | explicit Value(double in_float); |
Jun Choi | 7b6fbff | 2018-01-22 19:54:01 | [diff] [blame] | 133 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 134 | explicit Value(int integer_value); |
| 135 | explicit Value(int64_t integer_value); |
| 136 | explicit Value(uint64_t integer_value) = delete; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 137 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 138 | explicit Value(base::span<const uint8_t> in_bytes); |
| 139 | explicit Value(BinaryValue&& in_bytes) noexcept; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 140 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 141 | explicit Value(const char* in_string, Type type = Type::STRING); |
| 142 | explicit Value(std::string&& in_string, Type type = Type::STRING) noexcept; |
| 143 | explicit Value(base::StringPiece in_string, Type type = Type::STRING); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 144 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 145 | explicit Value(const ArrayValue& in_array); |
| 146 | explicit Value(ArrayValue&& in_array) noexcept; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 147 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 148 | explicit Value(const MapValue& in_map); |
| 149 | explicit Value(MapValue&& in_map) noexcept; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 150 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 151 | Value& operator=(Value&& that) noexcept; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 152 | |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 153 | Value(const Value&) = delete; |
| 154 | Value& operator=(const Value&) = delete; |
| 155 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 156 | ~Value(); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 157 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 158 | // Value's copy constructor and copy assignment operator are deleted. |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 159 | // Use this to obtain a deep copy explicitly. |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 160 | Value Clone() const; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 161 | |
| 162 | // Returns the type of the value stored by the current Value object. |
| 163 | Type type() const { return type_; } |
| 164 | |
| 165 | // Returns true if the current object represents a given type. |
| 166 | bool is_type(Type type) const { return type == type_; } |
| 167 | bool is_none() const { return type() == Type::NONE; } |
Adam Langley | 08718f73 | 2019-04-22 22:21:33 | [diff] [blame] | 168 | bool is_invalid_utf8() const { return type() == Type::INVALID_UTF8; } |
Jun Choi | 7b6fbff | 2018-01-22 19:54:01 | [diff] [blame] | 169 | bool is_simple() const { return type() == Type::SIMPLE_VALUE; } |
| 170 | bool is_bool() const { |
| 171 | return is_simple() && (simple_value_ == SimpleValue::TRUE_VALUE || |
| 172 | simple_value_ == SimpleValue::FALSE_VALUE); |
| 173 | } |
Russ Hamilton | 385541b | 2023-07-06 00:30:38 | [diff] [blame^] | 174 | bool is_double() const { return type() == Type::FLOAT_VALUE; } |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 175 | bool is_unsigned() const { return type() == Type::UNSIGNED; } |
Jun Choi | 06ae32d | 2017-12-21 18:52:39 | [diff] [blame] | 176 | bool is_negative() const { return type() == Type::NEGATIVE; } |
| 177 | bool is_integer() const { return is_unsigned() || is_negative(); } |
Jun Choi | 6d30c4a | 2017-12-09 01:10:32 | [diff] [blame] | 178 | bool is_bytestring() const { return type() == Type::BYTE_STRING; } |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 179 | bool is_string() const { return type() == Type::STRING; } |
| 180 | bool is_array() const { return type() == Type::ARRAY; } |
| 181 | bool is_map() const { return type() == Type::MAP; } |
| 182 | |
| 183 | // These will all fatally assert if the type doesn't match. |
Jun Choi | 07540c6 | 2017-12-21 02:51:43 | [diff] [blame] | 184 | SimpleValue GetSimpleValue() const; |
Jun Choi | 7b6fbff | 2018-01-22 19:54:01 | [diff] [blame] | 185 | bool GetBool() const; |
Russ Hamilton | 385541b | 2023-07-06 00:30:38 | [diff] [blame^] | 186 | double GetDouble() const; |
Jun Choi | 06ae32d | 2017-12-21 18:52:39 | [diff] [blame] | 187 | const int64_t& GetInteger() const; |
| 188 | const int64_t& GetUnsigned() const; |
| 189 | const int64_t& GetNegative() const; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 190 | const BinaryValue& GetBytestring() const; |
Kouhei Ueno | c12b0b9 | 2018-02-01 14:02:10 | [diff] [blame] | 191 | base::StringPiece GetBytestringAsString() const; |
Jun Choi | 47be22c | 2017-12-14 07:55:16 | [diff] [blame] | 192 | // Returned string may contain NUL characters. |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 193 | const std::string& GetString() const; |
| 194 | const ArrayValue& GetArray() const; |
| 195 | const MapValue& GetMap() const; |
Adam Langley | 08718f73 | 2019-04-22 22:21:33 | [diff] [blame] | 196 | const BinaryValue& GetInvalidUTF8() const; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 197 | |
| 198 | private: |
Adam Langley | 08718f73 | 2019-04-22 22:21:33 | [diff] [blame] | 199 | friend class Reader; |
| 200 | // This constructor allows INVALID_UTF8 values to be created, which only |
Martin Kreichgauer | 2344a64 | 2019-07-15 21:32:50 | [diff] [blame] | 201 | // |Reader| and InvalidUTF8StringValueForTesting() may do. |
Adam Langley | 08718f73 | 2019-04-22 22:21:33 | [diff] [blame] | 202 | Value(base::span<const uint8_t> in_bytes, Type type); |
| 203 | |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 204 | Type type_; |
| 205 | |
| 206 | union { |
Jun Choi | 07540c6 | 2017-12-21 02:51:43 | [diff] [blame] | 207 | SimpleValue simple_value_; |
Jun Choi | 06ae32d | 2017-12-21 18:52:39 | [diff] [blame] | 208 | int64_t integer_value_; |
Russ Hamilton | 385541b | 2023-07-06 00:30:38 | [diff] [blame^] | 209 | double float_value_; |
Daniel Cheng | 9eb43fd34 | 2017-10-10 21:28:37 | [diff] [blame] | 210 | BinaryValue bytestring_value_; |
| 211 | std::string string_value_; |
| 212 | ArrayValue array_value_; |
| 213 | MapValue map_value_; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 214 | }; |
| 215 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 216 | void InternalMoveConstructFrom(Value&& that); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 217 | void InternalCleanup(); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 218 | }; |
Martin Kreichgauer | 2344a64 | 2019-07-15 21:32:50 | [diff] [blame] | 219 | |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 220 | } // namespace cbor |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 221 | |
Adam Langley | e0e46cdf | 2018-10-29 19:23:16 | [diff] [blame] | 222 | #endif // COMPONENTS_CBOR_VALUES_H_ |