Skip to content

Commit

Permalink
add conversion for byte to/from unsigned (bigint) datatype
Browse files Browse the repository at this point in the history
Signed-off-by: Siebren Weertman <[email protected]>

const correctness and remove log

Signed-off-by: Siebren Weertman <[email protected]>
  • Loading branch information
Siebren Weertman committed Jan 31, 2024
1 parent 5d23b71 commit 272dee6
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 10 deletions.
72 changes: 66 additions & 6 deletions src/input/code_templates/c/static_code/exi_basetypes.c.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ int exi_basetypes_convert_64_to_signed(exi_signed_t* exi_signed, int64_t value)
return exi_basetypes_convert_64_to_unsigned(&exi_signed->data, value);
}

int exi_basetypes_convert_from_unsigned(exi_unsigned_t* exi_unsigned, uint32_t* value, size_t max_octets)
int exi_basetypes_convert_from_unsigned(const exi_unsigned_t* exi_unsigned, uint32_t* value, size_t max_octets)
{
if (exi_unsigned->octets_count > max_octets)
{
return EXI_ERROR__OCTET_COUNT_LARGER_THAN_TYPE_SUPPORTS;
}

uint8_t* current_octet = exi_unsigned->octets;
const uint8_t* current_octet = exi_unsigned->octets;
*value = 0;

for (size_t n = 0; n < exi_unsigned->octets_count; n++)
Expand All @@ -98,14 +98,14 @@ int exi_basetypes_convert_from_unsigned(exi_unsigned_t* exi_unsigned, uint32_t*
return EXI_ERROR__NO_ERROR;
}

int exi_basetypes_convert_64_from_unsigned(exi_unsigned_t* exi_unsigned, uint64_t* value)
int exi_basetypes_convert_64_from_unsigned(const exi_unsigned_t* exi_unsigned, uint64_t* value)
{
if (exi_unsigned->octets_count > EXI_BASETYPES_UINT64_MAX_OCTETS)
{
return EXI_ERROR__OCTET_COUNT_LARGER_THAN_TYPE_SUPPORTS;
}

uint8_t* current_octet = exi_unsigned->octets;
const uint8_t* current_octet = exi_unsigned->octets;
*value = 0;

for (size_t n = 0; n < exi_unsigned->octets_count; n++)
Expand All @@ -118,19 +118,79 @@ int exi_basetypes_convert_64_from_unsigned(exi_unsigned_t* exi_unsigned, uint64_
return EXI_ERROR__NO_ERROR;
}

int exi_basetypes_convert_from_signed(exi_signed_t* exi_signed, int32_t* value, size_t max_octets)
int exi_basetypes_convert_from_signed(const exi_signed_t* exi_signed, int32_t* value, size_t max_octets)
{
uint32_t uValue = 0;
int res = exi_basetypes_convert_from_unsigned(&exi_signed->data, &uValue, max_octets);
*value = (exi_signed->is_negative == 0) ? uValue : -uValue;
return res;
}

int exi_basetypes_convert_64_from_signed(exi_signed_t* exi_signed, int64_t* value)
int exi_basetypes_convert_64_from_signed(const exi_signed_t* exi_signed, int64_t* value)
{
uint64_t uValue = 0;
int res = exi_basetypes_convert_64_from_unsigned(&exi_signed->data, &uValue);
*value = (exi_signed->is_negative == 0) ? uValue : -uValue;
return res;
}

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;
uint16_t temp = 0;
*data_len = 0;
size_t total_offset = 0;

for (size_t n = 0; n < exi_unsigned->octets_count; n++) {
temp = 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) {
return -1;
}
total_offset -= 8;
data[(*data_len)++] = temp & 0xFF;
temp >>= 8;
}
current_octet++;
}
if (total_offset != 0) {
if (data_size == *data_len) {
return -1;
}
data[(*data_len)++] = temp & 0xFF;
}
return 0;
}

int exi_basetypes_convert_bytes_to_unsigned(exi_unsigned_t* exi_unsigned, const uint8_t* data, size_t data_len)
{
uint8_t *current_octet = &exi_unsigned->octets[0];
uint16_t dummy;
uint8_t dummy_count = 0;

for (size_t n = 0; n != data_len; n++, current_octet++) {
if (dummy_count <= 8) {
dummy = dummy | ((data[n]) << dummy_count);
dummy_count += 8;
}
exi_unsigned->octets_count++;
*current_octet = (uint8_t)(dummy & EXI_BASETYPES_OCTET_SEQ_VALUE_MASK);

dummy = 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;
exi_unsigned->octets_count++;
*current_octet = (uint8_t)(dummy & EXI_BASETYPES_OCTET_SEQ_VALUE_MASK);
}
return 0;
}

{% endblock %}
11 changes: 7 additions & 4 deletions src/input/code_templates/c/static_code/exi_basetypes.h.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ typedef char exi_character_t;
int exi_basetypes_convert_to_unsigned(exi_unsigned_t* exi_unsigned, uint32_t value, size_t max_octets);
int exi_basetypes_convert_64_to_unsigned(exi_unsigned_t* exi_unsigned, uint64_t value);

int exi_basetypes_convert_from_unsigned(exi_unsigned_t* exi_unsigned, uint32_t* value, size_t max_octets);
int exi_basetypes_convert_64_from_unsigned(exi_unsigned_t* exi_unsigned, uint64_t* value);
int exi_basetypes_convert_from_unsigned(const exi_unsigned_t* exi_unsigned, uint32_t* value, size_t max_octets);
int exi_basetypes_convert_64_from_unsigned(const exi_unsigned_t* exi_unsigned, uint64_t* value);

int exi_basetypes_convert_to_signed(exi_signed_t* exi_signed, int32_t value, size_t max_octets);
int exi_basetypes_convert_64_to_signed(exi_signed_t* exi_signed, int64_t value);

int exi_basetypes_convert_from_signed(exi_signed_t* exi_unsigned, int32_t* value, size_t max_octets);
int exi_basetypes_convert_64_from_signed(exi_signed_t* exi_unsigned, int64_t* value);
int exi_basetypes_convert_from_signed(const exi_signed_t* exi_unsigned, int32_t* value, size_t max_octets);
int exi_basetypes_convert_64_from_signed(const exi_signed_t* exi_unsigned, int64_t* value);

int exi_basetypes_convert_bytes_from_unsigned(const exi_unsigned_t* exi_unsigned, uint8_t* data, size_t* data_len, size_t data_size);
int exi_basetypes_convert_bytes_to_unsigned(exi_unsigned_t* exi_unsigned, const uint8_t* data, size_t data_len);
{% endblock %}

0 comments on commit 272dee6

Please sign in to comment.