Skip to content

Commit

Permalink
xz: add support for check-ID None to Writer
Browse files Browse the repository at this point in the history
So far we didn't support the check-ID None for Writer. To request it I
added a NoCheckSum boolean to the configuration, because CRC64 should
still be used as a default.

The commit is related to issue #27 - #27
  • Loading branch information
ulikunitz committed Feb 24, 2020
1 parent ef77b79 commit 2c0b983
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
13 changes: 11 additions & 2 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ type WriterConfig struct {
DictCap int
BufSize int
BlockSize int64
// checksum method: CRC32, CRC64 or SHA256
// checksum method: CRC32, CRC64 or SHA256 (default: CRC64)
CheckSum byte
// Forces NoChecksum (default: false)
NoCheckSum bool
// match algorithm
Matcher lzma.MatchAlgorithm
}
Expand All @@ -41,6 +43,9 @@ func (c *WriterConfig) fill() {
if c.CheckSum == 0 {
c.CheckSum = CRC64
}
if c.NoCheckSum {
c.CheckSum = None
}
}

// Verify checks the configuration for errors. Zero values will be
Expand Down Expand Up @@ -284,7 +289,11 @@ func (c *WriterConfig) newBlockWriter(xz io.Writer, hash hash.Hash) (bw *blockWr
if err != nil {
return nil, err
}
bw.mw = io.MultiWriter(bw.w, bw.hash)
if bw.hash.Size() != 0 {
bw.mw = io.MultiWriter(bw.w, bw.hash)
} else {
bw.mw = bw.w
}
return bw, nil
}

Expand Down
39 changes: 39 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,42 @@ func TestWriter2(t *testing.T) {
t.Fatal("decompressed data differs from original")
}
}

func TestWriterNoneCheck(t *testing.T) {
const txtlen = 1023
var buf bytes.Buffer
io.CopyN(&buf, randtxt.NewReader(rand.NewSource(41)), txtlen)
txt := buf.String()

buf.Reset()
w, err := WriterConfig{NoCheckSum: true}.NewWriter(&buf)
if err != nil {
t.Fatalf("NewWriter error %s", err)
}
n, err := io.WriteString(w, txt)
if err != nil {
t.Fatalf("WriteString error %s", err)
}
if n != len(txt) {
t.Fatalf("WriteString wrote %d bytes; want %d", n, len(txt))
}
if err = w.Close(); err != nil {
t.Fatalf("Close error %s", err)
}
t.Logf("buf.Len() %d", buf.Len())
r, err := NewReader(&buf)
if err != nil {
t.Fatalf("NewReader error %s", err)
}
var out bytes.Buffer
k, err := io.Copy(&out, r)
if err != nil {
t.Fatalf("Decompressing copy error %s after %d bytes", err, n)
}
if k != txtlen {
t.Fatalf("Decompression data length %d; want %d", k, txtlen)
}
if txt != out.String() {
t.Fatal("decompressed data differs from original")
}
}

0 comments on commit 2c0b983

Please sign in to comment.