From 2b3068fb85a07fbf3de8ba845194f800bb2107f5 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 19 Dec 2015 13:32:58 +0100 Subject: APER: Fix encoding of INTEGER with lower_bound != 0 When encoding an INTEGER, we need to subtract the lower bound before encoding the value. This is specified in Clause 10.5.7.x of X.691. The decoder already does this correct, but the encoder was wrong. --- skeletons/INTEGER.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/skeletons/INTEGER.c b/skeletons/INTEGER.c index 6ca67d4b..aa028585 100644 --- a/skeletons/INTEGER.c +++ b/skeletons/INTEGER.c @@ -973,28 +973,29 @@ INTEGER_encode_aper(asn_TYPE_descriptor_t *td, /* X.691, #12.2.2 */ if(ct && ct->range_bits >= 0) { /* #10.5.6 */ - ASN_DEBUG("Encoding integer with range %d bits", - ct->range_bits); + ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits", + value, value - ct->lower_bound, ct->range_bits); + unsigned long v = value - ct->lower_bound; /* #12 <= 8 -> alignment ? */ if (ct->range_bits < 8) { - if(per_put_few_bits(po, 0x00 | value, ct->range_bits)) + if(per_put_few_bits(po, 0x00 | v, ct->range_bits)) _ASN_ENCODE_FAILED; } else if (ct->range_bits == 8) { if(aper_put_align(po) < 0) _ASN_ENCODE_FAILED; - if(per_put_few_bits(po, 0x00 | value, ct->range_bits)) + if(per_put_few_bits(po, 0x00 | v, ct->range_bits)) _ASN_ENCODE_FAILED; } else if (ct->range_bits <= 16) { // Consume the bytes to align on octet if(aper_put_align(po) < 0) _ASN_ENCODE_FAILED; - if(per_put_few_bits(po, 0x0000 | value, + if(per_put_few_bits(po, 0x0000 | v, 16)) _ASN_ENCODE_FAILED; } else { /* TODO: extend to >64 bits */ - int64_t v = value; + int64_t v64 = v; int i; /* Putting length - 1 in the minimum number of bits ex: 5 = 3bits */ @@ -1006,7 +1007,7 @@ INTEGER_encode_aper(asn_TYPE_descriptor_t *td, _ASN_ENCODE_FAILED; /* Put the value */ for (i = 0; i < st->size; i++) { - if(per_put_few_bits(po, (v >> (8 * (st->size - i - 1))) & 0xff, 8)) _ASN_ENCODE_FAILED; + if(per_put_few_bits(po, (v64 >> (8 * (st->size - i - 1))) & 0xff, 8)) _ASN_ENCODE_FAILED; } } _ASN_ENCODED_OK(er); -- cgit v1.2.3