Perf: Update size expectations on linux-rel
[chromium-blink-merge.git] / net / quic / quic_data_reader.cc
blobb9a6acf32f5b28c442caf18ba17d93ebc912be8f
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_reader.h"
7 #include "net/base/int128.h"
8 #include "net/quic/quic_protocol.h"
10 using base::StringPiece;
12 namespace net {
14 QuicDataReader::QuicDataReader(const char* data, const size_t len)
15 : data_(data),
16 len_(len),
17 pos_(0) {
20 bool QuicDataReader::ReadUInt16(uint16* result) {
21 return ReadBytes(result, sizeof(*result));
24 bool QuicDataReader::ReadUInt32(uint32* result) {
25 return ReadBytes(result, sizeof(*result));
28 bool QuicDataReader::ReadUInt48(uint64* result) {
29 uint32 lo;
30 if (!ReadUInt32(&lo)) {
31 return false;
34 uint16 hi;
35 if (!ReadUInt16(&hi)) {
36 return false;
39 *result = hi;
40 *result <<= 32;
41 *result += lo;
43 return true;
46 bool QuicDataReader::ReadUInt64(uint64* result) {
47 return ReadBytes(result, sizeof(*result));
50 bool QuicDataReader::ReadUInt128(uint128* result) {
51 uint64 high_hash;
52 uint64 low_hash;
54 if (!ReadUInt64(&low_hash)) {
55 return false;
57 if (!ReadUInt64(&high_hash)) {
58 return false;
61 *result = uint128(high_hash, low_hash);
62 return true;
65 bool QuicDataReader::ReadUFloat16(uint64* result) {
66 uint16 value;
67 if (!ReadUInt16(&value)) {
68 return false;
71 *result = value;
72 if (*result < (1 << kUFloat16MantissaEffectiveBits)) {
73 // Fast path: either the value is denormalized (no hidden bit), or
74 // normalized (hidden bit set, exponent offset by one) with exponent zero.
75 // Zero exponent offset by one sets the bit exactly where the hidden bit is.
76 // So in both cases the value encodes itself.
77 return true;
80 uint16 exponent = value >> kUFloat16MantissaBits; // No sign extend on uint!
81 // After the fast pass, the exponent is at least one (offset by one).
82 // Un-offset the exponent.
83 --exponent;
84 DCHECK_GE(exponent, 1);
85 DCHECK_LE(exponent, kUFloat16MaxExponent);
86 // Here we need to clear the exponent and set the hidden bit. We have already
87 // decremented the exponent, so when we subtract it, it leaves behind the
88 // hidden bit.
89 *result -= exponent << kUFloat16MantissaBits;
90 *result <<= exponent;
91 DCHECK_GE(value, 1 << kUFloat16MantissaEffectiveBits);
92 DCHECK_LE(value, kUFloat16MaxValue);
93 return true;
96 bool QuicDataReader::ReadStringPiece16(StringPiece* result) {
97 // Read resultant length.
98 uint16 result_len;
99 if (!ReadUInt16(&result_len)) {
100 // OnFailure() already called.
101 return false;
104 return ReadStringPiece(result, result_len);
107 bool QuicDataReader::ReadStringPiece(StringPiece* result, size_t size) {
108 // Make sure that we have enough data to read.
109 if (!CanRead(size)) {
110 OnFailure();
111 return false;
114 // Set result.
115 result->set(data_ + pos_, size);
117 // Iterate.
118 pos_ += size;
120 return true;
123 StringPiece QuicDataReader::ReadRemainingPayload() {
124 StringPiece payload = PeekRemainingPayload();
125 pos_ = len_;
126 return payload;
129 StringPiece QuicDataReader::PeekRemainingPayload() {
130 return StringPiece(data_ + pos_, len_ - pos_);
133 bool QuicDataReader::ReadBytes(void* result, size_t size) {
134 // Make sure that we have enough data to read.
135 if (!CanRead(size)) {
136 OnFailure();
137 return false;
140 // Read into result.
141 memcpy(result, data_ + pos_, size);
143 // Iterate.
144 pos_ += size;
146 return true;
149 bool QuicDataReader::IsDoneReading() const {
150 return len_ == pos_;
153 size_t QuicDataReader::BytesRemaining() const {
154 return len_ - pos_;
157 bool QuicDataReader::CanRead(size_t bytes) const {
158 return bytes <= (len_ - pos_);
161 void QuicDataReader::OnFailure() {
162 // Set our iterator to the end of the buffer so that further reads fail
163 // immediately.
164 pos_ = len_;
167 } // namespace net