You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
6.2 KiB

  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package crc32 implements the 32-bit cyclic redundancy check, or CRC-32,
  5. // checksum. See http://en.wikipedia.org/wiki/Cyclic_redundancy_check for
  6. // information.
  7. //
  8. // Polynomials are represented in LSB-first form also known as reversed representation.
  9. //
  10. // See http://en.wikipedia.org/wiki/Mathematics_of_cyclic_redundancy_checks#Reversed_representations_and_reciprocal_polynomials
  11. // for information.
  12. package crc32
  13. import (
  14. "hash"
  15. "sync"
  16. )
  17. // The size of a CRC-32 checksum in bytes.
  18. const Size = 4
  19. // Predefined polynomials.
  20. const (
  21. // IEEE is by far and away the most common CRC-32 polynomial.
  22. // Used by ethernet (IEEE 802.3), v.42, fddi, gzip, zip, png, ...
  23. IEEE = 0xedb88320
  24. // Castagnoli's polynomial, used in iSCSI.
  25. // Has better error detection characteristics than IEEE.
  26. // http://dx.doi.org/10.1109/26.231911
  27. Castagnoli = 0x82f63b78
  28. // Koopman's polynomial.
  29. // Also has better error detection characteristics than IEEE.
  30. // http://dx.doi.org/10.1109/DSN.2002.1028931
  31. Koopman = 0xeb31d82e
  32. )
  33. // Table is a 256-word table representing the polynomial for efficient processing.
  34. type Table [256]uint32
  35. // This file makes use of functions implemented in architecture-specific files.
  36. // The interface that they implement is as follows:
  37. //
  38. // // archAvailableIEEE reports whether an architecture-specific CRC32-IEEE
  39. // // algorithm is available.
  40. // archAvailableIEEE() bool
  41. //
  42. // // archInitIEEE initializes the architecture-specific CRC3-IEEE algorithm.
  43. // // It can only be called if archAvailableIEEE() returns true.
  44. // archInitIEEE()
  45. //
  46. // // archUpdateIEEE updates the given CRC32-IEEE. It can only be called if
  47. // // archInitIEEE() was previously called.
  48. // archUpdateIEEE(crc uint32, p []byte) uint32
  49. //
  50. // // archAvailableCastagnoli reports whether an architecture-specific
  51. // // CRC32-C algorithm is available.
  52. // archAvailableCastagnoli() bool
  53. //
  54. // // archInitCastagnoli initializes the architecture-specific CRC32-C
  55. // // algorithm. It can only be called if archAvailableCastagnoli() returns
  56. // // true.
  57. // archInitCastagnoli()
  58. //
  59. // // archUpdateCastagnoli updates the given CRC32-C. It can only be called
  60. // // if archInitCastagnoli() was previously called.
  61. // archUpdateCastagnoli(crc uint32, p []byte) uint32
  62. // castagnoliTable points to a lazily initialized Table for the Castagnoli
  63. // polynomial. MakeTable will always return this value when asked to make a
  64. // Castagnoli table so we can compare against it to find when the caller is
  65. // using this polynomial.
  66. var castagnoliTable *Table
  67. var castagnoliTable8 *slicing8Table
  68. var castagnoliArchImpl bool
  69. var updateCastagnoli func(crc uint32, p []byte) uint32
  70. var castagnoliOnce sync.Once
  71. func castagnoliInit() {
  72. castagnoliTable = simpleMakeTable(Castagnoli)
  73. castagnoliArchImpl = archAvailableCastagnoli()
  74. if castagnoliArchImpl {
  75. archInitCastagnoli()
  76. updateCastagnoli = archUpdateCastagnoli
  77. } else {
  78. // Initialize the slicing-by-8 table.
  79. castagnoliTable8 = slicingMakeTable(Castagnoli)
  80. updateCastagnoli = func(crc uint32, p []byte) uint32 {
  81. return slicingUpdate(crc, castagnoliTable8, p)
  82. }
  83. }
  84. }
  85. // IEEETable is the table for the IEEE polynomial.
  86. var IEEETable = simpleMakeTable(IEEE)
  87. // ieeeTable8 is the slicing8Table for IEEE
  88. var ieeeTable8 *slicing8Table
  89. var ieeeArchImpl bool
  90. var updateIEEE func(crc uint32, p []byte) uint32
  91. var ieeeOnce sync.Once
  92. func ieeeInit() {
  93. ieeeArchImpl = archAvailableIEEE()
  94. if ieeeArchImpl {
  95. archInitIEEE()
  96. updateIEEE = archUpdateIEEE
  97. } else {
  98. // Initialize the slicing-by-8 table.
  99. ieeeTable8 = slicingMakeTable(IEEE)
  100. updateIEEE = func(crc uint32, p []byte) uint32 {
  101. return slicingUpdate(crc, ieeeTable8, p)
  102. }
  103. }
  104. }
  105. // MakeTable returns a Table constructed from the specified polynomial.
  106. // The contents of this Table must not be modified.
  107. func MakeTable(poly uint32) *Table {
  108. switch poly {
  109. case IEEE:
  110. ieeeOnce.Do(ieeeInit)
  111. return IEEETable
  112. case Castagnoli:
  113. castagnoliOnce.Do(castagnoliInit)
  114. return castagnoliTable
  115. }
  116. return simpleMakeTable(poly)
  117. }
  118. // digest represents the partial evaluation of a checksum.
  119. type digest struct {
  120. crc uint32
  121. tab *Table
  122. }
  123. // New creates a new hash.Hash32 computing the CRC-32 checksum
  124. // using the polynomial represented by the Table.
  125. // Its Sum method will lay the value out in big-endian byte order.
  126. func New(tab *Table) hash.Hash32 {
  127. if tab == IEEETable {
  128. ieeeOnce.Do(ieeeInit)
  129. }
  130. return &digest{0, tab}
  131. }
  132. // NewIEEE creates a new hash.Hash32 computing the CRC-32 checksum
  133. // using the IEEE polynomial.
  134. // Its Sum method will lay the value out in big-endian byte order.
  135. func NewIEEE() hash.Hash32 { return New(IEEETable) }
  136. func (d *digest) Size() int { return Size }
  137. func (d *digest) BlockSize() int { return 1 }
  138. func (d *digest) Reset() { d.crc = 0 }
  139. // Update returns the result of adding the bytes in p to the crc.
  140. func Update(crc uint32, tab *Table, p []byte) uint32 {
  141. switch tab {
  142. case castagnoliTable:
  143. return updateCastagnoli(crc, p)
  144. case IEEETable:
  145. // Unfortunately, because IEEETable is exported, IEEE may be used without a
  146. // call to MakeTable. We have to make sure it gets initialized in that case.
  147. ieeeOnce.Do(ieeeInit)
  148. return updateIEEE(crc, p)
  149. default:
  150. return simpleUpdate(crc, tab, p)
  151. }
  152. }
  153. func (d *digest) Write(p []byte) (n int, err error) {
  154. switch d.tab {
  155. case castagnoliTable:
  156. d.crc = updateCastagnoli(d.crc, p)
  157. case IEEETable:
  158. // We only create digest objects through New() which takes care of
  159. // initialization in this case.
  160. d.crc = updateIEEE(d.crc, p)
  161. default:
  162. d.crc = simpleUpdate(d.crc, d.tab, p)
  163. }
  164. return len(p), nil
  165. }
  166. func (d *digest) Sum32() uint32 { return d.crc }
  167. func (d *digest) Sum(in []byte) []byte {
  168. s := d.Sum32()
  169. return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s))
  170. }
  171. // Checksum returns the CRC-32 checksum of data
  172. // using the polynomial represented by the Table.
  173. func Checksum(data []byte, tab *Table) uint32 { return Update(0, tab, data) }
  174. // ChecksumIEEE returns the CRC-32 checksum of data
  175. // using the IEEE polynomial.
  176. func ChecksumIEEE(data []byte) uint32 {
  177. ieeeOnce.Do(ieeeInit)
  178. return updateIEEE(0, data)
  179. }