-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlzip.c
142 lines (109 loc) · 3.95 KB
/
lzip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* This file is part of the Aaru Data Preservation Suite.
* Copyright (c) 2019-2025 Natalia Portillo.
* 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>
#include <stdint.h>
#include "library.h"
#include "3rdparty/lzlib/lzlib.h"
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)
{
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_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);
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);
}
// Free buffers
LZ_decompress_close(ctx);
return out_pos;
}
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)
{
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;
}