From b334fb64df5ed4f503a55c89b4bf4145ea8dba4c Mon Sep 17 00:00:00 2001 From: geolffreym Date: Sun, 21 Apr 2024 09:39:55 -0600 Subject: [PATCH] refactor: remove nano package --- adler32/adler32.go | 79 ---------------------------------- adler32/adler32_test.go | 93 ----------------------------------------- sync/sync.go | 8 ++-- 3 files changed, 3 insertions(+), 177 deletions(-) delete mode 100644 adler32/adler32.go delete mode 100644 adler32/adler32_test.go diff --git a/adler32/adler32.go b/adler32/adler32.go deleted file mode 100644 index f5a8c25..0000000 --- a/adler32/adler32.go +++ /dev/null @@ -1,79 +0,0 @@ -// Adler Rolling Checksum -// Based on rsync algorithm -// See also: https://rsync.samba.org/tech_report/node3.html -package adler32 - -// The sums are done modulo 65521 (the largest prime number smaller than 2^16). -const M = 65521 - -type Adler32 struct { - window []byte // A fixed size array of temporary evaluated bytes - count int // Last position - old uint8 // Last element rolled out - a, b uint16 // adler32 formula -} - -// Factory function -func New() Adler32 { - return Adler32{ - window: make([]byte, 0), - count: 0, - a: 0, - b: 0, - } -} - -// Calculate initial checksum from byte slice -func (h Adler32) Write(data []byte) Adler32 { - //https://en.wikipedia.org/wiki/Adler-32 - //https://rsync.samba.org/tech_report/node3.html - for index, char := range data { - h.a += uint16(char) - h.b += uint16(len(data)-index) * uint16(char) - h.count++ - } - - h.a %= M - h.b %= M - return h -} - -// Calculate and return Checksum -func (h Adler32) Sum() uint32 { - // Enforce 16 bits - // a = 920 = 0x398 (base 16) - // b = 4582 = 0x11E6 - // Output = 0x11E6 << 16 + 0x398 = 0x11E60398 - return uint32(h.b)<<16 | uint32(h.a)&0xFFFFF -} - -func (h Adler32) Window() []byte { return h.window } -func (h Adler32) Count() int { return h.count } -func (h Adler32) Removed() uint8 { return h.old } - -// Add byte to rolling checksum -func (h Adler32) RollIn(input byte) Adler32 { - h.a = (h.a + uint16(input)) % M - h.b = (h.b + h.a) % M - // Keep stored windows bytes while get processed - h.window = append(h.window, input) - h.count++ - return h -} - -// Substract byte from checksum -func (h Adler32) RollOut() Adler32 { - // If window is empty. Nothing to roll out! - if len(h.window) == 0 { - h.count = 0 - return h - } - - h.old = h.window[0] - h.a = (h.a - uint16(h.old)) % M - h.b = (h.b - (uint16(len(h.window)) * uint16(h.old))) % M - h.window = h.window[1:] - h.count-- - - return h -} diff --git a/adler32/adler32_test.go b/adler32/adler32_test.go deleted file mode 100644 index 03b594c..0000000 --- a/adler32/adler32_test.go +++ /dev/null @@ -1,93 +0,0 @@ -package adler32 - -import ( - "testing" -) - -func TestWriteSum(t *testing.T) { - rolling := New().Write([]byte("how are you doing")) - w0 := rolling.Sum() - - if 944178772 != w0 { - t.Errorf("Expected 944178772 as hash for input text") - } - - if 17 != rolling.Count() { - t.Errorf("Expected 17 as output for current window") - } -} - -func TestWindowOverflow(t *testing.T) { - rolling := New().Write([]byte("abcdef")). - RollOut(). // remove a - RollOut(). // remove b - RollOut(). // remove c - RollOut(). // remove d - RollOut(). // remove e - RollOut(). // remove f - RollOut() // overflow - - if rolling.Count() > 0 { - t.Errorf("Expected count equal 0") - } -} - -func TestRollIn(t *testing.T) { - rolling := New() - - w0 := rolling.Write([]byte("ow are you doing")).Sum() - w1 := rolling. - RollIn('o'). - RollIn('w'). - RollIn(' '). - RollIn('a'). - RollIn('r'). - RollIn('e'). - RollIn(' '). - RollIn('y'). - RollIn('o'). - RollIn('u'). - RollIn(' '). - RollIn('d'). - RollIn('o'). - RollIn('i'). - RollIn('n'). - RollIn('g'). - Sum() - - if w0 != w1 { - t.Errorf("Expected same hash for same input after RolledIn bytes") - } - -} - -func TestRollOut(t *testing.T) { - rolling := New() - - w0 := rolling.Write([]byte("w are you doing")).Sum() - w1 := rolling.RollIn('h'). - RollIn('o'). - RollIn('w'). - RollIn(' '). - RollIn('a'). - RollIn('r'). - RollIn('e'). - RollIn(' '). - RollIn('y'). - RollIn('o'). - RollIn('u'). - RollIn(' '). - RollIn('d'). - RollIn('o'). - RollIn('i'). - RollIn('n'). - RollIn('g'). - RollOut(). // remove h - RollOut(). // remove o - Sum() - - if w0 != w1 { - t.Errorf("Expected same hash for same text after RolledOut byte") - } - -} diff --git a/sync/sync.go b/sync/sync.go index 180cd94..b7e898e 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -8,8 +8,6 @@ import ( "crypto/sha1" "encoding/hex" "io" - - "github.com/geolffreym/rolling-sync/adler32" ) const S = 16 @@ -67,7 +65,7 @@ func strong(block []byte) string { // Calc and return weak adler32 checksum func weak(block []byte) uint32 { - weak := adler32.New() + weak := NewAdler32() return weak.Write(block).Sum() } @@ -155,7 +153,7 @@ func (s *Sync) IntegrityCheck(sig []Table, matches Delta) Delta { // diff matches for block and the map key keep the block position. func (s *Sync) Delta(sig []Table, reader *bufio.Reader) Delta { // Weak checksum adler32 - weak := adler32.New() + weak := NewAdler32() // Delta matches delta := make(Delta) // Indexes for block position @@ -200,7 +198,7 @@ func (s *Sync) Delta(sig []Table, reader *bufio.Reader) Delta { delta.Add(index, newBlock) // Add new block to delta matches // Clear garbage collectable tmpLitMatches = tmpLitMatches[:0] // clear tmp literal matches - weak = adler32.New() // replace weak adler object + weak = NewAdler32() // replace weak adler object } }