adler32.mx raw

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