Sysinternals Homepage
Forum Home Forum Home > Windows Discussions > Internals
  New Posts New Posts RSS Feed - optional header checksum calculation
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

optional header checksum calculation

 Post Reply Post Reply
Author
Message
c796903 View Drop Down
Groupie
Groupie


Joined: 04 June 2010
Status: Offline
Points: 68
Post Options Post Options   Thanks (0) Thanks(0)   Quote c796903 Quote  Post ReplyReply Direct Link To This Post Topic: optional header checksum calculation
    Posted: 23 October 2010 at 2:45pm
what algoritm is used to calculate pe csum?
Back to Top
wj32 View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 January 2009
Location: Australia
Status: Offline
Points: 1016
Post Options Post Options   Thanks (0) Thanks(0)   Quote wj32 Quote  Post ReplyReply Direct Link To This Post Posted: 23 October 2010 at 10:59pm
Sum the entire image file, excluding the CheckSum field in the optional header, as an array of USHORTs, allowing any carry above 16 bits to be added back onto the low 16 bits. Then add the file size to get a 32-bit value. Here's an inefficient implementation:

calculate checksum (pvoid viewbase, size_t filesize):
ulong sum = 0
pushort shorts = viewbase
ulong numberOfShorts = (filesize + 1) / 2

for (i = 0; i < numberOfShorts; i++)
    sum += shorts[i ]
    sum += sum >> 16 // 16-bit carry

sum -= sum < low 16 bits of CheckSum in file // 16-bit borrow
sum -= low 16 bits of CheckSum in file
sum -= sum < high 16 bits of CheckSum in file
sum -= high 16 bits of CheckSum in file

sum += filesize

You can create a much faster implementation by using some assembly with adc/sbb operating on 32 bits at a time.

Edited by wj32 - 24 October 2010 at 8:33pm
PH, a free and open source process viewer.
Back to Top
GamingMasteR View Drop Down
Senior Member
Senior Member
Avatar

Joined: 10 August 2008
Status: Offline
Points: 245
Post Options Post Options   Thanks (0) Thanks(0)   Quote GamingMasteR Quote  Post ReplyReply Direct Link To This Post Posted: 24 October 2010 at 12:53am
Back to Top
wj32 View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 January 2009
Location: Australia
Status: Offline
Points: 1016
Post Options Post Options   Thanks (0) Thanks(0)   Quote wj32 Quote  Post ReplyReply Direct Link To This Post Posted: 24 October 2010 at 1:44am
The OP asked for the algorithm...
PH, a free and open source process viewer.
Back to Top
c796903 View Drop Down
Groupie
Groupie


Joined: 04 June 2010
Status: Offline
Points: 68
Post Options Post Options   Thanks (0) Thanks(0)   Quote c796903 Quote  Post ReplyReply Direct Link To This Post Posted: 24 October 2010 at 5:00pm
thank you.
its really so simple?
Back to Top
c796903 View Drop Down
Groupie
Groupie


Joined: 04 June 2010
Status: Offline
Points: 68
Post Options Post Options   Thanks (0) Thanks(0)   Quote c796903 Quote  Post ReplyReply Direct Link To This Post Posted: 24 October 2010 at 8:07pm
and what is file size doesnt fit in 32 bits?
Back to Top
wj32 View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 January 2009
Location: Australia
Status: Offline
Points: 1016
Post Options Post Options   Thanks (0) Thanks(0)   Quote wj32 Quote  Post ReplyReply Direct Link To This Post Posted: 24 October 2010 at 8:30pm
I don't know then. Do some experiments ;)
PH, a free and open source process viewer.
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down