| {%- macro validate(struct, class_name) %} |
| if (!data) |
| return true; |
| |
| if (!ValidateStructHeader( |
| data, sizeof({{class_name}}), |
| {{struct.packed.packed_fields|length}}, bounds_checker)) { |
| return false; |
| } |
| |
| const {{class_name}}* MOJO_ALLOW_UNUSED object = |
| static_cast<const {{class_name}}*>(data); |
| |
| {%- for packed_field in struct.packed.packed_fields %} |
| {%- set name = packed_field.field.name %} |
| {%- set kind = packed_field.field.kind %} |
| {%- if kind|is_object_kind %} |
| {%- set wrapper_type = kind|cpp_wrapper_type %} |
| {%- if not kind|is_nullable_kind %} |
| if (!object->{{name}}.offset) { |
| ReportValidationError( |
| mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| "null {{name}} field in {{struct.name}} struct"); |
| return false; |
| } |
| {%- endif %} |
| if (!mojo::internal::ValidateEncodedPointer(&object->{{name}}.offset)) { |
| ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER); |
| return false; |
| } |
| {%- if kind|is_any_array_kind or kind|is_string_kind %} |
| if (!{{wrapper_type}}::Data_::Validate< |
| {{kind|get_array_validate_params|indent(10)}}>( |
| mojo::internal::DecodePointerRaw(&object->{{name}}.offset), |
| bounds_checker)) { |
| {%- else %} |
| if (!{{wrapper_type}}::Data_::Validate( |
| mojo::internal::DecodePointerRaw(&object->{{name}}.offset), |
| bounds_checker)) { |
| {%- endif %} |
| return false; |
| } |
| {%- elif kind|is_any_handle_kind %} |
| {%- if not kind|is_nullable_kind %} |
| if (object->{{name}}.value() == mojo::internal::kEncodedInvalidHandleValue) { |
| ReportValidationError( |
| mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, |
| "invalid {{name}} field in {{struct.name}} struct"); |
| return false; |
| } |
| {%- endif %} |
| if (!bounds_checker->ClaimHandle(object->{{name}})) { |
| ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_HANDLE); |
| return false; |
| } |
| {%- endif %} |
| {%- endfor %} |
| |
| return true; |
| {%- endmacro %} |
| |
| {%- macro field_line(field) %} |
| {%- set type = field.kind|cpp_field_type %} |
| {%- set name = field.name -%} |
| {%- if field.kind.spec == 'b' -%} |
| uint8_t {{name}} : 1; |
| {%- elif field.kind|is_enum_kind -%} |
| int32_t {{name}}; |
| {%- else -%} |
| {{type}} {{name}}; |
| {%- endif %} |
| {%- endmacro %} |
| |
| {%- macro fields(struct) %} |
| {%- for packed_field in struct.packed.packed_fields %} |
| {{field_line(packed_field.field)}} |
| {%- if not loop.last %} |
| {%- set next_pf = struct.packed.packed_fields[loop.index0 + 1] %} |
| {%- set pad = next_pf.offset - (packed_field.offset + packed_field.size) %} |
| {%- if pad > 0 %} |
| uint8_t pad{{loop.index0}}_[{{pad}}]; |
| {%- endif %} |
| {%- endif %} |
| {%- endfor -%} |
| |
| {%- set num_fields = struct.packed.packed_fields|length %} |
| {%- if num_fields > 0 %} |
| {%- set last_field = struct.packed.packed_fields[num_fields - 1] %} |
| {%- set offset = last_field.offset + last_field.size %} |
| {%- set pad = offset|get_pad(8) -%} |
| {%- if pad > 0 %} |
| uint8_t padfinal_[{{pad}}]; |
| {%- endif %} |
| {%- endif %} |
| {%- endmacro %} |
| |
| {%- macro encodes(struct) -%} |
| {%- for pf in struct.packed.packed_fields %} |
| {%- if pf.field.kind|is_object_kind %} |
| mojo::internal::Encode(&{{pf.field.name}}, handles); |
| {%- elif pf.field.kind|is_any_handle_kind %} |
| mojo::internal::EncodeHandle(&{{pf.field.name}}, handles); |
| {%- endif %} |
| {%- endfor %} |
| {%- endmacro -%} |
| |
| {%- macro decodes(struct) -%} |
| {%- for pf in struct.packed.packed_fields %} |
| {%- if pf.field.kind|is_object_kind %} |
| mojo::internal::Decode(&{{pf.field.name}}, handles); |
| {%- elif pf.field.kind|is_any_handle_kind %} |
| mojo::internal::DecodeHandle(&{{pf.field.name}}, handles); |
| {%- endif %} |
| {%- endfor %} |
| {%- endmacro -%} |