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 "mojo/public/cpp/bindings/lib/message_header_validator.h" |
| 6 |
| 7 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" |
| 8 |
| 9 namespace mojo { |
| 10 namespace internal { |
| 11 namespace { |
| 12 |
| 13 bool IsValidMessageHeader(const internal::MessageHeader* header) { |
| 14 // Validate num_bytes: |
| 15 |
| 16 if (header->num_bytes < sizeof(internal::MessageHeader)) |
| 17 return false; |
| 18 if (internal::Align(header->num_bytes) != header->num_bytes) |
| 19 return false; |
| 20 |
| 21 // Validate num_fields: |
| 22 |
| 23 if (header->num_fields < 2) |
| 24 return false; |
| 25 if (header->num_fields == 2) { |
| 26 if (header->num_bytes != sizeof(internal::MessageHeader)) |
| 27 return false; |
| 28 } else if (header->num_fields == 3) { |
| 29 if (header->num_bytes != sizeof(internal::MessageHeaderWithRequestID)) |
| 30 return false; |
| 31 } else if (header->num_fields > 3) { |
| 32 if (header->num_bytes < sizeof(internal::MessageHeaderWithRequestID)) |
| 33 return false; |
| 34 } |
| 35 |
| 36 // Validate flags (allow unknown bits): |
| 37 |
| 38 // These flags require a RequestID. |
| 39 if (header->num_fields < 3 && |
| 40 ((header->flags & internal::kMessageExpectsResponse) || |
| 41 (header->flags & internal::kMessageIsResponse))) |
| 42 return false; |
| 43 |
| 44 // These flags are mutually exclusive. |
| 45 if ((header->flags & internal::kMessageExpectsResponse) && |
| 46 (header->flags & internal::kMessageIsResponse)) |
| 47 return false; |
| 48 |
| 49 return true; |
| 50 } |
| 51 |
| 52 } // namespace |
| 53 |
| 54 MessageHeaderValidator::MessageHeaderValidator(MessageReceiver* next) |
| 55 : next_(next) { |
| 56 assert(next); |
| 57 } |
| 58 |
| 59 bool MessageHeaderValidator::Accept(Message* message) { |
| 60 if (!IsValidMessageHeader(message->header())) |
| 61 return false; // Simulate an unhappy receiver. |
| 62 |
| 63 return next_->Accept(message); |
| 64 } |
| 65 |
| 66 bool MessageHeaderValidator::AcceptWithResponder(Message* message, |
| 67 MessageReceiver* responder) { |
| 68 assert(false); // Not reached! |
| 69 return false; |
| 70 } |
| 71 |
| 72 } // namespace internal |
| 73 } // namespace mojo |
OLD | NEW |