🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.msdos.programmer
Article #239

Re: changes to exe (binary) file

#239
From: Walter Bright
Date: Tue, 04 Aug 1992 21:11
105 lines
2955 bytes
In article <1992Jul30.152020.8261@menudo.uh.edu> cosc16to@menudo.uh.edu (Andy Hakim) writes:
/To figure out the offset:  (DEFAULT_OFFSET in example)  compile the
/program, then use a program such as LIST to scan for a sequence of
/matching bytes ( 100 118 138 )  The HEX DUMP mode of LIST is handy.
/Next, go back to your source file, and update the correct offset.
/An alternative way to figure out the offset is to use info from .map files.

Here is an even easier way:


/*_ patchexe.c   Wed Jun 15 1988   Modified by: Walter Bright */
/* Copyright (C) 1988 by Walter Bright	*/
/* Module to patch .COM and .EXE files			*/

#if M_I86SM
#include	<string.h>
#endif

#include	<dos.h>
#include	<io.h>

/*****************************
 * Patch .EXE or .COM file.
 * Doesn't work if data to be patched contains relocatable values.
 * Input:
 *	progname	Name of program to patch
 *	data		Pointer to data (for T,S,M models, this is the
 *			offset from DS)
 *	size		Number of bytes to patch
 * Returns:
 *	0	Success
 *	!=0	Failure
 */

typedef unsigned short unsigned16;	/* unsigned 16 bit integer	*/

int patchexe(char *progname,void *data,unsigned size)
{   int fd;
    unsigned seg;
    unsigned long offset;
#if M_I86SM
    int len;
    int comfile;

    len = strlen(progname);
    if (len < 4)
	goto fail;
    comfile = strcmpl(progname + len - 3,"exe");
#endif
    fd = open(progname,O_RDWR);
    if (fd == -1)
	goto fail;
    seg = getDS() - _psp - 0x10;
#if M_I86SM
    if (comfile)
    {
	if (seg >= 0x1000)
	    goto failclose;
    }
    else
#endif
    {   unsigned16 header[13];		/* buffer for EXE file header	*/

	if (read(fd,header,sizeof(header)) != sizeof(header))
	    goto failclose;
	if (header[0] != 0x5A4D)	/* check magic number		*/
	    goto failclose;

	seg += header[4];		/* offset to start of load module */
    }
    offset = ((unsigned long) seg << 4) + (unsigned) data;
    if (lseek(fd,offset,SEEK_SET) == -1L)
	goto failclose;
    if (write(fd,data,size) != size)
	goto failclose;
    if (close(fd) == -1)
	goto fail;

    return 0;			/* success		*/

failclose:
    close(fd);
fail:
    return -1;
}

/******************* TEST PROGRAM ****************/

#if TEST

#include	<stdio.h>
main(int argc,char *argv[])
{
    static int val = 0;

    printf("%s\n",argv[0]);
    printf("val = %d\n",val);
    val++;
    if (patchexe(argv[0],&val,sizeof(val)))
	printf("Failed\n");
    else
	printf("Succeeded\n");
}

#endif
Technical Details
Message-ID: <873@nazgul.UUCP>
Path: arc1-ca.pugleaf.net!sparky!uunet!pilchuck!fnx!nazgul!bright
References: <1992Jul29.125540.7401@hou.amoco.com> <1992Jul30.152020.8261@menudo.uh.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!pilchuck!fnx!nazgul!bright
From: bright@nazgul.UUCP (Walter Bright)
Newsgroups: comp.os.msdos.programmer
Subject: Re: changes to exe (binary) file
Message-ID: <873@nazgul.UUCP>
Date: 4 Aug 92 21:11:10 GMT
References: <1992Jul29.125540.7401@hou.amoco.com> <1992Jul30.152020.8261@menudo.uh.edu>
Reply-To: bright@nazgul.UUCP (Walter Bright)
Organization: Zortech, Seattle
Lines: 105