Debian Codegraph @

modules/lsp-runtime-lib/src/main/io/InBufStream.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: 17 янв. 2026 г.
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/io/InBufStream.h>
23 #include <lsp-plug.in/io/InFileStream.h>
24
25 namespace lsp
26 {
27 namespace io
28 {
29 InBufStream::InBufStream(size_t buf_size)
30 {
31 pIS = NULL;
32 vBuffer = NULL;
33 nBufCap = uint32_t(buf_size);
34 nBufHead = 0;
35 nBufOff = 0;
36 nBufSize = 0;
37 nPosition = 0;
38 nWrapFlags = EXT_BUFFERING;
39 }
40
41 InBufStream::~InBufStream()
42 {
43 do_close();
44 }
45
46 inline void InBufStream::clear_buffer()
47 {
48 nBufHead = 0;
49 nBufOff = 0;
50 nBufSize = 0;
51 }
52
53 status_t InBufStream::do_close()
54 {
55 status_t res = STATUS_OK;
56
57 // Close file descriptor
58 if (pIS != NULL)
59 {
60 // Perform close
61 if (nWrapFlags & WRAP_CLOSE)
62 res = pIS->close();
63 if (nWrapFlags & WRAP_DELETE)
64 delete pIS;
65 pIS = NULL;
66 }
67 nWrapFlags &= ~WRAP_FULL;
68
69 // Free the buffer
70 if (vBuffer != NULL)
71 {
72 free(vBuffer);
73 vBuffer = NULL;
74 }
75
76 clear_buffer();
77
78 return res;
79 }
80
81 status_t InBufStream::close()
82 {
83 if (pIS == NULL)
84 return STATUS_OK;
85
86 status_t res = do_close();
87 return set_error(res);
88 }
89
90 bool InBufStream::init_buffer()
91 {
92 if (nBufCap <= 0)
93 return false;
94
95 if (vBuffer == NULL)
96 {
97 vBuffer = static_cast<uint8_t *>(malloc(nBufCap));
98 if (vBuffer == NULL)
99 return false;
100 }
101
102 clear_buffer();
103
104 return true;
105 }
106
107 status_t InBufStream::do_wrap(IInStream *is, size_t flags)
108 {
109 // Obtain the position of the input stream
110 wssize_t pos = is->position();
111 if (pos < 0)
112 {
113 if (pos != -STATUS_NOT_IMPLEMENTED)
114 return pos;
115 else
116 pos = 0;
117 }
118
119 // Check buffer capacity
120 if (nBufCap <= 0)
121 return STATUS_BAD_STATE;
122
123 // Store pointers
124 pIS = is;
125 nWrapFlags = (nWrapFlags & (~uint32_t(WRAP_FULL))) | (uint32_t(flags) & WRAP_FULL);
126 nPosition = pos;
127
128 return STATUS_OK;
129 }
130
131 status_t InBufStream::open(const char *path)
132 {
133 if (pIS != NULL)
134 return set_error(STATUS_BAD_STATE);
135 if (!init_buffer())
136 return set_error(STATUS_NO_MEM);
137
138 status_t res;
139
140 InFileStream *ofs = new InFileStream();
141 if (ofs == NULL)
142 return set_error(STATUS_NO_MEM);
143
144 if ((res = ofs->open(path)) == STATUS_OK)
145 res = do_wrap(ofs, WRAP_CLOSE | WRAP_DELETE);
146
147 if (res != STATUS_OK)
148 {
149 ofs->close();
150 delete ofs;
151 }
152
153 return set_error(res);
154 }
155
156 status_t InBufStream::open(const LSPString *path)
157 {
158 if (pIS != NULL)
159 return set_error(STATUS_BAD_STATE);
160 if (!init_buffer())
161 return set_error(STATUS_NO_MEM);
162
163 status_t res;
164
165 InFileStream *ofs = new InFileStream();
166 if (ofs == NULL)
167 return set_error(STATUS_NO_MEM);
168
169 if ((res = ofs->open(path)) == STATUS_OK)
170 res = do_wrap(ofs, WRAP_CLOSE | WRAP_DELETE);
171
172 if (res != STATUS_OK)
173 {
174 ofs->close();
175 delete ofs;
176 }
177
178 return set_error(res);
179 }
180
181 status_t InBufStream::open(const Path *path)
182 {
183 if (pIS != NULL)
184 return set_error(STATUS_BAD_STATE);
185 if (!init_buffer())
186 return set_error(STATUS_NO_MEM);
187
188 status_t res;
189
190 InFileStream *ofs = new InFileStream();
191 if (ofs == NULL)
192 return set_error(STATUS_NO_MEM);
193
194 if ((res = ofs->open(path)) == STATUS_OK)
195 res = do_wrap(ofs, WRAP_CLOSE | WRAP_DELETE);
196
197 if (res != STATUS_OK)
198 {
199 ofs->close();
200 delete ofs;
201 }
202
203 return set_error(res);
204 }
205
206 status_t InBufStream::wrap(FILE *fd, bool close)
207 {
208 if (pIS != NULL)
209 return set_error(STATUS_BAD_STATE);
210 if (!init_buffer())
211 return set_error(STATUS_NO_MEM);
212
213 status_t res;
214
215 InFileStream *ofs = new InFileStream();
216 if (ofs == NULL)
217 return set_error(STATUS_NO_MEM);
218
219 if ((res = ofs->wrap(fd, close)) == STATUS_OK)
220 res = do_wrap(ofs, WRAP_CLOSE | WRAP_DELETE);
221
222 if (res != STATUS_OK)
223 {
224 ofs->close();
225 delete ofs;
226 }
227
228 return set_error(res);
229 }
230
231 status_t InBufStream::wrap_native(fhandle_t fd, bool close)
232 {
233 if (pIS != NULL)
234 return set_error(STATUS_BAD_STATE);
235 if (!init_buffer())
236 return set_error(STATUS_NO_MEM);
237
238 status_t res;
239
240 InFileStream *ofs = new InFileStream();
241 if (ofs == NULL)
242 return set_error(STATUS_NO_MEM);
243
244 if ((res = ofs->wrap_native(fd, close)) == STATUS_OK)
245 res = do_wrap(ofs, WRAP_CLOSE | WRAP_DELETE);
246
247 if (res != STATUS_OK)
248 {
249 ofs->close();
250 delete ofs;
251 }
252
253 return set_error(res);
254 }
255
256 status_t InBufStream::wrap(File *fd, size_t flags)
257 {
258 if (pIS != NULL)
259 return set_error(STATUS_BAD_STATE);
260 if (!init_buffer())
261 return set_error(STATUS_NO_MEM);
262
263 status_t res;
264
265 InFileStream *ofs = new InFileStream();
266 if (ofs == NULL)
267 return set_error(STATUS_NO_MEM);
268
269 if ((res = ofs->wrap(fd, flags)) == STATUS_OK)
270 res = do_wrap(ofs, WRAP_CLOSE | WRAP_DELETE);
271
272 if (res != STATUS_OK)
273 {
274 ofs->close();
275 delete ofs;
276 }
277
278 return set_error(res);
279 }
280
281 status_t InBufStream::wrap(IInStream *is, size_t flags)
282 {
283 if (pIS != NULL)
284 return set_error(STATUS_BAD_STATE);
285 else if (is == NULL)
286 return set_error(STATUS_BAD_ARGUMENTS);
287
288 if (!init_buffer())
289 return set_error(STATUS_NO_MEM);
290
291 return set_error(do_wrap(is, flags));
292 }
293
294 void InBufStream::set_buffering(bool enable)
295 {
296 nWrapFlags = lsp_setflag(nWrapFlags, EXT_BUFFERING, enable);
297 }
298
299 void InBufStream::set_seek_buffering(bool enable)
300 {
301 nWrapFlags = lsp_setflag(nWrapFlags, EXT_SEEK_BUFFERING, enable);
302 }
303
304 wssize_t InBufStream::avail()
305 {
306 const wssize_t res = pIS->avail();
307 if (res < 0)
308 return -set_error(status_t(-res));
309
310 return res + nBufSize - nBufOff;
311 }
312
313 // Fixing GCC 16.1 false positive warnings
314 #ifdef COMPILER_GCC
315 #pragma GCC diagnostic push
316 #pragma GCC diagnostic ignored "-Warray-bounds"
317 #endif /* COMPILER_GCC */
318 wssize_t InBufStream::position()
319 {
320 return nPosition + nBufOff;
321 }
322 #ifdef COMPILER_GCC
323 #pragma GCC diagnostic pop
324 #endif /* COMPILER_GCC */
325
326 ssize_t InBufStream::read_byte()
327 {
328 if (pIS == NULL)
329 return set_error(STATUS_BAD_STATE);
330
331 // Check that byte is cached in buffer
332 if (nBufOff < nBufSize)
333 {
334 const uint8_t byte = vBuffer[(nBufHead + nBufOff++) % nBufCap];
335 set_error(STATUS_OK);
336 return byte;
337 }
338
339 // Fetch new byte
340 ssize_t res = pIS->read_byte();
341 if (res < 0)
342 return -set_error(status_t(-res));
343
344 // Push byte to buffer
345 if (nWrapFlags & EXT_BUFFERING)
346 {
347 vBuffer[(nBufHead + nBufOff) % nBufCap] = uint8_t(res);
348 if (nBufOff >= nBufHead)
349 {
350 nBufHead = (nBufHead + 1) % nBufCap;
351 ++nPosition;
352 }
353 else
354 nBufSize = ++nBufOff;
355 }
356 else
357 {
358 nPosition += nBufSize + 1;
359 nBufSize = 0;
360 }
361
362 return res;
363 }
364
365 wssize_t InBufStream::seek(wsize_t position)
366 {
367 if (pIS == NULL)
368 return set_error(STATUS_BAD_STATE);
369
370 // Do quick positioning if possible
371 if ((position >= nPosition) && (position <= (nPosition + nBufSize)))
372 {
373 nBufOff = uint32_t(position - nPosition);
374 return position;
375 }
376
377 // Check that buffering is enabled
378 wssize_t res;
379 if ((nWrapFlags & (EXT_BUFFERING | EXT_SEEK_BUFFERING)) != (EXT_BUFFERING | EXT_SEEK_BUFFERING))
380 {
381 if ((res = pIS->seek(position)) < 0)
382 return -set_error(-status_t(res));
383
384 clear_buffer();
385 nPosition = res;
386 return res;
387 }
388
389 // Forward seek?
390 if (position >= nPosition)
391 {
392 res = skip(position - nBufSize + nBufOff);
393 if (res < 0)
394 return res;
395
396 return this->position();
397 }
398
399 // Backward seek
400 const wssize_t start = lsp_max(wssize_t(position) - wssize_t(nBufCap), wssize_t(0));
401 if ((res = pIS->seek(start)) < 0)
402 return -set_error(-status_t(res));
403
404 // Perform read
405 ssize_t nread = pIS->read(vBuffer, nBufCap);
406 if (nread < 0)
407 return -set_error(-status_t(nread));
408 else if (wsize_t(start + nread) < position)
409 return -set_error(STATUS_CORRUPTED);
410
411 // Update buffer position
412 nBufHead = 0;
413 nBufOff = uint32_t(position - start);
414 nBufSize = uint32_t(nread);
415 nPosition = start;
416
417 set_error(STATUS_OK);
418
419 return position;
420 }
421
422 ssize_t InBufStream::read(void *dst, size_t count)
423 {
424 if (pIS == NULL)
425 return set_error(STATUS_BAD_STATE);
426
427 size_t avail, head, split;
428 size_t nread = 0;
429 uint8_t *dptr = static_cast<uint8_t *>(dst);
430
431 // Check that bytes are cached in buffer
432 avail = uint32_t(lsp_min(count, size_t(nBufSize - nBufOff)));
433 if (avail > 0)
434 {
435 head = (nBufHead + nBufOff) % nBufCap;
436 if ((head + avail) > nBufCap)
437 {
438 split = nBufCap - head;
439 memcpy(dptr, &vBuffer[head], split);
440 memcpy(&dptr[split], vBuffer, avail - split);
441 }
442 else
443 memcpy(dptr, &vBuffer[head], avail);
444
445 nBufOff += uint32_t(avail);
446 nread += avail;
447 dptr += avail;
448 }
449 if (nread >= count)
450 {
451 set_error(STATUS_OK);
452 return nread;
453 }
454
455 // Perform read
456 ssize_t res = pIS->read(dptr, count - nread);
457 if (res < 0)
458 return (nread > 0) ? ssize_t(nread) : -set_error(status_t(-res));
459 nread += ssize_t(res);
460
461 // Fill buffer with the data
462 if (nWrapFlags & EXT_BUFFERING)
463 {
464 avail = nBufCap - nBufOff;
465
466 if (size_t(res) >= nBufCap)
467 {
468 // Need to replace full buffer
469 dptr += res - nBufCap;
470
471 memcpy(vBuffer, dptr, nBufCap);
472 nPosition = nPosition + nBufSize + res - nBufCap;
473 nBufSize = nBufCap;
474 nBufHead = 0;
475 nBufOff = nBufCap;
476 }
477 else
478 {
479 // Append to the end of buffer
480 avail = lsp_min(size_t(res), size_t(nBufCap));
481 dptr += res - avail;
482
483 // Copy data
484 head = (nBufHead + nBufOff) % nBufCap;
485 if ((head + avail) > nBufCap)
486 {
487 split = nBufCap - head;
488 memcpy(&vBuffer[head], dptr, split);
489 memcpy(vBuffer, &dptr[split], avail - split);
490 }
491 else
492 memcpy(&vBuffer[head], dptr, avail);
493
494 // Update position
495 nBufOff += res;
496 if (nBufOff > nBufCap)
497 {
498 split = nBufOff % nBufCap;
499 nBufHead = (nBufHead + split) % nBufCap;
500 nBufSize = nBufCap;
501 nBufOff = nBufCap;
502 nPosition += split;
503 }
504 else
505 nBufSize = nBufOff;
506 }
507 }
508 else
509 {
510 nPosition += nBufSize + nread;
511 nBufSize = 0;
512 }
513
514 return nread;
515 }
516
517 wssize_t InBufStream::skip(wsize_t amount)
518 {
519 if (pIS == NULL)
520 return set_error(STATUS_BAD_STATE);
521
522 // Do quick skip if possible
523 uint32_t head, split;
524 wsize_t skipped = 0;
525
526 if (nBufOff < nBufSize)
527 {
528 split = uint32_t(lsp_min(amount, wsize_t(nBufSize - nBufOff)));
529 nBufOff += uint32_t(split);
530 skipped += split;
531 if (skipped >= amount)
532 return skipped;
533 }
534
535 // Check that buffering is enabled
536 if ((nWrapFlags & (EXT_BUFFERING | EXT_SEEK_BUFFERING)) != (EXT_BUFFERING | EXT_SEEK_BUFFERING))
537 {
538 wssize_t res = pIS->skip(amount - skipped);
539 if (res < 0)
540 return -set_error(-status_t(res));
541
542 nPosition = nPosition + nBufOff + res;
543 clear_buffer();
544
545 return res;
546 }
547
548 // Do buffered skip using underlying read() operations
549 while (skipped < amount)
550 {
551 head = (nBufHead + nBufOff) % nBufCap;
552 split = uint32_t(lsp_min(amount - skipped, wsize_t(nBufCap - head)));
553 ssize_t nread = pIS->read(&vBuffer[head], split);
554 if (nread <= 0)
555 return skipped;
556
557 nBufOff += uint32_t(nread);
558 if (nBufOff > nBufCap)
559 {
560 split = nBufOff - nBufCap;
561 nBufHead += split;
562 nBufSize = nBufCap;
563 nBufOff = nBufCap;
564 nPosition += split;
565 }
566 else
567 nBufSize = nBufOff;
568
569 skipped += nread;
570 }
571
572 return skipped;
573 }
574
575 } /* namespaec io */
576 } /* namespace lsp */
577
578