blocks.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "crypto/sha256"
  10. "fmt"
  11. "io"
  12. "github.com/syncthing/protocol"
  13. )
  14. 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}
  15. // Blocks returns the blockwise hash of the reader.
  16. func Blocks(r io.Reader, blocksize int, sizehint int64) ([]protocol.BlockInfo, error) {
  17. var blocks []protocol.BlockInfo
  18. if sizehint > 0 {
  19. blocks = make([]protocol.BlockInfo, 0, int(sizehint/int64(blocksize)))
  20. }
  21. var offset int64
  22. hf := sha256.New()
  23. for {
  24. lr := &io.LimitedReader{R: r, N: int64(blocksize)}
  25. n, err := io.Copy(hf, lr)
  26. if err != nil {
  27. return nil, err
  28. }
  29. if n == 0 {
  30. break
  31. }
  32. b := protocol.BlockInfo{
  33. Size: int32(n),
  34. Offset: offset,
  35. Hash: hf.Sum(nil),
  36. }
  37. blocks = append(blocks, b)
  38. offset += int64(n)
  39. hf.Reset()
  40. }
  41. if len(blocks) == 0 {
  42. // Empty file
  43. blocks = append(blocks, protocol.BlockInfo{
  44. Offset: 0,
  45. Size: 0,
  46. Hash: SHA256OfNothing,
  47. })
  48. }
  49. return blocks, nil
  50. }
  51. // Set the Offset field on each block
  52. func PopulateOffsets(blocks []protocol.BlockInfo) {
  53. var offset int64
  54. for i := range blocks {
  55. blocks[i].Offset = offset
  56. offset += int64(blocks[i].Size)
  57. }
  58. }
  59. // BlockDiff returns lists of common and missing (to transform src into tgt)
  60. // blocks. Both block lists must have been created with the same block size.
  61. func BlockDiff(src, tgt []protocol.BlockInfo) (have, need []protocol.BlockInfo) {
  62. if len(tgt) == 0 && len(src) != 0 {
  63. return nil, nil
  64. }
  65. if len(tgt) != 0 && len(src) == 0 {
  66. // Copy the entire file
  67. return nil, tgt
  68. }
  69. for i := range tgt {
  70. if i >= len(src) || bytes.Compare(tgt[i].Hash, src[i].Hash) != 0 {
  71. // Copy differing block
  72. need = append(need, tgt[i])
  73. } else {
  74. have = append(have, tgt[i])
  75. }
  76. }
  77. return have, need
  78. }
  79. // Verify returns nil or an error describing the mismatch between the block
  80. // list and actual reader contents
  81. func Verify(r io.Reader, blocksize int, blocks []protocol.BlockInfo) error {
  82. hf := sha256.New()
  83. for i, block := range blocks {
  84. lr := &io.LimitedReader{R: r, N: int64(blocksize)}
  85. _, err := io.Copy(hf, lr)
  86. if err != nil {
  87. return err
  88. }
  89. hash := hf.Sum(nil)
  90. hf.Reset()
  91. if bytes.Compare(hash, block.Hash) != 0 {
  92. return fmt.Errorf("hash mismatch %x != %x for block %d", hash, block.Hash, i)
  93. }
  94. }
  95. // We should have reached the end now
  96. bs := make([]byte, 1)
  97. n, err := r.Read(bs)
  98. if n != 0 || err != io.EOF {
  99. return fmt.Errorf("file continues past end of blocks")
  100. }
  101. return nil
  102. }
  103. func VerifyBuffer(buf []byte, block protocol.BlockInfo) ([]byte, error) {
  104. if len(buf) != int(block.Size) {
  105. return nil, fmt.Errorf("length mismatch %d != %d", len(buf), block.Size)
  106. }
  107. hf := sha256.New()
  108. _, err := hf.Write(buf)
  109. if err != nil {
  110. return nil, err
  111. }
  112. hash := hf.Sum(nil)
  113. if !bytes.Equal(hash, block.Hash) {
  114. return hash, fmt.Errorf("hash mismatch %x != %x", hash, block.Hash)
  115. }
  116. return hash, nil
  117. }
  118. // BlockEqual returns whether two slices of blocks are exactly the same hash
  119. // and index pair wise.
  120. func BlocksEqual(src, tgt []protocol.BlockInfo) bool {
  121. if len(tgt) != len(src) {
  122. return false
  123. }
  124. for i, sblk := range src {
  125. if !bytes.Equal(sblk.Hash, tgt[i].Hash) {
  126. return false
  127. }
  128. }
  129. return true
  130. }