• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/inspector/string-16.h"
6 
7 #include <algorithm>
8 #include <cctype>
9 #include <cstdlib>
10 #include <cstring>
11 #include <limits>
12 #include <string>
13 
14 #include "src/base/platform/platform.h"
15 #include "src/conversions.h"
16 
17 namespace v8_inspector {
18 
19 namespace {
20 
isASCII(UChar c)21 bool isASCII(UChar c) { return !(c & ~0x7F); }
22 
isSpaceOrNewLine(UChar c)23 bool isSpaceOrNewLine(UChar c) {
24   return isASCII(c) && c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9));
25 }
26 
charactersToInteger(const UChar * characters,size_t length,bool * ok=nullptr)27 int64_t charactersToInteger(const UChar* characters, size_t length,
28                             bool* ok = nullptr) {
29   std::vector<char> buffer;
30   buffer.reserve(length + 1);
31   for (size_t i = 0; i < length; ++i) {
32     if (!isASCII(characters[i])) {
33       if (ok) *ok = false;
34       return 0;
35     }
36     buffer.push_back(static_cast<char>(characters[i]));
37   }
38   buffer.push_back('\0');
39 
40   char* endptr;
41   int64_t result =
42       static_cast<int64_t>(std::strtoll(buffer.data(), &endptr, 10));
43   if (ok) *ok = !(*endptr);
44   return result;
45 }
46 
47 const UChar replacementCharacter = 0xFFFD;
48 using UChar32 = uint32_t;
49 
inlineUTF8SequenceLengthNonASCII(char b0)50 inline int inlineUTF8SequenceLengthNonASCII(char b0) {
51   if ((b0 & 0xC0) != 0xC0) return 0;
52   if ((b0 & 0xE0) == 0xC0) return 2;
53   if ((b0 & 0xF0) == 0xE0) return 3;
54   if ((b0 & 0xF8) == 0xF0) return 4;
55   return 0;
56 }
57 
inlineUTF8SequenceLength(char b0)58 inline int inlineUTF8SequenceLength(char b0) {
59   return isASCII(b0) ? 1 : inlineUTF8SequenceLengthNonASCII(b0);
60 }
61 
62 // Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
63 // into the first byte, depending on how many bytes follow.  There are
64 // as many entries in this table as there are UTF-8 sequence types.
65 // (I.e., one byte sequence, two byte... etc.). Remember that sequences
66 // for *legal* UTF-8 will be 4 or fewer bytes total.
67 static const unsigned char firstByteMark[7] = {0x00, 0x00, 0xC0, 0xE0,
68                                                0xF0, 0xF8, 0xFC};
69 
70 typedef enum {
71   conversionOK,     // conversion successful
72   sourceExhausted,  // partial character in source, but hit end
73   targetExhausted,  // insuff. room in target for conversion
74   sourceIllegal     // source sequence is illegal/malformed
75 } ConversionResult;
76 
convertUTF16ToUTF8(const UChar ** sourceStart,const UChar * sourceEnd,char ** targetStart,char * targetEnd,bool strict)77 ConversionResult convertUTF16ToUTF8(const UChar** sourceStart,
78                                     const UChar* sourceEnd, char** targetStart,
79                                     char* targetEnd, bool strict) {
80   ConversionResult result = conversionOK;
81   const UChar* source = *sourceStart;
82   char* target = *targetStart;
83   while (source < sourceEnd) {
84     UChar32 ch;
85     uint32_t bytesToWrite = 0;
86     const UChar32 byteMask = 0xBF;
87     const UChar32 byteMark = 0x80;
88     const UChar* oldSource =
89         source;  // In case we have to back up because of target overflow.
90     ch = static_cast<uint16_t>(*source++);
91     // If we have a surrogate pair, convert to UChar32 first.
92     if (ch >= 0xD800 && ch <= 0xDBFF) {
93       // If the 16 bits following the high surrogate are in the source buffer...
94       if (source < sourceEnd) {
95         UChar32 ch2 = static_cast<uint16_t>(*source);
96         // If it's a low surrogate, convert to UChar32.
97         if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
98           ch = ((ch - 0xD800) << 10) + (ch2 - 0xDC00) + 0x0010000;
99           ++source;
100         } else if (strict) {  // it's an unpaired high surrogate
101           --source;           // return to the illegal value itself
102           result = sourceIllegal;
103           break;
104         }
105       } else {     // We don't have the 16 bits following the high surrogate.
106         --source;  // return to the high surrogate
107         result = sourceExhausted;
108         break;
109       }
110     } else if (strict) {
111       // UTF-16 surrogate values are illegal in UTF-32
112       if (ch >= 0xDC00 && ch <= 0xDFFF) {
113         --source;  // return to the illegal value itself
114         result = sourceIllegal;
115         break;
116       }
117     }
118     // Figure out how many bytes the result will require
119     if (ch < (UChar32)0x80) {
120       bytesToWrite = 1;
121     } else if (ch < (UChar32)0x800) {
122       bytesToWrite = 2;
123     } else if (ch < (UChar32)0x10000) {
124       bytesToWrite = 3;
125     } else if (ch < (UChar32)0x110000) {
126       bytesToWrite = 4;
127     } else {
128       bytesToWrite = 3;
129       ch = replacementCharacter;
130     }
131 
132     target += bytesToWrite;
133     if (target > targetEnd) {
134       source = oldSource;  // Back up source pointer!
135       target -= bytesToWrite;
136       result = targetExhausted;
137       break;
138     }
139     switch (bytesToWrite) {
140       case 4:
141         *--target = static_cast<char>((ch | byteMark) & byteMask);
142         ch >>= 6;
143         V8_FALLTHROUGH;
144       case 3:
145         *--target = static_cast<char>((ch | byteMark) & byteMask);
146         ch >>= 6;
147         V8_FALLTHROUGH;
148       case 2:
149         *--target = static_cast<char>((ch | byteMark) & byteMask);
150         ch >>= 6;
151         V8_FALLTHROUGH;
152       case 1:
153         *--target = static_cast<char>(ch | firstByteMark[bytesToWrite]);
154     }
155     target += bytesToWrite;
156   }
157   *sourceStart = source;
158   *targetStart = target;
159   return result;
160 }
161 
162 /**
163  * Is this code point a BMP code point (U+0000..U+ffff)?
164  * @param c 32-bit code point
165  * @return TRUE or FALSE
166  * @stable ICU 2.8
167  */
168 #define U_IS_BMP(c) ((uint32_t)(c) <= 0xFFFF)
169 
170 /**
171  * Is this code point a supplementary code point (U+010000..U+10FFFF)?
172  * @param c 32-bit code point
173  * @return TRUE or FALSE
174  * @stable ICU 2.8
175  */
176 #define U_IS_SUPPLEMENTARY(c) ((uint32_t)((c)-0x010000) <= 0xFFFFF)
177 
178 /**
179  * Is this code point a surrogate (U+d800..U+dfff)?
180  * @param c 32-bit code point
181  * @return TRUE or FALSE
182  * @stable ICU 2.4
183  */
184 #define U_IS_SURROGATE(c) (((c)&0xFFFFF800) == 0xD800)
185 
186 /**
187  * Get the lead surrogate (0xD800..0xDBFF) for a
188  * supplementary code point (0x010000..0x10FFFF).
189  * @param supplementary 32-bit code point (U+010000..U+10FFFF)
190  * @return lead surrogate (U+D800..U+DBFF) for supplementary
191  * @stable ICU 2.4
192  */
193 #define U16_LEAD(supplementary) (UChar)(((supplementary) >> 10) + 0xD7C0)
194 
195 /**
196  * Get the trail surrogate (0xDC00..0xDFFF) for a
197  * supplementary code point (0x010000..0x10FFFF).
198  * @param supplementary 32-bit code point (U+010000..U+10FFFF)
199  * @return trail surrogate (U+DC00..U+DFFF) for supplementary
200  * @stable ICU 2.4
201  */
202 #define U16_TRAIL(supplementary) (UChar)(((supplementary)&0x3FF) | 0xDC00)
203 
204 // This must be called with the length pre-determined by the first byte.
205 // If presented with a length > 4, this returns false.  The Unicode
206 // definition of UTF-8 goes up to 4-byte sequences.
isLegalUTF8(const unsigned char * source,int length)207 static bool isLegalUTF8(const unsigned char* source, int length) {
208   unsigned char a;
209   const unsigned char* srcptr = source + length;
210   switch (length) {
211     default:
212       return false;
213     // Everything else falls through when "true"...
214     case 4:
215       if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
216       V8_FALLTHROUGH;
217     case 3:
218       if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
219       V8_FALLTHROUGH;
220     case 2:
221       if ((a = (*--srcptr)) > 0xBF) return false;
222 
223       // no fall-through in this inner switch
224       switch (*source) {
225         case 0xE0:
226           if (a < 0xA0) return false;
227           break;
228         case 0xED:
229           if (a > 0x9F) return false;
230           break;
231         case 0xF0:
232           if (a < 0x90) return false;
233           break;
234         case 0xF4:
235           if (a > 0x8F) return false;
236           break;
237         default:
238           if (a < 0x80) return false;
239       }
240       V8_FALLTHROUGH;
241 
242     case 1:
243       if (*source >= 0x80 && *source < 0xC2) return false;
244   }
245   if (*source > 0xF4) return false;
246   return true;
247 }
248 
249 // Magic values subtracted from a buffer value during UTF8 conversion.
250 // This table contains as many values as there might be trailing bytes
251 // in a UTF-8 sequence.
252 static const UChar32 offsetsFromUTF8[6] = {0x00000000UL,
253                                            0x00003080UL,
254                                            0x000E2080UL,
255                                            0x03C82080UL,
256                                            static_cast<UChar32>(0xFA082080UL),
257                                            static_cast<UChar32>(0x82082080UL)};
258 
readUTF8Sequence(const char * & sequence,size_t length)259 static inline UChar32 readUTF8Sequence(const char*& sequence, size_t length) {
260   UChar32 character = 0;
261 
262   // The cases all fall through.
263   switch (length) {
264     case 6:
265       character += static_cast<unsigned char>(*sequence++);
266       character <<= 6;
267       V8_FALLTHROUGH;
268     case 5:
269       character += static_cast<unsigned char>(*sequence++);
270       character <<= 6;
271       V8_FALLTHROUGH;
272     case 4:
273       character += static_cast<unsigned char>(*sequence++);
274       character <<= 6;
275       V8_FALLTHROUGH;
276     case 3:
277       character += static_cast<unsigned char>(*sequence++);
278       character <<= 6;
279       V8_FALLTHROUGH;
280     case 2:
281       character += static_cast<unsigned char>(*sequence++);
282       character <<= 6;
283       V8_FALLTHROUGH;
284     case 1:
285       character += static_cast<unsigned char>(*sequence++);
286   }
287 
288   return character - offsetsFromUTF8[length - 1];
289 }
290 
convertUTF8ToUTF16(const char ** sourceStart,const char * sourceEnd,UChar ** targetStart,UChar * targetEnd,bool * sourceAllASCII,bool strict)291 ConversionResult convertUTF8ToUTF16(const char** sourceStart,
292                                     const char* sourceEnd, UChar** targetStart,
293                                     UChar* targetEnd, bool* sourceAllASCII,
294                                     bool strict) {
295   ConversionResult result = conversionOK;
296   const char* source = *sourceStart;
297   UChar* target = *targetStart;
298   UChar orAllData = 0;
299   while (source < sourceEnd) {
300     int utf8SequenceLength = inlineUTF8SequenceLength(*source);
301     if (sourceEnd - source < utf8SequenceLength) {
302       result = sourceExhausted;
303       break;
304     }
305     // Do this check whether lenient or strict
306     if (!isLegalUTF8(reinterpret_cast<const unsigned char*>(source),
307                      utf8SequenceLength)) {
308       result = sourceIllegal;
309       break;
310     }
311 
312     UChar32 character = readUTF8Sequence(source, utf8SequenceLength);
313 
314     if (target >= targetEnd) {
315       source -= utf8SequenceLength;  // Back up source pointer!
316       result = targetExhausted;
317       break;
318     }
319 
320     if (U_IS_BMP(character)) {
321       // UTF-16 surrogate values are illegal in UTF-32
322       if (U_IS_SURROGATE(character)) {
323         if (strict) {
324           source -= utf8SequenceLength;  // return to the illegal value itself
325           result = sourceIllegal;
326           break;
327         }
328         *target++ = replacementCharacter;
329         orAllData |= replacementCharacter;
330       } else {
331         *target++ = static_cast<UChar>(character);  // normal case
332         orAllData |= character;
333       }
334     } else if (U_IS_SUPPLEMENTARY(character)) {
335       // target is a character in range 0xFFFF - 0x10FFFF
336       if (target + 1 >= targetEnd) {
337         source -= utf8SequenceLength;  // Back up source pointer!
338         result = targetExhausted;
339         break;
340       }
341       *target++ = U16_LEAD(character);
342       *target++ = U16_TRAIL(character);
343       orAllData = 0xFFFF;
344     } else {
345       if (strict) {
346         source -= utf8SequenceLength;  // return to the start
347         result = sourceIllegal;
348         break;  // Bail out; shouldn't continue
349       } else {
350         *target++ = replacementCharacter;
351         orAllData |= replacementCharacter;
352       }
353     }
354   }
355   *sourceStart = source;
356   *targetStart = target;
357 
358   if (sourceAllASCII) *sourceAllASCII = !(orAllData & ~0x7F);
359 
360   return result;
361 }
362 
363 // Helper to write a three-byte UTF-8 code point to the buffer, caller must
364 // check room is available.
putUTF8Triple(char * & buffer,UChar ch)365 static inline void putUTF8Triple(char*& buffer, UChar ch) {
366   *buffer++ = static_cast<char>(((ch >> 12) & 0x0F) | 0xE0);
367   *buffer++ = static_cast<char>(((ch >> 6) & 0x3F) | 0x80);
368   *buffer++ = static_cast<char>((ch & 0x3F) | 0x80);
369 }
370 
371 }  // namespace
372 
String16()373 String16::String16() {}
374 
String16(const String16 & other)375 String16::String16(const String16& other)
376     : m_impl(other.m_impl), hash_code(other.hash_code) {}
377 
String16(String16 && other)378 String16::String16(String16&& other) V8_NOEXCEPT
379     : m_impl(std::move(other.m_impl)),
380       hash_code(other.hash_code) {}
381 
String16(const UChar * characters,size_t size)382 String16::String16(const UChar* characters, size_t size)
383     : m_impl(characters, size) {}
384 
String16(const UChar * characters)385 String16::String16(const UChar* characters) : m_impl(characters) {}
386 
String16(const char * characters)387 String16::String16(const char* characters)
388     : String16(characters, std::strlen(characters)) {}
389 
String16(const char * characters,size_t size)390 String16::String16(const char* characters, size_t size) {
391   m_impl.resize(size);
392   for (size_t i = 0; i < size; ++i) m_impl[i] = characters[i];
393 }
394 
String16(const std::basic_string<UChar> & impl)395 String16::String16(const std::basic_string<UChar>& impl) : m_impl(impl) {}
396 
operator =(const String16 & other)397 String16& String16::operator=(const String16& other) {
398   m_impl = other.m_impl;
399   hash_code = other.hash_code;
400   return *this;
401 }
402 
operator =(String16 && other)403 String16& String16::operator=(String16&& other) V8_NOEXCEPT {
404   m_impl = std::move(other.m_impl);
405   hash_code = other.hash_code;
406   return *this;
407 }
408 
409 // static
fromInteger(int number)410 String16 String16::fromInteger(int number) {
411   char arr[50];
412   v8::internal::Vector<char> buffer(arr, arraysize(arr));
413   return String16(IntToCString(number, buffer));
414 }
415 
416 // static
fromInteger(size_t number)417 String16 String16::fromInteger(size_t number) {
418   const size_t kBufferSize = 50;
419   char buffer[kBufferSize];
420 #if !defined(_WIN32) && !defined(_WIN64)
421   v8::base::OS::SNPrintF(buffer, kBufferSize, "%zu", number);
422 #else
423   v8::base::OS::SNPrintF(buffer, kBufferSize, "%Iu", number);
424 #endif
425   return String16(buffer);
426 }
427 
428 // static
fromDouble(double number)429 String16 String16::fromDouble(double number) {
430   char arr[50];
431   v8::internal::Vector<char> buffer(arr, arraysize(arr));
432   return String16(DoubleToCString(number, buffer));
433 }
434 
435 // static
fromDouble(double number,int precision)436 String16 String16::fromDouble(double number, int precision) {
437   std::unique_ptr<char[]> str(
438       v8::internal::DoubleToPrecisionCString(number, precision));
439   return String16(str.get());
440 }
441 
toInteger64(bool * ok) const442 int64_t String16::toInteger64(bool* ok) const {
443   return charactersToInteger(characters16(), length(), ok);
444 }
445 
toInteger(bool * ok) const446 int String16::toInteger(bool* ok) const {
447   int64_t result = toInteger64(ok);
448   if (ok && *ok) {
449     *ok = result <= std::numeric_limits<int>::max() &&
450           result >= std::numeric_limits<int>::min();
451   }
452   return static_cast<int>(result);
453 }
454 
stripWhiteSpace() const455 String16 String16::stripWhiteSpace() const {
456   if (!length()) return String16();
457 
458   size_t start = 0;
459   size_t end = length() - 1;
460 
461   // skip white space from start
462   while (start <= end && isSpaceOrNewLine(characters16()[start])) ++start;
463 
464   // only white space
465   if (start > end) return String16();
466 
467   // skip white space from end
468   while (end && isSpaceOrNewLine(characters16()[end])) --end;
469 
470   if (!start && end == length() - 1) return *this;
471   return String16(characters16() + start, end + 1 - start);
472 }
473 
String16Builder()474 String16Builder::String16Builder() {}
475 
append(const String16 & s)476 void String16Builder::append(const String16& s) {
477   m_buffer.insert(m_buffer.end(), s.characters16(),
478                   s.characters16() + s.length());
479 }
480 
append(UChar c)481 void String16Builder::append(UChar c) { m_buffer.push_back(c); }
482 
append(char c)483 void String16Builder::append(char c) {
484   UChar u = c;
485   m_buffer.push_back(u);
486 }
487 
append(const UChar * characters,size_t length)488 void String16Builder::append(const UChar* characters, size_t length) {
489   m_buffer.insert(m_buffer.end(), characters, characters + length);
490 }
491 
append(const char * characters,size_t length)492 void String16Builder::append(const char* characters, size_t length) {
493   m_buffer.insert(m_buffer.end(), characters, characters + length);
494 }
495 
appendNumber(int number)496 void String16Builder::appendNumber(int number) {
497   constexpr int kBufferSize = 11;
498   char buffer[kBufferSize];
499   int chars = v8::base::OS::SNPrintF(buffer, kBufferSize, "%d", number);
500   DCHECK_LE(0, chars);
501   m_buffer.insert(m_buffer.end(), buffer, buffer + chars);
502 }
503 
appendNumber(size_t number)504 void String16Builder::appendNumber(size_t number) {
505   constexpr int kBufferSize = 20;
506   char buffer[kBufferSize];
507 #if !defined(_WIN32) && !defined(_WIN64)
508   int chars = v8::base::OS::SNPrintF(buffer, kBufferSize, "%zu", number);
509 #else
510   int chars = v8::base::OS::SNPrintF(buffer, kBufferSize, "%Iu", number);
511 #endif
512   DCHECK_LE(0, chars);
513   m_buffer.insert(m_buffer.end(), buffer, buffer + chars);
514 }
515 
appendUnsignedAsHex(uint64_t number)516 void String16Builder::appendUnsignedAsHex(uint64_t number) {
517   constexpr int kBufferSize = 17;
518   char buffer[kBufferSize];
519   int chars =
520       v8::base::OS::SNPrintF(buffer, kBufferSize, "%016" PRIx64, number);
521   DCHECK_LE(0, chars);
522   m_buffer.insert(m_buffer.end(), buffer, buffer + chars);
523 }
524 
appendUnsignedAsHex(uint32_t number)525 void String16Builder::appendUnsignedAsHex(uint32_t number) {
526   constexpr int kBufferSize = 9;
527   char buffer[kBufferSize];
528   int chars = v8::base::OS::SNPrintF(buffer, kBufferSize, "%08" PRIx32, number);
529   DCHECK_LE(0, chars);
530   m_buffer.insert(m_buffer.end(), buffer, buffer + chars);
531 }
532 
toString()533 String16 String16Builder::toString() {
534   return String16(m_buffer.data(), m_buffer.size());
535 }
536 
reserveCapacity(size_t capacity)537 void String16Builder::reserveCapacity(size_t capacity) {
538   m_buffer.reserve(capacity);
539 }
540 
fromUTF8(const char * stringStart,size_t length)541 String16 String16::fromUTF8(const char* stringStart, size_t length) {
542   if (!stringStart || !length) return String16();
543 
544   std::vector<UChar> buffer(length);
545   UChar* bufferStart = buffer.data();
546 
547   UChar* bufferCurrent = bufferStart;
548   const char* stringCurrent = stringStart;
549   if (convertUTF8ToUTF16(&stringCurrent, stringStart + length, &bufferCurrent,
550                          bufferCurrent + buffer.size(), 0,
551                          true) != conversionOK)
552     return String16();
553 
554   size_t utf16Length = bufferCurrent - bufferStart;
555   return String16(bufferStart, utf16Length);
556 }
557 
utf8() const558 std::string String16::utf8() const {
559   size_t length = this->length();
560 
561   if (!length) return std::string("");
562 
563   // Allocate a buffer big enough to hold all the characters
564   // (an individual UTF-16 UChar can only expand to 3 UTF-8 bytes).
565   // Optimization ideas, if we find this function is hot:
566   //  * We could speculatively create a CStringBuffer to contain 'length'
567   //    characters, and resize if necessary (i.e. if the buffer contains
568   //    non-ascii characters). (Alternatively, scan the buffer first for
569   //    ascii characters, so we know this will be sufficient).
570   //  * We could allocate a CStringBuffer with an appropriate size to
571   //    have a good chance of being able to write the string into the
572   //    buffer without reallocing (say, 1.5 x length).
573   if (length > std::numeric_limits<unsigned>::max() / 3) return std::string();
574   std::vector<char> bufferVector(length * 3);
575   char* buffer = bufferVector.data();
576   const UChar* characters = m_impl.data();
577 
578   ConversionResult result =
579       convertUTF16ToUTF8(&characters, characters + length, &buffer,
580                          buffer + bufferVector.size(), false);
581   DCHECK(
582       result !=
583       targetExhausted);  // (length * 3) should be sufficient for any conversion
584 
585   // Only produced from strict conversion.
586   DCHECK(result != sourceIllegal);
587 
588   // Check for an unconverted high surrogate.
589   if (result == sourceExhausted) {
590     // This should be one unpaired high surrogate. Treat it the same
591     // was as an unpaired high surrogate would have been handled in
592     // the middle of a string with non-strict conversion - which is
593     // to say, simply encode it to UTF-8.
594     DCHECK((characters + 1) == (m_impl.data() + length));
595     DCHECK((*characters >= 0xD800) && (*characters <= 0xDBFF));
596     // There should be room left, since one UChar hasn't been
597     // converted.
598     DCHECK((buffer + 3) <= (buffer + bufferVector.size()));
599     putUTF8Triple(buffer, *characters);
600   }
601 
602   return std::string(bufferVector.data(), buffer - bufferVector.data());
603 }
604 
605 }  // namespace v8_inspector
606