2008-11-24 13:30:40 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2011-04-19 17:57:05 -06:00
|
|
|
// Package crc32 implements the 32-bit cyclic redundancy check, or CRC-32,
|
|
|
|
// checksum. See http://en.wikipedia.org/wiki/Cyclic_redundancy_check for
|
|
|
|
// information.
|
2008-11-24 13:30:40 -07:00
|
|
|
package crc32
|
|
|
|
|
2009-05-20 19:16:38 -06:00
|
|
|
import (
|
2009-12-15 16:35:38 -07:00
|
|
|
"hash"
|
2011-07-12 07:29:24 -06:00
|
|
|
"sync"
|
2009-05-20 19:16:38 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// The size of a CRC-32 checksum in bytes.
|
2009-10-06 20:41:51 -06:00
|
|
|
const Size = 4
|
2008-11-24 13:30:40 -07:00
|
|
|
|
2009-03-07 17:56:21 -07:00
|
|
|
// Predefined polynomials.
|
2009-01-20 15:40:40 -07:00
|
|
|
const (
|
2014-11-12 00:48:00 -07:00
|
|
|
// IEEE is by far and away the most common CRC-32 polynomial.
|
|
|
|
// Used by ethernet (IEEE 802.3), v.42, fddi, gzip, zip, png, ...
|
2009-12-15 16:35:38 -07:00
|
|
|
IEEE = 0xedb88320
|
2008-11-24 13:30:40 -07:00
|
|
|
|
|
|
|
// Castagnoli's polynomial, used in iSCSI.
|
|
|
|
// Has better error detection characteristics than IEEE.
|
|
|
|
// http://dx.doi.org/10.1109/26.231911
|
2009-12-15 16:35:38 -07:00
|
|
|
Castagnoli = 0x82f63b78
|
2008-11-24 13:30:40 -07:00
|
|
|
|
|
|
|
// Koopman's polynomial.
|
|
|
|
// Also has better error detection characteristics than IEEE.
|
|
|
|
// http://dx.doi.org/10.1109/DSN.2002.1028931
|
2009-12-15 16:35:38 -07:00
|
|
|
Koopman = 0xeb31d82e
|
2008-11-24 13:30:40 -07:00
|
|
|
)
|
|
|
|
|
2009-03-07 17:56:21 -07:00
|
|
|
// Table is a 256-word table representing the polynomial for efficient processing.
|
2009-05-20 19:16:38 -06:00
|
|
|
type Table [256]uint32
|
2008-11-24 13:30:40 -07:00
|
|
|
|
2011-07-12 07:29:24 -06:00
|
|
|
// castagnoliTable points to a lazily initialized Table for the Castagnoli
|
|
|
|
// polynomial. MakeTable will always return this value when asked to make a
|
|
|
|
// Castagnoli table so we can compare against it to find when the caller is
|
|
|
|
// using this polynomial.
|
|
|
|
var castagnoliTable *Table
|
|
|
|
var castagnoliOnce sync.Once
|
|
|
|
|
|
|
|
func castagnoliInit() {
|
|
|
|
castagnoliTable = makeTable(Castagnoli)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IEEETable is the table for the IEEE polynomial.
|
|
|
|
var IEEETable = makeTable(IEEE)
|
|
|
|
|
2009-03-07 17:56:21 -07:00
|
|
|
// MakeTable returns the Table constructed from the specified polynomial.
|
2009-05-20 19:16:38 -06:00
|
|
|
func MakeTable(poly uint32) *Table {
|
2011-07-12 07:29:24 -06:00
|
|
|
switch poly {
|
|
|
|
case IEEE:
|
|
|
|
return IEEETable
|
|
|
|
case Castagnoli:
|
|
|
|
castagnoliOnce.Do(castagnoliInit)
|
|
|
|
return castagnoliTable
|
|
|
|
}
|
|
|
|
return makeTable(poly)
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeTable returns the Table constructed from the specified polynomial.
|
|
|
|
func makeTable(poly uint32) *Table {
|
2009-12-15 16:35:38 -07:00
|
|
|
t := new(Table)
|
2008-11-24 13:30:40 -07:00
|
|
|
for i := 0; i < 256; i++ {
|
2009-12-15 16:35:38 -07:00
|
|
|
crc := uint32(i)
|
2008-11-24 13:30:40 -07:00
|
|
|
for j := 0; j < 8; j++ {
|
|
|
|
if crc&1 == 1 {
|
2009-11-09 22:23:52 -07:00
|
|
|
crc = (crc >> 1) ^ poly
|
2008-11-24 13:30:40 -07:00
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
crc >>= 1
|
2008-11-24 13:30:40 -07:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
t[i] = crc
|
2008-11-24 13:30:40 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return t
|
2008-11-24 13:30:40 -07:00
|
|
|
}
|
|
|
|
|
2009-05-20 19:16:38 -06:00
|
|
|
// digest represents the partial evaluation of a checksum.
|
|
|
|
type digest struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
crc uint32
|
|
|
|
tab *Table
|
2009-05-20 19:16:38 -06:00
|
|
|
}
|
|
|
|
|
2009-11-11 13:00:15 -07:00
|
|
|
// New creates a new hash.Hash32 computing the CRC-32 checksum
|
2009-05-20 19:16:38 -06:00
|
|
|
// using the polynomial represented by the Table.
|
2009-12-15 16:35:38 -07:00
|
|
|
func New(tab *Table) hash.Hash32 { return &digest{0, tab} }
|
2009-05-20 19:16:38 -06:00
|
|
|
|
2009-11-11 13:00:15 -07:00
|
|
|
// NewIEEE creates a new hash.Hash32 computing the CRC-32 checksum
|
2009-05-20 19:16:38 -06:00
|
|
|
// using the IEEE polynomial.
|
2009-12-15 16:35:38 -07:00
|
|
|
func NewIEEE() hash.Hash32 { return New(IEEETable) }
|
2008-11-24 13:30:40 -07:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (d *digest) Size() int { return Size }
|
2008-11-24 13:30:40 -07:00
|
|
|
|
2012-01-18 08:36:28 -07:00
|
|
|
func (d *digest) BlockSize() int { return 1 }
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (d *digest) Reset() { d.crc = 0 }
|
2008-11-24 13:30:40 -07:00
|
|
|
|
2009-05-20 19:16:38 -06:00
|
|
|
func update(crc uint32, tab *Table, p []byte) uint32 {
|
2009-12-15 16:35:38 -07:00
|
|
|
crc = ^crc
|
2010-03-12 18:38:18 -07:00
|
|
|
for _, v := range p {
|
|
|
|
crc = tab[byte(crc)^v] ^ (crc >> 8)
|
2008-11-24 13:30:40 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return ^crc
|
2009-05-20 19:16:38 -06:00
|
|
|
}
|
|
|
|
|
2010-03-12 18:38:18 -07:00
|
|
|
// Update returns the result of adding the bytes in p to the crc.
|
|
|
|
func Update(crc uint32, tab *Table, p []byte) uint32 {
|
2011-07-12 07:29:24 -06:00
|
|
|
if tab == castagnoliTable {
|
|
|
|
return updateCastagnoli(crc, p)
|
|
|
|
}
|
2010-03-12 18:38:18 -07:00
|
|
|
return update(crc, tab, p)
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:04:37 -06:00
|
|
|
func (d *digest) Write(p []byte) (n int, err error) {
|
2011-07-12 07:29:24 -06:00
|
|
|
d.crc = Update(d.crc, d.tab, p)
|
2009-12-15 16:35:38 -07:00
|
|
|
return len(p), nil
|
2008-11-24 13:30:40 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (d *digest) Sum32() uint32 { return d.crc }
|
2008-11-24 13:30:40 -07:00
|
|
|
|
2011-12-01 10:35:37 -07:00
|
|
|
func (d *digest) Sum(in []byte) []byte {
|
2009-12-15 16:35:38 -07:00
|
|
|
s := d.Sum32()
|
2012-04-10 13:15:39 -06:00
|
|
|
return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s))
|
2008-11-24 13:30:40 -07:00
|
|
|
}
|
|
|
|
|
2009-05-20 19:16:38 -06:00
|
|
|
// Checksum returns the CRC-32 checksum of data
|
|
|
|
// using the polynomial represented by the Table.
|
2011-07-12 07:29:24 -06:00
|
|
|
func Checksum(data []byte, tab *Table) uint32 { return Update(0, tab, data) }
|
2008-11-24 13:30:40 -07:00
|
|
|
|
2009-05-20 19:16:38 -06:00
|
|
|
// ChecksumIEEE returns the CRC-32 checksum of data
|
|
|
|
// using the IEEE polynomial.
|
2009-12-15 16:35:38 -07:00
|
|
|
func ChecksumIEEE(data []byte) uint32 { return update(0, IEEETable, data) }
|