| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/quic/quic_data_writer.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <limits> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/logging.h" | |
| 13 | |
| 14 using base::StringPiece; | |
| 15 using std::numeric_limits; | |
| 16 | |
| 17 namespace net { | |
| 18 | |
| 19 QuicDataWriter::QuicDataWriter(size_t size, char* buffer) | |
| 20 : buffer_(buffer), capacity_(size), length_(0) { | |
| 21 } | |
| 22 | |
| 23 QuicDataWriter::~QuicDataWriter() { | |
| 24 } | |
| 25 | |
| 26 char* QuicDataWriter::data() { | |
| 27 return buffer_; | |
| 28 } | |
| 29 | |
| 30 bool QuicDataWriter::WriteUInt8(uint8 value) { | |
| 31 return WriteBytes(&value, sizeof(value)); | |
| 32 } | |
| 33 | |
| 34 bool QuicDataWriter::WriteUInt16(uint16 value) { | |
| 35 return WriteBytes(&value, sizeof(value)); | |
| 36 } | |
| 37 | |
| 38 bool QuicDataWriter::WriteUInt32(uint32 value) { | |
| 39 return WriteBytes(&value, sizeof(value)); | |
| 40 } | |
| 41 | |
| 42 bool QuicDataWriter::WriteUInt48(uint64 value) { | |
| 43 uint16 hi = static_cast<uint16>(value >> 32); | |
| 44 uint32 lo = static_cast<uint32>(value); | |
| 45 return WriteUInt32(lo) && WriteUInt16(hi); | |
| 46 } | |
| 47 | |
| 48 bool QuicDataWriter::WriteUInt64(uint64 value) { | |
| 49 return WriteBytes(&value, sizeof(value)); | |
| 50 } | |
| 51 | |
| 52 bool QuicDataWriter::WriteUFloat16(uint64 value) { | |
| 53 uint16 result; | |
| 54 if (value < (GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits)) { | |
| 55 // Fast path: either the value is denormalized, or has exponent zero. | |
| 56 // Both cases are represented by the value itself. | |
| 57 result = static_cast<uint16>(value); | |
| 58 } else if (value >= kUFloat16MaxValue) { | |
| 59 // Value is out of range; clamp it to the maximum representable. | |
| 60 result = numeric_limits<uint16>::max(); | |
| 61 } else { | |
| 62 // The highest bit is between position 13 and 42 (zero-based), which | |
| 63 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10, | |
| 64 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11 | |
| 65 // and count the shifts. | |
| 66 uint16 exponent = 0; | |
| 67 for (uint16 offset = 16; offset > 0; offset /= 2) { | |
| 68 // Right-shift the value until the highest bit is in position 11. | |
| 69 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30), | |
| 70 // shift if the bit is at or above 11 + offset. | |
| 71 if (value >= (GG_UINT64_C(1) << (kUFloat16MantissaBits + offset))) { | |
| 72 exponent += offset; | |
| 73 value >>= offset; | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 DCHECK_GE(exponent, 1); | |
| 78 DCHECK_LE(exponent, kUFloat16MaxExponent); | |
| 79 DCHECK_GE(value, GG_UINT64_C(1) << kUFloat16MantissaBits); | |
| 80 DCHECK_LT(value, GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits); | |
| 81 | |
| 82 // Hidden bit (position 11) is set. We should remove it and increment the | |
| 83 // exponent. Equivalently, we just add it to the exponent. | |
| 84 // This hides the bit. | |
| 85 result = static_cast<uint16>(value + (exponent << kUFloat16MantissaBits)); | |
| 86 } | |
| 87 | |
| 88 return WriteBytes(&result, sizeof(result)); | |
| 89 } | |
| 90 | |
| 91 bool QuicDataWriter::WriteStringPiece16(StringPiece val) { | |
| 92 if (val.size() > numeric_limits<uint16>::max()) { | |
| 93 return false; | |
| 94 } | |
| 95 if (!WriteUInt16(static_cast<uint16>(val.size()))) { | |
| 96 return false; | |
| 97 } | |
| 98 return WriteBytes(val.data(), val.size()); | |
| 99 } | |
| 100 | |
| 101 bool QuicDataWriter::WriteIOVector(const IOVector& data) { | |
| 102 char *dest = BeginWrite(data.TotalBufferSize()); | |
| 103 if (!dest) { | |
| 104 return false; | |
| 105 } | |
| 106 for (size_t i = 0; i < data.Size(); ++i) { | |
| 107 WriteBytes(data.iovec()[i].iov_base, data.iovec()[i].iov_len); | |
| 108 } | |
| 109 | |
| 110 return true; | |
| 111 } | |
| 112 | |
| 113 char* QuicDataWriter::BeginWrite(size_t length) { | |
| 114 if (length_ > capacity_) { | |
| 115 return nullptr; | |
| 116 } | |
| 117 | |
| 118 if (capacity_ - length_ < length) { | |
| 119 return nullptr; | |
| 120 } | |
| 121 | |
| 122 #ifdef ARCH_CPU_64_BITS | |
| 123 DCHECK_LE(length, std::numeric_limits<uint32>::max()); | |
| 124 #endif | |
| 125 | |
| 126 return buffer_ + length_; | |
| 127 } | |
| 128 | |
| 129 bool QuicDataWriter::WriteBytes(const void* data, size_t data_len) { | |
| 130 char* dest = BeginWrite(data_len); | |
| 131 if (!dest) { | |
| 132 return false; | |
| 133 } | |
| 134 | |
| 135 memcpy(dest, data, data_len); | |
| 136 | |
| 137 length_ += data_len; | |
| 138 return true; | |
| 139 } | |
| 140 | |
| 141 bool QuicDataWriter::WriteRepeatedByte(uint8 byte, size_t count) { | |
| 142 char* dest = BeginWrite(count); | |
| 143 if (!dest) { | |
| 144 return false; | |
| 145 } | |
| 146 | |
| 147 memset(dest, byte, count); | |
| 148 | |
| 149 length_ += count; | |
| 150 return true; | |
| 151 } | |
| 152 | |
| 153 void QuicDataWriter::WritePadding() { | |
| 154 DCHECK_LE(length_, capacity_); | |
| 155 if (length_ > capacity_) { | |
| 156 return; | |
| 157 } | |
| 158 memset(buffer_ + length_, 0x00, capacity_ - length_); | |
| 159 length_ = capacity_; | |
| 160 } | |
| 161 | |
| 162 bool QuicDataWriter::WriteUInt8ToOffset(uint8 value, size_t offset) { | |
| 163 if (offset >= capacity_) { | |
| 164 LOG(DFATAL) << "offset: " << offset << " >= capacity: " << capacity_; | |
| 165 return false; | |
| 166 } | |
| 167 size_t latched_length = length_; | |
| 168 length_ = offset; | |
| 169 bool success = WriteUInt8(value); | |
| 170 DCHECK_LE(length_, latched_length); | |
| 171 length_ = latched_length; | |
| 172 return success; | |
| 173 } | |
| 174 | |
| 175 bool QuicDataWriter::WriteUInt32ToOffset(uint32 value, size_t offset) { | |
| 176 DCHECK_LT(offset, capacity_); | |
| 177 size_t latched_length = length_; | |
| 178 length_ = offset; | |
| 179 bool success = WriteUInt32(value); | |
| 180 DCHECK_LE(length_, latched_length); | |
| 181 length_ = latched_length; | |
| 182 return success; | |
| 183 } | |
| 184 | |
| 185 bool QuicDataWriter::WriteUInt48ToOffset(uint64 value, size_t offset) { | |
| 186 DCHECK_LT(offset, capacity_); | |
| 187 size_t latched_length = length_; | |
| 188 length_ = offset; | |
| 189 bool success = WriteUInt48(value); | |
| 190 DCHECK_LE(length_, latched_length); | |
| 191 length_ = latched_length; | |
| 192 return success; | |
| 193 } | |
| 194 | |
| 195 } // namespace net | |
| OLD | NEW |