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