2014-12-02 06:17:39 -05:00
|
|
|
/*
|
2015-03-19 17:35:50 -04:00
|
|
|
* Minimalist Object Storage, (C) 2014 Minio, Inc.
|
2014-12-02 06:17:39 -05:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-12-08 06:20:35 -05:00
|
|
|
package crc32c
|
2014-12-02 06:17:39 -05:00
|
|
|
|
2014-12-08 06:20:35 -05:00
|
|
|
import (
|
2015-02-18 18:33:19 -05:00
|
|
|
"hash/crc32"
|
2015-03-09 19:15:06 -04:00
|
|
|
"io"
|
2014-12-08 06:20:35 -05:00
|
|
|
)
|
|
|
|
|
2015-01-23 22:33:37 -05:00
|
|
|
var castanagoliTable = crc32.MakeTable(crc32.Castagnoli)
|
2014-12-08 06:20:35 -05:00
|
|
|
|
2015-03-03 05:36:12 -05:00
|
|
|
/// Convenience functions
|
|
|
|
|
2015-03-06 04:50:51 -05:00
|
|
|
// Sum32 - single caller crc helper
|
2015-02-27 15:49:49 -05:00
|
|
|
func Sum32(buffer []byte) uint32 {
|
2015-02-18 18:33:19 -05:00
|
|
|
crc := crc32.New(castanagoliTable)
|
2015-02-27 15:49:49 -05:00
|
|
|
crc.Reset()
|
2015-02-18 18:33:19 -05:00
|
|
|
crc.Write(buffer)
|
2015-02-27 15:49:49 -05:00
|
|
|
return crc.Sum32()
|
|
|
|
}
|
|
|
|
|
2015-03-06 04:50:51 -05:00
|
|
|
// Sum - io.Reader based crc helper
|
2015-02-27 15:49:49 -05:00
|
|
|
func Sum(reader io.Reader) (uint32, error) {
|
2015-03-06 05:35:54 -05:00
|
|
|
h := crc32.New(castanagoliTable)
|
2015-02-27 15:49:49 -05:00
|
|
|
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 {
|
2015-03-06 05:35:54 -05:00
|
|
|
return 0, err
|
2015-02-27 15:49:49 -05:00
|
|
|
}
|
|
|
|
return h.Sum32(), nil
|
2014-12-08 06:20:35 -05:00
|
|
|
}
|