digest-crc 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/.specopts +1 -0
- data/.yardopts +1 -0
- data/ChangeLog.md +18 -0
- data/LICENSE.txt +22 -0
- data/README.md +73 -0
- data/Rakefile +39 -0
- data/VERSION +1 -0
- data/digest-crc.gemspec +101 -0
- data/lib/digest/crc.rb +116 -0
- data/lib/digest/crc1.rb +35 -0
- data/lib/digest/crc16.rb +79 -0
- data/lib/digest/crc16_ccitt.rb +59 -0
- data/lib/digest/crc16_dnp.rb +62 -0
- data/lib/digest/crc16_modbus.rb +59 -0
- data/lib/digest/crc16_usb.rb +11 -0
- data/lib/digest/crc16_xmodem.rb +57 -0
- data/lib/digest/crc16_zmodem.rb +57 -0
- data/lib/digest/crc24.rb +80 -0
- data/lib/digest/crc32.rb +115 -0
- data/lib/digest/crc32_mpeg.rb +90 -0
- data/lib/digest/crc5.rb +49 -0
- data/lib/digest/crc64.rb +119 -0
- data/lib/digest/crc8.rb +58 -0
- data/spec/crc16_ccitt_spec.rb +13 -0
- data/spec/crc16_modbus_spec.rb +13 -0
- data/spec/crc16_spec.rb +13 -0
- data/spec/crc16_usb_spec.rb +13 -0
- data/spec/crc16_xmodem_spec.rb +13 -0
- data/spec/crc16_zmodem_spec.rb +13 -0
- data/spec/crc1_spec.rb +13 -0
- data/spec/crc24_spec.rb +13 -0
- data/spec/crc32_mpeg_spec.rb +13 -0
- data/spec/crc32_spec.rb +13 -0
- data/spec/crc5_spec.rb +14 -0
- data/spec/crc64_spec.rb +13 -0
- data/spec/crc8_spec.rb +13 -0
- data/spec/crc_examples.rb +27 -0
- data/spec/crc_spec.rb +14 -0
- data/spec/spec_helper.rb +4 -0
- metadata +138 -0
data/.gitignore
ADDED
data/.specopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour --format specdoc
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup markdown --title 'Digest CRC Documentation' --protected --files ChangeLog.md,LICENSE.txt
|
data/ChangeLog.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
Copyright (c) 2010 Hal Brodigan
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
'Software'), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
18
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
19
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
20
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
21
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Digest CRC
|
2
|
+
|
3
|
+
* [github.com/postmodern/digest-crc](http://github.com/postmodern/digest-crc)
|
4
|
+
* [github.com/postmodern/digest-crc/issues](http://github.com/postmodern/digest-crc/issues)
|
5
|
+
* Postmodern (postmodern.mod3 at gmail.com)
|
6
|
+
|
7
|
+
## Description
|
8
|
+
|
9
|
+
Adds support for calculating Cyclic Redundancy Check (CRC) to the Digest
|
10
|
+
module.
|
11
|
+
|
12
|
+
## Features
|
13
|
+
|
14
|
+
* Provides support for the following CRC algorithms:
|
15
|
+
* CRC1
|
16
|
+
* CRC5
|
17
|
+
* CRC8
|
18
|
+
* CRC16
|
19
|
+
* CRC16 CCITT
|
20
|
+
* CRC16 DNP
|
21
|
+
* CRC16 Modbus
|
22
|
+
* CRC16 USB
|
23
|
+
* CRC16 XModem
|
24
|
+
* CRC16 ZModem
|
25
|
+
* CRC24
|
26
|
+
* CRC32
|
27
|
+
* CRC32 Mpeg
|
28
|
+
* CRC64
|
29
|
+
* Pure Ruby implementation.
|
30
|
+
* Provides CRC Tables for optimized calculations.
|
31
|
+
|
32
|
+
## Install
|
33
|
+
|
34
|
+
$ sudo gem install digest-crc
|
35
|
+
|
36
|
+
## Examples
|
37
|
+
|
38
|
+
Calculate a CRC32:
|
39
|
+
|
40
|
+
require 'digest/crc32'
|
41
|
+
|
42
|
+
Digest::CRC32.hexdigest('hello')
|
43
|
+
# => "3610a686"
|
44
|
+
|
45
|
+
Calculate a CRC32 of a file:
|
46
|
+
|
47
|
+
Digest::CRC32.file('README.md')
|
48
|
+
# => #<Digest::CRC32: 127ad531>
|
49
|
+
|
50
|
+
Incrementally calculate a CRC32:
|
51
|
+
|
52
|
+
crc = Digest::CRC32.new
|
53
|
+
crc << 'one'
|
54
|
+
crc << 'two'
|
55
|
+
crc << 'three'
|
56
|
+
crc.hexdigest
|
57
|
+
# => "09e1c092"
|
58
|
+
|
59
|
+
Directly access the checksum:
|
60
|
+
|
61
|
+
crc.checksum
|
62
|
+
# => 165789842
|
63
|
+
|
64
|
+
## Thanks
|
65
|
+
|
66
|
+
Special thanks go out to the [pycrc](http://www.tty1.net/pycrc/) library
|
67
|
+
which is able to generate C source-code for all of the CRC algorithms,
|
68
|
+
including their CRC Tables.
|
69
|
+
|
70
|
+
## License
|
71
|
+
|
72
|
+
See {file:LICENSE.txt} for license information.
|
73
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = 'digest-crc'
|
8
|
+
gem.license = 'MIT'
|
9
|
+
gem.summary = %Q{A Cyclic Redundancy Check (CRC) library for Ruby.}
|
10
|
+
gem.description = %Q{Adds support for calculating Cyclic Redundancy Check (CRC) to the Digest module.}
|
11
|
+
gem.email = 'postmodern.mod3@gmail.com'
|
12
|
+
gem.homepage = 'http://github.com/postmodern/digest-crc'
|
13
|
+
gem.authors = ['Postmodern']
|
14
|
+
gem.add_development_dependency 'rspec', '>= 1.3.0'
|
15
|
+
gem.has_rdoc = 'yard'
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs += ['lib', 'spec']
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
spec.spec_opts = ['--options', '.specopts']
|
27
|
+
end
|
28
|
+
|
29
|
+
task :spec => :check_dependencies
|
30
|
+
task :default => :spec
|
31
|
+
|
32
|
+
begin
|
33
|
+
require 'yard'
|
34
|
+
YARD::Rake::YardocTask.new
|
35
|
+
rescue LoadError
|
36
|
+
task :yard do
|
37
|
+
abort "YARD is not available. In order to run yard, you must: gem install yard"
|
38
|
+
end
|
39
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/digest-crc.gemspec
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{digest-crc}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Postmodern"]
|
12
|
+
s.date = %q{2010-06-01}
|
13
|
+
s.description = %q{Adds support for calculating Cyclic Redundancy Check (CRC) to the Digest module.}
|
14
|
+
s.email = %q{postmodern.mod3@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"ChangeLog.md",
|
17
|
+
"LICENSE.txt",
|
18
|
+
"README.md"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".gitignore",
|
22
|
+
".specopts",
|
23
|
+
".yardopts",
|
24
|
+
"ChangeLog.md",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.md",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"digest-crc.gemspec",
|
30
|
+
"lib/digest/crc.rb",
|
31
|
+
"lib/digest/crc1.rb",
|
32
|
+
"lib/digest/crc16.rb",
|
33
|
+
"lib/digest/crc16_ccitt.rb",
|
34
|
+
"lib/digest/crc16_dnp.rb",
|
35
|
+
"lib/digest/crc16_modbus.rb",
|
36
|
+
"lib/digest/crc16_usb.rb",
|
37
|
+
"lib/digest/crc16_xmodem.rb",
|
38
|
+
"lib/digest/crc16_zmodem.rb",
|
39
|
+
"lib/digest/crc24.rb",
|
40
|
+
"lib/digest/crc32.rb",
|
41
|
+
"lib/digest/crc32_mpeg.rb",
|
42
|
+
"lib/digest/crc5.rb",
|
43
|
+
"lib/digest/crc64.rb",
|
44
|
+
"lib/digest/crc8.rb",
|
45
|
+
"spec/crc16_ccitt_spec.rb",
|
46
|
+
"spec/crc16_modbus_spec.rb",
|
47
|
+
"spec/crc16_spec.rb",
|
48
|
+
"spec/crc16_usb_spec.rb",
|
49
|
+
"spec/crc16_xmodem_spec.rb",
|
50
|
+
"spec/crc16_zmodem_spec.rb",
|
51
|
+
"spec/crc1_spec.rb",
|
52
|
+
"spec/crc24_spec.rb",
|
53
|
+
"spec/crc32_mpeg_spec.rb",
|
54
|
+
"spec/crc32_spec.rb",
|
55
|
+
"spec/crc5_spec.rb",
|
56
|
+
"spec/crc64_spec.rb",
|
57
|
+
"spec/crc8_spec.rb",
|
58
|
+
"spec/crc_examples.rb",
|
59
|
+
"spec/crc_spec.rb",
|
60
|
+
"spec/spec_helper.rb"
|
61
|
+
]
|
62
|
+
s.has_rdoc = %q{yard}
|
63
|
+
s.homepage = %q{http://github.com/postmodern/digest-crc}
|
64
|
+
s.licenses = ["MIT"]
|
65
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
66
|
+
s.require_paths = ["lib"]
|
67
|
+
s.rubygems_version = %q{1.3.7}
|
68
|
+
s.summary = %q{A Cyclic Redundancy Check (CRC) library for Ruby.}
|
69
|
+
s.test_files = [
|
70
|
+
"spec/crc_spec.rb",
|
71
|
+
"spec/crc8_spec.rb",
|
72
|
+
"spec/crc64_spec.rb",
|
73
|
+
"spec/spec_helper.rb",
|
74
|
+
"spec/crc32_mpeg_spec.rb",
|
75
|
+
"spec/crc_examples.rb",
|
76
|
+
"spec/crc16_spec.rb",
|
77
|
+
"spec/crc16_xmodem_spec.rb",
|
78
|
+
"spec/crc5_spec.rb",
|
79
|
+
"spec/crc1_spec.rb",
|
80
|
+
"spec/crc16_usb_spec.rb",
|
81
|
+
"spec/crc16_zmodem_spec.rb",
|
82
|
+
"spec/crc32_spec.rb",
|
83
|
+
"spec/crc24_spec.rb",
|
84
|
+
"spec/crc16_ccitt_spec.rb",
|
85
|
+
"spec/crc16_modbus_spec.rb"
|
86
|
+
]
|
87
|
+
|
88
|
+
if s.respond_to? :specification_version then
|
89
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
90
|
+
s.specification_version = 3
|
91
|
+
|
92
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
93
|
+
s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
|
94
|
+
else
|
95
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
96
|
+
end
|
97
|
+
else
|
98
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
data/lib/digest/crc.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'digest'
|
2
|
+
|
3
|
+
module Digest
|
4
|
+
class CRC < Digest::Class
|
5
|
+
|
6
|
+
include Digest::Instance
|
7
|
+
|
8
|
+
# The initial value of the CRC checksum
|
9
|
+
INIT_CRC = 0x00
|
10
|
+
|
11
|
+
# The XOR mask to apply to the resulting CRC checksum
|
12
|
+
XOR_MASK = 0x00
|
13
|
+
|
14
|
+
# The bit width of the CRC checksum
|
15
|
+
WIDTH = 0
|
16
|
+
|
17
|
+
#
|
18
|
+
# Calculates the CRC checksum.
|
19
|
+
#
|
20
|
+
# @param [String] data
|
21
|
+
# The given data.
|
22
|
+
#
|
23
|
+
# @return [Integer]
|
24
|
+
# The CRC checksum.
|
25
|
+
#
|
26
|
+
def self.checksum(data)
|
27
|
+
crc = self.new
|
28
|
+
crc << data
|
29
|
+
|
30
|
+
return crc.checksum
|
31
|
+
end
|
32
|
+
|
33
|
+
#
|
34
|
+
# Packs the given CRC checksum.
|
35
|
+
#
|
36
|
+
# @return [String]
|
37
|
+
# The packed CRC checksum.
|
38
|
+
#
|
39
|
+
def self.pack(crc)
|
40
|
+
''
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# Initializes the CRC checksum.
|
45
|
+
#
|
46
|
+
def initialize
|
47
|
+
@crc = self.class.const_get(:INIT_CRC)
|
48
|
+
end
|
49
|
+
|
50
|
+
#
|
51
|
+
# The input block length.
|
52
|
+
#
|
53
|
+
# @return [1]
|
54
|
+
#
|
55
|
+
def block_length
|
56
|
+
1
|
57
|
+
end
|
58
|
+
|
59
|
+
#
|
60
|
+
# The length of the digest.
|
61
|
+
#
|
62
|
+
# @return [Integer]
|
63
|
+
# The length in bytes.
|
64
|
+
#
|
65
|
+
def digest_length
|
66
|
+
(self.class.const_get(:WIDTH) / 8.0).ceil
|
67
|
+
end
|
68
|
+
|
69
|
+
#
|
70
|
+
# Updates the CRC checksum with the given data.
|
71
|
+
#
|
72
|
+
# @param [String] data
|
73
|
+
# The data to update the CRC checksum with.
|
74
|
+
#
|
75
|
+
def update(data)
|
76
|
+
end
|
77
|
+
|
78
|
+
#
|
79
|
+
# @see {#update}
|
80
|
+
#
|
81
|
+
def <<(data)
|
82
|
+
update(data)
|
83
|
+
return self
|
84
|
+
end
|
85
|
+
|
86
|
+
#
|
87
|
+
# Resets the CRC checksum.
|
88
|
+
#
|
89
|
+
# @return [Integer]
|
90
|
+
# The default value of the CRC checksum.
|
91
|
+
#
|
92
|
+
def reset
|
93
|
+
@crc = self.class.const_get(:INIT_CRC)
|
94
|
+
end
|
95
|
+
|
96
|
+
#
|
97
|
+
# The resulting CRC checksum.
|
98
|
+
#
|
99
|
+
# @return [Integer]
|
100
|
+
# The resulting CRC checksum.
|
101
|
+
#
|
102
|
+
def checksum
|
103
|
+
@crc ^ self.class.const_get(:XOR_MASK)
|
104
|
+
end
|
105
|
+
|
106
|
+
#
|
107
|
+
# Finishes the CRC checksum calculation.
|
108
|
+
#
|
109
|
+
# @see {pack}
|
110
|
+
#
|
111
|
+
def finish
|
112
|
+
self.class.pack(checksum)
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
end
|
data/lib/digest/crc1.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'digest/crc'
|
2
|
+
|
3
|
+
module Digest
|
4
|
+
class CRC1 < CRC
|
5
|
+
|
6
|
+
TABLE = []
|
7
|
+
CRC_MASK = 0x00
|
8
|
+
|
9
|
+
#
|
10
|
+
# Packs the CRC1 checksum.
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
# The CRC1 checksum.
|
14
|
+
#
|
15
|
+
def self.pack(crc)
|
16
|
+
[crc].pack('c*')
|
17
|
+
end
|
18
|
+
|
19
|
+
#
|
20
|
+
# Updates the CRC1 checksum.
|
21
|
+
#
|
22
|
+
# @param [String] data
|
23
|
+
# The data to update the checksum with.
|
24
|
+
#
|
25
|
+
def update(data)
|
26
|
+
accum = 0
|
27
|
+
data.each_byte { |b| accum += b }
|
28
|
+
|
29
|
+
@crc += (accum % 256)
|
30
|
+
|
31
|
+
return self
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
data/lib/digest/crc16.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'digest/crc'
|
2
|
+
|
3
|
+
module Digest
|
4
|
+
class CRC16 < CRC
|
5
|
+
|
6
|
+
WIDTH = 16
|
7
|
+
|
8
|
+
INIT_CRC = 0x0000
|
9
|
+
|
10
|
+
# Generated by `./pycrc.py --algorithm=table-driven --model=crc-16`
|
11
|
+
TABLE = [
|
12
|
+
0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
|
13
|
+
0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
|
14
|
+
0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
|
15
|
+
0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
|
16
|
+
0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
|
17
|
+
0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
|
18
|
+
0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
|
19
|
+
0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
|
20
|
+
0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
|
21
|
+
0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
|
22
|
+
0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
|
23
|
+
0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
|
24
|
+
0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
|
25
|
+
0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
|
26
|
+
0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
|
27
|
+
0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
|
28
|
+
0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
|
29
|
+
0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
|
30
|
+
0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
|
31
|
+
0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
|
32
|
+
0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
|
33
|
+
0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
|
34
|
+
0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
|
35
|
+
0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
|
36
|
+
0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
|
37
|
+
0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
|
38
|
+
0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
|
39
|
+
0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
|
40
|
+
0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
|
41
|
+
0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
|
42
|
+
0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
|
43
|
+
0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
|
44
|
+
]
|
45
|
+
|
46
|
+
#
|
47
|
+
# Packs the CRC16 checksum.
|
48
|
+
#
|
49
|
+
# @param [Integer] crc
|
50
|
+
# The CRC16 checksum to pack.
|
51
|
+
#
|
52
|
+
# @return [String]
|
53
|
+
# The packed CRC16 checksum.
|
54
|
+
#
|
55
|
+
def self.pack(crc)
|
56
|
+
buffer = ''
|
57
|
+
|
58
|
+
buffer << ((crc & 0xff00) >> 8).chr
|
59
|
+
buffer << (crc & 0xff).chr
|
60
|
+
|
61
|
+
buffer
|
62
|
+
end
|
63
|
+
|
64
|
+
#
|
65
|
+
# Updates the CRC16 checksum.
|
66
|
+
#
|
67
|
+
# @param [String] data
|
68
|
+
# The data to update the checksum with.
|
69
|
+
#
|
70
|
+
def update(data)
|
71
|
+
data.each_byte do |b|
|
72
|
+
@crc = ((TABLE[(@crc ^ b) & 0xff] ^ (@crc >> 8)) & 0xffff)
|
73
|
+
end
|
74
|
+
|
75
|
+
return self
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|