adler32.go 3.2 KB

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