This software and its source code (if available) are licensed under the Apache-2.0 license (unless otherwise specified) which DOES NOT APPLY to any third-party code (such as zlib for example) and borrowed Assembler code (ripped code from other software executable files).
#include <stdio.h>
#include <windows.h>
/*#define DEBUG 1*/
/*
This tool requires PATCHW32.dll from "RTPatch Apply" package - get it here (version v6.03):
http://siberian-studio.ru/files_pakers.html#rtpatch
Or here (version v12.00 and v12.21) from "myRTP command-line interface" package:
http://aluigi.altervista.org/mytoolz.htm#myrtp
*/
UINT CALLBACK RTPatchApply32(LPSTR CmdLine, LPVOID (CALLBACK *CallBackFn)(UINT, LPVOID), BOOL WaitFlag);
/* ----------------------------------------------------
CallBack - handle callbacks from the DLL
---------------------------------------------------- */
LPVOID CALLBACK CallBackFunc(UINT Id, LPVOID Parm) {
LPVOID RetVal;
RetVal = "";
switch(Id) {
case 1:
case 3:
case 9:
case 0x0A:
case 0x0B:
/* these are all text output... */
printf("%s", (char *) Parm);
break;
case 2:
/* warning */
printf("%s", (char *) Parm);
break;
case 4:
/* error */
printf("%s", (char *) Parm);
break;
case 0x0C:
/* set copyright string
parse it - format is sep line, centered line 1 (product name),
centered line 2 (platform/edition info), then second call has centered line 3
( copyright info), sep line, blank line */
printf("%s\n", (char *) Parm);
break;
case 5:
/* % complete */
/* PutPct( (int) (((long)(0xffff & *((UINT *) Parm))*100L) / 0x8000L) ); */
break;
case 6:
/* Number of patch files */
break;
case 7:
/* begin patch */
printf("%s\n", (char *) Parm);
break;
case 8:
/* end patch */
printf("\n");
break;
case 0x0D:
/* this one shouldn't happen (only occurs if the patch file
file bound to the executable is invalid in some way). */
printf("RTP_X0D: FATAL\n");
RetVal = NULL;
break;
case 0x0E:
/* this one shouldn't happen either (same reason) */
break;
case 0x0F:
/* Do password dialog here (abort in this skeleton) */
printf("RTP_PSS: NOT_IMPLEMENTED\n");
RetVal = NULL;
break;
case 0x10:
/* Incorrect password alert */
break;
case 0x11:
/* do disk change dialog (abort in this skeleton) */
printf("RTP_DLG: NOT_IMPLEMENTED\n");
RetVal = NULL;
break;
case 0x12:
/* Invalid disk alert (abort in this skeleton) */
printf("RTP_DSK: NOT_IMPLEMENTED\n");
RetVal = NULL;
break;
case 0x13:
/* system location confirmation (always returns "Y" in this skeleton) */
RetVal = "Y";
break;
case 0x14:
/* Do Location Dialog (abort in this skeleton) */
printf("RTP_LOC: NOT_IMPLEMENTED\n");
RetVal = NULL;
break;
case 0x16:
printf("RTP_MSG: Searching for Systems to Update...\n");
break;
case 0x15:
default:
break;
}
return(RetVal);
}
DWORD BoyerMooreHorspool(BYTE *p, DWORD ps, BYTE *q, BYTE qs) {
BYTE t[256];
DWORD i, s;
if (p && ps && q && qs && (ps >= qs)) {
FillMemory(t, 256, qs);
qs--;
for (i = 0; i < qs; i++) {
t[q[i]] = qs - i;
}
s = 0;
while ((ps - s) > qs) {
for (i = qs; (p[s + i] == q[i]); i--) {
if (!i) {
return(s + 1);
}
}
s += t[p[s + qs]];
}
}
return(0);
}
void MemWrite(void *p, void *b, DWORD l) {
DWORD dx;
if (p && b && l && VirtualProtect(p, l, PAGE_READWRITE, &dx)) {
CopyMemory(p, b, l);
VirtualProtect(p, l, dx, &dx);
FlushInstructionCache(GetCurrentProcess(), p, l);
}
}
#define CODESECT (IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_CNT_CODE)
int PatchW32Hack(void) {
#ifdef DEBUG
char s[IMAGE_SIZEOF_SHORT_NAME + 1];
#endif
IMAGE_DOS_HEADER *dh;
IMAGE_NT_HEADERS *nh;
IMAGE_SECTION_HEADER *sh;
DWORD i, dw;
BYTE *p, *d;
/* get and check library header */
p = (BYTE *) GetModuleHandle("PATCHW32.dll");
if (p) {
dh = (IMAGE_DOS_HEADER *) p;
if (dh->e_magic == IMAGE_DOS_SIGNATURE) {
nh = (IMAGE_NT_HEADERS *) &p[dh->e_lfanew];
if (
(nh->Signature == IMAGE_NT_SIGNATURE) &&
(nh->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR_MAGIC)
) {
/* find code section */
sh = (IMAGE_SECTION_HEADER *) IMAGE_FIRST_SECTION(nh);
for (i = 0; i < nh->FileHeader.NumberOfSections; i++) {
/* code section found */
if ((sh[i].Characteristics & CODESECT) == CODESECT) {
/* find byte sequence to patch */
d = &p[sh[i].VirtualAddress];
/* older patchw32.dll - v12.00 and below */
dw = BoyerMooreHorspool(
d, sh[i].Misc.VirtualSize, (BYTE *)
"\xB9\x0A\x00\x00\x00" /* mov ecx, 0A */
"\xF3\xA6" /* repe cmpsb */
"\x75" /* jne ... */
, 8
);
if (dw) {
dw--;
/* patch conditional jump */
MemWrite(&d[dw + 8], "\x00", 1);
#ifdef DEBUG
CopyMemory(s, sh[i].Name, IMAGE_SIZEOF_SHORT_NAME);
s[IMAGE_SIZEOF_SHORT_NAME] = 0;
printf("PATCHW32.dll:%08lX (%s)\n", (DWORD) &d[dw], s);
#endif
return(1);
}
/* newer patchw32.dll - v12.21 and above */
dw = BoyerMooreHorspool(
d, sh[i].Misc.VirtualSize, (BYTE *)
"\x83\xE9\x04" /* sub ecx, 4 */
"\x83\xC0\x04" /* add eax, 4 */
"\x83\xC6\x04" /* add esi, 4 */
"\x83\xF9\x04" /* cmp ecx, 4 */
, 4 * 3
);
if (dw) {
dw--;
/* patch conditional jumps */
MemWrite(&d[dw - 1], "\x00", 1); /* loop check */
MemWrite(&d[dw + 19], "\x00", 1); /* +0 check */
MemWrite(&d[dw + 27], "\x00", 1); /* +1 check */
MemWrite(&d[dw + 100], "\x00", 1); /* file check */
#ifdef DEBUG
CopyMemory(s, sh[i].Name, IMAGE_SIZEOF_SHORT_NAME);
s[IMAGE_SIZEOF_SHORT_NAME] = 0;
printf("PATCHW32.dll:%08lX (%s)\n", (DWORD) &d[dw], s);
#endif
return(1);
}
}
}
}
}
}
return(0);
}
/* -NOMESSAGE -QUIET // -NOM -Q */
/*static char strFmt[] = "/Z \"%s\" -NOPATHSEARCH -NOSUBDIRSEARCH -IGNOREMISSING -NOBACKUP -NOCONFIRM -NOERRORFILE -NOTIMEZONECHECK -NOUNDO";*/
static char strFmt[] = "/Z \"%s\" -NOP -NOS -I -NOB -NOC -NOE -NOT -NOU";
int main(int argc, char *argv[]) {
char s[1025];
HANDLE fl;
DWORD dw;
WORD w;
printf(
"RTPatchX v1.6 - RTPatch apply tool\n"
"(c) CTPAX-X Team 2013,2016,2017,2020,2021,2024\n"
"http://www.CTPAX-X.org/\n\n"
);
if ((argc < 2) || (argc > 3)) {
printf(
"Usage: rtpatchx <rtp-file.ext> [/force]\nWhere:\n"
" rtp-file.ext - RTPatch file (.RTP or all-in-one .EXE)\n"
" /force - force patch (for modified files with THE SAME SIZE as original)\n\n"
);
return(1);
}
/* v1.5 do not use /Z key if this is not an executable file */
fl = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
if (fl != INVALID_HANDLE_VALUE) {
w = 0;
ReadFile(fl, &w, 2, &dw, NULL);
CloseHandle(fl);
/* MZ or ZM */
if ((w != 0x5A4D) && (w != 0x4D5A)) {
/* not executable - remove /Z key */
strFmt[0] = ' ';
strFmt[1] = ' ';
}
}
/* if not force patch - cut off misc format tail bytes */
if (argc == 2) {
strFmt[7] = 0;
}
/* /Z allows to apply even executable files (code taken from myRTP by Luigi Auriemma) */
wsprintf(s, strFmt, argv[1]);
/* force - do in memory patch for library (skip source files CRC check)
with this you can patch modified files with THE SAME SIZE as original one */
if (argc == 3) {
if (!PatchW32Hack()) {
printf("Warning: can't apply force patch - unsupported PATCHW32.dll version!\n\n");
}
}
/* RTPatchApply32NoCall() not available at v4.00 and below
and below v4.00 callback argument MUST be non-NULL! */
RTPatchApply32(s, CallBackFunc, TRUE);
return(0);
}