Index: components/proximity_auth/permit_message.cc |
diff --git a/components/proximity_auth/permit_message.cc b/components/proximity_auth/permit_message.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e6db98953bcab300a79bba69fdc4ca219844d87b |
--- /dev/null |
+++ b/components/proximity_auth/permit_message.cc |
@@ -0,0 +1,69 @@ |
+// Copyright 2014 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 "components/proximity_auth/permit_message.h" |
+ |
+#include "base/logging.h" |
+ |
+namespace proximity_auth { |
+namespace { |
+ |
+// The length of the message header, in bytes. |
+const int kHeaderLength = 3; |
+ |
+// TODO(isherman): Uncomment this once code that uses it lands. |
+// The protocol version of the message format. |
+//const int kMessageFormatVersion = 3; |
+ |
+// Parses the |message|'s header and returns the full length of the message |
+// (i.e. the sum of the lengths of the message header and body), as recorded in |
+// the header. Returns -1 if the header is incomplete or malformed. |
+int ReadLengthFromHeader(const char* message, int size) { |
+ if (size < kHeaderLength) |
+ return -1; |
+ |
+ int body_length = |
+ static_cast<int>(message[1]) << 8 | static_cast<int>(message[2]); |
+ return kHeaderLength + body_length; |
+} |
+ |
+} // namespace |
+ |
+PermitMessage::~PermitMessage() { |
+} |
+ |
+// static |
+bool PermitMessage::IsCompleteMessage(const char* bytes, int size) { |
+ int message_length = ReadLengthFromHeader(bytes, size); |
+ return message_length != -1 && size >= message_length; |
+} |
+ |
+// static |
+scoped_ptr<PermitMessage> PermitMessage::FromBytes(const char* bytes, |
+ int size) { |
+ if (!IsCompleteMessage(bytes, size)) { |
+ VLOG(1) << "Error: Cannot parse incomplete received message."; |
+ return scoped_ptr<PermitMessage>(); |
+ } |
+ |
+ int message_length = ReadLengthFromHeader(bytes, size); |
+ scoped_ptr<PermitMessage> message = FromBytesImpl(bytes, message_length); |
+ if (!message) |
+ VLOG(1) << "Error: Unable to parse message."; |
+ |
+ return message.Pass(); |
+} |
+ |
+PermitMessage::PermitMessage() { |
+ // TODO(isherman): Implement. |
+} |
+ |
+// static |
+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;
|
+ int size) { |
+ // TODO(isherman): Implement. |
+ return scoped_ptr<PermitMessage>(); |
+} |
+ |
+} // namespace proximity_auth |