Category : Utilities for DOS and Windows Machines
Archive   : EXE.ZIP
Filename : EXEPRINT.C

 
Output of file : EXEPRINT.C contained in archive : EXE.ZIP

#include
#include
#include

/* EXEPRINT.C Either print or modify the exe file header:
*
* exe file Print the contents of a file's EXE header
* exe -mN file Modify the exe header so that N bytes of memory
* are allocated for the combined bss/stack/heap
* area. If N is smaller that the required minimum
* (bss + stack size) then it's rounded up. The
* largest permitted value of N is 65,535. Use
* -m1 for the minimum possible heap.
* exe -sN file Modify the exe header so that N bytes of stack
* are used. If necessary, increase the bss/stack/heap
* size to accommodate the new stack. (The
* bss/stack/heap won't be made smaller, however).
*/

typedef unsigned short word; /* 2-byte unsigned number */

typedef struct
{
word signature;
word image_len; /* Length of load module image % 512 */
word file_size; /* File size in 512-byte units */
word num_reloc; /* Number of relocation table items */
word header_size; /* Size of the header in paragraphs */
word bss_min; /* min size of data area above program */
word bss_max; /* max size of data area above program */
word stack_disp; /* displacement in para. of stack seg. */
word init_sp; /* Initial SP register contents */
word checksum; /* Checksum for file */
word init_ip; /* Initial IP register contents (PC) */
word code_disp; /* displacement in para. to code seg. */
word first_reloc; /* displacement (bytes) to 1st reloc item */
word overlay; /* overlay number. */
}
EXE_HEADER;

static int Hsize = 0 , Ssize = 0 ;

ARG Argtab[] =
{
{ 'm' , INTEGER, &Hsize, "Set miminum HEAP size to " },
{ 's' , INTEGER, &Ssize, "Set STACK size to " }
};

#define TSIZE (sizeof(Argtab)/sizeof(ARG))

/*----------------------------------------------------------------------*/

extern int cls();

/*----------------------------------------------------------------------*/

usage()
{
fprintf( stderr, "\nEXE [-ms[]] filename.ext\n\n" );
exit(1);
}

/*----------------------------------------------------------------------*/

main( argc, argv )
char **argv;
{
EXE_HEADER h;
int fd;
unsigned numpara, ostack, odata ;

argc = getargs( argc, argv, Argtab, TSIZE, usage );

if( argc != 2 )
fatal_err("\nEXE sez: Gimme a file name \n\n");


if( (fd = open( argv[1], O_RDWR | O_BINARY )) == -1 )
fatal_err( "\nEXE sez: I can't open %s\n\n", argv[1] );

if( read( fd, (char *) &h, sizeof(h) ) != sizeof(h) )
fatal_err( "\nEXE sez: I can't read %s\n\n", argv[1] );


if( Hsize )
{
/* 1) numpara = the number of paragraphs required to hold the
* specified number of bytes.
* 2) h.bss.max, the maximum heap size, gets either the
* current minimum or the specified size,
* whichever is larger.
* 3) write out the modified header.
*/

numpara = Hsize/16 + (Hsize % 16 != 0) ; /* 1 */

h.bss_max = (numpara
lseek( fd, 0L, 0 ); /* 3 */
write( fd, (char *) &h, sizeof(h) );
}

if( Ssize )
{
/* 1) ostack = number of paragraphs in original stack
* 2) odata = number of paragraphs of data.
* 2) numpara = number of paragraphs in new stack.
* 4) modify stack size.
* 5) Adjust the size of the stack+data area as appropriate.
* 6) write the modified header out to the file.
*/

ostack = h.init_sp/16 + (h.init_sp % 16 != 0) ; /* 1 */
odata = h.bss_max - ostack ; /* 2 */
numpara = Ssize/16 + (Ssize % 16 != 0) ; /* 3 */

h.init_sp = Ssize ; /* 4 */

h.bss_min = odata + numpara; /* 5 */

if( h.bss_min > h.bss_max )
h.bss_max = h.bss_min;

lseek( fd, 0L, 0 ); /* 6 */
write( fd, (char *) &h, sizeof(h) );
}

cls();
print_hdr( &h );
close( fd );
}

/*----------------------------------------------------------------------*/

print_hdr( h )
EXE_HEADER *h;
{
printf("W A R N I N G !! If the Signature is NOT 0x5a4d,");
printf(" DON'T mess with the Header!!!!\n");
printf(" It's OK to look, but don't Putz.\n\n");

printf("%6d (0x%04x): ", h->signature, h->signature );
printf("Signature (0x5a4d marks it as a valid .EXE file)\n\n");

printf("%6d (0x%04x): ", h->image_len, h->image_len );
printf("Length of image mod 512\n" );

printf("%6d (0x%04x): ", h->file_size, h->file_size );
printf("File size (512-byte blocks) including header\n");

printf("%6d (0x%04x): ", h->num_reloc, h->num_reloc );
printf("Number of relocation table entries\n");

printf("%6u (0x%04x): ", h->header_size, h->header_size );
printf("Size of the header (paragraphs) = %lu bytes\n\n",
(unsigned long) h->header_size * 16 );

printf("%6u (0x%04x): ", h->bss_min, h->bss_min );
printf("Min. memory above program (paragraphs) = %lu bytes\n",
(unsigned long) h->bss_min * 16 );

printf("%6u (0x%04x): ", h->bss_max, h->bss_max );
printf("Max. memory above program (paragraphs) = %lu bytes\n\n",
(unsigned long) h->bss_max * 16 );

printf("%6d (0x%04x): ", h->stack_disp, h->stack_disp );
printf("Displacement (paragraphs) of stack within load module\n");

printf("%6d (0x%04x): ", h->init_sp, h->init_sp );
printf("Initial value of the SP register: STACK SIZE\n\n");

printf("%6d (0x%04x): ", h->checksum, h->checksum );
printf("Checksum for file\n");

printf("%6d (0x%04x): ", h->init_ip, h->init_ip );
printf("Initial value of the PC (IP) register\n");

printf("%6d (0x%04x): ", h->code_disp, h->code_disp );
printf("Displacement (paragraphs) of code seg. within load module\n");

printf("%6d (0x%04x): ", h->first_reloc, h->first_reloc );
printf("Displacement (bytes) to first relocation item in module\n");

printf("%6d (0x%04x): ", h->overlay, h->overlay );
printf("Overlay number.\n");
}