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
;
14 QuicDataReader::QuicDataReader(const char* data
, const size_t len
)
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
) {
30 if (!ReadUInt32(&lo
)) {
35 if (!ReadUInt16(&hi
)) {
46 bool QuicDataReader::ReadUInt64(uint64
* result
) {
47 return ReadBytes(result
, sizeof(*result
));
50 bool QuicDataReader::ReadUInt128(uint128
* result
) {
54 if (!ReadUInt64(&low_hash
)) {
57 if (!ReadUInt64(&high_hash
)) {
61 *result
= uint128(high_hash
, low_hash
);
65 bool QuicDataReader::ReadUFloat16(uint64
* result
) {
67 if (!ReadUInt16(&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.
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.
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
89 *result
-= exponent
<< kUFloat16MantissaBits
;
91 DCHECK_GE(value
, 1 << kUFloat16MantissaEffectiveBits
);
92 DCHECK_LE(value
, kUFloat16MaxValue
);
96 bool QuicDataReader::ReadStringPiece16(StringPiece
* result
) {
97 // Read resultant length.
99 if (!ReadUInt16(&result_len
)) {
100 // OnFailure() already called.
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
)) {
115 result
->set(data_
+ pos_
, size
);
123 StringPiece
QuicDataReader::ReadRemainingPayload() {
124 StringPiece payload
= PeekRemainingPayload();
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
)) {
141 memcpy(result
, data_
+ pos_
, size
);
149 bool QuicDataReader::IsDoneReading() const {
153 size_t QuicDataReader::BytesRemaining() const {
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