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 |
(...skipping 22 matching lines...) Expand all Loading... |
33 } | 33 } |
34 | 34 |
35 bool SpdyFrameReader::ReadUInt16(uint16* result) { | 35 bool SpdyFrameReader::ReadUInt16(uint16* result) { |
36 // Make sure that we have the whole uint16. | 36 // Make sure that we have the whole uint16. |
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 = ntohs(*(reinterpret_cast<const uint16*>(data_ + ofs_))); | 43 *result = base::NetToHost16(*(reinterpret_cast<const uint16*>(data_ + ofs_))); |
44 | 44 |
45 // Iterate. | 45 // Iterate. |
46 ofs_ += 2; | 46 ofs_ += 2; |
47 | 47 |
48 return true; | 48 return true; |
49 } | 49 } |
50 | 50 |
51 bool SpdyFrameReader::ReadUInt32(uint32* result) { | 51 bool SpdyFrameReader::ReadUInt32(uint32* result) { |
52 // Make sure that we have the whole uint32. | 52 // Make sure that we have the whole uint32. |
53 if (!CanRead(4)) { | 53 if (!CanRead(4)) { |
54 OnFailure(); | 54 OnFailure(); |
55 return false; | 55 return false; |
56 } | 56 } |
57 | 57 |
58 // Read into result. | 58 // Read into result. |
59 *result = ntohl(*(reinterpret_cast<const uint32*>(data_ + ofs_))); | 59 *result = base::NetToHost32(*(reinterpret_cast<const uint32*>(data_ + ofs_))); |
60 | 60 |
61 // Iterate. | 61 // Iterate. |
62 ofs_ += 4; | 62 ofs_ += 4; |
63 | 63 |
64 return true; | 64 return true; |
65 } | 65 } |
66 | 66 |
67 bool SpdyFrameReader::ReadUInt64(uint64* result) { | 67 bool SpdyFrameReader::ReadUInt64(uint64* result) { |
68 // Make sure that we have the whole uint64. | 68 // Make sure that we have the whole uint64. |
69 if (!CanRead(8)) { | 69 if (!CanRead(8)) { |
70 OnFailure(); | 70 OnFailure(); |
71 return false; | 71 return false; |
72 } | 72 } |
73 | 73 |
74 // Read into result. Network byte order is big-endian. | 74 // Read into result. Network byte order is big-endian. |
75 uint64 upper = ntohl(*(reinterpret_cast<const uint32*>(data_ + ofs_))); | 75 uint64 upper = |
76 uint64 lower = ntohl(*(reinterpret_cast<const uint32*>(data_ + ofs_ + 4))); | 76 base::NetToHost32(*(reinterpret_cast<const uint32*>(data_ + ofs_))); |
| 77 uint64 lower = |
| 78 base::NetToHost32(*(reinterpret_cast<const uint32*>(data_ + ofs_ + 4))); |
77 *result = (upper << 32) + lower; | 79 *result = (upper << 32) + lower; |
78 | 80 |
79 // Iterate. | 81 // Iterate. |
80 ofs_ += 8; | 82 ofs_ += 8; |
81 | 83 |
82 return true; | 84 return true; |
83 } | 85 } |
84 | 86 |
85 bool SpdyFrameReader::ReadUInt31(uint32* result) { | 87 bool SpdyFrameReader::ReadUInt31(uint32* result) { |
86 bool success = ReadUInt32(result); | 88 bool success = ReadUInt32(result); |
87 | 89 |
88 // Zero out highest-order bit. | 90 // Zero out highest-order bit. |
89 if (success) { | 91 if (success) { |
90 *result &= 0x7fffffff; | 92 *result &= 0x7fffffff; |
91 } | 93 } |
92 | 94 |
93 return success; | 95 return success; |
94 } | 96 } |
95 | 97 |
96 bool SpdyFrameReader::ReadUInt24(uint32* result) { | 98 bool SpdyFrameReader::ReadUInt24(uint32* result) { |
97 // Make sure that we have the whole uint24. | 99 // Make sure that we have the whole uint24. |
98 if (!CanRead(3)) { | 100 if (!CanRead(3)) { |
99 OnFailure(); | 101 OnFailure(); |
100 return false; | 102 return false; |
101 } | 103 } |
102 | 104 |
103 // Read into result. | 105 // Read into result. |
104 *result = 0; | 106 *result = 0; |
105 memcpy(reinterpret_cast<char*>(result) + 1, data_ + ofs_, 3); | 107 memcpy(reinterpret_cast<char*>(result) + 1, data_ + ofs_, 3); |
106 *result = ntohl(*result); | 108 *result = base::NetToHost32(*result); |
107 | 109 |
108 // Iterate. | 110 // Iterate. |
109 ofs_ += 3; | 111 ofs_ += 3; |
110 | 112 |
111 return true; | 113 return true; |
112 } | 114 } |
113 | 115 |
114 bool SpdyFrameReader::ReadStringPiece16(base::StringPiece* result) { | 116 bool SpdyFrameReader::ReadStringPiece16(base::StringPiece* result) { |
115 // Read resultant length. | 117 // Read resultant length. |
116 uint16 result_len; | 118 uint16 result_len; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 return bytes <= (len_ - ofs_); | 195 return bytes <= (len_ - ofs_); |
194 } | 196 } |
195 | 197 |
196 void SpdyFrameReader::OnFailure() { | 198 void SpdyFrameReader::OnFailure() { |
197 // Set our iterator to the end of the buffer so that further reads fail | 199 // Set our iterator to the end of the buffer so that further reads fail |
198 // immediately. | 200 // immediately. |
199 ofs_ = len_; | 201 ofs_ = len_; |
200 } | 202 } |
201 | 203 |
202 } // namespace net | 204 } // namespace net |
OLD | NEW |