Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(558)

Unified Diff: device/u2f/u2f_packet.cc

Issue 2771673002: Use vectors instead of IOBuffer for U2fPackets (Closed)
Patch Set: Use vectors instead of IOBuffer for U2fPackets Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« device/u2f/u2f_packet.h ('K') | « device/u2f/u2f_packet.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: device/u2f/u2f_packet.cc
diff --git a/device/u2f/u2f_packet.cc b/device/u2f/u2f_packet.cc
index 76dbefb57301d30f877b7afbb480a24c3ff7b054..c2c715c54094f67613ad421251634f3559e77df2 100644
--- a/device/u2f/u2f_packet.cc
+++ b/device/u2f/u2f_packet.cc
@@ -16,13 +16,6 @@ U2fPacket::U2fPacket() {}
U2fPacket::~U2fPacket() {}
-scoped_refptr<net::IOBufferWithSize> U2fPacket::GetSerializedBuffer() {
- if (serialized_)
- return serialized_;
- else
- return make_scoped_refptr(new net::IOBufferWithSize(0));
-}
-
std::vector<uint8_t> U2fPacket::GetPacketPayload() const {
return data_;
}
@@ -38,65 +31,60 @@ U2fInitPacket::U2fInitPacket(const uint32_t channel_id,
const uint8_t cmd,
const std::vector<uint8_t> data,
Reilly Grant (use Gerrit) 2017/03/22 23:08:17 const std::vector<uint8_t>& The other parameters
Casey Piper 2017/03/23 03:47:25 Done.
const uint16_t payload_length)
- : U2fPacket(data, channel_id), command_(cmd) {
- serialized_ = new net::IOBufferWithSize(kPacketSize);
- size_t index = 0;
- // Byte at offset 0 is the report ID, which is always 0
- serialized_->data()[index++] = 0;
-
- serialized_->data()[index++] = (channel_id_ >> 24) & 0xff;
- serialized_->data()[index++] = (channel_id_ >> 16) & 0xff;
- serialized_->data()[index++] = (channel_id_ >> 8) & 0xff;
- serialized_->data()[index++] = channel_id_ & 0xff;
+ : U2fPacket(data, channel_id),
+ command_(cmd),
+ payload_length_(payload_length) {}
- serialized_->data()[index++] = command_;
- payload_length_ = payload_length;
- serialized_->data()[index++] = (payload_length >> 8) & 0xff;
- serialized_->data()[index++] = payload_length & 0xff;
+std::vector<uint8_t> U2fInitPacket::GetSerializedData() {
+ std::vector<uint8_t> serialized(kPacketSize);
+ // Byte at offset 0 is the report ID, which is always 0
+ size_t index = 1;
+ serialized[index++] = (channel_id_ >> 24) & 0xff;
+ serialized[index++] = (channel_id_ >> 16) & 0xff;
+ serialized[index++] = (channel_id_ >> 8) & 0xff;
+ serialized[index++] = channel_id_ & 0xff;
+
+ serialized[index++] = command_;
+ serialized[index++] = (payload_length_ >> 8) & 0xff;
+ serialized[index++] = payload_length_ & 0xff;
for (size_t data_idx = 0; data_idx < data_.size(); ++data_idx)
- serialized_->data()[index++] = data_.at(data_idx);
- while (static_cast<int>(index) < serialized_->size())
- serialized_->data()[index++] = 0;
+ serialized[index++] = data_[data_idx];
+ return serialized;
}
// static
std::unique_ptr<U2fInitPacket> U2fInitPacket::CreateFromSerializedData(
- scoped_refptr<net::IOBufferWithSize> buf,
+ std::vector<uint8_t> serialized,
size_t* remaining_size) {
- if (buf == nullptr || remaining_size == nullptr || buf->size() != kPacketSize)
+ if (remaining_size == nullptr || serialized.size() != kPacketSize)
return nullptr;
- return base::MakeUnique<U2fInitPacket>(buf, remaining_size);
+ return base::MakeUnique<U2fInitPacket>(serialized, remaining_size);
}
-U2fInitPacket::U2fInitPacket(scoped_refptr<net::IOBufferWithSize> buf,
+U2fInitPacket::U2fInitPacket(std::vector<uint8_t> serialized,
size_t* remaining_size) {
// Report ID is at index 0, so start at index 1 for channel ID
size_t index = 1;
uint16_t payload_size = 0;
- uint16_t data_size = 0;
-
- channel_id_ = (buf->data()[index++] & 0xff) << 24;
- channel_id_ |= (buf->data()[index++] & 0xff) << 16;
- channel_id_ |= (buf->data()[index++] & 0xff) << 8;
- channel_id_ |= buf->data()[index++] & 0xff;
- command_ = buf->data()[index++];
- payload_size = buf->data()[index++] << 8;
- payload_size |= static_cast<uint8_t>(buf->data()[index++]);
+
+ channel_id_ = (serialized[index++] & 0xff) << 24;
+ channel_id_ |= (serialized[index++] & 0xff) << 16;
+ channel_id_ |= (serialized[index++] & 0xff) << 8;
+ channel_id_ |= serialized[index++] & 0xff;
+ command_ = serialized[index++];
+ payload_size = serialized[index++] << 8;
+ payload_size |= static_cast<uint8_t>(serialized[index++]);
payload_length_ = payload_size;
// Check to see if payload is less than maximum size and padded with 0s
- data_size =
+ uint16_t data_size =
std::min(payload_size, static_cast<uint16_t>(kPacketSize - index));
// Update remaining size to determine the payload size of follow on packets
*remaining_size = payload_size - data_size;
- data_.insert(data_.end(), &buf->data()[index],
- &buf->data()[index + data_size]);
-
- for (int i = index + data_size; i < buf->size(); ++i)
- buf->data()[i] = 0;
- serialized_ = buf;
+ data_.insert(data_.end(), serialized.begin() + index,
+ serialized.begin() + index + data_size);
}
U2fInitPacket::~U2fInitPacket() {}
@@ -109,59 +97,50 @@ U2fInitPacket::~U2fInitPacket() {}
U2fContinuationPacket::U2fContinuationPacket(const uint32_t channel_id,
const uint8_t sequence,
std::vector<uint8_t> data)
- : U2fPacket(data, channel_id), sequence_(sequence) {
- serialized_ = new net::IOBufferWithSize(kPacketSize);
- size_t index = 0;
- // Byte at offset 0 is the report ID, which is always 0
- serialized_->data()[index++] = 0;
+ : U2fPacket(data, channel_id), sequence_(sequence) {}
- serialized_->data()[index++] = (channel_id_ >> 24) & 0xff;
- serialized_->data()[index++] = (channel_id_ >> 16) & 0xff;
- serialized_->data()[index++] = (channel_id_ >> 8) & 0xff;
- serialized_->data()[index++] = channel_id_ & 0xff;
+std::vector<uint8_t> U2fContinuationPacket::GetSerializedData() {
+ std::vector<uint8_t> serialized(kPacketSize);
+ // Byte at offset 0 is the report ID, which is always 0
+ size_t index = 1;
+ serialized[index++] = (channel_id_ >> 24) & 0xff;
+ serialized[index++] = (channel_id_ >> 16) & 0xff;
+ serialized[index++] = (channel_id_ >> 8) & 0xff;
+ serialized[index++] = channel_id_ & 0xff;
- serialized_->data()[index++] = sequence_;
+ serialized[index++] = sequence_;
for (size_t idx = 0; idx < data_.size(); ++idx)
- serialized_->data()[index++] = data_.at(idx);
-
- while (static_cast<int>(index) < serialized_->size())
- serialized_->data()[index++] = 0;
+ serialized[index++] = data_[idx];
+ return serialized;
}
// static
std::unique_ptr<U2fContinuationPacket>
-U2fContinuationPacket::CreateFromSerializedData(
- scoped_refptr<net::IOBufferWithSize> buf,
- size_t* remaining_size) {
- if (buf == nullptr || remaining_size == nullptr || buf->size() != kPacketSize)
+U2fContinuationPacket::CreateFromSerializedData(std::vector<uint8_t> serialized,
+ size_t* remaining_size) {
+ if (remaining_size == nullptr || serialized.size() != kPacketSize)
return nullptr;
- return base::MakeUnique<U2fContinuationPacket>(buf, remaining_size);
+ return base::MakeUnique<U2fContinuationPacket>(serialized, remaining_size);
}
-U2fContinuationPacket::U2fContinuationPacket(
- scoped_refptr<net::IOBufferWithSize> buf,
- size_t* remaining_size) {
+U2fContinuationPacket::U2fContinuationPacket(std::vector<uint8_t> serialized,
+ size_t* remaining_size) {
// Report ID is at index 0, so start at index 1 for channel ID
size_t index = 1;
size_t data_size;
- channel_id_ = (buf->data()[index++] & 0xff) << 24;
- channel_id_ |= (buf->data()[index++] & 0xff) << 16;
- channel_id_ |= (buf->data()[index++] & 0xff) << 8;
- channel_id_ |= buf->data()[index++] & 0xff;
- sequence_ = buf->data()[index++];
+ channel_id_ = (serialized[index++] & 0xff) << 24;
+ channel_id_ |= (serialized[index++] & 0xff) << 16;
+ channel_id_ |= (serialized[index++] & 0xff) << 8;
+ channel_id_ |= serialized[index++] & 0xff;
+ sequence_ = serialized[index++];
// Check to see if packet payload is less than maximum size and padded with 0s
data_size = std::min(*remaining_size, kPacketSize - index);
*remaining_size -= data_size;
- data_.insert(std::end(data_), &buf->data()[index],
- &buf->data()[index + data_size]);
-
- // Incoming buffer may not be padded with 0's, so manually update buffer
- for (int i = index + data_size; i < buf->size(); ++i)
- buf->data()[i] = 0;
- serialized_ = buf;
+ data_.insert(data_.end(), serialized.begin() + index,
+ serialized.begin() + index + data_size);
}
U2fContinuationPacket::~U2fContinuationPacket() {}
« device/u2f/u2f_packet.h ('K') | « device/u2f/u2f_packet.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698