| OLD | NEW |
| (Empty) |
| 1 {%- macro validate(struct, class_name) %} | |
| 2 if (!data) | |
| 3 return true; | |
| 4 | |
| 5 if (!ValidateStructHeader( | |
| 6 data, sizeof({{class_name}}), | |
| 7 {{struct.packed.packed_fields|length}}, bounds_checker)) { | |
| 8 return false; | |
| 9 } | |
| 10 | |
| 11 const {{class_name}}* object = static_cast<const {{class_name}}*>(data); | |
| 12 MOJO_ALLOW_UNUSED_LOCAL(object); | |
| 13 | |
| 14 {%- for packed_field in struct.packed.packed_fields %} | |
| 15 {%- set name = packed_field.field.name %} | |
| 16 {%- set kind = packed_field.field.kind %} | |
| 17 {%- if kind|is_object_kind %} | |
| 18 {%- set wrapper_type = kind|cpp_wrapper_type %} | |
| 19 {%- if not kind|is_nullable_kind %} | |
| 20 if (!object->{{name}}.offset) { | |
| 21 ReportValidationError( | |
| 22 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | |
| 23 "null {{name}} field in {{struct.name}} struct"); | |
| 24 return false; | |
| 25 } | |
| 26 {%- endif %} | |
| 27 if (!mojo::internal::ValidateEncodedPointer(&object->{{name}}.offset)) { | |
| 28 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER); | |
| 29 return false; | |
| 30 } | |
| 31 {%- if kind|is_array_kind or kind|is_string_kind %} | |
| 32 if (!{{wrapper_type}}::Data_::Validate< | |
| 33 {{kind|get_array_validate_params|indent(10)}}>( | |
| 34 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), | |
| 35 bounds_checker)) { | |
| 36 {%- elif kind|is_map_kind %} | |
| 37 if (!{{wrapper_type}}::Data_::Validate< | |
| 38 {{kind.value_kind|get_map_validate_params|indent(10)}}>( | |
| 39 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), | |
| 40 bounds_checker)) { | |
| 41 {%- elif kind|is_struct_kind %} | |
| 42 if (!{{kind|get_name_for_kind}}::Data_::Validate( | |
| 43 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), | |
| 44 bounds_checker)) { | |
| 45 {%- else %} | |
| 46 if (!{{wrapper_type}}::Data_::Validate( | |
| 47 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), | |
| 48 bounds_checker)) { | |
| 49 {%- endif %} | |
| 50 return false; | |
| 51 } | |
| 52 {%- elif kind|is_any_handle_kind %} | |
| 53 {%- if not kind|is_nullable_kind %} | |
| 54 if (object->{{name}}.value() == mojo::internal::kEncodedInvalidHandleValue) { | |
| 55 ReportValidationError( | |
| 56 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, | |
| 57 "invalid {{name}} field in {{struct.name}} struct"); | |
| 58 return false; | |
| 59 } | |
| 60 {%- endif %} | |
| 61 if (!bounds_checker->ClaimHandle(object->{{name}})) { | |
| 62 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_HANDLE); | |
| 63 return false; | |
| 64 } | |
| 65 {%- endif %} | |
| 66 {%- endfor %} | |
| 67 | |
| 68 return true; | |
| 69 {%- endmacro %} | |
| 70 | |
| 71 {%- macro field_line(field) %} | |
| 72 {%- set type = field.kind|cpp_field_type %} | |
| 73 {%- set name = field.name -%} | |
| 74 {%- if field.kind.spec == 'b' -%} | |
| 75 uint8_t {{name}} : 1; | |
| 76 {%- elif field.kind|is_enum_kind -%} | |
| 77 int32_t {{name}}; | |
| 78 {%- else -%} | |
| 79 {{type}} {{name}}; | |
| 80 {%- endif %} | |
| 81 {%- endmacro %} | |
| 82 | |
| 83 {%- macro fields(struct) %} | |
| 84 {%- for packed_field in struct.packed.packed_fields %} | |
| 85 {{field_line(packed_field.field)}} | |
| 86 {%- if not loop.last %} | |
| 87 {%- set next_pf = struct.packed.packed_fields[loop.index0 + 1] %} | |
| 88 {%- set pad = next_pf.offset - (packed_field.offset + packed_field.size) %
} | |
| 89 {%- if pad > 0 %} | |
| 90 uint8_t pad{{loop.index0}}_[{{pad}}]; | |
| 91 {%- endif %} | |
| 92 {%- endif %} | |
| 93 {%- endfor -%} | |
| 94 | |
| 95 {%- set num_fields = struct.packed.packed_fields|length %} | |
| 96 {%- if num_fields > 0 %} | |
| 97 {%- set last_field = struct.packed.packed_fields[num_fields - 1] %} | |
| 98 {%- set offset = last_field.offset + last_field.size %} | |
| 99 {%- set pad = offset|get_pad(8) -%} | |
| 100 {%- if pad > 0 %} | |
| 101 uint8_t padfinal_[{{pad}}]; | |
| 102 {%- endif %} | |
| 103 {%- endif %} | |
| 104 {%- endmacro %} | |
| 105 | |
| 106 {%- macro encodes(struct) -%} | |
| 107 {%- for pf in struct.packed.packed_fields %} | |
| 108 {%- if pf.field.kind|is_object_kind %} | |
| 109 mojo::internal::Encode(&{{pf.field.name}}, handles); | |
| 110 {%- elif pf.field.kind|is_any_handle_kind %} | |
| 111 mojo::internal::EncodeHandle(&{{pf.field.name}}, handles); | |
| 112 {%- endif %} | |
| 113 {%- endfor %} | |
| 114 {%- endmacro -%} | |
| 115 | |
| 116 {%- macro decodes(struct) -%} | |
| 117 {%- for pf in struct.packed.packed_fields %} | |
| 118 {%- if pf.field.kind|is_object_kind %} | |
| 119 mojo::internal::Decode(&{{pf.field.name}}, handles); | |
| 120 {%- elif pf.field.kind|is_any_handle_kind %} | |
| 121 mojo::internal::DecodeHandle(&{{pf.field.name}}, handles); | |
| 122 {%- endif %} | |
| 123 {%- endfor %} | |
| 124 {%- endmacro -%} | |
| OLD | NEW |