Skip to content

Instantly share code, notes, and snippets.

@ayende

ayende/program.c Secret

Created January 9, 2025 11:59
Show Gist options
  • Save ayende/906811ee6e975b318b7588e2122b7717 to your computer and use it in GitHub Desktop.
Save ayende/906811ee6e975b318b7588e2122b7717 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <windows.h>
#define MB (1024 * 1024)
int main() {
HANDLE hFile, hFile2;
DWORD bytesWritten;
LARGE_INTEGER fileSize, moveAmount;
char* buffer = malloc(MB);
int i;
DeleteFileA("R:\\original_file.bin");
DeleteFileA("R:\\another_file.bin");
hFile = CreateFileA("R:/original_file.bin", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("Error creating file: %d\n", GetLastError());
exit(__LINE__);
}
for (int i = 0; i < 20; i++) {
if (!WriteFile(hFile, buffer, MB, &bytesWritten, NULL)) {
fprintf(stderr, "WriteFile failed on iteration %d: %x\n", i, GetLastError());
exit(__LINE__);
}
}
if (!FlushFileBuffers(hFile)) {
fprintf(stderr, "Could not flush file buffers: %x\n", GetLastError());
exit(__LINE__);
}
FILE_ZERO_DATA_INFORMATION zeroDataInfo;
zeroDataInfo.FileOffset.QuadPart = 6 * MB;
zeroDataInfo.BeyondFinalZero.QuadPart = 18 * MB;
if (!DeviceIoControl(hFile, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, NULL, NULL) ||
!DeviceIoControl(hFile, FSCTL_SET_ZERO_DATA, &zeroDataInfo, sizeof(zeroDataInfo), NULL, 0, NULL, NULL)) {
printf("Error setting zero data: %d\n", GetLastError());
exit(__LINE__);
}
if (!FlushFileBuffers(hFile)) {
fprintf(stderr, "Could not flush file buffers: %x\n", GetLastError());
exit(__LINE__);
}
// Create another file of size 4 MB
hFile2 = CreateFileA("R:/another_file.bin", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile2 == INVALID_HANDLE_VALUE) {
printf("Error creating second file: %d\n", GetLastError());
exit(__LINE__);
}
for (int i = 0; i < 12; i++) {
if (!WriteFile(hFile2, buffer, MB, &bytesWritten, NULL)) {
fprintf(stderr, "WriteFile 2 failed on iteration %d: %x\n", i, GetLastError());
exit(__LINE__);
}
if (bytesWritten != MB) {
fprintf(stderr, "Failed to write full buffer 2 on iteration %d\n", i);
exit(__LINE__);
}
}
HANDLE hMapFile = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
if (hMapFile == NULL) {
fprintf(stderr, "Could not create file mapping object: %x\n", GetLastError());
exit(__LINE__);
}
char* lpMapAddress = MapViewOfFile(hMapFile, FILE_MAP_WRITE, 0, 0, 0);
if (lpMapAddress == NULL) {
fprintf(stderr, "Could not map view of file: %x\n", GetLastError());
exit(__LINE__);
}
moveAmount.QuadPart = 6 * MB;
SetFilePointerEx(hFile, moveAmount, NULL, FILE_BEGIN);
for (i = 6 ; i < 10 ; i++) {
if (!WriteFile(hFile, buffer, MB, &bytesWritten, NULL)) {
fprintf(stderr, "WriteFile failed on iteration %d: %x\n", i, GetLastError());
exit(__LINE__);
}
}
if (!FlushViewOfFile(lpMapAddress, 0)) {
fprintf(stderr, "Could not flush view of file: %x\n", GetLastError());
exit(__LINE__);
}
if (!FlushFileBuffers(hFile)) {
fprintf(stderr, "Could not flush file buffers: %x\n", GetLastError());
exit(__LINE__);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment