2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2014-03-08 22:02:01 +00:00
|
|
|
package scanner
|
2014-03-02 22:58:14 +00:00
|
|
|
|
|
|
|
import (
|
2018-05-05 08:24:44 +00:00
|
|
|
"bytes"
|
2017-04-26 00:15:23 +00:00
|
|
|
"context"
|
2017-01-23 13:50:32 +00:00
|
|
|
"hash"
|
2019-02-25 09:29:31 +00:00
|
|
|
"hash/adler32"
|
2014-03-02 22:58:14 +00:00
|
|
|
"io"
|
2014-07-12 21:06:48 +00:00
|
|
|
|
2015-09-22 17:38:46 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2016-09-23 19:33:54 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sha256"
|
2014-03-02 22:58:14 +00:00
|
|
|
)
|
|
|
|
|
2014-10-24 22:20:08 +00:00
|
|
|
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}
|
2014-07-26 19:28:32 +00:00
|
|
|
|
2015-11-17 20:08:36 +00:00
|
|
|
type Counter interface {
|
|
|
|
Update(bytes int64)
|
|
|
|
}
|
|
|
|
|
2014-03-02 22:58:14 +00:00
|
|
|
// Blocks returns the blockwise hash of the reader.
|
2017-04-26 00:15:23 +00:00
|
|
|
func Blocks(ctx context.Context, r io.Reader, blocksize int, sizehint int64, counter Counter, useWeakHashes bool) ([]protocol.BlockInfo, error) {
|
2018-01-14 14:30:11 +00:00
|
|
|
if counter == nil {
|
|
|
|
counter = &noopCounter{}
|
|
|
|
}
|
|
|
|
|
2015-10-27 08:31:28 +00:00
|
|
|
hf := sha256.New()
|
|
|
|
hashLength := hf.Size()
|
2017-01-23 13:50:32 +00:00
|
|
|
|
2018-01-14 14:30:11 +00:00
|
|
|
var weakHf hash.Hash32 = noopHash{}
|
|
|
|
var multiHf io.Writer = hf
|
2017-01-23 13:50:32 +00:00
|
|
|
if useWeakHashes {
|
2018-01-14 14:30:11 +00:00
|
|
|
// Use an actual weak hash function, make the multiHf
|
|
|
|
// write to both hash functions.
|
|
|
|
weakHf = adler32.New()
|
|
|
|
multiHf = io.MultiWriter(hf, weakHf)
|
2017-01-23 13:50:32 +00:00
|
|
|
}
|
2015-10-27 08:31:28 +00:00
|
|
|
|
2014-07-12 21:06:48 +00:00
|
|
|
var blocks []protocol.BlockInfo
|
2015-10-27 08:31:28 +00:00
|
|
|
var hashes, thisHash []byte
|
|
|
|
|
2016-07-26 08:51:39 +00:00
|
|
|
if sizehint >= 0 {
|
2015-10-27 08:31:28 +00:00
|
|
|
// Allocate contiguous blocks for the BlockInfo structures and their
|
2016-07-25 19:16:49 +00:00
|
|
|
// hashes once and for all, and stick to the specified size.
|
|
|
|
r = io.LimitReader(r, sizehint)
|
2015-10-27 08:31:28 +00:00
|
|
|
numBlocks := int(sizehint / int64(blocksize))
|
|
|
|
blocks = make([]protocol.BlockInfo, 0, numBlocks)
|
|
|
|
hashes = make([]byte, 0, hashLength*numBlocks)
|
2014-08-12 11:52:36 +00:00
|
|
|
}
|
2015-10-27 08:31:28 +00:00
|
|
|
|
|
|
|
// A 32k buffer is used for copying into the hash function.
|
|
|
|
buf := make([]byte, 32<<10)
|
|
|
|
|
2014-03-02 22:58:14 +00:00
|
|
|
var offset int64
|
2017-01-18 17:43:00 +00:00
|
|
|
lr := io.LimitReader(r, int64(blocksize)).(*io.LimitedReader)
|
2014-03-02 22:58:14 +00:00
|
|
|
for {
|
2017-04-26 00:15:23 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2017-01-18 17:43:00 +00:00
|
|
|
lr.N = int64(blocksize)
|
2018-01-14 14:30:11 +00:00
|
|
|
n, err := io.CopyBuffer(multiHf, lr, buf)
|
2014-03-02 22:58:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if n == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-01-14 14:30:11 +00:00
|
|
|
counter.Update(n)
|
2015-08-26 22:49:06 +00:00
|
|
|
|
2015-10-27 08:31:28 +00:00
|
|
|
// Carve out a hash-sized chunk of "hashes" to store the hash for this
|
|
|
|
// block.
|
|
|
|
hashes = hf.Sum(hashes)
|
|
|
|
thisHash, hashes = hashes[:hashLength], hashes[hashLength:]
|
|
|
|
|
2014-07-12 21:06:48 +00:00
|
|
|
b := protocol.BlockInfo{
|
2016-12-14 23:30:29 +00:00
|
|
|
Size: int32(n),
|
|
|
|
Offset: offset,
|
|
|
|
Hash: thisHash,
|
2018-01-14 14:30:11 +00:00
|
|
|
WeakHash: weakHf.Sum32(),
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
2015-10-27 08:31:28 +00:00
|
|
|
|
2014-03-02 22:58:14 +00:00
|
|
|
blocks = append(blocks, b)
|
2016-12-17 14:37:11 +00:00
|
|
|
offset += n
|
2014-08-12 11:52:36 +00:00
|
|
|
|
|
|
|
hf.Reset()
|
2018-01-14 14:30:11 +00:00
|
|
|
weakHf.Reset()
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(blocks) == 0 {
|
|
|
|
// Empty file
|
2014-07-12 21:06:48 +00:00
|
|
|
blocks = append(blocks, protocol.BlockInfo{
|
2014-03-02 22:58:14 +00:00
|
|
|
Offset: 0,
|
|
|
|
Size: 0,
|
2014-10-24 22:20:08 +00:00
|
|
|
Hash: SHA256OfNothing,
|
2014-03-02 22:58:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return blocks, nil
|
|
|
|
}
|
|
|
|
|
2018-05-05 08:24:44 +00:00
|
|
|
func Validate(buf, hash []byte, weakHash uint32) bool {
|
|
|
|
rd := bytes.NewReader(buf)
|
|
|
|
if weakHash != 0 {
|
|
|
|
whf := adler32.New()
|
|
|
|
if _, err := io.Copy(whf, rd); err == nil && whf.Sum32() == weakHash {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Copy error or mismatch, go to next algo.
|
2019-02-02 11:16:27 +00:00
|
|
|
rd.Seek(0, io.SeekStart)
|
2018-05-05 08:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(hash) > 0 {
|
|
|
|
hf := sha256.New()
|
|
|
|
if _, err := io.Copy(hf, rd); err == nil {
|
|
|
|
// Sum allocates, so let's hope we don't hit this often.
|
|
|
|
return bytes.Equal(hf.Sum(nil), hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both algos failed or no hashes were specified. Assume it's all good.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-01-23 13:50:32 +00:00
|
|
|
type noopHash struct{}
|
|
|
|
|
|
|
|
func (noopHash) Sum32() uint32 { return 0 }
|
|
|
|
func (noopHash) BlockSize() int { return 0 }
|
|
|
|
func (noopHash) Size() int { return 0 }
|
|
|
|
func (noopHash) Reset() {}
|
|
|
|
func (noopHash) Sum([]byte) []byte { return nil }
|
|
|
|
func (noopHash) Write([]byte) (int, error) { return 0, nil }
|
2018-01-14 14:30:11 +00:00
|
|
|
|
|
|
|
type noopCounter struct{}
|
|
|
|
|
|
|
|
func (c *noopCounter) Update(bytes int64) {}
|