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"
11 #include "base/basictypes.h"
12 #include "base/logging.h"
14 using base::StringPiece
;
15 using std::numeric_limits
;
19 QuicDataWriter::QuicDataWriter(size_t size
, char* buffer
)
20 : buffer_(buffer
), capacity_(size
), length_(0) {
23 QuicDataWriter::~QuicDataWriter() {
26 char* QuicDataWriter::data() {
30 bool QuicDataWriter::WriteUInt8(uint8 value
) {
31 return WriteBytes(&value
, sizeof(value
));
34 bool QuicDataWriter::WriteUInt16(uint16 value
) {
35 return WriteBytes(&value
, sizeof(value
));
38 bool QuicDataWriter::WriteUInt32(uint32 value
) {
39 return WriteBytes(&value
, sizeof(value
));
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
);
48 bool QuicDataWriter::WriteUInt64(uint64 value
) {
49 return WriteBytes(&value
, sizeof(value
));
52 bool QuicDataWriter::WriteUFloat16(uint64 value
) {
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();
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.
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
))) {
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
);
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
));
88 return WriteBytes(&result
, sizeof(result
));
91 bool QuicDataWriter::WriteStringPiece16(StringPiece val
) {
92 if (val
.size() > numeric_limits
<uint16
>::max()) {
95 if (!WriteUInt16(static_cast<uint16
>(val
.size()))) {
98 return WriteBytes(val
.data(), val
.size());
101 bool QuicDataWriter::WriteIOVector(const IOVector
& data
) {
102 char *dest
= BeginWrite(data
.TotalBufferSize());
106 for (size_t i
= 0; i
< data
.Size(); ++i
) {
107 WriteBytes(data
.iovec()[i
].iov_base
, data
.iovec()[i
].iov_len
);
113 char* QuicDataWriter::BeginWrite(size_t length
) {
114 if (length_
> capacity_
) {
118 if (capacity_
- length_
< length
) {
122 #ifdef ARCH_CPU_64_BITS
123 DCHECK_LE(length
, std::numeric_limits
<uint32
>::max());
126 return buffer_
+ length_
;
129 bool QuicDataWriter::WriteBytes(const void* data
, size_t data_len
) {
130 char* dest
= BeginWrite(data_len
);
135 memcpy(dest
, data
, data_len
);
141 bool QuicDataWriter::WriteRepeatedByte(uint8 byte
, size_t count
) {
142 char* dest
= BeginWrite(count
);
147 memset(dest
, byte
, count
);
153 void QuicDataWriter::WritePadding() {
154 DCHECK_LE(length_
, capacity_
);
155 if (length_
> capacity_
) {
158 memset(buffer_
+ length_
, 0x00, capacity_
- length_
);