finish field encoding

This commit is contained in:
Michele Balistreri 2023-08-11 11:14:41 +02:00
parent 85e6fc863c
commit 52d3ae6d56
No known key found for this signature in database
GPG Key ID: E9567DA33A4F791A
3 changed files with 48 additions and 13 deletions

View File

@ -75,3 +75,29 @@ bool base16_decode(const char* s, uint8_t* out, size_t s_len) {
return true;
}
bool atoi64(const char* str, size_t len, int64_t* res) {
int i;
int sign;
if (str[0] == '-') {
sign = -1;
i = 1;
} else if (str[0] == '+') {
i = 1;
} else {
i = 0;
}
while(i < len) {
if (!(str[i] >= '0' && str[i] <= '9')) {
return false;
}
*res = (10 * (*res)) + (str[i++] - '0');
}
*res = (*res) * sign;
return true;
}

View File

@ -11,6 +11,7 @@ uint8_t* u32toa(uint32_t value, uint8_t* buf, uint32_t len);
size_t strnlen(const char *s, size_t maxlen);
bool base16_decode(const char* s, uint8_t* out, size_t s_len);
bool atoi64(const char* str, size_t len, int64_t* res);
static inline int memcmp_ct(const uint8_t* a, const uint8_t* b, size_t length) {
int compareSum = 0;

View File

@ -396,32 +396,40 @@ static app_err_t eip712_encode_field(uint8_t out[32], uint8_t* heap, size_t heap
memset(out, 0, 32);
out[31] = json[tokens[field_val].start] == 't';
} else if (field_type->str[0] == 'b') {
if (tokens[field_val].type != JSMN_STRING) {
return ERR_DATA;
}
} else if (tokens[field_val].type == JSMN_STRING) {
struct eip712_string tmpstr;
tmpstr.str = &json[tokens[field_val].start];
tmpstr.len = tokens[field_val].end - tokens[field_val].start;
if ((tmpstr.len > 2) && (tmpstr.str[0] == '0') && (tmpstr.str[1] == 'x')) {
memset(out, 0, 32);
int out_len = ((tmpstr.len - 1) >> 1);
int padding = 32 - out_len;
int offset;
if (!base16_decode(&tmpstr.str[2], out, (tmpstr.len - 2))) {
return ERR_DATA;
}
// bytesX are right padded, others are left padded
if (field_type->str[0] == 'b') {
offset = 0;
memset(&out[offset], 0, padding);
} else {
return ERR_DATA;
offset = padding;
memset(out, 0, padding);
}
} else {
if (tokens[field_val].type != JSMN_STRING) {
return ERR_DATA;
} else if (tokens[field_val].type != JSMN_PRIMITIVE) {
if (!base16_decode(&tmpstr.str[2], &out[offset], (tmpstr.len - 2))) {
return ERR_DATA;
}
} else {
return ERR_DATA;
}
} else if (tokens[field_val].type == JSMN_PRIMITIVE) {
int64_t res;
if (!atoi64(&json[tokens[field_val].start], (tokens[field_val].end - tokens[field_val].start), &res)) {
return ERR_DATA;
}
memset(out, res < 0 ? 0xff : 0x00, 24);
} else {
return ERR_DATA;
}
return ERR_OK;