Skip to content

Commit

Permalink
go doc
Browse files Browse the repository at this point in the history
  • Loading branch information
gabstv committed Feb 25, 2019
1 parent e381fe0 commit b1fdfb7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 64 deletions.
4 changes: 1 addition & 3 deletions pkg/bsdiff/bsdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ import (
"github.com/gabstv/go-bsdiff/pkg/util"
)

// https://github.com/cnSchwarzer/bsdiff-win/blob/master/bsdiff-win/bsdiff.c

// Bytes takes the old and new byte slices and outputs the diff
func Bytes(oldbs, newbs []byte) ([]byte, error) {
return diffb(oldbs, newbs)
Expand Down Expand Up @@ -94,7 +92,7 @@ func diffb(oldbin, newbin []byte) ([]byte, error) {
var dblen, eblen int

// create the patch file
pf := new(BufWriter)
pf := new(util.BufWriter)

// Header is
// 0 8 "BSDIFF40"
Expand Down
61 changes: 0 additions & 61 deletions pkg/bsdiff/writer.go

This file was deleted.

Empty file added pkg/bspatch/bspatch_test.go
Empty file.
60 changes: 60 additions & 0 deletions pkg/util/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"fmt"
"io"
"sync"
)

const (
Expand Down Expand Up @@ -45,3 +46,62 @@ func Min(a, b int) int {
}
return b
}

// BufWriter is byte slice buffer that implements io.WriteSeeker
type BufWriter struct {
lock sync.Mutex
buf []byte
pos int
}

// Write the contents of p and return the bytes written
func (m *BufWriter) Write(p []byte) (n int, err error) {
m.lock.Lock()
defer m.lock.Unlock()
if m.buf == nil {
m.buf = make([]byte, 0)
m.pos = 0
}
minCap := m.pos + len(p)
if minCap > cap(m.buf) { // Make sure buf has enough capacity:
buf2 := make([]byte, len(m.buf), minCap+len(p)) // add some extra
copy(buf2, m.buf)
m.buf = buf2
}
if minCap > len(m.buf) {
m.buf = m.buf[:minCap]
}
copy(m.buf[m.pos:], p)
m.pos += len(p)
return len(p), nil
}

// Seek to a position on the byte slice
func (m *BufWriter) Seek(offset int64, whence int) (int64, error) {
newPos, offs := 0, int(offset)
switch whence {
case io.SeekStart:
newPos = offs
case io.SeekCurrent:
newPos = m.pos + offs
case io.SeekEnd:
newPos = len(m.buf) + offs
}
if newPos < 0 {
return 0, fmt.Errorf("negative result pos")
}
m.pos = newPos
return int64(newPos), nil
}

// Len returns the length of the internal byte slice
func (m *BufWriter) Len() int {
return len(m.buf)
}

// Bytes return a copy of the internal byte slice
func (m *BufWriter) Bytes() []byte {
b2 := make([]byte, len(m.buf))
copy(b2, m.buf)
return b2
}

0 comments on commit b1fdfb7

Please sign in to comment.