Coverage Report

Created: 2025-01-28 06:38

/src/hermes/external/llvh/lib/Support/ConvertUTFWrapper.cpp
Line
Count
Source (jump to first uncovered line)
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 "llvh/ADT/ArrayRef.h"
11
#include "llvh/ADT/StringRef.h"
12
#include "llvh/Support/ConvertUTF.h"
13
#include "llvh/Support/ErrorHandling.h"
14
#include "llvh/Support/SwapByteOrder.h"
15
#include <string>
16
#include <vector>
17
18
namespace llvh {
19
20
bool ConvertUTF8toWide(unsigned WideCharWidth, llvh::StringRef Source,
21
0
                       char *&ResultPtr, const UTF8 *&ErrorPtr) {
22
0
  assert(WideCharWidth == 1 || WideCharWidth == 2 || WideCharWidth == 4);
23
0
  ConversionResult result = conversionOK;
24
  // Copy the character span over.
25
0
  if (WideCharWidth == 1) {
26
0
    const UTF8 *Pos = reinterpret_cast<const UTF8*>(Source.begin());
27
0
    if (!isLegalUTF8String(&Pos, reinterpret_cast<const UTF8*>(Source.end()))) {
28
0
      result = sourceIllegal;
29
0
      ErrorPtr = Pos;
30
0
    } else {
31
0
      memcpy(ResultPtr, Source.data(), Source.size());
32
0
      ResultPtr += Source.size();
33
0
    }
34
0
  } else if (WideCharWidth == 2) {
35
0
    const UTF8 *sourceStart = (const UTF8*)Source.data();
36
    // FIXME: Make the type of the result buffer correct instead of
37
    // using reinterpret_cast.
38
0
    UTF16 *targetStart = reinterpret_cast<UTF16*>(ResultPtr);
39
0
    ConversionFlags flags = strictConversion;
40
0
    result = ConvertUTF8toUTF16(
41
0
        &sourceStart, sourceStart + Source.size(),
42
0
        &targetStart, targetStart + Source.size(), flags);
43
0
    if (result == conversionOK)
44
0
      ResultPtr = reinterpret_cast<char*>(targetStart);
45
0
    else
46
0
      ErrorPtr = sourceStart;
47
0
  } else if (WideCharWidth == 4) {
48
0
    const UTF8 *sourceStart = (const UTF8*)Source.data();
49
    // FIXME: Make the type of the result buffer correct instead of
50
    // using reinterpret_cast.
51
0
    UTF32 *targetStart = reinterpret_cast<UTF32*>(ResultPtr);
52
0
    ConversionFlags flags = strictConversion;
53
0
    result = ConvertUTF8toUTF32(
54
0
        &sourceStart, sourceStart + Source.size(),
55
0
        &targetStart, targetStart + Source.size(), flags);
56
0
    if (result == conversionOK)
57
0
      ResultPtr = reinterpret_cast<char*>(targetStart);
58
0
    else
59
0
      ErrorPtr = sourceStart;
60
0
  }
61
0
  assert((result != targetExhausted)
62
0
         && "ConvertUTF8toUTFXX exhausted target buffer");
63
0
  return result == conversionOK;
64
0
}
65
66
0
bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr) {
67
0
  const UTF32 *SourceStart = &Source;
68
0
  const UTF32 *SourceEnd = SourceStart + 1;
69
0
  UTF8 *TargetStart = reinterpret_cast<UTF8 *>(ResultPtr);
70
0
  UTF8 *TargetEnd = TargetStart + 4;
71
0
  ConversionResult CR = ConvertUTF32toUTF8(&SourceStart, SourceEnd,
72
0
                                           &TargetStart, TargetEnd,
73
0
                                           strictConversion);
74
0
  if (CR != conversionOK)
75
0
    return false;
76
77
0
  ResultPtr = reinterpret_cast<char*>(TargetStart);
78
0
  return true;
79
0
}
80
81
0
bool hasUTF16ByteOrderMark(ArrayRef<char> S) {
82
0
  return (S.size() >= 2 &&
83
0
          ((S[0] == '\xff' && S[1] == '\xfe') ||
84
0
           (S[0] == '\xfe' && S[1] == '\xff')));
85
0
}
86
87
0
bool convertUTF16ToUTF8String(ArrayRef<char> SrcBytes, std::string &Out) {
88
0
  assert(Out.empty());
89
90
  // Error out on an uneven byte count.
91
0
  if (SrcBytes.size() % 2)
92
0
    return false;
93
94
  // Avoid OOB by returning early on empty input.
95
0
  if (SrcBytes.empty())
96
0
    return true;
97
98
0
  const UTF16 *Src = reinterpret_cast<const UTF16 *>(SrcBytes.begin());
99
0
  const UTF16 *SrcEnd = reinterpret_cast<const UTF16 *>(SrcBytes.end());
100
101
  // Byteswap if necessary.
102
0
  std::vector<UTF16> ByteSwapped;
103
0
  if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_SWAPPED) {
104
0
    ByteSwapped.insert(ByteSwapped.end(), Src, SrcEnd);
105
0
    for (unsigned I = 0, E = ByteSwapped.size(); I != E; ++I)
106
0
      ByteSwapped[I] = llvh::sys::SwapByteOrder_16(ByteSwapped[I]);
107
0
    Src = &ByteSwapped[0];
108
0
    SrcEnd = &ByteSwapped[ByteSwapped.size() - 1] + 1;
109
0
  }
110
111
  // Skip the BOM for conversion.
112
0
  if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_NATIVE)
113
0
    Src++;
114
115
  // Just allocate enough space up front.  We'll shrink it later.  Allocate
116
  // enough that we can fit a null terminator without reallocating.
117
0
  Out.resize(SrcBytes.size() * UNI_MAX_UTF8_BYTES_PER_CODE_POINT + 1);
118
0
  UTF8 *Dst = reinterpret_cast<UTF8 *>(&Out[0]);
119
0
  UTF8 *DstEnd = Dst + Out.size();
120
121
0
  ConversionResult CR =
122
0
      ConvertUTF16toUTF8(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
123
0
  assert(CR != targetExhausted);
124
125
0
  if (CR != conversionOK) {
126
0
    Out.clear();
127
0
    return false;
128
0
  }
129
130
0
  Out.resize(reinterpret_cast<char *>(Dst) - &Out[0]);
131
0
  Out.push_back(0);
132
0
  Out.pop_back();
133
0
  return true;
134
0
}
135
136
bool convertUTF16ToUTF8String(ArrayRef<UTF16> Src, std::string &Out)
137
0
{
138
0
  return convertUTF16ToUTF8String(
139
0
      llvh::ArrayRef<char>(reinterpret_cast<const char *>(Src.data()),
140
0
      Src.size() * sizeof(UTF16)), Out);
141
0
}
142
143
bool convertUTF8ToUTF16String(StringRef SrcUTF8,
144
0
                              SmallVectorImpl<UTF16> &DstUTF16) {
145
0
  assert(DstUTF16.empty());
146
147
  // Avoid OOB by returning early on empty input.
148
0
  if (SrcUTF8.empty()) {
149
0
    DstUTF16.push_back(0);
150
0
    DstUTF16.pop_back();
151
0
    return true;
152
0
  }
153
154
0
  const UTF8 *Src = reinterpret_cast<const UTF8 *>(SrcUTF8.begin());
155
0
  const UTF8 *SrcEnd = reinterpret_cast<const UTF8 *>(SrcUTF8.end());
156
157
  // Allocate the same number of UTF-16 code units as UTF-8 code units. Encoding
158
  // as UTF-16 should always require the same amount or less code units than the
159
  // UTF-8 encoding.  Allocate one extra byte for the null terminator though,
160
  // so that someone calling DstUTF16.data() gets a null terminated string.
161
  // We resize down later so we don't have to worry that this over allocates.
162
0
  DstUTF16.resize(SrcUTF8.size()+1);
163
0
  UTF16 *Dst = &DstUTF16[0];
164
0
  UTF16 *DstEnd = Dst + DstUTF16.size();
165
166
0
  ConversionResult CR =
167
0
      ConvertUTF8toUTF16(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
168
0
  assert(CR != targetExhausted);
169
170
0
  if (CR != conversionOK) {
171
0
    DstUTF16.clear();
172
0
    return false;
173
0
  }
174
175
0
  DstUTF16.resize(Dst - &DstUTF16[0]);
176
0
  DstUTF16.push_back(0);
177
0
  DstUTF16.pop_back();
178
0
  return true;
179
0
}
180
181
static_assert(sizeof(wchar_t) == 1 || sizeof(wchar_t) == 2 ||
182
                  sizeof(wchar_t) == 4,
183
              "Expected wchar_t to be 1, 2, or 4 bytes");
184
185
template <typename TResult>
186
static inline bool ConvertUTF8toWideInternal(llvh::StringRef Source,
187
0
                                             TResult &Result) {
188
  // Even in the case of UTF-16, the number of bytes in a UTF-8 string is
189
  // at least as large as the number of elements in the resulting wide
190
  // string, because surrogate pairs take at least 4 bytes in UTF-8.
191
0
  Result.resize(Source.size() + 1);
192
0
  char *ResultPtr = reinterpret_cast<char *>(&Result[0]);
193
0
  const UTF8 *ErrorPtr;
194
0
  if (!ConvertUTF8toWide(sizeof(wchar_t), Source, ResultPtr, ErrorPtr)) {
195
0
    Result.clear();
196
0
    return false;
197
0
  }
198
0
  Result.resize(reinterpret_cast<wchar_t *>(ResultPtr) - &Result[0]);
199
0
  return true;
200
0
}
201
202
0
bool ConvertUTF8toWide(llvh::StringRef Source, std::wstring &Result) {
203
0
  return ConvertUTF8toWideInternal(Source, Result);
204
0
}
205
206
0
bool ConvertUTF8toWide(const char *Source, std::wstring &Result) {
207
0
  if (!Source) {
208
0
    Result.clear();
209
0
    return true;
210
0
  }
211
0
  return ConvertUTF8toWide(llvh::StringRef(Source), Result);
212
0
}
213
214
0
bool convertWideToUTF8(const std::wstring &Source, std::string &Result) {
215
0
  if (sizeof(wchar_t) == 1) {
216
0
    const UTF8 *Start = reinterpret_cast<const UTF8 *>(Source.data());
217
0
    const UTF8 *End =
218
0
        reinterpret_cast<const UTF8 *>(Source.data() + Source.size());
219
0
    if (!isLegalUTF8String(&Start, End))
220
0
      return false;
221
0
    Result.resize(Source.size());
222
0
    memcpy(&Result[0], Source.data(), Source.size());
223
0
    return true;
224
0
  } else if (sizeof(wchar_t) == 2) {
225
0
    return convertUTF16ToUTF8String(
226
0
        llvh::ArrayRef<UTF16>(reinterpret_cast<const UTF16 *>(Source.data()),
227
0
                              Source.size()),
228
0
        Result);
229
0
  } else if (sizeof(wchar_t) == 4) {
230
0
    const UTF32 *Start = reinterpret_cast<const UTF32 *>(Source.data());
231
0
    const UTF32 *End =
232
0
        reinterpret_cast<const UTF32 *>(Source.data() + Source.size());
233
0
    Result.resize(UNI_MAX_UTF8_BYTES_PER_CODE_POINT * Source.size());
234
0
    UTF8 *ResultPtr = reinterpret_cast<UTF8 *>(&Result[0]);
235
0
    UTF8 *ResultEnd = reinterpret_cast<UTF8 *>(&Result[0] + Result.size());
236
0
    if (ConvertUTF32toUTF8(&Start, End, &ResultPtr, ResultEnd,
237
0
                           strictConversion) == conversionOK) {
238
0
      Result.resize(reinterpret_cast<char *>(ResultPtr) - &Result[0]);
239
0
      return true;
240
0
    } else {
241
0
      Result.clear();
242
0
      return false;
243
0
    }
244
0
  } else {
245
0
    llvm_unreachable(
246
0
        "Control should never reach this point; see static_assert further up");
247
0
  }
248
0
}
249
250
} // end namespace llvh
251