blocks.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package scanner
  7. import (
  8. "bytes"
  9. "context"
  10. "crypto/sha256"
  11. "hash"
  12. "hash/adler32"
  13. "io"
  14. "github.com/syncthing/syncthing/lib/protocol"
  15. )
  16. var SHA256OfNothing = []uint8{0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55}
  17. type Counter interface {
  18. Update(bytes int64)
  19. }
  20. // Blocks returns the blockwise hash of the reader.
  21. func Blocks(ctx context.Context, r io.Reader, blocksize int, sizehint int64, counter Counter, useWeakHashes bool) ([]protocol.BlockInfo, error) {
  22. if counter == nil {
  23. counter = &noopCounter{}
  24. }
  25. hf := sha256.New()
  26. const hashLength = sha256.Size
  27. var weakHf hash.Hash32 = noopHash{}
  28. var multiHf io.Writer = hf
  29. if useWeakHashes {
  30. // Use an actual weak hash function, make the multiHf
  31. // write to both hash functions.
  32. weakHf = adler32.New()
  33. multiHf = io.MultiWriter(hf, weakHf)
  34. }
  35. var blocks []protocol.BlockInfo
  36. var hashes, thisHash []byte
  37. if sizehint >= 0 {
  38. // Allocate contiguous blocks for the BlockInfo structures and their
  39. // hashes once and for all, and stick to the specified size.
  40. r = io.LimitReader(r, sizehint)
  41. numBlocks := sizehint / int64(blocksize)
  42. remainder := sizehint % int64(blocksize)
  43. if remainder != 0 {
  44. numBlocks++
  45. }
  46. blocks = make([]protocol.BlockInfo, 0, numBlocks)
  47. hashes = make([]byte, 0, hashLength*numBlocks)
  48. }
  49. // A 32k buffer is used for copying into the hash function.
  50. buf := make([]byte, 32<<10)
  51. var offset int64
  52. lr := io.LimitReader(r, int64(blocksize)).(*io.LimitedReader)
  53. for {
  54. select {
  55. case <-ctx.Done():
  56. return nil, ctx.Err()
  57. default:
  58. }
  59. lr.N = int64(blocksize)
  60. n, err := io.CopyBuffer(multiHf, lr, buf)
  61. if err != nil {
  62. return nil, err
  63. }
  64. if n == 0 {
  65. break
  66. }
  67. counter.Update(n)
  68. // Carve out a hash-sized chunk of "hashes" to store the hash for this
  69. // block.
  70. hashes = hf.Sum(hashes)
  71. thisHash, hashes = hashes[:hashLength], hashes[hashLength:]
  72. b := protocol.BlockInfo{
  73. Size: int(n),
  74. Offset: offset,
  75. Hash: thisHash,
  76. WeakHash: weakHf.Sum32(),
  77. }
  78. blocks = append(blocks, b)
  79. offset += n
  80. hf.Reset()
  81. weakHf.Reset()
  82. }
  83. if len(blocks) == 0 {
  84. // Empty file
  85. blocks = append(blocks, protocol.BlockInfo{
  86. Offset: 0,
  87. Size: 0,
  88. Hash: SHA256OfNothing,
  89. })
  90. }
  91. return blocks, nil
  92. }
  93. // Validate quickly validates buf against the 32-bit weakHash, if not zero,
  94. // else against the cryptohash hash, if len(hash)>0. It is satisfied if
  95. // either hash matches or neither hash is given.
  96. func Validate(buf, hash []byte, weakHash uint32) bool {
  97. if weakHash != 0 && adler32.Checksum(buf) == weakHash {
  98. return true
  99. }
  100. if len(hash) > 0 {
  101. hbuf := sha256.Sum256(buf)
  102. return bytes.Equal(hbuf[:], hash)
  103. }
  104. return true
  105. }
  106. type noopHash struct{}
  107. func (noopHash) Sum32() uint32 { return 0 }
  108. func (noopHash) BlockSize() int { return 0 }
  109. func (noopHash) Size() int { return 0 }
  110. func (noopHash) Reset() {}
  111. func (noopHash) Sum([]byte) []byte { return nil }
  112. func (noopHash) Write([]byte) (int, error) { return 0, nil }
  113. type noopCounter struct{}
  114. func (*noopCounter) Update(_ int64) {}