CRC32 on ELF in C problem
From: Kevin (kevin_at_netkev.com)
Date: 07/26/04
- Next message: Eric: "Re: The Decline of C/C++, the rise of X"
- Previous message: Eric: "Re: The Decline of C/C++, the rise of X"
- Next in thread: Jens.Toerring_at_physik.fu-berlin.de: "Re: CRC32 on ELF in C problem"
- Reply: Jens.Toerring_at_physik.fu-berlin.de: "Re: CRC32 on ELF in C problem"
- Reply: Randy Howard: "Re: CRC32 on ELF in C problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 26 Jul 2004 07:54:41 -0700
I need to calculate the checksum of certain sections in an ELF binary
file. I'm able to parse through to the program header table and then
find the section header table and then find the correct sections using
the section header string table. Using this method, I am able to find
the
offset to the .text section and the number of bytes for the range of
sections i need to calculate the checksum for.
I've verified the starting offset and size on a test ELF file through
gdump -map and my program is correct in how it gathers those. My
problem lies in
actually calculating the checksum using a provided crc32 algorithm.
Following is my method for storing the binary data of the needed
sections into a buffer array and then the crc32 alg. You'll notice
that I have commented out fread and used fprintf instead. This is a
suggestion from the programmer who tasked me with the program but I do
not understand how it could work.
I do have the correct checksum to see if my program works. My
question is (obvious to some but not others), what do I need to change
in order to output the correct checksum?
Your help/suggs/pointers are appreciated.
-Kevin
-----------
int get_chksum(int i_addr, int i_nbytes, FILE* bin_fp)
{
int csum,l;
char *data = NULL;
/*i realize theres no reason for
this line, it was for fread*/
if(fseek(bin_fp,i_addr,SEEK_SET) == 0)
{
data = (char*) malloc(i_nbytes);
//fread(data,i_nbytes,1,bin_fp);
for(l=0;l<i_nbytes;l++)
{
/*i_addr is sec_hdr_ptr(at .text)->sh_offset
using 0xFF mask makes no diff. was a prev sugg*/
fprintf(data,"%c",(i_addr+l) & 0xFF);
}
//CRC_POLYNOMIAL is def earlier as 0x04c11db7L
csum = CalculateCRC32(data, i_nbytes, 0,
CRC_POLYNOMIAL,0xffffffff);
free(data);
}
else {
csum=0;
printf("Error: could not seek to start section address in
file\n");
}
return csum;
}
//provided crc32 alg
int CalculateCRC32(unsigned char *s, int len, int endianswap, unsigned
int polynomial, unsigned int seedvalue)
{
int csum=seedvalue;
int data;
int j, i;
printf("received 0x%8.8x byte char array\n",len);
for (j = 0; j < len ; j++)
{
data = s[endianswap ? j ^ 3 : j] << 24; // read 1 byte, int
= 4, so shift left 3(24bits)
//printf("j= %d, data=%d\n",j,data); //debug
for (i=0; i<8; ++i)
{ /* Loop for each
bit in byte */
if ((data^csum) & 0x80000000)
{ /* Shift CRC, feed
back hi bit */
csum = (csum<<1) ^ polynomial;
}
else
{
csum <<= 1;
}
data <<= 1;
}
}
return csum;
}
- Next message: Eric: "Re: The Decline of C/C++, the rise of X"
- Previous message: Eric: "Re: The Decline of C/C++, the rise of X"
- Next in thread: Jens.Toerring_at_physik.fu-berlin.de: "Re: CRC32 on ELF in C problem"
- Reply: Jens.Toerring_at_physik.fu-berlin.de: "Re: CRC32 on ELF in C problem"
- Reply: Randy Howard: "Re: CRC32 on ELF in C problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|