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 Utf16::CodePointIterator it(utf16, len); |
| 400 while (it.Next() && valid) { |
| 401 utf8_len += Utf8::Length(it.Current()); |
| 402 valid = !Utf16::IsSurrogate(it.Current()); |
| 403 } |
| 404 if (!valid) { |
| 405 return AllocateDartCObjectUnsupported(); |
396 } | 406 } |
397 Dart_CObject* object = AllocateDartCObjectString(utf8_len); | 407 Dart_CObject* object = AllocateDartCObjectString(utf8_len); |
398 AddBackRef(object_id, object, kIsDeserialized); | 408 AddBackRef(object_id, object, kIsDeserialized); |
399 char* p = object->value.as_string; | 409 char* p = object->value.as_string; |
400 for (intptr_t i = 0; i < len; i++) { | 410 it.Reset(); |
401 // TODO(sgjesse): Check for surrogate pairs. | 411 while (it.Next()) { |
402 p += Utf8::Encode(utf16[i], p); | 412 p += Utf8::Encode(it.Current(), p); |
403 } | 413 } |
404 *p = '\0'; | 414 *p = '\0'; |
405 ASSERT(p == (object->value.as_string + utf8_len)); | 415 ASSERT(p == (object->value.as_string + utf8_len)); |
406 ::free(utf16); | 416 ::free(utf16); |
407 return object; | 417 return object; |
408 } | 418 } |
409 case kUint8ArrayCid: { | 419 case kUint8ArrayCid: { |
410 intptr_t len = ReadSmiValue(); | 420 intptr_t len = ReadSmiValue(); |
411 Dart_CObject* object = AllocateDartCObjectUint8Array(len); | 421 Dart_CObject* object = AllocateDartCObjectUint8Array(len); |
412 AddBackRef(object_id, object, kIsDeserialized); | 422 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. | 810 // Write out the class and tags information. |
801 WriteIndexedObject(type == Utf8::kLatin1 ? kOneByteStringCid | 811 WriteIndexedObject(type == Utf8::kLatin1 ? kOneByteStringCid |
802 : kTwoByteStringCid); | 812 : kTwoByteStringCid); |
803 WriteIntptrValue(0); | 813 WriteIntptrValue(0); |
804 // Write string length, hash and content | 814 // Write string length, hash and content |
805 WriteSmi(len); | 815 WriteSmi(len); |
806 WriteSmi(0); // TODO(sgjesse): Hash - not written. | 816 WriteSmi(0); // TODO(sgjesse): Hash - not written. |
807 if (type == Utf8::kLatin1) { | 817 if (type == Utf8::kLatin1) { |
808 uint8_t* latin1_str = | 818 uint8_t* latin1_str = |
809 reinterpret_cast<uint8_t*>(::malloc(len * sizeof(uint8_t))); | 819 reinterpret_cast<uint8_t*>(::malloc(len * sizeof(uint8_t))); |
810 Utf8::DecodeToLatin1(utf8_str, utf8_len, latin1_str, len); | 820 bool success = Utf8::DecodeToLatin1(utf8_str, |
| 821 utf8_len, |
| 822 latin1_str, |
| 823 len); |
| 824 ASSERT(success); |
811 for (intptr_t i = 0; i < len; i++) { | 825 for (intptr_t i = 0; i < len; i++) { |
812 Write<uint8_t>(latin1_str[i]); | 826 Write<uint8_t>(latin1_str[i]); |
813 } | 827 } |
814 ::free(latin1_str); | 828 ::free(latin1_str); |
815 } else { | 829 } else { |
816 // TODO(sgjesse): Make sure surrogate pairs are handled. | 830 // 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 |