Skip to content

Commit 43111cb

Browse files
committed
fix encoding of negative offsets
1 parent cf79655 commit 43111cb

File tree

6 files changed

+64
-2
lines changed

6 files changed

+64
-2
lines changed

encoding.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package binarydist
2+
3+
// SMLE is the numeric encoding used by the bsdiff tools.
4+
// It implements binary.ByteOrder using a sign-magnitude format
5+
// and little-endian byte order. Only methods Uint64, String,
6+
// and GoString have been written; the rest panic.
7+
var SMLE smle
8+
9+
type smle struct{}
10+
11+
func (smle) Uint16(b []byte) uint16 { panic("unimplemented") }
12+
13+
func (smle) PutUint16(b []byte, v uint16) { panic("unimplemented") }
14+
15+
func (smle) Uint32(b []byte) uint32 { panic("unimplemented") }
16+
17+
func (smle) PutUint32(b []byte, v uint32) { panic("unimplemented") }
18+
19+
func (smle) Uint64(b []byte) uint64 {
20+
y := int64(b[0]) |
21+
int64(b[1])<<8 |
22+
int64(b[2])<<16 |
23+
int64(b[3])<<24 |
24+
int64(b[4])<<32 |
25+
int64(b[5])<<40 |
26+
int64(b[6])<<48 |
27+
int64(b[7]&0x7f)<<56
28+
29+
if b[7]&0x80 != 0 {
30+
y = -y
31+
}
32+
return uint64(y)
33+
}
34+
35+
func (smle) PutUint64(b []byte, v uint64) { panic("unimplemented") }
36+
37+
func (smle) String() string { return "SMLE" }
38+
39+
func (smle) GoString() string { return "binarydist.SMLE" }

patch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func Patch(old io.Reader, new io.Writer, patch io.Reader) error {
3737
DiffLen int64
3838
NewSize int64
3939
}
40-
err := binary.Read(patch, binary.LittleEndian, &header)
40+
err := binary.Read(patch, SMLE, &header)
4141
if err != nil {
4242
return err
4343
}
@@ -75,7 +75,7 @@ func Patch(old io.Reader, new io.Writer, patch io.Reader) error {
7575
var oldpos, newpos int64
7676
for newpos < header.NewSize {
7777
var ctrl struct{ Add, Copy, Seek int64 }
78-
err = binary.Read(cpfbz2, binary.LittleEndian, &ctrl)
78+
err = binary.Read(cpfbz2, SMLE, &ctrl)
7979
if err != nil {
8080
return err
8181
}

patch_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,26 @@ func TestPatch(t *testing.T) {
3737
t.Fatalf("produced different output at pos %d", n)
3838
}
3939
}
40+
41+
func TestPatchHk(t *testing.T) {
42+
got, err := ioutil.TempFile("/tmp", "bspatch.")
43+
if err != nil {
44+
panic(err)
45+
}
46+
os.Remove(got.Name())
47+
48+
err = Patch(mustOpen("testdata/sample.old"), got, mustOpen("testdata/sample.patch"))
49+
if err != nil {
50+
t.Fatal("err", err)
51+
}
52+
53+
ref, err := got.Seek(0, 2)
54+
if err != nil {
55+
panic(err)
56+
}
57+
58+
t.Logf("got %d bytes", ref)
59+
if n := fileCmp(got, mustOpen("testdata/sample.new")); n > -1 {
60+
t.Fatalf("produced different output at pos %d", n)
61+
}
62+
}

testdata/sample.new

9.77 KB
Binary file not shown.

testdata/sample.old

10.7 KB
Binary file not shown.

testdata/sample.patch

1.06 KB
Binary file not shown.

0 commit comments

Comments
 (0)