github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/klauspost/crc32/crc32.go (about)

     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  
     5  // Package crc32 implements the 32-bit cyclic redundancy check, or CRC-32,
     6  // checksum. See http://en.wikipedia.org/wiki/Cyclic_redundancy_check for
     7  // information.
     8  //
     9  // Polynomials are represented in LSB-first form also known as reversed representation.
    10  //
    11  // See http://en.wikipedia.org/wiki/Mathematics_of_cyclic_redundancy_checks#Reversed_representations_and_reciprocal_polynomials
    12  // for information.
    13  package crc32
    14  
    15  import (
    16  	"hash"
    17  	"sync"
    18  )
    19  
    20  // The size of a CRC-32 checksum in bytes.
    21  const Size = 4
    22  
    23  // Predefined polynomials.
    24  const (
    25  	// IEEE is by far and away the most common CRC-32 polynomial.
    26  	// Used by ethernet (IEEE 802.3), v.42, fddi, gzip, zip, png, ...
    27  	IEEE = 0xedb88320
    28  
    29  	// Castagnoli's polynomial, used in iSCSI.
    30  	// Has better error detection characteristics than IEEE.
    31  	// http://dx.doi.org/10.1109/26.231911
    32  	Castagnoli = 0x82f63b78
    33  
    34  	// Koopman's polynomial.
    35  	// Also has better error detection characteristics than IEEE.
    36  	// http://dx.doi.org/10.1109/DSN.2002.1028931
    37  	Koopman = 0xeb31d82e
    38  )
    39  
    40  // Table is a 256-word table representing the polynomial for efficient processing.
    41  type Table [256]uint32
    42  
    43  // castagnoliTable points to a lazily initialized Table for the Castagnoli
    44  // polynomial. MakeTable will always return this value when asked to make a
    45  // Castagnoli table so we can compare against it to find when the caller is
    46  // using this polynomial.
    47  var castagnoliTable *Table
    48  var castagnoliTable8 *slicing8Table
    49  var castagnoliOnce sync.Once
    50  
    51  func castagnoliInit() {
    52  	castagnoliTable = makeTable(Castagnoli)
    53  	castagnoliTable8 = makeTable8(Castagnoli)
    54  }
    55  
    56  // IEEETable is the table for the IEEE polynomial.
    57  var IEEETable = makeTable(IEEE)
    58  
    59  // slicing8Table is array of 8 Tables
    60  type slicing8Table [8]Table
    61  
    62  // ieeeTable8 is the slicing8Table for IEEE
    63  var ieeeTable8 *slicing8Table
    64  var ieeeTable8Once sync.Once
    65  
    66  // MakeTable returns a Table constructed from the specified polynomial.
    67  // The contents of this Table must not be modified.
    68  func MakeTable(poly uint32) *Table {
    69  	switch poly {
    70  	case IEEE:
    71  		return IEEETable
    72  	case Castagnoli:
    73  		castagnoliOnce.Do(castagnoliInit)
    74  		return castagnoliTable
    75  	}
    76  	return makeTable(poly)
    77  }
    78  
    79  // makeTable returns the Table constructed from the specified polynomial.
    80  func makeTable(poly uint32) *Table {
    81  	t := new(Table)
    82  	for i := 0; i < 256; i++ {
    83  		crc := uint32(i)
    84  		for j := 0; j < 8; j++ {
    85  			if crc&1 == 1 {
    86  				crc = (crc >> 1) ^ poly
    87  			} else {
    88  				crc >>= 1
    89  			}
    90  		}
    91  		t[i] = crc
    92  	}
    93  	return t
    94  }
    95  
    96  // makeTable8 returns slicing8Table constructed from the specified polynomial.
    97  func makeTable8(poly uint32) *slicing8Table {
    98  	t := new(slicing8Table)
    99  	t[0] = *makeTable(poly)
   100  	for i := 0; i < 256; i++ {
   101  		crc := t[0][i]
   102  		for j := 1; j < 8; j++ {
   103  			crc = t[0][crc&0xFF] ^ (crc >> 8)
   104  			t[j][i] = crc
   105  		}
   106  	}
   107  	return t
   108  }
   109  
   110  // digest represents the partial evaluation of a checksum.
   111  type digest struct {
   112  	crc uint32
   113  	tab *Table
   114  }
   115  
   116  // New creates a new hash.Hash32 computing the CRC-32 checksum
   117  // using the polynomial represented by the Table.
   118  // Its Sum method will lay the value out in big-endian byte order.
   119  func New(tab *Table) hash.Hash32 { return &digest{0, tab} }
   120  
   121  // NewIEEE creates a new hash.Hash32 computing the CRC-32 checksum
   122  // using the IEEE polynomial.
   123  // Its Sum method will lay the value out in big-endian byte order.
   124  func NewIEEE() hash.Hash32 { return New(IEEETable) }
   125  
   126  func (d *digest) Size() int { return Size }
   127  
   128  func (d *digest) BlockSize() int { return 1 }
   129  
   130  func (d *digest) Reset() { d.crc = 0 }
   131  
   132  func update(crc uint32, tab *Table, p []byte) uint32 {
   133  	crc = ^crc
   134  	for _, v := range p {
   135  		crc = tab[byte(crc)^v] ^ (crc >> 8)
   136  	}
   137  	return ^crc
   138  }
   139  
   140  // updateSlicingBy8 updates CRC using Slicing-by-8
   141  func updateSlicingBy8(crc uint32, tab *slicing8Table, p []byte) uint32 {
   142  	crc = ^crc
   143  	for len(p) > 8 {
   144  		crc ^= uint32(p[0]) | uint32(p[1])<<8 | uint32(p[2])<<16 | uint32(p[3])<<24
   145  		crc = tab[0][p[7]] ^ tab[1][p[6]] ^ tab[2][p[5]] ^ tab[3][p[4]] ^
   146  			tab[4][crc>>24] ^ tab[5][(crc>>16)&0xFF] ^
   147  			tab[6][(crc>>8)&0xFF] ^ tab[7][crc&0xFF]
   148  		p = p[8:]
   149  	}
   150  	crc = ^crc
   151  	if len(p) == 0 {
   152  		return crc
   153  	}
   154  	return update(crc, &tab[0], p)
   155  }
   156  
   157  // Update returns the result of adding the bytes in p to the crc.
   158  func Update(crc uint32, tab *Table, p []byte) uint32 {
   159  	if tab == castagnoliTable {
   160  		return updateCastagnoli(crc, p)
   161  	}
   162  	if tab == IEEETable {
   163  		return updateIEEE(crc, p)
   164  	}
   165  	return update(crc, tab, p)
   166  }
   167  
   168  func (d *digest) Write(p []byte) (n int, err error) {
   169  	d.crc = Update(d.crc, d.tab, p)
   170  	return len(p), nil
   171  }
   172  
   173  func (d *digest) Sum32() uint32 { return d.crc }
   174  
   175  func (d *digest) Sum(in []byte) []byte {
   176  	s := d.Sum32()
   177  	return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s))
   178  }
   179  
   180  // Checksum returns the CRC-32 checksum of data
   181  // using the polynomial represented by the Table.
   182  func Checksum(data []byte, tab *Table) uint32 { return Update(0, tab, data) }
   183  
   184  // ChecksumIEEE returns the CRC-32 checksum of data
   185  // using the IEEE polynomial.
   186  func ChecksumIEEE(data []byte) uint32 { return updateIEEE(0, data) }