Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / net / quic / quic_data_reader.cc
blob7542c8311398143b9c49679f3d695b211df61c07
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::ReadUInt64(uint64* result) {
29 return ReadBytes(result, sizeof(*result));
32 bool QuicDataReader::ReadUFloat16(uint64* result) {
33 uint16 value;
34 if (!ReadUInt16(&value)) {
35 return false;
38 *result = value;
39 if (*result < (1 << kUFloat16MantissaEffectiveBits)) {
40 // Fast path: either the value is denormalized (no hidden bit), or
41 // normalized (hidden bit set, exponent offset by one) with exponent zero.
42 // Zero exponent offset by one sets the bit exactly where the hidden bit is.
43 // So in both cases the value encodes itself.
44 return true;
47 uint16 exponent = value >> kUFloat16MantissaBits; // No sign extend on uint!
48 // After the fast pass, the exponent is at least one (offset by one).
49 // Un-offset the exponent.
50 --exponent;
51 DCHECK_GE(exponent, 1);
52 DCHECK_LE(exponent, kUFloat16MaxExponent);
53 // Here we need to clear the exponent and set the hidden bit. We have already
54 // decremented the exponent, so when we subtract it, it leaves behind the
55 // hidden bit.
56 *result -= exponent << kUFloat16MantissaBits;
57 *result <<= exponent;
58 DCHECK_GE(value, 1 << kUFloat16MantissaEffectiveBits);
59 DCHECK_LE(value, kUFloat16MaxValue);
60 return true;
63 bool QuicDataReader::ReadStringPiece16(StringPiece* result) {
64 // Read resultant length.
65 uint16 result_len;
66 if (!ReadUInt16(&result_len)) {
67 // OnFailure() already called.
68 return false;
71 return ReadStringPiece(result, result_len);
74 bool QuicDataReader::ReadStringPiece(StringPiece* result, size_t size) {
75 // Make sure that we have enough data to read.
76 if (!CanRead(size)) {
77 OnFailure();
78 return false;
81 // Set result.
82 result->set(data_ + pos_, size);
84 // Iterate.
85 pos_ += size;
87 return true;
90 StringPiece QuicDataReader::ReadRemainingPayload() {
91 StringPiece payload = PeekRemainingPayload();
92 pos_ = len_;
93 return payload;
96 StringPiece QuicDataReader::PeekRemainingPayload() {
97 return StringPiece(data_ + pos_, len_ - pos_);
100 bool QuicDataReader::ReadBytes(void* result, size_t size) {
101 // Make sure that we have enough data to read.
102 if (!CanRead(size)) {
103 OnFailure();
104 return false;
107 // Read into result.
108 memcpy(result, data_ + pos_, size);
110 // Iterate.
111 pos_ += size;
113 return true;
116 bool QuicDataReader::IsDoneReading() const {
117 return len_ == pos_;
120 size_t QuicDataReader::BytesRemaining() const {
121 return len_ - pos_;
124 bool QuicDataReader::CanRead(size_t bytes) const {
125 return bytes <= (len_ - pos_);
128 void QuicDataReader::OnFailure() {
129 // Set our iterator to the end of the buffer so that further reads fail
130 // immediately.
131 pos_ = len_;
134 } // namespace net