2012-03-27 23:13:14 +00: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.
|
|
|
|
|
|
|
|
// Package adler32 implements the Adler-32 checksum.
|
2014-09-21 17:33:12 +00:00
|
|
|
//
|
|
|
|
// It is defined in RFC 1950:
|
2012-03-27 23:13:14 +00:00
|
|
|
// Adler-32 is composed of two sums accumulated per byte: s1 is
|
|
|
|
// the sum of all bytes, s2 is the sum of all s1 values. Both sums
|
|
|
|
// are done modulo 65521. s1 is initialized to 1, s2 to zero. The
|
|
|
|
// Adler-32 checksum is stored as s2*65536 + s1 in most-
|
|
|
|
// significant-byte first (network) order.
|
|
|
|
package adler32
|
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"hash"
|
|
|
|
)
|
2012-03-27 23:13:14 +00:00
|
|
|
|
|
|
|
const (
|
2014-09-21 17:33:12 +00:00
|
|
|
// mod is the largest prime that is less than 65536.
|
2012-03-27 23:13:14 +00:00
|
|
|
mod = 65521
|
2014-09-21 17:33:12 +00:00
|
|
|
// nmax is the largest n such that
|
|
|
|
// 255 * n * (n+1) / 2 + (n+1) * (mod-1) <= 2^32-1.
|
|
|
|
// It is mentioned in RFC 1950 (search for "5552").
|
|
|
|
nmax = 5552
|
2012-03-27 23:13:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// The size of an Adler-32 checksum in bytes.
|
|
|
|
const Size = 4
|
|
|
|
|
|
|
|
// digest represents the partial evaluation of a checksum.
|
2014-09-21 17:33:12 +00:00
|
|
|
// The low 16 bits are s1, the high 16 bits are s2.
|
|
|
|
type digest uint32
|
2012-03-27 23:13:14 +00:00
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
func (d *digest) Reset() { *d = 1 }
|
2012-03-27 23:13:14 +00:00
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
// New returns a new hash.Hash32 computing the Adler-32 checksum. Its
|
|
|
|
// Sum method will lay the value out in big-endian byte order. The
|
|
|
|
// returned Hash32 also implements encoding.BinaryMarshaler and
|
|
|
|
// encoding.BinaryUnmarshaler to marshal and unmarshal the internal
|
|
|
|
// state of the hash.
|
2012-03-27 23:13:14 +00:00
|
|
|
func New() hash.Hash32 {
|
|
|
|
d := new(digest)
|
|
|
|
d.Reset()
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *digest) Size() int { return Size }
|
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
func (d *digest) BlockSize() int { return 4 }
|
2012-03-27 23:13:14 +00:00
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
const (
|
|
|
|
magic = "adl\x01"
|
|
|
|
marshaledSize = len(magic) + 4
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d *digest) MarshalBinary() ([]byte, error) {
|
|
|
|
b := make([]byte, 0, marshaledSize)
|
|
|
|
b = append(b, magic...)
|
|
|
|
b = appendUint32(b, uint32(*d))
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *digest) UnmarshalBinary(b []byte) error {
|
|
|
|
if len(b) < len(magic) || string(b[:len(magic)]) != magic {
|
|
|
|
return errors.New("hash/adler32: invalid hash state identifier")
|
|
|
|
}
|
|
|
|
if len(b) != marshaledSize {
|
|
|
|
return errors.New("hash/adler32: invalid hash state size")
|
|
|
|
}
|
|
|
|
*d = digest(readUint32(b[len(magic):]))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendUint32(b []byte, x uint32) []byte {
|
|
|
|
a := [4]byte{
|
|
|
|
byte(x >> 24),
|
|
|
|
byte(x >> 16),
|
|
|
|
byte(x >> 8),
|
|
|
|
byte(x),
|
|
|
|
}
|
|
|
|
return append(b, a[:]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readUint32(b []byte) uint32 {
|
|
|
|
_ = b[3]
|
|
|
|
return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// Add p to the running checksum d.
|
|
|
|
func update(d digest, p []byte) digest {
|
|
|
|
s1, s2 := uint32(d&0xffff), uint32(d>>16)
|
|
|
|
for len(p) > 0 {
|
|
|
|
var q []byte
|
|
|
|
if len(p) > nmax {
|
|
|
|
p, q = p[:nmax], p[nmax:]
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2017-10-07 00:16:47 +00:00
|
|
|
for len(p) >= 4 {
|
|
|
|
s1 += uint32(p[0])
|
|
|
|
s2 += s1
|
|
|
|
s1 += uint32(p[1])
|
|
|
|
s2 += s1
|
|
|
|
s1 += uint32(p[2])
|
|
|
|
s2 += s1
|
|
|
|
s1 += uint32(p[3])
|
|
|
|
s2 += s1
|
|
|
|
p = p[4:]
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
for _, x := range p {
|
|
|
|
s1 += uint32(x)
|
|
|
|
s2 += s1
|
|
|
|
}
|
|
|
|
s1 %= mod
|
|
|
|
s2 %= mod
|
|
|
|
p = q
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
return digest(s2<<16 | s1)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *digest) Write(p []byte) (nn int, err error) {
|
2014-09-21 17:33:12 +00:00
|
|
|
*d = update(*d, p)
|
2012-03-27 23:13:14 +00:00
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
func (d *digest) Sum32() uint32 { return uint32(*d) }
|
2012-03-27 23:13:14 +00:00
|
|
|
|
|
|
|
func (d *digest) Sum(in []byte) []byte {
|
2014-09-21 17:33:12 +00:00
|
|
|
s := uint32(*d)
|
|
|
|
return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s))
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checksum returns the Adler-32 checksum of data.
|
2014-09-21 17:33:12 +00:00
|
|
|
func Checksum(data []byte) uint32 { return uint32(update(1, data)) }
|