1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
require 'digest/crc16_ccitt'
module Digest
#
# Implements the CRC16_CCITT algorithm used in QT algorithms.
#
# @author Matthew Bednarski
#
class CRC16QT < CRC16CCITT
FINAL_XOR = 0xffff
REVERSE_CRC_RESULT = true
REVERSE_DATA = true
# Updates the CRC16 checksum.
#
# @param [String] data
# The data to update the checksum with.
#
def update(data)
data.each_byte do |b|
b = revert_byte(b) if REVERSE_DATA
@crc = ((TABLE[((@crc >> 8) ^ b) & 0xff] ^ (@crc << 8)) & 0xffff)
end
return self
end
def checksum
crc = @crc + 0
crc ^= FINAL_XOR if FINAL_XOR
crc = revert_bits crc if REVERSE_CRC_RESULT
return crc
end
protected
def revert_bits(cc)
ob = 0
b = (1 << 15)
16.times do |t|
ob |= (1 << t) if (cc & b) != 0
b >>= 1
end
return ob
end
def revert_byte(cc)
ob = 0
b = (1 << 7)
8.times do |t|
ob |= (1 << t) if (cc & b) != 0
b >>= 1
end
return ob
end
end
end
|