diff --git a/gozlib.go b/gozlib.go index 1a0da37..b0ad6f6 100644 --- a/gozlib.go +++ b/gozlib.go @@ -382,14 +382,14 @@ func GoUncompressStream(inputBufferSize uint32, outputBufferSize uint32, inputRe // GoGZipCompressBuffer compresses data in gzip format, reading from input and // writing to a pre allocated output buffer. If the output is too small to contain the compressed data, an error is returned func GoGZipCompressBuffer(level CompressionLevel, input []byte, output []byte) (uint64, error) { - inputCap := cap(input) + inputLen := len(input) outputCap := cap(output) if outputCap == 0 { return 0, OutputBufferTooSmallError } var inputPtr unsafe.Pointer = nil - if inputCap > 0 { + if inputLen > 0 { inputPtr = unsafe.Pointer(&input[0]) } @@ -398,7 +398,7 @@ func GoGZipCompressBuffer(level CompressionLevel, input []byte, output []byte) ( var errorCode C.int = C.Z_OK - compLen := C.gzip_compress_buffer(C.int(level), inputPtr, C.uInt(inputCap), outputPtr, C.uInt(outputCap), &errorCode) + compLen := C.gzip_compress_buffer(C.int(level), inputPtr, C.uInt(inputLen), outputPtr, C.uInt(outputCap), &errorCode) if errorCode != C.Z_OK { return 0, fmt.Errorf(wrapErrorFormat, BufferCompressError, errorCode) @@ -410,14 +410,14 @@ func GoGZipCompressBuffer(level CompressionLevel, input []byte, output []byte) ( // GoUncompressBuffer uncompresses a gzip or standard zlib input buffer writing to a pre allocated output // if the output is too small to contain the compressed data, an error is returned func GoUncompressBuffer(input []byte, output []byte) (uint64, error) { - inputCap := cap(input) + inputLen := len(input) outputCap := cap(output) if outputCap == 0 { return 0, OutputBufferTooSmallError } var inputPtr unsafe.Pointer = nil - if inputCap > 0 { + if inputLen > 0 { inputPtr = unsafe.Pointer(&input[0]) } @@ -426,7 +426,7 @@ func GoUncompressBuffer(input []byte, output []byte) (uint64, error) { var errorCode C.int = C.Z_OK - uncompLen := C.uncompress_buffer_any(inputPtr, C.uInt(inputCap), outputPtr, C.uInt(outputCap), &errorCode) + uncompLen := C.uncompress_buffer_any(inputPtr, C.uInt(inputLen), outputPtr, C.uInt(outputCap), &errorCode) if errorCode != C.Z_OK { return 0, fmt.Errorf(wrapErrorFormat, BufferUncompressError, errorCode) diff --git a/gozlib_buffer_test.go b/gozlib_buffer_test.go index bd9dd11..25a6078 100644 --- a/gozlib_buffer_test.go +++ b/gozlib_buffer_test.go @@ -2,6 +2,7 @@ package gozlib import ( "bytes" + "strconv" "testing" "github.com/stretchr/testify/assert" @@ -115,3 +116,25 @@ func TestGoCompressUncompressBuffer(t *testing.T) { assert.Equal(t, uint64(inputSize), uncompLen) assert.Equal(t, input, uncompressed[:uncompLen]) } + +func TestGoCompressUncompressBufferWithLenLessThanCap(t *testing.T) { + var i int = 1 + input := []byte(strconv.Itoa(i)) + // t.Logf("input: %s, len: %d, cap: %d", input, len(input), cap(input)) // input: 1, len: 1, cap: 8 + + inputSize := len(input) + outputSize := inputSize + 64 + output := make([]byte, 0, outputSize) + + compLen, compError := GoGZipCompressBuffer(CompressionLevelBestSpeed, input, output) + assert.NoError(t, compError) + + compressed := output[:compLen] + uncompressed := make([]byte, 0, inputSize+64) + + uncompLen, uncompErr := GoUncompressBuffer(compressed, uncompressed) + + assert.NoError(t, uncompErr) + assert.Equal(t, uint64(inputSize), uncompLen) + assert.Equal(t, input, uncompressed[:uncompLen]) +}