ConvertUTFWrapper.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //===-- ConvertUTFWrapper.cpp - Wrap ConvertUTF.h with clang data types -----===
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "ConvertUTF.h"
  10. //#include "llvm/Support/SwapByteOrder.h"
  11. #include <string>
  12. #include <vector>
  13. #include <stdint.h> // uint16_t
  14. #include <assert.h>
  15. #include <memory.h>
  16. namespace llvm {
  17. bool ConvertUTF8toWide(unsigned WideCharWidth, const std::string& Source,
  18. char *&ResultPtr, const UTF8 *&ErrorPtr) {
  19. assert(WideCharWidth == 1 || WideCharWidth == 2 || WideCharWidth == 4);
  20. ConversionResult result = conversionOK;
  21. // Copy the character span over.
  22. if (WideCharWidth == 1) {
  23. const UTF8 *Pos = reinterpret_cast<const UTF8*>(Source.data());
  24. if (!isLegalUTF8String(&Pos, reinterpret_cast<const UTF8*>(Source.data() + Source.length()))) {
  25. result = sourceIllegal;
  26. ErrorPtr = Pos;
  27. } else {
  28. memcpy(ResultPtr, Source.data(), Source.size());
  29. ResultPtr += Source.size();
  30. }
  31. } else if (WideCharWidth == 2) {
  32. const UTF8 *sourceStart = (const UTF8*)Source.data();
  33. // FIXME: Make the type of the result buffer correct instead of
  34. // using reinterpret_cast.
  35. UTF16 *targetStart = reinterpret_cast<UTF16*>(ResultPtr);
  36. ConversionFlags flags = strictConversion;
  37. result = ConvertUTF8toUTF16(
  38. &sourceStart, sourceStart + Source.size(),
  39. &targetStart, targetStart + 2*Source.size(), flags);
  40. if (result == conversionOK)
  41. ResultPtr = reinterpret_cast<char*>(targetStart);
  42. else
  43. ErrorPtr = sourceStart;
  44. } else if (WideCharWidth == 4) {
  45. const UTF8 *sourceStart = (const UTF8*)Source.data();
  46. // FIXME: Make the type of the result buffer correct instead of
  47. // using reinterpret_cast.
  48. UTF32 *targetStart = reinterpret_cast<UTF32*>(ResultPtr);
  49. ConversionFlags flags = strictConversion;
  50. result = ConvertUTF8toUTF32(
  51. &sourceStart, sourceStart + Source.size(),
  52. &targetStart, targetStart + 4*Source.size(), flags);
  53. if (result == conversionOK)
  54. ResultPtr = reinterpret_cast<char*>(targetStart);
  55. else
  56. ErrorPtr = sourceStart;
  57. }
  58. assert((result != targetExhausted)
  59. && "ConvertUTF8toUTFXX exhausted target buffer");
  60. return result == conversionOK;
  61. }
  62. bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr) {
  63. const UTF32 *SourceStart = &Source;
  64. const UTF32 *SourceEnd = SourceStart + 1;
  65. UTF8 *TargetStart = reinterpret_cast<UTF8 *>(ResultPtr);
  66. UTF8 *TargetEnd = TargetStart + 4;
  67. ConversionResult CR = ConvertUTF32toUTF8(&SourceStart, SourceEnd,
  68. &TargetStart, TargetEnd,
  69. strictConversion);
  70. if (CR != conversionOK)
  71. return false;
  72. ResultPtr = reinterpret_cast<char*>(TargetStart);
  73. return true;
  74. }
  75. bool hasUTF16ByteOrderMark(const char* S, size_t len) {
  76. return (len >= 2 &&
  77. ((S[0] == '\xff' && S[1] == '\xfe') ||
  78. (S[0] == '\xfe' && S[1] == '\xff')));
  79. }
  80. /// SwapByteOrder_16 - This function returns a byte-swapped representation of
  81. /// the 16-bit argument.
  82. inline uint16_t SwapByteOrder_16(uint16_t value) {
  83. #if defined(_MSC_VER) && !defined(_DEBUG)
  84. // The DLL version of the runtime lacks these functions (bug!?), but in a
  85. // release build they're replaced with BSWAP instructions anyway.
  86. return _byteswap_ushort(value);
  87. #else
  88. uint16_t Hi = value << 8;
  89. uint16_t Lo = value >> 8;
  90. return Hi | Lo;
  91. #endif
  92. }
  93. bool convertUTF16ToUTF8String(const std::u16string& utf16, std::string &Out) {
  94. assert(Out.empty());
  95. // Avoid OOB by returning early on empty input.
  96. if (utf16.empty())
  97. return true;
  98. const UTF16 *Src = reinterpret_cast<const UTF16 *>(utf16.data());
  99. const UTF16 *SrcEnd = reinterpret_cast<const UTF16 *>(utf16.data() + utf16.length());
  100. // Byteswap if necessary.
  101. std::vector<UTF16> ByteSwapped;
  102. if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_SWAPPED) {
  103. ByteSwapped.insert(ByteSwapped.end(), Src, SrcEnd);
  104. for (size_t I = 0, E = ByteSwapped.size(); I != E; ++I)
  105. ByteSwapped[I] = SwapByteOrder_16(ByteSwapped[I]);
  106. Src = &ByteSwapped[0];
  107. SrcEnd = &ByteSwapped[ByteSwapped.size() - 1] + 1;
  108. }
  109. // Skip the BOM for conversion.
  110. if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_NATIVE)
  111. Src++;
  112. // Just allocate enough space up front. We'll shrink it later.
  113. Out.resize(utf16.length() * UNI_MAX_UTF8_BYTES_PER_CODE_POINT + 1);
  114. UTF8 *Dst = reinterpret_cast<UTF8 *>(&Out[0]);
  115. UTF8 *DstEnd = Dst + Out.size();
  116. ConversionResult CR =
  117. ConvertUTF16toUTF8(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
  118. assert(CR != targetExhausted);
  119. if (CR != conversionOK) {
  120. Out.clear();
  121. return false;
  122. }
  123. Out.resize(reinterpret_cast<char *>(Dst) - &Out[0]);
  124. return true;
  125. }
  126. } // end namespace llvm