Home | History | Annotate | Download | only in Support
      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 
     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 
     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 
     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 
     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.  Allocate
    113   // enough that we can fit a null terminator without reallocating.
    114   Out.resize(SrcBytes.size() * UNI_MAX_UTF8_BYTES_PER_CODE_POINT + 1);
    115   UTF8 *Dst = reinterpret_cast<UTF8 *>(&Out[0]);
    116   UTF8 *DstEnd = Dst + Out.size();
    117 
    118   ConversionResult CR =
    119       ConvertUTF16toUTF8(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
    120   assert(CR != targetExhausted);
    121 
    122   if (CR != conversionOK) {
    123     Out.clear();
    124     return false;
    125   }
    126 
    127   Out.resize(reinterpret_cast<char *>(Dst) - &Out[0]);
    128   Out.push_back(0);
    129   Out.pop_back();
    130   return true;
    131 }
    132 
    133 bool convertUTF8ToUTF16String(StringRef SrcUTF8,
    134                               SmallVectorImpl<UTF16> &DstUTF16) {
    135   assert(DstUTF16.empty());
    136 
    137   // Avoid OOB by returning early on empty input.
    138   if (SrcUTF8.empty()) {
    139     DstUTF16.push_back(0);
    140     DstUTF16.pop_back();
    141     return true;
    142   }
    143 
    144   const UTF8 *Src = reinterpret_cast<const UTF8 *>(SrcUTF8.begin());
    145   const UTF8 *SrcEnd = reinterpret_cast<const UTF8 *>(SrcUTF8.end());
    146 
    147   // Allocate the same number of UTF-16 code units as UTF-8 code units. Encoding
    148   // as UTF-16 should always require the same amount or less code units than the
    149   // UTF-8 encoding.  Allocate one extra byte for the null terminator though,
    150   // so that someone calling DstUTF16.data() gets a null terminated string.
    151   // We resize down later so we don't have to worry that this over allocates.
    152   DstUTF16.resize(SrcUTF8.size()+1);
    153   UTF16 *Dst = &DstUTF16[0];
    154   UTF16 *DstEnd = Dst + DstUTF16.size();
    155 
    156   ConversionResult CR =
    157       ConvertUTF8toUTF16(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
    158   assert(CR != targetExhausted);
    159 
    160   if (CR != conversionOK) {
    161     DstUTF16.clear();
    162     return false;
    163   }
    164 
    165   DstUTF16.resize(Dst - &DstUTF16[0]);
    166   DstUTF16.push_back(0);
    167   DstUTF16.pop_back();
    168   return true;
    169 }
    170 
    171 } // end namespace llvm
    172 
    173