OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/dart_api_message.h" | 5 #include "vm/dart_api_message.h" |
6 #include "vm/object.h" | 6 #include "vm/object.h" |
7 #include "vm/snapshot_ids.h" | 7 #include "vm/snapshot_ids.h" |
8 #include "vm/symbols.h" | 8 #include "vm/symbols.h" |
9 #include "vm/unicode.h" | 9 #include "vm/unicode.h" |
10 | 10 |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 ::free(latin1); | 382 ::free(latin1); |
383 return object; | 383 return object; |
384 } | 384 } |
385 case kTwoByteStringCid: { | 385 case kTwoByteStringCid: { |
386 intptr_t len = ReadSmiValue(); | 386 intptr_t len = ReadSmiValue(); |
387 intptr_t hash = ReadSmiValue(); | 387 intptr_t hash = ReadSmiValue(); |
388 USE(hash); | 388 USE(hash); |
389 uint16_t *utf16 = | 389 uint16_t *utf16 = |
390 reinterpret_cast<uint16_t*>(::malloc(len * sizeof(uint16_t))); | 390 reinterpret_cast<uint16_t*>(::malloc(len * sizeof(uint16_t))); |
391 intptr_t utf8_len = 0; | 391 intptr_t utf8_len = 0; |
| 392 // Read all the UTF-16 code units. |
392 for (intptr_t i = 0; i < len; i++) { | 393 for (intptr_t i = 0; i < len; i++) { |
393 utf16[i] = Read<uint16_t>(); | 394 utf16[i] = Read<uint16_t>(); |
394 // TODO(sgjesse): Check for surrogate pairs. | 395 } |
395 utf8_len += Utf8::Length(utf16[i]); | 396 // Calculate the UTF-8 length and check if the string can be |
| 397 // UTF-8 encoded. |
| 398 bool valid = true; |
| 399 intptr_t i = 0; |
| 400 while (i < len && valid) { |
| 401 int32_t ch = Utf16::Next(utf16, &i, len); |
| 402 utf8_len += Utf8::Length(ch); |
| 403 valid = !Utf16::IsSurrogate(ch); |
| 404 } |
| 405 if (!valid) { |
| 406 return AllocateDartCObjectUnsupported(); |
396 } | 407 } |
397 Dart_CObject* object = AllocateDartCObjectString(utf8_len); | 408 Dart_CObject* object = AllocateDartCObjectString(utf8_len); |
398 AddBackRef(object_id, object, kIsDeserialized); | 409 AddBackRef(object_id, object, kIsDeserialized); |
399 char* p = object->value.as_string; | 410 char* p = object->value.as_string; |
400 for (intptr_t i = 0; i < len; i++) { | 411 i = 0; |
401 // TODO(sgjesse): Check for surrogate pairs. | 412 while (i < len) { |
402 p += Utf8::Encode(utf16[i], p); | 413 p += Utf8::Encode(Utf16::Next(utf16, &i, len), p); |
403 } | 414 } |
404 *p = '\0'; | 415 *p = '\0'; |
405 ASSERT(p == (object->value.as_string + utf8_len)); | 416 ASSERT(p == (object->value.as_string + utf8_len)); |
406 ::free(utf16); | 417 ::free(utf16); |
407 return object; | 418 return object; |
408 } | 419 } |
409 case kUint8ArrayCid: { | 420 case kUint8ArrayCid: { |
410 intptr_t len = ReadSmiValue(); | 421 intptr_t len = ReadSmiValue(); |
411 Dart_CObject* object = AllocateDartCObjectUint8Array(len); | 422 Dart_CObject* object = AllocateDartCObjectUint8Array(len); |
412 AddBackRef(object_id, object, kIsDeserialized); | 423 AddBackRef(object_id, object, kIsDeserialized); |
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
800 // Write out the class and tags information. | 811 // Write out the class and tags information. |
801 WriteIndexedObject(type == Utf8::kLatin1 ? kOneByteStringCid | 812 WriteIndexedObject(type == Utf8::kLatin1 ? kOneByteStringCid |
802 : kTwoByteStringCid); | 813 : kTwoByteStringCid); |
803 WriteIntptrValue(0); | 814 WriteIntptrValue(0); |
804 // Write string length, hash and content | 815 // Write string length, hash and content |
805 WriteSmi(len); | 816 WriteSmi(len); |
806 WriteSmi(0); // TODO(sgjesse): Hash - not written. | 817 WriteSmi(0); // TODO(sgjesse): Hash - not written. |
807 if (type == Utf8::kLatin1) { | 818 if (type == Utf8::kLatin1) { |
808 uint8_t* latin1_str = | 819 uint8_t* latin1_str = |
809 reinterpret_cast<uint8_t*>(::malloc(len * sizeof(uint8_t))); | 820 reinterpret_cast<uint8_t*>(::malloc(len * sizeof(uint8_t))); |
810 Utf8::DecodeToLatin1(utf8_str, utf8_len, latin1_str, len); | 821 bool success = Utf8::DecodeToLatin1(utf8_str, |
| 822 utf8_len, |
| 823 latin1_str, |
| 824 len); |
| 825 ASSERT(success); |
811 for (intptr_t i = 0; i < len; i++) { | 826 for (intptr_t i = 0; i < len; i++) { |
812 Write<uint8_t>(latin1_str[i]); | 827 Write<uint8_t>(latin1_str[i]); |
813 } | 828 } |
814 ::free(latin1_str); | 829 ::free(latin1_str); |
815 } else { | 830 } else { |
816 // TODO(sgjesse): Make sure surrogate pairs are handled. | |
817 uint16_t* utf16_str = | 831 uint16_t* utf16_str = |
818 reinterpret_cast<uint16_t*>(::malloc(len * sizeof(uint16_t))); | 832 reinterpret_cast<uint16_t*>(::malloc(len * sizeof(uint16_t))); |
819 Utf8::DecodeToUTF16(utf8_str, utf8_len, utf16_str, len); | 833 bool success = Utf8::DecodeToUTF16(utf8_str, utf8_len, utf16_str, len); |
| 834 ASSERT(success); |
820 for (intptr_t i = 0; i < len; i++) { | 835 for (intptr_t i = 0; i < len; i++) { |
821 Write<uint16_t>(utf16_str[i]); | 836 Write<uint16_t>(utf16_str[i]); |
822 } | 837 } |
823 ::free(utf16_str); | 838 ::free(utf16_str); |
824 } | 839 } |
825 break; | 840 break; |
826 } | 841 } |
827 case Dart_CObject::kUint8Array: { | 842 case Dart_CObject::kUint8Array: { |
828 // Write out the serialization header value for this object. | 843 // Write out the serialization header value for this object. |
829 WriteInlinedHeader(object); | 844 WriteInlinedHeader(object); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
882 if (!success) { | 897 if (!success) { |
883 UnmarkAllCObjects(object); | 898 UnmarkAllCObjects(object); |
884 return false; | 899 return false; |
885 } | 900 } |
886 } | 901 } |
887 UnmarkAllCObjects(object); | 902 UnmarkAllCObjects(object); |
888 return true; | 903 return true; |
889 } | 904 } |
890 | 905 |
891 } // namespace dart | 906 } // namespace dart |
OLD | NEW |