ConvertUTFWrapper.cpp 5.8 KB

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