crc32.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // castagnoliTable points to a lazily initialized Table for the Castagnoli
  36. // polynomial. MakeTable will always return this value when asked to make a
  37. // Castagnoli table so we can compare against it to find when the caller is
  38. // using this polynomial.
  39. var castagnoliTable *Table
  40. var castagnoliTable8 *slicing8Table
  41. var castagnoliOnce sync.Once
  42. func castagnoliInit() {
  43. castagnoliTable = makeTable(Castagnoli)
  44. castagnoliTable8 = makeTable8(Castagnoli)
  45. }
  46. // IEEETable is the table for the IEEE polynomial.
  47. var IEEETable = makeTable(IEEE)
  48. // slicing8Table is array of 8 Tables
  49. type slicing8Table [8]Table
  50. // iEEETable8 is the slicing8Table for IEEE
  51. var iEEETable8 *slicing8Table
  52. var iEEETable8Once sync.Once
  53. // MakeTable returns the Table constructed from the specified polynomial.
  54. func MakeTable(poly uint32) *Table {
  55. switch poly {
  56. case IEEE:
  57. return IEEETable
  58. case Castagnoli:
  59. castagnoliOnce.Do(castagnoliInit)
  60. return castagnoliTable
  61. }
  62. return makeTable(poly)
  63. }
  64. // makeTable returns the Table constructed from the specified polynomial.
  65. func makeTable(poly uint32) *Table {
  66. t := new(Table)
  67. for i := 0; i < 256; i++ {
  68. crc := uint32(i)
  69. for j := 0; j < 8; j++ {
  70. if crc&1 == 1 {
  71. crc = (crc >> 1) ^ poly
  72. } else {
  73. crc >>= 1
  74. }
  75. }
  76. t[i] = crc
  77. }
  78. return t
  79. }
  80. // makeTable8 returns slicing8Table constructed from the specified polynomial.
  81. func makeTable8(poly uint32) *slicing8Table {
  82. t := new(slicing8Table)
  83. t[0] = *makeTable(poly)
  84. for i := 0; i < 256; i++ {
  85. crc := t[0][i]
  86. for j := 1; j < 8; j++ {
  87. crc = t[0][crc&0xFF] ^ (crc >> 8)
  88. t[j][i] = crc
  89. }
  90. }
  91. return t
  92. }
  93. // digest represents the partial evaluation of a checksum.
  94. type digest struct {
  95. crc uint32
  96. tab *Table
  97. }
  98. // New creates a new hash.Hash32 computing the CRC-32 checksum
  99. // using the polynomial represented by the Table.
  100. func New(tab *Table) hash.Hash32 { return &digest{0, tab} }
  101. // NewIEEE creates a new hash.Hash32 computing the CRC-32 checksum
  102. // using the IEEE polynomial.
  103. func NewIEEE() hash.Hash32 { return New(IEEETable) }
  104. func (d *digest) Size() int { return Size }
  105. func (d *digest) BlockSize() int { return 1 }
  106. func (d *digest) Reset() { d.crc = 0 }
  107. func update(crc uint32, tab *Table, p []byte) uint32 {
  108. crc = ^crc
  109. for _, v := range p {
  110. crc = tab[byte(crc)^v] ^ (crc >> 8)
  111. }
  112. return ^crc
  113. }
  114. // updateSlicingBy8 updates CRC using Slicing-by-8
  115. func updateSlicingBy8(crc uint32, tab *slicing8Table, p []byte) uint32 {
  116. crc = ^crc
  117. for len(p) > 8 {
  118. crc ^= uint32(p[0]) | uint32(p[1])<<8 | uint32(p[2])<<16 | uint32(p[3])<<24
  119. crc = tab[0][p[7]] ^ tab[1][p[6]] ^ tab[2][p[5]] ^ tab[3][p[4]] ^
  120. tab[4][crc>>24] ^ tab[5][(crc>>16)&0xFF] ^
  121. tab[6][(crc>>8)&0xFF] ^ tab[7][crc&0xFF]
  122. p = p[8:]
  123. }
  124. crc = ^crc
  125. if len(p) == 0 {
  126. return crc
  127. }
  128. return update(crc, &tab[0], p)
  129. }
  130. // Update returns the result of adding the bytes in p to the crc.
  131. func Update(crc uint32, tab *Table, p []byte) uint32 {
  132. if tab == castagnoliTable {
  133. return updateCastagnoli(crc, p)
  134. } else if tab == IEEETable {
  135. return updateIEEE(crc, p)
  136. }
  137. return update(crc, tab, p)
  138. }
  139. func (d *digest) Write(p []byte) (n int, err error) {
  140. d.crc = Update(d.crc, d.tab, p)
  141. return len(p), nil
  142. }
  143. func (d *digest) Sum32() uint32 { return d.crc }
  144. func (d *digest) Sum(in []byte) []byte {
  145. s := d.Sum32()
  146. return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s))
  147. }
  148. // Checksum returns the CRC-32 checksum of data
  149. // using the polynomial represented by the Table.
  150. func Checksum(data []byte, tab *Table) uint32 { return Update(0, tab, data) }
  151. // ChecksumIEEE returns the CRC-32 checksum of data
  152. // using the IEEE polynomial.
  153. func ChecksumIEEE(data []byte) uint32 { return updateIEEE(0, data) }