2016-12-14 23:30:29 +00:00
|
|
|
// Copyright (C) 2016 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// 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/.
|
2016-12-14 23:30:29 +00:00
|
|
|
|
|
|
|
package weakhash
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
2017-01-04 21:04:13 +00:00
|
|
|
|
|
|
|
"github.com/chmduquesne/rollinghash/adler32"
|
2016-12-14 23:30:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
Size = 4
|
2017-01-24 08:26:45 +00:00
|
|
|
|
|
|
|
// don't track more hits than this for any given weakhash
|
|
|
|
maxWeakhashFinderHits = 10
|
2016-12-14 23:30:29 +00:00
|
|
|
)
|
|
|
|
|
2017-02-06 10:27:11 +00:00
|
|
|
var (
|
|
|
|
Enabled = true
|
|
|
|
)
|
|
|
|
|
2016-12-14 23:30:29 +00:00
|
|
|
// Find finds all the blocks of the given size within io.Reader that matches
|
|
|
|
// the hashes provided, and returns a hash -> slice of offsets within reader
|
|
|
|
// map, that produces the same weak hash.
|
|
|
|
func Find(ir io.Reader, hashesToFind []uint32, size int) (map[uint32][]int64, error) {
|
2017-02-06 10:27:11 +00:00
|
|
|
if ir == nil || len(hashesToFind) == 0 {
|
2016-12-14 23:30:29 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
r := bufio.NewReader(ir)
|
2017-01-04 21:04:13 +00:00
|
|
|
hf := adler32.New()
|
2016-12-14 23:30:29 +00:00
|
|
|
|
|
|
|
n, err := io.CopyN(hf, r, int64(size))
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if n != int64(size) {
|
|
|
|
return nil, io.ErrShortBuffer
|
|
|
|
}
|
|
|
|
|
|
|
|
offsets := make(map[uint32][]int64)
|
|
|
|
for _, hashToFind := range hashesToFind {
|
2017-02-06 10:27:11 +00:00
|
|
|
offsets[hashToFind] = make([]int64, 0, maxWeakhashFinderHits)
|
2016-12-14 23:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var i int64
|
|
|
|
var hash uint32
|
|
|
|
for {
|
|
|
|
hash = hf.Sum32()
|
2017-01-24 08:26:45 +00:00
|
|
|
if existing, ok := offsets[hash]; ok && len(existing) < maxWeakhashFinderHits {
|
2016-12-14 23:30:29 +00:00
|
|
|
offsets[hash] = append(existing, i)
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
|
|
|
|
bt, err := r.ReadByte()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return offsets, err
|
|
|
|
}
|
2017-01-04 21:04:13 +00:00
|
|
|
hf.Roll(bt)
|
2016-12-14 23:30:29 +00:00
|
|
|
}
|
|
|
|
return offsets, nil
|
|
|
|
}
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
func NewFinder(ir io.ReadSeeker, size int, hashesToFind []uint32) (*Finder, error) {
|
|
|
|
offsets, err := Find(ir, hashesToFind, size)
|
2016-12-14 23:30:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Finder{
|
2017-08-19 14:36:56 +00:00
|
|
|
reader: ir,
|
2016-12-14 23:30:29 +00:00
|
|
|
size: size,
|
|
|
|
offsets: offsets,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Finder struct {
|
2017-08-19 14:36:56 +00:00
|
|
|
reader io.ReadSeeker
|
2016-12-14 23:30:29 +00:00
|
|
|
size int
|
|
|
|
offsets map[uint32][]int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate iterates all available blocks that matches the provided hash, reads
|
|
|
|
// them into buf, and calls the iterator function. The iterator function should
|
2017-02-25 08:12:13 +00:00
|
|
|
// return whether it wishes to continue iterating.
|
2016-12-14 23:30:29 +00:00
|
|
|
func (h *Finder) Iterate(hash uint32, buf []byte, iterFunc func(int64) bool) (bool, error) {
|
|
|
|
if h == nil || hash == 0 || len(buf) != h.size {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, offset := range h.offsets[hash] {
|
2017-08-19 14:36:56 +00:00
|
|
|
_, err := h.reader.Seek(offset, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
_, err = h.reader.Read(buf)
|
2016-12-14 23:30:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !iterFunc(offset) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|