Reputation: 43
I'm trying to implement the checksum from the WinHex program, in WinHex and in 010 editor its called
Checksum Uint 32bit
For example, we calculate the checksum of 32byte (represented in HEX) data:
1122334455667788991122334455667711223344556677889911223344556677
The WinHex gives us checksum32 value:
EE65DE86
I was thinking it's just about summing each byte to the previous one:
unsigned char checksum(unsigned char data[]){
unsigned char sum = 0;
int i;
for(i=0; sizeof(data); i++){
sum += data[i];
}
return -sum;
}
I tried it, and it gave me the wrong checksum. It is clearly not crc, maybe its Adler or Fletcher or smh else, it's so many checksums variation that kinda difficult to find correct one
Upvotes: 1
Views: 777
Reputation: 224
function seems to never returns
it's an infinite loop, or crush
i
keeps on increasing till overlaps
besides sizeof(data)
is 4, data
is a pointer
unsigned char checksum(unsigned char data[]){
unsigned char sum = 0;
int i;
for(i=0; sizeof(data); i++){ // equivalent for(i=0; 4; i++), for(i=0; true; i++)
sum += data[i];
}
return -sum;
}
also we need data
length
unsigned char checksum(unsigned char data[], int data_length){ //<-- added data_length
int sum = 0; // <-- int instead of unsigned char
int i;
for(i=0; i<data_length; i++){ // as long as i is less than data_length
sum += data[i];
}
return -sum;
}
Upvotes: 1
Reputation: 43
I have found the solution, thanks to Gerhardh, we need to convert 4bytes into int32 then sum it up
unsigned int checksum( int data[])
{
unsigned int sum = 0;
int i;
int size;
for(i=0; sizeof(data); i++) {
sum += data[i];
}
return sum;
}
Upvotes: 0
Reputation: 153303
At least this problem:
sizeof(data)
below is the size of a pointer and not 0 --> Infinite loop.
unsigned char checksum(unsigned char data[]){
unsigned char sum = 0;
int i;
for(i=0; sizeof(data); i++){
sum += data[i];
}
return -sum;
}
Function only returns value 0-0xFF, never 0xEE65DE86
Upvotes: 0