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 "bin/dartutils.h" | 5 #include "bin/dartutils.h" |
6 | 6 |
7 #include "bin/crypto.h" | 7 #include "bin/crypto.h" |
8 #include "bin/directory.h" | 8 #include "bin/directory.h" |
9 #include "bin/extensions.h" | 9 #include "bin/extensions.h" |
10 #include "bin/file.h" | 10 #include "bin/file.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 const char* const DartUtils::kBuiltinLibURL = "dart:_builtin"; | 39 const char* const DartUtils::kBuiltinLibURL = "dart:_builtin"; |
40 const char* const DartUtils::kCoreLibURL = "dart:core"; | 40 const char* const DartUtils::kCoreLibURL = "dart:core"; |
41 const char* const DartUtils::kInternalLibURL = "dart:_internal"; | 41 const char* const DartUtils::kInternalLibURL = "dart:_internal"; |
42 const char* const DartUtils::kIsolateLibURL = "dart:isolate"; | 42 const char* const DartUtils::kIsolateLibURL = "dart:isolate"; |
43 const char* const DartUtils::kIOLibURL = "dart:io"; | 43 const char* const DartUtils::kIOLibURL = "dart:io"; |
44 const char* const DartUtils::kIOLibPatchURL = "dart:io-patch"; | 44 const char* const DartUtils::kIOLibPatchURL = "dart:io-patch"; |
45 const char* const DartUtils::kUriLibURL = "dart:uri"; | 45 const char* const DartUtils::kUriLibURL = "dart:uri"; |
46 const char* const DartUtils::kHttpScheme = "http:"; | 46 const char* const DartUtils::kHttpScheme = "http:"; |
47 const char* const DartUtils::kVMServiceLibURL = "dart:vmservice"; | 47 const char* const DartUtils::kVMServiceLibURL = "dart:vmservice"; |
48 | 48 |
49 const uint8_t DartUtils::magic_number[] = { 0xf5, 0xf5, 0xdc, 0xdc }; | 49 |
| 50 struct MagicNumberData { |
| 51 static const intptr_t kLength = 4; |
| 52 |
| 53 const uint8_t bytes[kLength]; |
| 54 bool should_skip; |
| 55 }; |
| 56 |
| 57 |
| 58 MagicNumberData snapshot_magic_number = { { 0xf5, 0xf5, 0xdc, 0xdc }, true }; |
| 59 MagicNumberData kernel_magic_number = { {0x90, 0xab, 0xcd, 0xef}, false }; |
| 60 |
| 61 |
| 62 bool TryReadKernel(const char* script_uri, |
| 63 const uint8_t** kernel_file, |
| 64 intptr_t* kernel_length) { |
| 65 *kernel_file = NULL; |
| 66 *kernel_length = -1; |
| 67 bool is_kernel_file = false; |
| 68 void* script_file = DartUtils::OpenFile(script_uri, false); |
| 69 if (script_file != NULL) { |
| 70 const uint8_t* buffer = NULL; |
| 71 DartUtils::ReadFile(&buffer, kernel_length, script_file); |
| 72 DartUtils::CloseFile(script_file); |
| 73 if (*kernel_length > 0 && buffer != NULL) { |
| 74 *kernel_file = buffer; |
| 75 if (DartUtils::SniffForMagicNumber(&buffer, kernel_length) != |
| 76 DartUtils::kKernelMagicNumber) { |
| 77 free(const_cast<uint8_t*>(buffer)); |
| 78 *kernel_file = NULL; |
| 79 } else { |
| 80 // Do not free buffer if this is a kernel file - kernel_file will be |
| 81 // backed by the same memory as the buffer and caller will own it. |
| 82 // Caller is responsible for freeing the buffer when this function |
| 83 // returns true. |
| 84 is_kernel_file = true; |
| 85 } |
| 86 } |
| 87 } |
| 88 return is_kernel_file; |
| 89 } |
50 | 90 |
51 | 91 |
52 static bool IsWindowsHost() { | 92 static bool IsWindowsHost() { |
53 #if defined(TARGET_OS_WINDOWS) | 93 #if defined(TARGET_OS_WINDOWS) |
54 return true; | 94 return true; |
55 #else // defined(TARGET_OS_WINDOWS) | 95 #else // defined(TARGET_OS_WINDOWS) |
56 return false; | 96 return false; |
57 #endif // defined(TARGET_OS_WINDOWS) | 97 #endif // defined(TARGET_OS_WINDOWS) |
58 } | 98 } |
59 | 99 |
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 extension_path, | 500 extension_path, |
461 library); | 501 library); |
462 } | 502 } |
463 | 503 |
464 // Handle 'import' or 'part' requests for all other URIs. Call dart code to | 504 // Handle 'import' or 'part' requests for all other URIs. Call dart code to |
465 // read the source code asynchronously. | 505 // read the source code asynchronously. |
466 return LoadDataAsync_Invoke(Dart_NewInteger(tag), url, library_url); | 506 return LoadDataAsync_Invoke(Dart_NewInteger(tag), url, library_url); |
467 } | 507 } |
468 | 508 |
469 | 509 |
470 const uint8_t* DartUtils::SniffForMagicNumber(const uint8_t* text_buffer, | 510 static bool CheckMagicNumber(const uint8_t** buf, |
471 intptr_t* buffer_len, | 511 intptr_t* len, |
472 bool* is_snapshot) { | 512 const MagicNumberData& magic_number) { |
473 intptr_t len = sizeof(magic_number); | 513 if ((*len >= MagicNumberData::kLength) && |
474 if (*buffer_len <= len) { | 514 (memcmp(*buf, magic_number.bytes, MagicNumberData::kLength) == 0)) { |
475 *is_snapshot = false; | 515 if (magic_number.should_skip) { |
476 return text_buffer; | 516 *buf += MagicNumberData::kLength; |
| 517 *len -= MagicNumberData::kLength; |
| 518 } |
| 519 return true; |
477 } | 520 } |
478 for (intptr_t i = 0; i < len; i++) { | 521 return false; |
479 if (text_buffer[i] != magic_number[i]) { | 522 } |
480 *is_snapshot = false; | 523 |
481 return text_buffer; | 524 |
482 } | 525 DartUtils::MagicNumber DartUtils::SniffForMagicNumber(const uint8_t** buf, |
| 526 intptr_t* len) { |
| 527 if (CheckMagicNumber(buf, len, snapshot_magic_number)) { |
| 528 return kSnapshotMagicNumber; |
483 } | 529 } |
484 *is_snapshot = true; | 530 |
485 ASSERT(*buffer_len > len); | 531 if (CheckMagicNumber(buf, len, kernel_magic_number)) { |
486 *buffer_len -= len; | 532 return kKernelMagicNumber; |
487 return text_buffer + len; | 533 } |
| 534 |
| 535 return kUnknownMagicNumber; |
488 } | 536 } |
489 | 537 |
490 | 538 |
491 void DartUtils::WriteMagicNumber(File* file) { | 539 void DartUtils::WriteMagicNumber(File* file) { |
492 // Write a magic number and version information into the snapshot file. | 540 // Write a magic number and version information into the snapshot file. |
493 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number)); | 541 bool bytes_written = file->WriteFully(snapshot_magic_number.bytes, |
| 542 MagicNumberData::kLength); |
494 ASSERT(bytes_written); | 543 ASSERT(bytes_written); |
495 } | 544 } |
496 | 545 |
497 | 546 |
498 Dart_Handle DartUtils::LoadScript(const char* script_uri) { | 547 Dart_Handle DartUtils::LoadScript(const char* script_uri) { |
499 Dart_TimelineEvent("LoadScript", | 548 Dart_TimelineEvent("LoadScript", |
500 Dart_TimelineGetMicros(), | 549 Dart_TimelineGetMicros(), |
501 Dart_GetMainPortId(), | 550 Dart_GetMainPortId(), |
502 Dart_Timeline_Event_Async_Begin, | 551 Dart_Timeline_Event_Async_Begin, |
503 0, NULL, NULL); | 552 0, NULL, NULL); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 // If the buffer is not external, take a copy. | 600 // If the buffer is not external, take a copy. |
552 buffer_copy = reinterpret_cast<uint8_t*>(malloc(num_bytes)); | 601 buffer_copy = reinterpret_cast<uint8_t*>(malloc(num_bytes)); |
553 memmove(buffer_copy, data, num_bytes); | 602 memmove(buffer_copy, data, num_bytes); |
554 data = buffer_copy; | 603 data = buffer_copy; |
555 } | 604 } |
556 | 605 |
557 Dart_TypedDataReleaseData(source_data); | 606 Dart_TypedDataReleaseData(source_data); |
558 | 607 |
559 if (Dart_IsNull(tag_in) && Dart_IsNull(library_uri)) { | 608 if (Dart_IsNull(tag_in) && Dart_IsNull(library_uri)) { |
560 // Entry file. Check for payload and load accordingly. | 609 // Entry file. Check for payload and load accordingly. |
561 bool is_snapshot = false; | 610 const uint8_t* payload = data; |
562 const uint8_t *payload = | 611 const DartUtils::MagicNumber payload_type = |
563 DartUtils::SniffForMagicNumber(data, &num_bytes, &is_snapshot); | 612 DartUtils::SniffForMagicNumber(&payload, &num_bytes); |
564 | 613 |
565 if (is_snapshot) { | 614 if (payload_type == DartUtils::kSnapshotMagicNumber) { |
566 result = Dart_LoadScriptFromSnapshot(payload, num_bytes); | 615 result = Dart_LoadScriptFromSnapshot(payload, num_bytes); |
| 616 } else if (payload_type == DartUtils::kKernelMagicNumber) { |
| 617 UNREACHABLE(); |
567 } else { | 618 } else { |
568 Dart_Handle source = Dart_NewStringFromUTF8(data, num_bytes); | 619 Dart_Handle source = Dart_NewStringFromUTF8(data, num_bytes); |
569 if (Dart_IsError(source)) { | 620 if (Dart_IsError(source)) { |
570 result = DartUtils::NewError("%s is not a valid UTF-8 script", | 621 result = DartUtils::NewError("%s is not a valid UTF-8 script", |
571 resolved_script_uri); | 622 resolved_script_uri); |
572 } else { | 623 } else { |
573 result = Dart_LoadScript(resolved_script_uri, Dart_Null(), | 624 result = Dart_LoadScript(resolved_script_uri, Dart_Null(), |
574 source, 0, 0); | 625 source, 0, 0); |
575 } | 626 } |
576 } | 627 } |
(...skipping 686 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1263 new CObjectString(CObject::NewString(os_error->message())); | 1314 new CObjectString(CObject::NewString(os_error->message())); |
1264 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 1315 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
1265 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 1316 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
1266 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 1317 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
1267 result->SetAt(2, error_message); | 1318 result->SetAt(2, error_message); |
1268 return result; | 1319 return result; |
1269 } | 1320 } |
1270 | 1321 |
1271 } // namespace bin | 1322 } // namespace bin |
1272 } // namespace dart | 1323 } // namespace dart |
OLD | NEW |