summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/INTEGER.c27
1 files changed, 22 insertions, 5 deletions
diff --git a/src/INTEGER.c b/src/INTEGER.c
index 9ba14bc..eef89f5 100644
--- a/src/INTEGER.c
+++ b/src/INTEGER.c
@@ -995,18 +995,35 @@ INTEGER_encode_aper(asn_TYPE_descriptor_t *td,
} else {
/* TODO: extend to >64 bits */
int64_t v64 = v;
- int i;
+ int i, j;
+ int max_range_bytes = (ct->range_bits >> 3) +
+ (((ct->range_bits % 8) > 0) ? 1 : 0);
+
+ for (i = 1; ; i++) {
+ int upper = 1 << i;
+ if (upper >= max_range_bytes)
+ break;
+ }
+
+ for (j = sizeof(int64_t) -1; j != 0; j--) {
+ uint8_t val;
+ val = v64 >> (j * 8);
+ if (val != 0)
+ break;
+ }
- /* Putting length - 1 in the minimum number of bits ex: 5 = 3bits */
- if (per_put_few_bits(po, st->size - 1, (ct->range_bits >> 3)-1))
+ /* Putting length in the minimum number of bits ex: 5 = 3bits */
+ if (per_put_few_bits(po, j, i))
_ASN_ENCODE_FAILED;
// Consume the bits to align on octet
if (aper_put_align(po) < 0)
_ASN_ENCODE_FAILED;
+
/* Put the value */
- for (i = 0; i < st->size; i++) {
- if(per_put_few_bits(po, (v64 >> (8 * (st->size - i - 1))) & 0xff, 8)) _ASN_ENCODE_FAILED;
+ for (i = 0; i <= j; i++) {
+ if(per_put_few_bits(po, (v64 >> (8 * (j - i))) & 0xff, 8))
+ _ASN_ENCODE_FAILED;
}
}
_ASN_ENCODED_OK(er);