Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 1 мар. 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/stdlib/string.h>
23 #include <lsp-plug.in/stdlib/stdio.h>
24 #include <lsp-plug.in/common/debug.h>
25 #include <lsp-plug.in/io/OutSequence.h>
26 #include <lsp-plug.in/io/OutBitStream.h>
27 #include <lsp-plug.in/common/bits.h>
28 #include <lsp-plug.in/common/alloc.h>
29 #include <lsp-plug.in/resource/Compressor.h>
30 #include <lsp-plug.in/resource/OutProxyStream.h>
31
32 namespace lsp
33 {
34 enum compress_cmd_t
35 {
36 COMMAND_APPEND_7B,
37 COMMAND_APPEND_8B,
38
39 COMMAND_TOTAL,
40 COMMAND_MAX = COMMAND_TOTAL - 1
41 };
42
43 namespace resource
44 {
45 Compressor::Compressor()
46 {
47 nSegment = 0;
48 nOffset = 0;
49 // hFD = fopen("/tmp/compressor.log", "w");
50 }
51
52 Compressor::~Compressor()
53 {
54 close();
55 }
56
57 status_t Compressor::close()
58 {
59 // Drop nodes
60 sBuffer.destroy();
61
62 // Drop entries
63 for (size_t i=0, n=vEntries.size(); i < n; ++i)
64 {
65 raw_resource_t *res = vEntries.uget(i);
66 if (res == NULL)
67 continue;
68 if (res->name != NULL)
69 free(const_cast<char *>(res->name));
70 res->name = NULL;
71 }
72 vEntries.flush();
73
74 // Drop buffer data
75 sTemp.drop();
76
77 status_t res = sTemp.close();
78 res = update_status(res, sOut.close());
79 res = update_status(res, sOS.close());
80
81 return res;
82 }
83
84 status_t Compressor::init(size_t log_buf_size, io::IOutStream *os, size_t flags)
85 {
86 status_t res;
87
88 if (sBuffer.data != NULL)
89 return STATUS_BAD_STATE;
90
91 if ((res = sBuffer.init(log_buf_size)) != STATUS_OK)
92 return res;
93
94 if ((res = sOS.wrap(os, flags)) != STATUS_OK)
95 return res;
96
97 if ((res = sOut.wrap(&sOS)) != STATUS_OK)
98 return res;
99
100 return STATUS_OK;
101 }
102
103 status_t Compressor::alloc_entry(raw_resource_t **r, io::Path *path, resource_type_t type)
104 {
105 if (path->is_empty())
106 return STATUS_BAD_ARGUMENTS;
107
108 ssize_t index = -1, sindex = -1;
109 LSPString item;
110 status_t res;
111
112 while (true)
113 {
114 // Get first item and remove it
115 if ((res = path->remove_first(&item)) != STATUS_OK)
116 return res;
117
118 // Lookup for existing directory/create yet another one
119 raw_resource_t *found = NULL;
120 for (size_t i=0, n=vEntries.size(); i<n; ++i)
121 {
122 raw_resource_t *ent = vEntries.uget(i);
123 if ((ent == NULL) || (ent->parent != index) || (ent->name == NULL))
124 continue;
125 if (item.equals_utf8(ent->name))
126 {
127 found = ent;
128 sindex = i;
129 break;
130 }
131 }
132
133 // Last entry?
134 if (path->is_empty())
135 {
136 // Item already exists?
137 if (found != NULL)
138 return STATUS_ALREADY_EXISTS;
139
140 // Add new resource entry
141 found = vEntries.add();
142 if (found == NULL)
143 return STATUS_NO_MEM;
144
145 // Initialize
146 found->type = type;
147 found->parent = int32_t(index);
148 found->segment = -1;
149 found->offset = -1;
150 found->length = 0;
151 found->name = item.clone_utf8();
152
153 if (found->name == NULL)
154 return STATUS_NO_MEM;
155
156 // Return the pointer
157 if (r != NULL)
158 *r = found;
159 return STATUS_OK;
160 }
161 else
162 {
163 // Need to create new one?
164 if (found == NULL)
165 {
166 // Allocate item
167 sindex = vEntries.size();
168 if ((found = vEntries.add()) == NULL)
169 return STATUS_NO_MEM;
170
171 found->type = RES_DIR;
172 found->parent = int32_t(index);
173 found->segment = -1;
174 found->offset = -1;
175 found->length = 0;
176 found->name = item.clone_utf8();
177
178 if (found->name == NULL)
179 return STATUS_NO_MEM;
180 }
181 else if (found->type != RES_DIR)
182 return STATUS_BAD_TYPE; // Bad entry type
183
184 // Update position
185 index = sindex;
186 }
187 }
188 }
189
190 wssize_t Compressor::write_entry(raw_resource_t *r, io::IInStream *is)
191 {
192 if (sBuffer.data == NULL)
193 return -STATUS_BAD_STATE;
194
195 // Clear data
196 sTemp.clear();
197 wssize_t flength = is->sink(&sTemp);
198 if (flength < 0)
199 return flength;
200
201 status_t res = STATUS_OK;
202 const uint8_t *head = sTemp.data();
203 const uint8_t *tail = &head[flength];
204 size_t offset = 0;
205
206 // IF_TRACE(
207 // wssize_t coffset = sOS.position();
208 // size_t octets = 0;
209 // size_t replays = 0;
210 // size_t repeats = 0;
211 // )
212
213 while (head < tail)
214 {
215 // Estimate the length of match
216 const size_t length = sBuffer.lookup(&offset, head, tail-head);
217
218 // Estimate size of output
219 const size_t est1 = est_uint(sBuffer.size() + *head, 5, 5) * length; // How many bits used to emit octet command
220 const size_t est2 = (length > 0) ? est_uint(offset, 5, 5) + est_uint(length - 1, 5, 5) : est1 + 1; // How many bits used to encode buffer replay command
221
222 if (est2 < est1) // Prefer buffer replay over octet emission
223 {
224 const size_t repeats = calc_repeats(&head[length], tail);
225
226 // fprintf(hFD, "BUFFER replays=%d, off=%d, length=%d, sequence=",
227 // int(repeats), int(offset), int(length));
228 // for (size_t i=0; i<length; ++i)
229 // fprintf(hFD, "%02x ", head[i]);
230 // for (size_t i=0; i<repeats; ++i)
231 // fprintf(hFD, "%02x ", head[length - 1]);
232 // fprintf(hFD, "\n");
233
234 // REPLAY BUFFER
235 // Emit Offset
236 if ((res = emit_uint(offset, 5, 5)) != STATUS_OK)
237 break;
238 // Emit Length - 1
239 if ((res = emit_uint(length - 1, 5, 5)) != STATUS_OK)
240 break;
241 // Emit Repeat counter
242 if ((res = emit_uint(repeats, 0, 4)) != STATUS_OK)
243 break;
244
245 // Append data to buffer
246 sBuffer.append(head, length + lsp_min(repeats, REPEAT_BUF_MAX));
247 head += length + repeats;
248
249 // IF_TRACE(
250 // ++replays;
251 // if (rep)
252 // ++repeats;
253 // );
254 }
255 else
256 {
257 const size_t repeats = calc_repeats(&head[1], tail);
258
259 // fprintf(hFD, "OCTET replays=%d, sequence=%02x ",
260 // int(repeats), int(*head));
261 // for (size_t i=0; i<repeats; ++i)
262 // fprintf(hFD, "%02x ", int(*head));
263 // fprintf(hFD, "\n");
264
265 // EMIT OCTET
266 // Emit Value
267 if ((res = emit_uint(sBuffer.size() + *head, 5, 5)) != STATUS_OK)
268 break;
269 // Emit Repeat counter
270 if ((res = emit_uint(repeats, 0, 4)) != STATUS_OK)
271 break;
272
273 // Append data to buffer
274 sBuffer.append(head, 1 + lsp_min(repeats, REPEAT_BUF_MAX));
275 head += 1 + repeats;
276 // IF_TRACE(
277 // ++octets;
278 // if (rep)
279 // ++repeats;
280 // );
281 }
282 }
283
284 // Flush the bit sequence
285 if (res != STATUS_OK)
286 return -res;
287
288 // Output stats
289 // IF_TRACE(
290 // size_t cbytes = sOS.position() - coffset;
291 //
292 // lsp_trace(" octets: %d, replays: %d, repeats: %d",
293 // int(octets), int(replays), int(repeats));
294 // lsp_trace(" original size: %d, compressed size: %d, ratio: %.2f",
295 // int(flength), int(cbytes), double(flength) / double(cbytes));
296 // )
297
298 // Remember the actual coordinates of the entry within data array
299 r->segment = int32_t(nSegment);
300 r->offset = int32_t(nOffset);
301 r->length = int32_t(flength);
302 nOffset += flength;
303
304 // lsp_trace(" compressed entry segment=%d, offset=%d length=%d",
305 // int(r->segment), int(r->offset), int(r->length));
306
307 return r->length;
308 }
309
310 wssize_t Compressor::create_file(const char *name, io::IInStream *is)
311 {
312 io::Path tmp;
313 raw_resource_t *r = NULL;
314
315 status_t res = tmp.set(name);
316 if (res == STATUS_OK)
317 res = alloc_entry(&r, &tmp, RES_FILE);
318
319 return (res == STATUS_OK) ? write_entry(r, is) : - res;
320 }
321
322 wssize_t Compressor::create_file(const LSPString *name, io::IInStream *is)
323 {
324 io::Path tmp;
325 raw_resource_t *r = NULL;
326
327 status_t res = tmp.set(name);
328 if (res == STATUS_OK)
329 res = alloc_entry(&r, &tmp, RES_FILE);
330
331 return (res == STATUS_OK) ? write_entry(r, is) : - res;
332 }
333
334 wssize_t Compressor::create_file(const io::Path *name, io::IInStream *is)
335 {
336 io::Path tmp;
337 raw_resource_t *r = NULL;
338
339 status_t res = tmp.set(name);
340 if (res == STATUS_OK)
341 res = alloc_entry(&r, &tmp, RES_FILE);
342
343 return (res == STATUS_OK) ? write_entry(r, is) : - res;
344 }
345
346 status_t Compressor::create_dir(const char *name)
347 {
348 io::Path tmp;
349
350 status_t res = tmp.set(name);
351 if (res == STATUS_OK)
352 res = alloc_entry(NULL, &tmp, RES_DIR);
353
354 return res;
355 }
356
357 status_t Compressor::create_dir(const LSPString *name)
358 {
359 io::Path tmp;
360
361 status_t res = tmp.set(name);
362 if (res == STATUS_OK)
363 res = alloc_entry(NULL, &tmp, RES_DIR);
364
365 return res;
366 }
367
368 status_t Compressor::create_dir(const io::Path *name)
369 {
370 io::Path tmp;
371
372 status_t res = tmp.set(name);
373 if (res == STATUS_OK)
374 res = alloc_entry(NULL, &tmp, RES_DIR);
375
376 return res;
377 }
378
379 status_t Compressor::emit_uint(size_t value, size_t initial, size_t stepping)
380 {
381 status_t res;
382 size_t bits = initial;
383
384 while (true)
385 {
386 size_t max = (1 << bits);
387 if (value < max)
388 break;
389 if ((res = sOut.bwrite(true)) != STATUS_OK)
390 return res;
391
392 value -= max;
393 bits += stepping;
394 }
395
396 if ((res = sOut.bwrite(false)) != STATUS_OK)
397 return res;
398
399 return (bits > 0) ? sOut.writev(lsp::fixed_int(value), bits) : STATUS_OK;
400 }
401
402 size_t Compressor::est_uint(size_t value, size_t initial, size_t stepping)
403 {
404 size_t bits = initial;
405 size_t est = 1;
406
407 while (true)
408 {
409 size_t max = (1 << bits);
410 if (value < max)
411 break;
412 est ++;
413 value -= max;
414 bits += stepping;
415 }
416
417 return est + bits;
418 }
419
420 size_t Compressor::calc_repeats(const uint8_t *head, const uint8_t *tail)
421 {
422 uint8_t b = head[-1];
423 const uint8_t *s= head;
424 while ((s < tail) && (*s == b))
425 ++s;
426
427 return s - head;
428 }
429
430 status_t Compressor::flush()
431 {
432 status_t res = sOut.flush();
433 if (res != STATUS_OK)
434 return res;
435
436 nSegment = sOS.position();
437 nOffset = 0;
438 sBuffer.clear(); // Clear state of the buffer
439
440 return STATUS_OK;
441 }
442 } /* namespaec resource */
443 } /* namespace lsp */
444
445