1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
|
/*
*
* Copyright (C) 2011-2024, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
*
* OFFIS e.V.
* R&D Division Health
* Escherweg 2
* D-26121 Oldenburg, Germany
*
*
* Module: ofstd
*
* Author: Joerg Riesmeier, Jan Schlamelcher
*
* Purpose: Class for character encoding conversion (Source)
*
*/
#include "dcmtk/config/osconfig.h"
#include "dcmtk/ofstd/ofchrenc.h"
#include "dcmtk/ofstd/ofstd.h"
#include "dcmtk/ofstd/ofdiag.h"
#include "dcmtk/ofstd/ofconsol.h"
#include <cerrno>
#ifdef HAVE_WINDOWS_H
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
/*-------------*
* constants *
*-------------*/
// Windows-specific code page identifiers
const unsigned int OFCharacterEncoding::CPC_ANSI = CP_ACP;
const unsigned int OFCharacterEncoding::CPC_OEM = CP_OEMCP;
const unsigned int OFCharacterEncoding::CPC_ASCII = 20127;
const unsigned int OFCharacterEncoding::CPC_Latin1 = 28591;
const unsigned int OFCharacterEncoding::CPC_UTF8 = CP_UTF8;
#endif
/*------------------*
* implementation *
*------------------*/
#ifdef DCMTK_ENABLE_CHARSET_CONVERSION
#if DCMTK_ENABLE_CHARSET_CONVERSION == DCMTK_CHARSET_CONVERSION_ICONV ||\
DCMTK_ENABLE_CHARSET_CONVERSION == DCMTK_CHARSET_CONVERSION_OFICONV ||\
DCMTK_ENABLE_CHARSET_CONVERSION == DCMTK_CHARSET_CONVERSION_STDLIBC_ICONV
#if DCMTK_ENABLE_CHARSET_CONVERSION == DCMTK_CHARSET_CONVERSION_OFICONV
#include "dcmtk/oficonv/iconv.h"
// helper class for cleanup up oficonv at application exit
class OFiconvCleanupHelper
{
public:
~OFiconvCleanupHelper()
{
OFiconv_cleanup();
}
};
// global helper object that will clean up the oficonv csmapper area buffer
// at application exit and thus avoid reports about memory leaks
OFiconvCleanupHelper cleanupHelper;
#undef LIBICONV_SECOND_ARGUMENT_CONST
#define iconv_open OFiconv_open
#define iconv_close OFiconv_close
#define iconv OFiconv
#define iconvctl OFiconvctl
#ifndef WITH_LIBICONV
#define WITH_LIBICONV
#endif
#else /* DCMTK_ENABLE_CHARSET_CONVERSION == DCMTK_CHARSET_CONVERSION_OFICONV */
#include <iconv.h>
#ifdef WITH_LIBICONV
#include <localcharset.h>
#endif
#ifdef __GLIBC__
#include <langinfo.h> // nl_langinfo / CODESET
#endif
#endif /* DCMTK_ENABLE_CHARSET_CONVERSION == DCMTK_CHARSET_CONVERSION_OFICONV */
#define ILLEGAL_DESCRIPTOR OFreinterpret_cast(iconv_t, -1)
#define CONVERSION_ERROR OFstatic_cast(size_t, -1)
#define CONVERSION_BUFFER_SIZE 1024
class OFCharacterEncoding::Implementation
{
public:
static Implementation* create(const OFString& fromEncoding,
const OFString& toEncoding,
OFCondition& result)
{
iconv_t descriptor = ::iconv_open(toEncoding.c_str(), fromEncoding.c_str());
if (descriptor == ILLEGAL_DESCRIPTOR)
{
// return an appropriate error message
createErrnoCondition(result, "Cannot open character encoding: ", EC_CODE_CannotOpenEncoding);
return OFnullptr;
}
#if DCMTK_ENABLE_CHARSET_CONVERSION == DCMTK_CHARSET_CONVERSION_OFICONV
// GNU libiconv does not perform transliteration by default. We emulate this when using oficonv.
int flag1 = 1;
(void) ::iconvctl(descriptor, ICONV_SET_ILSEQ_INVALID, &flag1);
#endif
if (Implementation* pImplementation = new Implementation(descriptor))
{
result = EC_Normal;
return pImplementation;
}
result = EC_MemoryExhausted;
return OFnullptr;
}
static OFString getVersionString()
{
#if DCMTK_ENABLE_CHARSET_CONVERSION == DCMTK_CHARSET_CONVERSION_OFICONV
return "Citrus iconv, Version FreeBSD 13.1 (modified)"
#ifdef DCMTK_ENABLE_BUILTIN_OFICONV_DATA
", with built-in data files"
#endif
;
#elif defined(WITH_LIBICONV)
OFString versionStr = "LIBICONV, Version ";
char buf[10];
// extract major and minor version number
OFStandard::snprintf(buf, sizeof(buf), "%i.%i", (_LIBICONV_VERSION >> 8), (_LIBICONV_VERSION & 0xff));
versionStr.append(buf);
return versionStr;
#elif defined(__GLIBC__)
OFOStringStream oss;
oss << "GNU C library (iconv), version "
<< __GLIBC__ << '.'
<< __GLIBC_MINOR__;
OFSTRINGSTREAM_GETOFSTRING(oss, version);
return version;
#else
return "Unknown C library (iconv)";
#endif
}
static OFString getLocaleEncoding()
{
#if DCMTK_ENABLE_CHARSET_CONVERSION == DCMTK_CHARSET_CONVERSION_OFICONV
iconv_locale_allocation_t buf;
return OFSTRING_GUARD(::OFlocale_charset(&buf));
#elif defined(WITH_LIBICONV)
// basically, the function below should always return a non-empty string
// but older versions of libiconv might return NULL in certain cases
return OFSTRING_GUARD(::locale_charset());
#elif defined(__GLIBC__)
const char *oldlocale = setlocale(LC_ALL, "");
const char *codeset = nl_langinfo (CODESET);
setlocale(LC_ALL, oldlocale);
return OFSTRING_GUARD(codeset);
#else
return OFString();
#endif
}
static OFBool supportsConversionFlags(const unsigned flags)
{
#if defined(WITH_LIBICONV) && _LIBICONV_VERSION >= 0x0108
return flags == AbortTranscodingOnIllegalSequence
|| flags == DiscardIllegalSequences
|| flags == TransliterateIllegalSequences
|| flags == (DiscardIllegalSequences | TransliterateIllegalSequences)
;
#elif defined DCMTK_FIXED_ICONV_CONVERSION_FLAGS
// the iconvctl function is implemented only in newer versions of the
// GNU libiconv and not in other iconv implementations. For instance,
// the iconv implementation in the C standard library does not support
// different encoding flags and only has a (varying) fixed functionality
// that we detect with a configuration test.
return flags == DCMTK_FIXED_ICONV_CONVERSION_FLAGS;
#else
return OFFalse;
#endif
}
unsigned getConversionFlags() const
{
#if defined(WITH_LIBICONV) && _LIBICONV_VERSION >= 0x0108
unsigned result = 0;
int flag;
#if DCMTK_ENABLE_CHARSET_CONVERSION == DCMTK_CHARSET_CONVERSION_OFICONV
if (::iconvctl(ConversionDescriptor, ICONV_GET_ILSEQ_INVALID, &flag))
return 0;
if (flag == 0)
result |= TransliterateIllegalSequences;
#else
if (::iconvctl(ConversionDescriptor, ICONV_GET_TRANSLITERATE, &flag))
return 0;
if (flag)
result |= TransliterateIllegalSequences;
#endif
if (::iconvctl(ConversionDescriptor, ICONV_GET_DISCARD_ILSEQ, &flag))
return 0;
if (flag)
result |= DiscardIllegalSequences;
if (result)
return result;
// no flags set, so return libiconv default (=abort)
return AbortTranscodingOnIllegalSequence;
#elif defined DCMTK_FIXED_ICONV_CONVERSION_FLAGS
// the iconvctl function is implemented only in newer versions of the
// GNU libiconv and not in other iconv implementations. For instance,
// the iconv implementation in the C standard library does not support
// different encoding flags and only has a (varying) fixed functionality
// that we detect with a configuration test.
return DCMTK_FIXED_ICONV_CONVERSION_FLAGS;
#else
return 0;
#endif
}
OFBool setConversionFlags(const unsigned flags)
{
#if defined(WITH_LIBICONV) && _LIBICONV_VERSION >= 0x0108
int flag0 = 0;
int flag1 = 1;
switch (flags)
{
case AbortTranscodingOnIllegalSequence:
#if DCMTK_ENABLE_CHARSET_CONVERSION == DCMTK_CHARSET_CONVERSION_OFICONV
if (::iconvctl(ConversionDescriptor, ICONV_SET_DISCARD_ILSEQ, &flag0))
return OFFalse;
if (::iconvctl(ConversionDescriptor, ICONV_SET_ILSEQ_INVALID, &flag1))
return OFFalse;
#else
if (::iconvctl(ConversionDescriptor, ICONV_SET_DISCARD_ILSEQ, &flag0))
return OFFalse;
if (::iconvctl(ConversionDescriptor, ICONV_SET_TRANSLITERATE, &flag0))
return OFFalse;
#endif
return OFTrue;
case DiscardIllegalSequences:
#if DCMTK_ENABLE_CHARSET_CONVERSION == DCMTK_CHARSET_CONVERSION_OFICONV
if (::iconvctl(ConversionDescriptor, ICONV_SET_DISCARD_ILSEQ, &flag1))
return OFFalse;
if (::iconvctl(ConversionDescriptor, ICONV_SET_ILSEQ_INVALID, &flag0))
return OFFalse;
#else
if (::iconvctl(ConversionDescriptor, ICONV_SET_DISCARD_ILSEQ, &flag1))
return OFFalse;
if (::iconvctl(ConversionDescriptor, ICONV_SET_TRANSLITERATE, &flag0))
return OFFalse;
#endif
return OFTrue;
case TransliterateIllegalSequences:
#if DCMTK_ENABLE_CHARSET_CONVERSION == DCMTK_CHARSET_CONVERSION_OFICONV
if (::iconvctl(ConversionDescriptor, ICONV_SET_DISCARD_ILSEQ, &flag0))
return OFFalse;
if (::iconvctl(ConversionDescriptor, ICONV_SET_ILSEQ_INVALID, &flag0))
return OFFalse;
#else
if (::iconvctl(ConversionDescriptor, ICONV_SET_DISCARD_ILSEQ, &flag0))
return OFFalse;
if (::iconvctl(ConversionDescriptor, ICONV_SET_TRANSLITERATE, &flag1))
return OFFalse;
#endif
return OFTrue;
case (TransliterateIllegalSequences | DiscardIllegalSequences):
#if DCMTK_ENABLE_CHARSET_CONVERSION == DCMTK_CHARSET_CONVERSION_OFICONV
if (::iconvctl(ConversionDescriptor, ICONV_SET_DISCARD_ILSEQ, &flag1))
return OFFalse;
if (::iconvctl(ConversionDescriptor, ICONV_SET_ILSEQ_INVALID, &flag0))
return OFFalse;
#else
if (::iconvctl(ConversionDescriptor, ICONV_SET_DISCARD_ILSEQ, &flag1))
return OFFalse;
if (::iconvctl(ConversionDescriptor, ICONV_SET_TRANSLITERATE, &flag1))
return OFFalse;
#endif
return OFTrue;
default:
return OFFalse;
}
#elif defined DCMTK_FIXED_ICONV_CONVERSION_FLAGS
// the iconvctl function is implemented only in newer versions of the
// GNU libiconv and not in other iconv implementations. For instance,
// the iconv implementation in the C standard library does not support
// different encoding flags and only has a (varying) fixed functionality
// that we detect with a configuration test.
return flags == DCMTK_FIXED_ICONV_CONVERSION_FLAGS;
#else
return OFFalse;
#endif
}
OFCondition convert(OFString& toString,
const char* fromString,
const size_t fromLength)
{
OFCondition status = EC_Normal;
// if the input string is empty or NULL, we are done
if (status.good() && (fromString != NULL) && (fromLength > 0))
{
#ifdef LIBICONV_SECOND_ARGUMENT_CONST
const char *inputPos = fromString;
#else
char *inputPos = OFconst_cast(char *, fromString);
#endif
size_t inputLeft = fromLength;
// set the conversion descriptor to the initial state
::iconv(ConversionDescriptor, NULL, NULL, NULL, NULL);
// iterate as long as there are characters to be converted
while (inputLeft > 0)
{
char buffer[CONVERSION_BUFFER_SIZE];
char *bufferPos = buffer;
const size_t bufferLength = sizeof(buffer);
size_t bufferLeft = bufferLength;
// convert the current block of the given string to the selected character encoding
if (::iconv(ConversionDescriptor, &inputPos, &inputLeft, &bufferPos, &bufferLeft) == CONVERSION_ERROR)
{
// check whether the output buffer was too small for the next converted character
// (also make sure that the output buffer has been filled to avoid an endless loop)
if ((errno != E2BIG) || (bufferLeft == bufferLength))
{
// if the conversion was unsuccessful, return with an appropriate error message
createErrnoCondition(status, "Cannot convert character encoding: ",
EC_CODE_CannotConvertEncoding);
break;
}
}
// append the converted character string to the result variable
toString.append(buffer, bufferLength - bufferLeft);
}
}
return status;
}
~Implementation()
{
// try to close given descriptor and check whether it worked
if (::iconv_close(ConversionDescriptor) == -1)
{
char errBuf[256];
CERR << "Cannot close character encoding: "
<< OFStandard::strerror(errno, errBuf, sizeof(errBuf))
<< OFendl;
}
}
private:
#include DCMTK_DIAGNOSTIC_PUSH
#include DCMTK_DIAGNOSTIC_IGNORE_SHADOW
Implementation(iconv_t ConversionDescriptor)
: ConversionDescriptor(ConversionDescriptor)
{
}
#include DCMTK_DIAGNOSTIC_POP
static void createErrnoCondition(OFCondition &status,
OFString message,
const unsigned short code)
{
char errBuf[256];
message.append(OFStandard::strerror(errno, errBuf, sizeof(errBuf)));
status = makeOFCondition(0, code, OF_error, message.c_str());
}
iconv_t ConversionDescriptor;
};
#endif // ICONV
#else // DCMTK_ENABLE_CHARSET_CONVERSION
// for suppressing unnecessary warnings
class OFCharacterEncoding::Implementation {};
#endif // NOT DCMTK_ENABLE_CHARSET_CONVERSION
OFBool OFCharacterEncoding::isLibraryAvailable()
{
#ifdef DCMTK_ENABLE_CHARSET_CONVERSION
return OFTrue;
#else
return OFFalse;
#endif
}
OFString OFCharacterEncoding::getLibraryVersionString()
{
#ifdef DCMTK_ENABLE_CHARSET_CONVERSION
return Implementation::getVersionString();
#else
return "<no character encoding library available>";
#endif
}
size_t OFCharacterEncoding::countCharactersInUTF8String(const OFString &utf8String)
{
const size_t length = utf8String.length();
size_t count = 0;
// iterate over all bytes and count start of UTF-8 characters
for (size_t i = 0; i < length; i++)
{
if ((utf8String.at(i) & 0xc0) != 0x80)
count++;
}
return count;
}
OFBool OFCharacterEncoding::hasDefaultEncoding()
{
#if defined(DCMTK_ENABLE_CHARSET_CONVERSION) &&\
(\
DCMTK_ENABLE_CHARSET_CONVERSION != DCMTK_CHARSET_CONVERSION_STDLIBC_ICONV\
|| DCMTK_STDLIBC_ICONV_HAS_DEFAULT_ENCODING\
)
return OFTrue;
#else
return OFFalse;
#endif
}
OFString OFCharacterEncoding::getLocaleEncoding()
{
#ifdef DCMTK_ENABLE_CHARSET_CONVERSION
return Implementation::getLocaleEncoding();
#else
return OFString();
#endif
}
#ifdef DCMTK_ENABLE_CHARSET_CONVERSION
OFBool OFCharacterEncoding::supportsConversionFlags(const unsigned flags)
{
return Implementation::supportsConversionFlags(flags);
}
#else
OFBool OFCharacterEncoding::supportsConversionFlags(const unsigned /* flags */)
{
return OFFalse;
}
#endif
OFCharacterEncoding::OFCharacterEncoding()
: TheImplementation()
{
}
OFCharacterEncoding::OFCharacterEncoding(const OFCharacterEncoding& rhs)
: TheImplementation(rhs.TheImplementation)
{
}
OFCharacterEncoding::~OFCharacterEncoding()
{
}
OFCharacterEncoding& OFCharacterEncoding::operator=(const OFCharacterEncoding& rhs)
{
TheImplementation = rhs.TheImplementation;
return *this;
}
OFCharacterEncoding::operator OFBool() const
{
return OFstatic_cast(OFBool, TheImplementation);
}
OFBool OFCharacterEncoding::operator!() const
{
return !TheImplementation;
}
OFBool OFCharacterEncoding::operator==(const OFCharacterEncoding& rhs) const
{
return TheImplementation == rhs.TheImplementation;
}
OFBool OFCharacterEncoding::operator!=(const OFCharacterEncoding& rhs) const
{
return TheImplementation != rhs.TheImplementation;
}
void OFCharacterEncoding::clear()
{
#ifdef DCMTK_ENABLE_CHARSET_CONVERSION
TheImplementation.reset();
#endif
}
unsigned OFCharacterEncoding::getConversionFlags() const
{
#ifdef DCMTK_ENABLE_CHARSET_CONVERSION
if (TheImplementation)
return TheImplementation->getConversionFlags();
#endif
return 0;
}
#ifdef DCMTK_ENABLE_CHARSET_CONVERSION
OFCondition OFCharacterEncoding::setConversionFlags(const unsigned flags)
{
if (TheImplementation)
{
if (TheImplementation->setConversionFlags(flags))
return EC_Normal;
return makeOFCondition(0, EC_CODE_CannotControlConverter, OF_error,
"Conversion flags not supported by the underlying implementation");
}
return EC_NoEncodingSelected;
}
#else
OFCondition OFCharacterEncoding::setConversionFlags(const unsigned /* flags */)
{
return EC_NoEncodingLibrary;
}
#endif
#ifdef DCMTK_ENABLE_CHARSET_CONVERSION
OFCondition OFCharacterEncoding::selectEncoding(const OFString &fromEncoding,
const OFString &toEncoding)
{
OFCondition result;
TheImplementation.reset(Implementation::create(fromEncoding, toEncoding, result));
return result;
}
#else
OFCondition OFCharacterEncoding::selectEncoding(const OFString & /* fromEncoding */,
const OFString & /* toEncoding */)
{
return EC_NoEncodingLibrary;
}
#endif
#ifdef DCMTK_ENABLE_CHARSET_CONVERSION
OFCondition OFCharacterEncoding::convertString(const OFString &fromString,
OFString &toString,
const OFBool clearMode)
{
return convertString(fromString.c_str(), fromString.length(), toString, clearMode);
}
#else
OFCondition OFCharacterEncoding::convertString(const OFString & /* fromString */,
OFString & /* toString */,
const OFBool /* clearMode */)
{
return EC_NoEncodingLibrary;
}
#endif
#ifdef DCMTK_ENABLE_CHARSET_CONVERSION
OFCondition OFCharacterEncoding::convertString(const char *fromString,
const size_t fromLength,
OFString &toString,
const OFBool clearMode)
{
if (TheImplementation)
{
// first, clear result variable if requested
if (clearMode)
toString.clear();
return TheImplementation->convert(toString, fromString, fromLength);
}
return EC_NoEncodingSelected;
}
#else
OFCondition OFCharacterEncoding::convertString(const char * /* fromString */,
const size_t /* fromLength */,
OFString & /* toString */,
const OFBool /* clearMode */)
{
return EC_NoEncodingLibrary;
}
#endif
#ifdef HAVE_WINDOWS_H // Windows-specific conversion functions
OFCondition OFCharacterEncoding::convertFromWideCharString(const wchar_t *fromString,
const size_t fromLength,
OFString &toString,
const unsigned int codePage,
const OFBool clearMode)
{
// first, clear result variable if requested
if (clearMode)
toString.clear();
OFCondition status = EC_Normal;
// check for empty string
if ((fromString != NULL) && (fromLength > 0))
{
// determine required size for output buffer
const int sizeNeeded = WideCharToMultiByte(codePage, 0, fromString, OFstatic_cast(int, fromLength), NULL, 0, NULL, NULL);
if (sizeNeeded > 0)
{
// allocate temporary buffer
char *toBuffer = new char[sizeNeeded];
if (toBuffer != NULL)
{
// convert characters (without trailing NULL byte)
const int charsConverted = WideCharToMultiByte(codePage, 0, fromString, OFstatic_cast(int, fromLength), toBuffer, sizeNeeded, NULL, NULL);
if (charsConverted > 0)
{
// append the converted character string to the result variable
toString.append(toBuffer, charsConverted);
} else {
// if conversion failed, create appropriate condition text
createGetLastErrorCondition(status, "Cannot convert character encoding: ", EC_CODE_CannotConvertEncoding);
}
delete[] toBuffer;
} else {
// output buffer could not be allocated
status = EC_MemoryExhausted;
}
}
}
return status;
}
OFCondition OFCharacterEncoding::convertToWideCharString(const OFString &fromString,
wchar_t *&toString,
size_t &toLength,
const unsigned int codePage)
{
// call the real method converting the given string
return OFCharacterEncoding::convertToWideCharString(fromString.c_str(), fromString.length(),
toString, toLength, codePage);
}
OFCondition OFCharacterEncoding::convertToWideCharString(const char *fromString,
const size_t fromLength,
wchar_t *&toString,
size_t &toLength,
const unsigned int codePage)
{
OFCondition status = EC_Normal;
// check for empty string
if ((fromString != NULL) && (fromLength > 0))
{
// determine required size for output buffer
const int sizeNeeded = MultiByteToWideChar(codePage, 0, fromString, OFstatic_cast(int, fromLength), NULL, 0);
// allocate output buffer (one extra byte for the terminating NULL)
toString = new wchar_t[sizeNeeded + 1];
if (toString != NULL)
{
// convert characters (without trailing NULL byte)
toLength = MultiByteToWideChar(codePage, 0, fromString, OFstatic_cast(int, fromLength), toString, sizeNeeded);
// append NULL byte to mark "end of string"
toString[toLength] = L'\0';
if (toLength == 0)
{
// if conversion failed, create appropriate condition text
createGetLastErrorCondition(status, "Cannot convert character encoding: ", EC_CODE_CannotConvertEncoding);
}
} else {
// output buffer could not be allocated
status = EC_MemoryExhausted;
}
} else {
// create an empty string (should never fail)
toString = new wchar_t[1];
toString[0] = L'\0';
toLength = 0;
}
return status;
}
void OFCharacterEncoding::createGetLastErrorCondition(OFCondition &status,
OFString message,
const unsigned short code)
{
LPVOID errBuf = NULL;
// obtain an error string from system error code
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), OFreinterpret_cast(LPTSTR, &errBuf), 0, NULL) > 0)
{
message.append(OFstatic_cast(const char *, errBuf));
} else
message.append("unknown error code");
LocalFree(errBuf);
status = makeOFCondition(0, code, OF_error, message.c_str());
}
#endif // HAVE_WINDOWS_H
|