class Digest::CRC16QT

Implements the CRC16_CCITT algorithm used in QT algorithms.

@author Matthew Bednarski

Constants

FINAL_XOR
REVERSE_CRC_RESULT
REVERSE_DATA

Public Instance Methods

checksum() click to toggle source
# File lib/digest/crc16_qt.rb, line 31
def checksum
  crc = @crc + 0
  crc ^= FINAL_XOR      if FINAL_XOR
  crc = revert_bits crc if REVERSE_CRC_RESULT
  return crc
end
update(data) click to toggle source

Updates the CRC16 checksum.

@param [String] data

The data to update the checksum with.
# File lib/digest/crc16_qt.rb, line 22
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

Protected Instance Methods

revert_bits(cc) click to toggle source
# File lib/digest/crc16_qt.rb, line 40
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
revert_byte(cc) click to toggle source
# File lib/digest/crc16_qt.rb, line 52
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