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/crypto.h" | 5 #include "bin/crypto.h" |
6 #include "bin/dartutils.h" | 6 #include "bin/dartutils.h" |
7 | 7 |
8 #include "include/dart_api.h" | 8 #include "include/dart_api.h" |
9 | 9 |
10 | |
11 namespace dart { | 10 namespace dart { |
12 namespace bin { | 11 namespace bin { |
13 | 12 |
14 void FUNCTION_NAME(Crypto_GetRandomBytes)(Dart_NativeArguments args) { | 13 void FUNCTION_NAME(Crypto_GetRandomBytes)(Dart_NativeArguments args) { |
15 Dart_Handle count_obj = Dart_GetNativeArgument(args, 0); | 14 Dart_Handle count_obj = Dart_GetNativeArgument(args, 0); |
16 const int64_t kMaxRandomBytes = 4096; | 15 const int64_t kMaxRandomBytes = 4096; |
17 int64_t count64 = 0; | 16 int64_t count64 = 0; |
18 if (!DartUtils::GetInt64Value(count_obj, &count64) || | 17 if (!DartUtils::GetInt64Value(count_obj, &count64) || |
19 (count64 < 0) || (count64 > kMaxRandomBytes)) { | 18 (count64 < 0) || (count64 > kMaxRandomBytes)) { |
20 Dart_Handle error = | 19 Dart_Handle error = |
21 DartUtils::NewString("Invalid argument: count must be a positive int " | 20 DartUtils::NewString("Invalid argument: count must be a positive int " |
22 "less than or equal to 4096."); | 21 "less than or equal to 4096."); |
23 Dart_ThrowException(error); | 22 Dart_ThrowException(error); |
24 } | 23 } |
25 intptr_t count = static_cast<intptr_t>(count64); | 24 intptr_t count = static_cast<intptr_t>(count64); |
26 uint8_t* buffer = new uint8_t[count]; | 25 uint8_t* buffer = Dart_ScopeAllocate(count); |
27 ASSERT(buffer != NULL); | 26 ASSERT(buffer != NULL); |
28 if (!Crypto::GetRandomBytes(count, buffer)) { | 27 if (!Crypto::GetRandomBytes(count, buffer)) { |
29 delete[] buffer; | |
30 Dart_ThrowException(DartUtils::NewDartOSError()); | 28 Dart_ThrowException(DartUtils::NewDartOSError()); |
31 UNREACHABLE(); | 29 UNREACHABLE(); |
32 } | 30 } |
33 Dart_Handle result = Dart_NewTypedData(Dart_TypedData_kUint8, count); | 31 Dart_Handle result = Dart_NewTypedData(Dart_TypedData_kUint8, count); |
34 if (Dart_IsError(result)) { | 32 if (Dart_IsError(result)) { |
35 delete[] buffer; | |
36 Dart_Handle error = DartUtils::NewString("Failed to allocate storage."); | 33 Dart_Handle error = DartUtils::NewString("Failed to allocate storage."); |
37 Dart_ThrowException(error); | 34 Dart_ThrowException(error); |
38 UNREACHABLE(); | 35 UNREACHABLE(); |
39 } | 36 } |
40 Dart_ListSetAsBytes(result, 0, buffer, count); | 37 Dart_ListSetAsBytes(result, 0, buffer, count); |
41 Dart_SetReturnValue(args, result); | 38 Dart_SetReturnValue(args, result); |
42 delete[] buffer; | |
43 } | 39 } |
44 | 40 |
45 } // namespace bin | 41 } // namespace bin |
46 } // namespace dart | 42 } // namespace dart |
OLD | NEW |