| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| 11 using base::StringPiece; | 11 using base::StringPiece; |
| 12 using std::numeric_limits; | |
| 13 | 12 |
| 14 namespace net { | 13 namespace net { |
| 15 | 14 |
| 16 QuicDataWriter::QuicDataWriter(size_t size, char* buffer) | 15 QuicDataWriter::QuicDataWriter(size_t size, char* buffer) |
| 17 : buffer_(buffer), capacity_(size), length_(0) {} | 16 : buffer_(buffer), capacity_(size), length_(0) {} |
| 18 | 17 |
| 19 QuicDataWriter::~QuicDataWriter() {} | 18 QuicDataWriter::~QuicDataWriter() {} |
| 20 | 19 |
| 21 char* QuicDataWriter::data() { | 20 char* QuicDataWriter::data() { |
| 22 return buffer_; | 21 return buffer_; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 45 } | 44 } |
| 46 | 45 |
| 47 bool QuicDataWriter::WriteUFloat16(uint64_t value) { | 46 bool QuicDataWriter::WriteUFloat16(uint64_t value) { |
| 48 uint16_t result; | 47 uint16_t result; |
| 49 if (value < (UINT64_C(1) << kUFloat16MantissaEffectiveBits)) { | 48 if (value < (UINT64_C(1) << kUFloat16MantissaEffectiveBits)) { |
| 50 // Fast path: either the value is denormalized, or has exponent zero. | 49 // Fast path: either the value is denormalized, or has exponent zero. |
| 51 // Both cases are represented by the value itself. | 50 // Both cases are represented by the value itself. |
| 52 result = static_cast<uint16_t>(value); | 51 result = static_cast<uint16_t>(value); |
| 53 } else if (value >= kUFloat16MaxValue) { | 52 } else if (value >= kUFloat16MaxValue) { |
| 54 // Value is out of range; clamp it to the maximum representable. | 53 // Value is out of range; clamp it to the maximum representable. |
| 55 result = numeric_limits<uint16_t>::max(); | 54 result = std::numeric_limits<uint16_t>::max(); |
| 56 } else { | 55 } else { |
| 57 // The highest bit is between position 13 and 42 (zero-based), which | 56 // The highest bit is between position 13 and 42 (zero-based), which |
| 58 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10, | 57 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10, |
| 59 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11 | 58 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11 |
| 60 // and count the shifts. | 59 // and count the shifts. |
| 61 uint16_t exponent = 0; | 60 uint16_t exponent = 0; |
| 62 for (uint16_t offset = 16; offset > 0; offset /= 2) { | 61 for (uint16_t offset = 16; offset > 0; offset /= 2) { |
| 63 // Right-shift the value until the highest bit is in position 11. | 62 // Right-shift the value until the highest bit is in position 11. |
| 64 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30), | 63 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30), |
| 65 // shift if the bit is at or above 11 + offset. | 64 // shift if the bit is at or above 11 + offset. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 77 // Hidden bit (position 11) is set. We should remove it and increment the | 76 // Hidden bit (position 11) is set. We should remove it and increment the |
| 78 // exponent. Equivalently, we just add it to the exponent. | 77 // exponent. Equivalently, we just add it to the exponent. |
| 79 // This hides the bit. | 78 // This hides the bit. |
| 80 result = static_cast<uint16_t>(value + (exponent << kUFloat16MantissaBits)); | 79 result = static_cast<uint16_t>(value + (exponent << kUFloat16MantissaBits)); |
| 81 } | 80 } |
| 82 | 81 |
| 83 return WriteBytes(&result, sizeof(result)); | 82 return WriteBytes(&result, sizeof(result)); |
| 84 } | 83 } |
| 85 | 84 |
| 86 bool QuicDataWriter::WriteStringPiece16(StringPiece val) { | 85 bool QuicDataWriter::WriteStringPiece16(StringPiece val) { |
| 87 if (val.size() > numeric_limits<uint16_t>::max()) { | 86 if (val.size() > std::numeric_limits<uint16_t>::max()) { |
| 88 return false; | 87 return false; |
| 89 } | 88 } |
| 90 if (!WriteUInt16(static_cast<uint16_t>(val.size()))) { | 89 if (!WriteUInt16(static_cast<uint16_t>(val.size()))) { |
| 91 return false; | 90 return false; |
| 92 } | 91 } |
| 93 return WriteBytes(val.data(), val.size()); | 92 return WriteBytes(val.data(), val.size()); |
| 94 } | 93 } |
| 95 | 94 |
| 96 char* QuicDataWriter::BeginWrite(size_t length) { | 95 char* QuicDataWriter::BeginWrite(size_t length) { |
| 97 if (length_ > capacity_) { | 96 if (length_ > capacity_) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 void QuicDataWriter::WritePadding() { | 135 void QuicDataWriter::WritePadding() { |
| 137 DCHECK_LE(length_, capacity_); | 136 DCHECK_LE(length_, capacity_); |
| 138 if (length_ > capacity_) { | 137 if (length_ > capacity_) { |
| 139 return; | 138 return; |
| 140 } | 139 } |
| 141 memset(buffer_ + length_, 0x00, capacity_ - length_); | 140 memset(buffer_ + length_, 0x00, capacity_ - length_); |
| 142 length_ = capacity_; | 141 length_ = capacity_; |
| 143 } | 142 } |
| 144 | 143 |
| 145 } // namespace net | 144 } // namespace net |
| OLD | NEW |