CodecLz4
CodecLz4.LZ4FastCompressor
— MethodLZ4FastCompressor(; kwargs...)
Creates an LZ4 compression codec.
Keywords
acceleration::Integer=0
: acceleration factorblock_size::Integer=1024
: The size in bytes to encrypt into each block.
CodecLz4.LZ4FastCompressorStream
— MethodLZ4FastCompressor(stream::IO; kwargs...)
Creates an LZ4 compression stream. See LZ4FastCompressor()
and TranscodingStream()
for arguments.
CodecLz4.LZ4FrameCompressor
— MethodLZ4FrameCompressor(; kwargs...)
Creates an LZ4 compression codec.
Keywords
blocksizeid::BlockSizeID=default_size
:max64KB
,max256KB
,max1MB
, ormax4MB
ordefault_size
blockmode::BlockMode=block_linked
:block_linked
orblock_independent
contentchecksum::Bool=false
: iftrue
, frame is terminated with a 32-bits checksum of decompressed dataframetype::FrameType=normal_frame)
:normal_frame
orskippable_frame
contentsize::Integer=0
: Size of uncompressed content (0 for unknown)blockchecksum::Bool=false
: iftrue
, each block is followed by a checksum of block's compressed datacompressionlevel::Integer=0
: compression level (-1..12)autoflush::Bool=false
: always flush iftrue
CodecLz4.LZ4FrameCompressorStream
— MethodLZ4FrameCompressorStream(stream::IO; kwargs...)
Creates an LZ4 compression stream. See LZ4FrameCompressor()
and TranscodingStream()
for arguments.
CodecLz4.LZ4FrameDecompressor
— MethodLZ4FrameCompressor()
Creates an LZ4 decompression codec.
CodecLz4.LZ4FrameDecompressorStream
— MethodLZ4FrameCompressorStream(stream::IO; kwargs...)
Creates an LZ4 decompression stream. See TranscodingStream()
for arguments.
CodecLz4.LZ4HCCompressor
— MethodLZ4HCCompressor(; kwargs...)
Creates an LZ4 HC compression codec.
Keywords
compressionlevel::Integer=9
: compression levelblock_size::Integer=1024
: The size in bytes to encrypt into each block. (Max 4MB)
CodecLz4.LZ4HCCompressorStream
— MethodLZ4HCCompressorStream(stream::IO; kwargs...)
Creates an LZ4 HC compression stream. See LZ4HCCompressorStream()
and TranscodingStream()
for arguments.
CodecLz4.LZ4SafeDecompressor
— MethodLZ4SafeDecompressor(; kwargs...)
Creates an LZ4 compression codec.
Keywords
block_size::Integer=1024
: The size in bytes of unecrypted data contained in each block. Must match or exceed the compressionblock_size
or decompression will fail.
CodecLz4.LZ4SafeDecompressorStream
— MethodLZ4SafeDecompressorStream(stream::IO; kwargs...)
Creates an LZ4 compression stream. See LZ4SafeDecompressor()
and TranscodingStream()
for arguments.
CodecLz4.LZ4F_compressBegin
— MethodWill write the frame header into dstBuffer
.
dstCapacity
must be >= LZ4F_HEADER_SIZE_MAX
bytes. prefsPtr
is optional : you can provide C_NULL
as argument, all preferences will then be set to default. Returns the number of bytes written into dstBuffer
for the header or throws an error.
CodecLz4.LZ4F_compressBound
— MethodProvides minimum dstCapacity
for a given srcSize
to guarantee operation success in worst case situations.
prefsPtr
is optional : when C_NULL
is provided, preferences will be set to cover worst case scenario. Result is always the same for a srcSize
and prefsPtr
, so it can be trusted to size reusable buffers. When srcSize==0
, LZ4F_compressBound()
provides an upper bound for LZ4F_flush()
and LZ4F_compressEnd()
operations.
CodecLz4.LZ4F_compressEnd
— MethodInvoke to properly finish an LZ4 frame.
It will flush whatever data remained within cctx
(like LZ4_flush()
) and properly finalize the frame, with an endMark and a checksum. cOptPtr
is optional : C_NULL
can be provided, in which case all options will be set to default. Returns the number of bytes written into dstBuffer
(necessarily >= 4 (endMark), or 8 if optional frame checksum is enabled) or throws an error if it fails (which can be tested using LZ4F_isError()
) A successful call to LZ4F_compressEnd()
makes cctx
available again for another compression task.
CodecLz4.LZ4F_compressUpdate
— MethodCan be called repetitively to compress as much data as necessary.
An important rule is that dstCapacity
MUST be large enough to ensure operation success even in worst case situations. This value is provided by LZ4F_compressBound()
. If this condition is not respected, LZ4F_compress()
will fail. LZ4FcompressUpdate() doesn't guarantee error recovery. When an error occurs, compression context must be freed or resized. cOptPtr
is optional : `CNULLcan be provided, in which case all options are set to default. Returns the number of bytes written into
dstBuffer` (it can be zero, meaning input data was just buffered). or throws an error if it fails.
CodecLz4.LZ4F_createCompressionContext
— MethodThe first thing to do is to create a compressionContext object, which will be used in all compression operations. This is achieved using LZ4F_createCompressionContext()
, which takes as argument a version. The version provided MUST be the current version. It is intended to track potential version mismatch, notably when using DLL. The function will provide a pointer to a fully allocated LZ4F_cctx
object. Will throw an error if there was an error during context creation.
CodecLz4.LZ4F_createDecompressionContext
— MethodCreate an LZ4F_dctx object
, to track all decompression operations.
The version provided MUST be the current LZ4F version. The function provides a pointer to an allocated and initialized LZ4F_dctx
object. The the function throws an error if the LZ4F_dctx
object cannot be initialized. The dctx
memory can be released using LZ4F_freeDecompressionContext()
.
CodecLz4.LZ4F_decompress
— MethodCall this function repetitively to regenerate compressed data from srcBuffer
.
The function will read up to srcSizePtr
bytes from srcBuffer
, and decompress data into dstBuffer
, of capacity dstSizePtr
. The number of bytes consumed from srcBuffer
will be written into srcSizePtr
(necessarily <= original value). The number of bytes decompressed into dstBuffer
will be written into dstSizePtr
(necessarily <= original value). The function does not necessarily read all input bytes, so always check value in srcSizePtr
. Unconsumed source data must be presented again in subsequent invocations.
dstBuffer
can freely change between each consecutive function invocation. dstBuffer
content will be overwritten.
Returns an hint of how many srcSize
bytes LZ4F_decompress()
expects for next call. Schematically, it's the size of the current (or remaining) compressed block + header of next block. Respecting the hint provides some small speed benefit, because it skips intermediate buffers. This is just a hint though, it's always possible to provide any srcSize.
When a frame is fully decoded, returns 0 (no more data expected). When provided with more bytes than necessary to decode a frame, LZ4F_decompress()
will stop reading exactly at end of current frame, and return 0.
If decompression failed, an error is thrown. After a decompression error, the dctx
context is not resumable. Use LZ4F_resetDecompressionContext()
to return to clean state. After a frame is fully decoded, dctx can be used again to decompress another frame.
CodecLz4.LZ4F_flush
— MethodWhen data must be generated and sent immediately, without waiting for a block to be completely filled, it's possible to call LZ4_flush()
. It will immediately compress any data buffered within cctx
. dstCapacity
must be large enough to ensure the operation will be successful. cOptPtr
is optional : it's possible to provide C_NULL
, all options will be set to default. Returns the number of bytes written into dstBuffer
(it can be zero, which means there was no data stored within cctx
) or throws an error if it fails.
CodecLz4.LZ4F_freeCompressionContext
— MethodReleases the memory of a LZ4F_cctx
.
CodecLz4.LZ4F_freeDecompressionContext
— MethodFrees the decompressionContext.
The result of LZ4F_freeDecompressionContext()
is indicative of the current state of decompressionContext when being released. That is, it should be == 0 if decompression has been completed fully and correctly.
CodecLz4.LZ4F_getFrameInfo
— MethodExtracts frame parameters (max blockSize, dictID, etc.).
Its usage is optional. Extracted information is typically useful for allocation and dictionary. This function works in 2 situations :
- At the beginning of a new frame, in which case it will decode information from
srcBuffer
, starting the decoding process. Input size must be large enough to successfully decode the entire frame header. Frame header size is variable, but is guaranteed to be <=LZ4F_HEADER_SIZE_MAX
bytes. It's allowed to provide more input data than this minimum. - After decoding has been started. In which case, no input is read, frame parameters are extracted from dctx.
- If decoding has barely started, but not yet extracted information from header,
LZ4F_getFrameInfo()
will fail.
The number of bytes consumed from srcBuffer will be updated within srcSizePtr
(necessarily <= original value). Decompression must resume from (srcBuffer
+ srcSizePtr
). Returns an hint about how many srcSize
bytes LZ4F_decompress()
expects for next call or throws an error.
Note 1 : In case of error, dctx is not modified. Decoding operation can resume from beginning safely. Note 2 : Frame parameters are copied into an already allocated LZ4F_frameInfo_t
structure.
CodecLz4.LZ4F_getVersion
— MethodGets the current LZ4F version.
CodecLz4.LZ4F_resetDecompressionContext
— MethodRe-initializes decompression context
In case of an error, the context is left in "undefined" state. In which case, it's necessary to reset it, before re-using it. This method can also be used to abruptly stop any unfinished decompression, and start a new one using same context resources.
CodecLz4.LZ4_compressBound
— MethodLZ4_compressBound(inputsize)
Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible) This function is primarily useful for memory allocation purposes (destination buffer size). Macro LZ4COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example). Note that LZ4compressdefault() compress faster when dest buffer size is >= LZ4compressBound(srcsize) inputsize : max supported value is LZ4MAXINPUT_SIZE
Returns maximum output size in a "worst case" scenario or 0, if input size is too large ( > LZ4MAXINPUT_SIZE)
CodecLz4.LZ4_compress_HC
— FunctionLZ4_compress_HC(src, dst, srcsize, dstcapacity, compressionlevel)
Compress data from src
into dst
, using the more powerful but slower "HC" algorithm. dst
must be already allocated. Compression is guaranteed to succeed if dstcapacity >= LZ4_compressBound(srcsize)
Max supported srcsize
value is LZ4MAXINPUTSIZE compressionlevel
: any value between 1 and LZ4HCCLEVELMAX will work. Values > LZ4HCCLEVELMAX behave the same as LZ4HCCLEVEL_MAX. Returns the number of bytes written into dst
CodecLz4.LZ4_compress_HC_continue
— MethodLZ4_compress_HC_continue(streamptr::Ptr{LZ4_streamHC_t}, src, dst, srcsize, maxdstsize)
Compress data in successive blocks of any size, using previous blocks as dictionary. One key assumption is that previous blocks (up to 64 KB) remain read-accessible while compressing next blocks. There is an exception for ring buffers, which can be smaller than 64 KB. Ring buffers scenario is automatically detected and handled by LZ4compressHC_continue().
Before starting compression, state must be properly initialized, using LZ4_resetStreamHC().
Then, use LZ4compressHCcontinue() to compress each successive block. Previous memory blocks (including initial dictionary when present) must remain accessible and unmodified during compression. dst
buffer should be sized to handle worst case scenarios (see LZ4compressBound()), to ensure operation success. Because in case of failure, the API does not guarantee context recovery, and context will have to be reset.
CodecLz4.LZ4_compress_destSize
— MethodLZ4_compress_destSize(src, dst, srcsize, dstcapacity)
Reverse the logic : compresses as much data as possible from src
buffer into already allocated buffer dst
of size dstcapacity
. This function either compresses the entire src
content into dst
if it's large enough, or fill dst
buffer completely with as much data as possible from src
. *srcsize : will be modified to indicate how many bytes where read from src
to fill dst
. New value is necessarily <= old value.
Returns number of bytes written into dst
(necessarily <= dstcapacity)
CodecLz4.LZ4_compress_fast
— FunctionLZ4_compress_fast(src, dst, srcsize, dstcapacity=1)
Compresses srcsize
bytes from buffer src
into already allocated dst
buffer of size dstcapacity
. Compression is guaranteed to succeed if dstcapacity
>= LZ4_compressBound(srcsize). It also runs faster, so it's a recommended setting. If the function cannot compress src
into a limited dst
budget, compression stops immediately, and the function result is zero. As a consequence, dst
content is not valid.
Also allows to select an acceleration
factor. The larger the acceleration value, the faster the algorithm, but also the lesser the compression. It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed. Values <= 0 will be replaced by the default which is 1.
This function never writes outside dst
buffer, nor read outside source
buffer. srcsize : supported max value is LZ4MAXINPUT_VALUE dstcapacity : full or partial size of buffer dst
(which must be already allocated)
Returns the number of bytes written into buffer dst
(necessarily <= dstcapacity)
CodecLz4.LZ4_compress_fast_continue
— FunctionLZ4_compress_fast_continue(streamptr, src, dst, srcsize, dstcapacity, acceleration)
Compress content into src
using data from previously compressed blocks, improving compression ratio. dst
buffer must be already allocated. If dstcapacity >= LZ4_compressBound(srcsize), compression is guaranteed to succeed, and runs faster.
Important : Up to 64KB of previously compressed data is assumed to remain present and unmodified in memory ! Special 1 : If input buffer is a double-buffer, it can have any size, including < 64 KB. Special 2 : If input buffer is a ring-buffer, it can have any size, including < 64 KB.
Returns size of compressed block. After an error, the stream status is invalid, it can only be reset or freed.
CodecLz4.LZ4_createStream
— MethodLZ4_createStream()
Will allocate and initialize an LZ4_stream_t
structure.
CodecLz4.LZ4_createStreamDecode
— MethodLZ4_createStreamDecode()
These decoding functions work the same as creation / destruction of streaming decompression tracking structure. A tracking structure can be re-used multiple times sequentially.
CodecLz4.LZ4_createStreamHC
— MethodLZ4_createStreamHC()
Create memory for LZ4 HC streaming state. Newly created states are automatically initialized. Existing states can be re-used several times, using LZ4_resetStreamHC().
CodecLz4.LZ4_decompress_safe
— MethodLZ4_decompress_safe(src, dst, cmpsize, dstcapacity)
Decompresses cmpsize
bytes from buffer src
into dst
. If destination buffer is not large enough, decoding will stop and output an error. If the source stream is detected malformed, the function will stop decoding and error. This function is protected against buffer overflow exploits, including malicious data packets. It never writes outside output buffer, nor reads outside input buffer.
cmpsize : is the exact complete size of the compressed block. dstcapacity : is the size of destination buffer, which must be already allocated.
Returns the number of bytes decompressed into destination buffer (necessarily <= dstcapacity)
CodecLz4.LZ4_decompress_safe_continue
— MethodLZ4_decompress_safe_continue(streamptr, src, dst, srcsize, dstcapacity)
These decoding functions allow decompression of consecutive blocks in "streaming" mode. A block is an unsplittable entity, it must be presented entirely to a decompression function. Decompression functions only accept one block at a time. Previously decoded blocks must remain available at the memory position where they were decoded (up to 64 KB).
Special : if application sets a ring buffer for decompression, it must respect one of the following conditions :
- Exactly same size as encoding buffer, with same update rule (block boundaries at same positions) In which case, the decoding & encoding ring buffer can have any size, including very small ones ( < 64 KB).
- Larger than encoding buffer, by a minimum of maxBlockSize more bytes. maxBlockSize is implementation dependent. It's the maximum size of any single block. In which case, encoding and decoding buffers do not need to be synchronized, and encoding ring buffer can have any size, including small ones ( < 64 KB).
- At least 64 KB + 8 bytes + maxBlockSize. In which case, encoding and decoding buffers do not need to be synchronized, and encoding ring buffer can have any size, including larger than decoding buffer.
Whenever these conditions are not possible, save the last 64KB of decoded data into a safe buffer, and indicate where it is saved using LZ4_setStreamDecode()
before decompressing next block.
CodecLz4.LZ4_freeStream
— MethodLZ4_freeStream(streamptr::Ptr{LZ4_stream_t})
Releases memory allocated by LZ4_createStream.
CodecLz4.LZ4_freeStreamDecode
— MethodLZ4_freeStreamDecode(streamptr)
These decoding functions work the same as creation / destruction of streaming decompression tracking structure. A tracking structure can be re-used multiple times sequentially.
CodecLz4.LZ4_freeStreamHC
— MethodLZ4_freeStreamHC(streamptr)
Release memory for LZ4 HC streaming state. Existing states can be re-used several times, using LZ4_resetStreamHC().
CodecLz4.LZ4_resetStream
— MethodLZ4_resetStream(streamptr)
An LZ4streamt structure can be allocated once and re-used multiple times. Use this function to start compressing a new stream.
CodecLz4.LZ4_setStreamDecode
— FunctionLZ4_setStreamDecode(streamptr, dictionary, dictsize)
An LZ4streamDecodet structure can be allocated once and re-used multiple times. Use this function to start decompression of a new stream of blocks. A dictionary can optionally be set. Use NULL or size 0 for a simple reset order. Returns 1 if OK
CodecLz4.lz4_compress
— Functionlz4_compress(input::Union{Vector{UInt8},Base.CodeUnits{UInt8}}, acceleration::Integer=0)
Compresses input
using LZ4compressfast. Returns a Vector{UInt8} of the compressed data.
CodecLz4.lz4_decompress
— Functionlz4_decompress(input::Union{Vector{UInt8},Base.CodeUnits{UInt8}}, expected_size::Integer=input.size * 2)
Decompresses input
using LZ4decompresssafe. expected_size
must be equal to or larger than the expected decompressed size of the input or decompression will fail. Returns a Vector{UInt8} of the decompressed data.
CodecLz4.lz4_hc_compress
— Functionlz4_hc_compress(input::Union{Vector{UInt8},Base.CodeUnits{UInt8}}, acceleration::Integer=9)
Compresses input
using LZ4compressHC. Returns a Vector{UInt8} of the compressed data.
TranscodingStreams.expectedsize
— MethodTranscodingStreams.expectedsize(codec::Union{LZ4FastCompressor, LZ4HCCompressor}, input::Memory)
Returns the expected size of the transcoded data.
TranscodingStreams.expectedsize
— MethodTranscodingStreams.expectedsize(codec::LZ4FrameCompressor, input::Memory)
Returns the expected size of the transcoded data.
TranscodingStreams.expectedsize
— MethodTranscodingStreams.expectedsize(codec::Union{LZ4FastCompressor, LZ4HCCompressor}, input::Memory)
Returns the expected size of the transcoded data.
TranscodingStreams.expectedsize
— MethodTranscodingStreams.expectedsize(codec::LZ4SafeDecompressor, input::Memory)
Returns the expected size of the transcoded data.
TranscodingStreams.finalize
— MethodTranscodingStreams.finalize(codec::LZ4FastCompressor)
Finalizes the LZ4 Compression Codec.
TranscodingStreams.finalize
— MethodTranscodingStreams.finalize(codec::LZ4FrameCompressor)
Finalizes the LZ4F Compression Codec.
TranscodingStreams.finalize
— MethodTranscodingStreams.finalize(codec::LZ4FrameDecompressor)
Finalizes the LZ4F Decompression Codec.
TranscodingStreams.finalize
— MethodTranscodingStreams.finalize(codec::LZ4HCCompressor)
Finalizes the LZHC Compression Codec.
TranscodingStreams.finalize
— MethodTranscodingStreams.finalize(codec::LZ4SafeDecompressor)
Finalizes the LZ4SafeDecompression Codec.
TranscodingStreams.initialize
— MethodTranscodingStreams.initialize(codec::LZ4FastCompressor)
Initializes the LZ4 Compression Codec.
TranscodingStreams.initialize
— MethodTranscodingStreams.initialize(codec::LZ4FrameCompressor)
Initializes the LZ4F Compression Codec.
TranscodingStreams.initialize
— MethodTranscodingStreams.initialize(codec::LZ4FrameDecompressor)
Initializes the LZ4F Decompression Codec.
TranscodingStreams.initialize
— MethodTranscodingStreams.initialize(codec::LZ4HCCompressor)
Initializes the LZ4 HC compression Codec.
TranscodingStreams.initialize
— MethodTranscodingStreams.initialize(codec::LZ4SafeDecompressor)
Initializes the LZ4 Compression Codec.
TranscodingStreams.minoutsize
— MethodTranscodingStreams.minoutsize(codec::Union{LZ4FastCompressor, LZ4HCCompressor}, input::Memory)
Returns the minimum output size of process
.
TranscodingStreams.minoutsize
— MethodTranscodingStreams.minoutsize(codec::LZ4FrameCompressor, input::Memory)
Returns the minimum output size of process
.
TranscodingStreams.minoutsize
— MethodTranscodingStreams.minoutsize(codec::Union{LZ4FastCompressor, LZ4HCCompressor}, input::Memory)
Returns the minimum output size of process
.
TranscodingStreams.minoutsize
— MethodTranscodingStreams.minoutsize(codec::LZ4SafeDecompressor, input::Memory)
Returns the minimum output size of process
.
TranscodingStreams.process
— MethodTranscodingStreams.process(codec::LZ4FastCompressor, input::Memory, output::Memory, error::Error)
Compresses the data from input
and writes to output
. The process follows the compression example from https://github.com/lz4/lz4/blob/dev/examples/blockStreaming_doubleBuffer.c wherein each block of encoded data is prefixed by the number of bytes contained in that block. Decoding can be done through the LZ4SafeDecompressor.
TranscodingStreams.process
— MethodTranscodingStreams.process(codec::LZ4FrameCompressor, input::Memory, output::Memory, error::Error)
Compresses the data from input
and writes to output
. The LZ4 compression algorithm may simply buffer the input data a full frame can be produced, so data_written
may be 0. flush()
may be used to force output
to be written.
TranscodingStreams.process
— MethodTranscodingStreams.process(codec::LZ4FrameDecompressor, input::Memory, output::Memory, error::Error)
Decompresses the data from input
and writes to output
. If the input data is not properly formatted this function will throw an error.
TranscodingStreams.process
— MethodTranscodingStreams.process(codec::LZ4HCCompressor, input::Memory, output::Memory, error::Error)
Compresses the data from input
and writes to output
. The process follows the compression example from https://github.com/lz4/lz4/blob/dev/examples/HCStreaming_ringBuffer.c wherein each block of encoded data is prefixed by the number of bytes contained in that block. Decoding can be done through the LZ4SafeDecompressor.
TranscodingStreams.process
— MethodTranscodingStreams.process(codec::LZ4SafeDecompressor, input::Memory, output::Memory, error::Error)
Compresses the data from input
and writes to output
. The process follows the decompression example from https://github.com/lz4/lz4/blob/dev/examples/blockStreaming_doubleBuffer.c and requires each block of encoded data to be prefixed by the number of bytes contained in that block.
TranscodingStreams.startproc
— MethodTranscodingStreams.startproc(codec::LZ4FastCompressor, mode::Symbol, error::Error)
Starts processing with the codec
TranscodingStreams.startproc
— MethodTranscodingStreams.startproc(codec::LZ4FrameCompressor, mode::Symbol, error::Error)
Starts processing with the codec Creates the LZ4F header to be written to the output.
TranscodingStreams.startproc
— MethodTranscodingStreams.startproc(codec::LZ4HCCompressor, mode::Symbol, error::Error)
Starts processing with the codec
TranscodingStreams.startproc
— MethodTranscodingStreams.startproc(codec::LZ4SafeDecompressor, mode::Symbol, error::Error)
Starts processing with the codec