| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "net/quic/core/quic_data_writer.h" | 5 #include "net/quic/core/quic_data_writer.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "net/quic/core/quic_utils.h" | 10 #include "net/quic/core/quic_utils.h" |
| 11 #include "net/quic/platform/api/quic_endian.h" | |
| 12 #include "net/quic/platform/api/quic_flags.h" | 11 #include "net/quic/platform/api/quic_flags.h" |
| 13 #include "net/quic/platform/api/quic_logging.h" | 12 #include "net/quic/platform/api/quic_logging.h" |
| 14 | 13 |
| 15 namespace net { | 14 namespace net { |
| 16 | 15 |
| 17 #define ENDPOINT \ | |
| 18 (perspective_ == Perspective::IS_SERVER ? "Server: " : "Client: ") | |
| 19 | |
| 20 QuicDataWriter::QuicDataWriter(size_t size, | 16 QuicDataWriter::QuicDataWriter(size_t size, |
| 21 char* buffer, | 17 char* buffer, |
| 22 Perspective perspective) | 18 Perspective perspective, |
| 23 : buffer_(buffer), capacity_(size), length_(0), perspective_(perspective) { | 19 Endianness endianness) |
| 24 QUIC_DVLOG(1) << ENDPOINT << "QuicDataReader"; | 20 : buffer_(buffer), |
| 25 } | 21 capacity_(size), |
| 22 length_(0), |
| 23 perspective_(perspective), |
| 24 endianness_(endianness) {} |
| 26 | 25 |
| 27 QuicDataWriter::~QuicDataWriter() {} | 26 QuicDataWriter::~QuicDataWriter() {} |
| 28 | 27 |
| 29 char* QuicDataWriter::data() { | 28 char* QuicDataWriter::data() { |
| 30 return buffer_; | 29 return buffer_; |
| 31 } | 30 } |
| 32 | 31 |
| 33 bool QuicDataWriter::WriteUInt8(uint8_t value) { | 32 bool QuicDataWriter::WriteUInt8(uint8_t value) { |
| 34 return WriteBytes(&value, sizeof(value)); | 33 return WriteBytes(&value, sizeof(value)); |
| 35 } | 34 } |
| 36 | 35 |
| 37 bool QuicDataWriter::WriteUInt16(uint16_t value) { | 36 bool QuicDataWriter::WriteUInt16(uint16_t value) { |
| 37 if (endianness_ == NETWORK_BYTE_ORDER) { |
| 38 value = QuicEndian::HostToNet16(value); |
| 39 } |
| 38 return WriteBytes(&value, sizeof(value)); | 40 return WriteBytes(&value, sizeof(value)); |
| 39 } | 41 } |
| 40 | 42 |
| 41 bool QuicDataWriter::WriteUInt32(uint32_t value) { | 43 bool QuicDataWriter::WriteUInt32(uint32_t value) { |
| 44 if (endianness_ == NETWORK_BYTE_ORDER) { |
| 45 value = QuicEndian::HostToNet32(value); |
| 46 } |
| 42 return WriteBytes(&value, sizeof(value)); | 47 return WriteBytes(&value, sizeof(value)); |
| 43 } | 48 } |
| 44 | 49 |
| 45 bool QuicDataWriter::WriteUInt48(uint64_t value) { | 50 bool QuicDataWriter::WriteUInt64(uint64_t value) { |
| 46 uint16_t hi = static_cast<uint16_t>(value >> 32); | 51 if (endianness_ == NETWORK_BYTE_ORDER) { |
| 47 uint32_t lo = static_cast<uint32_t>(value); | 52 value = QuicEndian::HostToNet64(value); |
| 48 return WriteUInt32(lo) && WriteUInt16(hi); | 53 } |
| 54 return WriteBytes(&value, sizeof(value)); |
| 49 } | 55 } |
| 50 | 56 |
| 51 bool QuicDataWriter::WriteUInt64(uint64_t value) { | 57 bool QuicDataWriter::WriteBytesToUInt64(size_t num_bytes, uint64_t value) { |
| 52 return WriteBytes(&value, sizeof(value)); | 58 if (num_bytes > sizeof(value)) { |
| 59 return false; |
| 60 } |
| 61 if (endianness_ == HOST_BYTE_ORDER) { |
| 62 return WriteBytes(&value, num_bytes); |
| 63 } |
| 64 |
| 65 value = QuicEndian::HostToNet64(value); |
| 66 return WriteBytes(reinterpret_cast<char*>(&value) + sizeof(value) - num_bytes, |
| 67 num_bytes); |
| 53 } | 68 } |
| 54 | 69 |
| 55 bool QuicDataWriter::WriteUFloat16(uint64_t value) { | 70 bool QuicDataWriter::WriteUFloat16(uint64_t value) { |
| 56 uint16_t result; | 71 uint16_t result; |
| 57 if (value < (UINT64_C(1) << kUFloat16MantissaEffectiveBits)) { | 72 if (value < (UINT64_C(1) << kUFloat16MantissaEffectiveBits)) { |
| 58 // Fast path: either the value is denormalized, or has exponent zero. | 73 // Fast path: either the value is denormalized, or has exponent zero. |
| 59 // Both cases are represented by the value itself. | 74 // Both cases are represented by the value itself. |
| 60 result = static_cast<uint16_t>(value); | 75 result = static_cast<uint16_t>(value); |
| 61 } else if (value >= kUFloat16MaxValue) { | 76 } else if (value >= kUFloat16MaxValue) { |
| 62 // Value is out of range; clamp it to the maximum representable. | 77 // Value is out of range; clamp it to the maximum representable. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 81 DCHECK_LE(exponent, kUFloat16MaxExponent); | 96 DCHECK_LE(exponent, kUFloat16MaxExponent); |
| 82 DCHECK_GE(value, UINT64_C(1) << kUFloat16MantissaBits); | 97 DCHECK_GE(value, UINT64_C(1) << kUFloat16MantissaBits); |
| 83 DCHECK_LT(value, UINT64_C(1) << kUFloat16MantissaEffectiveBits); | 98 DCHECK_LT(value, UINT64_C(1) << kUFloat16MantissaEffectiveBits); |
| 84 | 99 |
| 85 // Hidden bit (position 11) is set. We should remove it and increment the | 100 // Hidden bit (position 11) is set. We should remove it and increment the |
| 86 // exponent. Equivalently, we just add it to the exponent. | 101 // exponent. Equivalently, we just add it to the exponent. |
| 87 // This hides the bit. | 102 // This hides the bit. |
| 88 result = static_cast<uint16_t>(value + (exponent << kUFloat16MantissaBits)); | 103 result = static_cast<uint16_t>(value + (exponent << kUFloat16MantissaBits)); |
| 89 } | 104 } |
| 90 | 105 |
| 106 if (endianness_ == NETWORK_BYTE_ORDER) { |
| 107 result = QuicEndian::HostToNet16(result); |
| 108 } |
| 91 return WriteBytes(&result, sizeof(result)); | 109 return WriteBytes(&result, sizeof(result)); |
| 92 } | 110 } |
| 93 | 111 |
| 94 bool QuicDataWriter::WriteStringPiece16(QuicStringPiece val) { | 112 bool QuicDataWriter::WriteStringPiece16(QuicStringPiece val) { |
| 95 if (val.size() > std::numeric_limits<uint16_t>::max()) { | 113 if (val.size() > std::numeric_limits<uint16_t>::max()) { |
| 96 return false; | 114 return false; |
| 97 } | 115 } |
| 98 if (!WriteUInt16(static_cast<uint16_t>(val.size()))) { | 116 if (!WriteUInt16(static_cast<uint16_t>(val.size()))) { |
| 99 return false; | 117 return false; |
| 100 } | 118 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 | 170 |
| 153 bool QuicDataWriter::WritePaddingBytes(size_t count) { | 171 bool QuicDataWriter::WritePaddingBytes(size_t count) { |
| 154 return WriteRepeatedByte(0x00, count); | 172 return WriteRepeatedByte(0x00, count); |
| 155 } | 173 } |
| 156 | 174 |
| 157 bool QuicDataWriter::WriteConnectionId(uint64_t connection_id) { | 175 bool QuicDataWriter::WriteConnectionId(uint64_t connection_id) { |
| 158 if (QuicUtils::IsConnectionIdWireFormatBigEndian(perspective_)) { | 176 if (QuicUtils::IsConnectionIdWireFormatBigEndian(perspective_)) { |
| 159 connection_id = QuicEndian::HostToNet64(connection_id); | 177 connection_id = QuicEndian::HostToNet64(connection_id); |
| 160 } | 178 } |
| 161 | 179 |
| 162 return WriteUInt64(connection_id); | 180 return WriteBytes(&connection_id, sizeof(connection_id)); |
| 163 } | 181 } |
| 164 | 182 |
| 165 bool QuicDataWriter::WriteTag(uint32_t tag) { | 183 bool QuicDataWriter::WriteTag(uint32_t tag) { |
| 166 return WriteBytes(&tag, sizeof(tag)); | 184 return WriteBytes(&tag, sizeof(tag)); |
| 167 } | 185 } |
| 168 | 186 |
| 169 } // namespace net | 187 } // namespace net |
| OLD | NEW |