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