Files
Aaru.Compression.Native/lzip.c

142 lines
3.9 KiB
C
Raw Normal View History

2021-10-18 02:44:57 +01:00
/*
* This file is part of the Aaru Data Preservation Suite.
2022-12-02 10:59:00 +00:00
* Copyright (c) 2019-2023 Natalia Portillo.
2021-10-18 02:44:57 +01:00
* Copyright © 2018-2019 David Ryskalczyk
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <stddef.h>
2021-10-18 02:44:57 +01:00
#include <stdint.h>
#include "library.h"
2023-09-24 03:13:25 +01:00
#include "3rdparty/lzlib/lzlib.h"
2021-10-18 02:44:57 +01:00
2024-04-30 15:37:29 +01:00
AARU_EXPORT int32_t AARU_CALL AARU_lzip_decode_buffer(uint8_t *dst_buffer, int32_t dst_size, const uint8_t *src_buffer,
int32_t src_size)
2021-10-18 02:44:57 +01:00
{
int max_in_size;
2024-04-30 15:37:29 +01:00
void *ctx;
2021-10-18 02:44:57 +01:00
enum LZ_Errno lz_err;
int32_t in_pos = 0, out_pos = 0, in_size;
int rd;
2021-10-18 02:44:57 +01:00
// Initialize the decoder
ctx = LZ_decompress_open();
lz_err = LZ_decompress_errno(ctx);
if(lz_err != LZ_ok)
{
// Ensure freed
LZ_decompress_close(ctx);
return 0;
}
// Check maximum lzip buffer input size
max_in_size = LZ_decompress_write_size(ctx);
if(src_size < max_in_size) max_in_size = src_size;
// Decompress 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_decompress_write(ctx, src_buffer + in_pos, max_in_size);
in_pos += in_size;
rd = LZ_decompress_read(ctx, dst_buffer + out_pos, dst_size);
2021-10-18 02:44:57 +01:00
out_pos += rd;
if(LZ_decompress_finished(ctx) == 1) break;
}
LZ_decompress_finish(ctx);
if(out_pos < dst_size)
{
do {
rd = LZ_decompress_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);
}
2021-10-18 02:44:57 +01:00
// Free buffers
LZ_decompress_close(ctx);
2021-10-21 05:02:25 +01:00
return out_pos;
}
2024-04-30 15:37:29 +01:00
AARU_EXPORT int32_t AARU_CALL AARU_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)
2021-10-21 05:02:25 +01:00
{
int max_in_size;
2024-04-30 15:37:29 +01:00
void *ctx;
2021-10-21 05:02:25 +01:00
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);
2021-10-18 02:44:57 +01:00
return out_pos;
}