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 // TODO(rch): use sizeof instead of magic numbers. |
| 20 // Refactor to use a common Read(void* buffer, size_t len) |
| 21 // method that will do the memcpy and the advancement of pos_. |
| 22 if (!CanRead(2)) { |
| 23 OnFailure(); |
| 24 return false; |
| 25 } |
| 26 |
| 27 // Read into result. |
| 28 memcpy(result, data_ + pos_, 2); |
| 29 |
| 30 // Iterate. |
| 31 pos_ += 2; |
| 32 |
| 33 return true; |
| 34 } |
| 35 |
| 36 bool QuicDataReader::ReadUInt32(uint32* result) { |
| 37 // Make sure that we have the whole uint32. |
| 38 if (!CanRead(4)) { |
| 39 OnFailure(); |
| 40 return false; |
| 41 } |
| 42 |
| 43 // Read into result. |
| 44 memcpy(result, data_ + pos_, 4); |
| 45 |
| 46 // Iterate. |
| 47 pos_ += 4; |
| 48 |
| 49 return true; |
| 50 } |
| 51 |
| 52 bool QuicDataReader::ReadUInt48(uint64* result) { |
| 53 uint32 lo; |
| 54 if (!ReadUInt32(&lo)) { |
| 55 return false; |
| 56 } |
| 57 |
| 58 uint16 hi; |
| 59 if (!ReadUInt16(&hi)) { |
| 60 return false; |
| 61 } |
| 62 |
| 63 *result = hi; |
| 64 *result <<= 32; |
| 65 *result += lo; |
| 66 |
| 67 return true; |
| 68 } |
| 69 |
| 70 bool QuicDataReader::ReadUInt64(uint64* result) { |
| 71 // Make sure that we have the whole uint64. |
| 72 if (!CanRead(8)) { |
| 73 OnFailure(); |
| 74 return false; |
| 75 } |
| 76 |
| 77 // Read into result. |
| 78 memcpy(result, data_ + pos_, 8); |
| 79 |
| 80 // Iterate. |
| 81 pos_ += 8; |
| 82 |
| 83 return true; |
| 84 } |
| 85 |
| 86 bool QuicDataReader::ReadUInt128(uint128* result) { |
| 87 uint64 high_hash; |
| 88 uint64 low_hash; |
| 89 |
| 90 if (!ReadUInt64(&low_hash)) { |
| 91 return false; |
| 92 } |
| 93 if (!ReadUInt64(&high_hash)) { |
| 94 return false; |
| 95 } |
| 96 |
| 97 *result = uint128(high_hash, low_hash); |
| 98 return true; |
| 99 } |
| 100 |
| 101 bool QuicDataReader::ReadStringPiece16(StringPiece* result) { |
| 102 // Read resultant length. |
| 103 uint16 result_len; |
| 104 if (!ReadUInt16(&result_len)) { |
| 105 // OnFailure() already called. |
| 106 return false; |
| 107 } |
| 108 |
| 109 return ReadStringPiece(result, result_len); |
| 110 } |
| 111 |
| 112 bool QuicDataReader::ReadBytes(void* result, size_t size) { |
| 113 // Make sure that we have enough data to read. |
| 114 if (!CanRead(size)) { |
| 115 OnFailure(); |
| 116 return false; |
| 117 } |
| 118 |
| 119 // Read into result. |
| 120 memcpy(result, data_ + pos_, size); |
| 121 |
| 122 // Iterate. |
| 123 pos_ += size; |
| 124 |
| 125 return true; |
| 126 } |
| 127 |
| 128 |
| 129 bool QuicDataReader::ReadStringPiece(StringPiece* result, size_t size) { |
| 130 // Make sure that we have enough data to read. |
| 131 if (!CanRead(size)) { |
| 132 OnFailure(); |
| 133 return false; |
| 134 } |
| 135 |
| 136 // Set result. |
| 137 result->set(data_ + pos_, size); |
| 138 |
| 139 // Iterate. |
| 140 pos_ += size; |
| 141 |
| 142 return true; |
| 143 } |
| 144 |
| 145 StringPiece QuicDataReader::PeekRemainingPayload() { |
| 146 return StringPiece(data_ + pos_, len_ - pos_); |
| 147 } |
| 148 |
| 149 StringPiece QuicDataReader::ReadRemainingPayload() { |
| 150 StringPiece payload = PeekRemainingPayload(); |
| 151 pos_ = len_; |
| 152 return payload; |
| 153 } |
| 154 |
| 155 bool QuicDataReader::IsDoneReading() const { |
| 156 return len_ == pos_; |
| 157 } |
| 158 |
| 159 size_t QuicDataReader::BytesRemaining() const { |
| 160 return len_ - pos_; |
| 161 } |
| 162 |
| 163 bool QuicDataReader::CanRead(size_t bytes) const { |
| 164 return bytes <= (len_ - pos_); |
| 165 } |
| 166 |
| 167 void QuicDataReader::OnFailure() { |
| 168 // Set our iterator to the end of the buffer so that further reads fail |
| 169 // immediately. |
| 170 pos_ = len_; |
| 171 } |
| 172 |
| 173 } // namespace net |
OLD | NEW |