Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: mojo/public/c/bindings/lib/message.c

Issue 2163793002: C bindings: Implement _Validate(), and some pre-requisites (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: share common validation utility code between c/c++ tests. remove generating __HasResponse for inte… Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/public/c/bindings/message.h" 5 #include "mojo/public/c/bindings/message.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "mojo/public/c/bindings/struct.h" 9 #include "mojo/public/c/bindings/struct.h"
10 10
11 MojomValidationResult MojomMessage_ValidateHeader(const void* in_buf, 11 MojomValidationResult MojomMessage_ValidateHeader(const void* in_buf,
12 uint32_t in_buf_size) { 12 uint32_t in_buf_size) {
13 const struct MojomStructHeader* header = 13 const struct MojomStructHeader* header =
14 (const struct MojomStructHeader*)in_buf; 14 (const struct MojomStructHeader*)in_buf;
15
15 if (in_buf_size < sizeof(struct MojomStructHeader) || 16 if (in_buf_size < sizeof(struct MojomStructHeader) ||
viettrungluu 2016/07/28 23:03:01 For arrays, you return "illegal memory range" for
vardhan 2016/07/29 21:29:21 True -- this should be split up (but i guess the v
16 in_buf_size < header->num_bytes) { 17 in_buf_size < header->num_bytes)
17 return MOJOM_VALIDATION_UNEXPECTED_STRUCT_HEADER; 18 return MOJOM_VALIDATION_ILLEGAL_MEMORY_RANGE;
18 }
19 19
20 const struct MojomMessage* msg = (const struct MojomMessage*)in_buf; 20 const struct MojomMessage* msg = (const struct MojomMessage*)in_buf;
21 if (header->version == 0u) { 21 if (header->version == 0u) {
22 if (header->num_bytes != sizeof(struct MojomMessage)) { 22 if (header->num_bytes != sizeof(struct MojomMessage)) {
23 return MOJOM_VALIDATION_UNEXPECTED_STRUCT_HEADER; 23 return MOJOM_VALIDATION_UNEXPECTED_STRUCT_HEADER;
24 } 24 }
25 25
26 // Version 0 has no request id and should not have either of these flags. 26 // Version 0 has no request id and should not have either of these flags.
27 if ((msg->flags & MOJOM_MESSAGE_FLAGS_EXPECTS_RESPONSE) || 27 if ((msg->flags & MOJOM_MESSAGE_FLAGS_EXPECTS_RESPONSE) ||
28 (msg->flags & MOJOM_MESSAGE_FLAGS_IS_RESPONSE)) { 28 (msg->flags & MOJOM_MESSAGE_FLAGS_IS_RESPONSE)) {
(...skipping 11 matching lines...) Expand all
40 40
41 // Mutually exclusive flags. 41 // Mutually exclusive flags.
42 if ((msg->flags & MOJOM_MESSAGE_FLAGS_EXPECTS_RESPONSE) && 42 if ((msg->flags & MOJOM_MESSAGE_FLAGS_EXPECTS_RESPONSE) &&
43 (msg->flags & MOJOM_MESSAGE_FLAGS_IS_RESPONSE)) { 43 (msg->flags & MOJOM_MESSAGE_FLAGS_IS_RESPONSE)) {
44 return MOJOM_VALIDATION_MESSAGE_HEADER_INVALID_FLAGS; 44 return MOJOM_VALIDATION_MESSAGE_HEADER_INVALID_FLAGS;
45 } 45 }
46 46
47 // Accept unknown versions of the message header to be future-proof. 47 // Accept unknown versions of the message header to be future-proof.
48 return MOJOM_VALIDATION_ERROR_NONE; 48 return MOJOM_VALIDATION_ERROR_NONE;
49 } 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) &&
viettrungluu 2016/07/28 23:03:01 nit: I'd suggest always putting (...) around the e
vardhan 2016/07/29 21:29:21 Done.
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
viettrungluu 2016/07/28 23:03:01 "
vardhan 2016/07/29 21:29:21 Done.
72 ? MOJOM_VALIDATION_ERROR_NONE
73 : MOJOM_VALIDATION_MESSAGE_HEADER_INVALID_FLAGS;
74 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698