| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#define MOZ_PRETEND_NO_JSRUST 1 |
| 8 |
|
| 9 |
#include "mozilla/Utf8.h" |
| 10 |
|
| 11 |
#include "mozilla/ArrayUtils.h" |
| 12 |
#include "mozilla/Assertions.h" |
| 13 |
#include "mozilla/EnumSet.h" |
| 14 |
#include "mozilla/IntegerRange.h" |
| 15 |
#include "mozilla/Span.h" |
| 16 |
|
| 17 |
using mozilla::AsChars; |
| 18 |
using mozilla::DecodeOneUtf8CodePoint; |
| 19 |
using mozilla::EnumSet; |
| 20 |
using mozilla::IntegerRange; |
| 21 |
using mozilla::IsAscii; |
| 22 |
using mozilla::IsUtf8; |
| 23 |
using mozilla::Span; |
| 24 |
using mozilla::Utf8Unit; |
| 25 |
|
| 26 |
static void TestUtf8Unit() { |
| 27 |
Utf8Unit c('A'); |
| 28 |
MOZ_RELEASE_ASSERT(c.toChar() == 'A'); |
| 29 |
MOZ_RELEASE_ASSERT(c == Utf8Unit('A')); |
| 30 |
MOZ_RELEASE_ASSERT(c != Utf8Unit('B')); |
| 31 |
MOZ_RELEASE_ASSERT(c.toUint8() == 0x41); |
| 32 |
|
| 33 |
unsigned char asUnsigned = 'A'; |
| 34 |
MOZ_RELEASE_ASSERT(c.toUnsignedChar() == asUnsigned); |
| 35 |
MOZ_RELEASE_ASSERT(Utf8Unit('B').toUnsignedChar() != asUnsigned); |
| 36 |
|
| 37 |
Utf8Unit first('@'); |
| 38 |
Utf8Unit second('#'); |
| 39 |
|
| 40 |
MOZ_RELEASE_ASSERT(first != second); |
| 41 |
|
| 42 |
first = second; |
| 43 |
MOZ_RELEASE_ASSERT(first == second); |
| 44 |
} |
| 45 |
|
| 46 |
template <typename Char> |
| 47 |
struct ToUtf8Units { |
| 48 |
public: |
| 49 |
explicit ToUtf8Units(const Char* aStart, const Char* aEnd) |
| 50 |
: lead(Utf8Unit(aStart[0])), iter(aStart + 1), end(aEnd) { |
| 51 |
MOZ_RELEASE_ASSERT(!IsAscii(aStart[0])); |
| 52 |
} |
| 53 |
|
| 54 |
const Utf8Unit lead; |
| 55 |
const Char* iter; |
| 56 |
const Char* const end; |
| 57 |
}; |
| 58 |
|
| 59 |
class AssertIfCalled { |
| 60 |
public: |
| 61 |
template <typename... Args> |
| 62 |
void operator()(Args&&... aArgs) { |
| 63 |
MOZ_RELEASE_ASSERT(false, "AssertIfCalled instance was called"); |
| 64 |
} |
| 65 |
}; |
| 66 |
|
| 67 |
// NOTE: For simplicity in treating |aCharN| identically regardless whether it's |
| 68 |
// a string literal or a more-generalized array, we require |aCharN| be |
| 69 |
// null-terminated. |
| 70 |
|
| 71 |
template <typename Char, size_t N> |
| 72 |
static void ExpectValidCodePoint(const Char (&aCharN)[N], |
| 73 |
char32_t aExpectedCodePoint) { |
| 74 |
MOZ_RELEASE_ASSERT(aCharN[N - 1] == 0, |
| 75 |
"array must be null-terminated for |aCharN + N - 1| to " |
| 76 |
"compute the value of |aIter| as altered by " |
| 77 |
"DecodeOneUtf8CodePoint"); |
| 78 |
|
| 79 |
ToUtf8Units<Char> simpleUnit(aCharN, aCharN + N - 1); |
| 80 |
auto simple = |
| 81 |
DecodeOneUtf8CodePoint(simpleUnit.lead, &simpleUnit.iter, simpleUnit.end); |
| 82 |
MOZ_RELEASE_ASSERT(simple.isSome()); |
| 83 |
MOZ_RELEASE_ASSERT(*simple == aExpectedCodePoint); |
| 84 |
MOZ_RELEASE_ASSERT(simpleUnit.iter == simpleUnit.end); |
| 85 |
|
| 86 |
ToUtf8Units<Char> complexUnit(aCharN, aCharN + N - 1); |
| 87 |
auto complex = DecodeOneUtf8CodePoint( |
| 88 |
complexUnit.lead, &complexUnit.iter, complexUnit.end, AssertIfCalled(), |
| 89 |
AssertIfCalled(), AssertIfCalled(), AssertIfCalled(), AssertIfCalled()); |
| 90 |
MOZ_RELEASE_ASSERT(complex.isSome()); |
| 91 |
MOZ_RELEASE_ASSERT(*complex == aExpectedCodePoint); |
| 92 |
MOZ_RELEASE_ASSERT(complexUnit.iter == complexUnit.end); |
| 93 |
} |
| 94 |
|
| 95 |
enum class InvalidUtf8Reason { |
| 96 |
BadLeadUnit, |
| 97 |
NotEnoughUnits, |
| 98 |
BadTrailingUnit, |
| 99 |
BadCodePoint, |
| 100 |
NotShortestForm, |
| 101 |
}; |
| 102 |
|
| 103 |
template <typename Char, size_t N> |
| 104 |
static void ExpectInvalidCodePointHelper(const Char (&aCharN)[N], |
| 105 |
InvalidUtf8Reason aExpectedReason, |
| 106 |
uint8_t aExpectedUnitsAvailable, |
| 107 |
uint8_t aExpectedUnitsNeeded, |
| 108 |
char32_t aExpectedBadCodePoint, |
| 109 |
uint8_t aExpectedUnitsObserved) { |
| 110 |
MOZ_RELEASE_ASSERT(aCharN[N - 1] == 0, |
| 111 |
"array must be null-terminated for |aCharN + N - 1| to " |
| 112 |
"compute the value of |aIter| as altered by " |
| 113 |
"DecodeOneUtf8CodePoint"); |
| 114 |
|
| 115 |
ToUtf8Units<Char> simpleUnit(aCharN, aCharN + N - 1); |
| 116 |
auto simple = |
| 117 |
DecodeOneUtf8CodePoint(simpleUnit.lead, &simpleUnit.iter, simpleUnit.end); |
| 118 |
MOZ_RELEASE_ASSERT(simple.isNothing()); |
| 119 |
MOZ_RELEASE_ASSERT(static_cast<const void*>(simpleUnit.iter) == aCharN); |
| 120 |
|
| 121 |
EnumSet<InvalidUtf8Reason> reasons; |
| 122 |
uint8_t unitsAvailable; |
| 123 |
uint8_t unitsNeeded; |
| 124 |
char32_t badCodePoint; |
| 125 |
uint8_t unitsObserved; |
| 126 |
|
| 127 |
struct OnNotShortestForm { |
| 128 |
EnumSet<InvalidUtf8Reason>& reasons; |
| 129 |
char32_t& badCodePoint; |
| 130 |
uint8_t& unitsObserved; |
| 131 |
|
| 132 |
void operator()(char32_t aBadCodePoint, uint8_t aUnitsObserved) { |
| 133 |
reasons += InvalidUtf8Reason::NotShortestForm; |
| 134 |
badCodePoint = aBadCodePoint; |
| 135 |
unitsObserved = aUnitsObserved; |
| 136 |
} |
| 137 |
}; |
| 138 |
|
| 139 |
ToUtf8Units<Char> complexUnit(aCharN, aCharN + N - 1); |
| 140 |
auto complex = DecodeOneUtf8CodePoint( |
| 141 |
complexUnit.lead, &complexUnit.iter, complexUnit.end, |
| 142 |
[&reasons]() { reasons += InvalidUtf8Reason::BadLeadUnit; }, |
| 143 |
[&reasons, &unitsAvailable, &unitsNeeded](uint8_t aUnitsAvailable, |
| 144 |
uint8_t aUnitsNeeded) { |
| 145 |
reasons += InvalidUtf8Reason::NotEnoughUnits; |
| 146 |
unitsAvailable = aUnitsAvailable; |
| 147 |
unitsNeeded = aUnitsNeeded; |
| 148 |
}, |
| 149 |
[&reasons, &unitsObserved](uint8_t aUnitsObserved) { |
| 150 |
reasons += InvalidUtf8Reason::BadTrailingUnit; |
| 151 |
unitsObserved = aUnitsObserved; |
| 152 |
}, |
| 153 |
[&reasons, &badCodePoint, &unitsObserved](char32_t aBadCodePoint, |
| 154 |
uint8_t aUnitsObserved) { |
| 155 |
reasons += InvalidUtf8Reason::BadCodePoint; |
| 156 |
badCodePoint = aBadCodePoint; |
| 157 |
unitsObserved = aUnitsObserved; |
| 158 |
}, |
| 159 |
[&reasons, &badCodePoint, &unitsObserved](char32_t aBadCodePoint, |
| 160 |
uint8_t aUnitsObserved) { |
| 161 |
reasons += InvalidUtf8Reason::NotShortestForm; |
| 162 |
badCodePoint = aBadCodePoint; |
| 163 |
unitsObserved = aUnitsObserved; |
| 164 |
}); |
| 165 |
MOZ_RELEASE_ASSERT(complex.isNothing()); |
| 166 |
MOZ_RELEASE_ASSERT(static_cast<const void*>(complexUnit.iter) == aCharN); |
| 167 |
|
| 168 |
bool alreadyIterated = false; |
| 169 |
for (InvalidUtf8Reason reason : reasons) { |
| 170 |
MOZ_RELEASE_ASSERT(!alreadyIterated); |
| 171 |
alreadyIterated = true; |
| 172 |
|
| 173 |
switch (reason) { |
| 174 |
case InvalidUtf8Reason::BadLeadUnit: |
| 175 |
break; |
| 176 |
|
| 177 |
case InvalidUtf8Reason::NotEnoughUnits: |
| 178 |
MOZ_RELEASE_ASSERT(unitsAvailable == aExpectedUnitsAvailable); |
| 179 |
MOZ_RELEASE_ASSERT(unitsNeeded == aExpectedUnitsNeeded); |
| 180 |
break; |
| 181 |
|
| 182 |
case InvalidUtf8Reason::BadTrailingUnit: |
| 183 |
MOZ_RELEASE_ASSERT(unitsObserved == aExpectedUnitsObserved); |
| 184 |
break; |
| 185 |
|
| 186 |
case InvalidUtf8Reason::BadCodePoint: |
| 187 |
MOZ_RELEASE_ASSERT(badCodePoint == aExpectedBadCodePoint); |
| 188 |
MOZ_RELEASE_ASSERT(unitsObserved == aExpectedUnitsObserved); |
| 189 |
break; |
| 190 |
|
| 191 |
case InvalidUtf8Reason::NotShortestForm: |
| 192 |
MOZ_RELEASE_ASSERT(badCodePoint == aExpectedBadCodePoint); |
| 193 |
MOZ_RELEASE_ASSERT(unitsObserved == aExpectedUnitsObserved); |
| 194 |
break; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
// NOTE: For simplicity in treating |aCharN| identically regardless whether it's |
| 200 |
// a string literal or a more-generalized array, we require |aCharN| be |
| 201 |
// null-terminated in all these functions. |
| 202 |
|
| 203 |
template <typename Char, size_t N> |
| 204 |
static void ExpectBadLeadUnit(const Char (&aCharN)[N]) { |
| 205 |
ExpectInvalidCodePointHelper(aCharN, InvalidUtf8Reason::BadLeadUnit, 0xFF, |
| 206 |
0xFF, 0xFFFFFFFF, 0xFF); |
| 207 |
} |
| 208 |
|
| 209 |
template <typename Char, size_t N> |
| 210 |
static void ExpectNotEnoughUnits(const Char (&aCharN)[N], |
| 211 |
uint8_t aExpectedUnitsAvailable, |
| 212 |
uint8_t aExpectedUnitsNeeded) { |
| 213 |
ExpectInvalidCodePointHelper(aCharN, InvalidUtf8Reason::NotEnoughUnits, |
| 214 |
aExpectedUnitsAvailable, aExpectedUnitsNeeded, |
| 215 |
0xFFFFFFFF, 0xFF); |
| 216 |
} |
| 217 |
|
| 218 |
template <typename Char, size_t N> |
| 219 |
static void ExpectBadTrailingUnit(const Char (&aCharN)[N], |
| 220 |
uint8_t aExpectedUnitsObserved) { |
| 221 |
ExpectInvalidCodePointHelper(aCharN, InvalidUtf8Reason::BadTrailingUnit, 0xFF, |
| 222 |
0xFF, 0xFFFFFFFF, aExpectedUnitsObserved); |
| 223 |
} |
| 224 |
|
| 225 |
template <typename Char, size_t N> |
| 226 |
static void ExpectNotShortestForm(const Char (&aCharN)[N], |
| 227 |
char32_t aExpectedBadCodePoint, |
| 228 |
uint8_t aExpectedUnitsObserved) { |
| 229 |
ExpectInvalidCodePointHelper(aCharN, InvalidUtf8Reason::NotShortestForm, 0xFF, |
| 230 |
0xFF, aExpectedBadCodePoint, |
| 231 |
aExpectedUnitsObserved); |
| 232 |
} |
| 233 |
|
| 234 |
template <typename Char, size_t N> |
| 235 |
static void ExpectBadCodePoint(const Char (&aCharN)[N], |
| 236 |
char32_t aExpectedBadCodePoint, |
| 237 |
uint8_t aExpectedUnitsObserved) { |
| 238 |
ExpectInvalidCodePointHelper(aCharN, InvalidUtf8Reason::BadCodePoint, 0xFF, |
| 239 |
0xFF, aExpectedBadCodePoint, |
| 240 |
aExpectedUnitsObserved); |
| 241 |
} |
| 242 |
|
| 243 |
static void TestIsUtf8() { |
| 244 |
// Note we include the U+0000 NULL in this one -- and that's fine. |
| 245 |
static const char asciiBytes[] = u8"How about a nice game of chess?"; |
| 246 |
MOZ_RELEASE_ASSERT(IsUtf8(Span(asciiBytes, std::size(asciiBytes)))); |
| 247 |
|
| 248 |
static const char endNonAsciiBytes[] = u8"Life is like a 🌯"; |
| 249 |
MOZ_RELEASE_ASSERT( |
| 250 |
IsUtf8(Span(endNonAsciiBytes, std::size(endNonAsciiBytes) - 1))); |
| 251 |
|
| 252 |
static const unsigned char badLeading[] = {0x80}; |
| 253 |
MOZ_RELEASE_ASSERT(!IsUtf8(AsChars(Span(badLeading, std::size(badLeading))))); |
| 254 |
|
| 255 |
// Byte-counts |
| 256 |
|
| 257 |
// 1 |
| 258 |
static const char oneBytes[] = u8"A"; // U+0041 LATIN CAPITAL LETTER A |
| 259 |
constexpr size_t oneBytesLen = std::size(oneBytes); |
| 260 |
static_assert(oneBytesLen == 2, "U+0041 plus nul"); |
| 261 |
MOZ_RELEASE_ASSERT(IsUtf8(Span(oneBytes, oneBytesLen))); |
| 262 |
|
| 263 |
// 2 |
| 264 |
static const char twoBytes[] = u8"؆"; // U+0606 ARABIC-INDIC CUBE ROOT |
| 265 |
constexpr size_t twoBytesLen = std::size(twoBytes); |
| 266 |
static_assert(twoBytesLen == 3, "U+0606 in two bytes plus nul"); |
| 267 |
MOZ_RELEASE_ASSERT(IsUtf8(Span(twoBytes, twoBytesLen))); |
| 268 |
|
| 269 |
ExpectValidCodePoint(twoBytes, 0x0606); |
| 270 |
|
| 271 |
// 3 |
| 272 |
static const char threeBytes[] = u8"᨞"; // U+1A1E BUGINESE PALLAWA |
| 273 |
constexpr size_t threeBytesLen = std::size(threeBytes); |
| 274 |
static_assert(threeBytesLen == 4, "U+1A1E in three bytes plus nul"); |
| 275 |
MOZ_RELEASE_ASSERT(IsUtf8(Span(threeBytes, threeBytesLen))); |
| 276 |
|
| 277 |
ExpectValidCodePoint(threeBytes, 0x1A1E); |
| 278 |
|
| 279 |
// 4 |
| 280 |
static const char fourBytes[] = |
| 281 |
u8"🁡"; // U+1F061 DOMINO TILE HORIZONTAL-06-06 |
| 282 |
constexpr size_t fourBytesLen = std::size(fourBytes); |
| 283 |
static_assert(fourBytesLen == 5, "U+1F061 in four bytes plus nul"); |
| 284 |
MOZ_RELEASE_ASSERT(IsUtf8(Span(fourBytes, fourBytesLen))); |
| 285 |
|
| 286 |
ExpectValidCodePoint(fourBytes, 0x1F061); |
| 287 |
|
| 288 |
// Max code point |
| 289 |
static const char maxCodePoint[] = u8""; // U+10FFFF |
| 290 |
constexpr size_t maxCodePointLen = std::size(maxCodePoint); |
| 291 |
static_assert(maxCodePointLen == 5, "U+10FFFF in four bytes plus nul"); |
| 292 |
MOZ_RELEASE_ASSERT(IsUtf8(Span(maxCodePoint, maxCodePointLen))); |
| 293 |
|
| 294 |
ExpectValidCodePoint(maxCodePoint, 0x10FFFF); |
| 295 |
|
| 296 |
// One past max code point |
| 297 |
static const unsigned char onePastMaxCodePoint[] = {0xF4, 0x90, 0x80, 0x80, |
| 298 |
0x0}; |
| 299 |
constexpr size_t onePastMaxCodePointLen = std::size(onePastMaxCodePoint); |
| 300 |
MOZ_RELEASE_ASSERT( |
| 301 |
!IsUtf8(AsChars(Span(onePastMaxCodePoint, onePastMaxCodePointLen)))); |
| 302 |
|
| 303 |
ExpectBadCodePoint(onePastMaxCodePoint, 0x110000, 4); |
| 304 |
|
| 305 |
// Surrogate-related testing |
| 306 |
|
| 307 |
// (Note that the various code unit sequences here are null-terminated to |
| 308 |
// simplify life for ExpectValidCodePoint, which presumes null termination.) |
| 309 |
|
| 310 |
static const unsigned char justBeforeSurrogates[] = {0xED, 0x9F, 0xBF, 0x0}; |
| 311 |
constexpr size_t justBeforeSurrogatesLen = |
| 312 |
std::size(justBeforeSurrogates) - 1; |
| 313 |
MOZ_RELEASE_ASSERT( |
| 314 |
IsUtf8(AsChars(Span(justBeforeSurrogates, justBeforeSurrogatesLen)))); |
| 315 |
|
| 316 |
ExpectValidCodePoint(justBeforeSurrogates, 0xD7FF); |
| 317 |
|
| 318 |
static const unsigned char leastSurrogate[] = {0xED, 0xA0, 0x80, 0x0}; |
| 319 |
constexpr size_t leastSurrogateLen = std::size(leastSurrogate) - 1; |
| 320 |
MOZ_RELEASE_ASSERT(!IsUtf8(AsChars(Span(leastSurrogate, leastSurrogateLen)))); |
| 321 |
|
| 322 |
ExpectBadCodePoint(leastSurrogate, 0xD800, 3); |
| 323 |
|
| 324 |
static const unsigned char arbitraryHighSurrogate[] = {0xED, 0xA2, 0x87, 0x0}; |
| 325 |
constexpr size_t arbitraryHighSurrogateLen = |
| 326 |
std::size(arbitraryHighSurrogate) - 1; |
| 327 |
MOZ_RELEASE_ASSERT(!IsUtf8( |
| 328 |
AsChars(Span(arbitraryHighSurrogate, arbitraryHighSurrogateLen)))); |
| 329 |
|
| 330 |
ExpectBadCodePoint(arbitraryHighSurrogate, 0xD887, 3); |
| 331 |
|
| 332 |
static const unsigned char arbitraryLowSurrogate[] = {0xED, 0xB7, 0xAF, 0x0}; |
| 333 |
constexpr size_t arbitraryLowSurrogateLen = |
| 334 |
std::size(arbitraryLowSurrogate) - 1; |
| 335 |
MOZ_RELEASE_ASSERT( |
| 336 |
!IsUtf8(AsChars(Span(arbitraryLowSurrogate, arbitraryLowSurrogateLen)))); |
| 337 |
|
| 338 |
ExpectBadCodePoint(arbitraryLowSurrogate, 0xDDEF, 3); |
| 339 |
|
| 340 |
static const unsigned char greatestSurrogate[] = {0xED, 0xBF, 0xBF, 0x0}; |
| 341 |
constexpr size_t greatestSurrogateLen = std::size(greatestSurrogate) - 1; |
| 342 |
MOZ_RELEASE_ASSERT( |
| 343 |
!IsUtf8(AsChars(Span(greatestSurrogate, greatestSurrogateLen)))); |
| 344 |
|
| 345 |
ExpectBadCodePoint(greatestSurrogate, 0xDFFF, 3); |
| 346 |
|
| 347 |
static const unsigned char justAfterSurrogates[] = {0xEE, 0x80, 0x80, 0x0}; |
| 348 |
constexpr size_t justAfterSurrogatesLen = std::size(justAfterSurrogates) - 1; |
| 349 |
MOZ_RELEASE_ASSERT( |
| 350 |
IsUtf8(AsChars(Span(justAfterSurrogates, justAfterSurrogatesLen)))); |
| 351 |
|
| 352 |
ExpectValidCodePoint(justAfterSurrogates, 0xE000); |
| 353 |
} |
| 354 |
|
| 355 |
static void TestDecodeOneValidUtf8CodePoint() { |
| 356 |
// NOTE: DecodeOneUtf8CodePoint decodes only *non*-ASCII code points that |
| 357 |
// consist of multiple code units, so there are no ASCII tests below. |
| 358 |
|
| 359 |
// Length two. |
| 360 |
|
| 361 |
ExpectValidCodePoint(u8"", 0x80); // <control> |
| 362 |
ExpectValidCodePoint(u8"©", 0xA9); // COPYRIGHT SIGN |
| 363 |
ExpectValidCodePoint(u8"¶", 0xB6); // PILCROW SIGN |
| 364 |
ExpectValidCodePoint(u8"¾", 0xBE); // VULGAR FRACTION THREE QUARTERS |
| 365 |
ExpectValidCodePoint(u8"÷", 0xF7); // DIVISION SIGN |
| 366 |
ExpectValidCodePoint(u8"ÿ", 0xFF); // LATIN SMALL LETTER Y WITH DIAERESIS |
| 367 |
ExpectValidCodePoint(u8"Ā", 0x100); // LATIN CAPITAL LETTER A WITH MACRON |
| 368 |
ExpectValidCodePoint(u8"IJ", 0x132); // LATIN CAPITAL LETTER LIGATURE IJ |
| 369 |
ExpectValidCodePoint(u8"ͼ", 0x37C); // GREEK SMALL DOTTED LUNATE SIGMA SYMBOL |
| 370 |
ExpectValidCodePoint(u8"Ӝ", |
| 371 |
0x4DC); // CYRILLIC CAPITAL LETTER ZHE WITTH DIAERESIS |
| 372 |
ExpectValidCodePoint(u8"۩", 0x6E9); // ARABIC PLACE OF SAJDAH |
| 373 |
ExpectValidCodePoint(u8"߿", 0x7FF); // <not assigned> |
| 374 |
|
| 375 |
// Length three. |
| 376 |
|
| 377 |
ExpectValidCodePoint(u8"ࠀ", 0x800); // SAMARITAN LETTER ALAF |
| 378 |
ExpectValidCodePoint(u8"ࡁ", 0x841); // MANDAIC LETTER AB |
| 379 |
ExpectValidCodePoint(u8"ࣿ", 0x8FF); // ARABIC MARK SIDEWAYS NOON GHUNNA |
| 380 |
ExpectValidCodePoint(u8"ஆ", 0xB86); // TAMIL LETTER AA |
| 381 |
ExpectValidCodePoint(u8"༃", |
| 382 |
0xF03); // TIBETAN MARK GTER YIG MGO -UM GTER TSHEG MA |
| 383 |
ExpectValidCodePoint( |
| 384 |
u8"࿉", |
| 385 |
0xFC9); // TIBETAN SYMBOL NOR BU (but on my system it really looks like |
| 386 |
// SOFT-SERVE ICE CREAM FROM ABOVE THE PLANE if you ask me) |
| 387 |
ExpectValidCodePoint(u8"ဪ", 0x102A); // MYANMAR LETTER AU |
| 388 |
ExpectValidCodePoint(u8"ᚏ", 0x168F); // OGHAM LETTER RUIS |
| 389 |
ExpectValidCodePoint("\xE2\x80\xA8", 0x2028); // (the hated) LINE SEPARATOR |
| 390 |
ExpectValidCodePoint("\xE2\x80\xA9", |
| 391 |
0x2029); // (the hated) PARAGRAPH SEPARATOR |
| 392 |
ExpectValidCodePoint(u8"☬", 0x262C); // ADI SHAKTI |
| 393 |
ExpectValidCodePoint(u8"㊮", 0x32AE); // CIRCLED IDEOGRAPH RESOURCE |
| 394 |
ExpectValidCodePoint(u8"㏖", 0x33D6); // SQUARE MOL |
| 395 |
ExpectValidCodePoint(u8"ꔄ", 0xA504); // VAI SYLLABLE WEEN |
| 396 |
ExpectValidCodePoint(u8"ퟕ", 0xD7D5); // HANGUL JONGSEONG RIEUL-SSANGKIYEOK |
| 397 |
ExpectValidCodePoint(u8"", 0xD7FF); // <not assigned> |
| 398 |
ExpectValidCodePoint(u8"", 0xE000); // <Private Use> |
| 399 |
ExpectValidCodePoint(u8"鱗", 0xF9F2); // CJK COMPATIBILITY IDEOGRAPH-F9F |
| 400 |
ExpectValidCodePoint( |
| 401 |
u8"﷽", 0xFDFD); // ARABIC LIGATURE BISMILLAH AR-RAHMAN AR-RAHHHEEEEM |
| 402 |
ExpectValidCodePoint(u8"", 0xFFFF); // <not assigned> |
| 403 |
|
| 404 |
// Length four. |
| 405 |
ExpectValidCodePoint(u8"𐀀", 0x10000); // LINEAR B SYLLABLE B008 A |
| 406 |
ExpectValidCodePoint(u8"𔑀", 0x14440); // ANATOLIAN HIEROGLYPH A058 |
| 407 |
ExpectValidCodePoint(u8"𝛗", 0x1D6D7); // MATHEMATICAL BOLD SMALL PHI |
| 408 |
ExpectValidCodePoint(u8"💩", 0x1F4A9); // PILE OF POO |
| 409 |
ExpectValidCodePoint(u8"🔫", 0x1F52B); // PISTOL |
| 410 |
ExpectValidCodePoint(u8"🥌", 0x1F94C); // CURLING STONE |
| 411 |
ExpectValidCodePoint(u8"🥏", 0x1F94F); // FLYING DISC |
| 412 |
ExpectValidCodePoint(u8"𠍆", 0x20346); // CJK UNIFIED IDEOGRAPH-20346 |
| 413 |
ExpectValidCodePoint(u8"𡠺", 0x2183A); // CJK UNIFIED IDEOGRAPH-2183A |
| 414 |
ExpectValidCodePoint(u8"", 0x417F6); // <not assigned> |
| 415 |
ExpectValidCodePoint(u8"", 0x7E836); // <not assigned> |
| 416 |
ExpectValidCodePoint(u8"", 0xFEF67); // <Plane 15 Private Use> |
| 417 |
ExpectValidCodePoint(u8"", 0x10FFFF); // |
| 418 |
} |
| 419 |
|
| 420 |
static void TestDecodeBadLeadUnit() { |
| 421 |
// These tests are actually exhaustive. |
| 422 |
|
| 423 |
unsigned char badLead[] = {'\0', '\0'}; |
| 424 |
|
| 425 |
for (uint8_t lead : IntegerRange(0b1000'0000, 0b1100'0000)) { |
| 426 |
badLead[0] = lead; |
| 427 |
ExpectBadLeadUnit(badLead); |
| 428 |
} |
| 429 |
|
| 430 |
{ |
| 431 |
uint8_t lead = 0b1111'1000; |
| 432 |
do { |
| 433 |
badLead[0] = lead; |
| 434 |
ExpectBadLeadUnit(badLead); |
| 435 |
if (lead == 0b1111'1111) { |
| 436 |
break; |
| 437 |
} |
| 438 |
|
| 439 |
lead++; |
| 440 |
} while (true); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
static void TestTooFewOrBadTrailingUnits() { |
| 445 |
// Lead unit indicates a two-byte code point. |
| 446 |
|
| 447 |
char truncatedTwo[] = {'\0', '\0'}; |
| 448 |
char badTrailTwo[] = {'\0', '\0', '\0'}; |
| 449 |
|
| 450 |
for (uint8_t lead : IntegerRange(0b1100'0000, 0b1110'0000)) { |
| 451 |
truncatedTwo[0] = lead; |
| 452 |
ExpectNotEnoughUnits(truncatedTwo, 1, 2); |
| 453 |
|
| 454 |
badTrailTwo[0] = lead; |
| 455 |
for (uint8_t trail : IntegerRange(0b0000'0000, 0b1000'0000)) { |
| 456 |
badTrailTwo[1] = trail; |
| 457 |
ExpectBadTrailingUnit(badTrailTwo, 2); |
| 458 |
} |
| 459 |
|
| 460 |
for (uint8_t trail : IntegerRange(0b1100'0000, 0b1111'1111)) { |
| 461 |
badTrailTwo[1] = trail; |
| 462 |
ExpectBadTrailingUnit(badTrailTwo, 2); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
// Lead unit indicates a three-byte code point. |
| 467 |
|
| 468 |
char truncatedThreeOne[] = {'\0', '\0'}; |
| 469 |
char truncatedThreeTwo[] = {'\0', '\0', '\0'}; |
| 470 |
unsigned char badTrailThree[] = {'\0', '\0', '\0', '\0'}; |
| 471 |
|
| 472 |
for (uint8_t lead : IntegerRange(0b1110'0000, 0b1111'0000)) { |
| 473 |
truncatedThreeOne[0] = lead; |
| 474 |
ExpectNotEnoughUnits(truncatedThreeOne, 1, 3); |
| 475 |
|
| 476 |
truncatedThreeTwo[0] = lead; |
| 477 |
ExpectNotEnoughUnits(truncatedThreeTwo, 2, 3); |
| 478 |
|
| 479 |
badTrailThree[0] = lead; |
| 480 |
badTrailThree[2] = 0b1011'1111; // make valid to test overreads |
| 481 |
for (uint8_t mid : IntegerRange(0b0000'0000, 0b1000'0000)) { |
| 482 |
badTrailThree[1] = mid; |
| 483 |
ExpectBadTrailingUnit(badTrailThree, 2); |
| 484 |
} |
| 485 |
{ |
| 486 |
uint8_t mid = 0b1100'0000; |
| 487 |
do { |
| 488 |
badTrailThree[1] = mid; |
| 489 |
ExpectBadTrailingUnit(badTrailThree, 2); |
| 490 |
if (mid == 0b1111'1111) { |
| 491 |
break; |
| 492 |
} |
| 493 |
|
| 494 |
mid++; |
| 495 |
} while (true); |
| 496 |
} |
| 497 |
|
| 498 |
badTrailThree[1] = 0b1011'1111; |
| 499 |
for (uint8_t last : IntegerRange(0b0000'0000, 0b1000'0000)) { |
| 500 |
badTrailThree[2] = last; |
| 501 |
ExpectBadTrailingUnit(badTrailThree, 3); |
| 502 |
} |
| 503 |
{ |
| 504 |
uint8_t last = 0b1100'0000; |
| 505 |
do { |
| 506 |
badTrailThree[2] = last; |
| 507 |
ExpectBadTrailingUnit(badTrailThree, 3); |
| 508 |
if (last == 0b1111'1111) { |
| 509 |
break; |
| 510 |
} |
| 511 |
|
| 512 |
last++; |
| 513 |
} while (true); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
// Lead unit indicates a four-byte code point. |
| 518 |
|
| 519 |
char truncatedFourOne[] = {'\0', '\0'}; |
| 520 |
char truncatedFourTwo[] = {'\0', '\0', '\0'}; |
| 521 |
char truncatedFourThree[] = {'\0', '\0', '\0', '\0'}; |
| 522 |
|
| 523 |
unsigned char badTrailFour[] = {'\0', '\0', '\0', '\0', '\0'}; |
| 524 |
|
| 525 |
for (uint8_t lead : IntegerRange(0b1111'0000, 0b1111'1000)) { |
| 526 |
truncatedFourOne[0] = lead; |
| 527 |
ExpectNotEnoughUnits(truncatedFourOne, 1, 4); |
| 528 |
|
| 529 |
truncatedFourTwo[0] = lead; |
| 530 |
ExpectNotEnoughUnits(truncatedFourTwo, 2, 4); |
| 531 |
|
| 532 |
truncatedFourThree[0] = lead; |
| 533 |
ExpectNotEnoughUnits(truncatedFourThree, 3, 4); |
| 534 |
|
| 535 |
badTrailFour[0] = lead; |
| 536 |
badTrailFour[2] = badTrailFour[3] = 0b1011'1111; // test for overreads |
| 537 |
for (uint8_t second : IntegerRange(0b0000'0000, 0b1000'0000)) { |
| 538 |
badTrailFour[1] = second; |
| 539 |
ExpectBadTrailingUnit(badTrailFour, 2); |
| 540 |
} |
| 541 |
{ |
| 542 |
uint8_t second = 0b1100'0000; |
| 543 |
do { |
| 544 |
badTrailFour[1] = second; |
| 545 |
ExpectBadTrailingUnit(badTrailFour, 2); |
| 546 |
if (second == 0b1111'1111) { |
| 547 |
break; |
| 548 |
} |
| 549 |
|
| 550 |
second++; |
| 551 |
} while (true); |
| 552 |
} |
| 553 |
|
| 554 |
badTrailFour[1] = badTrailFour[3] = 0b1011'1111; // test for overreads |
| 555 |
for (uint8_t third : IntegerRange(0b0000'0000, 0b1000'0000)) { |
| 556 |
badTrailFour[2] = third; |
| 557 |
ExpectBadTrailingUnit(badTrailFour, 3); |
| 558 |
} |
| 559 |
{ |
| 560 |
uint8_t third = 0b1100'0000; |
| 561 |
do { |
| 562 |
badTrailFour[2] = third; |
| 563 |
ExpectBadTrailingUnit(badTrailFour, 3); |
| 564 |
if (third == 0b1111'1111) { |
| 565 |
break; |
| 566 |
} |
| 567 |
|
| 568 |
third++; |
| 569 |
} while (true); |
| 570 |
} |
| 571 |
|
| 572 |
badTrailFour[2] = 0b1011'1111; |
| 573 |
for (uint8_t fourth : IntegerRange(0b0000'0000, 0b1000'0000)) { |
| 574 |
badTrailFour[3] = fourth; |
| 575 |
ExpectBadTrailingUnit(badTrailFour, 4); |
| 576 |
} |
| 577 |
{ |
| 578 |
uint8_t fourth = 0b1100'0000; |
| 579 |
do { |
| 580 |
badTrailFour[3] = fourth; |
| 581 |
ExpectBadTrailingUnit(badTrailFour, 4); |
| 582 |
if (fourth == 0b1111'1111) { |
| 583 |
break; |
| 584 |
} |
| 585 |
|
| 586 |
fourth++; |
| 587 |
} while (true); |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
static void TestBadSurrogate() { |
| 593 |
// These tests are actually exhaustive. |
| 594 |
|
| 595 |
ExpectValidCodePoint("\xED\x9F\xBF", 0xD7FF); // last before surrogates |
| 596 |
ExpectValidCodePoint("\xEE\x80\x80", 0xE000); // first after surrogates |
| 597 |
|
| 598 |
// First invalid surrogate encoding is { 0xED, 0xA0, 0x80 }. Last invalid |
| 599 |
// surrogate encoding is { 0xED, 0xBF, 0xBF }. |
| 600 |
|
| 601 |
char badSurrogate[] = {'\xED', '\0', '\0', '\0'}; |
| 602 |
|
| 603 |
for (char32_t c = 0xD800; c < 0xE000; c++) { |
| 604 |
badSurrogate[1] = 0b1000'0000 ^ ((c & 0b1111'1100'0000) >> 6); |
| 605 |
badSurrogate[2] = 0b1000'0000 ^ ((c & 0b0000'0011'1111)); |
| 606 |
|
| 607 |
ExpectBadCodePoint(badSurrogate, c, 3); |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
static void TestBadTooBig() { |
| 612 |
// These tests are actually exhaustive. |
| 613 |
|
| 614 |
ExpectValidCodePoint("\xF4\x8F\xBF\xBF", 0x10'FFFF); // last code point |
| 615 |
|
| 616 |
// Four-byte code points are |
| 617 |
// |
| 618 |
// 0b1111'0xxx 0b10xx'xxxx 0b10xx'xxxx 0b10xx'xxxx |
| 619 |
// |
| 620 |
// with 3 + 6 + 6 + 6 == 21 unconstrained bytes, so the structurally |
| 621 |
// representable limit (exclusive) is 2**21 - 1 == 2097152. |
| 622 |
|
| 623 |
char tooLargeCodePoint[] = {'\0', '\0', '\0', '\0', '\0'}; |
| 624 |
|
| 625 |
for (char32_t c = 0x11'0000; c < (1 << 21); c++) { |
| 626 |
tooLargeCodePoint[0] = |
| 627 |
0b1111'0000 ^ ((c & 0b1'1100'0000'0000'0000'0000) >> 18); |
| 628 |
tooLargeCodePoint[1] = |
| 629 |
0b1000'0000 ^ ((c & 0b0'0011'1111'0000'0000'0000) >> 12); |
| 630 |
tooLargeCodePoint[2] = |
| 631 |
0b1000'0000 ^ ((c & 0b0'0000'0000'1111'1100'0000) >> 6); |
| 632 |
tooLargeCodePoint[3] = 0b1000'0000 ^ ((c & 0b0'0000'0000'0000'0011'1111)); |
| 633 |
|
| 634 |
ExpectBadCodePoint(tooLargeCodePoint, c, 4); |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
static void TestBadCodePoint() { |
| 639 |
TestBadSurrogate(); |
| 640 |
TestBadTooBig(); |
| 641 |
} |
| 642 |
|
| 643 |
static void TestNotShortestForm() { |
| 644 |
{ |
| 645 |
// One-byte in two-byte. |
| 646 |
|
| 647 |
char oneInTwo[] = {'\0', '\0', '\0'}; |
| 648 |
|
| 649 |
for (char32_t c = '\0'; c < 0x80; c++) { |
| 650 |
oneInTwo[0] = 0b1100'0000 ^ ((c & 0b0111'1100'0000) >> 6); |
| 651 |
oneInTwo[1] = 0b1000'0000 ^ ((c & 0b0000'0011'1111)); |
| 652 |
|
| 653 |
ExpectNotShortestForm(oneInTwo, c, 2); |
| 654 |
} |
| 655 |
|
| 656 |
// One-byte in three-byte. |
| 657 |
|
| 658 |
char oneInThree[] = {'\0', '\0', '\0', '\0'}; |
| 659 |
|
| 660 |
for (char32_t c = '\0'; c < 0x80; c++) { |
| 661 |
oneInThree[0] = 0b1110'0000 ^ ((c & 0b1111'0000'0000'0000) >> 12); |
| 662 |
oneInThree[1] = 0b1000'0000 ^ ((c & 0b0000'1111'1100'0000) >> 6); |
| 663 |
oneInThree[2] = 0b1000'0000 ^ ((c & 0b0000'0000'0011'1111)); |
| 664 |
|
| 665 |
ExpectNotShortestForm(oneInThree, c, 3); |
| 666 |
} |
| 667 |
|
| 668 |
// One-byte in four-byte. |
| 669 |
|
| 670 |
char oneInFour[] = {'\0', '\0', '\0', '\0', '\0'}; |
| 671 |
|
| 672 |
for (char32_t c = '\0'; c < 0x80; c++) { |
| 673 |
oneInFour[0] = 0b1111'0000 ^ ((c & 0b1'1100'0000'0000'0000'0000) >> 18); |
| 674 |
oneInFour[1] = 0b1000'0000 ^ ((c & 0b0'0011'1111'0000'0000'0000) >> 12); |
| 675 |
oneInFour[2] = 0b1000'0000 ^ ((c & 0b0'0000'0000'1111'1100'0000) >> 6); |
| 676 |
oneInFour[3] = 0b1000'0000 ^ ((c & 0b0'0000'0000'0000'0011'1111)); |
| 677 |
|
| 678 |
ExpectNotShortestForm(oneInFour, c, 4); |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
{ |
| 683 |
// Two-byte in three-byte. |
| 684 |
|
| 685 |
char twoInThree[] = {'\0', '\0', '\0', '\0'}; |
| 686 |
|
| 687 |
for (char32_t c = 0x80; c < 0x800; c++) { |
| 688 |
twoInThree[0] = 0b1110'0000 ^ ((c & 0b1111'0000'0000'0000) >> 12); |
| 689 |
twoInThree[1] = 0b1000'0000 ^ ((c & 0b0000'1111'1100'0000) >> 6); |
| 690 |
twoInThree[2] = 0b1000'0000 ^ ((c & 0b0000'0000'0011'1111)); |
| 691 |
|
| 692 |
ExpectNotShortestForm(twoInThree, c, 3); |
| 693 |
} |
| 694 |
|
| 695 |
// Two-byte in four-byte. |
| 696 |
|
| 697 |
char twoInFour[] = {'\0', '\0', '\0', '\0', '\0'}; |
| 698 |
|
| 699 |
for (char32_t c = 0x80; c < 0x800; c++) { |
| 700 |
twoInFour[0] = 0b1111'0000 ^ ((c & 0b1'1100'0000'0000'0000'0000) >> 18); |
| 701 |
twoInFour[1] = 0b1000'0000 ^ ((c & 0b0'0011'1111'0000'0000'0000) >> 12); |
| 702 |
twoInFour[2] = 0b1000'0000 ^ ((c & 0b0'0000'0000'1111'1100'0000) >> 6); |
| 703 |
twoInFour[3] = 0b1000'0000 ^ ((c & 0b0'0000'0000'0000'0011'1111)); |
| 704 |
|
| 705 |
ExpectNotShortestForm(twoInFour, c, 4); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
{ |
| 710 |
// Three-byte in four-byte. |
| 711 |
|
| 712 |
char threeInFour[] = {'\0', '\0', '\0', '\0', '\0'}; |
| 713 |
|
| 714 |
for (char32_t c = 0x800; c < 0x1'0000; c++) { |
| 715 |
threeInFour[0] = 0b1111'0000 ^ ((c & 0b1'1100'0000'0000'0000'0000) >> 18); |
| 716 |
threeInFour[1] = 0b1000'0000 ^ ((c & 0b0'0011'1111'0000'0000'0000) >> 12); |
| 717 |
threeInFour[2] = 0b1000'0000 ^ ((c & 0b0'0000'0000'1111'1100'0000) >> 6); |
| 718 |
threeInFour[3] = 0b1000'0000 ^ ((c & 0b0'0000'0000'0000'0011'1111)); |
| 719 |
|
| 720 |
ExpectNotShortestForm(threeInFour, c, 4); |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
static void TestDecodeOneInvalidUtf8CodePoint() { |
| 726 |
TestDecodeBadLeadUnit(); |
| 727 |
TestTooFewOrBadTrailingUnits(); |
| 728 |
TestBadCodePoint(); |
| 729 |
TestNotShortestForm(); |
| 730 |
} |
| 731 |
|
| 732 |
static void TestDecodeOneUtf8CodePoint() { |
| 733 |
TestDecodeOneValidUtf8CodePoint(); |
| 734 |
TestDecodeOneInvalidUtf8CodePoint(); |
| 735 |
} |
| 736 |
|
| 737 |
int main() { |
| 738 |
TestUtf8Unit(); |
| 739 |
TestIsUtf8(); |
| 740 |
TestDecodeOneUtf8CodePoint(); |
| 741 |
return 0; |
| 742 |
} |