2009-11-04 18:55:06 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package patch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes";
|
|
|
|
"compress/zlib";
|
|
|
|
"crypto/sha1";
|
|
|
|
"encoding/git85";
|
|
|
|
"fmt";
|
|
|
|
"io";
|
|
|
|
"os";
|
|
|
|
)
|
|
|
|
|
|
|
|
func gitSHA1(data []byte) []byte {
|
|
|
|
if len(data) == 0 {
|
|
|
|
// special case: 0 length is all zeros sum
|
2009-11-09 13:07:39 -07:00
|
|
|
return make([]byte, 20)
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
h := sha1.New();
|
|
|
|
fmt.Fprintf(h, "blob %d\x00", len(data));
|
|
|
|
h.Write(data);
|
|
|
|
return h.Sum();
|
|
|
|
}
|
|
|
|
|
2009-11-11 18:31:28 -07:00
|
|
|
// BUG(rsc): The Git binary delta format is not implemented, only Git binary literals.
|
2009-11-04 18:55:06 -07:00
|
|
|
|
2009-11-11 18:31:28 -07:00
|
|
|
// GitBinaryLiteral represents a Git binary literal diff.
|
|
|
|
type GitBinaryLiteral struct {
|
2009-11-04 18:55:06 -07:00
|
|
|
OldSHA1 []byte; // if non-empty, the SHA1 hash of the original
|
|
|
|
New []byte; // the new contents
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply implements the Diff interface's Apply method.
|
2009-11-11 18:31:28 -07:00
|
|
|
func (d *GitBinaryLiteral) Apply(old []byte) ([]byte, os.Error) {
|
2009-11-04 18:55:06 -07:00
|
|
|
if sum := gitSHA1(old); !bytes.HasPrefix(sum, d.OldSHA1) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, ErrPatchFailure
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
return d.New, nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
func unhex(c byte) uint8 {
|
|
|
|
switch {
|
|
|
|
case '0' <= c && c <= '9':
|
2009-11-09 22:23:52 -07:00
|
|
|
return c - '0'
|
2009-11-04 18:55:06 -07:00
|
|
|
case 'a' <= c && c <= 'f':
|
2009-11-09 22:23:52 -07:00
|
|
|
return c - 'a' + 10
|
2009-11-04 18:55:06 -07:00
|
|
|
case 'A' <= c && c <= 'F':
|
2009-11-09 22:23:52 -07:00
|
|
|
return c - 'A' + 10
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHex(s []byte) (data []byte, rest []byte) {
|
|
|
|
n := 0;
|
|
|
|
for n < len(s) && unhex(s[n]) != 255 {
|
2009-11-09 13:07:39 -07:00
|
|
|
n++
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
n &^= 1; // Only take an even number of hex digits.
|
|
|
|
data = make([]byte, n/2);
|
|
|
|
for i := range data {
|
2009-11-09 22:23:52 -07:00
|
|
|
data[i] = unhex(s[2*i])<<4 | unhex(s[2*i+1])
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
rest = s[n:len(s)];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-11 18:31:28 -07:00
|
|
|
// ParseGitBinary parses raw as a Git binary patch.
|
|
|
|
func ParseGitBinary(raw []byte) (Diff, os.Error) {
|
2009-11-04 18:55:06 -07:00
|
|
|
var oldSHA1, newSHA1 []byte;
|
|
|
|
var sawBinary bool;
|
|
|
|
|
|
|
|
for {
|
|
|
|
var first []byte;
|
|
|
|
first, raw, _ = getLine(raw, 1);
|
|
|
|
first = bytes.TrimSpace(first);
|
|
|
|
if s, ok := skip(first, "index "); ok {
|
|
|
|
oldSHA1, s = getHex(s);
|
|
|
|
if s, ok = skip(s, ".."); !ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
continue
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
newSHA1, s = getHex(s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if _, ok := skip(first, "GIT binary patch"); ok {
|
|
|
|
sawBinary = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if n, _, ok := atoi(first, "literal ", 10); ok && sawBinary {
|
|
|
|
data := make([]byte, n);
|
|
|
|
d := git85.NewDecoder(bytes.NewBuffer(raw));
|
|
|
|
z, err := zlib.NewInflater(d);
|
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, err
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
defer z.Close();
|
|
|
|
if _, err = io.ReadFull(z, data); err != nil {
|
|
|
|
if err == os.EOF {
|
2009-11-09 13:07:39 -07:00
|
|
|
err = io.ErrUnexpectedEOF
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
return nil, err;
|
|
|
|
}
|
|
|
|
var buf [1]byte;
|
|
|
|
m, err := z.Read(&buf);
|
|
|
|
if m != 0 || err != os.EOF {
|
2009-11-11 18:31:28 -07:00
|
|
|
return nil, os.NewError("Git binary literal longer than expected")
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if sum := gitSHA1(data); !bytes.HasPrefix(sum, newSHA1) {
|
2009-11-11 18:31:28 -07:00
|
|
|
return nil, os.NewError("Git binary literal SHA1 mismatch")
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
2009-11-11 18:31:28 -07:00
|
|
|
return &GitBinaryLiteral{oldSHA1, data}, nil;
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
if !sawBinary {
|
2009-11-11 18:31:28 -07:00
|
|
|
return nil, os.NewError("unexpected Git patch header: " + string(first))
|
2009-11-04 18:55:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("unreachable");
|
|
|
|
}
|