1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
|
// Algorithm found at: https://rsync.samba.org/tech_report/tech_report.html
// Code in this file is inspired by: https://github.com/jbreiding/rsync-go
//
// Definitions
//
// Source: The final content.
// Target: The content to be made into final content.
// Signature: The sequence of hashes used to identify the content.
package rsync
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"hash"
"io"
"slices"
"strconv"
"github.com/zeebo/xxh3"
)
// If no BlockSize is specified in the rsync instance, this value is used.
const DefaultBlockSize = 1024 * 6
// Internal constant used in rolling checksum.
const _M = 1 << 16
// Operation Types.
type OpType byte // enum
const (
OpBlock OpType = iota
OpData
OpHash
OpBlockRange
)
type xxh3_128 struct {
xxh3.Hasher
}
func (self *xxh3_128) Sum(b []byte) []byte {
s := self.Sum128()
pos := len(b)
limit := pos + 16
if limit > cap(b) {
var x [16]byte
b = append(b, x[:]...)
} else {
b = b[:limit]
}
binary.BigEndian.PutUint64(b[pos:], s.Hi)
binary.BigEndian.PutUint64(b[pos+8:], s.Lo)
return b
}
func new_xxh3_64() hash.Hash64 {
ans := xxh3.New()
ans.Reset()
return ans
}
func new_xxh3_128() hash.Hash {
ans := new(xxh3_128)
ans.Reset()
return ans
}
// Instruction to mutate target to align to source.
type Operation struct {
Type OpType
BlockIndex uint64
BlockIndexEnd uint64
Data []byte
}
func (self Operation) String() string {
ans := "{" + self.Type.String() + " "
switch self.Type {
case OpBlock:
ans += strconv.FormatUint(self.BlockIndex, 10)
case OpBlockRange:
ans += strconv.FormatUint(self.BlockIndex, 10) + " to " + strconv.FormatUint(self.BlockIndexEnd, 10)
case OpData:
ans += strconv.Itoa(len(self.Data))
case OpHash:
ans += hex.EncodeToString(self.Data)
}
return ans + "}"
}
var bin = binary.LittleEndian
func (self Operation) SerializeSize() int {
switch self.Type {
case OpBlock:
return 9
case OpBlockRange:
return 13
case OpHash:
return 3 + len(self.Data)
case OpData:
return 5 + len(self.Data)
}
return -1
}
func (self Operation) Serialize(ans []byte) {
switch self.Type {
case OpBlock:
bin.PutUint64(ans[1:], self.BlockIndex)
case OpBlockRange:
bin.PutUint64(ans[1:], self.BlockIndex)
bin.PutUint32(ans[9:], uint32(self.BlockIndexEnd-self.BlockIndex))
case OpHash:
bin.PutUint16(ans[1:], uint16(len(self.Data)))
copy(ans[3:], self.Data)
case OpData:
bin.PutUint32(ans[1:], uint32(len(self.Data)))
copy(ans[5:], self.Data)
}
ans[0] = byte(self.Type)
}
func (self *Operation) Unserialize(data []byte) (n int, err error) {
if len(data) < 1 {
return -1, io.ErrShortBuffer
}
switch OpType(data[0]) {
case OpBlock:
n = 9
if len(data) < n {
return -1, io.ErrShortBuffer
}
self.BlockIndex = bin.Uint64(data[1:])
self.Data = nil
case OpBlockRange:
n = 13
if len(data) < n {
return -1, io.ErrShortBuffer
}
self.BlockIndex = bin.Uint64(data[1:])
self.BlockIndexEnd = self.BlockIndex + uint64(bin.Uint32(data[9:]))
self.Data = nil
case OpHash:
n = 3
if len(data) < n {
return -1, io.ErrShortBuffer
}
sz := int(bin.Uint16(data[1:]))
n += sz
if len(data) < n {
return -1, io.ErrShortBuffer
}
self.Data = data[3:n]
case OpData:
n = 5
if len(data) < n {
return -1, io.ErrShortBuffer
}
sz := int(bin.Uint32(data[1:]))
n += sz
if len(data) < n {
return -1, io.ErrShortBuffer
}
self.Data = data[5:n]
default:
return 0, fmt.Errorf("record has unknown operation type: %d", data[0])
}
self.Type = OpType(data[0])
return
}
// Signature hash item generated from target.
type BlockHash struct {
Index uint64
WeakHash uint32
StrongHash uint64
}
const BlockHashSize = 20
// Put the serialization of this BlockHash to output
func (self BlockHash) Serialize(output []byte) {
bin.PutUint64(output, self.Index)
bin.PutUint32(output[8:], self.WeakHash)
bin.PutUint64(output[12:], self.StrongHash)
}
func (self *BlockHash) Unserialize(data []byte) (err error) {
if len(data) < 20 {
return fmt.Errorf("record too small to be a BlockHash: %d < %d", len(data), 20)
}
self.Index = bin.Uint64(data)
self.WeakHash = bin.Uint32(data[8:])
self.StrongHash = bin.Uint64(data[12:])
return
}
// Properties to use while working with the rsync algorithm.
// A single rsync should not be used concurrently as it may contain
// internal buffers and hash sums.
type rsync struct {
BlockSize int
// This must be non-nil before using any functions
hasher hash.Hash64
hasher_constructor func() hash.Hash64
checksummer_constructor func() hash.Hash
checksummer hash.Hash
checksum_done bool
buffer []byte
}
func (r *rsync) SetHasher(c func() hash.Hash64) {
r.hasher_constructor = c
r.hasher = c()
}
func (r *rsync) SetChecksummer(c func() hash.Hash) {
r.checksummer_constructor = c
r.checksummer = c()
}
// If the target length is known the number of hashes in the
// signature can be determined.
func (r *rsync) BlockHashCount(targetLength int64) (count int64) {
bs := int64(r.BlockSize)
count = targetLength / bs
if targetLength%bs != 0 {
count++
}
return
}
type signature_iterator struct {
hasher hash.Hash64
buffer []byte
src io.Reader
rc rolling_checksum
index uint64
}
// ans is valid iff err == nil
func (self *signature_iterator) next() (ans BlockHash, err error) {
n, err := io.ReadAtLeast(self.src, self.buffer, cap(self.buffer))
switch err {
case io.ErrUnexpectedEOF, io.EOF, nil:
err = nil
default:
return
}
if n == 0 {
return ans, io.EOF
}
b := self.buffer[:n]
self.hasher.Reset()
self.hasher.Write(b)
ans = BlockHash{Index: self.index, WeakHash: self.rc.full(b), StrongHash: self.hasher.Sum64()}
self.index++
return
}
// Calculate the signature of target.
func (r *rsync) CreateSignatureIterator(target io.Reader) func() (BlockHash, error) {
return (&signature_iterator{
hasher: r.hasher_constructor(), buffer: make([]byte, r.BlockSize), src: target,
}).next
}
// Apply the difference to the target.
func (r *rsync) ApplyDelta(output io.Writer, target io.ReadSeeker, op Operation) error {
var err error
var n int
var block []byte
r.set_buffer_to_size(r.BlockSize)
buffer := r.buffer
if r.checksummer == nil {
r.checksummer = r.checksummer_constructor()
}
write := func(b []byte) (err error) {
if _, err = r.checksummer.Write(b); err == nil {
_, err = output.Write(b)
}
return err
}
write_block := func(op Operation) (err error) {
if _, err = target.Seek(int64(r.BlockSize*int(op.BlockIndex)), io.SeekStart); err != nil {
return err
}
if n, err = io.ReadAtLeast(target, buffer, r.BlockSize); err != nil && err != io.ErrUnexpectedEOF {
return err
}
block = buffer[:n]
return write(block)
}
switch op.Type {
case OpBlockRange:
for i := op.BlockIndex; i <= op.BlockIndexEnd; i++ {
if err = write_block(Operation{
Type: OpBlock,
BlockIndex: i,
}); err != nil {
return err
}
}
case OpBlock:
return write_block(op)
case OpData:
return write(op.Data)
case OpHash:
actual := r.checksummer.Sum(nil)
if !bytes.Equal(actual, op.Data) {
return fmt.Errorf("Failed to verify overall file checksum actual: %s != expected: %s. This usually happens if some data was corrupted in transit or one of the involved files was altered while the transfer was in progress.", hex.EncodeToString(actual), hex.EncodeToString(op.Data))
}
r.checksum_done = true
}
return nil
}
func (r *rsync) set_buffer_to_size(sz int) {
if cap(r.buffer) < sz {
r.buffer = make([]byte, sz)
} else {
r.buffer = r.buffer[:sz]
}
}
// see https://rsync.samba.org/tech_report/node3.html
type rolling_checksum struct {
alpha, beta, val, l uint32
first_byte_of_previous_window uint32
}
func (self *rolling_checksum) full(data []byte) uint32 {
var alpha, beta uint32
self.l = uint32(len(data)) // actually should be len(data) - 1 but the equations always use l+1
for i, b := range data {
alpha += uint32(b)
beta += (self.l - uint32(i)) * uint32(b)
}
self.first_byte_of_previous_window = uint32(data[0])
self.alpha = alpha % _M
self.beta = beta % _M
self.val = self.alpha + _M*self.beta
return self.val
}
func (self *rolling_checksum) add_one_byte(first_byte, last_byte byte) {
self.alpha = (self.alpha - self.first_byte_of_previous_window + uint32(last_byte)) % _M
self.beta = (self.beta - (self.l)*self.first_byte_of_previous_window + self.alpha) % _M
self.val = self.alpha + _M*self.beta
self.first_byte_of_previous_window = uint32(first_byte)
}
type diff struct {
buffer []byte
op_write_buf [32]byte
// A single β hash may correlate with many unique hashes.
hash_lookup map[uint32][]BlockHash
source io.Reader
hasher hash.Hash64
checksummer hash.Hash
output io.Writer
window, data struct{ pos, sz int }
block_size int
finished, written bool
rc rolling_checksum
pending_op *Operation
}
func (self *diff) Next() (err error) {
return self.pump_till_op_written()
}
func (self *diff) hash(b []byte) uint64 {
self.hasher.Reset()
self.hasher.Write(b)
return self.hasher.Sum64()
}
// Combine OpBlock into OpBlockRange. To do this store the previous
// non-data operation and determine if it can be extended.
func (self *diff) send_pending() (err error) {
if self.pending_op != nil {
err = self.send_op(self.pending_op)
self.pending_op = nil
}
return
}
func (self *diff) enqueue(op Operation) (err error) {
switch op.Type {
case OpBlock:
if self.pending_op != nil {
switch self.pending_op.Type {
case OpBlock:
if self.pending_op.BlockIndex+1 == op.BlockIndex {
self.pending_op = &Operation{
Type: OpBlockRange,
BlockIndex: self.pending_op.BlockIndex,
BlockIndexEnd: op.BlockIndex,
}
return
}
case OpBlockRange:
if self.pending_op.BlockIndexEnd+1 == op.BlockIndex {
self.pending_op.BlockIndexEnd = op.BlockIndex
return
}
}
if err = self.send_pending(); err != nil {
return err
}
}
self.pending_op = &op
case OpHash:
if err = self.send_pending(); err != nil {
return
}
if err = self.send_op(&op); err != nil {
return
}
}
return
}
func (self *diff) send_op(op *Operation) error {
b := self.op_write_buf[:op.SerializeSize()]
op.Serialize(b)
self.written = true
_, err := self.output.Write(b)
return err
}
func (self *diff) send_data() error {
if self.data.sz > 0 {
if err := self.send_pending(); err != nil {
return err
}
self.written = true
data := self.buffer[self.data.pos : self.data.pos+self.data.sz]
var buf [5]byte
bin.PutUint32(buf[1:], uint32(len(data)))
buf[0] = byte(OpData)
if _, err := self.output.Write(buf[:]); err != nil {
return err
}
if _, err := self.output.Write(data); err != nil {
return err
}
self.data.pos += self.data.sz
self.data.sz = 0
}
return nil
}
func (self *diff) pump_till_op_written() error {
self.written = false
for !self.finished && !self.written {
if err := self.read_next(); err != nil {
return err
}
}
if self.finished {
if err := self.send_pending(); err != nil {
return err
}
return io.EOF
}
return nil
}
func (self *diff) ensure_idx_valid(idx int) (ok bool, err error) {
if idx < len(self.buffer) {
return true, nil
}
if idx >= cap(self.buffer) {
// need to wrap the buffer, so send off any data present behind the window
if err = self.send_data(); err != nil {
return
}
// copy the window and any data present after it to the start of the buffer
distance_from_window_pos := idx - self.window.pos
amt_to_copy := len(self.buffer) - self.window.pos
copy(self.buffer, self.buffer[self.window.pos:self.window.pos+amt_to_copy])
self.buffer = self.buffer[:amt_to_copy]
self.window.pos = 0
self.data.pos = 0
return self.ensure_idx_valid(distance_from_window_pos)
}
extra := idx - len(self.buffer) + 1
var n int
n, err = io.ReadAtLeast(self.source, self.buffer[len(self.buffer):cap(self.buffer)], extra)
block := self.buffer[len(self.buffer):][:n]
switch err {
case nil:
ok = true
self.buffer = self.buffer[:len(self.buffer)+n]
self.checksummer.Write(block)
case io.ErrUnexpectedEOF, io.EOF:
err = nil
self.buffer = self.buffer[:len(self.buffer)+n]
self.checksummer.Write(block)
}
return
}
func (self *diff) finish_up() (err error) {
if err = self.send_data(); err != nil {
return
}
self.data.pos = self.window.pos
self.data.sz = len(self.buffer) - self.window.pos
if err = self.send_data(); err != nil {
return
}
self.enqueue(Operation{Type: OpHash, Data: self.checksummer.Sum(nil)})
self.finished = true
return
}
// See https://rsync.samba.org/tech_report/node4.html for the design of this algorithm
func (self *diff) read_next() (err error) {
if self.window.sz > 0 {
if ok, err := self.ensure_idx_valid(self.window.pos + self.window.sz); !ok {
if err != nil {
return err
}
return self.finish_up()
}
self.window.pos++
self.data.sz++
self.rc.add_one_byte(self.buffer[self.window.pos], self.buffer[self.window.pos+self.window.sz-1])
} else {
if ok, err := self.ensure_idx_valid(self.window.pos + self.block_size - 1); !ok {
if err != nil {
return err
}
return self.finish_up()
}
self.window.sz = self.block_size
self.rc.full(self.buffer[self.window.pos : self.window.pos+self.window.sz])
}
found_hash := false
var block_index uint64
if hh, ok := self.hash_lookup[self.rc.val]; ok {
block_index, found_hash = find_hash(hh, self.hash(self.buffer[self.window.pos:self.window.pos+self.window.sz]))
}
if found_hash {
if err = self.send_data(); err != nil {
return
}
self.enqueue(Operation{Type: OpBlock, BlockIndex: block_index})
self.window.pos += self.window.sz
self.data.pos = self.window.pos
self.window.sz = 0
}
return nil
}
type OperationWriter struct {
Operations []Operation
expecting_data bool
}
func (self *OperationWriter) Write(p []byte) (n int, err error) {
if self.expecting_data {
self.expecting_data = false
self.Operations = append(self.Operations, Operation{Type: OpData, Data: slices.Clone(p)})
} else {
switch OpType(p[0]) {
case OpData:
self.expecting_data = true
case OpBlock, OpBlockRange, OpHash:
op := Operation{}
if n, err = op.Unserialize(p); err != nil {
return 0, err
} else if n < len(p) {
return 0, io.ErrShortWrite
}
self.Operations = append(self.Operations, op)
default:
return 0, fmt.Errorf("Unknown OpType: %d", p[0])
}
}
return
}
func (r *rsync) CreateDelta(source io.Reader, signature []BlockHash) ([]Operation, error) {
w := OperationWriter{}
it := r.CreateDiff(source, signature, &w)
for {
if err := it(); err != nil {
if err == io.EOF {
return w.Operations, nil
}
return nil, err
}
}
}
const DataSizeMultiple int = 8
func (r *rsync) CreateDiff(source io.Reader, signature []BlockHash, output io.Writer) func() error {
ans := &diff{
block_size: r.BlockSize, buffer: make([]byte, 0, (r.BlockSize * DataSizeMultiple)),
hash_lookup: make(map[uint32][]BlockHash, len(signature)),
source: source, hasher: r.hasher_constructor(),
checksummer: r.checksummer_constructor(), output: output,
}
for _, h := range signature {
key := h.WeakHash
ans.hash_lookup[key] = append(ans.hash_lookup[key], h)
}
return ans.Next
}
// Use a more unique way to identify a set of bytes.
func (r *rsync) hash(v []byte) uint64 {
r.hasher.Reset()
r.hasher.Write(v)
return r.hasher.Sum64()
}
func (r *rsync) HashSize() int { return r.hasher.Size() }
func (r *rsync) HashBlockSize() int { return r.hasher.BlockSize() }
func (r *rsync) HasHasher() bool { return r.hasher != nil }
// Searches for a given strong hash among all strong hashes in this bucket.
func find_hash(hh []BlockHash, hv uint64) (uint64, bool) {
for _, block := range hh {
if block.StrongHash == hv {
return block.Index, true
}
}
return 0, false
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
|