Skip to content

Commit

Permalink
Set @table in Digest::CRC#initialize.
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Apr 16, 2014
1 parent e6e921e commit c04b2ed
Show file tree
Hide file tree
Showing 13 changed files with 17 additions and 25 deletions.
6 changes: 5 additions & 1 deletion lib/digest/crc.rb
Expand Up @@ -17,6 +17,9 @@ class CRC < Digest::Class
# The bit width of the CRC checksum
WIDTH = 0

# Default place holder CRC table
TABLE = [].freeze

#
# Calculates the CRC checksum.
#
Expand Down Expand Up @@ -47,7 +50,8 @@ def self.pack(crc)
# Initializes the CRC checksum.
#
def initialize
@crc = self.class.const_get(:INIT_CRC)
@table = self.class.const_get(:TABLE)
@crc = self.class.const_get(:INIT_CRC)
end

#
Expand Down
3 changes: 1 addition & 2 deletions lib/digest/crc16.rb
Expand Up @@ -71,9 +71,8 @@ def self.pack(crc)
# The data to update the checksum with.
#
def update(data)
table = self.class.const_get(:TABLE)
data.each_byte do |b|
@crc = ((table[(@crc ^ b) & 0xff] ^ (@crc >> 8)) & 0xffff)
@crc = ((@table[(@crc ^ b) & 0xff] ^ (@crc >> 8)) & 0xffff)
end

return self
Expand Down
3 changes: 1 addition & 2 deletions lib/digest/crc16_ccitt.rb
Expand Up @@ -51,9 +51,8 @@ class CRC16CCITT < CRC16
# The data to update the checksum with.
#
def update(data)
table = self.class.const_get(:TABLE)
data.each_byte do |b|
@crc = ((table[((@crc >> 8) ^ b) & 0xff] ^ (@crc << 8)) & 0xffff)
@crc = ((@table[((@crc >> 8) ^ b) & 0xff] ^ (@crc << 8)) & 0xffff)
end

return self
Expand Down
3 changes: 1 addition & 2 deletions lib/digest/crc16_dnp.rb
Expand Up @@ -50,9 +50,8 @@ class CRC16DNP < CRC16
# The data to update the checksum with.
#
def update(data)
table = self.class.const_get(:TABLE)
data.each_byte do |b|
@crc = ((@crc >> 8) ^ table[(@crc ^ b) & 0xff])
@crc = ((@crc >> 8) ^ @table[(@crc ^ b) & 0xff])
end

return self
Expand Down
3 changes: 1 addition & 2 deletions lib/digest/crc16_qt.rb
Expand Up @@ -20,10 +20,9 @@ class CRC16QT < CRC16CCITT
# The data to update the checksum with.
#
def update(data)
table = self.class.const_get(:TABLE)
data.each_byte do |b|
b = revert_byte(b) if REVERSE_DATA
@crc = ((table[((@crc >> 8) ^ b) & 0xff] ^ (@crc << 8)) & 0xffff)
@crc = ((@table[((@crc >> 8) ^ b) & 0xff] ^ (@crc << 8)) & 0xffff)
end

return self
Expand Down
3 changes: 1 addition & 2 deletions lib/digest/crc16_xmodem.rb
Expand Up @@ -49,9 +49,8 @@ class CRC16XModem < CRC16
# The data to update the checksum with.
#
def update(data)
table = self.class.const_get(:TABLE)
data.each_byte do |b|
@crc = ((table[((@crc >> 8) ^ b) & 0xff] ^ (@crc << 8)) & 0xffff)
@crc = ((@table[((@crc >> 8) ^ b) & 0xff] ^ (@crc << 8)) & 0xffff)
end

return self
Expand Down
3 changes: 1 addition & 2 deletions lib/digest/crc16_zmodem.rb
Expand Up @@ -49,9 +49,8 @@ class CRC16ZModem < CRC16
# The data to update the checksum with.
#
def update(data)
table = self.class.const_get(:TABLE)
data.each_byte do |b|
@crc = ((table[((@crc >> 8) ^ b) & 0xff] ^ (@crc << 8)) & 0xffff)
@crc = ((@table[((@crc >> 8) ^ b) & 0xff] ^ (@crc << 8)) & 0xffff)
end

return self
Expand Down
3 changes: 1 addition & 2 deletions lib/digest/crc24.rb
Expand Up @@ -72,9 +72,8 @@ def self.pack(crc)
# The data to update the checksum with.
#
def update(data)
table = self.class.const_get(:TABLE)
data.each_byte do |b|
@crc = ((table[((@crc >> 16) ^ b) & 0xff] ^ (@crc << 8)) & 0xffffff)
@crc = ((@table[((@crc >> 16) ^ b) & 0xff] ^ (@crc << 8)) & 0xffffff)
end

return self
Expand Down
3 changes: 1 addition & 2 deletions lib/digest/crc32.rb
Expand Up @@ -107,9 +107,8 @@ def self.pack(crc)
# The data to update the checksum with.
#
def update(data)
table = self.class.const_get(:TABLE)
data.each_byte do |b|
@crc = (((@crc >> 8) & 0x00ffffff) ^ table[(@crc ^ b) & 0xff])
@crc = (((@crc >> 8) & 0x00ffffff) ^ @table[(@crc ^ b) & 0xff])
end

return self
Expand Down
3 changes: 1 addition & 2 deletions lib/digest/crc32_mpeg.rb
Expand Up @@ -82,9 +82,8 @@ class CRC32Mpeg < CRC32
# The data to update the checksum with.
#
def update(data)
table = self.class.const_get(:TABLE)
data.each_byte do |b|
@crc = ((table[((@crc >> 24) ^ b) & 0xff] ^ (@crc << 8)) & 0xffffffff)
@crc = ((@table[((@crc >> 24) ^ b) & 0xff] ^ (@crc << 8)) & 0xffffffff)
end

return self
Expand Down
3 changes: 1 addition & 2 deletions lib/digest/crc5.rb
Expand Up @@ -41,9 +41,8 @@ class CRC5 < CRC
# The data to update the checksum with.
#
def update(data)
table = self.class.const_get(:TABLE)
data.each_byte do |b|
@crc = ((table[((@crc >> 3) ^ b) & 0xff] ^ (@crc >> 8)) & CRC_MASK)
@crc = ((@table[((@crc >> 3) ^ b) & 0xff] ^ (@crc >> 8)) & CRC_MASK)
end

return self
Expand Down
3 changes: 1 addition & 2 deletions lib/digest/crc64.rb
Expand Up @@ -111,9 +111,8 @@ def self.pack(crc)
# The data to update the checksum with.
#
def update(data)
table = self.class.const_get(:TABLE)
data.each_byte do |b|
@crc = ((table[(@crc ^ b) & 0xff] ^ (@crc >> 8)) & 0xffffffffffffffff)
@crc = ((@table[(@crc ^ b) & 0xff] ^ (@crc >> 8)) & 0xffffffffffffffff)
end

return self
Expand Down
3 changes: 1 addition & 2 deletions lib/digest/crc8.rb
Expand Up @@ -50,9 +50,8 @@ def self.pack(crc)
# The data to update the checksum with.
#
def update(data)
table = self.class.const_get(:TABLE)
data.each_byte do |b|
@crc = ((table[(@crc ^ b) & 0xff] ^ (@crc << 8)) & 0xff)
@crc = ((@table[(@crc ^ b) & 0xff] ^ (@crc << 8)) & 0xff)
end

return self
Expand Down

1 comment on commit c04b2ed

@korun
Copy link
Contributor

@korun korun commented on c04b2ed Apr 16, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this approach is much better than mine. 👍

Please sign in to comment.