Skip to content

Instantly share code, notes, and snippets.

@Jackbail4
Last active March 10, 2022 16:55
Show Gist options
  • Save Jackbail4/c57947a4015e56a334a0d96bdcd3598b to your computer and use it in GitHub Desktop.
Save Jackbail4/c57947a4015e56a334a0d96bdcd3598b to your computer and use it in GitHub Desktop.
#include <windows.h>
#include <cstdint>
void DisplayError(DWORD ErrCode) {
const char* msg;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, ErrCode, 0, (char*)&msg, 0, 0);
MessageBoxA(0, msg, "Problem", MB_OK);
exit(0);
}
__forceinline bool BitFlipFile(wchar_t* FilePath) {
HANDLE hFile = CreateFileA((char*)FilePath, GENERIC_ALL, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile == INVALID_HANDLE_VALUE)
DisplayError(GetLastError());
uint64_t FileSize = GetFileSize(hFile, nullptr);
if (!FileSize)
DisplayError(GetLastError());
void* File = malloc(FileSize);
if (File == nullptr)
DisplayError(GetLastError());
if (!ReadFile(hFile, File, FileSize, nullptr, nullptr))
DisplayError(GetLastError());
for (uint64_t i = 0; i < FileSize; i++) {
byte* FileByte = (byte*)((uint64_t)File + i);
*FileByte = ~*FileByte;
}
SetFilePointer(hFile, 0, 0, FILE_BEGIN);
if (!WriteFile(hFile, File, FileSize, nullptr, nullptr))
DisplayError(GetLastError());
free(File);
if (!CloseHandle(hFile))
DisplayError(GetLastError());
return true;
}
int main(int argc, wchar_t* argv[], wchar_t* envp[]) {
BitFlipFile(argv[1]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment