-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Represent big integers as arrays of bytes #87
base: main
Are you sure you want to change the base?
Changes from all commits
45775f8
235b878
89f70bc
9b4171d
cc31c50
57bdd1a
f5e6d0d
9525138
98e3fd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ int exi_basetypes_convert_to_unsigned(exi_unsigned_t* exi_unsigned, uint32_t val | |
exi_unsigned->octets_count++; | ||
*current_octet = (uint8_t)(dummy & EXI_BASETYPES_OCTET_SEQ_VALUE_MASK); | ||
|
||
dummy = dummy >> 7u; | ||
dummy >>= 7u; | ||
if (dummy == 0) | ||
{ | ||
break; | ||
|
@@ -45,7 +45,7 @@ int exi_basetypes_convert_64_to_unsigned(exi_unsigned_t* exi_unsigned, uint64_t | |
exi_unsigned->octets_count++; | ||
*current_octet = (uint8_t)(dummy & EXI_BASETYPES_OCTET_SEQ_VALUE_MASK); | ||
|
||
dummy = dummy >> 7u; | ||
dummy >>= 7u; | ||
if (dummy == 0) | ||
{ | ||
break; | ||
|
@@ -134,6 +134,24 @@ int exi_basetypes_convert_64_from_signed(const exi_signed_t* exi_signed, int64_t | |
return res; | ||
} | ||
|
||
static void _reverse_array(uint8_t* data, size_t data_size) | ||
{ | ||
if (!data || !data_size) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this function is static and you control the inputs, why should it have such a silent error handling of data? It will have gone wrong in data_size makes sense of course. |
||
{ | ||
return; | ||
} | ||
|
||
size_t i = 0; | ||
size_t j = data_size - 1; | ||
while (i < j) { | ||
const uint8_t temp = data[i]; | ||
data[i] = data[j]; | ||
data[j] = temp; | ||
i++; | ||
j--; | ||
} | ||
} | ||
|
||
int exi_basetypes_convert_bytes_from_unsigned(const exi_unsigned_t* exi_unsigned, uint8_t* data, size_t* data_len, size_t data_size) | ||
{ | ||
const uint8_t* current_octet = exi_unsigned->octets; | ||
|
@@ -145,21 +163,24 @@ int exi_basetypes_convert_bytes_from_unsigned(const exi_unsigned_t* exi_unsigned | |
temp += ((uint16_t)(*current_octet & EXI_BASETYPES_OCTET_SEQ_VALUE_MASK) << total_offset); | ||
total_offset += 7; | ||
if (total_offset >= 8) { | ||
if (data_size == *data_len) { | ||
if (*data_len >= data_size) { | ||
return EXI_ERROR__ENCODED_INTEGER_SIZE_LARGER_THAN_DESTINATION; | ||
} | ||
total_offset -= 8; | ||
data[(*data_len)++] = temp & 0xFF; | ||
data[*data_len] = temp & 0xFF; | ||
(*data_len)++; | ||
temp >>= 8; | ||
} | ||
current_octet++; | ||
} | ||
if (total_offset != 0) { | ||
if (data_size == *data_len) { | ||
if (total_offset != 0 && (temp & 0xFF) != 0) { | ||
if (*data_len >= data_size) { | ||
return EXI_ERROR__ENCODED_INTEGER_SIZE_LARGER_THAN_DESTINATION; | ||
} | ||
data[(*data_len)++] = temp & 0xFF; | ||
data[*data_len] = temp & 0xFF; | ||
(*data_len)++; | ||
} | ||
_reverse_array(data, *data_len); | ||
return EXI_ERROR__NO_ERROR; | ||
} | ||
|
||
|
@@ -169,24 +190,20 @@ int exi_basetypes_convert_bytes_to_unsigned(exi_unsigned_t* exi_unsigned, const | |
uint16_t dummy = 0; | ||
uint8_t dummy_count = 0; | ||
|
||
for (size_t n = 0; n != data_len; n++, current_octet++) { | ||
for (size_t n = 0; n < data_len; n++, current_octet++) { | ||
if (dummy_count <= 8) { | ||
dummy |= (data[n] << dummy_count); | ||
dummy |= (data[data_len - n - 1] << dummy_count); | ||
dummy_count += 8; | ||
} | ||
exi_unsigned->octets_count++; | ||
*current_octet = (uint8_t)(dummy & EXI_BASETYPES_OCTET_SEQ_VALUE_MASK); | ||
*current_octet |= EXI_BASETYPES_OCTET_SEQ_FLAG_MASK; | ||
|
||
dummy >>= 7u; | ||
dummy_count -= 7; | ||
if (n == data_len) { | ||
break; | ||
} | ||
|
||
*current_octet |= EXI_BASETYPES_OCTET_SEQ_FLAG_MASK; | ||
} | ||
if (dummy_count > 0) { | ||
*(current_octet-1) |= EXI_BASETYPES_OCTET_SEQ_FLAG_MASK; | ||
if (dummy_count > 0 && dummy != 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
*(current_octet - 1) |= EXI_BASETYPES_OCTET_SEQ_FLAG_MASK; | ||
exi_unsigned->octets_count++; | ||
*current_octet = (uint8_t)(dummy & EXI_BASETYPES_OCTET_SEQ_VALUE_MASK); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get why we want to reverse it, but I rather see it written backwards from the get go to speed it up a bit.
Nice to have. I realise this is technically not easy.