The Go Programming Language

Source file src/pkg/hash/crc32/crc32.go

     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	package crc32
     9	
    10	import (
    11		"hash"
    12		"os"
    13		"sync"
    14	)
    15	
    16	// The size of a CRC-32 checksum in bytes.
    17	const Size = 4
    18	
    19	// Predefined polynomials.
    20	const (
    21		// Far and away the most common CRC-32 polynomial.
    22		// Used by ethernet (IEEE 802.3), v.42, fddi, gzip, zip, png, mpeg-2, ...
    23		IEEE = 0xedb88320
    24	
    25		// Castagnoli's polynomial, used in iSCSI.
    26		// Has better error detection characteristics than IEEE.
    27		// http://dx.doi.org/10.1109/26.231911
    28		Castagnoli = 0x82f63b78
    29	
    30		// Koopman's polynomial.
    31		// Also has better error detection characteristics than IEEE.
    32		// http://dx.doi.org/10.1109/DSN.2002.1028931
    33		Koopman = 0xeb31d82e
    34	)
    35	
    36	// Table is a 256-word table representing the polynomial for efficient processing.
    37	type Table [256]uint32
    38	
    39	// castagnoliTable points to a lazily initialized Table for the Castagnoli
    40	// polynomial. MakeTable will always return this value when asked to make a
    41	// Castagnoli table so we can compare against it to find when the caller is
    42	// using this polynomial.
    43	var castagnoliTable *Table
    44	var castagnoliOnce sync.Once
    45	
    46	func castagnoliInit() {
    47		castagnoliTable = makeTable(Castagnoli)
    48	}
    49	
    50	// IEEETable is the table for the IEEE polynomial.
    51	var IEEETable = makeTable(IEEE)
    52	
    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	
    65	// makeTable returns the Table constructed from the specified polynomial.
    66	func makeTable(poly uint32) *Table {
    67		t := new(Table)
    68		for i := 0; i < 256; i++ {
    69			crc := uint32(i)
    70			for j := 0; j < 8; j++ {
    71				if crc&1 == 1 {
    72					crc = (crc >> 1) ^ poly
    73				} else {
    74					crc >>= 1
    75				}
    76			}
    77			t[i] = crc
    78		}
    79		return t
    80	}
    81	
    82	// digest represents the partial evaluation of a checksum.
    83	type digest struct {
    84		crc uint32
    85		tab *Table
    86	}
    87	
    88	// New creates a new hash.Hash32 computing the CRC-32 checksum
    89	// using the polynomial represented by the Table.
    90	func New(tab *Table) hash.Hash32 { return &digest{0, tab} }
    91	
    92	// NewIEEE creates a new hash.Hash32 computing the CRC-32 checksum
    93	// using the IEEE polynomial.
    94	func NewIEEE() hash.Hash32 { return New(IEEETable) }
    95	
    96	func (d *digest) Size() int { return Size }
    97	
    98	func (d *digest) Reset() { d.crc = 0 }
    99	
   100	func update(crc uint32, tab *Table, p []byte) uint32 {
   101		crc = ^crc
   102		for _, v := range p {
   103			crc = tab[byte(crc)^v] ^ (crc >> 8)
   104		}
   105		return ^crc
   106	}
   107	
   108	// Update returns the result of adding the bytes in p to the crc.
   109	func Update(crc uint32, tab *Table, p []byte) uint32 {
   110		if tab == castagnoliTable {
   111			return updateCastagnoli(crc, p)
   112		}
   113		return update(crc, tab, p)
   114	}
   115	
   116	func (d *digest) Write(p []byte) (n int, err os.Error) {
   117		d.crc = Update(d.crc, d.tab, p)
   118		return len(p), nil
   119	}
   120	
   121	func (d *digest) Sum32() uint32 { return d.crc }
   122	
   123	func (d *digest) Sum() []byte {
   124		p := make([]byte, 4)
   125		s := d.Sum32()
   126		p[0] = byte(s >> 24)
   127		p[1] = byte(s >> 16)
   128		p[2] = byte(s >> 8)
   129		p[3] = byte(s)
   130		return p
   131	}
   132	
   133	// Checksum returns the CRC-32 checksum of data
   134	// using the polynomial represented by the Table.
   135	func Checksum(data []byte, tab *Table) uint32 { return Update(0, tab, data) }
   136	
   137	// ChecksumIEEE returns the CRC-32 checksum of data
   138	// using the IEEE polynomial.
   139	func ChecksumIEEE(data []byte) uint32 { return update(0, IEEETable, data) }

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.