aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2015-12-19 13:37:02 +0100
committerHarald Welte <laforge@gnumonks.org>2015-12-19 13:37:02 +0100
commit667d758c92825208e318ec27e090aac2805d4678 (patch)
tree6cd68901518feb0a996d5d1b52b82feb45a49d66
parenta37b06d7354c5cfec0e88dc22a8a5d784551f1b3 (diff)
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.
-rw-r--r--src/INTEGER.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/src/INTEGER.c b/src/INTEGER.c
index c74c5b4..9ba14bc 100644
--- a/src/INTEGER.c
+++ b/src/INTEGER.c
@@ -972,28 +972,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 */
@@ -1005,7 +1006,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);