Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 1 | // Copyright 2017 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. |
| 4 | |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 5 | #ifndef COMPONENTS_CBOR_CBOR_VALUES_H_ |
| 6 | #define COMPONENTS_CBOR_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 | |
| 14 | #include "base/containers/flat_map.h" |
jdoerrie | b538442 | 2018-06-14 21:22:19 | [diff] [blame] | 15 | #include "base/containers/span.h" |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 16 | #include "base/macros.h" |
Adam Langley | df763d79 | 2017-11-28 19:20:15 | [diff] [blame] | 17 | #include "base/strings/string_piece.h" |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 18 | #include "components/cbor/cbor_export.h" |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 19 | |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 20 | namespace cbor { |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 21 | |
| 22 | // A class for Concise Binary Object Representation (CBOR) values. |
| 23 | // This does not support: |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 24 | // * Floating-point numbers. |
| 25 | // * Indefinite-length encodings. |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame^] | 26 | class CBOR_EXPORT Value { |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 27 | public: |
Kouhei Ueno | 6d1629e | 2018-02-01 17:28:03 | [diff] [blame] | 28 | struct Less { |
Adam Langley | 68672fd | 2017-10-17 19:35:27 | [diff] [blame] | 29 | // Comparison predicate to order keys in a dictionary as required by the |
Kouhei Ueno | 6d1629e | 2018-02-01 17:28:03 | [diff] [blame] | 30 | // canonical CBOR order defined in |
| 31 | // https://tools.ietf.org/html/rfc7049#section-3.9 |
| 32 | // TODO(808022): Clarify where this stands. |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame^] | 33 | bool operator()(const Value& a, const Value& b) const { |
Kouhei Ueno | 6d1629e | 2018-02-01 17:28:03 | [diff] [blame] | 34 | // The current implementation only supports integer, text string, |
| 35 | // and byte string keys. |
| 36 | DCHECK((a.is_integer() || a.is_string() || a.is_bytestring()) && |
| 37 | (b.is_integer() || b.is_string() || b.is_bytestring())); |
| 38 | |
| 39 | // Below text from https://tools.ietf.org/html/rfc7049 errata 4409: |
| 40 | // * If the major types are different, the one with the lower value |
| 41 | // in numerical order sorts earlier. |
Jun Choi | 98a59e46 | 2017-12-14 23:04:09 | [diff] [blame] | 42 | if (a.type() != b.type()) |
| 43 | return a.type() < b.type(); |
Kouhei Ueno | 6d1629e | 2018-02-01 17:28:03 | [diff] [blame] | 44 | |
| 45 | // * If two keys have different lengths, the shorter one sorts |
| 46 | // earlier; |
| 47 | // * If two keys have the same length, the one with the lower value |
| 48 | // in (byte-wise) lexical order sorts earlier. |
Jun Choi | 98a59e46 | 2017-12-14 23:04:09 | [diff] [blame] | 49 | switch (a.type()) { |
| 50 | case Type::UNSIGNED: |
Kouhei Ueno | 6d1629e | 2018-02-01 17:28:03 | [diff] [blame] | 51 | // For unsigned integers, the smaller value has shorter length, |
| 52 | // and (byte-wise) lexical representation. |
Jun Choi | 06ae32d | 2017-12-21 18:52:39 | [diff] [blame] | 53 | return a.GetInteger() < b.GetInteger(); |
| 54 | case Type::NEGATIVE: |
Kouhei Ueno | 6d1629e | 2018-02-01 17:28:03 | [diff] [blame] | 55 | // For negative integers, the value closer to zero has shorter length, |
| 56 | // and (byte-wise) lexical representation. |
Jun Choi | 06ae32d | 2017-12-21 18:52:39 | [diff] [blame] | 57 | return a.GetInteger() > b.GetInteger(); |
Jun Choi | 98a59e46 | 2017-12-14 23:04:09 | [diff] [blame] | 58 | case Type::STRING: { |
| 59 | const auto& a_str = a.GetString(); |
| 60 | const size_t a_length = a_str.size(); |
| 61 | const auto& b_str = b.GetString(); |
| 62 | const size_t b_length = b_str.size(); |
| 63 | return std::tie(a_length, a_str) < std::tie(b_length, b_str); |
| 64 | } |
Kouhei Ueno | 6d1629e | 2018-02-01 17:28:03 | [diff] [blame] | 65 | case Type::BYTE_STRING: { |
| 66 | const auto& a_str = a.GetBytestring(); |
| 67 | const size_t a_length = a_str.size(); |
| 68 | const auto& b_str = b.GetBytestring(); |
| 69 | const size_t b_length = b_str.size(); |
| 70 | return std::tie(a_length, a_str) < std::tie(b_length, b_str); |
| 71 | } |
Jun Choi | 98a59e46 | 2017-12-14 23:04:09 | [diff] [blame] | 72 | default: |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | NOTREACHED(); |
| 77 | return false; |
Adam Langley | 68672fd | 2017-10-17 19:35:27 | [diff] [blame] | 78 | } |
| 79 | |
Adam Langley | 68672fd | 2017-10-17 19:35:27 | [diff] [blame] | 80 | using is_transparent = void; |
| 81 | }; |
| 82 | |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 83 | using BinaryValue = std::vector<uint8_t>; |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame^] | 84 | using ArrayValue = std::vector<Value>; |
| 85 | using MapValue = base::flat_map<Value, Value, Less>; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 86 | |
| 87 | enum class Type { |
Jun Choi | 6d30c4a | 2017-12-09 01:10:32 | [diff] [blame] | 88 | UNSIGNED = 0, |
Jun Choi | 06ae32d | 2017-12-21 18:52:39 | [diff] [blame] | 89 | NEGATIVE = 1, |
Jun Choi | 6d30c4a | 2017-12-09 01:10:32 | [diff] [blame] | 90 | BYTE_STRING = 2, |
| 91 | STRING = 3, |
| 92 | ARRAY = 4, |
| 93 | MAP = 5, |
Chris Palmer | be2d8dc | 2018-09-14 00:31:42 | [diff] [blame] | 94 | TAG = 6, |
Jun Choi | 07540c6 | 2017-12-21 02:51:43 | [diff] [blame] | 95 | SIMPLE_VALUE = 7, |
Jun Choi | 6d30c4a | 2017-12-09 01:10:32 | [diff] [blame] | 96 | NONE = -1, |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 97 | }; |
| 98 | |
Jun Choi | 07540c6 | 2017-12-21 02:51:43 | [diff] [blame] | 99 | enum class SimpleValue { |
| 100 | FALSE_VALUE = 20, |
| 101 | TRUE_VALUE = 21, |
| 102 | NULL_VALUE = 22, |
| 103 | UNDEFINED = 23, |
| 104 | }; |
| 105 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame^] | 106 | Value(Value&& that) noexcept; |
| 107 | Value() noexcept; // A NONE value. |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 108 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame^] | 109 | explicit Value(Type type); |
Jun Choi | 7b6fbff | 2018-01-22 19:54:01 | [diff] [blame] | 110 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame^] | 111 | explicit Value(SimpleValue in_simple); |
| 112 | explicit Value(bool boolean_value); |
Jun Choi | 7b6fbff | 2018-01-22 19:54:01 | [diff] [blame] | 113 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame^] | 114 | explicit Value(int integer_value); |
| 115 | explicit Value(int64_t integer_value); |
| 116 | explicit Value(uint64_t integer_value) = delete; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 117 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame^] | 118 | explicit Value(base::span<const uint8_t> in_bytes); |
| 119 | explicit Value(BinaryValue&& in_bytes) noexcept; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 120 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame^] | 121 | explicit Value(const char* in_string, Type type = Type::STRING); |
| 122 | explicit Value(std::string&& in_string, Type type = Type::STRING) noexcept; |
| 123 | explicit Value(base::StringPiece in_string, Type type = Type::STRING); |
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(const ArrayValue& in_array); |
| 126 | explicit Value(ArrayValue&& in_array) noexcept; |
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(const MapValue& in_map); |
| 129 | explicit Value(MapValue&& in_map) noexcept; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 130 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame^] | 131 | Value& operator=(Value&& that) noexcept; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 132 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame^] | 133 | ~Value(); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 134 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame^] | 135 | // Value's copy constructor and copy assignment operator are deleted. |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 136 | // Use this to obtain a deep copy explicitly. |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame^] | 137 | Value Clone() const; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 138 | |
| 139 | // Returns the type of the value stored by the current Value object. |
| 140 | Type type() const { return type_; } |
| 141 | |
| 142 | // Returns true if the current object represents a given type. |
| 143 | bool is_type(Type type) const { return type == type_; } |
| 144 | bool is_none() const { return type() == Type::NONE; } |
Jun Choi | 7b6fbff | 2018-01-22 19:54:01 | [diff] [blame] | 145 | bool is_simple() const { return type() == Type::SIMPLE_VALUE; } |
| 146 | bool is_bool() const { |
| 147 | return is_simple() && (simple_value_ == SimpleValue::TRUE_VALUE || |
| 148 | simple_value_ == SimpleValue::FALSE_VALUE); |
| 149 | } |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 150 | bool is_unsigned() const { return type() == Type::UNSIGNED; } |
Jun Choi | 06ae32d | 2017-12-21 18:52:39 | [diff] [blame] | 151 | bool is_negative() const { return type() == Type::NEGATIVE; } |
| 152 | bool is_integer() const { return is_unsigned() || is_negative(); } |
Jun Choi | 6d30c4a | 2017-12-09 01:10:32 | [diff] [blame] | 153 | bool is_bytestring() const { return type() == Type::BYTE_STRING; } |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 154 | bool is_string() const { return type() == Type::STRING; } |
| 155 | bool is_array() const { return type() == Type::ARRAY; } |
| 156 | bool is_map() const { return type() == Type::MAP; } |
| 157 | |
| 158 | // These will all fatally assert if the type doesn't match. |
Jun Choi | 07540c6 | 2017-12-21 02:51:43 | [diff] [blame] | 159 | SimpleValue GetSimpleValue() const; |
Jun Choi | 7b6fbff | 2018-01-22 19:54:01 | [diff] [blame] | 160 | bool GetBool() const; |
Jun Choi | 06ae32d | 2017-12-21 18:52:39 | [diff] [blame] | 161 | const int64_t& GetInteger() const; |
| 162 | const int64_t& GetUnsigned() const; |
| 163 | const int64_t& GetNegative() const; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 164 | const BinaryValue& GetBytestring() const; |
Kouhei Ueno | c12b0b9 | 2018-02-01 14:02:10 | [diff] [blame] | 165 | base::StringPiece GetBytestringAsString() const; |
Jun Choi | 47be22c | 2017-12-14 07:55:16 | [diff] [blame] | 166 | // Returned string may contain NUL characters. |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 167 | const std::string& GetString() const; |
| 168 | const ArrayValue& GetArray() const; |
| 169 | const MapValue& GetMap() const; |
| 170 | |
| 171 | private: |
| 172 | Type type_; |
| 173 | |
| 174 | union { |
Jun Choi | 07540c6 | 2017-12-21 02:51:43 | [diff] [blame] | 175 | SimpleValue simple_value_; |
Jun Choi | 06ae32d | 2017-12-21 18:52:39 | [diff] [blame] | 176 | int64_t integer_value_; |
Daniel Cheng | 9eb43fd34 | 2017-10-10 21:28:37 | [diff] [blame] | 177 | BinaryValue bytestring_value_; |
| 178 | std::string string_value_; |
| 179 | ArrayValue array_value_; |
| 180 | MapValue map_value_; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 181 | }; |
| 182 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame^] | 183 | void InternalMoveConstructFrom(Value&& that); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 184 | void InternalCleanup(); |
| 185 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame^] | 186 | DISALLOW_COPY_AND_ASSIGN(Value); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 187 | }; |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 188 | } // namespace cbor |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 189 | |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 190 | #endif // COMPONENTS_CBOR_CBOR_VALUES_H_ |