Debian Codegraph @

modules/lsp-runtime-lib/src/main/resource/Decompressor.cpp

raw
1 /*
2 * Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2026 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 16 мар. 2021 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/common/alloc.h>
23 #include <lsp-plug.in/common/debug.h>
24 #include <lsp-plug.in/io/InMemoryStream.h>
25 #include <lsp-plug.in/resource/Decompressor.h>
26
27 #define BUFFER_QUANTITY 0x1000
28
29 namespace lsp
30 {
31 namespace resource
32 {
33 Decompressor::Decompressor()
34 {
35 sReplay.data = NULL;
36 sReplay.off = 0;
37 sReplay.size = 0;
38 sReplay.cap = 0;
39 sReplay.rep = 0;
40
41 nOffset = 0;
42 nSize = 0;
43 }
44
45 Decompressor::~Decompressor()
46 {
47 do_close();
48 }
49
50 status_t Decompressor::do_close()
51 {
52 // Destroy compression buffer
53 sBuffer.destroy();
54
55 // Destroy replay buffer
56 if (sReplay.data != NULL)
57 free(sReplay.data);
58
59 // Clear values
60 sReplay.data = NULL;
61 sReplay.off = 0;
62 sReplay.size = 0;
63 sReplay.cap = 0;
64 sReplay.rep = 0;
65
66 nOffset = 0;
67 nSize = 0;
68
69 return sIn.close();
70 }
71
72 status_t Decompressor::init(const void *data, size_t data_size, size_t decompressed_size, size_t log_buf_sz)
73 {
74 // Create buffer
75 status_t res = sBuffer.init(log_buf_sz);
76 if (res != STATUS_OK)
77 return res;
78
79 // Create input memory stream
80 io::InMemoryStream *ims = new io::InMemoryStream(data, data_size);
81 if (ims == NULL)
82 return STATUS_NO_MEM;
83
84 // Wrap input memory stream
85 res = sIn.wrap(ims, WRAP_CLOSE | WRAP_DELETE);
86 if (res != STATUS_OK)
87 {
88 ims->close();
89 delete ims;
90 }
91
92 // Update positions
93 nOffset = 0;
94 nSize = decompressed_size;
95
96 // Clear replay buffer
97 sReplay.off = 0;
98 sReplay.size = 0;
99 sReplay.cap = 0;
100
101 return res;
102 }
103
104 status_t Decompressor::read_uint(size_t *out, size_t initial, size_t stepping)
105 {
106 ssize_t res;
107 ssize_t bits = initial;
108 size_t value = 0;
109 bool flag;
110
111 while (true)
112 {
113 if ((res = sIn.readb(&flag)) != 1)
114 return (res < 0) ? status_t(-res) : STATUS_IO_ERROR;
115 if (!flag)
116 break;
117
118 value += 1 << bits;
119 bits += stepping;
120 }
121
122 fixed_size_t v = 0;
123 if ((res = sIn.readv(&v, bits)) != bits)
124 return (res < 0) ? status_t(-res) : STATUS_IO_ERROR;
125
126 *out = value + size_t(v);
127 return STATUS_OK;
128 }
129
130 status_t Decompressor::close()
131 {
132 return do_close();
133 }
134
135 status_t Decompressor::set_buf(size_t off, size_t count, size_t rep)
136 {
137 // Need to allocate data?
138 if ((sReplay.cap < count) || (sReplay.data == NULL))
139 {
140 size_t cap = align_size(lsp_max(sReplay.cap + (sReplay.cap >> 1), count), BUFFER_QUANTITY);
141 uint8_t *ptr = reinterpret_cast<uint8_t *>(realloc(sReplay.data, cap));
142 if (ptr == NULL)
143 return STATUS_NO_MEM;
144
145 sReplay.data = ptr;
146 sReplay.cap = cap;
147 }
148
149 // Copy data to replay buffer
150 status_t res = sBuffer.extract(sReplay.data, off, count);
151 if (res != STATUS_OK)
152 return res;
153
154 sReplay.off = 0;
155 sReplay.size = count;
156 sReplay.rep = rep;
157
158 return STATUS_OK;
159 }
160
161 status_t Decompressor::set_bufc(uint8_t c, size_t rep)
162 {
163 // Need to allocate data?
164 if ((sReplay.cap < 1) || (sReplay.data == NULL))
165 {
166 uint8_t *ptr = reinterpret_cast<uint8_t *>(realloc(sReplay.data, BUFFER_QUANTITY));
167 if (ptr == NULL)
168 return STATUS_NO_MEM;
169
170 sReplay.data = ptr;
171 sReplay.cap = BUFFER_QUANTITY;
172 }
173
174 // Copy data to replay buffer
175 sReplay.data[0] = c;
176 sReplay.off = 0;
177 sReplay.size = 1;
178 sReplay.rep = rep;
179
180 return STATUS_OK;
181 }
182
183 size_t Decompressor::get_buf(uint8_t *dst, size_t count)
184 {
185 size_t nread = 0;
186
187 // Check for data available in buffer
188 size_t avail = sReplay.size - sReplay.off;
189 if (avail > 0)
190 {
191 avail = lsp_min(avail, count);
192 memcpy(dst, &sReplay.data[sReplay.off], avail);
193 sReplay.off += avail;
194 dst += avail;
195 nread += avail;
196
197 if (nread >= count)
198 return nread;
199 }
200
201 // Now check if there are any repeats available
202 if (sReplay.rep > 0)
203 {
204 avail = lsp_min(sReplay.rep, count - nread);
205 memset(dst, sReplay.data[sReplay.off-1], avail);
206 sReplay.rep -= avail;
207 nread += avail;
208 }
209
210 return nread;
211 }
212
213 ssize_t Decompressor::get_bufc()
214 {
215 if (sReplay.off < sReplay.size)
216 return sReplay.data[sReplay.off++];
217
218 if (sReplay.rep <= 0)
219 return -STATUS_EOF;
220
221 --sReplay.rep;
222 return sReplay.data[sReplay.off-1];
223 }
224
225 status_t Decompressor::fill_buf()
226 {
227 // Check that data is present in the buffer
228 if ((sReplay.off < sReplay.size) || (sReplay.rep > 0))
229 return STATUS_OK;
230
231 status_t res;
232 size_t offset = 0, length = 0, rep = 0, append = 0;
233 uint8_t b;
234
235 // Read offset
236 if ((res = read_uint(&offset, 5, 5)) != STATUS_OK)
237 return res;
238
239 if (offset < sBuffer.size())
240 {
241 // REPLAY BUFFER
242 // Length
243 if ((res = read_uint(&length, 5, 5)) != STATUS_OK)
244 return res;
245 // Repeat
246 if ((res = read_uint(&rep, 0, 4)) != STATUS_OK)
247 return res;
248
249 // Fill replay buffer with data
250 length += 1;
251 if ((res = set_buf(offset, length, rep)) != STATUS_OK)
252 return res;
253
254 // Append decompression buffer
255 b = sReplay.data[length - 1];
256 append = lsp_min(rep, REPEAT_BUF_MAX);
257 sBuffer.append(sReplay.data, length);
258 }
259 else
260 {
261 // EMIT OCTET
262 // Repeat
263 if ((res = read_uint(&rep, 0, 4)) != STATUS_OK)
264 return res;
265
266 b = offset - sBuffer.size();
267 append = lsp_min(rep, REPEAT_BUF_MAX) + 1;
268
269 // Fill replay buffer with data
270 if ((res = set_bufc(b, rep)) != STATUS_OK)
271 return res;
272 }
273
274 // Append decompression buffer
275 while (append--)
276 sBuffer.append(b);
277
278 return STATUS_OK;
279 }
280
281 ssize_t Decompressor::read(void *dst, size_t count)
282 {
283 status_t res;
284 uint8_t *d = reinterpret_cast<uint8_t *>(dst);
285 size_t nread = 0;
286
287 while (nread < count)
288 {
289 // Check offset
290 if (nOffset >= nSize)
291 {
292 if (nread <= 0)
293 return -set_error(STATUS_EOF);
294 break;
295 }
296
297 // Check if data has been read
298 size_t bufrd = get_buf(&d[nread], count - nread);
299 if (bufrd > 0)
300 {
301 nOffset += bufrd;
302 nread += bufrd;
303 continue;
304 }
305
306 // There is no data in the buffer, try to get new data
307 if ((res = fill_buf()) != STATUS_OK)
308 {
309 if (nread > 0)
310 break;
311 set_error(res);
312 return -res;
313 }
314 }
315
316 set_error(STATUS_OK);
317 return nread;
318 }
319
320 ssize_t Decompressor::read_byte()
321 {
322 ssize_t res;
323
324 if (nOffset >= nSize)
325 return -set_error(STATUS_EOF);
326
327 do
328 {
329 // Check if data has been read
330 res = get_bufc();
331 if (res >= 0)
332 {
333 ++nOffset;
334 return res;
335 }
336 } while ((res = fill_buf()) == STATUS_OK);
337
338 set_error(status_t(res));
339 return res;
340 }
341
342 wssize_t Decompressor::position()
343 {
344 return nOffset;
345 }
346
347 } /* namespace resource */
348 } /* namespace lsp */