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 
10 #include "llvm/Support/ConvertUTF.h"
11 #include "llvm/Support/SwapByteOrder.h"
12 #include <string>
13 #include <vector>
14 
15 namespace llvm {
16 
ConvertUTF8toWide(unsigned WideCharWidth,llvm::StringRef Source,char * & ResultPtr,const UTF8 * & ErrorPtr)17 bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef 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.begin());
24     if (!isLegalUTF8String(&Pos, reinterpret_cast<const UTF8*>(Source.end()))) {
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 
ConvertCodePointToUTF8(unsigned Source,char * & ResultPtr)63 bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr) {
64   const UTF32 *SourceStart = &Source;
65   const UTF32 *SourceEnd = SourceStart + 1;
66   UTF8 *TargetStart = reinterpret_cast<UTF8 *>(ResultPtr);
67   UTF8 *TargetEnd = TargetStart + 4;
68   ConversionResult CR = ConvertUTF32toUTF8(&SourceStart, SourceEnd,
69                                            &TargetStart, TargetEnd,
70                                            strictConversion);
71   if (CR != conversionOK)
72     return false;
73 
74   ResultPtr = reinterpret_cast<char*>(TargetStart);
75   return true;
76 }
77 
hasUTF16ByteOrderMark(ArrayRef<char> S)78 bool hasUTF16ByteOrderMark(ArrayRef<char> S) {
79   return (S.size() >= 2 &&
80           ((S[0] == '\xff' && S[1] == '\xfe') ||
81            (S[0] == '\xfe' && S[1] == '\xff')));
82 }
83 
convertUTF16ToUTF8String(ArrayRef<char> SrcBytes,std::string & Out)84 bool convertUTF16ToUTF8String(ArrayRef<char> SrcBytes, std::string &Out) {
85   assert(Out.empty());
86 
87   // Error out on an uneven byte count.
88   if (SrcBytes.size() % 2)
89     return false;
90 
91   // Avoid OOB by returning early on empty input.
92   if (SrcBytes.empty())
93     return true;
94 
95   const UTF16 *Src = reinterpret_cast<const UTF16 *>(SrcBytes.begin());
96   const UTF16 *SrcEnd = reinterpret_cast<const UTF16 *>(SrcBytes.end());
97 
98   // Byteswap if necessary.
99   std::vector<UTF16> ByteSwapped;
100   if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_SWAPPED) {
101     ByteSwapped.insert(ByteSwapped.end(), Src, SrcEnd);
102     for (unsigned I = 0, E = ByteSwapped.size(); I != E; ++I)
103       ByteSwapped[I] = llvm::sys::SwapByteOrder_16(ByteSwapped[I]);
104     Src = &ByteSwapped[0];
105     SrcEnd = &ByteSwapped[ByteSwapped.size() - 1] + 1;
106   }
107 
108   // Skip the BOM for conversion.
109   if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_NATIVE)
110     Src++;
111 
112   // Just allocate enough space up front.  We'll shrink it later.
113   Out.resize(SrcBytes.size() * UNI_MAX_UTF8_BYTES_PER_CODE_POINT);
114   UTF8 *Dst = reinterpret_cast<UTF8 *>(&Out[0]);
115   UTF8 *DstEnd = Dst + Out.size();
116 
117   ConversionResult CR =
118       ConvertUTF16toUTF8(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
119   assert(CR != targetExhausted);
120 
121   if (CR != conversionOK) {
122     Out.clear();
123     return false;
124   }
125 
126   Out.resize(reinterpret_cast<char *>(Dst) - &Out[0]);
127   return true;
128 }
129 
130 } // end namespace llvm
131 
132