Fix UI display of signed negative integers smaller than 256 bits

This commit is contained in:
Alexandre Paillier
2022-06-08 18:30:25 +02:00
parent bbcfe5abc3
commit d5603857b3
3 changed files with 60 additions and 4 deletions

View File

@@ -34,6 +34,16 @@ void array_hexstr(char *strbuf, const void *bin, unsigned int len) {
*strbuf = 0; // EOS
}
void convertUint64BEto128(const uint8_t *const data, uint32_t length, uint128_t *const target) {
uint8_t tmp[INT128_LENGTH];
int64_t value;
value = u64_from_BE(data, length);
memset(tmp, ((value < 0) ? 0xff : 0), sizeof(tmp) - length);
memmove(tmp + sizeof(tmp) - length, data, length);
readu128BE(tmp, target);
}
void convertUint128BE(const uint8_t *const data, uint32_t length, uint128_t *const target) {
uint8_t tmp[INT128_LENGTH];

View File

@@ -26,6 +26,7 @@ void array_hexstr(char* strbuf, const void* bin, unsigned int len);
void convertUint128BE(const uint8_t *const data, uint32_t length, uint128_t *const target);
void convertUint256BE(const uint8_t *const data, uint32_t length, uint256_t *const target);
void convertUint64BEto128(const uint8_t *const data, uint32_t length, uint128_t *const target);
uint64_t u64_from_BE(const uint8_t* in, uint8_t size);

View File

@@ -14,8 +14,7 @@
#include "utils.h" // uint256_to_decimal
#include "common_712.h"
#include "context.h" // eip712_context_deinit
#include "uint256.h" // tostring256
#include "int256.h" // tostring256_s
#include "uint256.h" // tostring256 && tostring256_signed
static t_ui_context *ui_ctx = NULL;
@@ -111,6 +110,9 @@ void ui_712_new_field(const void *const field_ptr, const uint8_t *const data,
const char *key;
uint8_t key_len;
uint256_t value256;
uint128_t value128;
int32_t value32;
int16_t value16;
if (ui_ctx == NULL)
{
@@ -166,8 +168,51 @@ void ui_712_new_field(const void *const field_ptr, const uint8_t *const data,
}
break;
case TYPE_SOL_INT:
convertUint256BE(data, length, &value256);
tostring256_s(&value256, 10, strings.tmp.tmp, sizeof(strings.tmp.tmp));
switch (get_struct_field_typesize(field_ptr) * 8)
{
case 256:
convertUint256BE(data, length, &value256);
tostring256_signed(&value256, 10, strings.tmp.tmp, sizeof(strings.tmp.tmp));
break;
case 128:
convertUint128BE(data, length, &value128);
tostring128_signed(&value128, 10, strings.tmp.tmp, sizeof(strings.tmp.tmp));
break;
case 64:
convertUint64BEto128(data, length, &value128);
tostring128_signed(&value128, 10, strings.tmp.tmp, sizeof(strings.tmp.tmp));
break;
case 32:
value32 = 0;
for (int i = 0; i < length; ++i)
{
((uint8_t*)&value32)[length - 1 - i] = data[i];
}
snprintf(strings.tmp.tmp,
sizeof(strings.tmp.tmp),
"%d",
value32);
break;
case 16:
value16 = 0;
for (int i = 0; i < length; ++i)
{
((uint8_t*)&value16)[length - 1 - i] = data[i];
}
snprintf(strings.tmp.tmp,
sizeof(strings.tmp.tmp),
"%d",
value16); // expanded to 32 bits
break;
case 8:
snprintf(strings.tmp.tmp,
sizeof(strings.tmp.tmp),
"%d",
((int8_t*)data)[0]); // expanded to 32 bits
break;
default:
PRINTF("Unhandled field typesize\n");
}
break;
case TYPE_SOL_UINT:
convertUint256BE(data, length, &value256);