Now uses an eip712 context struct instead of having multiple global variables => reduced bss footprint
This commit is contained in:
@@ -5,15 +5,13 @@
|
||||
#include "context.h"
|
||||
#include "eip712.h"
|
||||
#include "mem.h"
|
||||
#include "mem_utils.h"
|
||||
#include "sol_typenames.h"
|
||||
#include "path.h"
|
||||
#include "field_hash.h"
|
||||
#include "ui_logic.h"
|
||||
|
||||
uint8_t *typenames_array;
|
||||
uint8_t *structs_array;
|
||||
uint8_t *current_struct_fields_array;
|
||||
bool eip712_context_initialized = false;
|
||||
s_eip712_context *eip712_context = NULL;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -24,6 +22,11 @@ bool eip712_context_init(void)
|
||||
// init global variables
|
||||
mem_init();
|
||||
|
||||
if ((eip712_context = MEM_ALLOC_AND_ALIGN_TO_TYPE(sizeof(*eip712_context), *eip712_context)) == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (sol_typenames_init() == false)
|
||||
{
|
||||
return false;
|
||||
@@ -45,15 +48,13 @@ bool eip712_context_init(void)
|
||||
}
|
||||
|
||||
// set types pointer
|
||||
if ((structs_array = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||
if ((eip712_context->structs_array = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// create len(types)
|
||||
*structs_array = 0;
|
||||
|
||||
eip712_context_initialized = true;
|
||||
*(eip712_context->structs_array) = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -64,7 +65,7 @@ void eip712_context_deinit(void)
|
||||
field_hash_deinit();
|
||||
ui_712_deinit();
|
||||
mem_reset();
|
||||
eip712_context_initialized = false;
|
||||
eip712_context = NULL;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -5,15 +5,18 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
extern uint8_t *typenames_array;
|
||||
extern uint8_t *structs_array;
|
||||
extern uint8_t *current_struct_fields_array;
|
||||
typedef struct
|
||||
{
|
||||
uint8_t *typenames_array;
|
||||
uint8_t *structs_array;
|
||||
uint8_t *current_struct_fields_array;
|
||||
} s_eip712_context;
|
||||
|
||||
extern s_eip712_context *eip712_context;
|
||||
|
||||
bool eip712_context_init(void);
|
||||
void eip712_context_deinit(void);
|
||||
|
||||
extern bool eip712_context_initialized;
|
||||
|
||||
#endif // HAVE_EIP712_FULL_SUPPORT
|
||||
|
||||
#endif // EIP712_CTX_H_
|
||||
|
||||
@@ -239,7 +239,7 @@ bool set_struct_name(const uint8_t *const data)
|
||||
char *name_ptr;
|
||||
|
||||
// increment number of structs
|
||||
*structs_array += 1;
|
||||
*(eip712_context->structs_array) += 1;
|
||||
|
||||
// copy length
|
||||
if ((length_ptr = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||
@@ -256,11 +256,11 @@ bool set_struct_name(const uint8_t *const data)
|
||||
memmove(name_ptr, &data[OFFSET_CDATA], data[OFFSET_LC]);
|
||||
|
||||
// initialize number of fields
|
||||
if ((current_struct_fields_array = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||
if ((eip712_context->current_struct_fields_array = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
*current_struct_fields_array = 0;
|
||||
*(eip712_context->current_struct_fields_array) = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -280,7 +280,7 @@ bool set_struct_field(const uint8_t *const data)
|
||||
char *fieldname_ptr;
|
||||
|
||||
// increment number of struct fields
|
||||
*current_struct_fields_array += 1;
|
||||
*(eip712_context->current_struct_fields_array) += 1;
|
||||
|
||||
// copy TypeDesc
|
||||
if ((type_desc_ptr = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||
@@ -369,7 +369,7 @@ bool handle_eip712_struct_def(const uint8_t *const apdu_buf)
|
||||
{
|
||||
bool ret = true;
|
||||
|
||||
if (!eip712_context_initialized)
|
||||
if (eip712_context == NULL)
|
||||
{
|
||||
if (!eip712_context_init())
|
||||
{
|
||||
|
||||
@@ -59,7 +59,7 @@ static const void *get_nth_field_from_path(uint8_t *const fields_count_ptr,
|
||||
if (struct_field_type(field_ptr) == TYPE_CUSTOM)
|
||||
{
|
||||
typename = get_struct_field_typename(field_ptr, &length);
|
||||
if ((struct_ptr = get_structn(structs_array, typename, length)) == NULL)
|
||||
if ((struct_ptr = get_structn(eip712_context->structs_array, typename, length)) == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
@@ -273,7 +273,7 @@ static bool path_update(void)
|
||||
while (struct_field_type(field_ptr) == TYPE_CUSTOM)
|
||||
{
|
||||
typename = get_struct_field_typename(field_ptr, &typename_len);
|
||||
if ((struct_ptr = get_structn(structs_array, typename, typename_len)) == NULL)
|
||||
if ((struct_ptr = get_structn(eip712_context->structs_array, typename, typename_len)) == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -293,7 +293,7 @@ static bool path_update(void)
|
||||
}
|
||||
cx_keccak_init(hash_ctx, 256); // initialize it
|
||||
// get the struct typehash
|
||||
if ((thash_ptr = type_hash(structs_array, typename, typename_len)) == NULL)
|
||||
if ((thash_ptr = type_hash(eip712_context->structs_array, typename, typename_len)) == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -326,7 +326,7 @@ bool path_set_root(const char *const struct_name, uint8_t name_length)
|
||||
return false;
|
||||
}
|
||||
|
||||
path_struct->root_struct = get_structn(structs_array, struct_name, name_length);
|
||||
path_struct->root_struct = get_structn(eip712_context->structs_array, struct_name, name_length);
|
||||
|
||||
if (path_struct->root_struct == NULL)
|
||||
{
|
||||
@@ -347,7 +347,7 @@ bool path_set_root(const char *const struct_name, uint8_t name_length)
|
||||
return false;
|
||||
}
|
||||
cx_keccak_init(hash_ctx, 256); // init hash
|
||||
if ((thash_ptr = type_hash(structs_array, struct_name, name_length)) == NULL)
|
||||
if ((thash_ptr = type_hash(eip712_context->structs_array, struct_name, name_length)) == NULL)
|
||||
{
|
||||
PRINTF("Memory allocation failed!\n");
|
||||
return false;
|
||||
|
||||
@@ -64,11 +64,11 @@ bool sol_typenames_init(void)
|
||||
uint8_t *typename_len_ptr;
|
||||
char *typename_ptr;
|
||||
|
||||
if ((typenames_array = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||
if ((eip712_context->typenames_array = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
*typenames_array = 0;
|
||||
*(eip712_context->typenames_array) = 0;
|
||||
// loop over typenames
|
||||
for (uint8_t s_idx = 0; s_idx < ARRAY_SIZE(typenames); ++s_idx)
|
||||
{
|
||||
@@ -90,7 +90,7 @@ bool sol_typenames_init(void)
|
||||
memcpy(typename_ptr, PIC(typenames[s_idx]), *typename_len_ptr);
|
||||
}
|
||||
// increment array size
|
||||
*typenames_array += 1;
|
||||
*(eip712_context->typenames_array) += 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -111,7 +111,7 @@ const char *get_struct_field_sol_typename(const uint8_t *field_ptr,
|
||||
bool typename_found;
|
||||
|
||||
field_type = struct_field_type(field_ptr);
|
||||
typename_ptr = get_array_in_mem(typenames_array, &typenames_count);
|
||||
typename_ptr = get_array_in_mem(eip712_context->typenames_array, &typenames_count);
|
||||
typename_found = false;
|
||||
while (typenames_count-- > 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user