Skip to content
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

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
49 changes: 33 additions & 16 deletions src/input/code_templates/c/static_code/exi_basetypes.c.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Copy link
Contributor

@SiebrenW SiebrenW Oct 26, 2024

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.

{
if (!data || !data_size)
Copy link
Contributor

Choose a reason for hiding this comment

The 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 exi_basetypes_convert_bytes_from_unsigned before this function is reached.

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;
Expand All @@ -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;
}

Expand All @@ -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) {
Copy link
Collaborator

@SebaLukas SebaLukas Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is dummy != 0 here necessary?
It's relevant, I just read dummy_count twice...

*(current_octet - 1) |= EXI_BASETYPES_OCTET_SEQ_FLAG_MASK;
exi_unsigned->octets_count++;
*current_octet = (uint8_t)(dummy & EXI_BASETYPES_OCTET_SEQ_VALUE_MASK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#define EXI_STRING_MAX_LEN 64
#define EXI_BYTE_ARRAY_MAX_LEN 350

// To support EXI integer 8/7 coding, this needs to be 8/7 of the desired
// size of 25 for EXI representation
#define EXI_BASETYPES_MAX_OCTETS_SUPPORTED 29

#define EXI_BASETYPES_OCTET_SEQ_FLAG_MASK 0x80
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,13 @@ int exi_basetypes_decoder_signed(exi_bitstream_t* stream, exi_signed_t* value)
}
value->is_negative = (sign == 0) ? 0 : 1;

error = exi_basetypes_decoder_unsigned(stream, &value->data);
exi_unsigned_t raw_value;
error = exi_basetypes_decoder_unsigned(stream, &raw_value);
if (error != EXI_ERROR__NO_ERROR)
{
return error;
}
error = exi_basetypes_convert_bytes_from_unsigned(&raw_value, value->data.octets, &value->data.octets_count, sizeof(raw_value.octets));
return error;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,16 @@ int exi_basetypes_encoder_uint_64(exi_bitstream_t* stream, uint64_t value)

int exi_basetypes_encoder_unsigned(exi_bitstream_t* stream, const exi_unsigned_t* value)
{
return exi_basetypes_encoder_write_unsigned(stream, value);
int error;
exi_unsigned_t raw_exi_unsigned;

error = exi_basetypes_convert_bytes_from_unsigned(value, raw_exi_unsigned.octets, &raw_exi_unsigned.octets_count, sizeof(value->octets));
if (error != EXI_ERROR__NO_ERROR)
{
return error;
}

return exi_basetypes_encoder_write_unsigned(stream, &raw_exi_unsigned);
}

/*****************************************************************************
Expand Down