From 95257d41d0b946197093ad67657fb3d4f7f9030f Mon Sep 17 00:00:00 2001 From: Determinant Date: Sat, 22 Jun 2019 01:04:36 -0400 Subject: update C API; fix bugs --- src/stream.cpp | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) (limited to 'src/stream.cpp') diff --git a/src/stream.cpp b/src/stream.cpp index 71a5b27..f5d4ac9 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -6,8 +6,21 @@ extern "C" { uint256_t *uint256_new() { return new uint256_t(); } uint256_t *uint256_new_from_bytes(const uint8_t *arr) { - return new uint256_t(arr); + try { + return new uint256_t(arr); + } catch (...) { + return nullptr; + } } + +uint256_t *uint256_new_from_bytearray(const bytearray_t *bytes) { + try { + return new uint256_t(*bytes); + } catch (...) { + return nullptr; + } +} + void uint256_free(const uint256_t *self) { delete self; } bool uint256_is_null(const uint256_t *self) { return self->is_null(); } @@ -39,6 +52,22 @@ datastream_t *datastream_new_from_bytes(const uint8_t *base, size_t size) { } } +datastream_t *datastream_new_from_bytearray(const bytearray_t *bytes) { + try { + return new datastream_t(*bytes); + } catch (...) { + return nullptr; + } +} + +datastream_t *datastream_new_moved_from_bytearray(bytearray_t *bytes) { + try { + return new datastream_t(std::move(*bytes)); + } catch (...) { + return nullptr; + } +} + void datastream_free(const datastream_t *self) { delete self; } datastream_t *datastream_copy(const datastream_t *self) { @@ -98,9 +127,15 @@ uint256_t *datastream_get_hash(const datastream_t *self) { bytearray_t *bytearray_new_moved_from_datastream(datastream_t *_moved_src) { try { - auto res = new bytearray_t(std::move(*_moved_src)); - //delete _moved_src; - return res; + return new bytearray_t(std::move(*_moved_src)); + } catch (...) { + return nullptr; + } +} + +bytearray_t *bytearray_new_from_hex(const char *hex_str) { + try { + return new bytearray_t(salticidae::from_hex(hex_str)); } catch (...) { return nullptr; } -- cgit v1.2.3