From e3c46441518ec0ed284a61357fee17a7b46abecd Mon Sep 17 00:00:00 2001 From: tequ Date: Mon, 29 Sep 2025 12:50:03 +0900 Subject: [PATCH] float_sto_set --- src/ripple/app/hook/HookAPI.h | 5 ++ src/ripple/app/hook/impl/HookAPI.cpp | 71 ++++++++++++++++++++++++++ src/ripple/app/hook/impl/applyHook.cpp | 68 +++--------------------- 3 files changed, 82 insertions(+), 62 deletions(-) diff --git a/src/ripple/app/hook/HookAPI.h b/src/ripple/app/hook/HookAPI.h index 5d55ef7d9..bfb569773 100644 --- a/src/ripple/app/hook/HookAPI.h +++ b/src/ripple/app/hook/HookAPI.h @@ -95,7 +95,12 @@ public: float_sum(uint64_t float1, uint64_t float2) const; // float_sto + + Expected + float_sto_set(Bytes const& data) const; + // float_sto_set + Expected float_invert(uint64_t float1) const; diff --git a/src/ripple/app/hook/impl/HookAPI.cpp b/src/ripple/app/hook/impl/HookAPI.cpp index 3662b6a1b..d7930f698 100644 --- a/src/ripple/app/hook/impl/HookAPI.cpp +++ b/src/ripple/app/hook/impl/HookAPI.cpp @@ -988,6 +988,77 @@ HookAPI::float_sum(uint64_t float1, uint64_t float2) const } } +// float_sto + +Expected +HookAPI::float_sto_set(Bytes const& data) const +{ + uint8_t* upto = const_cast(data.data()); + uint8_t length = data.size(); + + if (length > 8) + { + uint8_t hi = upto[0] >> 4U; + uint8_t lo = upto[0] & 0xFU; + + if (hi == 0 && lo == 0) + { + // typecode >= 16 && fieldcode >= 16 + if (length < 11) + return Unexpected(NOT_AN_OBJECT); + upto += 3; + length -= 3; + } + else if (hi == 0 || lo == 0) + { + // typecode >= 16 && fieldcode < 16 + if (length < 10) + return Unexpected(NOT_AN_OBJECT); + upto += 2; + length -= 2; + } + else + { + // typecode < 16 && fieldcode < 16 + upto++; + length--; + } + } + + if (length < 8) + return Unexpected(NOT_AN_OBJECT); + + bool is_xrp = (((*upto) & 0b10000000U) == 0); + bool is_negative = (((*upto) & 0b01000000U) == 0); + + int32_t exponent = 0; + + if (is_xrp) + { + // exponent remains 0 + upto++; + } + else + { + exponent = (((*upto++) & 0b00111111U)) << 2U; + exponent += ((*upto) >> 6U); + exponent -= 97; + } + + uint64_t mantissa = (((uint64_t)(*upto++)) & 0b00111111U) << 48U; + mantissa += ((uint64_t)*upto++) << 40U; + mantissa += ((uint64_t)*upto++) << 32U; + mantissa += ((uint64_t)*upto++) << 24U; + mantissa += ((uint64_t)*upto++) << 16U; + mantissa += ((uint64_t)*upto++) << 8U; + mantissa += ((uint64_t)*upto++); + + if (mantissa == 0) + return 0; + + return hook_float::normalize_xfl(mantissa, exponent, is_negative); +} + Expected HookAPI::float_invert(uint64_t float1) const { diff --git a/src/ripple/app/hook/impl/applyHook.cpp b/src/ripple/app/hook/impl/applyHook.cpp index e54514f5a..fb0e18129 100644 --- a/src/ripple/app/hook/impl/applyHook.cpp +++ b/src/ripple/app/hook/impl/applyHook.cpp @@ -4253,69 +4253,13 @@ DEFINE_HOOK_FUNCTION( if (NOT_IN_BOUNDS(read_ptr, read_len, memory_length)) return OUT_OF_BOUNDS; - uint8_t* upto = memory + read_ptr; + Bytes data{read_ptr + memory, read_ptr + read_len + memory}; - if (read_len > 8) - { - uint8_t hi = memory[read_ptr] >> 4U; - uint8_t lo = memory[read_ptr] & 0xFU; - - if (hi == 0 && lo == 0) - { - // typecode >= 16 && fieldcode >= 16 - if (read_len < 11) - return NOT_AN_OBJECT; - upto += 3; - read_len -= 3; - } - else if (hi == 0 || lo == 0) - { - // typecode >= 16 && fieldcode < 16 - if (read_len < 10) - return NOT_AN_OBJECT; - upto += 2; - read_len -= 2; - } - else - { - // typecode < 16 && fieldcode < 16 - upto++; - read_len--; - } - } - - if (read_len < 8) - return NOT_AN_OBJECT; - - bool is_xrp = (((*upto) & 0b10000000U) == 0); - bool is_negative = (((*upto) & 0b01000000U) == 0); - - int32_t exponent = 0; - - if (is_xrp) - { - // exponent remains 0 - upto++; - } - else - { - exponent = (((*upto++) & 0b00111111U)) << 2U; - exponent += ((*upto) >> 6U); - exponent -= 97; - } - - uint64_t mantissa = (((uint64_t)(*upto++)) & 0b00111111U) << 48U; - mantissa += ((uint64_t)*upto++) << 40U; - mantissa += ((uint64_t)*upto++) << 32U; - mantissa += ((uint64_t)*upto++) << 24U; - mantissa += ((uint64_t)*upto++) << 16U; - mantissa += ((uint64_t)*upto++) << 8U; - mantissa += ((uint64_t)*upto++); - - if (mantissa == 0) - return 0; - - return hook_float::normalize_xfl(mantissa, exponent, is_negative); + hook::HookAPI api(hookCtx); + auto const result = api.float_sto_set(data); + if (!result) + return result.error(); + return result.value(); HOOK_TEARDOWN(); }