OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "device/u2f/u2f_packet.h" | 5 #include "base/memory/ptr_util.h" |
6 #include "net/base/io_buffer.h" | 6 #include "net/base/io_buffer.h" |
7 | 7 |
| 8 #include "u2f_packet.h" |
| 9 |
8 namespace device { | 10 namespace device { |
9 | 11 |
10 U2fPacket::U2fPacket(const std::vector<uint8_t> data, const uint32_t channel_id) | 12 U2fPacket::U2fPacket(const std::vector<uint8_t> data, const uint32_t channel_id) |
11 : data_(data), channel_id_(channel_id) {} | 13 : data_(data), channel_id_(channel_id) {} |
12 | 14 |
13 U2fPacket::U2fPacket() {} | 15 U2fPacket::U2fPacket() {} |
14 | 16 |
15 U2fPacket::~U2fPacket() {} | 17 U2fPacket::~U2fPacket() {} |
16 | 18 |
17 scoped_refptr<net::IOBufferWithSize> U2fPacket::GetSerializedBuffer() { | 19 scoped_refptr<net::IOBufferWithSize> U2fPacket::GetSerializedBuffer() { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 payload_length_ = payload_length; | 53 payload_length_ = payload_length; |
52 serialized_->data()[index++] = (payload_length >> 8) & 0xff; | 54 serialized_->data()[index++] = (payload_length >> 8) & 0xff; |
53 serialized_->data()[index++] = payload_length & 0xff; | 55 serialized_->data()[index++] = payload_length & 0xff; |
54 for (size_t data_idx = 0; data_idx < data_.size(); ++data_idx) | 56 for (size_t data_idx = 0; data_idx < data_.size(); ++data_idx) |
55 serialized_->data()[index++] = data_.at(data_idx); | 57 serialized_->data()[index++] = data_.at(data_idx); |
56 while (static_cast<int>(index) < serialized_->size()) | 58 while (static_cast<int>(index) < serialized_->size()) |
57 serialized_->data()[index++] = 0; | 59 serialized_->data()[index++] = 0; |
58 } | 60 } |
59 | 61 |
60 // static | 62 // static |
61 scoped_refptr<U2fInitPacket> U2fInitPacket::CreateFromSerializedData( | 63 std::unique_ptr<U2fInitPacket> U2fInitPacket::CreateFromSerializedData( |
62 scoped_refptr<net::IOBufferWithSize> buf, | 64 scoped_refptr<net::IOBufferWithSize> buf, |
63 size_t* remaining_size) { | 65 size_t* remaining_size) { |
64 if (buf == nullptr || remaining_size == nullptr || buf->size() != kPacketSize) | 66 if (buf == nullptr || remaining_size == nullptr || buf->size() != kPacketSize) |
65 return nullptr; | 67 return nullptr; |
66 | 68 |
67 return make_scoped_refptr(new U2fInitPacket(buf, remaining_size)); | 69 return base::MakeUnique<U2fInitPacket>(buf, remaining_size); |
68 } | 70 } |
69 | 71 |
70 U2fInitPacket::U2fInitPacket(scoped_refptr<net::IOBufferWithSize> buf, | 72 U2fInitPacket::U2fInitPacket(scoped_refptr<net::IOBufferWithSize> buf, |
71 size_t* remaining_size) { | 73 size_t* remaining_size) { |
72 // Report ID is at index 0, so start at index 1 for channel ID | 74 // Report ID is at index 0, so start at index 1 for channel ID |
73 size_t index = 1; | 75 size_t index = 1; |
74 uint16_t payload_size = 0; | 76 uint16_t payload_size = 0; |
75 uint16_t data_size = 0; | 77 uint16_t data_size = 0; |
76 | 78 |
77 channel_id_ = (buf->data()[index++] & 0xff) << 24; | 79 channel_id_ = (buf->data()[index++] & 0xff) << 24; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 122 |
121 serialized_->data()[index++] = sequence_; | 123 serialized_->data()[index++] = sequence_; |
122 for (size_t idx = 0; idx < data_.size(); ++idx) | 124 for (size_t idx = 0; idx < data_.size(); ++idx) |
123 serialized_->data()[index++] = data_.at(idx); | 125 serialized_->data()[index++] = data_.at(idx); |
124 | 126 |
125 while (static_cast<int>(index) < serialized_->size()) | 127 while (static_cast<int>(index) < serialized_->size()) |
126 serialized_->data()[index++] = 0; | 128 serialized_->data()[index++] = 0; |
127 } | 129 } |
128 | 130 |
129 // static | 131 // static |
130 scoped_refptr<U2fContinuationPacket> | 132 std::unique_ptr<U2fContinuationPacket> |
131 U2fContinuationPacket::CreateFromSerializedData( | 133 U2fContinuationPacket::CreateFromSerializedData( |
132 scoped_refptr<net::IOBufferWithSize> buf, | 134 scoped_refptr<net::IOBufferWithSize> buf, |
133 size_t* remaining_size) { | 135 size_t* remaining_size) { |
134 if (buf == nullptr || remaining_size == nullptr || buf->size() != kPacketSize) | 136 if (buf == nullptr || remaining_size == nullptr || buf->size() != kPacketSize) |
135 return nullptr; | 137 return nullptr; |
136 | 138 |
137 return make_scoped_refptr(new U2fContinuationPacket(buf, remaining_size)); | 139 return base::MakeUnique<U2fContinuationPacket>(buf, remaining_size); |
138 } | 140 } |
139 | 141 |
140 U2fContinuationPacket::U2fContinuationPacket( | 142 U2fContinuationPacket::U2fContinuationPacket( |
141 scoped_refptr<net::IOBufferWithSize> buf, | 143 scoped_refptr<net::IOBufferWithSize> buf, |
142 size_t* remaining_size) { | 144 size_t* remaining_size) { |
143 // Report ID is at index 0, so start at index 1 for channel ID | 145 // Report ID is at index 0, so start at index 1 for channel ID |
144 size_t index = 1; | 146 size_t index = 1; |
145 size_t data_size; | 147 size_t data_size; |
146 | 148 |
147 channel_id_ = (buf->data()[index++] & 0xff) << 24; | 149 channel_id_ = (buf->data()[index++] & 0xff) << 24; |
148 channel_id_ |= (buf->data()[index++] & 0xff) << 16; | 150 channel_id_ |= (buf->data()[index++] & 0xff) << 16; |
149 channel_id_ |= (buf->data()[index++] & 0xff) << 8; | 151 channel_id_ |= (buf->data()[index++] & 0xff) << 8; |
150 channel_id_ |= buf->data()[index++] & 0xff; | 152 channel_id_ |= buf->data()[index++] & 0xff; |
151 sequence_ = buf->data()[index++]; | 153 sequence_ = buf->data()[index++]; |
152 | 154 |
153 // Check to see if packet payload is less than maximum size and padded with 0s | 155 // Check to see if packet payload is less than maximum size and padded with 0s |
154 data_size = std::min(*remaining_size, kPacketSize - index); | 156 data_size = std::min(*remaining_size, kPacketSize - index); |
155 *remaining_size -= data_size; | 157 *remaining_size -= data_size; |
156 data_.insert(std::end(data_), &buf->data()[index], | 158 data_.insert(std::end(data_), &buf->data()[index], |
157 &buf->data()[index + data_size]); | 159 &buf->data()[index + data_size]); |
158 | 160 |
159 // Incoming buffer may not be padded with 0's, so manually update buffer | 161 // Incoming buffer may not be padded with 0's, so manually update buffer |
160 for (int i = index + data_size; i < buf->size(); ++i) | 162 for (int i = index + data_size; i < buf->size(); ++i) |
161 buf->data()[i] = 0; | 163 buf->data()[i] = 0; |
162 serialized_ = buf; | 164 serialized_ = buf; |
163 } | 165 } |
164 | 166 |
165 U2fContinuationPacket::~U2fContinuationPacket() {} | 167 U2fContinuationPacket::~U2fContinuationPacket() {} |
| 168 |
166 } // namespace device | 169 } // namespace device |
OLD | NEW |