Skip to content

Commit d5ddbc4

Browse files
author
Siebren Weertman
committed
add conversion for byte to/from unsigned (bigint) datatype
Signed-off-by: Siebren Weertman <[email protected]>
1 parent 5d23b71 commit d5ddbc4

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/input/code_templates/c/static_code/exi_basetypes.c.jinja

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,65 @@ int exi_basetypes_convert_64_from_signed(exi_signed_t* exi_signed, int64_t* valu
133133
*value = (exi_signed->is_negative == 0) ? uValue : -uValue;
134134
return res;
135135
}
136+
137+
int exi_basetypes_convert_bytes_from_unsigned(const exi_unsigned_t* exi_unsigned, uint8_t* data, size_t* data_len, size_t data_size)
138+
{
139+
const uint8_t* current_octet = exi_unsigned->octets;
140+
uint16_t temp = 0;
141+
*data_len = 0;
142+
size_t total_offset = 0;
143+
144+
for (size_t n = 0; n < exi_unsigned->octets_count; n++) {
145+
temp = temp + ((uint16_t)(*current_octet & EXI_BASETYPES_OCTET_SEQ_VALUE_MASK) << (total_offset));
146+
total_offset += 7;
147+
if (total_offset >= 8) {
148+
if (data_size == *data_len) {
149+
return -1;
150+
}
151+
total_offset -= 8;
152+
data[(*data_len)++] = temp & 0xFF;
153+
temp >>= 8;
154+
}
155+
current_octet++;
156+
}
157+
if (total_offset != 0) {
158+
if (data_size == *data_len) {
159+
return -1;
160+
}
161+
data[(*data_len)++] = temp & 0xFF;
162+
}
163+
return 0;
164+
}
165+
166+
int exi_basetypes_convert_bytes_to_unsigned(exi_unsigned_t* exi_unsigned, const uint8_t* data, size_t data_len)
167+
{
168+
uint8_t *current_octet = &exi_unsigned->octets[0];
169+
uint16_t dummy;
170+
uint8_t dummy_count = 0;
171+
172+
for (size_t n = 0; n != data_len; n++, current_octet++) {
173+
if (dummy_count <= 8) {
174+
dummy = dummy | ((data[n]) << dummy_count);
175+
dummy_count += 8;
176+
}
177+
fmt::print("dummy {}\n", dummy);
178+
exi_unsigned->octets_count++;
179+
*current_octet = (uint8_t)(dummy & EXI_BASETYPES_OCTET_SEQ_VALUE_MASK);
180+
181+
dummy = dummy >> 7u;
182+
dummy_count -= 7;
183+
if (n == data_len) {
184+
break;
185+
}
186+
187+
*current_octet |= EXI_BASETYPES_OCTET_SEQ_FLAG_MASK;
188+
}
189+
if (dummy_count > 0) {
190+
*(current_octet-1) |= EXI_BASETYPES_OCTET_SEQ_FLAG_MASK;
191+
exi_unsigned->octets_count++;
192+
*current_octet = (uint8_t)(dummy & EXI_BASETYPES_OCTET_SEQ_VALUE_MASK);
193+
}
194+
return 0;
195+
}
196+
136197
{% endblock %}

src/input/code_templates/c/static_code/exi_basetypes.h.jinja

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,7 @@ int exi_basetypes_convert_64_to_signed(exi_signed_t* exi_signed, int64_t value);
5959

6060
int exi_basetypes_convert_from_signed(exi_signed_t* exi_unsigned, int32_t* value, size_t max_octets);
6161
int exi_basetypes_convert_64_from_signed(exi_signed_t* exi_unsigned, int64_t* value);
62+
63+
int exi_basetypes_convert_bytes_from_unsigned(const exi_unsigned_t* exi_unsigned, uint8_t* data, size_t* data_len, size_t data_size);
64+
int exi_basetypes_convert_bytes_to_unsigned(exi_unsigned_t* exi_unsigned, const uint8_t* data, size_t data_len);
6265
{% endblock %}

0 commit comments

Comments
 (0)