ConvertUTFWrapper.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "dxc/Support/WinAdapter.h" // HLSL Change
  10. #include "llvm/Support/ConvertUTF.h"
  11. #include "llvm/Support/SwapByteOrder.h"
  12. #include <string>
  13. #include <vector>
  14. namespace llvm {
  15. _Use_decl_annotations_ // HLSL Change
  16. bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source,
  17. char *&ResultPtr, const UTF8 *&ErrorPtr) {
  18. assert(WideCharWidth == 1 || WideCharWidth == 2 || WideCharWidth == 4);
  19. ConversionResult result = conversionOK;
  20. // Copy the character span over.
  21. if (WideCharWidth == 1) {
  22. const UTF8 *Pos = reinterpret_cast<const UTF8*>(Source.begin());
  23. if (!isLegalUTF8String(&Pos, reinterpret_cast<const UTF8*>(Source.end()))) {
  24. result = sourceIllegal;
  25. ErrorPtr = Pos;
  26. } else {
  27. memcpy(ResultPtr, Source.data(), Source.size());
  28. ResultPtr += Source.size();
  29. }
  30. } else if (WideCharWidth == 2) {
  31. const UTF8 *sourceStart = (const UTF8*)Source.data();
  32. // FIXME: Make the type of the result buffer correct instead of
  33. // using reinterpret_cast.
  34. UTF16 *targetStart = reinterpret_cast<UTF16*>(ResultPtr);
  35. ConversionFlags flags = strictConversion;
  36. result = ConvertUTF8toUTF16(
  37. &sourceStart, sourceStart + Source.size(),
  38. &targetStart, targetStart + 2*Source.size(), flags);
  39. if (result == conversionOK)
  40. ResultPtr = reinterpret_cast<char*>(targetStart);
  41. else
  42. ErrorPtr = sourceStart;
  43. } else if (WideCharWidth == 4) {
  44. const UTF8 *sourceStart = (const UTF8*)Source.data();
  45. // FIXME: Make the type of the result buffer correct instead of
  46. // using reinterpret_cast.
  47. UTF32 *targetStart = reinterpret_cast<UTF32*>(ResultPtr);
  48. ConversionFlags flags = strictConversion;
  49. result = ConvertUTF8toUTF32(
  50. &sourceStart, sourceStart + Source.size(),
  51. &targetStart, targetStart + 4*Source.size(), flags);
  52. if (result == conversionOK)
  53. ResultPtr = reinterpret_cast<char*>(targetStart);
  54. else
  55. ErrorPtr = sourceStart;
  56. }
  57. assert((result != targetExhausted)
  58. && "ConvertUTF8toUTFXX exhausted target buffer");
  59. return result == conversionOK;
  60. }
  61. _Use_decl_annotations_ // HLSL Change
  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(ArrayRef<char> S) {
  76. return (S.size() >= 2 &&
  77. ((S[0] == '\xff' && S[1] == '\xfe') ||
  78. (S[0] == '\xfe' && S[1] == '\xff')));
  79. }
  80. bool convertUTF16ToUTF8String(ArrayRef<char> SrcBytes, std::string &Out) {
  81. assert(Out.empty());
  82. // Error out on an uneven byte count.
  83. if (SrcBytes.size() % 2)
  84. return false;
  85. // Avoid OOB by returning early on empty input.
  86. if (SrcBytes.empty())
  87. return true;
  88. const UTF16 *Src = reinterpret_cast<const UTF16 *>(SrcBytes.begin());
  89. const UTF16 *SrcEnd = reinterpret_cast<const UTF16 *>(SrcBytes.end());
  90. // Byteswap if necessary.
  91. std::vector<UTF16> ByteSwapped;
  92. if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_SWAPPED) {
  93. ByteSwapped.insert(ByteSwapped.end(), Src, SrcEnd);
  94. for (unsigned I = 0, E = ByteSwapped.size(); I != E; ++I)
  95. ByteSwapped[I] = llvm::sys::SwapByteOrder_16(ByteSwapped[I]);
  96. Src = &ByteSwapped[0];
  97. SrcEnd = &ByteSwapped[ByteSwapped.size() - 1] + 1;
  98. }
  99. // Skip the BOM for conversion.
  100. if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_NATIVE)
  101. Src++;
  102. // Just allocate enough space up front. We'll shrink it later. Allocate
  103. // enough that we can fit a null terminator without reallocating.
  104. Out.resize(SrcBytes.size() * UNI_MAX_UTF8_BYTES_PER_CODE_POINT + 1);
  105. UTF8 *Dst = reinterpret_cast<UTF8 *>(&Out[0]);
  106. UTF8 *DstEnd = Dst + Out.size();
  107. ConversionResult CR =
  108. ConvertUTF16toUTF8(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
  109. assert(CR != targetExhausted);
  110. if (CR != conversionOK) {
  111. Out.clear();
  112. return false;
  113. }
  114. Out.resize(reinterpret_cast<char *>(Dst) - &Out[0]);
  115. Out.push_back(0);
  116. Out.pop_back();
  117. return true;
  118. }
  119. bool convertUTF8ToUTF16String(StringRef SrcUTF8,
  120. SmallVectorImpl<UTF16> &DstUTF16) {
  121. assert(DstUTF16.empty());
  122. // Avoid OOB by returning early on empty input.
  123. if (SrcUTF8.empty()) {
  124. DstUTF16.push_back(0);
  125. DstUTF16.pop_back();
  126. return true;
  127. }
  128. const UTF8 *Src = reinterpret_cast<const UTF8 *>(SrcUTF8.begin());
  129. const UTF8 *SrcEnd = reinterpret_cast<const UTF8 *>(SrcUTF8.end());
  130. // Allocate the same number of UTF-16 code units as UTF-8 code units. Encoding
  131. // as UTF-16 should always require the same amount or less code units than the
  132. // UTF-8 encoding. Allocate one extra byte for the null terminator though,
  133. // so that someone calling DstUTF16.data() gets a null terminated string.
  134. // We resize down later so we don't have to worry that this over allocates.
  135. DstUTF16.resize(SrcUTF8.size()+1);
  136. UTF16 *Dst = &DstUTF16[0];
  137. UTF16 *DstEnd = Dst + DstUTF16.size();
  138. ConversionResult CR =
  139. ConvertUTF8toUTF16(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
  140. assert(CR != targetExhausted);
  141. if (CR != conversionOK) {
  142. DstUTF16.clear();
  143. return false;
  144. }
  145. DstUTF16.resize(Dst - &DstUTF16[0]);
  146. DstUTF16.push_back(0);
  147. DstUTF16.pop_back();
  148. return true;
  149. }
  150. } // end namespace llvm