COMPRESSION: Use LZ4 frame for compatibility with command lz4.

Signed-off-by: Grzegorz Kowal <grzegorz@amuncode.org>
This commit is contained in:
Grzegorz Kowal 2020-08-13 09:17:09 -03:00
parent b05f186765
commit f9b6dd47e9
2 changed files with 41 additions and 31 deletions

View File

@ -56,7 +56,7 @@ try:
except ImportError:
zstd_available = False
try:
import lz4.block as lz4
import lz4.frame as lz4
lz4_available = True
except ImportError:
lz4_available = False

View File

@ -37,37 +37,46 @@ module compression
!
#ifdef ZSTD
interface
integer(c_int) function zstd_compress(dst, dstCapacity, src, srcSize, lev) &
bind(C, name="ZSTD_compress")
use iso_c_binding, only: c_size_t, c_int, c_ptr
implicit none
type(c_ptr), value :: src, dst
integer(kind=c_size_t), value :: srcSize, dstCapacity
integer(kind=c_int), value :: lev
end function zstd_compress
integer(c_size_t) function zstd_compressBound(srcSize) &
bind(C, name="ZSTD_compressBound")
integer(c_size_t) function zstd_bound(srcSize) &
bind(C, name="ZSTD_compressBound")
use iso_c_binding, only: c_size_t
implicit none
integer(kind=c_size_t), value :: srcSize
end function zstd_compressBound
end function zstd_bound
integer(c_size_t) function zstd_compress(dst, dstCapacity, &
src, srcSize, level) &
bind(C, name="ZSTD_compress")
use iso_c_binding, only: c_size_t, c_int, c_ptr
implicit none
integer(kind=c_size_t), value :: srcSize, dstCapacity
type(c_ptr) , value :: src, dst
integer(kind=c_int) , value :: level
end function zstd_compress
end interface
#endif /* ZSTD */
#ifdef LZ4
interface
integer(c_int) function lz4_compress(src, dst, srcSize, dstCapacity) &
bind(C, name="LZ4_compress_default")
use iso_c_binding, only: c_int, c_ptr
integer(kind=c_size_t) function lz4_bound(srcSize, prefsPtr) &
bind(C, name="LZ4F_compressFrameBound")
use iso_c_binding, only: c_size_t, c_ptr
implicit none
type(c_ptr), value :: src, dst
integer(kind=c_int), value :: srcSize, dstCapacity
integer(kind=c_size_t), value :: srcSize
type(c_ptr) , value :: prefsPtr
end function lz4_bound
integer(kind=c_size_t) function lz4_compress(dst, dstCapacity, &
src, srcSize, prefsPtr) &
bind(C, name="LZ4F_compressFrame")
use iso_c_binding, only: c_size_t, c_ptr
implicit none
integer(kind=c_size_t), value :: dstCapacity, srcSize
type(c_ptr) , value :: src, dst, prefsPtr
end function lz4_compress
integer(c_int) function lz4_compressBound(srcSize) &
bind(C, name="LZ4_compressBound")
use iso_c_binding, only: c_int
implicit none
integer(kind=c_int), value :: srcSize
end function lz4_compressBound
end interface
#endif /* LZ4 */
@ -192,6 +201,9 @@ module compression
subroutine compress(input, output, csize)
use iso_c_binding, only: c_int, c_loc
#ifdef LZ4
use iso_c_binding, only: c_null_ptr
#endif /* LZ4 */
implicit none
@ -211,7 +223,7 @@ module compression
select case(compression_format)
#ifdef ZSTD
case(compression_zstd)
allocate(buffer(zstd_compressBound(sizeof(input))))
allocate(buffer(zstd_bound(sizeof(input))))
csize = zstd_compress(c_loc(buffer), sizeof(buffer), &
c_loc(input), sizeof(input), compression_level)
if (csize > 0 .and. csize <= sizeof(output)) then
@ -223,13 +235,11 @@ module compression
#endif /* ZSTD */
#ifdef LZ4
case(compression_lz4)
allocate(buffer(lz4_compressBound(size(input))))
csize = lz4_compress(c_loc(input), c_loc(buffer), &
size(input), size(buffer))
if (csize > 0 .and. csize <= size(output) - 4) then
output(1:4) = transfer(1_4 * size(input), [1_1])
output(5:csize+4) = buffer(1:csize)
csize = csize + 4
allocate(buffer(lz4_bound(sizeof(input), c_null_ptr)))
csize = lz4_compress(c_loc(buffer), sizeof(buffer), &
c_loc(input), sizeof(input), c_null_ptr)
if (csize > 0 .and. csize <= sizeof(output)) then
output(1:csize) = buffer(1:csize)
else
csize = -1
end if