mirror of
https://github.com/aaru-dps/Aaru.Compression.Native.git
synced 2025-12-16 11:14:30 +00:00
Add code to compress buffer with LZIP.
This commit is contained in:
63
lzip.c
63
lzip.c
@@ -80,5 +80,68 @@ AARU_EXPORT int32_t AARU_CALL lzip_decode_buffer(uint8_t* dst_buffer,
|
||||
// Free buffers
|
||||
LZ_decompress_close(ctx);
|
||||
|
||||
return out_pos;
|
||||
}
|
||||
|
||||
AARU_EXPORT int32_t AARU_CALL lzip_encode_buffer(uint8_t* dst_buffer,
|
||||
int32_t dst_size,
|
||||
const uint8_t* src_buffer,
|
||||
int32_t src_size,
|
||||
int32_t dictionary_size,
|
||||
int32_t match_len_limit)
|
||||
{
|
||||
int max_in_size;
|
||||
void* ctx;
|
||||
enum LZ_Errno lz_err;
|
||||
int32_t in_pos = 0, out_pos = 0, in_size;
|
||||
int rd;
|
||||
|
||||
// Initialize the decoder
|
||||
ctx = LZ_compress_open(dictionary_size, match_len_limit, src_size);
|
||||
lz_err = LZ_compress_errno(ctx);
|
||||
|
||||
if(lz_err != LZ_ok)
|
||||
{
|
||||
// Ensure freed
|
||||
LZ_compress_close(ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Check maximum lzip buffer input size
|
||||
max_in_size = LZ_compress_write_size(ctx);
|
||||
if(src_size < max_in_size) max_in_size = src_size;
|
||||
|
||||
// Compress buffer
|
||||
while(in_pos < src_size && out_pos < dst_size)
|
||||
{
|
||||
if(src_size - in_pos < max_in_size) max_in_size = src_size - in_pos;
|
||||
|
||||
in_size = LZ_compress_write(ctx, src_buffer + in_pos, max_in_size);
|
||||
|
||||
in_pos += in_size;
|
||||
|
||||
rd = LZ_compress_read(ctx, dst_buffer + out_pos, dst_size);
|
||||
|
||||
out_pos += rd;
|
||||
|
||||
if(LZ_compress_finished(ctx) == 1) break;
|
||||
}
|
||||
|
||||
LZ_compress_finish(ctx);
|
||||
|
||||
if(out_pos < dst_size)
|
||||
{
|
||||
do {
|
||||
rd = LZ_compress_read(ctx, dst_buffer + out_pos, dst_size);
|
||||
|
||||
out_pos += rd;
|
||||
|
||||
if(LZ_compress_finished(ctx) == 1) break;
|
||||
} while(rd > 0 && out_pos < dst_size);
|
||||
}
|
||||
|
||||
// Free buffers
|
||||
LZ_compress_close(ctx);
|
||||
|
||||
return out_pos;
|
||||
}
|
||||
6
lzip.h
6
lzip.h
@@ -24,4 +24,10 @@ AARU_EXPORT int32_t AARU_CALL lzip_decode_buffer(uint8_t* dst_buffer,
|
||||
const uint8_t* src_buffer,
|
||||
int32_t src_size);
|
||||
|
||||
AARU_EXPORT int32_t AARU_CALL lzip_encode_buffer(uint8_t* dst_buffer,
|
||||
int32_t dst_size,
|
||||
const uint8_t* src_buffer,
|
||||
int32_t src_size,
|
||||
int32_t dictionary_size,
|
||||
int32_t match_len_limit);
|
||||
#endif // AARU_CHECKSUMS_NATIVE__LZIP_H_
|
||||
|
||||
Reference in New Issue
Block a user