Skip to content

Instantly share code, notes, and snippets.

@snipsnipsnip
Last active August 24, 2019 15:37
Show Gist options
  • Save snipsnipsnip/150436 to your computer and use it in GitHub Desktop.
Save snipsnipsnip/150436 to your computer and use it in GitHub Desktop.
sfx
ER=sfx_ready.exe
E=sfx.exe
OBJS=sfx.obj
TEXT=text.txt
all: $(ER)
clean:
del $(OBJS) $(E) $(ER)
$(ER): $(E) $(TEXT)
copy /b $(E)+$(TEXT) $(ER)
$(E): sfx.c
cl /DWIN32 /D_DEBUG /D_WINDOWS /TP $? /link user32.lib
// originally from http://www.strchr.com/creating_self-extracting_executables
// (c) Peter Kankowski, 2006 http://smallcode.weblogs.us kankowski@narod.ru
#include <windows.h>
// **** Reading from exe file ****
typedef enum
{
ERR_OK = 0,
ERR_READFAILED = 1,
ERR_NOINFO = 2,
ERR_BADFORMAT = 3,
}
Err;
typedef struct
{
BYTE *data; // you need to free it
DWORD size;
}
Data;
Err ReadFromExeFile(Data *out)
{
/* Reads data attached to the exe file and calls
ProcessData(pointertodata, datasize).
Return values:
* ERR_READFAILED - read from exe file had failed;
* ERR_BADFORMAT - invalid format of the exe file;
* ERR_NOINFO - no info was attached.
If the data were read OK, it returns the return value of ProcessData.
*/
#define ErrIf(a) if (a) goto HANDLE_BADFORMAT;
BYTE buff[4096]; DWORD read; BYTE* data;
// Open exe file
GetModuleFileName(NULL, (CHAR*)buff, sizeof(buff));
HANDLE hFile = CreateFile((CHAR*)buff, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile) return ERR_READFAILED;
if (!ReadFile(hFile, buff, sizeof(buff), &read, NULL)) goto HANDLE_READFAILED;
IMAGE_DOS_HEADER* dosheader = (IMAGE_DOS_HEADER*)buff;
ErrIf(dosheader->e_magic != IMAGE_DOS_SIGNATURE);
ErrIf(ULONG(dosheader->e_lfanew) >= ULONG(sizeof(buff) - sizeof(IMAGE_NT_HEADERS32)));
// Locate PE header
IMAGE_NT_HEADERS32* header = (IMAGE_NT_HEADERS32*)(buff + dosheader->e_lfanew);
ErrIf(header->Signature != IMAGE_NT_SIGNATURE);
IMAGE_SECTION_HEADER* sectiontable =
(IMAGE_SECTION_HEADER*)((BYTE*)header + sizeof(IMAGE_NT_HEADERS32));
ErrIf((BYTE*)sectiontable >= buff + sizeof(buff));
DWORD maxpointer = 0, exesize = 0;
// For each section
for(int i = 0; i < header->FileHeader.NumberOfSections; ++i)
{
if (sectiontable->PointerToRawData > maxpointer)
{
maxpointer = sectiontable->PointerToRawData;
exesize = sectiontable->PointerToRawData + sectiontable->SizeOfRawData;
}
sectiontable++;
}
// Seek to the overlay
DWORD filesize = GetFileSize(hFile, NULL);
if (exesize == filesize) goto HANDLE_NOINFO;
ErrIf(filesize == INVALID_FILE_SIZE || exesize > filesize);
if (SetFilePointer(hFile, exesize, NULL, FILE_BEGIN) ==
INVALID_SET_FILE_POINTER) goto HANDLE_READFAILED;
data = (BYTE*)malloc(filesize - exesize + 8);
if (!ReadFile(hFile, data, filesize - exesize, &read, NULL)) goto HANDLE_WITHFREE;
CloseHandle(hFile);
out->data = data;
out->size = filesize - exesize;
return ERR_OK;
HANDLE_WITHFREE:
free(data);
HANDLE_READFAILED:
CloseHandle(hFile);
return ERR_READFAILED;
HANDLE_BADFORMAT:
CloseHandle(hFile);
return ERR_BADFORMAT;
HANDLE_NOINFO:
CloseHandle(hFile);
return ERR_NOINFO;
#undef ErrIf
}
void ProcessData(BYTE* data, int datasize)
{
*(data + datasize) = '\0';
MessageBox(0, (CHAR*)data, "sfx", MB_ICONINFORMATION);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
Err err;
Data data;
err = ReadFromExeFile(&data);
if (err == ERR_OK)
{
ProcessData(data.data, data.size);
free(data.data);
}
else if (err == ERR_NOINFO)
{
MessageBox(0, TEXT("No data were attached"), "sfx", MB_ICONINFORMATION);
}
else
{
MessageBox(0, TEXT("Can't read from exe file"), "sfx", MB_ICONERROR);
}
return 0;
}
Universal Declaration of Human Rights
Article 1
All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.
Article 2
Everyone is entitled to all the rights and freedoms set forth in this Declaration, without distinction of any kind, such as race, colour, sex, language, religion, political or other opinion, national or social origin, property, birth or other status.
Furthermore, no distinction shall be made on the basis of the political, jurisdictional or international status of the country or territory to which a person belongs, whether it be independent, trust, non-self-governing or under any other limitation of sovereignty.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment