//===-- ConvertUTFWrapper.cpp - Wrap ConvertUTF.h with clang data types -----=== // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #include "llvm/Support/ConvertUTF.h" #include "llvm/Support/SwapByteOrder.h" #include #include namespace llvm { bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source, char *&ResultPtr, const UTF8 *&ErrorPtr) { assert(WideCharWidth == 1 || WideCharWidth == 2 || WideCharWidth == 4); ConversionResult result = conversionOK; // Copy the character span over. if (WideCharWidth == 1) { const UTF8 *Pos = reinterpret_cast(Source.begin()); if (!isLegalUTF8String(&Pos, reinterpret_cast(Source.end()))) { result = sourceIllegal; ErrorPtr = Pos; } else { memcpy(ResultPtr, Source.data(), Source.size()); ResultPtr += Source.size(); } } else if (WideCharWidth == 2) { const UTF8 *sourceStart = (const UTF8*)Source.data(); // FIXME: Make the type of the result buffer correct instead of // using reinterpret_cast. UTF16 *targetStart = reinterpret_cast(ResultPtr); ConversionFlags flags = strictConversion; result = ConvertUTF8toUTF16( &sourceStart, sourceStart + Source.size(), &targetStart, targetStart + 2*Source.size(), flags); if (result == conversionOK) ResultPtr = reinterpret_cast(targetStart); else ErrorPtr = sourceStart; } else if (WideCharWidth == 4) { const UTF8 *sourceStart = (const UTF8*)Source.data(); // FIXME: Make the type of the result buffer correct instead of // using reinterpret_cast. UTF32 *targetStart = reinterpret_cast(ResultPtr); ConversionFlags flags = strictConversion; result = ConvertUTF8toUTF32( &sourceStart, sourceStart + Source.size(), &targetStart, targetStart + 4*Source.size(), flags); if (result == conversionOK) ResultPtr = reinterpret_cast(targetStart); else ErrorPtr = sourceStart; } assert((result != targetExhausted) && "ConvertUTF8toUTFXX exhausted target buffer"); return result == conversionOK; } bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr) { const UTF32 *SourceStart = &Source; const UTF32 *SourceEnd = SourceStart + 1; UTF8 *TargetStart = reinterpret_cast(ResultPtr); UTF8 *TargetEnd = TargetStart + 4; ConversionResult CR = ConvertUTF32toUTF8(&SourceStart, SourceEnd, &TargetStart, TargetEnd, strictConversion); if (CR != conversionOK) return false; ResultPtr = reinterpret_cast(TargetStart); return true; } bool hasUTF16ByteOrderMark(ArrayRef S) { return (S.size() >= 2 && ((S[0] == '\xff' && S[1] == '\xfe') || (S[0] == '\xfe' && S[1] == '\xff'))); } bool convertUTF16ToUTF8String(ArrayRef SrcBytes, std::string &Out) { assert(Out.empty()); // Error out on an uneven byte count. if (SrcBytes.size() % 2) return false; // Avoid OOB by returning early on empty input. if (SrcBytes.empty()) return true; const UTF16 *Src = reinterpret_cast(SrcBytes.begin()); const UTF16 *SrcEnd = reinterpret_cast(SrcBytes.end()); // Byteswap if necessary. std::vector ByteSwapped; if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_SWAPPED) { ByteSwapped.insert(ByteSwapped.end(), Src, SrcEnd); for (unsigned I = 0, E = ByteSwapped.size(); I != E; ++I) ByteSwapped[I] = llvm::sys::SwapByteOrder_16(ByteSwapped[I]); Src = &ByteSwapped[0]; SrcEnd = &ByteSwapped[ByteSwapped.size() - 1] + 1; } // Skip the BOM for conversion. if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_NATIVE) Src++; // Just allocate enough space up front. We'll shrink it later. Out.resize(SrcBytes.size() * UNI_MAX_UTF8_BYTES_PER_CODE_POINT); UTF8 *Dst = reinterpret_cast(&Out[0]); UTF8 *DstEnd = Dst + Out.size(); ConversionResult CR = ConvertUTF16toUTF8(&Src, SrcEnd, &Dst, DstEnd, strictConversion); assert(CR != targetExhausted); if (CR != conversionOK) { Out.clear(); return false; } Out.resize(reinterpret_cast(Dst) - &Out[0]); return true; } } // end namespace llvm