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.
5 #include "net/quic/quic_data_writer.h"
10 using base::StringPiece
;
11 using std::numeric_limits
;
15 QuicDataWriter::QuicDataWriter(size_t size
, char* buffer
)
16 : buffer_(buffer
), capacity_(size
), length_(0) {
19 QuicDataWriter::~QuicDataWriter() {
22 char* QuicDataWriter::data() {
26 bool QuicDataWriter::WriteUInt8(uint8 value
) {
27 return WriteBytes(&value
, sizeof(value
));
30 bool QuicDataWriter::WriteUInt16(uint16 value
) {
31 return WriteBytes(&value
, sizeof(value
));
34 bool QuicDataWriter::WriteUInt32(uint32 value
) {
35 return WriteBytes(&value
, sizeof(value
));
38 bool QuicDataWriter::WriteUInt48(uint64 value
) {
39 uint16 hi
= static_cast<uint16
>(value
>> 32);
40 uint32 lo
= static_cast<uint32
>(value
);
41 return WriteUInt32(lo
) && WriteUInt16(hi
);
44 bool QuicDataWriter::WriteUInt64(uint64 value
) {
45 return WriteBytes(&value
, sizeof(value
));
48 bool QuicDataWriter::WriteUFloat16(uint64 value
) {
50 if (value
< (GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits
)) {
51 // Fast path: either the value is denormalized, or has exponent zero.
52 // Both cases are represented by the value itself.
53 result
= static_cast<uint16
>(value
);
54 } else if (value
>= kUFloat16MaxValue
) {
55 // Value is out of range; clamp it to the maximum representable.
56 result
= numeric_limits
<uint16
>::max();
58 // The highest bit is between position 13 and 42 (zero-based), which
59 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10,
60 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11
61 // and count the shifts.
63 for (uint16 offset
= 16; offset
> 0; offset
/= 2) {
64 // Right-shift the value until the highest bit is in position 11.
65 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30),
66 // shift if the bit is at or above 11 + offset.
67 if (value
>= (GG_UINT64_C(1) << (kUFloat16MantissaBits
+ offset
))) {
73 DCHECK_GE(exponent
, 1);
74 DCHECK_LE(exponent
, kUFloat16MaxExponent
);
75 DCHECK_GE(value
, GG_UINT64_C(1) << kUFloat16MantissaBits
);
76 DCHECK_LT(value
, GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits
);
78 // Hidden bit (position 11) is set. We should remove it and increment the
79 // exponent. Equivalently, we just add it to the exponent.
80 // This hides the bit.
81 result
= static_cast<uint16
>(value
+ (exponent
<< kUFloat16MantissaBits
));
84 return WriteBytes(&result
, sizeof(result
));
87 bool QuicDataWriter::WriteStringPiece16(StringPiece val
) {
88 if (val
.size() > numeric_limits
<uint16
>::max()) {
91 if (!WriteUInt16(static_cast<uint16
>(val
.size()))) {
94 return WriteBytes(val
.data(), val
.size());
97 bool QuicDataWriter::WriteIOVector(const IOVector
& data
) {
98 char *dest
= BeginWrite(data
.TotalBufferSize());
102 for (size_t i
= 0; i
< data
.Size(); ++i
) {
103 WriteBytes(data
.iovec()[i
].iov_base
, data
.iovec()[i
].iov_len
);
109 char* QuicDataWriter::BeginWrite(size_t length
) {
110 if (length_
> capacity_
) {
114 if (capacity_
- length_
< length
) {
118 #ifdef ARCH_CPU_64_BITS
119 DCHECK_LE(length
, std::numeric_limits
<uint32
>::max());
122 return buffer_
+ length_
;
125 bool QuicDataWriter::WriteBytes(const void* data
, size_t data_len
) {
126 char* dest
= BeginWrite(data_len
);
131 memcpy(dest
, data
, data_len
);
137 bool QuicDataWriter::WriteRepeatedByte(uint8 byte
, size_t count
) {
138 char* dest
= BeginWrite(count
);
143 memset(dest
, byte
, count
);
149 void QuicDataWriter::WritePadding() {
150 DCHECK_LE(length_
, capacity_
);
151 if (length_
> capacity_
) {
154 memset(buffer_
+ length_
, 0x00, capacity_
- length_
);