Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/proximity_auth/permit_message.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 | |
| 9 namespace proximity_auth { | |
| 10 namespace { | |
| 11 | |
| 12 // The length of the message header, in bytes. | |
| 13 const int kHeaderLength = 3; | |
| 14 | |
| 15 // TODO(isherman): Uncomment this once code that uses it lands. | |
| 16 // The protocol version of the message format. | |
| 17 //const int kMessageFormatVersion = 3; | |
| 18 | |
| 19 // Parses the |message|'s header and returns the full length of the message | |
| 20 // (i.e. the sum of the lengths of the message header and body), as recorded in | |
| 21 // the header. Returns -1 if the header is incomplete or malformed. | |
| 22 int ReadLengthFromHeader(const char* message, int size) { | |
| 23 if (size < kHeaderLength) | |
| 24 return -1; | |
| 25 | |
| 26 int body_length = | |
| 27 static_cast<int>(message[1]) << 8 | static_cast<int>(message[2]); | |
| 28 return kHeaderLength + body_length; | |
| 29 } | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 PermitMessage::~PermitMessage() { | |
| 34 } | |
| 35 | |
| 36 // static | |
| 37 bool PermitMessage::IsCompleteMessage(const char* bytes, int size) { | |
| 38 int message_length = ReadLengthFromHeader(bytes, size); | |
| 39 return message_length != -1 && size >= message_length; | |
| 40 } | |
| 41 | |
| 42 // static | |
| 43 scoped_ptr<PermitMessage> PermitMessage::FromBytes(const char* bytes, | |
| 44 int size) { | |
| 45 if (!IsCompleteMessage(bytes, size)) { | |
| 46 VLOG(1) << "Error: Cannot parse incomplete received message."; | |
| 47 return scoped_ptr<PermitMessage>(); | |
| 48 } | |
| 49 | |
| 50 int message_length = ReadLengthFromHeader(bytes, size); | |
| 51 scoped_ptr<PermitMessage> message = FromBytesImpl(bytes, message_length); | |
| 52 if (!message) | |
| 53 VLOG(1) << "Error: Unable to parse message."; | |
| 54 | |
| 55 return message.Pass(); | |
| 56 } | |
| 57 | |
| 58 PermitMessage::PermitMessage() { | |
| 59 // TODO(isherman): Implement. | |
| 60 } | |
| 61 | |
| 62 // static | |
| 63 scoped_ptr<PermitMessage> PermitMessage::FromBytesImpl(const char* bytes, | |
|
Tim Song
2014/09/05 00:30:34
Why not put this in the namespace above?
Ilya Sherman
2014/09/05 01:02:11
I've since decided that this method isn't needed;
| |
| 64 int size) { | |
| 65 // TODO(isherman): Implement. | |
| 66 return scoped_ptr<PermitMessage>(); | |
| 67 } | |
| 68 | |
| 69 } // namespace proximity_auth | |
| OLD | NEW |