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 | #include "components/cbor/writer.h" |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 6 | |
Russ Hamilton | 385541b | 2023-07-06 00:30:38 | [diff] [blame] | 7 | #include <cstdint> |
Tom Sepez | b03f3f8 | 2021-12-09 23:43:38 | [diff] [blame] | 8 | #include <ostream> |
Jun Choi | 0ca8667 | 2017-11-14 04:58:24 | [diff] [blame] | 9 | #include <string> |
Helmut Januschka | 26faf71 | 2024-05-01 20:04:48 | [diff] [blame] | 10 | #include <string_view> |
Jun Choi | 0ca8667 | 2017-11-14 04:58:24 | [diff] [blame] | 11 | |
Russ Hamilton | 385541b | 2023-07-06 00:30:38 | [diff] [blame] | 12 | #include "base/bit_cast.h" |
Hans Wennborg | df87046c | 2020-04-28 11:06:24 | [diff] [blame] | 13 | #include "base/check_op.h" |
| 14 | #include "base/notreached.h" |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 15 | #include "base/numerics/safe_conversions.h" |
Adam Langley | e0e46cdf | 2018-10-29 19:23:16 | [diff] [blame] | 16 | #include "components/cbor/constants.h" |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 17 | |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 18 | namespace cbor { |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 19 | |
Sorin Jianu | ad029cb7 | 2024-10-04 03:35:55 | [diff] [blame] | 20 | Writer::~Writer() = default; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 21 | |
| 22 | // static |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 23 | std::optional<std::vector<uint8_t>> Writer::Write(const Value& node, |
| 24 | const Config& config) { |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 25 | std::vector<uint8_t> cbor; |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 26 | Writer writer(&cbor); |
Martin Kreichgauer | 2344a64 | 2019-07-15 21:32:50 | [diff] [blame] | 27 | if (!writer.EncodeCBOR(node, config.max_nesting_level, |
| 28 | config.allow_invalid_utf8_for_testing)) { |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 29 | return std::nullopt; |
Martin Kreichgauer | 2344a64 | 2019-07-15 21:32:50 | [diff] [blame] | 30 | } |
| 31 | return cbor; |
| 32 | } |
| 33 | |
| 34 | // static |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 35 | std::optional<std::vector<uint8_t>> Writer::Write(const Value& node, |
| 36 | size_t max_nesting_level) { |
Martin Kreichgauer | 2344a64 | 2019-07-15 21:32:50 | [diff] [blame] | 37 | Config config; |
| 38 | config.max_nesting_level = base::checked_cast<int>(max_nesting_level); |
| 39 | return Write(node, config); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 40 | } |
| 41 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 42 | Writer::Writer(std::vector<uint8_t>* cbor) : encoded_cbor_(cbor) {} |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 43 | |
Martin Kreichgauer | 2344a64 | 2019-07-15 21:32:50 | [diff] [blame] | 44 | bool Writer::EncodeCBOR(const Value& node, |
| 45 | int max_nesting_level, |
| 46 | bool allow_invalid_utf8) { |
Jun Choi | 0ca8667 | 2017-11-14 04:58:24 | [diff] [blame] | 47 | if (max_nesting_level < 0) |
| 48 | return false; |
| 49 | |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 50 | switch (node.type()) { |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 51 | case Value::Type::NONE: { |
| 52 | StartItem(Value::Type::BYTE_STRING, 0); |
Jun Choi | 0ca8667 | 2017-11-14 04:58:24 | [diff] [blame] | 53 | return true; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 54 | } |
| 55 | |
Martin Kreichgauer | 2344a64 | 2019-07-15 21:32:50 | [diff] [blame] | 56 | case Value::Type::INVALID_UTF8: { |
| 57 | if (!allow_invalid_utf8) { |
Peter Boström | 77d2135 | 2024-11-13 22:26:11 | [diff] [blame] | 58 | NOTREACHED() << constants::kUnsupportedMajorType; |
Martin Kreichgauer | 2344a64 | 2019-07-15 21:32:50 | [diff] [blame] | 59 | } |
| 60 | // Encode a CBOR string with invalid UTF-8 data. This may produce invalid |
| 61 | // CBOR and is reachable in tests only. See |
| 62 | // |allow_invalid_utf8_for_testing| in Config. |
| 63 | const Value::BinaryValue& bytes = node.GetInvalidUTF8(); |
| 64 | StartItem(Value::Type::STRING, base::strict_cast<uint64_t>(bytes.size())); |
| 65 | encoded_cbor_->insert(encoded_cbor_->end(), bytes.begin(), bytes.end()); |
| 66 | return true; |
| 67 | } |
Adam Langley | 08718f73 | 2019-04-22 22:21:33 | [diff] [blame] | 68 | |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 69 | // Represents unsigned integers. |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 70 | case Value::Type::UNSIGNED: { |
Jun Choi | 06ae32d | 2017-12-21 18:52:39 | [diff] [blame] | 71 | int64_t value = node.GetUnsigned(); |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 72 | StartItem(Value::Type::UNSIGNED, static_cast<uint64_t>(value)); |
Jun Choi | 06ae32d | 2017-12-21 18:52:39 | [diff] [blame] | 73 | return true; |
| 74 | } |
| 75 | |
| 76 | // Represents negative integers. |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 77 | case Value::Type::NEGATIVE: { |
Jun Choi | 06ae32d | 2017-12-21 18:52:39 | [diff] [blame] | 78 | int64_t value = node.GetNegative(); |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 79 | StartItem(Value::Type::NEGATIVE, static_cast<uint64_t>(-(value + 1))); |
Jun Choi | 0ca8667 | 2017-11-14 04:58:24 | [diff] [blame] | 80 | return true; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // Represents a byte string. |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 84 | case Value::Type::BYTE_STRING: { |
| 85 | const Value::BinaryValue& bytes = node.GetBytestring(); |
| 86 | StartItem(Value::Type::BYTE_STRING, |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 87 | base::strict_cast<uint64_t>(bytes.size())); |
| 88 | // Add the bytes. |
| 89 | encoded_cbor_->insert(encoded_cbor_->end(), bytes.begin(), bytes.end()); |
Jun Choi | 0ca8667 | 2017-11-14 04:58:24 | [diff] [blame] | 90 | return true; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 91 | } |
| 92 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 93 | case Value::Type::STRING: { |
Helmut Januschka | 26faf71 | 2024-05-01 20:04:48 | [diff] [blame] | 94 | std::string_view string = node.GetString(); |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 95 | StartItem(Value::Type::STRING, |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 96 | base::strict_cast<uint64_t>(string.size())); |
| 97 | |
| 98 | // Add the characters. |
| 99 | encoded_cbor_->insert(encoded_cbor_->end(), string.begin(), string.end()); |
Jun Choi | 0ca8667 | 2017-11-14 04:58:24 | [diff] [blame] | 100 | return true; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // Represents an array. |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 104 | case Value::Type::ARRAY: { |
| 105 | const Value::ArrayValue& array = node.GetArray(); |
| 106 | StartItem(Value::Type::ARRAY, array.size()); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 107 | for (const auto& value : array) { |
Martin Kreichgauer | 2344a64 | 2019-07-15 21:32:50 | [diff] [blame] | 108 | if (!EncodeCBOR(value, max_nesting_level - 1, allow_invalid_utf8)) |
Jun Choi | 0ca8667 | 2017-11-14 04:58:24 | [diff] [blame] | 109 | return false; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 110 | } |
Jun Choi | 0ca8667 | 2017-11-14 04:58:24 | [diff] [blame] | 111 | return true; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Represents a map. |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 115 | case Value::Type::MAP: { |
| 116 | const Value::MapValue& map = node.GetMap(); |
| 117 | StartItem(Value::Type::MAP, map.size()); |
Jun Choi | 0ca8667 | 2017-11-14 04:58:24 | [diff] [blame] | 118 | |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 119 | for (const auto& value : map) { |
Martin Kreichgauer | 2344a64 | 2019-07-15 21:32:50 | [diff] [blame] | 120 | if (!EncodeCBOR(value.first, max_nesting_level - 1, allow_invalid_utf8)) |
Jun Choi | 0ca8667 | 2017-11-14 04:58:24 | [diff] [blame] | 121 | return false; |
Martin Kreichgauer | 2344a64 | 2019-07-15 21:32:50 | [diff] [blame] | 122 | if (!EncodeCBOR(value.second, max_nesting_level - 1, |
| 123 | allow_invalid_utf8)) |
Jun Choi | 0ca8667 | 2017-11-14 04:58:24 | [diff] [blame] | 124 | return false; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 125 | } |
Jun Choi | 0ca8667 | 2017-11-14 04:58:24 | [diff] [blame] | 126 | return true; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 127 | } |
Jun Choi | 07540c6 | 2017-12-21 02:51:43 | [diff] [blame] | 128 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 129 | case Value::Type::TAG: |
Peter Boström | 77d2135 | 2024-11-13 22:26:11 | [diff] [blame] | 130 | NOTREACHED() << constants::kUnsupportedMajorType; |
Chris Palmer | be2d8dc | 2018-09-14 00:31:42 | [diff] [blame] | 131 | |
Jun Choi | 07540c6 | 2017-12-21 02:51:43 | [diff] [blame] | 132 | // Represents a simple value. |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 133 | case Value::Type::SIMPLE_VALUE: { |
| 134 | const Value::SimpleValue simple_value = node.GetSimpleValue(); |
| 135 | StartItem(Value::Type::SIMPLE_VALUE, |
Jun Choi | 07540c6 | 2017-12-21 02:51:43 | [diff] [blame] | 136 | base::checked_cast<uint64_t>(simple_value)); |
| 137 | return true; |
| 138 | } |
Russ Hamilton | 385541b | 2023-07-06 00:30:38 | [diff] [blame] | 139 | |
| 140 | case Value::Type::FLOAT_VALUE: { |
| 141 | const double float_value = node.GetDouble(); |
| 142 | encoded_cbor_->push_back(base::checked_cast<uint8_t>( |
| 143 | static_cast<unsigned>(Value::Type::SIMPLE_VALUE) |
| 144 | << constants::kMajorTypeBitShift)); |
| 145 | { |
| 146 | uint16_t value_16 = EncodeHalfPrecisionFloat(float_value); |
| 147 | const double decoded_float_16 = DecodeHalfPrecisionFloat(value_16); |
| 148 | if (decoded_float_16 == float_value || |
| 149 | (std::isnan(decoded_float_16) && std::isnan(float_value))) { |
| 150 | // We can encode it in 16 bits. |
| 151 | |
| 152 | SetAdditionalInformation(constants::kAdditionalInformation2Bytes); |
| 153 | for (int shift = 1; shift >= 0; shift--) { |
| 154 | encoded_cbor_->push_back(0xFF & (value_16 >> (shift * 8))); |
| 155 | } |
| 156 | return true; |
| 157 | } |
| 158 | } |
| 159 | { |
| 160 | const float float_value_32 = float_value; |
| 161 | if (float_value == float_value_32) { |
| 162 | // We can encode it in 32 bits. |
| 163 | |
| 164 | SetAdditionalInformation(constants::kAdditionalInformation4Bytes); |
| 165 | uint32_t value_32 = base::bit_cast<uint32_t>(float_value_32); |
| 166 | for (int shift = 3; shift >= 0; shift--) { |
| 167 | encoded_cbor_->push_back(0xFF & (value_32 >> (shift * 8))); |
| 168 | } |
| 169 | return true; |
| 170 | } |
| 171 | } |
| 172 | { |
| 173 | // We can always encode it in 64 bits. |
| 174 | SetAdditionalInformation(constants::kAdditionalInformation8Bytes); |
| 175 | uint64_t value_64 = base::bit_cast<uint64_t>(float_value); |
| 176 | for (int shift = 7; shift >= 0; shift--) { |
| 177 | encoded_cbor_->push_back(0xFF & (value_64 >> (shift * 8))); |
| 178 | } |
| 179 | return true; |
| 180 | } |
| 181 | } |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 182 | } |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 183 | } |
| 184 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 185 | void Writer::StartItem(Value::Type type, uint64_t size) { |
Jun Choi | 6d30c4a | 2017-12-09 01:10:32 | [diff] [blame] | 186 | encoded_cbor_->push_back(base::checked_cast<uint8_t>( |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 187 | static_cast<unsigned>(type) << constants::kMajorTypeBitShift)); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 188 | SetUint(size); |
| 189 | } |
| 190 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 191 | void Writer::SetAdditionalInformation(uint8_t additional_information) { |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 192 | DCHECK(!encoded_cbor_->empty()); |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 193 | DCHECK_EQ(additional_information & constants::kAdditionalInformationMask, |
Adam Langley | 31c85e7 | 2017-10-17 06:10:35 | [diff] [blame] | 194 | additional_information); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 195 | encoded_cbor_->back() |= |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 196 | (additional_information & constants::kAdditionalInformationMask); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 197 | } |
| 198 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 199 | void Writer::SetUint(uint64_t value) { |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 200 | size_t count = GetNumUintBytes(value); |
| 201 | int shift = -1; |
| 202 | // Values under 24 are encoded directly in the initial byte. |
| 203 | // Otherwise, the last 5 bits of the initial byte contains the length |
| 204 | // of unsigned integer, which is encoded in following bytes. |
| 205 | switch (count) { |
| 206 | case 0: |
| 207 | SetAdditionalInformation(base::checked_cast<uint8_t>(value)); |
| 208 | break; |
| 209 | case 1: |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 210 | SetAdditionalInformation(constants::kAdditionalInformation1Byte); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 211 | shift = 0; |
| 212 | break; |
| 213 | case 2: |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 214 | SetAdditionalInformation(constants::kAdditionalInformation2Bytes); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 215 | shift = 1; |
| 216 | break; |
| 217 | case 4: |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 218 | SetAdditionalInformation(constants::kAdditionalInformation4Bytes); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 219 | shift = 3; |
| 220 | break; |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 221 | case 8: |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 222 | SetAdditionalInformation(constants::kAdditionalInformation8Bytes); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 223 | shift = 7; |
| 224 | break; |
Adam Langley | 31c85e7 | 2017-10-17 06:10:35 | [diff] [blame] | 225 | default: |
Peter Boström | 77d2135 | 2024-11-13 22:26:11 | [diff] [blame] | 226 | NOTREACHED(); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 227 | } |
| 228 | for (; shift >= 0; shift--) { |
Adam Langley | 31c85e7 | 2017-10-17 06:10:35 | [diff] [blame] | 229 | encoded_cbor_->push_back(0xFF & (value >> (shift * 8))); |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 230 | } |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 231 | } |
| 232 | |
Adam Langley | b4f12f9 | 2018-10-26 21:00:02 | [diff] [blame] | 233 | size_t Writer::GetNumUintBytes(uint64_t value) { |
Kim Paulhamus | 6efcf495 | 2017-09-14 22:46:27 | [diff] [blame] | 234 | if (value < 24) { |
| 235 | return 0; |
| 236 | } else if (value <= 0xFF) { |
| 237 | return 1; |
| 238 | } else if (value <= 0xFFFF) { |
| 239 | return 2; |
| 240 | } else if (value <= 0xFFFFFFFF) { |
| 241 | return 4; |
| 242 | } |
| 243 | return 8; |
| 244 | } |
| 245 | |
Jun Choi | 9f1446c0 | 2017-12-21 23:33:27 | [diff] [blame] | 246 | } // namespace cbor |