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::ReadUInt64(uint64
* result
) {
29 return ReadBytes(result
, sizeof(*result
));
32 bool QuicDataReader::ReadUFloat16(uint64
* result
) {
34 if (!ReadUInt16(&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.
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.
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
56 *result
-= exponent
<< kUFloat16MantissaBits
;
58 DCHECK_GE(value
, 1 << kUFloat16MantissaEffectiveBits
);
59 DCHECK_LE(value
, kUFloat16MaxValue
);
63 bool QuicDataReader::ReadStringPiece16(StringPiece
* result
) {
64 // Read resultant length.
66 if (!ReadUInt16(&result_len
)) {
67 // OnFailure() already called.
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.
82 result
->set(data_
+ pos_
, size
);
90 StringPiece
QuicDataReader::ReadRemainingPayload() {
91 StringPiece payload
= PeekRemainingPayload();
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
)) {
108 memcpy(result
, data_
+ pos_
, size
);
116 bool QuicDataReader::IsDoneReading() const {
120 size_t QuicDataReader::BytesRemaining() const {
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