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 <stdint.h> | |
8 #include <algorithm> | |
9 #include <limits> | |
10 | |
11 using base::StringPiece; | |
12 using std::numeric_limits; | |
13 | |
14 namespace net { | |
15 | |
16 QuicDataWriter::QuicDataWriter(size_t size, char* buffer) | |
17 : buffer_(buffer), capacity_(size), length_(0) {} | |
18 | |
19 QuicDataWriter::~QuicDataWriter() {} | |
20 | |
21 char* QuicDataWriter::data() { | |
22 return buffer_; | |
23 } | |
24 | |
25 bool QuicDataWriter::WriteUInt8(uint8_t value) { | |
26 return WriteBytes(&value, sizeof(value)); | |
27 } | |
28 | |
29 bool QuicDataWriter::WriteUInt16(uint16_t value) { | |
30 return WriteBytes(&value, sizeof(value)); | |
31 } | |
32 | |
33 bool QuicDataWriter::WriteUInt32(uint32_t value) { | |
34 return WriteBytes(&value, sizeof(value)); | |
35 } | |
36 | |
37 bool QuicDataWriter::WriteUInt48(uint64_t value) { | |
38 uint16_t hi = static_cast<uint16_t>(value >> 32); | |
39 uint32_t lo = static_cast<uint32_t>(value); | |
40 return WriteUInt32(lo) && WriteUInt16(hi); | |
41 } | |
42 | |
43 bool QuicDataWriter::WriteUInt64(uint64_t value) { | |
44 return WriteBytes(&value, sizeof(value)); | |
45 } | |
46 | |
47 bool QuicDataWriter::WriteUFloat16(uint64_t value) { | |
48 uint16_t result; | |
49 if (value < (UINT64_C(1) << kUFloat16MantissaEffectiveBits)) { | |
50 // Fast path: either the value is denormalized, or has exponent zero. | |
51 // Both cases are represented by the value itself. | |
52 result = static_cast<uint16_t>(value); | |
53 } else if (value >= kUFloat16MaxValue) { | |
54 // Value is out of range; clamp it to the maximum representable. | |
55 result = numeric_limits<uint16_t>::max(); | |
56 } else { | |
57 // 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, | |
59 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11 | |
60 // and count the shifts. | |
61 uint16_t exponent = 0; | |
62 for (uint16_t offset = 16; offset > 0; offset /= 2) { | |
63 // 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), | |
65 // shift if the bit is at or above 11 + offset. | |
66 if (value >= (UINT64_C(1) << (kUFloat16MantissaBits + offset))) { | |
67 exponent += offset; | |
68 value >>= offset; | |
69 } | |
70 } | |
71 | |
72 DCHECK_GE(exponent, 1); | |
73 DCHECK_LE(exponent, kUFloat16MaxExponent); | |
74 DCHECK_GE(value, UINT64_C(1) << kUFloat16MantissaBits); | |
75 DCHECK_LT(value, UINT64_C(1) << kUFloat16MantissaEffectiveBits); | |
76 | |
77 // Hidden bit (position 11) is set. We should remove it and increment the | |
78 // exponent. Equivalently, we just add it to the exponent. | |
79 // This hides the bit. | |
80 result = static_cast<uint16_t>(value + (exponent << kUFloat16MantissaBits)); | |
81 } | |
82 | |
83 return WriteBytes(&result, sizeof(result)); | |
84 } | |
85 | |
86 bool QuicDataWriter::WriteStringPiece16(StringPiece val) { | |
87 if (val.size() > numeric_limits<uint16_t>::max()) { | |
88 return false; | |
89 } | |
90 if (!WriteUInt16(static_cast<uint16_t>(val.size()))) { | |
91 return false; | |
92 } | |
93 return WriteBytes(val.data(), val.size()); | |
94 } | |
95 | |
96 char* QuicDataWriter::BeginWrite(size_t length) { | |
97 if (length_ > capacity_) { | |
98 return nullptr; | |
99 } | |
100 | |
101 if (capacity_ - length_ < length) { | |
102 return nullptr; | |
103 } | |
104 | |
105 #ifdef ARCH_CPU_64_BITS | |
106 DCHECK_LE(length, std::numeric_limits<uint32_t>::max()); | |
107 #endif | |
108 | |
109 return buffer_ + length_; | |
110 } | |
111 | |
112 bool QuicDataWriter::WriteBytes(const void* data, size_t data_len) { | |
113 char* dest = BeginWrite(data_len); | |
114 if (!dest) { | |
115 return false; | |
116 } | |
117 | |
118 memcpy(dest, data, data_len); | |
119 | |
120 length_ += data_len; | |
121 return true; | |
122 } | |
123 | |
124 bool QuicDataWriter::WriteRepeatedByte(uint8_t byte, size_t count) { | |
125 char* dest = BeginWrite(count); | |
126 if (!dest) { | |
127 return false; | |
128 } | |
129 | |
130 memset(dest, byte, count); | |
131 | |
132 length_ += count; | |
133 return true; | |
134 } | |
135 | |
136 void QuicDataWriter::WritePadding() { | |
137 DCHECK_LE(length_, capacity_); | |
138 if (length_ > capacity_) { | |
139 return; | |
140 } | |
141 memset(buffer_ + length_, 0x00, capacity_ - length_); | |
142 length_ = capacity_; | |
143 } | |
144 | |
145 } // namespace net | |
OLD | NEW |