diff options
Diffstat (limited to 'image_utils.h')
-rw-r--r-- | image_utils.h | 52 |
1 files changed, 42 insertions, 10 deletions
diff --git a/image_utils.h b/image_utils.h index 3765763..868e358 100644 --- a/image_utils.h +++ b/image_utils.h @@ -8,6 +8,7 @@ #include <stddef.h> #include <stdint.h> +#include "base/macros.h" #include "base/numerics/safe_conversions.h" #include "base/optional.h" #include "components/zucchini/buffer_view.h" @@ -137,20 +138,51 @@ struct EquivalenceCandidate { double similarity; }; -// Enumerations for supported executables. +template <size_t N> +inline constexpr uint32_t ExeTypeToUint32(const char (&exe_type)[N]) { + static_assert(N == 5, "Expected ExeType of length 4 + 1 null byte."); + return (exe_type[3] << 24) | (exe_type[2] << 16) | (exe_type[1] << 8) | + exe_type[0]; +} + +// Enumerations for supported executables. Values in this enum must be distinct. +// Once present, values should never be altered or removed to ensure backwards +// compatibility and patch type collision avoidance. enum ExecutableType : uint32_t { kExeTypeUnknown = UINT32_MAX, - kExeTypeNoOp = 0, - kExeTypeWin32X86 = 1, - kExeTypeWin32X64 = 2, - kExeTypeElfX86 = 3, - kExeTypeElfX64 = 4, - kExeTypeElfArm32 = 5, - kExeTypeElfAArch64 = 6, - kExeTypeDex = 7, - kNumExeType + kExeTypeNoOp = ExeTypeToUint32("NoOp"), + kExeTypeWin32X86 = ExeTypeToUint32("Px86"), + kExeTypeWin32X64 = ExeTypeToUint32("Px64"), + kExeTypeElfX86 = ExeTypeToUint32("Ex86"), + kExeTypeElfX64 = ExeTypeToUint32("Ex64"), + kExeTypeElfArm32 = ExeTypeToUint32("EA32"), + kExeTypeElfAArch64 = ExeTypeToUint32("EA64"), + kExeTypeDex = ExeTypeToUint32("DEX "), }; +constexpr ExecutableType CastToExecutableType(uint32_t possible_exe_type) { + switch (static_cast<ExecutableType>(possible_exe_type)) { + case kExeTypeNoOp: // Falls through. + case kExeTypeWin32X86: // Falls through. + case kExeTypeWin32X64: // Falls through. + case kExeTypeElfX86: // Falls through. + case kExeTypeElfX64: // Falls through. + case kExeTypeElfArm32: // Falls through. + case kExeTypeElfAArch64: // Falls through. + case kExeTypeDex: // Falls through. + case kExeTypeUnknown: + return static_cast<ExecutableType>(possible_exe_type); + default: + NOTREACHED() << "Unknown type."; + return kExeTypeUnknown; + } +} + +inline std::string CastExecutableTypeToString(ExecutableType exe_type) { + uint32_t v = static_cast<uint32_t>(exe_type); + return {v & 0xFF, (v >> 8) & 0xFF, (v >> 16) & 0xFF, (v >> 24) & 0xFF}; +} + // A region in an image with associated executable type |exe_type|. If // |exe_type == kExeTypeNoOp|, then the Element represents a region of raw data. struct Element : public BufferRegion { |