Skip to content

Commit

Permalink
fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Ochsenhofer authored and Gabriel Ochsenhofer committed Mar 20, 2019
1 parent 039022e commit f093062
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
26 changes: 24 additions & 2 deletions pkg/bspatch/bspatch.go
Expand Up @@ -172,7 +172,8 @@ func patchb(oldfile, patch []byte) ([]byte, error) {
}

// Read diff string
lenread, err = dpfbz2.Read(pnew[newpos : newpos+ctrl[0]])
// lenread, err = dpfbz2.Read(pnew[newpos : newpos+ctrl[0]])
lenread, err = bzreadall(dpfbz2, pnew[newpos:newpos+ctrl[0]], ctrl[0])
if lenread < ctrl[0] || (err != nil && err != io.EOF) {
e0 := ""
if err != nil {
Expand All @@ -197,7 +198,9 @@ func patchb(oldfile, patch []byte) ([]byte, error) {
}

// Read extra string
lenread, err = epfbz2.Read(pnew[newpos : newpos+ctrl[1]])
// epfbz2.Read was not reading all the requested bytes, probably an internal buffer limitation ?
// it was encapsulated by bzreadall to work around the issue
lenread, err = bzreadall(epfbz2, pnew[newpos:newpos+ctrl[1]], ctrl[1])
if lenread < ctrl[1] || (err != nil && err != io.EOF) {
e0 := ""
if err != nil {
Expand Down Expand Up @@ -254,3 +257,22 @@ func offtin(buf []byte) int {
}
return y
}

func bzreadall(r io.Reader, b []byte, expected int) (int, error) {
var allread int
var offset int
for {
nread, err := r.Read(b[offset:])
if nread == expected {
return nread, err
}
if err != nil {
return allread + nread, err
}
allread += nread
if allread >= expected {
return allread, nil
}
offset += nread
}
}
40 changes: 40 additions & 0 deletions pkg/bspatch/bspatch_test.go
Expand Up @@ -249,3 +249,43 @@ func TestCorruptHeader(t *testing.T) {
t.Fatal("header should be corrupt (6)")
}
}

type lowcaprdr struct {
read []byte
n int
}

func (r *lowcaprdr) Read(b []byte) (int, error) {
if len(b) > 8 {
copy(r.read[r.n:], b[:8])
r.n += 8
return 8, nil
}
copy(r.read[r.n:], b)
r.n += len(b)
return len(b), nil
}

func TestBZReadAll(t *testing.T) {
buf := []byte{
0x10, 0x10, 0x10, 0x10, 0x20, 0x20, 0x20, 0x20,
0x30, 0x30, 0x30, 0x30, 0x40, 0x40, 0x40, 0x40,
0x43,
}
rr := &lowcaprdr{
read: make([]byte, 1024),
}
nr, err := bzreadall(rr, buf, len(buf))
if err != nil {
t.Fail()
}
if nr != len(buf) {
t.Fail()
}
if buf[16] != rr.read[16] {
t.Fail()
}
if buf[7] != rr.read[7] {
t.Fail()
}
}

0 comments on commit f093062

Please sign in to comment.