| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| package adler32 |
|
|
| import ( |
| "errors" |
| "hash" |
| "internal/byteorder" |
| ) |
|
|
| const ( |
| |
| mod = 65521 |
| |
| |
| |
| nmax = 5552 |
| ) |
|
|
| |
| const Size = 4 |
|
|
| |
| |
| type digest uint32 |
|
|
| func (d *digest) Reset() { *d = 1 } |
|
|
| |
| |
| |
| |
| |
| func New() hash.Hash32 { |
| d := new(digest) |
| d.Reset() |
| return d |
| } |
|
|
| func (d *digest) Size() int { return Size } |
|
|
| func (d *digest) BlockSize() int { return 4 } |
|
|
| const ( |
| magic = "adl\x01" |
| marshaledSize = len(magic) + 4 |
| ) |
|
|
| func (d *digest) AppendBinary(b []byte) ([]byte, error) { |
| b = append(b, magic...) |
| b = byteorder.BEAppendUint32(b, uint32(*d)) |
| return b, nil |
| } |
|
|
| func (d *digest) MarshalBinary() ([]byte, error) { |
| return d.AppendBinary(make([]byte, 0, marshaledSize)) |
| } |
|
|
| 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(byteorder.BEUint32(b[len(magic):])) |
| return nil |
| } |
|
|
| func (d *digest) Clone() (hash.Cloner, error) { |
| r := *d |
| return &r, nil |
| } |
|
|
| |
| 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:] |
| } |
| 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:] |
| } |
| for _, x := range p { |
| s1 += uint32(x) |
| s2 += s1 |
| } |
| s1 %= mod |
| s2 %= mod |
| p = q |
| } |
| return digest(s2<<16 | s1) |
| } |
|
|
| func (d *digest) Write(p []byte) (nn int, err error) { |
| *d = update(*d, p) |
| return len(p), nil |
| } |
|
|
| func (d *digest) Sum32() uint32 { return uint32(*d) } |
|
|
| func (d *digest) Sum(in []byte) []byte { |
| s := uint32(*d) |
| return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s)) |
| } |
|
|
| |
| func Checksum(data []byte) uint32 { return uint32(update(1, data)) } |
|
|