mirror of
https://github.com/esphome/esphome.git
synced 2026-05-28 13:37:24 +08:00
[ota] Collapse Mach-O 32/64 paths
cputype/cpusubtype/filetype have identical offsets in mach_header and mach_header_64. Read the common prefix once and dispatch only on endianness; drops the unused mach-o/fat.h include.
This commit is contained in:
@@ -20,7 +20,6 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
#include <mach-o/fat.h>
|
|
||||||
#include <mach-o/loader.h>
|
#include <mach-o/loader.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -125,43 +124,32 @@ struct MachOIdent {
|
|||||||
|
|
||||||
MachOIdent parse_macho_(const uint8_t *buf, size_t len) {
|
MachOIdent parse_macho_(const uint8_t *buf, size_t len) {
|
||||||
MachOIdent out{};
|
MachOIdent out{};
|
||||||
if (len < sizeof(struct mach_header_64))
|
// mach_header is the common prefix of mach_header and mach_header_64;
|
||||||
|
// cputype/cpusubtype/filetype have identical offsets in both.
|
||||||
|
if (len < sizeof(struct mach_header))
|
||||||
return out;
|
return out;
|
||||||
uint32_t magic;
|
uint32_t magic;
|
||||||
std::memcpy(&magic, buf, sizeof(magic));
|
std::memcpy(&magic, buf, sizeof(magic));
|
||||||
// Thin Mach-O only; the running exe is also thin (slice path).
|
bool swap;
|
||||||
if (magic == MH_MAGIC_64 || magic == MH_CIGAM_64) {
|
if (magic == MH_MAGIC || magic == MH_MAGIC_64) {
|
||||||
struct mach_header_64 hdr;
|
swap = false;
|
||||||
std::memcpy(&hdr, buf, sizeof(hdr));
|
} else if (magic == MH_CIGAM || magic == MH_CIGAM_64) {
|
||||||
if (magic == MH_CIGAM_64) {
|
swap = true;
|
||||||
hdr.cputype = OSSwapInt32(hdr.cputype);
|
} else {
|
||||||
hdr.cpusubtype = OSSwapInt32(hdr.cpusubtype);
|
|
||||||
hdr.filetype = OSSwapInt32(hdr.filetype);
|
|
||||||
}
|
|
||||||
if (hdr.filetype != MH_EXECUTE)
|
|
||||||
return out;
|
|
||||||
out.cputype = hdr.cputype;
|
|
||||||
out.cpusubtype = hdr.cpusubtype;
|
|
||||||
out.valid = true;
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
if (magic == MH_MAGIC || magic == MH_CIGAM) {
|
struct mach_header hdr;
|
||||||
if (len < sizeof(struct mach_header))
|
std::memcpy(&hdr, buf, sizeof(hdr));
|
||||||
return out;
|
if (swap) {
|
||||||
struct mach_header hdr;
|
hdr.cputype = OSSwapInt32(hdr.cputype);
|
||||||
std::memcpy(&hdr, buf, sizeof(hdr));
|
hdr.cpusubtype = OSSwapInt32(hdr.cpusubtype);
|
||||||
if (magic == MH_CIGAM) {
|
hdr.filetype = OSSwapInt32(hdr.filetype);
|
||||||
hdr.cputype = OSSwapInt32(hdr.cputype);
|
|
||||||
hdr.cpusubtype = OSSwapInt32(hdr.cpusubtype);
|
|
||||||
hdr.filetype = OSSwapInt32(hdr.filetype);
|
|
||||||
}
|
|
||||||
if (hdr.filetype != MH_EXECUTE)
|
|
||||||
return out;
|
|
||||||
out.cputype = hdr.cputype;
|
|
||||||
out.cpusubtype = hdr.cpusubtype;
|
|
||||||
out.valid = true;
|
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
|
if (hdr.filetype != MH_EXECUTE)
|
||||||
|
return out;
|
||||||
|
out.cputype = hdr.cputype;
|
||||||
|
out.cpusubtype = hdr.cpusubtype;
|
||||||
|
out.valid = true;
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user