Debian Codegraph @

modules/lsp-runtime-lib/src/main/io/InBitStream.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: 14 мар. 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/endian.h>
23 #include <lsp-plug.in/io/InBitStream.h>
24 #include <lsp-plug.in/io/InFileStream.h>
25
26 namespace lsp
27 {
28 namespace io
29 {
30 static constexpr size_t BITSTREAM_BUFSZ = sizeof(umword_t) * 8;
31 static constexpr size_t BITSTREAM_BUFSZ32 = sizeof(uint32_t) * 8;
32
33 InBitStream::InBitStream()
34 {
35 pIS = NULL;
36 nWrapFlags = 0;
37 nBuffer = 0;
38 nBits = 0;
39 }
40
41 InBitStream::~InBitStream()
42 {
43 do_close();
44 }
45
46 status_t InBitStream::wrap(FILE *fd, bool close)
47 {
48 if (pIS != NULL)
49 return set_error(STATUS_BAD_STATE);
50
51 status_t res;
52
53 InFileStream *ofs = new InFileStream();
54 if (ofs == NULL)
55 return set_error(STATUS_NO_MEM);
56
57 if ((res = ofs->wrap(fd, close)) == STATUS_OK)
58 res = wrap(ofs, WRAP_CLOSE | WRAP_DELETE);
59
60 if (res != STATUS_OK)
61 {
62 ofs->close();
63 delete ofs;
64 }
65
66 return res;
67 }
68
69 status_t InBitStream::wrap_native(fhandle_t fd, bool close)
70 {
71 if (pIS != NULL)
72 return set_error(STATUS_BAD_STATE);
73
74 status_t res;
75
76 InFileStream *ofs = new InFileStream();
77 if (ofs == NULL)
78 return set_error(STATUS_NO_MEM);
79
80 if ((res = ofs->wrap_native(fd, close)) == STATUS_OK)
81 res = wrap(ofs, WRAP_CLOSE | WRAP_DELETE);
82
83 if (res != STATUS_OK)
84 {
85 ofs->close();
86 delete ofs;
87 }
88
89 return res;
90 }
91
92 status_t InBitStream::wrap(File *fd, size_t flags)
93 {
94 if (pIS != NULL)
95 return set_error(STATUS_BAD_STATE);
96
97 status_t res;
98
99 InFileStream *ofs = new InFileStream();
100 if (ofs == NULL)
101 return set_error(STATUS_NO_MEM);
102
103 if ((res = ofs->wrap(fd, flags)) == STATUS_OK)
104 res = wrap(ofs, WRAP_CLOSE | WRAP_DELETE);
105
106 if (res != STATUS_OK)
107 {
108 ofs->close();
109 delete ofs;
110 }
111
112 return res;
113 }
114
115 status_t InBitStream::open(const char *path)
116 {
117 if (pIS != NULL)
118 return set_error(STATUS_BAD_STATE);
119
120 status_t res;
121
122 InFileStream *ofs = new InFileStream();
123 if (ofs == NULL)
124 return set_error(STATUS_NO_MEM);
125
126 if ((res = ofs->open(path)) == STATUS_OK)
127 res = wrap(ofs, WRAP_CLOSE | WRAP_DELETE);
128
129 if (res != STATUS_OK)
130 {
131 ofs->close();
132 delete ofs;
133 }
134
135 return res;
136 }
137
138 status_t InBitStream::open(const LSPString *path)
139 {
140 if (pIS != NULL)
141 return set_error(STATUS_BAD_STATE);
142
143 status_t res;
144
145 InFileStream *ofs = new InFileStream();
146 if (ofs == NULL)
147 return set_error(STATUS_NO_MEM);
148
149 if ((res = ofs->open(path)) == STATUS_OK)
150 res = wrap(ofs, WRAP_CLOSE | WRAP_DELETE);
151
152 if (res != STATUS_OK)
153 {
154 ofs->close();
155 delete ofs;
156 }
157
158 return res;
159 }
160
161
162 status_t InBitStream::open(const Path *path)
163 {
164 if (pIS != NULL)
165 return set_error(STATUS_BAD_STATE);
166
167 status_t res;
168
169 InFileStream *ofs = new InFileStream();
170 if (ofs == NULL)
171 return set_error(STATUS_NO_MEM);
172
173 if ((res = ofs->open(path)) == STATUS_OK)
174 res = wrap(ofs, WRAP_CLOSE | WRAP_DELETE);
175
176 if (res != STATUS_OK)
177 {
178 ofs->close();
179 delete ofs;
180 }
181
182 return res;
183 }
184
185 status_t InBitStream::wrap(IInStream *is, size_t flags)
186 {
187 if (pIS != NULL)
188 return set_error(STATUS_BAD_STATE);
189 else if (is == NULL)
190 return set_error(STATUS_BAD_ARGUMENTS);
191
192 // Store pointers
193 pIS = is;
194 nWrapFlags = flags;
195 nBuffer = 0;
196 nBits = 0;
197
198 return set_error(STATUS_OK);
199 }
200
201 status_t InBitStream::do_close()
202 {
203 status_t res = STATUS_OK;
204
205 // Close file descriptor
206 if (pIS != NULL)
207 {
208 // Perform close
209 if (nWrapFlags & WRAP_CLOSE)
210 res = pIS->close();
211 if (nWrapFlags & WRAP_DELETE)
212 delete pIS;
213 pIS = NULL;
214 }
215
216 nWrapFlags = 0;
217 nBuffer = 0;
218 nBits = 0;
219
220 return res;
221 }
222
223 status_t InBitStream::close()
224 {
225 return set_error(do_close());
226 }
227
228 ssize_t InBitStream::read(void *dst, size_t count)
229 {
230 ssize_t nread = bread(dst, count * 8);
231 if (nread < 0)
232 return nread;
233
234 // Need to unget something?
235 if (nread & 7)
236 {
237 uint8_t *p = reinterpret_cast<uint8_t *>(dst);
238 unread(p[nread >> 3], nread & 7);
239 }
240
241 return nread >> 3;
242 }
243
244 ssize_t InBitStream::bread(void *buf, size_t bits)
245 {
246 if (pIS == NULL)
247 return -set_error(STATUS_CLOSED);
248
249 uint8_t *dst = reinterpret_cast<uint8_t *>(buf);
250 size_t nread = 0;
251
252 while (nread < bits)
253 {
254 size_t to_read = lsp_min(bits - nread, 8u);
255 ssize_t n = readv(dst++, to_read);
256 if (n < 0)
257 {
258 if (nread > 0)
259 break;
260 set_error(status_t(-n));
261 return n;
262 }
263
264 nread += n;
265 if (n < 8)
266 break;
267 }
268
269 set_error(STATUS_OK);
270 return nread;
271 }
272
273 wssize_t InBitStream::bskip(wsize_t amount)
274 {
275 if (pIS == NULL)
276 return -set_error(STATUS_CLOSED);
277
278 wsize_t skipped = 0;
279
280 // Simple skip?
281 if (amount <= nBits)
282 {
283 nBuffer <<= amount;
284 nBits -= amount;
285 return amount;
286 }
287 else
288 {
289 skipped = nBits;
290 nBits = 0;
291 amount -= skipped;
292 }
293
294 // Can skip bytes?
295 wssize_t bytes = amount >> 3;
296 while (bytes > 0)
297 {
298 wssize_t n = pIS->skip(bytes);
299 if (n < 0)
300 {
301 if (skipped > 0)
302 break;
303 set_error(status_t(-n));
304 return n;
305 }
306 bytes -= n;
307 skipped += n << 3;
308 amount -= n << 3;
309 }
310
311 // Tail left?
312 if (amount > 0)
313 {
314 umword_t v;
315 ssize_t n = readv(&v, amount);
316 if ((n < 0) && (skipped <= 0))
317 {
318 set_error(status_t(-n));
319 return n;
320 }
321 }
322
323 set_error(STATUS_OK);
324 return skipped;
325 }
326
327 ssize_t InBitStream::readb(bool *value)
328 {
329 status_t res;
330 if (pIS == NULL)
331 return -set_error(STATUS_CLOSED);
332
333 // Fill buffer with new data
334 if ((res = fill()) != STATUS_OK)
335 return -set_error(res);
336
337 *value = nBuffer >> (BITSTREAM_BUFSZ - 1);
338 nBuffer <<= 1;
339 --nBits;
340
341 set_error(STATUS_OK);
342 return 1;
343 }
344
345 ssize_t InBitStream::readv(uint8_t *value, size_t bits)
346 {
347 umword_t v;
348 ssize_t n = readv(&v, bits);
349 if (n > 0)
350 *value = uint8_t(v);
351 return n;
352 }
353
354 ssize_t InBitStream::readv(uint16_t *value, size_t bits)
355 {
356 umword_t v;
357 ssize_t n = readv(&v, bits);
358 if (n > 0)
359 *value = uint16_t(v);
360 return n;
361 }
362
363 ssize_t InBitStream::readv(uint32_t *value, size_t bits)
364 {
365 if (pIS == NULL)
366 return -set_error(STATUS_CLOSED);
367
368 size_t nread = 0;
369 status_t res;
370 uint32_t v = 0;
371
372 while (nread < bits)
373 {
374 // Fill buffer with new data
375 if ((res = fill()) != STATUS_OK)
376 {
377 if (nread > 0)
378 break;
379 return -set_error(res);
380 }
381
382 // Estimate number of bits to read
383 const size_t to_read= lsp_min(nBits, bits - nread);
384 uint32_t x = uint32_t(nBuffer >> (BITSTREAM_BUFSZ - to_read));
385 v = (to_read < BITSTREAM_BUFSZ32) ? uint32_t((v << to_read) | x) : x;
386 nBuffer <<= to_read;
387 nBits -= to_read;
388 nread += to_read;
389 }
390
391 *value = v;
392 set_error(STATUS_OK);
393 return nread;
394 }
395
396 ssize_t InBitStream::readv(uint64_t *value, size_t bits)
397 {
398 if (pIS == NULL)
399 return -set_error(STATUS_CLOSED);
400
401 size_t nread = 0;
402 status_t res;
403 uint64_t v = 0;
404
405 while (nread < bits)
406 {
407 // Fill buffer with new data
408 if ((res = fill()) != STATUS_OK)
409 {
410 if (nread > 0)
411 break;
412 return -set_error(res);
413 }
414
415 // Estimate number of bits to read
416 size_t to_read = lsp_min(nBits, bits - nread);
417 v = (to_read < BITSTREAM_BUFSZ) ? (v << to_read) | (nBuffer >> (BITSTREAM_BUFSZ - to_read)) : nBuffer;
418 nBuffer <<= to_read;
419 nBits -= to_read;
420 nread += to_read;
421 }
422
423 *value = v;
424 set_error(STATUS_OK);
425 return nread;
426 }
427
428 void InBitStream::unread(umword_t v, size_t bits)
429 {
430 if (bits < BITSTREAM_BUFSZ)
431 v <<= BITSTREAM_BUFSZ - bits;
432 nBuffer = (nBuffer >> bits) | v;
433 nBits += bits;
434 }
435
436 status_t InBitStream::fill()
437 {
438 if (nBits > 0)
439 return STATUS_OK;
440
441 nBuffer = 0;
442 ssize_t n = pIS->read(&nBuffer, sizeof(umword_t));
443 if (n <= 0)
444 return status_t(-n);
445
446 nBits = n << 3;
447 nBuffer = BE_TO_CPU(nBuffer);
448
449 return STATUS_OK;
450 }
451
452 } /* namespace io */
453 } /* namespace lsp */
454
455