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.
52 lines
1.2 KiB
52 lines
1.2 KiB
10 years ago
|
/*
|
||
10 years ago
|
* Minimalist Object Storage, (C) 2014 Minio, Inc.
|
||
10 years ago
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
10 years ago
|
package crc32c
|
||
10 years ago
|
|
||
10 years ago
|
import (
|
||
10 years ago
|
"hash/crc32"
|
||
10 years ago
|
"io"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
var castanagoliTable = crc32.MakeTable(crc32.Castagnoli)
|
||
10 years ago
|
|
||
10 years ago
|
/// Convenience functions
|
||
|
|
||
10 years ago
|
// Sum32 - single caller crc helper
|
||
10 years ago
|
func Sum32(buffer []byte) uint32 {
|
||
10 years ago
|
crc := crc32.New(castanagoliTable)
|
||
10 years ago
|
crc.Reset()
|
||
10 years ago
|
crc.Write(buffer)
|
||
10 years ago
|
return crc.Sum32()
|
||
|
}
|
||
|
|
||
10 years ago
|
// Sum - io.Reader based crc helper
|
||
10 years ago
|
func Sum(reader io.Reader) (uint32, error) {
|
||
10 years ago
|
h := crc32.New(castanagoliTable)
|
||
10 years ago
|
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 {
|
||
10 years ago
|
return 0, err
|
||
10 years ago
|
}
|
||
|
return h.Sum32(), nil
|
||
10 years ago
|
}
|