|
| 1 | +/** |
| 2 | + * ██╗ ██╗███╗ ███╗ █████╗ ██╗ ██╗ █████╗ ██████╗ ███████╗ |
| 3 | + * ██║ ██║████╗ ████║██╔══██╗██║ ██║██╔══██╗██╔══██╗██╔════╝ |
| 4 | + * ██║ ██║██╔████╔██║███████║██║ █╗ ██║███████║██████╔╝█████╗ |
| 5 | + * ╚██╗ ██╔╝██║╚██╔╝██║██╔══██║██║███╗██║██╔══██║██╔══██╗██╔══╝ |
| 6 | + * ╚████╔╝ ██║ ╚═╝ ██║██║ ██║╚███╔███╔╝██║ ██║██║ ██║███████╗ |
| 7 | + * ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚══╝╚══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ |
| 8 | + * |
| 9 | + * C++ VM detection library |
| 10 | + * |
| 11 | + * =============================================================== |
| 12 | + * |
| 13 | + * This program computes a CRC32 checksum of boot logo images |
| 14 | + * via hardware‐accelerated intrinsics in 8-byte chunks. |
| 15 | + * |
| 16 | + * =============================================================== |
| 17 | + * |
| 18 | + * - Made by: @Requiem (https://github.com/NotRequiem) |
| 19 | + * - Repository: https://github.com/kernelwernel/VMAware |
| 20 | + * - License: MIT |
| 21 | + */ |
| 22 | + |
| 23 | +#include <immintrin.h> |
| 24 | +#include <fstream> |
| 25 | +#include <vector> |
| 26 | +#include <iostream> |
| 27 | +#include <cstdint> |
| 28 | + |
| 29 | +static inline uint32_t crc32c_bmp(const char* filename) { |
| 30 | + std::ifstream in{ filename, std::ios::binary | std::ios::ate }; |
| 31 | + if (!in) return 0; |
| 32 | + auto size = in.tellg(); |
| 33 | + in.seekg(0, std::ios::beg); |
| 34 | + |
| 35 | + std::vector<uint8_t> buf(static_cast<size_t>(size)); |
| 36 | + in.read(reinterpret_cast<char*>(buf.data()), size); |
| 37 | + |
| 38 | + // offset in BMP header (bytes 10–13) |
| 39 | + uint32_t offset = *reinterpret_cast<uint32_t*>(buf.data() + 10); |
| 40 | + uint8_t* bmp = buf.data() + offset; |
| 41 | + size_t len = buf.size() - offset; |
| 42 | + |
| 43 | +#if defined(_M_X64) || defined(__x86_64__) |
| 44 | + uint64_t crc64 = 0xFFFFFFFFull; |
| 45 | + auto q64 = len / 8; |
| 46 | + auto const* p64 = reinterpret_cast<uint64_t const*>(bmp); |
| 47 | + for (size_t i = 0; i < q64; ++i) |
| 48 | + crc64 = _mm_crc32_u64(crc64, p64[i]); |
| 49 | + |
| 50 | + uint32_t crc = static_cast<uint32_t>(crc64); |
| 51 | + auto const* tail = reinterpret_cast<uint8_t const*>(p64 + q64); |
| 52 | + for (size_t i = 0, r = len & 7; i < r; ++i) |
| 53 | + crc = _mm_crc32_u8(crc, tail[i]); |
| 54 | +#else |
| 55 | + uint32_t crc = 0xFFFFFFFFu; |
| 56 | + auto q32 = len / 4; |
| 57 | + auto const* p32 = reinterpret_cast<uint32_t const*>(bmp); |
| 58 | + for (size_t i = 0; i < q32; ++i) |
| 59 | + crc = _mm_crc32_u32(crc, p32[i]); |
| 60 | + |
| 61 | + auto const* tail = reinterpret_cast<uint8_t const*>(p32 + q32); |
| 62 | + for (size_t i = 0, r = len & 3; i < r; ++i) |
| 63 | + crc = _mm_crc32_u8(crc, tail[i]); |
| 64 | +#endif |
| 65 | + |
| 66 | + return crc ^ 0xFFFFFFFFu; |
| 67 | +} |
| 68 | + |
| 69 | +int main(int argc, char** argv) { |
| 70 | + for (int i = 1; i < argc; ++i) { |
| 71 | + uint32_t h = crc32c_bmp(argv[i]); |
| 72 | + std::cout << argv[i] |
| 73 | + << ": 0x" << std::hex << std::uppercase << h |
| 74 | + << std::dec << "\n"; |
| 75 | + } |
| 76 | + return 0; |
| 77 | +} |
0 commit comments