blocks.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "hash"
  11. "hash/adler32"
  12. "io"
  13. "github.com/syncthing/syncthing/lib/protocol"
  14. "github.com/syncthing/syncthing/lib/sha256"
  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. hashLength := hf.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 := int(sizehint / int64(blocksize))
  42. blocks = make([]protocol.BlockInfo, 0, numBlocks)
  43. hashes = make([]byte, 0, hashLength*numBlocks)
  44. }
  45. // A 32k buffer is used for copying into the hash function.
  46. buf := make([]byte, 32<<10)
  47. var offset int64
  48. lr := io.LimitReader(r, int64(blocksize)).(*io.LimitedReader)
  49. for {
  50. select {
  51. case <-ctx.Done():
  52. return nil, ctx.Err()
  53. default:
  54. }
  55. lr.N = int64(blocksize)
  56. n, err := io.CopyBuffer(multiHf, lr, buf)
  57. if err != nil {
  58. return nil, err
  59. }
  60. if n == 0 {
  61. break
  62. }
  63. counter.Update(n)
  64. // Carve out a hash-sized chunk of "hashes" to store the hash for this
  65. // block.
  66. hashes = hf.Sum(hashes)
  67. thisHash, hashes = hashes[:hashLength], hashes[hashLength:]
  68. b := protocol.BlockInfo{
  69. Size: int32(n),
  70. Offset: offset,
  71. Hash: thisHash,
  72. WeakHash: weakHf.Sum32(),
  73. }
  74. blocks = append(blocks, b)
  75. offset += n
  76. hf.Reset()
  77. weakHf.Reset()
  78. }
  79. if len(blocks) == 0 {
  80. // Empty file
  81. blocks = append(blocks, protocol.BlockInfo{
  82. Offset: 0,
  83. Size: 0,
  84. Hash: SHA256OfNothing,
  85. })
  86. }
  87. return blocks, nil
  88. }
  89. // Validate quickly validates buf against the 32-bit weakHash, if not zero,
  90. // else against the cryptohash hash, if len(hash)>0.
  91. // It is satisfied if neither hash is given.
  92. func Validate(buf, hash []byte, weakHash uint32) bool {
  93. if weakHash != 0 {
  94. return adler32.Checksum(buf) == weakHash
  95. }
  96. if len(hash) > 0 {
  97. hbuf := sha256.Sum256(buf)
  98. return bytes.Equal(hbuf[:], hash)
  99. }
  100. return true
  101. }
  102. type noopHash struct{}
  103. func (noopHash) Sum32() uint32 { return 0 }
  104. func (noopHash) BlockSize() int { return 0 }
  105. func (noopHash) Size() int { return 0 }
  106. func (noopHash) Reset() {}
  107. func (noopHash) Sum([]byte) []byte { return nil }
  108. func (noopHash) Write([]byte) (int, error) { return 0, nil }
  109. type noopCounter struct{}
  110. func (c *noopCounter) Update(bytes int64) {}