Skip to content

Commit

Permalink
binary packages
Browse files Browse the repository at this point in the history
  • Loading branch information
gabstv committed Feb 25, 2019
1 parent a592625 commit 0ee885d
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 64 deletions.
23 changes: 23 additions & 0 deletions cmd/bsdiff/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"os"

"github.com/gabstv/go-bsdiff/pkg/bsdiff"
)

func main() {
if len(os.Args) != 4 {
printusage(1)
}
err := bsdiff.File(os.Args[1], os.Args[2], os.Args[3])
if err != nil {
println(err.Error())
printusage(1)
}
}

func printusage(exitcode int) {
println("usage: " + os.Args[0] + " oldfile newfile patchfile")
os.Exit(exitcode)
}
23 changes: 23 additions & 0 deletions cmd/bspatch/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"os"

"github.com/gabstv/go-bsdiff/pkg/bspatch"
)

func main() {
if len(os.Args) != 4 {
printusage(1)
}
err := bspatch.File(os.Args[1], os.Args[2], os.Args[3])
if err != nil {
println(err.Error())
printusage(1)
}
}

func printusage(exitcode int) {
println("usage: " + os.Args[0] + " oldfile newfile patchfile")
os.Exit(exitcode)
}
74 changes: 10 additions & 64 deletions pkg/bsdiff/bsdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ import (
"io/ioutil"

"github.com/dsnet/compress/bzip2"
)

const (
buffersize = 1024 * 16
"github.com/gabstv/go-bsdiff/pkg/util"
)

// https://github.com/cnSchwarzer/bsdiff-win/blob/master/bsdiff-win/bsdiff.c
Expand All @@ -60,24 +57,27 @@ func Reader(oldbin io.Reader, newbin io.Reader, patchf io.Writer) error {
if err != nil {
return err
}
return putWriter(patchf, diffbytes)
return util.PutWriter(patchf, diffbytes)
}

// File reads the old and new files to create a diff patch file
func File(oldfile, newfile, patchfile string) error {
oldbs, err := ioutil.ReadFile(oldfile)
if err != nil {
return err
return fmt.Errorf("could not read oldfile '%v': %v", oldfile, err.Error())
}
newbs, err := ioutil.ReadFile(newfile)
if err != nil {
return err
return fmt.Errorf("could not read newfile '%v': %v", newfile, err.Error())
}
diffbytes, err := diffb(oldbs, newbs)
if err != nil {
return err
return fmt.Errorf("bsdiff: %v", err.Error())
}
if err := ioutil.WriteFile(patchfile, diffbytes, 0644); err != nil {
return fmt.Errorf("could create patchfile '%v': %v", patchfile, err.Error())
}
return ioutil.WriteFile(patchfile, diffbytes, 0644)
return nil
}

// REVIEW OK
Expand Down Expand Up @@ -326,7 +326,7 @@ func search(iii []int, oldbin []byte, newbin []byte, st, en int, pos *int) int {
}

x = st + (en-st)/2
cmpln := min(oldsize-iii[x], newsize)
cmpln := util.Min(oldsize-iii[x], newsize)
// xxx = oldbin[iii[x]:]
if bytes.Compare(oldbin[iii[x]:iii[x]+cmpln], newbin[:cmpln]) < 0 {
return search(iii, oldbin, newbin, x, en, pos)
Expand Down Expand Up @@ -530,57 +530,3 @@ func split(iii, vvv []int, start, ln, h int) {
split(iii, vvv, kk, start+ln-kk, h)
}
}

func putWriter(target io.Writer, b []byte) error {
lb := len(b)
if lb < buffersize {
n, err := target.Write(b)
if err != nil {
return err
}
if lb != n {
return fmt.Errorf("%v of %v bytes written", n, lb)
}
return nil
}
offs := 0

for offs < lb {
n := min(buffersize, lb-offs)
n2, err := target.Write(b[offs:n])
if err != nil {
return err
}
if n2 != n {
return fmt.Errorf("%v of %v bytes written", offs+n2, lb)
}
offs += n
}
return nil
}

func copyReader(target []byte, rdr io.Reader) error {
offs := 0
buf := make([]byte, buffersize)
for {
nread, err := rdr.Read(buf)
if nread > 0 {
copy(target[offs:], buf[:nread])
offs += nread
}
if err != nil {
if err == io.EOF {
return nil
}
return err
}
}
return nil
}

func min(a, b int) int {
if a < b {
return a
}
return b
}
39 changes: 39 additions & 0 deletions pkg/bspatch/bspatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,54 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"

"github.com/dsnet/compress/bzip2"
"github.com/gabstv/go-bsdiff/pkg/util"
)

// Bytes applies a patch with the oldfile to create the newfile
func Bytes(oldfile, patch []byte) (newfile []byte, err error) {
return patchb(oldfile, patch)
}

// Reader applies a BSDIFF4 patch (using oldbin and patchf) to create the newbin
func Reader(oldbin io.Reader, newbin io.Writer, patchf io.Reader) error {
oldbs, err := ioutil.ReadAll(oldbin)
if err != nil {
return err
}
diffbytes, err := ioutil.ReadAll(patchf)
if err != nil {
return err
}
newbs, err := patchb(oldbs, diffbytes)
if err != nil {
return err
}
return util.PutWriter(newbin, newbs)
}

// File applies a BSDIFF4 patch (using oldfile and patchfile) to create the newfile
func File(oldfile, newfile, patchfile string) error {
oldbs, err := ioutil.ReadFile(oldfile)
if err != nil {
return fmt.Errorf("could not read oldfile '%v': %v", oldfile, err.Error())
}
patchbs, err := ioutil.ReadFile(newfile)
if err != nil {
return fmt.Errorf("could not read patchfile '%v': %v", patchfile, err.Error())
}
newbytes, err := patchb(oldbs, patchbs)
if err != nil {
return fmt.Errorf("bspatch: %v", err.Error())
}
if err := ioutil.WriteFile(newfile, newbytes, 0644); err != nil {
return fmt.Errorf("could not create newfile '%v': %v", newfile, err.Error())
}
return nil
}

func patchb(oldfile, patch []byte) ([]byte, error) {
oldsize := len(oldfile)
var newsize int
Expand Down
47 changes: 47 additions & 0 deletions pkg/util/io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package util

import (
"fmt"
"io"
)

const (
buffersize = 1024 * 16
)

// PutWriter writes all content from b to target
func PutWriter(target io.Writer, b []byte) error {
lb := len(b)
if lb < buffersize {
n, err := target.Write(b)
if err != nil {
return err
}
if lb != n {
return fmt.Errorf("%v of %v bytes written", n, lb)
}
return nil
}
offs := 0

for offs < lb {
n := Min(buffersize, lb-offs)
n2, err := target.Write(b[offs:n])
if err != nil {
return err
}
if n2 != n {
return fmt.Errorf("%v of %v bytes written", offs+n2, lb)
}
offs += n
}
return nil
}

// Min int
func Min(a, b int) int {
if a < b {
return a
}
return b
}

0 comments on commit 0ee885d

Please sign in to comment.