github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/hash/adler32/adler32.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package adler32 implements the Adler-32 checksum.
     6  //
     7  // It is defined in RFC 1950:
     8  //	Adler-32 is composed of two sums accumulated per byte: s1 is
     9  //	the sum of all bytes, s2 is the sum of all s1 values. Both sums
    10  //	are done modulo 65521. s1 is initialized to 1, s2 to zero.  The
    11  //	Adler-32 checksum is stored as s2*65536 + s1 in most-
    12  //	significant-byte first (network) order.
    13  package adler32
    14  
    15  import (
    16  	"errors"
    17  	"hash"
    18  )
    19  
    20  const (
    21  	// mod is the largest prime that is less than 65536.
    22  	mod = 65521
    23  	// nmax is the largest n such that
    24  	// 255 * n * (n+1) / 2 + (n+1) * (mod-1) <= 2^32-1.
    25  	// It is mentioned in RFC 1950 (search for "5552").
    26  	nmax = 5552
    27  )
    28  
    29  // The size of an Adler-32 checksum in bytes.
    30  const Size = 4
    31  
    32  // digest represents the partial evaluation of a checksum.
    33  // The low 16 bits are s1, the high 16 bits are s2.
    34  type digest uint32
    35  
    36  func (d *digest) Reset() { *d = 1 }
    37  
    38  // New returns a new hash.Hash32 computing the Adler-32 checksum.
    39  // Its Sum method will lay the value out in big-endian byte order.
    40  func New() hash.Hash32 {
    41  	d := new(digest)
    42  	d.Reset()
    43  	return d
    44  }
    45  
    46  func (d *digest) Size() int { return Size }
    47  
    48  func (d *digest) BlockSize() int { return 4 }
    49  
    50  const (
    51  	magic         = "adl\x01"
    52  	marshaledSize = len(magic) + 4
    53  )
    54  
    55  func (d *digest) MarshalBinary() ([]byte, error) {
    56  	b := make([]byte, 0, marshaledSize)
    57  	b = append(b, magic...)
    58  	b = appendUint32(b, uint32(*d))
    59  	return b, nil
    60  }
    61  
    62  func (d *digest) UnmarshalBinary(b []byte) error {
    63  	if len(b) < len(magic) || string(b[:len(magic)]) != magic {
    64  		return errors.New("hash/adler32: invalid hash state identifier")
    65  	}
    66  	if len(b) != marshaledSize {
    67  		return errors.New("hash/adler32: invalid hash state size")
    68  	}
    69  	*d = digest(readUint32(b[len(magic):]))
    70  	return nil
    71  }
    72  
    73  func appendUint32(b []byte, x uint32) []byte {
    74  	a := [4]byte{
    75  		byte(x >> 24),
    76  		byte(x >> 16),
    77  		byte(x >> 8),
    78  		byte(x),
    79  	}
    80  	return append(b, a[:]...)
    81  }
    82  
    83  func readUint32(b []byte) uint32 {
    84  	_ = b[3]
    85  	return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
    86  }
    87  
    88  // Add p to the running checksum d.
    89  func update(d digest, p []byte) digest {
    90  	s1, s2 := uint32(d&0xffff), uint32(d>>16)
    91  	for len(p) > 0 {
    92  		var q []byte
    93  		if len(p) > nmax {
    94  			p, q = p[:nmax], p[nmax:]
    95  		}
    96  		for len(p) >= 4 {
    97  			s1 += uint32(p[0])
    98  			s2 += s1
    99  			s1 += uint32(p[1])
   100  			s2 += s1
   101  			s1 += uint32(p[2])
   102  			s2 += s1
   103  			s1 += uint32(p[3])
   104  			s2 += s1
   105  			p = p[4:]
   106  		}
   107  		for _, x := range p {
   108  			s1 += uint32(x)
   109  			s2 += s1
   110  		}
   111  		s1 %= mod
   112  		s2 %= mod
   113  		p = q
   114  	}
   115  	return digest(s2<<16 | s1)
   116  }
   117  
   118  func (d *digest) Write(p []byte) (nn int, err error) {
   119  	*d = update(*d, p)
   120  	return len(p), nil
   121  }
   122  
   123  func (d *digest) Sum32() uint32 { return uint32(*d) }
   124  
   125  func (d *digest) Sum(in []byte) []byte {
   126  	s := uint32(*d)
   127  	return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s))
   128  }
   129  
   130  // Checksum returns the Adler-32 checksum of data.
   131  func Checksum(data []byte) uint32 { return uint32(update(1, data)) }