-
Notifications
You must be signed in to change notification settings - Fork 508
/
Copy pathextended_header.cpp
98 lines (80 loc) · 3 KB
/
extended_header.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/schema/extended_header.h>
#include <cinttypes>
#include <cstring>
#include <executorch/runtime/core/error.h>
#include <executorch/runtime/core/result.h>
namespace executorch {
namespace runtime {
namespace {
/// The expected location of the header length field relative to the beginning
/// of the header.
static constexpr size_t kHeaderLengthOffset = ExtendedHeader::kMagicSize;
/// The expected location of the program_size field relative to the beginning of
/// the header.
static constexpr size_t kHeaderProgramSizeOffset =
kHeaderLengthOffset + sizeof(uint32_t);
/// The expected location of the segment_base_offset field relative to the
/// beginning of the header.
static constexpr size_t kHeaderSegmentBaseOffsetOffset =
kHeaderProgramSizeOffset + sizeof(uint64_t);
/**
* The size of the header that covers the fields known of by this version of
* the code. It's ok for a header to be larger as long as the fields stay in
* the same place, but this code will ignore any new fields.
*/
static constexpr size_t kMinimumHeaderLength =
kHeaderSegmentBaseOffsetOffset + sizeof(uint64_t);
/// Interprets the 4 bytes at `data` as a little-endian uint32_t.
uint32_t GetUInt32LE(const uint8_t* data) {
return (uint32_t)data[0] | ((uint32_t)data[1] << 8) |
((uint32_t)data[2] << 16) | ((uint32_t)data[3] << 24);
}
/// Interprets the 8 bytes at `data` as a little-endian uint64_t.
uint64_t GetUInt64LE(const uint8_t* data) {
return (uint64_t)data[0] | ((uint64_t)data[1] << 8) |
((uint64_t)data[2] << 16) | ((uint64_t)data[3] << 24) |
((uint64_t)data[4] << 32) | ((uint64_t)data[5] << 40) |
((uint64_t)data[6] << 48) | ((uint64_t)data[7] << 56);
}
} // namespace
/* static */ Result<ExtendedHeader> ExtendedHeader::Parse(
const void* data,
size_t size) {
if (size < ExtendedHeader::kNumHeadBytes) {
return Error::InvalidArgument;
}
const uint8_t* header =
reinterpret_cast<const uint8_t*>(data) + kHeaderOffset;
// Check magic bytes.
if (std::memcmp(header, ExtendedHeader::kMagic, ExtendedHeader::kMagicSize) !=
0) {
return Error::NotFound;
}
// Check header length.
uint32_t header_length = GetUInt32LE(header + kHeaderLengthOffset);
if (header_length < kMinimumHeaderLength) {
ET_LOG(
Error,
"Extended header length %" PRIu32 " < %zu",
header_length,
kMinimumHeaderLength);
return Error::InvalidProgram;
}
// The header is present and apparently valid.
return ExtendedHeader{
/*program_size=*/GetUInt64LE(header + kHeaderProgramSizeOffset),
/*segment_base_offset=*/
GetUInt64LE(header + kHeaderSegmentBaseOffsetOffset),
};
}
// Define storage for the static.
constexpr char ExtendedHeader::kMagic[kMagicSize];
} // namespace runtime
} // namespace executorch