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 <limits> |
| 6 |
| 7 #include "base/sys_byteorder.h" |
| 8 #include "net/spdy/spdy_frame_reader.h" |
| 9 |
| 10 namespace spdy { |
| 11 |
| 12 SpdyFrameReader::SpdyFrameReader(const char* data, const size_t len) |
| 13 : data_(data), |
| 14 len_(len), |
| 15 ofs_(0) { |
| 16 } |
| 17 |
| 18 bool SpdyFrameReader::ReadUInt16(uint16* result) { |
| 19 // Make sure that we have the whole uint16. |
| 20 if (!CanRead(2)) { |
| 21 OnFailure(); |
| 22 return false; |
| 23 } |
| 24 |
| 25 // Read into result. |
| 26 *result = ntohs(*(reinterpret_cast<const uint16*>(data_ + ofs_))); |
| 27 |
| 28 // Iterate. |
| 29 ofs_ += 2; |
| 30 |
| 31 return true; |
| 32 } |
| 33 |
| 34 bool SpdyFrameReader::ReadUInt32(uint32* result) { |
| 35 // Make sure that we have the whole uint32. |
| 36 if (!CanRead(4)) { |
| 37 OnFailure(); |
| 38 return false; |
| 39 } |
| 40 |
| 41 // Read into result. |
| 42 *result = ntohl(*(reinterpret_cast<const uint32*>(data_ + ofs_))); |
| 43 |
| 44 // Iterate. |
| 45 ofs_ += 4; |
| 46 |
| 47 return true; |
| 48 } |
| 49 |
| 50 bool SpdyFrameReader::ReadStringPiece16(base::StringPiece* result) { |
| 51 // Read resultant length. |
| 52 uint16 result_len; |
| 53 if (!ReadUInt16(&result_len)) { |
| 54 // OnFailure() already called. |
| 55 return false; |
| 56 } |
| 57 |
| 58 // Make sure that we have the whole string. |
| 59 if (!CanRead(result_len)) { |
| 60 OnFailure(); |
| 61 return false; |
| 62 } |
| 63 |
| 64 // Set result. |
| 65 result->set(data_ + ofs_, result_len); |
| 66 |
| 67 // Iterate. |
| 68 ofs_ += result_len; |
| 69 |
| 70 return true; |
| 71 } |
| 72 |
| 73 bool SpdyFrameReader::ReadStringPiece32(base::StringPiece* result) { |
| 74 // Read resultant length. |
| 75 uint32 result_len; |
| 76 if (!ReadUInt32(&result_len)) { |
| 77 // OnFailure() already called. |
| 78 return false; |
| 79 } |
| 80 |
| 81 // Make sure that we have the whole string. |
| 82 if (!CanRead(result_len)) { |
| 83 OnFailure(); |
| 84 return false; |
| 85 } |
| 86 |
| 87 // Set result. |
| 88 result->set(data_ + ofs_, result_len); |
| 89 |
| 90 // Iterate. |
| 91 ofs_ += result_len; |
| 92 |
| 93 return true; |
| 94 } |
| 95 |
| 96 bool SpdyFrameReader::ReadBytes(void* result, size_t size) { |
| 97 // Make sure that we have enough data to read. |
| 98 if (!CanRead(size)) { |
| 99 OnFailure(); |
| 100 return false; |
| 101 } |
| 102 |
| 103 // Read into result. |
| 104 memcpy(result, data_ + ofs_, size); |
| 105 |
| 106 // Iterate. |
| 107 ofs_ += size; |
| 108 |
| 109 return true; |
| 110 } |
| 111 |
| 112 bool SpdyFrameReader::IsDoneReading() const { |
| 113 return len_ == ofs_; |
| 114 } |
| 115 |
| 116 bool SpdyFrameReader::CanRead(size_t bytes) const { |
| 117 return bytes <= (len_ - ofs_); |
| 118 } |
| 119 |
| 120 void SpdyFrameReader::OnFailure() { |
| 121 // Set our iterator to the end of the buffer so that further reads fail |
| 122 // immediately. |
| 123 ofs_ = len_; |
| 124 } |
| 125 |
| 126 } // namespace spdy |
OLD | NEW |