# OFT Checksum calculator. defcalc_checksum(buffer='',prev_checksum=0xffff,odd_start=false)
checksum = (prev_checksum >>16) &0xffff
buffer.split(//).each_with_indexdo |x,i|
old_checksum = checksum
# Even bytes are high, odd are low, unless the last chunk ended up starting # this chunk on an odd byte, in which case the reverse is true. Note that # the first chunk (and often the only chunk) is always starting on 0, thus, # even. This is the typical use case. Implementations should set the # odd_start flag and the prev_checksum value if this is the case.if odd_start
value = (!(i %2).zero?? x[0] <<8 : x[0])
else
value = ((i %2).zero?? x[0] <<8 : x[0]) # usually this one.end
checksum -= value
checksum -=1if checksum > old_checksum
end2.times {checksum = (checksum &0x0000ffff) + (checksum >>16) }
checksum <<16end