diff options
Diffstat (limited to 'librsync')
-rw-r--r-- | librsync/librsync.go | 58 | ||||
-rw-r--r-- | librsync/librsync_test.go | 9 | ||||
-rw-r--r-- | librsync/testdata/random_data.sig.go | 30 | ||||
-rw-r--r-- | librsync/testdata/random_data.sig2 | bin | 0 -> 156 bytes |
4 files changed, 83 insertions, 14 deletions
diff --git a/librsync/librsync.go b/librsync/librsync.go index 641e480..1ffc7ec 100644 --- a/librsync/librsync.go +++ b/librsync/librsync.go @@ -6,6 +6,7 @@ package librsync #include <stdio.h> #include <librsync.h> #include <stdlib.h> +#include <stdbool.h> static inline rs_buffers_t* new_rs_buffers() { return (rs_buffers_t*) malloc(sizeof(rs_buffers_t)); @@ -17,6 +18,27 @@ rs_result patchCallback(void* _patcher, rs_long_t pos, size_t* len, void** _buf) return patchCallbackGo(_patcher, pos, len, _buf); } +#ifndef RS_DEFAULT_STRONG_LEN +// librsync >= 1.0.0, 0 is the full size (32 bytes) +#define DEFAULT_STRONG_LEN 0 +#else +// librsync < 1.0.0, using md4 (8 bytes) +#define DEFAULT_STRONG_LEN RS_DEFAULT_STRONG_LEN +#endif + +static inline rs_job_t* sig_begin(size_t new_block_len, size_t strong_sum_len, bool compat) { +#ifndef RS_DEFAULT_STRONG_LEN + // librsync >= 1.0.0, supporting the newer hash function (blake2b) + if (compat) { + return rs_sig_begin(new_block_len, strong_sum_len, RS_MD4_SIG_MAGIC); + } + return rs_sig_begin(new_block_len, strong_sum_len, RS_BLAKE2_SIG_MAGIC); +#else + // not supporting the newer hash function, fall back to the md4 hash + return rs_sig_begin(new_block_len, strong_sum_len); +#endif +} + */ import "C" @@ -32,6 +54,11 @@ const ( outbufSize = 16 * 1024 ) +const ( + DefaultBlockLen = C.RS_DEFAULT_BLOCK_LEN + DefaultStrongLen = C.DEFAULT_STRONG_LEN +) + var ( ErrInputEnded = errors.New("Input ended (possibly unexpected)") ErrBadMagic = errors.New("Bad magic number. Probably not an librsync file.") @@ -74,24 +101,43 @@ func newJob(input io.Reader) (job *Job, err error) { return } -// NewDefaultSignatureGen is like NewSignatureGen, but uses default values for blocklen and stronglen. +// Config sets parameters for NewSignatureGen. May be the zero value for default +// values. +type Config struct { + BlockLen uint // length of a block, e.g. 2048 + StrongLen uint // length of a strong hash, e.g. 32 or 0 + CompatMD4 bool // enable for compatibility with librsync < 1.0.0 +} + +func (c *Config) setup() { + if c.BlockLen == 0 { + c.BlockLen = DefaultBlockLen + } + if c.StrongLen == 0 { + c.StrongLen = DefaultStrongLen + } +} + +// NewDefaultSignatureGen is like NewSignatureGen, but uses the default +// configuration. func NewDefaultSignatureGen(basis io.Reader) (job *Job, err error) { - job, err = NewSignatureGen(C.RS_DEFAULT_BLOCK_LEN, C.RS_DEFAULT_STRONG_LEN, basis) + job, err = NewSignatureGen(Config{}, basis) return } // NewSignatureGen creates a signature generation job. // -// blocklen is the length of a block. -// stronglen is the length of the stong hash. +// config is a Config object for more options. // basis is an io.Reader that provides data of the basis file. -func NewSignatureGen(blocklen, stronglen uint, basis io.Reader) (job *Job, err error) { +func NewSignatureGen(config Config, basis io.Reader) (job *Job, err error) { job, err = newJob(basis) if err != nil { return } - job.job = C.rs_sig_begin(C.size_t(blocklen), C.size_t(stronglen)) + config.setup() + + job.job = C.sig_begin(C.size_t(config.BlockLen), C.size_t(config.StrongLen), C.bool(config.CompatMD4)) if job.job == nil { job.Close() return nil, errors.New("rs_sig_begin failed") diff --git a/librsync/librsync_test.go b/librsync/librsync_test.go index 7c6a278..c291fac 100644 --- a/librsync/librsync_test.go +++ b/librsync/librsync_test.go @@ -22,7 +22,14 @@ func TestSignatureDeltaPatch(t *testing.T) { t.Fatalf("Creating the signature failed: %s", err) } - if !bytes.Equal(sigbuf.Bytes(), testdata.RandomDataSig()) { + matches := false + // Check both possible signatures. + for _, sigcheck := range testdata.RandomDataSig() { + if bytes.Equal(sigbuf.Bytes(), sigcheck) { + matches = true + } + } + if !matches { if path, err := dump(sigbuf); err == nil { t.Fatalf("Signatures do not match. Generated signature dumped to %s", path) } else { diff --git a/librsync/testdata/random_data.sig.go b/librsync/testdata/random_data.sig.go index 8822edf..e5e57a3 100644 --- a/librsync/testdata/random_data.sig.go +++ b/librsync/testdata/random_data.sig.go @@ -1,12 +1,28 @@ package testdata -func RandomDataSig() []byte { +func RandomDataSig() [][]byte { // Was generated with `rdiff signature random_data random_data.sig` - return []byte{ - 0x72, 0x73, 0x01, 0x36, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x08, - 0x60, 0xcb, 0xf7, 0x64, 0x2e, 0xbe, 0x39, 0x5e, 0x1a, 0xf2, 0x77, 0x07, - 0xf5, 0x8f, 0xf3, 0xdc, 0x1d, 0xcc, 0xe0, 0x3b, 0x3b, 0xc9, 0x7e, 0x0d, - 0x2e, 0xcf, 0xed, 0xc5, 0x09, 0x1f, 0xbb, 0x75, 0x27, 0xbb, 0x46, 0xa9, - 0x86, 0x26, 0x04, 0xa1, 0x9d, 0x4d, 0xa3, 0x7a, 0xfa, 0xdd, 0x5d, 0xc5, + return [][]byte{ + { + 0x72, 0x73, 0x01, 0x36, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x08, + 0x60, 0xcb, 0xf7, 0x64, 0x2e, 0xbe, 0x39, 0x5e, 0x1a, 0xf2, 0x77, 0x07, + 0xf5, 0x8f, 0xf3, 0xdc, 0x1d, 0xcc, 0xe0, 0x3b, 0x3b, 0xc9, 0x7e, 0x0d, + 0x2e, 0xcf, 0xed, 0xc5, 0x09, 0x1f, 0xbb, 0x75, 0x27, 0xbb, 0x46, 0xa9, + 0x86, 0x26, 0x04, 0xa1, 0x9d, 0x4d, 0xa3, 0x7a, 0xfa, 0xdd, 0x5d, 0xc5, + }, { + 0x72, 0x73, 0x01, 0x37, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x20, + 0x60, 0xcb, 0xf7, 0x64, 0x3a, 0x2b, 0x95, 0x58, 0xad, 0xd8, 0xa9, 0x71, + 0x70, 0x7d, 0x38, 0x64, 0xd5, 0xff, 0xa2, 0xc1, 0xae, 0xdf, 0x62, 0x76, + 0x7a, 0x84, 0xf8, 0xa5, 0xc7, 0x68, 0xd9, 0xbd, 0x04, 0xea, 0x09, 0x2b, + 0xf5, 0x8f, 0xf3, 0xdc, 0x13, 0x67, 0x5e, 0x50, 0xd9, 0xd3, 0x38, 0x47, + 0x1c, 0xb9, 0x27, 0x60, 0x26, 0x8d, 0x23, 0x39, 0x47, 0x0e, 0x57, 0x2c, + 0xfd, 0x2a, 0x27, 0xf1, 0xab, 0xee, 0x88, 0xe6, 0x74, 0x60, 0x78, 0xf8, + 0x2e, 0xcf, 0xed, 0xc5, 0x3b, 0xef, 0xab, 0x41, 0x13, 0x93, 0x0e, 0x26, + 0x38, 0x79, 0xc6, 0xed, 0x4d, 0xce, 0x65, 0xcd, 0x02, 0x5f, 0x2f, 0x19, + 0x42, 0x4a, 0x2d, 0xac, 0x8f, 0xb1, 0x40, 0xa2, 0x3c, 0xfc, 0x90, 0x66, + 0x86, 0x26, 0x04, 0xa1, 0x08, 0x25, 0xe4, 0x2c, 0xf7, 0x77, 0xa8, 0x7e, + 0x8f, 0xbf, 0xe9, 0xdf, 0x7a, 0x2d, 0x89, 0xc3, 0xe8, 0x15, 0x65, 0x46, + 0xa2, 0xe0, 0x33, 0x38, 0xc9, 0x73, 0xf0, 0xc3, 0x20, 0x79, 0x5a, 0x11, + }, } } diff --git a/librsync/testdata/random_data.sig2 b/librsync/testdata/random_data.sig2 Binary files differnew file mode 100644 index 0000000..684eacc --- /dev/null +++ b/librsync/testdata/random_data.sig2 |