Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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/bigint_operations.h" | 5 #include "vm/bigint_operations.h" |
| 6 | 6 |
| 7 #include <openssl/crypto.h> | 7 #include <openssl/crypto.h> |
| 8 | 8 |
| 9 #include "vm/bigint_store.h" | 9 #include "vm/bigint_store.h" |
| 10 #include "vm/double_internals.h" | 10 #include "vm/double_internals.h" |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 result[0] = '0'; | 213 result[0] = '0'; |
| 214 result[1] = 'x'; | 214 result[1] = 'x'; |
| 215 // memcpy would suffice | 215 // memcpy would suffice |
| 216 memmove(result + 2, str, length - 2); | 216 memmove(result + 2, str, length - 2); |
| 217 } | 217 } |
| 218 OPENSSL_free(to_free); | 218 OPENSSL_free(to_free); |
| 219 return result; | 219 return result; |
| 220 } | 220 } |
| 221 | 221 |
| 222 | 222 |
| 223 const char* BigintOperations::ToDecCString(const Bigint& bigint, | |
| 224 uword (*allocator)(intptr_t size)) { | |
| 225 return ToDecCString(bigint.BNAddr(), allocator); | |
| 226 } | |
| 227 | |
| 228 | |
| 229 const char* BigintOperations::ToDecCString(const BIGNUM *bn, | |
| 230 uword (*allocator)(intptr_t size)) { | |
| 231 char* str = BN_bn2dec(bn); | |
|
cshapiro
2011/11/02 00:19:01
BN has historically used an inefficient algorithm
sra1
2011/11/02 03:10:23
It might be fun to write the b^(n*2^i) algorithm i
| |
| 232 intptr_t length = strlen(str) + 1; // '\0'-terminated. | |
| 233 char* result = reinterpret_cast<char*>(allocator(length)); | |
| 234 memmove(result, str, length); | |
| 235 OPENSSL_free(str); | |
| 236 return result; | |
| 237 } | |
| 238 | |
| 239 | |
| 223 const char* BigintOperations::ToHexCString(const Bigint& bigint, | 240 const char* BigintOperations::ToHexCString(const Bigint& bigint, |
| 224 uword (*allocator)(intptr_t size)) { | 241 uword (*allocator)(intptr_t size)) { |
| 225 return ToHexCString(bigint.BNAddr(), allocator); | 242 return ToHexCString(bigint.BNAddr(), allocator); |
| 226 } | 243 } |
| 227 | 244 |
| 228 | 245 |
| 229 bool BigintOperations::FitsIntoSmi(const Bigint& bigint) { | 246 bool BigintOperations::FitsIntoSmi(const Bigint& bigint) { |
| 230 const BIGNUM *bn = bigint.BNAddr(); | 247 const BIGNUM *bn = bigint.BNAddr(); |
| 231 int bits = BN_num_bits(bn); | 248 int bits = BN_num_bits(bn); |
| 232 // Special case for kMinValue as the absolute value is 1 bit longer | 249 // Special case for kMinValue as the absolute value is 1 bit longer |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 549 result.ToggleSign(); | 566 result.ToggleSign(); |
| 550 return result.raw(); | 567 return result.raw(); |
| 551 } | 568 } |
| 552 | 569 |
| 553 | 570 |
| 554 int BigintOperations::Compare(const Bigint& a, const Bigint& b) { | 571 int BigintOperations::Compare(const Bigint& a, const Bigint& b) { |
| 555 return BN_cmp(a.BNAddr(), b.BNAddr()); | 572 return BN_cmp(a.BNAddr(), b.BNAddr()); |
| 556 } | 573 } |
| 557 | 574 |
| 558 } // namespace dart | 575 } // namespace dart |
| OLD | NEW |