digest-crc 0.6.5 → 0.7.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.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/integration.yml +1 -1
  3. data/.github/workflows/ruby.yml +3 -3
  4. data/ChangeLog.md +6 -0
  5. data/LICENSE.txt +1 -1
  6. data/README.md +5 -10
  7. data/benchmarks.rb +1 -0
  8. data/digest-crc.gemspec +8 -4
  9. data/ext/digest/crc64_nvme/crc64_nvme.c +102 -0
  10. data/ext/digest/crc64_nvme/crc64_nvme.h +8 -0
  11. data/ext/digest/crc64_nvme/crc64_nvme_ext.c +31 -0
  12. data/ext/digest/crc64_nvme/extconf.rb +7 -0
  13. data/gemspec.yml +1 -1
  14. data/lib/digest/crc.rb +85 -9
  15. data/lib/digest/crc1.rb +2 -0
  16. data/lib/digest/crc15.rb +2 -32
  17. data/lib/digest/crc16.rb +2 -32
  18. data/lib/digest/crc16_ccitt.rb +2 -14
  19. data/lib/digest/crc16_dnp.rb +2 -14
  20. data/lib/digest/crc16_genibus.rb +2 -14
  21. data/lib/digest/crc16_kermit.rb +2 -14
  22. data/lib/digest/crc16_x_25.rb +3 -1
  23. data/lib/digest/crc16_xmodem.rb +2 -14
  24. data/lib/digest/crc16_zmodem.rb +2 -14
  25. data/lib/digest/crc24.rb +2 -33
  26. data/lib/digest/crc32.rb +2 -34
  27. data/lib/digest/crc32_bzip2.rb +2 -14
  28. data/lib/digest/crc32_mpeg.rb +2 -14
  29. data/lib/digest/crc32_posix.rb +2 -14
  30. data/lib/digest/crc32_xfer.rb +2 -14
  31. data/lib/digest/crc5.rb +2 -27
  32. data/lib/digest/crc64.rb +2 -38
  33. data/lib/digest/crc64_nvme.rb +90 -0
  34. data/lib/digest/crc8.rb +2 -27
  35. metadata +8 -36
  36. data/spec/crc15_spec.rb +0 -10
  37. data/spec/crc16_ccitt_spec.rb +0 -10
  38. data/spec/crc16_genibus_spec.rb +0 -10
  39. data/spec/crc16_kermit_spec.rb +0 -10
  40. data/spec/crc16_modbus_spec.rb +0 -10
  41. data/spec/crc16_qt_spec.rb +0 -10
  42. data/spec/crc16_spec.rb +0 -10
  43. data/spec/crc16_usb_spec.rb +0 -10
  44. data/spec/crc16_x_25_spec.rb +0 -10
  45. data/spec/crc16_xmodem_spec.rb +0 -10
  46. data/spec/crc16_zmodem_spec.rb +0 -10
  47. data/spec/crc1_spec.rb +0 -10
  48. data/spec/crc24_spec.rb +0 -10
  49. data/spec/crc32_bzip2_spec.rb +0 -10
  50. data/spec/crc32_jam_spec.rb +0 -10
  51. data/spec/crc32_mpeg_spec.rb +0 -16
  52. data/spec/crc32_posix_spec.rb +0 -10
  53. data/spec/crc32_spec.rb +0 -10
  54. data/spec/crc32_xfer_spec.rb +0 -10
  55. data/spec/crc32c_spec.rb +0 -10
  56. data/spec/crc5_spec.rb +0 -10
  57. data/spec/crc64_jones_spec.rb +0 -10
  58. data/spec/crc64_spec.rb +0 -10
  59. data/spec/crc64_xz_spec.rb +0 -10
  60. data/spec/crc8_1wire_spec.rb +0 -16
  61. data/spec/crc8_spec.rb +0 -10
  62. data/spec/crc_examples.rb +0 -37
  63. data/spec/crc_spec.rb +0 -72
  64. data/spec/integration/docker/Dockerfile.base +0 -9
  65. data/spec/integration/docker/Dockerfile.with-gcc +0 -3
  66. data/spec/integration/docker/Dockerfile.with-gcc-and-make +0 -3
  67. data/spec/integration/install_spec.rb +0 -59
  68. data/spec/spec_helper.rb +0 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a91420951947a2ed738b2b786ab0c22105124fa9c92fa2cbaebf5664abd8b8e4
4
- data.tar.gz: b46f6cc23d7d6e300d55524a31d4cb4c331bd3c0489819c56d6f05e60c6246bc
3
+ metadata.gz: dc7bf9540848173d66f9cd834da9d5a77d2f185bfe839023195cd16c6041dd6d
4
+ data.tar.gz: 5fb1d168b82d68f2bec09eecfeb6666d47a6ad0c46689b9320a67c5791975dd3
5
5
  SHA512:
6
- metadata.gz: ce767424edcbdf407a449b6f3dc3f31bf3cb60f240bbaf47d4bc8754da13d913b05644bc77e73c892f419e289cdde52651496c67d72a754d7d562528a9c3a64a
7
- data.tar.gz: 334b17ae6e0e36546cf99617d9a0f4e2bf826bb1db578f4a8c4f556f66edf9585f00fe21b5e69865105e873e50a458e820ede34ddf204883858def40dbdfff4b
6
+ metadata.gz: 768dc6d55b6aaa199d56c1d554d6f7dc91a53c2f774e381dce0772bd6430e0e58b0e1a7d4cede0397e30e6eed4238214a636dedac061d7cfbf0b931b668f0590
7
+ data.tar.gz: 3273faef714eb259b681b56c825c7cba2e7950fe25a2d7a275ae2e19af92e15d09b7d1ab811572fc27d55bcf42f1d6b5c7955261e937b94c80d214af608dd216
@@ -13,7 +13,7 @@ jobs:
13
13
  fail-fast: false
14
14
  name: Integration Testing
15
15
  steps:
16
- - uses: actions/checkout@v2
16
+ - uses: actions/checkout@v4
17
17
  - name: Set up Docker
18
18
  uses: docker-practice/actions-setup-docker@master
19
19
  - name: Set up Ruby
@@ -12,15 +12,15 @@ jobs:
12
12
  - ubuntu-latest
13
13
  - macos-latest
14
14
  ruby:
15
- - '2.7'
16
15
  - '3.0'
17
16
  - '3.1'
18
- - '3.2.0-preview1'
17
+ - '3.2'
18
+ - '3.3'
19
19
  - jruby
20
20
  - truffleruby
21
21
  name: OS ${{ matrix.os }} / Ruby ${{ matrix.ruby }}
22
22
  steps:
23
- - uses: actions/checkout@v2
23
+ - uses: actions/checkout@v4
24
24
  - name: Set up Ruby
25
25
  uses: ruby/setup-ruby@v1
26
26
  with:
data/ChangeLog.md CHANGED
@@ -1,3 +1,9 @@
1
+ ### 0.7.0 / 2025-01-28
2
+
3
+ * Added {Digest::CRC64NVMe}.
4
+ * Added {Digest::CRC.implementation}.
5
+ * Reduced duplicate code.
6
+
1
7
  ### 0.6.5 / 2023-07-03
2
8
 
3
9
  * Corrected the implementation of {Digest::CRC5}.
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010-2021 Hal Brodigan
1
+ Copyright (c) 2010-2025 Hal Brodigan
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -40,6 +40,7 @@ module.
40
40
  * [CRC32 XFER](https://rubydoc.info/gems/digest-crc/Digest/CRC32XFER)
41
41
  * [CRC64](https://rubydoc.info/gems/digest-crc/Digest/CRC64)
42
42
  * [CRC64 Jones](https://rubydoc.info/gems/digest-crc/Digest/CRC64Jones)
43
+ * [CRC64 NVMe](https://rubydoc.info/gems/digest-crc/Digest/CRC64Nvme)
43
44
  * [CRC64 XZ](https://rubydoc.info/gems/digest-crc/Digest/CRC64XZ)
44
45
  * Pure Ruby implementation.
45
46
  * Provides CRC Tables for optimized calculations.
@@ -115,7 +116,9 @@ require 'digest/crc32'
115
116
  module Digest
116
117
  class CRC3000 < CRC32
117
118
 
118
- WIDTH = 4
119
+ WIDTH = 32
120
+
121
+ REFLECT_INPUT = true
119
122
 
120
123
  INIT_CRC = 0xffffffff
121
124
 
@@ -124,14 +127,6 @@ module Digest
124
127
  TABLE = [
125
128
  # ....
126
129
  ].freeze
127
-
128
- def update(data)
129
- data.each_byte do |b|
130
- @crc = (((@crc >> 8) & 0x00ffffff) ^ @table[(@crc ^ b) & 0xff])
131
- end
132
-
133
- return self
134
- end
135
130
  end
136
131
  end
137
132
  ```
@@ -401,6 +396,6 @@ including their CRC Tables.
401
396
 
402
397
  ## License
403
398
 
404
- Copyright (c) 2010-2021 Hal Brodigan
399
+ Copyright (c) 2010-2025 Hal Brodigan
405
400
 
406
401
  See {file:LICENSE.txt} for license information.
data/benchmarks.rb CHANGED
@@ -29,6 +29,7 @@ CRCs = {
29
29
  'crc32_xfer' => 'CRC32XFER',
30
30
  'crc64' => 'CRC64',
31
31
  'crc64_jones' => 'CRC64Jones',
32
+ 'crc64_nvme' => 'CRC64NVMe',
32
33
  'crc64_xz' => 'CRC64XZ',
33
34
  }
34
35
 
data/digest-crc.gemspec CHANGED
@@ -22,10 +22,14 @@ Gem::Specification.new do |gem|
22
22
  gem.homepage = gemspec['homepage']
23
23
  gem.metadata = gemspec['metadata'] if gemspec['metadata']
24
24
 
25
- glob = lambda { |patterns| gem.files & Dir[*patterns] }
25
+ glob = ->(patterns) { gem.files & Dir[*patterns] }
26
26
 
27
- gem.files = `git ls-files`.split($/)
28
- gem.files = glob[gemspec['files']] if gemspec['files']
27
+ gem.files = if gemspec['files'] then glob[gemspec['files']]
28
+ else `git ls-files`.split($/)
29
+ end
30
+
31
+ # exclude test files from the packages gem
32
+ gem.files -= glob[gemspec['test_files'] || 'spec/{**/}*']
29
33
 
30
34
  gem.executables = gemspec.fetch('executables') do
31
35
  glob['bin/*'].map { |path| File.basename(path) }
@@ -45,7 +49,7 @@ Gem::Specification.new do |gem|
45
49
  gem.required_rubygems_version = gemspec['required_rubygems_version']
46
50
  gem.post_install_message = gemspec['post_install_message']
47
51
 
48
- split = lambda { |string| string.split(/,\s*/) }
52
+ split = ->(string) { string.split(/,\s*/) }
49
53
 
50
54
  if gemspec['dependencies']
51
55
  gemspec['dependencies'].each do |name,versions|
@@ -0,0 +1,102 @@
1
+ /**
2
+ * \file
3
+ * Functions and types for CRC checks.
4
+ *
5
+ * Generated on Mon Jan 6 09:50:24 2025
6
+ * by pycrc v0.10.0, https://pycrc.org
7
+ * using the configuration:
8
+ * - Width. = 64
9
+ * - Poly. = 0xad93d23594c93659
10
+ * - XorIn. = 0xffffffffffffffff
11
+ * - ReflectIn = True
12
+ * - XorOut. = 0xffffffffffffffff
13
+ * - ReflectOut = True
14
+ * - Algorithm = table-driven
15
+ */
16
+
17
+ #include "crc64_nvme.h"
18
+
19
+ /**
20
+ * Static table used for the table_driven implementation.
21
+ */
22
+ static const crc64_t crc64_table[256] = {
23
+ 0x0000000000000000, 0x7f6ef0c830358979, 0xfedde190606b12f2, 0x81b31158505e9b8b,
24
+ 0xc962e5739841b68f, 0xb60c15bba8743ff6, 0x37bf04e3f82aa47d, 0x48d1f42bc81f2d04,
25
+ 0xa61cecb46814fe75, 0xd9721c7c5821770c, 0x58c10d24087fec87, 0x27affdec384a65fe,
26
+ 0x6f7e09c7f05548fa, 0x1010f90fc060c183, 0x91a3e857903e5a08, 0xeecd189fa00bd371,
27
+ 0x78e0ff3b88be6f81, 0x078e0ff3b88be6f8, 0x863d1eabe8d57d73, 0xf953ee63d8e0f40a,
28
+ 0xb1821a4810ffd90e, 0xceecea8020ca5077, 0x4f5ffbd87094cbfc, 0x30310b1040a14285,
29
+ 0xdefc138fe0aa91f4, 0xa192e347d09f188d, 0x2021f21f80c18306, 0x5f4f02d7b0f40a7f,
30
+ 0x179ef6fc78eb277b, 0x68f0063448deae02, 0xe943176c18803589, 0x962de7a428b5bcf0,
31
+ 0xf1c1fe77117cdf02, 0x8eaf0ebf2149567b, 0x0f1c1fe77117cdf0, 0x7072ef2f41224489,
32
+ 0x38a31b04893d698d, 0x47cdebccb908e0f4, 0xc67efa94e9567b7f, 0xb9100a5cd963f206,
33
+ 0x57dd12c379682177, 0x28b3e20b495da80e, 0xa900f35319033385, 0xd66e039b2936bafc,
34
+ 0x9ebff7b0e12997f8, 0xe1d10778d11c1e81, 0x606216208142850a, 0x1f0ce6e8b1770c73,
35
+ 0x8921014c99c2b083, 0xf64ff184a9f739fa, 0x77fce0dcf9a9a271, 0x08921014c99c2b08,
36
+ 0x4043e43f0183060c, 0x3f2d14f731b68f75, 0xbe9e05af61e814fe, 0xc1f0f56751dd9d87,
37
+ 0x2f3dedf8f1d64ef6, 0x50531d30c1e3c78f, 0xd1e00c6891bd5c04, 0xae8efca0a188d57d,
38
+ 0xe65f088b6997f879, 0x9931f84359a27100, 0x1882e91b09fcea8b, 0x67ec19d339c963f2,
39
+ 0xd75adabd7a6e2d6f, 0xa8342a754a5ba416, 0x29873b2d1a053f9d, 0x56e9cbe52a30b6e4,
40
+ 0x1e383fcee22f9be0, 0x6156cf06d21a1299, 0xe0e5de5e82448912, 0x9f8b2e96b271006b,
41
+ 0x71463609127ad31a, 0x0e28c6c1224f5a63, 0x8f9bd7997211c1e8, 0xf0f5275142244891,
42
+ 0xb824d37a8a3b6595, 0xc74a23b2ba0eecec, 0x46f932eaea507767, 0x3997c222da65fe1e,
43
+ 0xafba2586f2d042ee, 0xd0d4d54ec2e5cb97, 0x5167c41692bb501c, 0x2e0934dea28ed965,
44
+ 0x66d8c0f56a91f461, 0x19b6303d5aa47d18, 0x980521650afae693, 0xe76bd1ad3acf6fea,
45
+ 0x09a6c9329ac4bc9b, 0x76c839faaaf135e2, 0xf77b28a2faafae69, 0x8815d86aca9a2710,
46
+ 0xc0c42c4102850a14, 0xbfaadc8932b0836d, 0x3e19cdd162ee18e6, 0x41773d1952db919f,
47
+ 0x269b24ca6b12f26d, 0x59f5d4025b277b14, 0xd846c55a0b79e09f, 0xa72835923b4c69e6,
48
+ 0xeff9c1b9f35344e2, 0x90973171c366cd9b, 0x1124202993385610, 0x6e4ad0e1a30ddf69,
49
+ 0x8087c87e03060c18, 0xffe938b633338561, 0x7e5a29ee636d1eea, 0x0134d92653589793,
50
+ 0x49e52d0d9b47ba97, 0x368bddc5ab7233ee, 0xb738cc9dfb2ca865, 0xc8563c55cb19211c,
51
+ 0x5e7bdbf1e3ac9dec, 0x21152b39d3991495, 0xa0a63a6183c78f1e, 0xdfc8caa9b3f20667,
52
+ 0x97193e827bed2b63, 0xe877ce4a4bd8a21a, 0x69c4df121b863991, 0x16aa2fda2bb3b0e8,
53
+ 0xf86737458bb86399, 0x8709c78dbb8deae0, 0x06bad6d5ebd3716b, 0x79d4261ddbe6f812,
54
+ 0x3105d23613f9d516, 0x4e6b22fe23cc5c6f, 0xcfd833a67392c7e4, 0xb0b6c36e43a74e9d,
55
+ 0x9a6c9329ac4bc9b5, 0xe50263e19c7e40cc, 0x64b172b9cc20db47, 0x1bdf8271fc15523e,
56
+ 0x530e765a340a7f3a, 0x2c608692043ff643, 0xadd397ca54616dc8, 0xd2bd67026454e4b1,
57
+ 0x3c707f9dc45f37c0, 0x431e8f55f46abeb9, 0xc2ad9e0da4342532, 0xbdc36ec59401ac4b,
58
+ 0xf5129aee5c1e814f, 0x8a7c6a266c2b0836, 0x0bcf7b7e3c7593bd, 0x74a18bb60c401ac4,
59
+ 0xe28c6c1224f5a634, 0x9de29cda14c02f4d, 0x1c518d82449eb4c6, 0x633f7d4a74ab3dbf,
60
+ 0x2bee8961bcb410bb, 0x548079a98c8199c2, 0xd53368f1dcdf0249, 0xaa5d9839ecea8b30,
61
+ 0x449080a64ce15841, 0x3bfe706e7cd4d138, 0xba4d61362c8a4ab3, 0xc52391fe1cbfc3ca,
62
+ 0x8df265d5d4a0eece, 0xf29c951de49567b7, 0x732f8445b4cbfc3c, 0x0c41748d84fe7545,
63
+ 0x6bad6d5ebd3716b7, 0x14c39d968d029fce, 0x95708ccedd5c0445, 0xea1e7c06ed698d3c,
64
+ 0xa2cf882d2576a038, 0xdda178e515432941, 0x5c1269bd451db2ca, 0x237c997575283bb3,
65
+ 0xcdb181ead523e8c2, 0xb2df7122e51661bb, 0x336c607ab548fa30, 0x4c0290b2857d7349,
66
+ 0x04d364994d625e4d, 0x7bbd94517d57d734, 0xfa0e85092d094cbf, 0x856075c11d3cc5c6,
67
+ 0x134d926535897936, 0x6c2362ad05bcf04f, 0xed9073f555e26bc4, 0x92fe833d65d7e2bd,
68
+ 0xda2f7716adc8cfb9, 0xa54187de9dfd46c0, 0x24f29686cda3dd4b, 0x5b9c664efd965432,
69
+ 0xb5517ed15d9d8743, 0xca3f8e196da80e3a, 0x4b8c9f413df695b1, 0x34e26f890dc31cc8,
70
+ 0x7c339ba2c5dc31cc, 0x035d6b6af5e9b8b5, 0x82ee7a32a5b7233e, 0xfd808afa9582aa47,
71
+ 0x4d364994d625e4da, 0x3258b95ce6106da3, 0xb3eba804b64ef628, 0xcc8558cc867b7f51,
72
+ 0x8454ace74e645255, 0xfb3a5c2f7e51db2c, 0x7a894d772e0f40a7, 0x05e7bdbf1e3ac9de,
73
+ 0xeb2aa520be311aaf, 0x944455e88e0493d6, 0x15f744b0de5a085d, 0x6a99b478ee6f8124,
74
+ 0x224840532670ac20, 0x5d26b09b16452559, 0xdc95a1c3461bbed2, 0xa3fb510b762e37ab,
75
+ 0x35d6b6af5e9b8b5b, 0x4ab846676eae0222, 0xcb0b573f3ef099a9, 0xb465a7f70ec510d0,
76
+ 0xfcb453dcc6da3dd4, 0x83daa314f6efb4ad, 0x0269b24ca6b12f26, 0x7d0742849684a65f,
77
+ 0x93ca5a1b368f752e, 0xeca4aad306bafc57, 0x6d17bb8b56e467dc, 0x12794b4366d1eea5,
78
+ 0x5aa8bf68aecec3a1, 0x25c64fa09efb4ad8, 0xa4755ef8cea5d153, 0xdb1bae30fe90582a,
79
+ 0xbcf7b7e3c7593bd8, 0xc399472bf76cb2a1, 0x422a5673a732292a, 0x3d44a6bb9707a053,
80
+ 0x759552905f188d57, 0x0afba2586f2d042e, 0x8b48b3003f739fa5, 0xf42643c80f4616dc,
81
+ 0x1aeb5b57af4dc5ad, 0x6585ab9f9f784cd4, 0xe436bac7cf26d75f, 0x9b584a0fff135e26,
82
+ 0xd389be24370c7322, 0xace74eec0739fa5b, 0x2d545fb4576761d0, 0x523aaf7c6752e8a9,
83
+ 0xc41748d84fe75459, 0xbb79b8107fd2dd20, 0x3acaa9482f8c46ab, 0x45a459801fb9cfd2,
84
+ 0x0d75adabd7a6e2d6, 0x721b5d63e7936baf, 0xf3a84c3bb7cdf024, 0x8cc6bcf387f8795d,
85
+ 0x620ba46c27f3aa2c, 0x1d6554a417c62355, 0x9cd645fc4798b8de, 0xe3b8b53477ad31a7,
86
+ 0xab69411fbfb21ca3, 0xd407b1d78f8795da, 0x55b4a08fdfd90e51, 0x2ada5047efec8728
87
+ };
88
+
89
+ crc64_t crc64_nvme_update(crc64_t crc, const void *data, size_t data_len)
90
+ {
91
+ const unsigned char *d = (const unsigned char *)data;
92
+ unsigned int tbl_idx;
93
+
94
+ while (data_len--)
95
+ {
96
+ tbl_idx = (crc ^ *d) & 0xff;
97
+ crc = (crc64_table[tbl_idx] ^ (crc >> 8)) & 0xffffffffffffffff;
98
+ d++;
99
+ }
100
+
101
+ return crc & 0xffffffffffffffff;
102
+ }
@@ -0,0 +1,8 @@
1
+ #ifndef _CRC64_NVME_H_
2
+ #define _CRC64_NVME_H_
3
+
4
+ #include "../crc64/crc64.h"
5
+
6
+ crc64_t crc64_nvme_update(crc64_t crc, const void *data, size_t data_len);
7
+
8
+ #endif
@@ -0,0 +1,31 @@
1
+ #include "ruby.h"
2
+ #include "extconf.h"
3
+
4
+ #include "crc64_nvme.h"
5
+
6
+ VALUE Digest_CRC64NVMe_update(VALUE self, VALUE data)
7
+ {
8
+ VALUE crc_ivar_name = rb_intern("@crc");
9
+ VALUE crc_ivar = rb_ivar_get(self, crc_ivar_name);
10
+ crc64_t crc = NUM2ULONG(crc_ivar);
11
+
12
+ const char *data_ptr = StringValuePtr(data);
13
+ size_t length = RSTRING_LEN(data);
14
+
15
+ crc = crc64_nvme_update(crc,data_ptr,length);
16
+
17
+ rb_ivar_set(self, crc_ivar_name, ULONG2NUM(crc));
18
+ return self;
19
+ }
20
+
21
+ void Init_crc64_nvme_ext()
22
+ {
23
+ VALUE mDigest = rb_const_get(rb_cObject, rb_intern("Digest"));
24
+ VALUE cCRC64NVMe = rb_const_get(mDigest, rb_intern("CRC64NVMe"));
25
+
26
+ #ifdef HAVE_RB_EXT_RACTOR_SAFE
27
+ rb_ext_ractor_safe(true);
28
+ #endif
29
+
30
+ rb_define_method(cCRC64NVMe, "update", Digest_CRC64NVMe_update, 1);
31
+ }
@@ -0,0 +1,7 @@
1
+ require 'mkmf'
2
+
3
+ have_header("stdint.h")
4
+ have_header('stddef.h')
5
+
6
+ create_header
7
+ create_makefile "crc64_nvme_ext"
data/gemspec.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  name: digest-crc
2
- version: 0.6.5
2
+ version: 0.7.0
3
3
  summary: A Cyclic Redundancy Check (CRC) library for Ruby.
4
4
  description:
5
5
  Adds support for calculating Cyclic Redundancy Check (CRC) to the Digest
data/lib/digest/crc.rb CHANGED
@@ -17,6 +17,9 @@ module Digest
17
17
  # The bit width of the CRC checksum
18
18
  WIDTH = 0
19
19
 
20
+ # Define true or false whether the input direction is bit reversed or not of the CRC checksum
21
+ REFLECT_INPUT = nil
22
+
20
23
  # Default place holder CRC table
21
24
  TABLE = [].freeze
22
25
 
@@ -45,20 +48,46 @@ module Digest
45
48
  # @return [String]
46
49
  # The packed CRC checksum.
47
50
  #
48
- # @abstract
49
- #
50
51
  def self.pack(crc)
51
- raise(NotImplementedError,"#{self.class}##{__method__} not implemented")
52
+ unless (width = self::WIDTH) > 0
53
+ raise(NotImplementedError, "#{self} is incompleted as CRC")
54
+ end
55
+
56
+ bitclass = width + (-width & 0x07)
57
+ byte_count = bitclass / 8
58
+
59
+ crc &= ~(-1 << width)
60
+
61
+ result = [crc].pack("Q>")
62
+ result[0, result.bytesize - byte_count] = ""
63
+ result
64
+ end
65
+
66
+ #
67
+ # Determines whether the library is using the optimized C extensions
68
+ # implementation, or the pure-Ruby implementation.
69
+ #
70
+ # @return [:c_ext, :pure]
71
+ #
72
+ # @since 0.7.0
73
+ #
74
+ def self.implementation
75
+ if instance_method(:update).source_location.nil?
76
+ :c_ext
77
+ else
78
+ :pure
79
+ end
52
80
  end
53
81
 
54
82
  #
55
83
  # Initializes the CRC checksum.
56
84
  #
57
85
  def initialize
58
- @init_crc = self.class.const_get(:INIT_CRC)
59
- @xor_mask = self.class.const_get(:XOR_MASK)
60
- @width = self.class.const_get(:WIDTH)
61
- @table = self.class.const_get(:TABLE)
86
+ @init_crc = self.class.const_get(:INIT_CRC)
87
+ @xor_mask = self.class.const_get(:XOR_MASK)
88
+ @width = self.class.const_get(:WIDTH)
89
+ @reflect_input = self.class.const_get(:REFLECT_INPUT)
90
+ @table = self.class.const_get(:TABLE)
62
91
 
63
92
  reset
64
93
  end
@@ -88,10 +117,57 @@ module Digest
88
117
  # @param [String] data
89
118
  # The data to update the CRC checksum with.
90
119
  #
91
- # @abstract
120
+ # @raise [NotImplementedError]
121
+ # If WIDTH, TABLE, or REFLECT_INPUT constants are not set properly.
92
122
  #
93
123
  def update(data)
94
- raise(NotImplementedError,"#{self.class}##{__method__} not implemented")
124
+ unless @width >= 1
125
+ raise(NotImplementedError, "incompleted #{self.class} as CRC (expected WIDTH to be 1 or more)")
126
+ end
127
+
128
+ if @table.empty?
129
+ raise(NotImplementedError, "incompleted #{self.class} as CRC (expected TABLE to be not empty)")
130
+ end
131
+
132
+ if @reflect_input.nil?
133
+ raise(NotImplementedError, "incompleted #{self.class} as CRC (expected REFLECT_INPUT to be not nil)")
134
+ end
135
+
136
+ table = @table
137
+ crc = @crc
138
+
139
+ if @reflect_input
140
+ if @width > 8
141
+ data.each_byte do |b|
142
+ crc = table[b ^ (0xff & crc)] ^ (crc >> 8)
143
+ end
144
+ else
145
+ data.each_byte do |b|
146
+ # Omit (crc >> 8) since bits upper than the lower 8 bits are always 0
147
+ crc = table[b ^ (0xff & crc)]
148
+ end
149
+ end
150
+ else
151
+ if @width > 8
152
+ higher_bit_off = @width - 8
153
+ remain_mask = ~(-1 << higher_bit_off)
154
+
155
+ data.each_byte do |b|
156
+ crc = table[b ^ (0xff & (crc >> higher_bit_off))] ^ ((remain_mask & crc) << 8)
157
+ end
158
+ else
159
+ padding = 8 - @width
160
+
161
+ data.each_byte do |b|
162
+ # Omit (crc << 8) since bits lower than the upper 8 bits are always 0
163
+ crc = table[b ^ (0xff & (crc << padding))]
164
+ end
165
+ end
166
+ end
167
+
168
+ @crc = crc
169
+
170
+ self
95
171
  end
96
172
 
97
173
  #
data/lib/digest/crc1.rb CHANGED
@@ -6,6 +6,8 @@ module Digest
6
6
  #
7
7
  class CRC1 < CRC
8
8
 
9
+ WIDTH = 1
10
+
9
11
  #
10
12
  # Packs the CRC1 checksum.
11
13
  #
data/lib/digest/crc15.rb CHANGED
@@ -10,6 +10,8 @@ module Digest
10
10
 
11
11
  WIDTH = 15
12
12
 
13
+ REFLECT_INPUT = false
14
+
13
15
  # Generated by `./pycrc.py --algorithm=table-driven --model=crc-16 --generate=c`
14
16
  TABLE = [
15
17
  0x0000, 0x4599, 0x4eab, 0x0b32, 0x58cf, 0x1d56, 0x1664, 0x53fd, 0x7407, 0x319e, 0x3aac, 0x7f35, 0x2cc8, 0x6951, 0x6263, 0x27fa,
@@ -30,38 +32,6 @@ module Digest
30
32
  0x276f, 0x62f6, 0x69c4, 0x2c5d, 0x7fa0, 0x3a39, 0x310b, 0x7492, 0x5368, 0x16f1, 0x1dc3, 0x585a, 0x0ba7, 0x4e3e, 0x450c, 0x0095
31
33
  ].freeze
32
34
 
33
- #
34
- # Packs the CRC15 checksum.
35
- #
36
- # @param [Integer] crc
37
- # The CRC15 checksum to pack.
38
- #
39
- # @return [String]
40
- # The packed CRC15 checksum.
41
- #
42
- def self.pack(crc)
43
- buffer = ''
44
-
45
- buffer << ((crc & 0x7f00) >> 8).chr
46
- buffer << (crc & 0xff).chr
47
-
48
- buffer
49
- end
50
-
51
- #
52
- # Updates the CRC15 checksum.
53
- #
54
- # @param [String] data
55
- # The data to update the checksum with.
56
- #
57
- def update(data)
58
- data.each_byte do |b|
59
- @crc = (@table[((@crc >> 7) ^ b) & 0xff] ^ (@crc << 8)) & 0x7fff
60
- end
61
-
62
- return self
63
- end
64
-
65
35
  end
66
36
  end
67
37
 
data/lib/digest/crc16.rb CHANGED
@@ -8,6 +8,8 @@ module Digest
8
8
 
9
9
  WIDTH = 16
10
10
 
11
+ REFLECT_INPUT = true
12
+
11
13
  INIT_CRC = 0x0000
12
14
 
13
15
  # Generated by `./pycrc.py --algorithm=table-driven --model=crc-16`
@@ -46,38 +48,6 @@ module Digest
46
48
  0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
47
49
  ].freeze
48
50
 
49
- #
50
- # Packs the CRC16 checksum.
51
- #
52
- # @param [Integer] crc
53
- # The CRC16 checksum to pack.
54
- #
55
- # @return [String]
56
- # The packed CRC16 checksum.
57
- #
58
- def self.pack(crc)
59
- buffer = ''
60
-
61
- buffer << ((crc & 0xff00) >> 8).chr
62
- buffer << (crc & 0xff).chr
63
-
64
- buffer
65
- end
66
-
67
- #
68
- # Updates the CRC16 checksum.
69
- #
70
- # @param [String] data
71
- # The data to update the checksum with.
72
- #
73
- def update(data)
74
- data.each_byte do |b|
75
- @crc = ((@table[(@crc ^ b) & 0xff] ^ (@crc >> 8)) & 0xffff)
76
- end
77
-
78
- return self
79
- end
80
-
81
51
  end
82
52
  end
83
53
 
@@ -6,6 +6,8 @@ module Digest
6
6
  #
7
7
  class CRC16CCITT < CRC16
8
8
 
9
+ REFLECT_INPUT = false
10
+
9
11
  INIT_CRC = 0xffff
10
12
 
11
13
  # Generated by `./pycrc.py --algorithm=table-driven --model=crc-16-ccitt --generate=c`
@@ -44,20 +46,6 @@ module Digest
44
46
  0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
45
47
  ].freeze
46
48
 
47
- #
48
- # Updates the CRC16 CCITT checksum.
49
- #
50
- # @param [String] data
51
- # The data to update the checksum with.
52
- #
53
- def update(data)
54
- data.each_byte do |b|
55
- @crc = ((@table[((@crc >> 8) ^ b) & 0xff] ^ (@crc << 8)) & 0xffff)
56
- end
57
-
58
- return self
59
- end
60
-
61
49
  end
62
50
  end
63
51
 
@@ -6,6 +6,8 @@ module Digest
6
6
  #
7
7
  class CRC16DNP < CRC16
8
8
 
9
+ REFLECT_INPUT = true
10
+
9
11
  INIT_CRC = 0
10
12
 
11
13
  TABLE = [
@@ -43,20 +45,6 @@ module Digest
43
45
  0x91af, 0xa7f1, 0xfd13, 0xcb4d, 0x48d7, 0x7e89, 0x246b, 0x1235
44
46
  ].freeze
45
47
 
46
- #
47
- # Updates the CRC16 DNP checksum.
48
- #
49
- # @param [String] data
50
- # The data to update the checksum with.
51
- #
52
- def update(data)
53
- data.each_byte do |b|
54
- @crc = ((@crc >> 8) ^ @table[(@crc ^ b) & 0xff])
55
- end
56
-
57
- return self
58
- end
59
-
60
48
  def finish
61
49
  self.class.pack(~@crc)
62
50
  end
@@ -8,6 +8,8 @@ module Digest
8
8
  #
9
9
  class CRC16Genibus < CRC16
10
10
 
11
+ REFLECT_INPUT = false
12
+
11
13
  INIT_XOR = 0xffff
12
14
 
13
15
  INIT_CRC = 0x0000 ^ INIT_XOR
@@ -50,20 +52,6 @@ module Digest
50
52
  0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
51
53
  ].freeze
52
54
 
53
- #
54
- # Updates the CRC16 Genibus checksum.
55
- #
56
- # @param [String] data
57
- # The data to update the checksum with.
58
- #
59
- def update(data)
60
- data.each_byte do |b|
61
- @crc = (@table[((@crc >> 8) ^ b) & 0xff] ^ (@crc << 8)) & 0xffff
62
- end
63
-
64
- return self
65
- end
66
-
67
55
  end
68
56
  end
69
57
 
@@ -8,6 +8,8 @@ module Digest
8
8
  #
9
9
  class CRC16Kermit < CRC16
10
10
 
11
+ REFLECT_INPUT = true
12
+
11
13
  # Generated by `./pycrc.py --algorithm=table-driven --model=kermit --generate=c`
12
14
  TABLE = [
13
15
  0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
@@ -44,20 +46,6 @@ module Digest
44
46
  0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
45
47
  ].freeze
46
48
 
47
- #
48
- # Updates the CRC16 Kermit checksum.
49
- #
50
- # @param [String] data
51
- # The data to update the checksum with.
52
- #
53
- def update(data)
54
- data.each_byte do |b|
55
- @crc = (@table[(@crc ^ b) & 0xff] ^ (@crc >> 8)) & 0xffff
56
- end
57
-
58
- return self
59
- end
60
-
61
49
  end
62
50
  end
63
51
 
@@ -6,6 +6,8 @@ module Digest
6
6
  #
7
7
  class CRC16X25 < CRC16
8
8
 
9
+ REFLECT_INPUT = true
10
+
9
11
  INIT_XOR = 0xffff
10
12
 
11
13
  INIT_CRC = 0x0 ^ INIT_XOR
@@ -47,7 +49,7 @@ module Digest
47
49
  0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
48
50
  0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
49
51
  ].freeze
50
-
52
+
51
53
  end
52
54
  end
53
55