OLD | NEW |
| (Empty) |
1 // Copyright 2016 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/bindings/message.h> | |
6 | |
7 #include <mojo/bindings/struct.h> | |
8 #include <stdint.h> | |
9 | |
10 MojomValidationResult MojomMessage_ValidateHeader(const void* in_buf, | |
11 uint32_t in_buf_size) { | |
12 const struct MojomStructHeader* header = | |
13 (const struct MojomStructHeader*)in_buf; | |
14 | |
15 if (in_buf_size < sizeof(struct MojomStructHeader) || | |
16 in_buf_size < header->num_bytes) | |
17 return MOJOM_VALIDATION_ILLEGAL_MEMORY_RANGE; | |
18 | |
19 const struct MojomMessage* msg = (const struct MojomMessage*)in_buf; | |
20 if (header->version == 0u) { | |
21 if (header->num_bytes != sizeof(struct MojomMessage)) { | |
22 return MOJOM_VALIDATION_UNEXPECTED_STRUCT_HEADER; | |
23 } | |
24 | |
25 // Version 0 has no request id and should not have either of these flags. | |
26 if ((msg->flags & MOJOM_MESSAGE_FLAGS_EXPECTS_RESPONSE) || | |
27 (msg->flags & MOJOM_MESSAGE_FLAGS_IS_RESPONSE)) { | |
28 return MOJOM_VALIDATION_MESSAGE_HEADER_MISSING_REQUEST_ID; | |
29 } | |
30 } else if (header->version == 1u) { | |
31 if (header->num_bytes != sizeof(struct MojomMessageWithRequestId)) { | |
32 return MOJOM_VALIDATION_UNEXPECTED_STRUCT_HEADER; | |
33 } | |
34 } else if (header->version > 1u) { | |
35 if (header->num_bytes < sizeof(struct MojomMessageWithRequestId)) { | |
36 return MOJOM_VALIDATION_UNEXPECTED_STRUCT_HEADER; | |
37 } | |
38 } | |
39 | |
40 // Mutually exclusive flags. | |
41 if ((msg->flags & MOJOM_MESSAGE_FLAGS_EXPECTS_RESPONSE) && | |
42 (msg->flags & MOJOM_MESSAGE_FLAGS_IS_RESPONSE)) { | |
43 return MOJOM_VALIDATION_MESSAGE_HEADER_INVALID_FLAGS; | |
44 } | |
45 | |
46 // Accept unknown versions of the message header to be future-proof. | |
47 return MOJOM_VALIDATION_ERROR_NONE; | |
48 } | |
49 | |
50 MojomValidationResult MojomMessage_ValidateRequestExpectingResponse( | |
51 const void* in_buf) { | |
52 const struct MojomMessage* msg = (const struct MojomMessage*)in_buf; | |
53 return (msg->flags & MOJOM_MESSAGE_FLAGS_EXPECTS_RESPONSE) | |
54 ? MOJOM_VALIDATION_ERROR_NONE | |
55 : MOJOM_VALIDATION_MESSAGE_HEADER_INVALID_FLAGS; | |
56 } | |
57 | |
58 MojomValidationResult MojomMessage_ValidateRequestWithoutResponse( | |
59 const void* in_buf) { | |
60 const struct MojomMessage* msg = (const struct MojomMessage*)in_buf; | |
61 return (!(msg->flags & MOJOM_MESSAGE_FLAGS_EXPECTS_RESPONSE) && | |
62 !(msg->flags & MOJOM_MESSAGE_FLAGS_IS_RESPONSE)) | |
63 ? MOJOM_VALIDATION_ERROR_NONE | |
64 : MOJOM_VALIDATION_MESSAGE_HEADER_INVALID_FLAGS; | |
65 } | |
66 | |
67 MojomValidationResult MojomMessage_ValidateResponse( | |
68 const void* in_buf) { | |
69 const struct MojomMessage* msg = (const struct MojomMessage*)in_buf; | |
70 return (msg->flags & MOJOM_MESSAGE_FLAGS_IS_RESPONSE) | |
71 ? MOJOM_VALIDATION_ERROR_NONE | |
72 : MOJOM_VALIDATION_MESSAGE_HEADER_INVALID_FLAGS; | |
73 } | |
OLD | NEW |