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