| OLD | NEW |
| 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/union.h" | 5 #include "mojo/public/c/bindings/union.h" |
| 6 | 6 |
| 7 #include <assert.h> | 7 #include <assert.h> |
| 8 #include <string.h> |
| 8 | 9 |
| 9 #include "mojo/public/c/bindings/lib/type_descriptor.h" | 10 #include "mojo/public/c/bindings/lib/type_descriptor.h" |
| 10 | 11 |
| 11 size_t MojomUnion_ComputeSerializedSize( | 12 size_t MojomUnion_ComputeSerializedSize( |
| 12 const struct MojomTypeDescriptorUnion* in_type_desc, | 13 const struct MojomTypeDescriptorUnion* in_type_desc, |
| 13 const struct MojomUnionLayout* in_union_data) { | 14 const struct MojomUnionLayout* in_union_data) { |
| 14 assert(in_type_desc); | 15 assert(in_type_desc); |
| 15 assert(in_union_data); | 16 assert(in_union_data); |
| 16 | 17 |
| 17 for (size_t i = 0; i < in_type_desc->num_entries; i++) { | 18 for (size_t i = 0; i < in_type_desc->num_entries; i++) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 48 if (entry->elem_type == MOJOM_TYPE_DESCRIPTOR_TYPE_POD) | 49 if (entry->elem_type == MOJOM_TYPE_DESCRIPTOR_TYPE_POD) |
| 49 continue; | 50 continue; |
| 50 | 51 |
| 51 MojomType_DispatchEncodePointersAndHandles( | 52 MojomType_DispatchEncodePointersAndHandles( |
| 52 entry->elem_type, | 53 entry->elem_type, |
| 53 entry->elem_descriptor, | 54 entry->elem_descriptor, |
| 54 entry->nullable, | 55 entry->nullable, |
| 55 &inout_union->data, | 56 &inout_union->data, |
| 56 in_buf_size - ((char*)&inout_union->data - (char*)inout_union), | 57 in_buf_size - ((char*)&inout_union->data - (char*)inout_union), |
| 57 inout_handles_buffer); | 58 inout_handles_buffer); |
| 59 |
| 60 break; |
| 58 } | 61 } |
| 59 } | 62 } |
| 60 | 63 |
| 61 void MojomUnion_DecodePointersAndHandles( | 64 void MojomUnion_DecodePointersAndHandles( |
| 62 const struct MojomTypeDescriptorUnion* in_type_desc, | 65 const struct MojomTypeDescriptorUnion* in_type_desc, |
| 63 struct MojomUnionLayout* inout_union, | 66 struct MojomUnionLayout* inout_union, |
| 64 uint32_t in_union_size, | 67 uint32_t in_union_size, |
| 65 MojoHandle* inout_handles, | 68 MojoHandle* inout_handles, |
| 66 uint32_t in_num_handles) { | 69 uint32_t in_num_handles) { |
| 67 assert(in_union_size >= sizeof(struct MojomUnionLayout)); | 70 assert(in_union_size >= sizeof(struct MojomUnionLayout)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 78 continue; | 81 continue; |
| 79 | 82 |
| 80 MojomType_DispatchDecodePointersAndHandles( | 83 MojomType_DispatchDecodePointersAndHandles( |
| 81 entry->elem_type, | 84 entry->elem_type, |
| 82 entry->elem_descriptor, | 85 entry->elem_descriptor, |
| 83 entry->nullable, | 86 entry->nullable, |
| 84 &inout_union->data, | 87 &inout_union->data, |
| 85 in_union_size - ((char*)&inout_union->data - (char*)inout_union), | 88 in_union_size - ((char*)&inout_union->data - (char*)inout_union), |
| 86 inout_handles, | 89 inout_handles, |
| 87 in_num_handles); | 90 in_num_handles); |
| 91 |
| 92 break; |
| 88 } | 93 } |
| 89 } | 94 } |
| 90 | 95 |
| 91 MojomValidationResult MojomUnion_Validate( | 96 MojomValidationResult MojomUnion_Validate( |
| 92 const struct MojomTypeDescriptorUnion* in_type_desc, | 97 const struct MojomTypeDescriptorUnion* in_type_desc, |
| 93 bool in_nullable, | 98 bool in_nullable, |
| 94 const struct MojomUnionLayout* in_union, | 99 const struct MojomUnionLayout* in_union, |
| 95 uint32_t in_union_size, | 100 uint32_t in_union_size, |
| 96 uint32_t in_num_handles, | 101 uint32_t in_num_handles, |
| 97 struct MojomValidationContext* inout_context) { | 102 struct MojomValidationContext* inout_context) { |
| 98 for (size_t i = 0; i < in_type_desc->num_entries; i++) { | 103 for (size_t i = 0; i < in_type_desc->num_entries; i++) { |
| 99 const struct MojomTypeDescriptorUnionEntry* entry = | 104 const struct MojomTypeDescriptorUnionEntry* entry = |
| 100 &(in_type_desc->entries[i]); | 105 &(in_type_desc->entries[i]); |
| 101 | 106 |
| 102 if (in_union->tag != entry->tag) | 107 if (in_union->tag != entry->tag) |
| 103 continue; | 108 continue; |
| 104 | 109 |
| 105 if (entry->elem_type == MOJOM_TYPE_DESCRIPTOR_TYPE_POD) | 110 if (entry->elem_type == MOJOM_TYPE_DESCRIPTOR_TYPE_POD) |
| 106 continue; | 111 continue; |
| 107 | 112 |
| 108 if (!in_nullable && in_union->size != sizeof(struct MojomUnionLayout)) | 113 if (!in_nullable && in_union->size != sizeof(struct MojomUnionLayout)) |
| 109 return MOJOM_VALIDATION_UNEXPECTED_NULL_UNION; | 114 return MOJOM_VALIDATION_UNEXPECTED_NULL_UNION; |
| 110 | 115 |
| 111 MojomValidationResult result = MojomType_DispatchValidate( | 116 return MojomType_DispatchValidate( |
| 112 entry->elem_type, | 117 entry->elem_type, |
| 113 entry->elem_descriptor, | 118 entry->elem_descriptor, |
| 114 entry->nullable, | 119 entry->nullable, |
| 115 &(in_union->data), | 120 &(in_union->data), |
| 116 in_union_size - ((char*)&(in_union->data) - (char*)in_union), | 121 in_union_size - ((char*)&(in_union->data) - (char*)in_union), |
| 117 in_num_handles, | 122 in_num_handles, |
| 118 inout_context); | 123 inout_context); |
| 119 if (result != MOJOM_VALIDATION_ERROR_NONE) | |
| 120 return result; | |
| 121 } | 124 } |
| 122 return MOJOM_VALIDATION_ERROR_NONE; | 125 return MOJOM_VALIDATION_ERROR_NONE; |
| 123 } | 126 } |
| 127 |
| 128 void MojomUnion_DeepCopy(struct MojomBuffer* buffer, |
| 129 const struct MojomTypeDescriptorUnion* in_type_desc, |
| 130 struct MojomUnionLayout* in_union_data, |
| 131 struct MojomUnionLayout* out_union_data) { |
| 132 memcpy(out_union_data, in_union_data, sizeof(struct MojomUnionLayout)); |
| 133 |
| 134 if (in_union_data->size == 0) |
| 135 return; |
| 136 |
| 137 for (size_t i = 0; i < in_type_desc->num_entries; i++) { |
| 138 const struct MojomTypeDescriptorUnionEntry* entry = |
| 139 &(in_type_desc->entries[i]); |
| 140 if (in_union_data->tag != entry->tag) |
| 141 continue; |
| 142 |
| 143 // We should skip non-pointer types. |
| 144 if (entry->elem_type == MOJOM_TYPE_DESCRIPTOR_TYPE_POD) |
| 145 continue; |
| 146 |
| 147 MojomType_DispatchDeepCopy( |
| 148 buffer, entry->elem_type, entry->elem_descriptor, |
| 149 &(in_union_data->data), &(out_union_data->data)); |
| 150 break; |
| 151 } |
| 152 } |
| OLD | NEW |