Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / quic / quic_data_writer.cc
blob49257829389d066beceadfe6aa82fd1cb4c59e77
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"
7 #include <stdint.h>
8 #include <algorithm>
9 #include <limits>
11 using base::StringPiece;
12 using std::numeric_limits;
14 namespace net {
16 QuicDataWriter::QuicDataWriter(size_t size, char* buffer)
17 : buffer_(buffer), capacity_(size), length_(0) {
20 QuicDataWriter::~QuicDataWriter() {
23 char* QuicDataWriter::data() {
24 return buffer_;
27 bool QuicDataWriter::WriteUInt8(uint8 value) {
28 return WriteBytes(&value, sizeof(value));
31 bool QuicDataWriter::WriteUInt16(uint16 value) {
32 return WriteBytes(&value, sizeof(value));
35 bool QuicDataWriter::WriteUInt32(uint32 value) {
36 return WriteBytes(&value, sizeof(value));
39 bool QuicDataWriter::WriteUInt48(uint64 value) {
40 uint16 hi = static_cast<uint16>(value >> 32);
41 uint32 lo = static_cast<uint32>(value);
42 return WriteUInt32(lo) && WriteUInt16(hi);
45 bool QuicDataWriter::WriteUInt64(uint64 value) {
46 return WriteBytes(&value, sizeof(value));
49 bool QuicDataWriter::WriteUFloat16(uint64 value) {
50 uint16 result;
51 if (value < (UINT64_C(1) << kUFloat16MantissaEffectiveBits)) {
52 // Fast path: either the value is denormalized, or has exponent zero.
53 // Both cases are represented by the value itself.
54 result = static_cast<uint16>(value);
55 } else if (value >= kUFloat16MaxValue) {
56 // Value is out of range; clamp it to the maximum representable.
57 result = numeric_limits<uint16>::max();
58 } else {
59 // The highest bit is between position 13 and 42 (zero-based), which
60 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10,
61 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11
62 // and count the shifts.
63 uint16 exponent = 0;
64 for (uint16 offset = 16; offset > 0; offset /= 2) {
65 // Right-shift the value until the highest bit is in position 11.
66 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30),
67 // shift if the bit is at or above 11 + offset.
68 if (value >= (UINT64_C(1) << (kUFloat16MantissaBits + offset))) {
69 exponent += offset;
70 value >>= offset;
74 DCHECK_GE(exponent, 1);
75 DCHECK_LE(exponent, kUFloat16MaxExponent);
76 DCHECK_GE(value, UINT64_C(1) << kUFloat16MantissaBits);
77 DCHECK_LT(value, UINT64_C(1) << kUFloat16MantissaEffectiveBits);
79 // Hidden bit (position 11) is set. We should remove it and increment the
80 // exponent. Equivalently, we just add it to the exponent.
81 // This hides the bit.
82 result = static_cast<uint16>(value + (exponent << kUFloat16MantissaBits));
85 return WriteBytes(&result, sizeof(result));
88 bool QuicDataWriter::WriteStringPiece16(StringPiece val) {
89 if (val.size() > numeric_limits<uint16>::max()) {
90 return false;
92 if (!WriteUInt16(static_cast<uint16>(val.size()))) {
93 return false;
95 return WriteBytes(val.data(), val.size());
98 char* QuicDataWriter::BeginWrite(size_t length) {
99 if (length_ > capacity_) {
100 return nullptr;
103 if (capacity_ - length_ < length) {
104 return nullptr;
107 #ifdef ARCH_CPU_64_BITS
108 DCHECK_LE(length, std::numeric_limits<uint32>::max());
109 #endif
111 return buffer_ + length_;
114 bool QuicDataWriter::WriteBytes(const void* data, size_t data_len) {
115 char* dest = BeginWrite(data_len);
116 if (!dest) {
117 return false;
120 memcpy(dest, data, data_len);
122 length_ += data_len;
123 return true;
126 bool QuicDataWriter::WriteRepeatedByte(uint8 byte, size_t count) {
127 char* dest = BeginWrite(count);
128 if (!dest) {
129 return false;
132 memset(dest, byte, count);
134 length_ += count;
135 return true;
138 void QuicDataWriter::WritePadding() {
139 DCHECK_LE(length_, capacity_);
140 if (length_ > capacity_) {
141 return;
143 memset(buffer_ + length_, 0x00, capacity_ - length_);
144 length_ = capacity_;
148 } // namespace net