blocks.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. "io"
  13. "sync"
  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. const bufSize = 32 << 10 // 32k
  21. var bufPool = sync.Pool{
  22. New: func() any {
  23. return new([bufSize]byte) // 32k buffer
  24. },
  25. }
  26. const hashLength = sha256.Size
  27. var hashPool = sync.Pool{
  28. New: func() any {
  29. return sha256.New()
  30. },
  31. }
  32. // Blocks returns the blockwise hash of the reader.
  33. func Blocks(ctx context.Context, r io.Reader, blocksize int, sizehint int64, counter Counter) ([]protocol.BlockInfo, error) {
  34. if counter == nil {
  35. counter = &noopCounter{}
  36. }
  37. var blocks []protocol.BlockInfo
  38. var hashes, thisHash []byte
  39. if sizehint >= 0 {
  40. // Allocate contiguous blocks for the BlockInfo structures and their
  41. // hashes once and for all, and stick to the specified size.
  42. r = io.LimitReader(r, sizehint)
  43. numBlocks := sizehint / int64(blocksize)
  44. remainder := sizehint % int64(blocksize)
  45. if remainder != 0 {
  46. numBlocks++
  47. }
  48. blocks = make([]protocol.BlockInfo, 0, numBlocks)
  49. hashes = make([]byte, 0, hashLength*numBlocks)
  50. }
  51. hf := hashPool.Get().(hash.Hash) //nolint:forcetypeassert
  52. // A 32k buffer is used for copying into the hash function.
  53. buf := bufPool.Get().(*[bufSize]byte)[:] //nolint:forcetypeassert
  54. defer func() {
  55. bufPool.Put((*[bufSize]byte)(buf))
  56. hf.Reset()
  57. hashPool.Put(hf)
  58. }()
  59. var offset int64
  60. lr := io.LimitReader(r, int64(blocksize)).(*io.LimitedReader)
  61. for {
  62. select {
  63. case <-ctx.Done():
  64. return nil, ctx.Err()
  65. default:
  66. }
  67. lr.N = int64(blocksize)
  68. n, err := io.CopyBuffer(hf, lr, buf)
  69. if err != nil {
  70. return nil, err
  71. }
  72. if n == 0 {
  73. break
  74. }
  75. counter.Update(n)
  76. // Carve out a hash-sized chunk of "hashes" to store the hash for this
  77. // block.
  78. hashes = hf.Sum(hashes)
  79. thisHash, hashes = hashes[:hashLength], hashes[hashLength:]
  80. b := protocol.BlockInfo{
  81. Size: int(n),
  82. Offset: offset,
  83. Hash: thisHash,
  84. }
  85. blocks = append(blocks, b)
  86. offset += n
  87. hf.Reset()
  88. }
  89. if len(blocks) == 0 {
  90. // Empty file
  91. blocks = append(blocks, protocol.BlockInfo{
  92. Offset: 0,
  93. Size: 0,
  94. Hash: SHA256OfNothing,
  95. })
  96. }
  97. return blocks, nil
  98. }
  99. // Validate validates the hash, if len(hash)>0.
  100. func Validate(buf, hash []byte) bool {
  101. if len(hash) > 0 {
  102. hbuf := sha256.Sum256(buf)
  103. return bytes.Equal(hbuf[:], hash)
  104. }
  105. return true
  106. }
  107. type noopHash struct{}
  108. func (noopHash) Sum32() uint32 { return 0 }
  109. func (noopHash) BlockSize() int { return 0 }
  110. func (noopHash) Size() int { return 0 }
  111. func (noopHash) Reset() {}
  112. func (noopHash) Sum([]byte) []byte { return nil }
  113. func (noopHash) Write([]byte) (int, error) { return 0, nil }
  114. type noopCounter struct{}
  115. func (*noopCounter) Update(_ int64) {}