blocks.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package scanner
  16. import (
  17. "bytes"
  18. "crypto/sha256"
  19. "fmt"
  20. "io"
  21. "github.com/syncthing/syncthing/internal/protocol"
  22. )
  23. 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}
  24. // Blocks returns the blockwise hash of the reader.
  25. func Blocks(r io.Reader, blocksize int, sizehint int64) ([]protocol.BlockInfo, error) {
  26. var blocks []protocol.BlockInfo
  27. if sizehint > 0 {
  28. blocks = make([]protocol.BlockInfo, 0, int(sizehint/int64(blocksize)))
  29. }
  30. var offset int64
  31. hf := sha256.New()
  32. for {
  33. lr := &io.LimitedReader{R: r, N: int64(blocksize)}
  34. n, err := io.Copy(hf, lr)
  35. if err != nil {
  36. return nil, err
  37. }
  38. if n == 0 {
  39. break
  40. }
  41. b := protocol.BlockInfo{
  42. Size: uint32(n),
  43. Offset: offset,
  44. Hash: hf.Sum(nil),
  45. }
  46. blocks = append(blocks, b)
  47. offset += int64(n)
  48. hf.Reset()
  49. }
  50. if len(blocks) == 0 {
  51. // Empty file
  52. blocks = append(blocks, protocol.BlockInfo{
  53. Offset: 0,
  54. Size: 0,
  55. Hash: SHA256OfNothing,
  56. })
  57. }
  58. return blocks, nil
  59. }
  60. // Set the Offset field on each block
  61. func PopulateOffsets(blocks []protocol.BlockInfo) {
  62. var offset int64
  63. for i := range blocks {
  64. blocks[i].Offset = offset
  65. offset += int64(blocks[i].Size)
  66. }
  67. }
  68. // BlockDiff returns lists of common and missing (to transform src into tgt)
  69. // blocks. Both block lists must have been created with the same block size.
  70. func BlockDiff(src, tgt []protocol.BlockInfo) (have, need []protocol.BlockInfo) {
  71. if len(tgt) == 0 && len(src) != 0 {
  72. return nil, nil
  73. }
  74. if len(tgt) != 0 && len(src) == 0 {
  75. // Copy the entire file
  76. return nil, tgt
  77. }
  78. for i := range tgt {
  79. if i >= len(src) || bytes.Compare(tgt[i].Hash, src[i].Hash) != 0 {
  80. // Copy differing block
  81. need = append(need, tgt[i])
  82. } else {
  83. have = append(have, tgt[i])
  84. }
  85. }
  86. return have, need
  87. }
  88. // Verify returns nil or an error describing the mismatch between the block
  89. // list and actual reader contents
  90. func Verify(r io.Reader, blocksize int, blocks []protocol.BlockInfo) error {
  91. hf := sha256.New()
  92. for i, block := range blocks {
  93. lr := &io.LimitedReader{R: r, N: int64(blocksize)}
  94. _, err := io.Copy(hf, lr)
  95. if err != nil {
  96. return err
  97. }
  98. hash := hf.Sum(nil)
  99. hf.Reset()
  100. if bytes.Compare(hash, block.Hash) != 0 {
  101. return fmt.Errorf("hash mismatch %x != %x for block %d", hash, block.Hash, i)
  102. }
  103. }
  104. // We should have reached the end now
  105. bs := make([]byte, 1)
  106. n, err := r.Read(bs)
  107. if n != 0 || err != io.EOF {
  108. return fmt.Errorf("file continues past end of blocks")
  109. }
  110. return nil
  111. }
  112. func VerifyBuffer(buf []byte, block protocol.BlockInfo) ([]byte, error) {
  113. if len(buf) != int(block.Size) {
  114. return nil, fmt.Errorf("length mismatch %d != %d", len(buf), block.Size)
  115. }
  116. hf := sha256.New()
  117. _, err := hf.Write(buf)
  118. if err != nil {
  119. return nil, err
  120. }
  121. hash := hf.Sum(nil)
  122. if !bytes.Equal(hash, block.Hash) {
  123. return hash, fmt.Errorf("hash mismatch %x != %x", hash, block.Hash)
  124. }
  125. return hash, nil
  126. }
  127. // BlockEqual returns whether two slices of blocks are exactly the same hash
  128. // and index pair wise.
  129. func BlocksEqual(src, tgt []protocol.BlockInfo) bool {
  130. if len(tgt) != len(src) {
  131. return false
  132. }
  133. for i, sblk := range src {
  134. if !bytes.Equal(sblk.Hash, tgt[i].Hash) {
  135. return false
  136. }
  137. }
  138. return true
  139. }