Mail Archives: djgpp/1998/09/04/21:31:24
From: | oohoseoo AT aol DOT com (OoHOSEoO)
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | SIGSEGV from reading an executable... (?)
|
Lines: | 194
|
Message-ID: | <1998090501154700.VAA20831@ladder01.news.aol.com>
|
NNTP-Posting-Host: | ladder01.news.aol.com
|
Date: | 5 Sep 1998 01:15:47 GMT
|
Organization: | AOL http://www.aol.com
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
I wrote a small program that reads each byte from a command line specified file
and displays the byte in binary. it works perfectly for ascii based files, but
when I try and read executable files I get this error message while processing
the bytes:
Exiting due to signal SIGSEGV
Stack Fault at eip=00001700
eax=00001c78 ebx=009c4040 ecx=009b7f15 edx=009b7f74 esi=00000054 edi=0000d4c0
ebp=46464646 esp=009b7f80 program=C:\MYSTUFF\BINARY.EXE
cs: sel=00f7 base=83260000 limit=009dffff
ds: sel=00ff base=83260000 limit=009dffff
es: sel=00ff base=83260000 limit=009dffff
fs: sel=00cf base=00022570 limit=0000ffff
gs: sel=010f base=00000000 limit=ffffffff
ss: sel=00ff base=83260000 limit=009dffff
Call frame traceback EIPs:
0x00001700
here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
long getsize(const char*);
int readfile(const char*, long);
void convert(int, char*, char*);
int output(long, const char*);
int main(int argc, char* argv[])
{
long size = 0;
if(argc == 2){
if((size = getsize(argv[1])) == 0){
puts("The file was zero bytes long.\n");
return 1;
}
if(readfile(argv[1], size)){
return 1;
}
}
return 0;
}
int readfile(const char* argv, long size)
{
unsigned char buf[32768];
long amount = 32768;
long left = size;
long read = 0;
FILE* fp;
if((fp = fopen(argv, "rb")) == NULL){
puts("There was an error opening the file");
return 1;
}
while(left){
if(left < amount){
amount = left;
}
if((read = fread(buf, 1, amount, fp)) == 0){
puts("There was an error reading the file");
return 1;
}
output(read, buf);
left -= amount;
}
fclose(fp);
return 0;
}
int output(long size, const char* buf)
{
char HI[5];
char LO[5];
long count;
for(count = 0; count < size; count++){
convert(buf[count], HI, LO);
printf("%s%s ", HI, LO);
}
return 0;
}
long getsize(const char* argv)
{
FILE* fp;
long curpos = 0;
long length = 0;
if((fp = fopen(argv, "rb")) == NULL){
return 0;
}
curpos = ftell(fp);
fseek(fp, 0, SEEK_END);
length = ftell(fp);
fseek(fp, curpos, SEEK_SET);
fclose(fp);
return length;
}
void convert(int Byte, char* HI, char* LO)
{
char chr[3];
int status = 0;
sprintf(chr, "%X", Byte);
while(status < 2){
switch(chr[status]){
case '1':
if(!status) strcpy(HI, "0001");
if(status) strcpy(LO, "0001");
break;
case '2':
if(!status) strcpy(HI, "0010");
if(status) strcpy(LO, "0010");
break;
case '3':
if(!status) strcpy(HI, "0011");
if(status) strcpy(LO, "0011");
break;
case '4':
if(!status) strcpy(HI, "0100");
if(status) strcpy(LO, "0100");
break;
case '5':
if(!status) strcpy(HI, "0101");
if(status) strcpy(LO, "0101");
break;
case '6':
if(!status) strcpy(HI, "0110");
if(status) strcpy(LO, "0110");
break;
case '7':
if(!status) strcpy(HI, "0111");
if(status) strcpy(LO, "0111");
break;
case '8':
if(!status) strcpy(HI, "1000");
if(status) strcpy(LO, "1000");
break;
case '9':
if(!status) strcpy(HI, "1001");
if(status) strcpy(LO, "1001");
break;
case 'A':
if(!status) strcpy(HI, "1010");
if(status) strcpy(LO, "1010");
break;
case 'B':
if(!status) strcpy(HI, "1011");
if(status) strcpy(LO, "1011");
break;
case 'C':
if(!status) strcpy(HI, "1100");
if(status) strcpy(LO, "1100");
break;
case 'D':
if(!status) strcpy(HI, "1101");
if(status) strcpy(LO, "1101");
break;
case 'E':
if(!status) strcpy(HI, "1110");
if(status) strcpy(LO, "1110");
break;
case 'F':
if(!status) strcpy(HI, "1111");
if(status) strcpy(LO, "1111");
break;
}
status++;
}
return;
}
I'd really appreciate any help you can give, thanks.
James B. (OoHOSEoO AT aol DOT com)
*please enable "Email author" or "CC Author" before you post, thank you :)
- Raw text -