| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <limits> | 5 #include <limits> |
| 6 | 6 |
| 7 #include "base/sys_byteorder.h" | 7 #include "base/sys_byteorder.h" |
| 8 #include "net/spdy/spdy_frame_reader.h" | 8 #include "net/spdy/spdy_frame_reader.h" |
| 9 #include "net/spdy/spdy_protocol.h" | 9 #include "net/spdy/spdy_protocol.h" |
| 10 | 10 |
| 11 namespace net { | 11 namespace net { |
| 12 | 12 |
| 13 SpdyFrameReader::SpdyFrameReader(const char* data, const size_t len) | 13 SpdyFrameReader::SpdyFrameReader(const char* data, const size_t len) |
| 14 : data_(data), | 14 : data_(data), |
| 15 len_(len), | 15 len_(len), |
| 16 ofs_(0) { | 16 ofs_(0) { |
| 17 } | 17 } |
| 18 | 18 |
| 19 bool SpdyFrameReader::ReadUInt8(uint8* result) { | 19 bool SpdyFrameReader::ReadUInt8(uint8_t* result) { |
| 20 // Make sure that we have the whole uint8. | 20 // Make sure that we have the whole uint8_t. |
| 21 if (!CanRead(1)) { | 21 if (!CanRead(1)) { |
| 22 OnFailure(); | 22 OnFailure(); |
| 23 return false; | 23 return false; |
| 24 } | 24 } |
| 25 | 25 |
| 26 // Read into result. | 26 // Read into result. |
| 27 *result = *reinterpret_cast<const uint8*>(data_ + ofs_); | 27 *result = *reinterpret_cast<const uint8_t*>(data_ + ofs_); |
| 28 | 28 |
| 29 // Iterate. | 29 // Iterate. |
| 30 ofs_ += 1; | 30 ofs_ += 1; |
| 31 | 31 |
| 32 return true; | 32 return true; |
| 33 } | 33 } |
| 34 | 34 |
| 35 bool SpdyFrameReader::ReadUInt16(uint16* result) { | 35 bool SpdyFrameReader::ReadUInt16(uint16_t* result) { |
| 36 // Make sure that we have the whole uint16. | 36 // Make sure that we have the whole uint16_t. |
| 37 if (!CanRead(2)) { | 37 if (!CanRead(2)) { |
| 38 OnFailure(); | 38 OnFailure(); |
| 39 return false; | 39 return false; |
| 40 } | 40 } |
| 41 | 41 |
| 42 // Read into result. | 42 // Read into result. |
| 43 *result = base::NetToHost16(*(reinterpret_cast<const uint16*>(data_ + ofs_))); | 43 *result = |
| 44 base::NetToHost16(*(reinterpret_cast<const uint16_t*>(data_ + ofs_))); |
| 44 | 45 |
| 45 // Iterate. | 46 // Iterate. |
| 46 ofs_ += 2; | 47 ofs_ += 2; |
| 47 | 48 |
| 48 return true; | 49 return true; |
| 49 } | 50 } |
| 50 | 51 |
| 51 bool SpdyFrameReader::ReadUInt32(uint32* result) { | 52 bool SpdyFrameReader::ReadUInt32(uint32_t* result) { |
| 52 // Make sure that we have the whole uint32. | 53 // Make sure that we have the whole uint32_t. |
| 53 if (!CanRead(4)) { | 54 if (!CanRead(4)) { |
| 54 OnFailure(); | 55 OnFailure(); |
| 55 return false; | 56 return false; |
| 56 } | 57 } |
| 57 | 58 |
| 58 // Read into result. | 59 // Read into result. |
| 59 *result = base::NetToHost32(*(reinterpret_cast<const uint32*>(data_ + ofs_))); | 60 *result = |
| 61 base::NetToHost32(*(reinterpret_cast<const uint32_t*>(data_ + ofs_))); |
| 60 | 62 |
| 61 // Iterate. | 63 // Iterate. |
| 62 ofs_ += 4; | 64 ofs_ += 4; |
| 63 | 65 |
| 64 return true; | 66 return true; |
| 65 } | 67 } |
| 66 | 68 |
| 67 bool SpdyFrameReader::ReadUInt64(uint64* result) { | 69 bool SpdyFrameReader::ReadUInt64(uint64_t* result) { |
| 68 // Make sure that we have the whole uint64. | 70 // Make sure that we have the whole uint64_t. |
| 69 if (!CanRead(8)) { | 71 if (!CanRead(8)) { |
| 70 OnFailure(); | 72 OnFailure(); |
| 71 return false; | 73 return false; |
| 72 } | 74 } |
| 73 | 75 |
| 74 // Read into result. Network byte order is big-endian. | 76 // Read into result. Network byte order is big-endian. |
| 75 uint64 upper = | 77 uint64_t upper = |
| 76 base::NetToHost32(*(reinterpret_cast<const uint32*>(data_ + ofs_))); | 78 base::NetToHost32(*(reinterpret_cast<const uint32_t*>(data_ + ofs_))); |
| 77 uint64 lower = | 79 uint64_t lower = |
| 78 base::NetToHost32(*(reinterpret_cast<const uint32*>(data_ + ofs_ + 4))); | 80 base::NetToHost32(*(reinterpret_cast<const uint32_t*>(data_ + ofs_ + 4))); |
| 79 *result = (upper << 32) + lower; | 81 *result = (upper << 32) + lower; |
| 80 | 82 |
| 81 // Iterate. | 83 // Iterate. |
| 82 ofs_ += 8; | 84 ofs_ += 8; |
| 83 | 85 |
| 84 return true; | 86 return true; |
| 85 } | 87 } |
| 86 | 88 |
| 87 bool SpdyFrameReader::ReadUInt31(uint32* result) { | 89 bool SpdyFrameReader::ReadUInt31(uint32_t* result) { |
| 88 bool success = ReadUInt32(result); | 90 bool success = ReadUInt32(result); |
| 89 | 91 |
| 90 // Zero out highest-order bit. | 92 // Zero out highest-order bit. |
| 91 if (success) { | 93 if (success) { |
| 92 *result &= 0x7fffffff; | 94 *result &= 0x7fffffff; |
| 93 } | 95 } |
| 94 | 96 |
| 95 return success; | 97 return success; |
| 96 } | 98 } |
| 97 | 99 |
| 98 bool SpdyFrameReader::ReadUInt24(uint32* result) { | 100 bool SpdyFrameReader::ReadUInt24(uint32_t* result) { |
| 99 // Make sure that we have the whole uint24. | 101 // Make sure that we have the whole uint24. |
| 100 if (!CanRead(3)) { | 102 if (!CanRead(3)) { |
| 101 OnFailure(); | 103 OnFailure(); |
| 102 return false; | 104 return false; |
| 103 } | 105 } |
| 104 | 106 |
| 105 // Read into result. | 107 // Read into result. |
| 106 *result = 0; | 108 *result = 0; |
| 107 memcpy(reinterpret_cast<char*>(result) + 1, data_ + ofs_, 3); | 109 memcpy(reinterpret_cast<char*>(result) + 1, data_ + ofs_, 3); |
| 108 *result = base::NetToHost32(*result); | 110 *result = base::NetToHost32(*result); |
| 109 | 111 |
| 110 // Iterate. | 112 // Iterate. |
| 111 ofs_ += 3; | 113 ofs_ += 3; |
| 112 | 114 |
| 113 return true; | 115 return true; |
| 114 } | 116 } |
| 115 | 117 |
| 116 bool SpdyFrameReader::ReadStringPiece16(base::StringPiece* result) { | 118 bool SpdyFrameReader::ReadStringPiece16(base::StringPiece* result) { |
| 117 // Read resultant length. | 119 // Read resultant length. |
| 118 uint16 result_len; | 120 uint16_t result_len; |
| 119 if (!ReadUInt16(&result_len)) { | 121 if (!ReadUInt16(&result_len)) { |
| 120 // OnFailure() already called. | 122 // OnFailure() already called. |
| 121 return false; | 123 return false; |
| 122 } | 124 } |
| 123 | 125 |
| 124 // Make sure that we have the whole string. | 126 // Make sure that we have the whole string. |
| 125 if (!CanRead(result_len)) { | 127 if (!CanRead(result_len)) { |
| 126 OnFailure(); | 128 OnFailure(); |
| 127 return false; | 129 return false; |
| 128 } | 130 } |
| 129 | 131 |
| 130 // Set result. | 132 // Set result. |
| 131 result->set(data_ + ofs_, result_len); | 133 result->set(data_ + ofs_, result_len); |
| 132 | 134 |
| 133 // Iterate. | 135 // Iterate. |
| 134 ofs_ += result_len; | 136 ofs_ += result_len; |
| 135 | 137 |
| 136 return true; | 138 return true; |
| 137 } | 139 } |
| 138 | 140 |
| 139 bool SpdyFrameReader::ReadStringPiece32(base::StringPiece* result) { | 141 bool SpdyFrameReader::ReadStringPiece32(base::StringPiece* result) { |
| 140 // Read resultant length. | 142 // Read resultant length. |
| 141 uint32 result_len; | 143 uint32_t result_len; |
| 142 if (!ReadUInt32(&result_len)) { | 144 if (!ReadUInt32(&result_len)) { |
| 143 // OnFailure() already called. | 145 // OnFailure() already called. |
| 144 return false; | 146 return false; |
| 145 } | 147 } |
| 146 | 148 |
| 147 // Make sure that we have the whole string. | 149 // Make sure that we have the whole string. |
| 148 if (!CanRead(result_len)) { | 150 if (!CanRead(result_len)) { |
| 149 OnFailure(); | 151 OnFailure(); |
| 150 return false; | 152 return false; |
| 151 } | 153 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 return bytes <= (len_ - ofs_); | 197 return bytes <= (len_ - ofs_); |
| 196 } | 198 } |
| 197 | 199 |
| 198 void SpdyFrameReader::OnFailure() { | 200 void SpdyFrameReader::OnFailure() { |
| 199 // Set our iterator to the end of the buffer so that further reads fail | 201 // Set our iterator to the end of the buffer so that further reads fail |
| 200 // immediately. | 202 // immediately. |
| 201 ofs_ = len_; | 203 ofs_ = len_; |
| 202 } | 204 } |
| 203 | 205 |
| 204 } // namespace net | 206 } // namespace net |
| OLD | NEW |