Commit 98b176f1 authored by Raphael Riebl's avatar Raphael Riebl

per_support: use intmax_t for integer constraints

long causes trouble on 32bit systems, see https://github.com/riebl/vanetza/issues/78
parent 92d2802c
......@@ -216,13 +216,13 @@ uper_put_nslength(asn_per_outp_t *po, size_t length) {
}
static int
per__long_range(long lb, long ub, unsigned long *range_r) {
unsigned long bounds_range;
per__long_range(intmax_t lb, intmax_t ub, uintmax_t *range_r) {
uintmax_t bounds_range;
if((ub < 0) == (lb < 0)) {
bounds_range = ub - lb;
} else if(lb < 0) {
assert(ub >= 0);
bounds_range = 1 + ((unsigned long)ub + (unsigned long)-(lb + 1));
bounds_range = 1 + ((uintmax_t)ub + (uintmax_t)-(lb + 1));
} else {
assert(!"Unreachable");
return -1;
......@@ -232,8 +232,8 @@ per__long_range(long lb, long ub, unsigned long *range_r) {
}
int
per_long_range_rebase(long v, long lb, long ub, unsigned long *output) {
unsigned long range;
per_long_range_rebase(intmax_t v, intmax_t lb, intmax_t ub, uintmax_t *output) {
uintmax_t range;
assert(lb <= ub);
......@@ -253,12 +253,12 @@ per_long_range_rebase(long v, long lb, long ub, unsigned long *output) {
*output = v-lb;
return 0;
} else if(v < 0) {
unsigned long rebased = 1 + (unsigned long)-(v+1) + (unsigned long)lb;
uintmax_t rebased = 1 + (uintmax_t)-(v+1) + (uintmax_t)lb;
assert(rebased <= range); /* By construction */
*output = rebased;
return 0;
} else if(lb < 0) {
unsigned long rebased = 1 + (unsigned long)-(lb+1) + (unsigned long)v;
uintmax_t rebased = 1 + (uintmax_t)-(lb+1) + (uintmax_t)v;
assert(rebased <= range); /* By construction */
*output = rebased;
return 0;
......@@ -269,8 +269,8 @@ per_long_range_rebase(long v, long lb, long ub, unsigned long *output) {
}
int
per_long_range_unrebase(unsigned long inp, long lb, long ub, long *outp) {
unsigned long range;
per_long_range_unrebase(uintmax_t inp, intmax_t lb, intmax_t ub, intmax_t *outp) {
uintmax_t range;
if(per__long_range(lb, ub, &range) != 0) {
return -1;
......@@ -285,10 +285,10 @@ per_long_range_unrebase(unsigned long inp, long lb, long ub, long *outp) {
return -1;
}
if(inp <= LONG_MAX) {
if(inp <= INTMAX_MAX) {
*outp = (long)inp + lb;
} else {
*outp = (lb + LONG_MAX + 1) + (long)((inp - LONG_MAX) - 1);
*outp = (lb + INTMAX_MAX + 1) + (intmax_t)((inp - INTMAX_MAX) - 1);
}
return 0;
......
......@@ -24,8 +24,8 @@ typedef struct asn_per_constraint_s {
} flags;
int range_bits; /* Full number of bits in the range */
int effective_bits; /* Effective bits */
long lower_bound; /* "lb" value */
long upper_bound; /* "ub" value */
intmax_t lower_bound; /* "lb" value */
intmax_t upper_bound; /* "ub" value */
} asn_per_constraint_t;
typedef struct asn_per_constraints_s {
asn_per_constraint_t value;
......@@ -81,9 +81,9 @@ typedef struct asn_bit_outp_s asn_per_outp_t;
* -1: Conversion failed due to range problems.
* 0: Conversion was successful.
*/
int per_long_range_rebase(long v, long lb, long ub, unsigned long *output);
int per_long_range_rebase(intmax_t v, intmax_t lb, intmax_t ub, uintmax_t *output);
/* The inverse operation: restores the value by the offset and its bounds. */
int per_long_range_unrebase(unsigned long inp, long lb, long ub, long *outp);
int per_long_range_unrebase(uintmax_t inp, intmax_t lb, intmax_t ub, intmax_t *outp);
/* X.691-2008/11, #11.5 */
int uper_put_constrained_whole_number_u(asn_per_outp_t *po, unsigned long v, int nbits);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment