Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 using base::StringPiece; | |
| 8 | |
| 9 namespace net { | |
| 10 | |
| 11 QuicDataReader::QuicDataReader(const char* data, const size_t len) | |
| 12 : data_(data), | |
| 13 len_(len), | |
| 14 pos_(0) { | |
| 15 } | |
| 16 | |
| 17 bool QuicDataReader::ReadUInt16(uint16* result) { | |
| 18 // Make sure that we have the whole uint16. | |
| 19 if (!CanRead(2)) { | |
|
jar (doing other things)
2012/10/14 23:04:38
nit: probably better is to use:
CanRead(sizeof(*re
Ryan Hamilton
2012/10/15 21:22:08
Added TODO. Will address in a follow on CL once t
| |
| 20 OnFailure(); | |
| 21 return false; | |
| 22 } | |
| 23 | |
| 24 // Read into result. | |
| 25 *result = *reinterpret_cast<const uint16*>(data_ + pos_); | |
| 26 | |
| 27 // Iterate. | |
| 28 pos_ += 2; | |
| 29 | |
| 30 return true; | |
| 31 } | |
| 32 | |
| 33 bool QuicDataReader::ReadUInt32(uint32* result) { | |
| 34 // Make sure that we have the whole uint32. | |
| 35 if (!CanRead(4)) { | |
| 36 OnFailure(); | |
| 37 return false; | |
| 38 } | |
| 39 | |
| 40 // Read into result. | |
| 41 *result = *reinterpret_cast<const uint32*>(data_ + pos_); | |
| 42 | |
| 43 // Iterate. | |
| 44 pos_ += 4; | |
| 45 | |
| 46 return true; | |
| 47 } | |
| 48 | |
| 49 bool QuicDataReader::ReadUInt48(uint64* result) { | |
| 50 // Make sure that we have the whole uint48. | |
|
jar (doing other things)
2012/10/14 23:04:38
nit: you don't need lines 51-54, since the other r
Ryan Hamilton
2012/10/15 21:22:08
Done.
| |
| 51 if (!CanRead(6)) { | |
| 52 OnFailure(); | |
| 53 return false; | |
| 54 } | |
| 55 | |
| 56 uint32 lo; | |
| 57 if (!ReadUInt32(&lo)) { | |
| 58 return false; | |
| 59 } | |
| 60 | |
| 61 uint16 hi; | |
| 62 if (!ReadUInt16(&hi)) { | |
| 63 return false; | |
| 64 } | |
| 65 | |
| 66 *result = hi; | |
| 67 *result <<= 32; | |
| 68 *result += lo; | |
| 69 | |
| 70 return true; | |
| 71 } | |
| 72 | |
| 73 bool QuicDataReader::ReadUInt64(uint64* result) { | |
| 74 // Make sure that we have the whole uint64. | |
| 75 if (!CanRead(8)) { | |
| 76 OnFailure(); | |
| 77 return false; | |
| 78 } | |
| 79 | |
| 80 // Read into result. | |
| 81 LOG(INFO) << "here"; | |
|
jar (doing other things)
2012/10/14 23:04:38
nit: DLOG?
Ryan Hamilton
2012/10/15 21:22:08
Part of my on-going debugging. will remove.
| |
| 82 *result = *reinterpret_cast<const uint64*>(data_ + pos_); | |
| 83 LOG(INFO) << "here"; | |
| 84 | |
| 85 // Iterate. | |
| 86 pos_ += 8; | |
| 87 | |
| 88 return true; | |
| 89 } | |
| 90 | |
| 91 bool QuicDataReader::ReadUInt128(uint128* result) { | |
| 92 uint64 high_hash; | |
| 93 uint64 low_hash; | |
| 94 | |
| 95 if (!ReadUInt64(&low_hash)) { | |
| 96 return false; | |
| 97 } | |
| 98 if (!ReadUInt64(&high_hash)) { | |
| 99 return false; | |
| 100 } | |
| 101 | |
| 102 *result = uint128(high_hash, low_hash); | |
| 103 return true; | |
| 104 } | |
| 105 | |
| 106 bool QuicDataReader::ReadStringPiece16(StringPiece* result) { | |
| 107 // Read resultant length. | |
| 108 uint16 result_len; | |
| 109 if (!ReadUInt16(&result_len)) { | |
| 110 // OnFailure() already called. | |
| 111 return false; | |
| 112 } | |
| 113 | |
| 114 return ReadStringPiece(result, result_len); | |
| 115 } | |
| 116 | |
| 117 bool QuicDataReader::ReadBytes(void* result, size_t size) { | |
| 118 // Make sure that we have enough data to read. | |
| 119 if (!CanRead(size)) { | |
| 120 OnFailure(); | |
| 121 return false; | |
| 122 } | |
| 123 | |
| 124 // Read into result. | |
| 125 memcpy(result, data_ + pos_, size); | |
| 126 | |
| 127 // Iterate. | |
| 128 pos_ += size; | |
| 129 | |
| 130 return true; | |
| 131 } | |
| 132 | |
| 133 | |
| 134 bool QuicDataReader::ReadStringPiece(StringPiece* result, size_t size) { | |
| 135 // Make sure that we have enough data to read. | |
| 136 if (!CanRead(size)) { | |
| 137 OnFailure(); | |
| 138 return false; | |
| 139 } | |
| 140 | |
| 141 // Set result. | |
| 142 result->set(data_ + pos_, size); | |
| 143 | |
| 144 // Iterate. | |
| 145 pos_ += size; | |
| 146 | |
| 147 return true; | |
| 148 } | |
| 149 | |
| 150 StringPiece QuicDataReader::PeekRemainingPayload() { | |
| 151 return StringPiece(data_ + pos_, len_ - pos_); | |
| 152 } | |
| 153 | |
| 154 StringPiece QuicDataReader::ReadRemainingPayload() { | |
| 155 StringPiece payload = PeekRemainingPayload(); | |
| 156 pos_ = len_; | |
| 157 return payload; | |
| 158 } | |
| 159 | |
| 160 bool QuicDataReader::IsDoneReading() const { | |
| 161 return len_ == pos_; | |
| 162 } | |
| 163 | |
| 164 size_t QuicDataReader::BytesRemaining() const { | |
| 165 return len_ - pos_; | |
| 166 } | |
| 167 | |
| 168 bool QuicDataReader::CanRead(size_t bytes) const { | |
| 169 return bytes <= (len_ - pos_); | |
| 170 } | |
| 171 | |
| 172 void QuicDataReader::OnFailure() { | |
| 173 // Set our iterator to the end of the buffer so that further reads fail | |
| 174 // immediately. | |
| 175 pos_ = len_; | |
| 176 } | |
| 177 | |
| 178 } // namespace net | |
| OLD | NEW |