| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 8 мар. 2019 г. |
| 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/CharsetEncoder.h> |
| 23 |
#include <errno.h> |
| 24 |
|
| 25 |
#define DATA_BUFSIZE 0x1000 |
| 26 |
|
| 27 |
namespace lsp |
| 28 |
{ |
| 29 |
namespace io |
| 30 |
{ |
| 31 |
|
| 32 |
CharsetEncoder::CharsetEncoder() |
| 33 |
{ |
| 34 |
bBuffer = NULL; |
| 35 |
bBufHead = NULL; |
| 36 |
bBufTail = NULL; |
| 37 |
|
| 38 |
cBuffer = NULL; |
| 39 |
cBufHead = NULL; |
| 40 |
cBufTail = NULL; |
| 41 |
|
| 42 |
#if defined(PLATFORM_WINDOWS) |
| 43 |
xBuffer = NULL; |
| 44 |
nCodePage = uint32_t(-1); |
| 45 |
#else |
| 46 |
hIconv = iconv_t(-1); |
| 47 |
#endif /* PLATFORM_WINDOWS */ |
| 48 |
} |
| 49 |
|
| 50 |
CharsetEncoder::~CharsetEncoder() |
| 51 |
{ |
| 52 |
close(); |
| 53 |
} |
| 54 |
|
| 55 |
status_t CharsetEncoder::init(const char *charset) |
| 56 |
{ |
| 57 |
#if defined(PLATFORM_WINDOWS) |
| 58 |
if (nCodePage != uint32_t(-1)) |
| 59 |
return STATUS_BAD_STATE; |
| 60 |
|
| 61 |
ssize_t cp = codepage_from_name(charset); |
| 62 |
if (cp < 0) |
| 63 |
return STATUS_BAD_LOCALE; |
| 64 |
nCodePage = cp; |
| 65 |
#else |
| 66 |
if (hIconv != iconv_t(-1)) |
| 67 |
return STATUS_BAD_STATE; |
| 68 |
|
| 69 |
iconv_t handle = init_iconv_from_wchar_t(charset); |
| 70 |
if (handle == iconv_t(-1)) |
| 71 |
return STATUS_BAD_LOCALE; |
| 72 |
hIconv = handle; |
| 73 |
#endif /* PLATFORM_WINDOWS */ |
| 74 |
|
| 75 |
// Allocate buffer |
| 76 |
uint8_t *buf= reinterpret_cast<uint8_t *>(::malloc( |
| 77 |
sizeof(lsp_utf32_t) * DATA_BUFSIZE * 2 // Byte buffer size |
| 78 |
+ sizeof(lsp_wchar_t) * DATA_BUFSIZE // wchar_t buffer size |
| 79 |
#if defined(PLATFORM_WINDOWS) |
| 80 |
+ sizeof(lsp_utf16_t) * DATA_BUFSIZE * 2 |
| 81 |
#endif /* PLATFORM_WINDOWS */ |
| 82 |
)); |
| 83 |
if (buf == NULL) |
| 84 |
{ |
| 85 |
close(); |
| 86 |
return STATUS_NO_MEM; |
| 87 |
} |
| 88 |
|
| 89 |
bBuffer = buf; |
| 90 |
bBufHead = bBuffer; |
| 91 |
bBufTail = bBuffer; |
| 92 |
|
| 93 |
buf += sizeof(lsp_utf32_t) * DATA_BUFSIZE * 2; |
| 94 |
cBuffer = reinterpret_cast<lsp_wchar_t *>(buf); |
| 95 |
cBufHead = cBuffer; |
| 96 |
cBufTail = cBuffer; |
| 97 |
|
| 98 |
#if defined(PLATFORM_WINDOWS) |
| 99 |
buf += sizeof(lsp_wchar_t) * DATA_BUFSIZE; |
| 100 |
xBuffer = reinterpret_cast<lsp_utf16_t *>(buf); |
| 101 |
#endif /* PLATFORM_WINDOWS */ |
| 102 |
|
| 103 |
return STATUS_OK; |
| 104 |
} |
| 105 |
|
| 106 |
void CharsetEncoder::close() |
| 107 |
{ |
| 108 |
if (bBuffer != NULL) |
| 109 |
{ |
| 110 |
free(bBuffer); |
| 111 |
|
| 112 |
bBuffer = NULL; |
| 113 |
bBufHead = NULL; |
| 114 |
bBufTail = NULL; |
| 115 |
cBuffer = NULL; |
| 116 |
cBufHead = NULL; |
| 117 |
cBufTail = NULL; |
| 118 |
} |
| 119 |
|
| 120 |
#if defined(PLATFORM_WINDOWS) |
| 121 |
xBuffer = NULL; |
| 122 |
nCodePage = uint32_t(-1); |
| 123 |
#else |
| 124 |
if (hIconv != iconv_t(-1)) |
| 125 |
{ |
| 126 |
::iconv_close(hIconv); |
| 127 |
hIconv = iconv_t(-1); |
| 128 |
} |
| 129 |
#endif /* PLATFORM_WINDOWS */ |
| 130 |
} |
| 131 |
|
| 132 |
#if 0 |
| 133 |
ssize_t CharsetEncoder::encode(void **outbuf, size_t *outleft, lsp_wchar_t **inbuf, size_t *inleft) |
| 134 |
{ |
| 135 |
size_t nconv; |
| 136 |
#if defined(PLATFORM_WINDOWS) |
| 137 |
lsp_wchar_t *xinbuf = *inbuf; |
| 138 |
CHAR *xoutbuf = reinterpret_cast<CHAR *>(*outbuf); |
| 139 |
size_t xinleft = *inleft; |
| 140 |
size_t xoutleft = *outleft; |
| 141 |
nconv = 0; |
| 142 |
|
| 143 |
while (xoutleft > 0) |
| 144 |
{ |
| 145 |
// Perform optimistic UTF-32 -> UTF-16 encoding of the whole buffer |
| 146 |
size_t nsrc = (xinleft > DATA_BUFSIZE) ? DATA_BUFSIZE : xinleft; |
| 147 |
size_t ndst = DATA_BUFSIZE; |
| 148 |
ssize_t nchars = utf32_to_utf16(cBuffer, &ndst, xinbuf, &nsrc, false); |
| 149 |
if (nchars <= 0) |
| 150 |
{ |
| 151 |
if (nconv <= 0) |
| 152 |
return nchars; |
| 153 |
break; |
| 154 |
} |
| 155 |
|
| 156 |
// Perform safe UTF-16 -> native encoding |
| 157 |
size_t xnchars = nchars; |
| 158 |
ssize_t nbytes = widechar_to_multibyte(nCodePage, cBuffer, &xnchars, xoutbuf, &xoutleft); |
| 159 |
if (nbytes <= 0) |
| 160 |
{ |
| 161 |
if (nconv <= 0) |
| 162 |
return nbytes; |
| 163 |
break; |
| 164 |
} |
| 165 |
nchars -= xnchars; |
| 166 |
|
| 167 |
// Update statistics |
| 168 |
xinbuf += nchars; |
| 169 |
xinleft -= nchars; |
| 170 |
xoutbuf += nbytes; |
| 171 |
} |
| 172 |
|
| 173 |
*outbuf = xoutbuf; |
| 174 |
*outleft = xoutleft; |
| 175 |
*inbuf = reinterpret_cast<lsp_wchar_t *>(xinbuf); |
| 176 |
*inleft = xinleft; |
| 177 |
#else |
| 178 |
char *xinbuf = reinterpret_cast<char *>(*inbuf); |
| 179 |
char *xoutbuf = reinterpret_cast<char *>(*outbuf); |
| 180 |
size_t xinleft = *inleft * sizeof(lsp_wchar_t); |
| 181 |
size_t xoutleft = *outleft; |
| 182 |
|
| 183 |
// Perform conversion |
| 184 |
nconv = ::iconv(hIconv, &xinbuf, &xinleft, &xoutbuf, &xoutleft); |
| 185 |
if (nconv == size_t(-1)) |
| 186 |
{ |
| 187 |
int code = errno; |
| 188 |
switch (code) |
| 189 |
{ |
| 190 |
case E2BIG: |
| 191 |
case EINVAL: |
| 192 |
nconv = *inleft - (xinleft / sizeof(lsp_wchar_t)); |
| 193 |
break; |
| 194 |
default: |
| 195 |
return -STATUS_BAD_FORMAT; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
// Update pointers and values |
| 200 |
*outbuf = xoutbuf; |
| 201 |
*outleft = xoutleft; |
| 202 |
*inbuf = reinterpret_cast<lsp_wchar_t *>(xinbuf); |
| 203 |
*inleft = xinleft / sizeof(lsp_wchar_t); |
| 204 |
#endif /* PLATFORM_WINDOWS */ |
| 205 |
|
| 206 |
return nconv; |
| 207 |
} |
| 208 |
|
| 209 |
#endif |
| 210 |
|
| 211 |
size_t CharsetEncoder::prepare_buffer() |
| 212 |
{ |
| 213 |
size_t bufsz = cBufTail - cBufHead; |
| 214 |
if (bufsz > (DATA_BUFSIZE >> 1)) |
| 215 |
return 0; |
| 216 |
else if (cBufHead != cBuffer) |
| 217 |
{ |
| 218 |
if (bufsz > 0) |
| 219 |
::memmove(cBuffer, cBufHead, bufsz * sizeof(lsp_wchar_t)); |
| 220 |
|
| 221 |
cBufHead = cBuffer; |
| 222 |
cBufTail = &cBuffer[bufsz]; |
| 223 |
} |
| 224 |
return DATA_BUFSIZE - bufsz; |
| 225 |
} |
| 226 |
|
| 227 |
ssize_t CharsetEncoder::encode_buffer() |
| 228 |
{ |
| 229 |
// Prepare buffer |
| 230 |
size_t bufsz = bBufTail - bBufHead; |
| 231 |
if (bufsz > DATA_BUFSIZE * sizeof(lsp_utf32_t)) |
| 232 |
return bufsz; |
| 233 |
else if (bBufHead != bBuffer) |
| 234 |
{ |
| 235 |
if (bufsz > 0) |
| 236 |
::memmove(bBuffer, bBufHead, bufsz); |
| 237 |
|
| 238 |
bBufHead = bBuffer; |
| 239 |
bBufTail = &bBuffer[bufsz]; |
| 240 |
} |
| 241 |
|
| 242 |
// Is there any data in byte buffer? |
| 243 |
size_t xinleft = cBufTail - cBufHead; |
| 244 |
if (!xinleft) |
| 245 |
return bufsz; |
| 246 |
|
| 247 |
#ifdef PLATFORM_WINDOWS |
| 248 |
// Round 1: encode UTF-32 -> UTF-16 |
| 249 |
size_t nsrc = xinleft; |
| 250 |
size_t ndst = DATA_BUFSIZE*2; |
| 251 |
ssize_t nchars = utf32_to_utf16(xBuffer, &ndst, cBufHead, &nsrc, false); |
| 252 |
if (nchars <= 0) |
| 253 |
return nchars; |
| 254 |
lsp_wchar_t *bhead = &cBufHead[xinleft - nsrc]; |
| 255 |
|
| 256 |
// Round 2: encode UTF-16 -> native charset |
| 257 |
nsrc = DATA_BUFSIZE*2 - ndst; |
| 258 |
ndst = DATA_BUFSIZE * sizeof(lsp_utf32_t); |
| 259 |
char *xoutbuf = reinterpret_cast<char *>(bBufTail); |
| 260 |
ssize_t nbytes = widechar_to_multibyte(nCodePage, xBuffer, &nsrc, xoutbuf, &ndst); |
| 261 |
if (nbytes <= 0) |
| 262 |
return nbytes; |
| 263 |
|
| 264 |
cBufHead = bhead; |
| 265 |
bBufTail += nbytes; |
| 266 |
#else |
| 267 |
char *xinbuf = reinterpret_cast<char *>(cBufHead); |
| 268 |
char *xoutbuf = reinterpret_cast<char *>(bBufTail); |
| 269 |
bufsz = DATA_BUFSIZE * sizeof(lsp_utf32_t); |
| 270 |
xinleft *= sizeof(lsp_wchar_t); |
| 271 |
|
| 272 |
// Perform conversion |
| 273 |
size_t nconv = ::iconv(hIconv, &xinbuf, &xinleft, &xoutbuf, &bufsz); |
| 274 |
if (nconv == size_t(-1)) |
| 275 |
{ |
| 276 |
int code = errno; |
| 277 |
switch (code) |
| 278 |
{ |
| 279 |
case E2BIG: |
| 280 |
case EINVAL: |
| 281 |
break; |
| 282 |
default: |
| 283 |
return -STATUS_BAD_FORMAT; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
cBufHead = reinterpret_cast<lsp_wchar_t *>(xinbuf); |
| 288 |
bBufTail = reinterpret_cast<uint8_t *>(xoutbuf); |
| 289 |
#endif |
| 290 |
return bBufTail - bBufHead; |
| 291 |
} |
| 292 |
|
| 293 |
ssize_t CharsetEncoder::fill(lsp_wchar_t ch) |
| 294 |
{ |
| 295 |
if (bBuffer == NULL) |
| 296 |
return -STATUS_CLOSED; |
| 297 |
|
| 298 |
// Is there a space in the buffer for reading? |
| 299 |
size_t bufsz = prepare_buffer(); |
| 300 |
if (bufsz <= 0) |
| 301 |
return bufsz; |
| 302 |
|
| 303 |
*(cBufTail++) = ch; |
| 304 |
return 1; |
| 305 |
} |
| 306 |
|
| 307 |
ssize_t CharsetEncoder::fill(const lsp_wchar_t *buf, size_t count) |
| 308 |
{ |
| 309 |
if (bBuffer == NULL) |
| 310 |
return -STATUS_CLOSED; |
| 311 |
else if (buf == NULL) |
| 312 |
return -STATUS_BAD_ARGUMENTS; |
| 313 |
|
| 314 |
// Is there a space in the buffer for reading? |
| 315 |
size_t bufsz = prepare_buffer(); |
| 316 |
if (bufsz <= 0) |
| 317 |
return bufsz; |
| 318 |
|
| 319 |
if (count > bufsz) |
| 320 |
count = bufsz; |
| 321 |
::memcpy(cBufTail, buf, count * sizeof(lsp_wchar_t)); |
| 322 |
cBufTail += count; |
| 323 |
return count; |
| 324 |
} |
| 325 |
|
| 326 |
ssize_t CharsetEncoder::fill(const char *buf, size_t count) |
| 327 |
{ |
| 328 |
if (bBuffer == NULL) |
| 329 |
return -STATUS_CLOSED; |
| 330 |
else if (buf == NULL) |
| 331 |
return -STATUS_BAD_ARGUMENTS; |
| 332 |
|
| 333 |
// Is there a space in the buffer for reading? |
| 334 |
size_t bufsz = prepare_buffer(); |
| 335 |
if (bufsz <= 0) |
| 336 |
return bufsz; |
| 337 |
|
| 338 |
if (count > bufsz) |
| 339 |
count = bufsz; |
| 340 |
for (size_t i=0; i<count; ++i) // Copy ASCII data |
| 341 |
cBufTail[i] = uint8_t(buf[i]); |
| 342 |
cBufTail += count; |
| 343 |
return count; |
| 344 |
} |
| 345 |
|
| 346 |
ssize_t CharsetEncoder::fill(const LSPString *in, size_t first, size_t last) |
| 347 |
{ |
| 348 |
if (bBuffer == NULL) |
| 349 |
return -STATUS_CLOSED; |
| 350 |
else if ((in == NULL) || (first > last) || (last > in->length())) |
| 351 |
return -STATUS_BAD_ARGUMENTS; |
| 352 |
|
| 353 |
// Is there a space in the buffer for reading? |
| 354 |
size_t bufsz = prepare_buffer(); |
| 355 |
if (bufsz <= 0) |
| 356 |
return bufsz; |
| 357 |
|
| 358 |
size_t count = last - first; |
| 359 |
const lsp_wchar_t *buf = in->characters(); |
| 360 |
if (count > bufsz) |
| 361 |
count = bufsz; |
| 362 |
::memcpy(cBufTail, &buf[first], count * sizeof(lsp_wchar_t)); |
| 363 |
cBufTail += count; |
| 364 |
return count; |
| 365 |
} |
| 366 |
|
| 367 |
ssize_t CharsetEncoder::fill(IInSequence *in, size_t count) |
| 368 |
{ |
| 369 |
if (bBuffer == NULL) |
| 370 |
return -STATUS_CLOSED; |
| 371 |
else if (in == NULL) |
| 372 |
return -STATUS_BAD_ARGUMENTS; |
| 373 |
|
| 374 |
// Is there a space in the buffer for reading? |
| 375 |
size_t bufsz = prepare_buffer(); |
| 376 |
if (bufsz <= 0) |
| 377 |
return bufsz; |
| 378 |
|
| 379 |
// Compute the amount of data to read |
| 380 |
size_t read = 0; |
| 381 |
if ((!count) || (count > bufsz)) |
| 382 |
count = bufsz; |
| 383 |
|
| 384 |
// Perform read |
| 385 |
do |
| 386 |
{ |
| 387 |
ssize_t nread = in->read(cBufTail, count - read); |
| 388 |
if (nread <= 0) |
| 389 |
{ |
| 390 |
if (read > 0) // Ignore error if there is data on the input |
| 391 |
break; |
| 392 |
return nread; |
| 393 |
} |
| 394 |
|
| 395 |
cBufTail += nread; |
| 396 |
read += nread; |
| 397 |
} |
| 398 |
while (read < count); |
| 399 |
|
| 400 |
return read; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
ssize_t CharsetEncoder::fetch(void *buf, size_t count) |
| 405 |
{ |
| 406 |
if (bBuffer == NULL) |
| 407 |
return -STATUS_CLOSED; |
| 408 |
else if (buf == NULL) |
| 409 |
return -STATUS_BAD_ARGUMENTS; |
| 410 |
|
| 411 |
// Compute the amount of data to read |
| 412 |
size_t processed = 0; |
| 413 |
if (!count) |
| 414 |
count = DATA_BUFSIZE*2; |
| 415 |
|
| 416 |
// Perform read |
| 417 |
uint8_t *dst = reinterpret_cast<uint8_t *>(buf); |
| 418 |
|
| 419 |
while (processed < count) |
| 420 |
{ |
| 421 |
// Perform decoding |
| 422 |
ssize_t nbytes = encode_buffer(); |
| 423 |
if (nbytes <= 0) |
| 424 |
{ |
| 425 |
if (processed > 0) |
| 426 |
break; |
| 427 |
return nbytes; |
| 428 |
} |
| 429 |
|
| 430 |
// Write data to output sequence |
| 431 |
ssize_t to_copy = count - processed; |
| 432 |
if (nbytes > to_copy) |
| 433 |
nbytes = to_copy; |
| 434 |
::memcpy(dst, bBufHead, nbytes); |
| 435 |
|
| 436 |
// Update state |
| 437 |
bBufHead += nbytes; |
| 438 |
processed += nbytes; |
| 439 |
dst += nbytes; |
| 440 |
} |
| 441 |
|
| 442 |
return processed; |
| 443 |
} |
| 444 |
|
| 445 |
ssize_t CharsetEncoder::fetch(File *out, size_t count) |
| 446 |
{ |
| 447 |
if (bBuffer == NULL) |
| 448 |
return -STATUS_CLOSED; |
| 449 |
else if (out == NULL) |
| 450 |
return -STATUS_BAD_ARGUMENTS; |
| 451 |
|
| 452 |
// Compute the amount of data to read |
| 453 |
size_t processed = 0; |
| 454 |
if (!count) |
| 455 |
count = DATA_BUFSIZE*2; |
| 456 |
|
| 457 |
// Perform read |
| 458 |
while (processed < count) |
| 459 |
{ |
| 460 |
// Perform decoding |
| 461 |
ssize_t nbytes = encode_buffer(); |
| 462 |
if (nbytes <= 0) |
| 463 |
{ |
| 464 |
if (processed > 0) |
| 465 |
break; |
| 466 |
return nbytes; |
| 467 |
} |
| 468 |
|
| 469 |
// Write data to output sequence |
| 470 |
ssize_t to_copy = count - processed; |
| 471 |
if (nbytes > to_copy) |
| 472 |
nbytes = to_copy; |
| 473 |
nbytes = out->write(bBufHead, nbytes); |
| 474 |
if (nbytes < 0) |
| 475 |
{ |
| 476 |
if (processed > 0) |
| 477 |
break; |
| 478 |
return nbytes; |
| 479 |
} |
| 480 |
|
| 481 |
// Update state |
| 482 |
bBufHead += nbytes; |
| 483 |
processed += nbytes; |
| 484 |
} |
| 485 |
|
| 486 |
return processed; |
| 487 |
} |
| 488 |
|
| 489 |
ssize_t CharsetEncoder::fetch(IOutStream *out, size_t count) |
| 490 |
{ |
| 491 |
if (bBuffer == NULL) |
| 492 |
return -STATUS_CLOSED; |
| 493 |
else if (out == NULL) |
| 494 |
return -STATUS_BAD_ARGUMENTS; |
| 495 |
|
| 496 |
// Compute the amount of data to read |
| 497 |
size_t processed = 0; |
| 498 |
if (!count) |
| 499 |
count = DATA_BUFSIZE*2; |
| 500 |
|
| 501 |
// Perform read |
| 502 |
while (processed < count) |
| 503 |
{ |
| 504 |
// Perform decoding |
| 505 |
ssize_t nbytes = encode_buffer(); |
| 506 |
if (nbytes <= 0) |
| 507 |
{ |
| 508 |
if (processed > 0) |
| 509 |
break; |
| 510 |
return nbytes; |
| 511 |
} |
| 512 |
|
| 513 |
// Write data to output sequence |
| 514 |
ssize_t to_copy = count - processed; |
| 515 |
if (nbytes > to_copy) |
| 516 |
nbytes = to_copy; |
| 517 |
nbytes = out->write(bBufHead, nbytes); |
| 518 |
if (nbytes < 0) |
| 519 |
{ |
| 520 |
if (processed > 0) |
| 521 |
break; |
| 522 |
return nbytes; |
| 523 |
} |
| 524 |
|
| 525 |
// Update state |
| 526 |
bBufHead += nbytes; |
| 527 |
processed += nbytes; |
| 528 |
} |
| 529 |
|
| 530 |
return processed; |
| 531 |
} |
| 532 |
|
| 533 |
} /* namespace io */ |
| 534 |
} /* namespace lsp */ |