Chromium Code Reviews| Index: device/u2f/u2f_message.cc |
| diff --git a/device/u2f/u2f_message.cc b/device/u2f/u2f_message.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..70adb1d131b0607ec2b46e3a0f6b09c956f2626c |
| --- /dev/null |
| +++ b/device/u2f/u2f_message.cc |
| @@ -0,0 +1,81 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "device/u2f/u2f_message.h" |
| +#include "device/u2f/u2f_packet.h" |
| + |
| +namespace device { |
| + |
| +// static |
| +scoped_refptr<U2fMessage> U2fMessage::Create( |
| + const char channel_id[U2fPacket::kChannelIdSize], |
|
Casey Piper
2016/11/29 22:17:56
Should I change all char's to uint8_t?
Reilly Grant (use Gerrit)
2016/11/29 22:51:48
Yes.
Casey Piper
2016/11/30 17:37:04
Done.
|
| + const Type type, |
| + const std::vector<uint8_t>& data) { |
| + if (data.size() > kMaxMessageSize) |
| + return nullptr; |
| + |
| + return make_scoped_refptr(new U2fMessage(channel_id, type, data)); |
| +} |
| + |
| +U2fMessage::U2fMessage(const char channel_id[U2fPacket::kChannelIdSize], |
| + const Type type, |
| + const std::vector<uint8_t>& data) { |
| + size_t remaining_size = data.size(); |
| + char sequence = 0; |
| + |
| + std::vector<uint8_t>::const_iterator first = data.begin(); |
| + std::vector<uint8_t>::const_iterator last; |
| + |
| + if (remaining_size > kInitPacketDataSize) { |
| + last = data.begin() + kInitPacketDataSize; |
| + remaining_size -= kInitPacketDataSize; |
| + } else { |
| + last = data.begin() + remaining_size; |
| + remaining_size = 0; |
| + } |
| + |
| + packets_.push_back(scoped_refptr<U2fPacket>( |
| + new U2fInitPacket(channel_id, static_cast<char>(type), |
| + std::vector<uint8_t>(first, last), data.size()))); |
| + |
| + while (remaining_size > 0) { |
| + first = last; |
| + if (remaining_size > kContPacketDataSize) { |
| + last = first + kContPacketDataSize; |
| + remaining_size -= kContPacketDataSize; |
| + } else { |
| + last = first + remaining_size; |
| + remaining_size = 0; |
| + } |
| + |
| + packets_.push_back(make_scoped_refptr(new U2fContPacket( |
| + channel_id, sequence, std::vector<uint8_t>(first, last)))); |
| + sequence++; |
| + } |
| +} |
| + |
| +U2fMessage::~U2fMessage() {} |
| + |
| +std::list<scoped_refptr<U2fPacket>>::const_iterator U2fMessage::begin() { |
| + return packets_.begin(); |
| +} |
| + |
| +std::list<scoped_refptr<U2fPacket>>::const_iterator U2fMessage::end() { |
| + return packets_.end(); |
| +} |
| + |
| +scoped_refptr<net::IOBufferWithSize> U2fMessage::GetNextPacket() { |
| + if (NumPackets() > 0) { |
| + scoped_refptr<net::IOBufferWithSize> buf = packets_.front()->GetBuffer(); |
| + packets_.pop_front(); |
| + return buf; |
| + } |
| + return nullptr; |
| +} |
| + |
| +size_t U2fMessage::NumPackets() { |
| + return packets_.size(); |
| +} |
| + |
| +} // namespace device |