diff --git a/lzop/lzop.go b/lzop/lzop.go index f9c1ada..2a3040e 100644 --- a/lzop/lzop.go +++ b/lzop/lzop.go @@ -55,28 +55,15 @@ func WriteHeader(buff *bytes.Buffer, fileTime int64, fileName string) error { func WriteBytes(buff *bytes.Buffer, data []byte, compressionFunction func([]byte) []byte) error { blockSize := 256 * 1024 - if len(data) < blockSize { - blockSize = len(data) - } - - iterations := len(data) / blockSize - for i := 0; i < iterations+1; i++ { - - var compressed []byte - var unCompressed []byte - - leftOver := len(data) - (i * blockSize) - if leftOver < blockSize { - unCompressed = data[(i * blockSize):] - } else { - unCompressed = data[(i * blockSize):((i + 1) * blockSize)] - } + for i, n := 0, len(data); i < n; i += blockSize { + j := i + blockSize - if len(unCompressed) == 0 { - continue + if j > n { + j = n } - compressed = compressionFunction(unCompressed) + unCompressed := data[i:j] + compressed := compressionFunction(unCompressed) // did you actually compress anything? //this is to stop the compression library from sticking in extra diff --git a/lzop/lzop_test.go b/lzop/lzop_test.go index 0753223..3e07a9c 100644 --- a/lzop/lzop_test.go +++ b/lzop/lzop_test.go @@ -92,6 +92,7 @@ func testData(input []byte, buff *bytes.Buffer) { } func TestRandomDataSizes(t *testing.T) { + testData(getData(0), nil) testData(getData(256), nil) testData(getData(512), nil) testData(getData(1024), nil)