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.
90 lines
2.1 KiB
90 lines
2.1 KiB
10 years ago
|
// 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.
|
||
10 years ago
|
// Golang project:
|
||
|
// https://github.com/golang/go/blob/master/LICENSE
|
||
|
|
||
|
// Using this part of Minio codebase under the license
|
||
|
// Apache License Version 2.0 with modifications
|
||
10 years ago
|
|
||
10 years ago
|
// Package crc32 implements the 32-bit cyclic redundancy check, or CRC-32,
|
||
|
// checksum. See http://en.wikipedia.org/wiki/Cyclic_redundancy_check for
|
||
|
// information.
|
||
10 years ago
|
|
||
10 years ago
|
package crc32c
|
||
10 years ago
|
|
||
10 years ago
|
import (
|
||
10 years ago
|
"hash"
|
||
|
"io"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
// The size of a CRC-32 checksum in bytes.
|
||
|
const Size = 4
|
||
|
|
||
|
// digest represents the partial evaluation of a checksum.
|
||
|
type digest struct {
|
||
|
crc uint32
|
||
|
}
|
||
|
|
||
|
// New creates a new hash.Hash32 computing the CRC-32 checksum
|
||
|
// using the polynomial represented by the Table.
|
||
|
func New() hash.Hash32 {
|
||
|
return &digest{crc: 0}
|
||
|
}
|
||
|
|
||
10 years ago
|
// Return size of crc
|
||
10 years ago
|
func (d *digest) Size() int { return Size }
|
||
|
|
||
10 years ago
|
// Stub
|
||
10 years ago
|
func (d *digest) BlockSize() int { return 1 }
|
||
|
|
||
10 years ago
|
// Get crc in bytes
|
||
10 years ago
|
func (d *digest) Sum(in []byte) []byte {
|
||
|
s := d.crc
|
||
|
return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s))
|
||
|
}
|
||
|
|
||
10 years ago
|
// Sum32 - return current crc in digest object
|
||
10 years ago
|
func (d *digest) Sum32() uint32 { return d.crc }
|
||
10 years ago
|
|
||
10 years ago
|
// Reset default crc
|
||
10 years ago
|
func (d *digest) Reset() { d.crc = 0 }
|
||
10 years ago
|
|
||
10 years ago
|
// Update returns the result of adding the bytes in p to the crc.
|
||
|
func (d *digest) update(crc uint32, p []byte) uint32 {
|
||
|
return updateCastanagoliPCL(crc, p)
|
||
|
}
|
||
|
|
||
10 years ago
|
// Write data
|
||
10 years ago
|
func (d *digest) Write(p []byte) (n int, err error) {
|
||
|
d.crc = d.update(d.crc, p)
|
||
|
return len(p), nil
|
||
|
}
|
||
|
|
||
10 years ago
|
/// Convenience functions
|
||
10 years ago
|
|
||
10 years ago
|
// Sum32 - single caller crc helper
|
||
10 years ago
|
func Sum32(data []byte) uint32 {
|
||
|
crc32 := New()
|
||
|
crc32.Reset()
|
||
|
crc32.Write(data)
|
||
|
return crc32.Sum32()
|
||
|
}
|
||
|
|
||
10 years ago
|
// Sum - low memory footprint io.Reader based crc helper
|
||
10 years ago
|
func Sum(reader io.Reader) (uint32, error) {
|
||
|
h := New()
|
||
|
var err error
|
||
|
for err == nil {
|
||
|
length := 0
|
||
|
byteBuffer := make([]byte, 1024*1024)
|
||
|
length, err = reader.Read(byteBuffer)
|
||
|
byteBuffer = byteBuffer[0:length]
|
||
|
h.Write(byteBuffer)
|
||
|
}
|
||
|
if err != io.EOF {
|
||
|
return 0, err
|
||
|
}
|
||
|
return h.Sum32(), nil
|
||
10 years ago
|
}
|