## [REBOL] Re: checksum calculation

### From: rebol-list2:seznam:cz at: 11-Sep-2003 14:31

Hello Anton,
Thursday, September 11, 2003, 9:48:38 AM, you wrote:
AR> Hi Oldes,
AR> Well, what have you got so far?
AR> I suppose your algorithm in rebol doesn't match the
AR> one in C?
Thanx Anton... your script helped me very much...
Yes... almost none of checksums I've found in binary formats I was
working with doesn't match the rebol's one:( Another problem with
rebol's checksum is that it's not possible to use it on large files or
data streams, because it first loads all the content into memory.
I was trying to use it (for example) for counting md5 checksums of redhat
distribution cds and I have to say it's useless:(
You script was not working but I found the right way how to detect the
length and improved your script to this one (with a hope that negative
overflow should never happen) and it's working:))
ULONG: [;(byte-order is Big Endian)
copy v 4 skip (
v: to binary! v
v: either #{80000000} = and v #{80000000} [
2147483648 + to integer! (and v #{7FFFFFFF})
][to integer! v]
)
] ;returns decimal if the u.integer is >= 2147483648
calc-table-checksum: func [table [binary!] /local sum length n][
sum: 0 ; (unsigned long integer)
length: length? table
if 0 < n: length // 4 [
length: length + 4 - n
insert/dup tail table #{00} (4 - n)
;print "padding"
]
; step through the table data in chunks of four bytes
parse/all table [
any [
ULONG (
either error? try [sum: sum + v][
;print "positive overflow"
sum: sum - 4294967296 + v
][
if sum >= 4294967295 [sum: sum - 4294967296]
]
)
]
]
either positive? sum [sum][sum + 4294967296]
]
--
Best regards,
rebOldes -----------------[ http://oldes.multimedia.cz/ ]