blocks.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 http://mozilla.org/MPL/2.0/.
  6. package scanner
  7. import (
  8. "bytes"
  9. "fmt"
  10. "hash/adler32"
  11. "io"
  12. "github.com/syncthing/syncthing/lib/protocol"
  13. "github.com/syncthing/syncthing/lib/sha256"
  14. )
  15. 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}
  16. type Counter interface {
  17. Update(bytes int64)
  18. }
  19. // Blocks returns the blockwise hash of the reader.
  20. func Blocks(r io.Reader, blocksize int, sizehint int64, counter Counter) ([]protocol.BlockInfo, error) {
  21. hf := sha256.New()
  22. hashLength := hf.Size()
  23. whf := adler32.New()
  24. mhf := io.MultiWriter(hf, whf)
  25. var blocks []protocol.BlockInfo
  26. var hashes, thisHash []byte
  27. if sizehint >= 0 {
  28. // Allocate contiguous blocks for the BlockInfo structures and their
  29. // hashes once and for all, and stick to the specified size.
  30. r = io.LimitReader(r, sizehint)
  31. numBlocks := int(sizehint / int64(blocksize))
  32. blocks = make([]protocol.BlockInfo, 0, numBlocks)
  33. hashes = make([]byte, 0, hashLength*numBlocks)
  34. }
  35. // A 32k buffer is used for copying into the hash function.
  36. buf := make([]byte, 32<<10)
  37. var offset int64
  38. for {
  39. lr := io.LimitReader(r, int64(blocksize))
  40. n, err := io.CopyBuffer(mhf, lr, buf)
  41. if err != nil {
  42. return nil, err
  43. }
  44. if n == 0 {
  45. break
  46. }
  47. if counter != nil {
  48. counter.Update(n)
  49. }
  50. // Carve out a hash-sized chunk of "hashes" to store the hash for this
  51. // block.
  52. hashes = hf.Sum(hashes)
  53. thisHash, hashes = hashes[:hashLength], hashes[hashLength:]
  54. b := protocol.BlockInfo{
  55. Size: int32(n),
  56. Offset: offset,
  57. Hash: thisHash,
  58. WeakHash: whf.Sum32(),
  59. }
  60. blocks = append(blocks, b)
  61. offset += n
  62. hf.Reset()
  63. whf.Reset()
  64. }
  65. if len(blocks) == 0 {
  66. // Empty file
  67. blocks = append(blocks, protocol.BlockInfo{
  68. Offset: 0,
  69. Size: 0,
  70. Hash: SHA256OfNothing,
  71. })
  72. }
  73. return blocks, nil
  74. }
  75. // PopulateOffsets sets the Offset field on each block
  76. func PopulateOffsets(blocks []protocol.BlockInfo) {
  77. var offset int64
  78. for i := range blocks {
  79. blocks[i].Offset = offset
  80. offset += int64(blocks[i].Size)
  81. }
  82. }
  83. // BlockDiff returns lists of common and missing (to transform src into tgt)
  84. // blocks. Both block lists must have been created with the same block size.
  85. func BlockDiff(src, tgt []protocol.BlockInfo) (have, need []protocol.BlockInfo) {
  86. if len(tgt) == 0 && len(src) != 0 {
  87. return nil, nil
  88. }
  89. if len(tgt) != 0 && len(src) == 0 {
  90. // Copy the entire file
  91. return nil, tgt
  92. }
  93. for i := range tgt {
  94. if i >= len(src) || !bytes.Equal(tgt[i].Hash, src[i].Hash) {
  95. // Copy differing block
  96. need = append(need, tgt[i])
  97. } else {
  98. have = append(have, tgt[i])
  99. }
  100. }
  101. return have, need
  102. }
  103. // Verify returns nil or an error describing the mismatch between the block
  104. // list and actual reader contents
  105. func Verify(r io.Reader, blocksize int, blocks []protocol.BlockInfo) error {
  106. hf := sha256.New()
  107. // A 32k buffer is used for copying into the hash function.
  108. buf := make([]byte, 32<<10)
  109. for i, block := range blocks {
  110. lr := &io.LimitedReader{R: r, N: int64(blocksize)}
  111. _, err := io.CopyBuffer(hf, lr, buf)
  112. if err != nil {
  113. return err
  114. }
  115. hash := hf.Sum(nil)
  116. hf.Reset()
  117. if !bytes.Equal(hash, block.Hash) {
  118. return fmt.Errorf("hash mismatch %x != %x for block %d", hash, block.Hash, i)
  119. }
  120. }
  121. // We should have reached the end now
  122. bs := make([]byte, 1)
  123. n, err := r.Read(bs)
  124. if n != 0 || err != io.EOF {
  125. return fmt.Errorf("file continues past end of blocks")
  126. }
  127. return nil
  128. }
  129. func VerifyBuffer(buf []byte, block protocol.BlockInfo) ([]byte, error) {
  130. if len(buf) != int(block.Size) {
  131. return nil, fmt.Errorf("length mismatch %d != %d", len(buf), block.Size)
  132. }
  133. hf := sha256.New()
  134. _, err := hf.Write(buf)
  135. if err != nil {
  136. return nil, err
  137. }
  138. hash := hf.Sum(nil)
  139. if !bytes.Equal(hash, block.Hash) {
  140. return hash, fmt.Errorf("hash mismatch %x != %x", hash, block.Hash)
  141. }
  142. return hash, nil
  143. }
  144. // BlocksEqual returns whether two slices of blocks are exactly the same hash
  145. // and index pair wise.
  146. func BlocksEqual(src, tgt []protocol.BlockInfo) bool {
  147. if len(tgt) != len(src) {
  148. return false
  149. }
  150. for i, sblk := range src {
  151. if !bytes.Equal(sblk.Hash, tgt[i].Hash) {
  152. return false
  153. }
  154. }
  155. return true
  156. }