Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: net/quic/quic_data_reader.cc

Issue 2193073003: Move shared files in net/quic/ into net/quic/core/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: io_thread_unittest.cc Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/quic/quic_data_reader.h ('k') | net/quic/quic_data_writer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_reader.h"
6
7 #include "net/base/int128.h"
8 #include "net/quic/quic_protocol.h"
9
10 using base::StringPiece;
11
12 namespace net {
13
14 QuicDataReader::QuicDataReader(const char* data, const size_t len)
15 : data_(data), len_(len), pos_(0) {}
16
17 bool QuicDataReader::ReadUInt16(uint16_t* result) {
18 return ReadBytes(result, sizeof(*result));
19 }
20
21 bool QuicDataReader::ReadUInt32(uint32_t* result) {
22 return ReadBytes(result, sizeof(*result));
23 }
24
25 bool QuicDataReader::ReadUInt64(uint64_t* result) {
26 return ReadBytes(result, sizeof(*result));
27 }
28
29 bool QuicDataReader::ReadUFloat16(uint64_t* result) {
30 uint16_t value;
31 if (!ReadUInt16(&value)) {
32 return false;
33 }
34
35 *result = value;
36 if (*result < (1 << kUFloat16MantissaEffectiveBits)) {
37 // Fast path: either the value is denormalized (no hidden bit), or
38 // normalized (hidden bit set, exponent offset by one) with exponent zero.
39 // Zero exponent offset by one sets the bit exactly where the hidden bit is.
40 // So in both cases the value encodes itself.
41 return true;
42 }
43
44 uint16_t exponent =
45 value >> kUFloat16MantissaBits; // No sign extend on uint!
46 // After the fast pass, the exponent is at least one (offset by one).
47 // Un-offset the exponent.
48 --exponent;
49 DCHECK_GE(exponent, 1);
50 DCHECK_LE(exponent, kUFloat16MaxExponent);
51 // Here we need to clear the exponent and set the hidden bit. We have already
52 // decremented the exponent, so when we subtract it, it leaves behind the
53 // hidden bit.
54 *result -= exponent << kUFloat16MantissaBits;
55 *result <<= exponent;
56 DCHECK_GE(*result,
57 static_cast<uint64_t>(1 << kUFloat16MantissaEffectiveBits));
58 DCHECK_LE(*result, kUFloat16MaxValue);
59 return true;
60 }
61
62 bool QuicDataReader::ReadStringPiece16(StringPiece* result) {
63 // Read resultant length.
64 uint16_t result_len;
65 if (!ReadUInt16(&result_len)) {
66 // OnFailure() already called.
67 return false;
68 }
69
70 return ReadStringPiece(result, result_len);
71 }
72
73 bool QuicDataReader::ReadStringPiece(StringPiece* result, size_t size) {
74 // Make sure that we have enough data to read.
75 if (!CanRead(size)) {
76 OnFailure();
77 return false;
78 }
79
80 // Set result.
81 result->set(data_ + pos_, size);
82
83 // Iterate.
84 pos_ += size;
85
86 return true;
87 }
88
89 StringPiece QuicDataReader::ReadRemainingPayload() {
90 StringPiece payload = PeekRemainingPayload();
91 pos_ = len_;
92 return payload;
93 }
94
95 StringPiece QuicDataReader::PeekRemainingPayload() {
96 return StringPiece(data_ + pos_, len_ - pos_);
97 }
98
99 bool QuicDataReader::ReadBytes(void* result, size_t size) {
100 // Make sure that we have enough data to read.
101 if (!CanRead(size)) {
102 OnFailure();
103 return false;
104 }
105
106 // Read into result.
107 memcpy(result, data_ + pos_, size);
108
109 // Iterate.
110 pos_ += size;
111
112 return true;
113 }
114
115 bool QuicDataReader::IsDoneReading() const {
116 return len_ == pos_;
117 }
118
119 size_t QuicDataReader::BytesRemaining() const {
120 return len_ - pos_;
121 }
122
123 bool QuicDataReader::CanRead(size_t bytes) const {
124 return bytes <= (len_ - pos_);
125 }
126
127 void QuicDataReader::OnFailure() {
128 // Set our iterator to the end of the buffer so that further reads fail
129 // immediately.
130 pos_ = len_;
131 }
132
133 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/quic_data_reader.h ('k') | net/quic/quic_data_writer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698