simplify numeric constraints checking in runtime

This commit is contained in:
Lev Walkin 2017-08-02 10:37:31 -07:00
parent 5861e8a08d
commit 4118ccf997
6 changed files with 256 additions and 292 deletions

View File

@ -41,6 +41,8 @@ static int expr_break_recursion(arg_t *arg, asn1p_expr_t *expr);
static int expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr);
static int expr_elements_count(arg_t *arg, asn1p_expr_t *expr);
static int emit_single_member_OER_constraint(arg_t *arg, asn1cnst_range_t *range, char *type);
static int emit_single_member_OER_constraint_value(arg_t *arg, asn1cnst_range_t *range, char *type);
static int emit_single_member_OER_constraint_size(arg_t *arg, asn1cnst_range_t *range, char *type);
static int emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, int juscountvalues, char *type);
static int emit_member_OER_constraints(arg_t *arg, asn1p_expr_t *expr, const char *pfx);
static int emit_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr, const char *pfx);
@ -1954,17 +1956,117 @@ PER_FROM_alphabet_characters(asn1cnst_range_t *range) {
return numchars;
}
static void
emit_single_member_OER_constraint_comment(arg_t *arg, asn1cnst_range_t *range, char *type) {
/*
* Print some courtesy debug information.
*/
if(range
&& (range->left.type == ARE_VALUE || range->right.type == ARE_VALUE)) {
OUT("\t/* ");
if(type) OUT("(%s", type);
OUT("(");
if(range->left.type == ARE_VALUE)
OUT("%" PRIdASN, range->left.value);
else
OUT("MIN");
OUT("..");
if(range->right.type == ARE_VALUE)
OUT("%" PRIdASN, range->right.value);
else
OUT("MAX");
if(range->extensible) OUT(",...");
if(type) OUT(")");
OUT(") */");
}
}
static int
emit_single_member_OER_constraint_value(arg_t *arg, asn1cnst_range_t *range, char *type) {
if(!range) {
/* oer_support.h: asn_oer_constraint_s */
OUT("{ 0, 0 }");
return 0;
}
if(range->incompatible || range->not_OER_visible) {
OUT("{ 0, 0 }");
} else if(range->left.type == ARE_VALUE &&
range->right.type == ARE_VALUE) {
asn1c_integer_t lb = range->left.value;
asn1c_integer_t ub = range->right.value;
unsigned width = 0;
unsigned positive = 0;
if(lb >= 0) {
/* X.969 08/2015 10.2(a) */
if(ub <= 255) {
width = 1;
} else if(ub <= 65535) {
width = 2;
} else if(ub <= 4294967295UL) {
width = 4;
} else if(ub <= 18446744073709551615ULL) {
width = 8;
}
positive = 1;
} else {
positive = 0;
/* X.969 08/2015 10.2(b) - no lower bound or negative lower bound */
if(lb >= -128 && ub <= 127) {
width = 1;
} else if(lb >= -32768 && ub <= 32767) {
width = 2;
} else if(lb >= -2147483648L && ub <= 2147483647L) {
width = 4;
} else if(lb >= -9223372036854775808LL
&& ub <= 9223372036854775807LL) {
width = 8;
}
}
OUT("{ %u, %u }", width, positive);
} else {
OUT("{ 0, 0 }");
}
return 0;
}
static int
emit_single_member_OER_constraint_size(arg_t *arg, asn1cnst_range_t *range, char *type) {
if(!range) {
/* oer_support.h: asn_oer_constraint_s */
OUT("-1");
return 0;
}
if(range->incompatible || range->not_OER_visible) {
OUT("-1");
} else {
if(range->left.type == ARE_VALUE && range->right.type == ARE_VALUE
&& range->left.value == range->right.value
&& range->left.value >= 0) {
OUT("%" PRIdMAX "", range->left.value);
} else {
OUT("-1");
}
}
return 0;
}
static int
emit_single_member_OER_constraint(arg_t *arg, asn1cnst_range_t *range, char *type) {
if(!range) {
/* oer_support.h: asn_oer_constraint_s */
OUT("{ 0, 0, 0 }");
return 0;
OUT("{ 0, 0, 0 }");
return 0;
}
if(range->incompatible || range->not_OER_visible) {
OUT("{ 0, 0, 0 }");
} else {
if(range->incompatible || range->not_OER_visible) {
OUT("{ 0, 0, 0 }");
} else {
if(range->left.type == ARE_VALUE) {
if(range->right.type == ARE_VALUE) {
OUT("{ AOC_HAS_LOWER_BOUND | AOC_HAS_UPPER_BOUND, %" PRIdMAX
@ -1985,39 +2087,17 @@ emit_single_member_OER_constraint(arg_t *arg, asn1cnst_range_t *range, char *typ
}
}
/*
* Print some courtesy debug information.
*/
if(range->left.type == ARE_VALUE
|| range->right.type == ARE_VALUE) {
OUT("\t/* ");
if(type) OUT("(%s", type);
OUT("(");
if(range->left.type == ARE_VALUE)
OUT("%" PRIdASN, range->left.value);
else
OUT("MIN");
OUT("..");
if(range->right.type == ARE_VALUE)
OUT("%" PRIdASN, range->right.value);
else
OUT("MAX");
if(range->extensible) OUT(",...");
if(type) OUT(")");
OUT(") */");
}
return 0;
}
static int
emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, int alphabetsize, char *type) {
if(!range || range->incompatible || range->not_PER_visible) {
OUT("{ APC_UNCONSTRAINED,\t-1, -1, 0, 0 }");
if(!range || range->incompatible || range->not_PER_visible) {
OUT("{ APC_UNCONSTRAINED,\t-1, -1, 0, 0 }");
return 0;
}
}
if(range->left.type == ARE_VALUE) {
if(range->left.type == ARE_VALUE) {
if(range->right.type == ARE_VALUE) {
asn1c_integer_t cover = 1;
asn1c_integer_t r = 1 + range->right.value
@ -2151,22 +2231,26 @@ emit_member_OER_constraints(arg_t *arg, asn1p_expr_t *expr, const char *pfx) {
INDENT(+1);
/* .value{.width,.positive} */
range = asn1constraint_compute_OER_range(expr->Identifier, etype,
expr->combined_constraints,
ACT_EL_RANGE, 0, 0, 0);
if(emit_single_member_OER_constraint(arg, range, 0)) {
if(emit_single_member_OER_constraint_value(arg, range, 0)) {
return -1;
}
emit_single_member_OER_constraint_comment(arg, range, 0);
asn1constraint_range_free(range);
OUT(",\n");
/* .size */
range = asn1constraint_compute_OER_range(expr->Identifier, etype,
expr->combined_constraints,
ACT_CT_SIZE, 0, 0, 0);
if(emit_single_member_OER_constraint(arg, range, "SIZE")) {
if(emit_single_member_OER_constraint_size(arg, range, "SIZE")) {
return -1;
}
emit_single_member_OER_constraint_comment(arg, range, "SIZE");
asn1constraint_range_free(range);
INDENT(-1);

View File

@ -16,8 +16,8 @@ INTEGER_decode_oer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics;
asn_dec_rval_t rval = {RC_OK, 0};
INTEGER_t *st = (INTEGER_t *)*sptr;
asn_oer_constraint_t *ct;
size_t req_bytes = 0; /* 0 = length determinant is required */
struct asn_oer_constraint_number_s ct = {0, 0};
size_t req_bytes;
(void)opt_codec_ctx;
(void)specs;
@ -32,42 +32,33 @@ INTEGER_decode_oer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
st->size = 0;
if(!constraints) constraints = td->oer_constraints;
ct = constraints ? &constraints->value : 0;
if(constraints) ct = constraints->value;
if(ct && (ct->flags & AOC_HAS_LOWER_BOUND) && ct->lower_bound >= 0) {
if(ct.width) {
req_bytes = ct.width;
} else {
/* No lower bound and no upper bound, effectively */
ssize_t consumed = oer_fetch_length(ptr, size, &req_bytes);
if(consumed == 0) {
ASN__DECODE_STARVED;
} else if(consumed == -1) {
ASN__DECODE_FAILED;
}
rval.consumed += consumed;
ptr = (const char *)ptr + consumed;
size -= consumed;
}
if(req_bytes > size) {
ASN__DECODE_STARVED;
}
if(ct.positive) {
/* X.969 08/2015 10.2(a) */
unsigned msb; /* Most significant bit */
size_t useful_size;
intmax_t ub = ct->upper_bound;
if(ct->flags & AOC_HAS_UPPER_BOUND) {
if(ub <= 255) {
req_bytes = 1;
} else if(ub <= 65535) {
req_bytes = 2;
} else if(ub <= 4294967295UL) {
req_bytes = 4;
} else if(ub <= 18446744073709551615ULL) {
req_bytes = 8;
}
}
if(req_bytes == 0) { /* #8.6, using length determinant */
ssize_t consumed = oer_fetch_length(ptr, size, &req_bytes);
if(consumed == 0) {
ASN__DECODE_STARVED;
} else if(consumed == -1) {
ASN__DECODE_FAILED;
}
rval.consumed += consumed;
ptr = (const char *)ptr + consumed;
size -= consumed;
}
if(req_bytes > size) {
ASN__DECODE_STARVED;
}
/* Check most significant bit */
msb = *(const uint8_t *)ptr >> 7; /* yields 0 or 1 */
useful_size = msb + req_bytes;
@ -87,55 +78,20 @@ INTEGER_decode_oer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
rval.consumed += req_bytes;
return rval;
} else if(ct
&& ((ct->flags
& (AOC_HAS_LOWER_BOUND | AOC_HAS_UPPER_BOUND))
== (AOC_HAS_LOWER_BOUND | AOC_HAS_UPPER_BOUND))) {
/* X.969 08/2015 10.2(b) - no lower bound or negative lower bound */
intmax_t lb = ct->lower_bound;
intmax_t ub = ct->upper_bound;
if(lb >= -128 && ub <= 127) {
req_bytes = 1;
} else if(lb >= -32768 && ub <= 32767) {
req_bytes = 2;
} else if(lb >= -2147483648L && ub <= 2147483647L) {
req_bytes = 4;
} else if(lb >= -9223372036854775808LL && ub <= 9223372036854775807LL) {
req_bytes = 8;
}
}
/* No lower bound and no upper bound, effectively */
if(req_bytes == 0) { /* #8.6, using length determinant */
ssize_t consumed = oer_fetch_length(ptr, size, &req_bytes);
if(consumed == 0) {
ASN__DECODE_STARVED;
} else if(consumed == -1) {
} else {
/* X.969 08/2015 10.2(b) */
st->buf = (uint8_t *)MALLOC(req_bytes + 1);
if(!st->buf) {
ASN__DECODE_FAILED;
}
rval.consumed += consumed;
ptr = (const char *)ptr + consumed;
size -= consumed;
memcpy(st->buf, ptr, req_bytes);
st->buf[req_bytes] = '\0'; /* Just in case, 0-terminate */
st->size = req_bytes;
rval.consumed += req_bytes;
return rval;
}
if(req_bytes > size) {
ASN__DECODE_STARVED;
}
st->buf = (uint8_t *)MALLOC(req_bytes + 1);
if(!st->buf) {
ASN__DECODE_FAILED;
}
memcpy(st->buf, ptr, req_bytes);
st->buf[req_bytes] = '\0'; /* Just in case, 0-terminate */
st->size = req_bytes;
rval.consumed += req_bytes;
return rval;
}
/*
@ -147,31 +103,27 @@ INTEGER_encode_oer(asn_TYPE_descriptor_t *td,
asn_app_consume_bytes_f *cb, void *app_key) {
const INTEGER_t *st = sptr;
asn_enc_rval_t er;
asn_oer_constraint_t *ct;
struct asn_oer_constraint_number_s ct = {0, 0};
const uint8_t *buf;
const uint8_t *end;
size_t useful_bytes;
size_t req_bytes = 0;
int encode_as_unsigned;
int sign = 0;
if(!st || st->size == 0) ASN__ENCODE_FAILED;
if(!constraints) constraints = td->oer_constraints;
ct = constraints ? &constraints->value : 0;
if(constraints) ct = constraints->value;
er.encoded = 0;
buf = st->buf;
end = buf + st->size;
encode_as_unsigned =
ct && (ct->flags & AOC_HAS_LOWER_BOUND) && ct->lower_bound >= 0;
sign = (buf && buf < end) ? buf[0] & 0x80 : 0;
/* Ignore 9 leading zeroes or ones */
if(encode_as_unsigned) {
if(ct.positive) {
if(sign) {
/* The value given is a signed value. Can't proceed. */
ASN__ENCODE_FAILED;
@ -192,46 +144,18 @@ INTEGER_encode_oer(asn_TYPE_descriptor_t *td,
}
useful_bytes = end - buf;
if(encode_as_unsigned) {
intmax_t ub = ct->upper_bound;
if(ub <= 255) {
req_bytes = 1;
} else if(ub <= 65535) {
req_bytes = 2;
} else if(ub <= 4294967295UL) {
req_bytes = 4;
} else if(ub <= 18446744073709551615ULL) {
req_bytes = 8;
}
} else if(ct
&& ((ct->flags
& (AOC_HAS_LOWER_BOUND | AOC_HAS_UPPER_BOUND))
== (AOC_HAS_LOWER_BOUND | AOC_HAS_UPPER_BOUND))) {
/* X.969 08/2015 10.2(b) - no lower bound or negative lower bound */
intmax_t lb = ct->lower_bound;
intmax_t ub = ct->upper_bound;
if(lb >= -128 && ub <= 127) {
req_bytes = 1;
} else if(lb >= -32768 && ub <= 32767) {
req_bytes = 2;
} else if(lb >= -2147483648L && ub <= 2147483647L) {
req_bytes = 4;
} else if(lb >= -9223372036854775808LL && ub <= 9223372036854775807LL) {
req_bytes = 8;
}
}
if(req_bytes == 0) {
if(ct.width) {
req_bytes = ct.width;
} else {
ssize_t r = oer_serialize_length(useful_bytes, cb, app_key);
if(r < 0) {
ASN__ENCODE_FAILED;
}
er.encoded += r;
req_bytes = useful_bytes;
} else if(req_bytes < useful_bytes) {
}
if(req_bytes < useful_bytes) {
ASN__ENCODE_FAILED;
}

View File

@ -17,22 +17,16 @@ NativeInteger_decode_oer(asn_codec_ctx_t *opt_codec_ctx,
asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics;
asn_dec_rval_t rval = {RC_OK, 0};
long *native = (long *)*nint_ptr;
asn_oer_constraint_t *ct;
INTEGER_t tmpint;
INTEGER_t *tmpintptr = &tmpint;
memset(&tmpint, 0, sizeof(tmpint));
(void)opt_codec_ctx;
if(!native) {
native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
if(!native) ASN__DECODE_FAILED;
}
if(!constraints) constraints = td->oer_constraints;
ct = constraints ? &constraints->value : 0;
/*
* OPTIMIZATION: Encode directly rather than passing through INTEGER.
* Saves a memory allocation.

View File

@ -9,11 +9,6 @@
#include <OCTET_STRING.h>
#include <errno.h>
static asn_oer_constraints_t asn_DEF_OCTET_STRING_oer_constraints = {
{ 0, 0, 0 },
{ AOC_HAS_LOWER_BOUND, 0, 0 }
};
asn_dec_rval_t
OCTET_STRING_decode_oer(asn_codec_ctx_t *opt_codec_ctx,
asn_TYPE_descriptor_t *td,
@ -26,8 +21,7 @@ OCTET_STRING_decode_oer(asn_codec_ctx_t *opt_codec_ctx,
OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
asn_oer_constraints_t *cts =
constraints ? constraints : td->oer_constraints;
asn_oer_constraint_t *csiz =
cts ? &cts->size : &asn_DEF_OCTET_STRING_oer_constraints.size;
ssize_t ct_size = cts ? cts->size : -1;
asn_dec_rval_t rval = {RC_OK, 0};
size_t expected_length = 0;
@ -57,11 +51,8 @@ OCTET_STRING_decode_oer(asn_codec_ctx_t *opt_codec_ctx,
if(!st) ASN__DECODE_FAILED;
}
if((csiz->flags
& (AOC_HAS_LOWER_BOUND | AOC_HAS_UPPER_BOUND))
== (AOC_HAS_LOWER_BOUND | AOC_HAS_UPPER_BOUND)
&& csiz->lower_bound == csiz->upper_bound) {
expected_length = unit_bytes * csiz->lower_bound;
if(ct_size >= 0) {
expected_length = unit_bytes * ct_size;
} else {
/*
* X.696 (08/2015) #27.2
@ -120,18 +111,14 @@ OCTET_STRING_encode_oer(asn_TYPE_descriptor_t *td,
OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
asn_oer_constraints_t *cts =
constraints ? constraints : td->oer_constraints;
asn_oer_constraint_t *csiz =
cts ? &cts->size : &asn_DEF_OCTET_STRING_oer_constraints.size;
ssize_t ct_size = cts ? cts->size : -1;
asn_enc_rval_t er = {0, 0, 0};
if(!st) ASN__ENCODE_FAILED;
ASN_DEBUG("Encoding %s %d as OCTET STRING", td ? td->name : "", st->size);
if((csiz->flags
& (AOC_HAS_LOWER_BOUND | AOC_HAS_UPPER_BOUND))
== (AOC_HAS_LOWER_BOUND | AOC_HAS_UPPER_BOUND)
&& csiz->lower_bound == csiz->upper_bound) {
if(ct_size >= 0) {
/*
* Check that available data matches the constraint
*/
@ -154,11 +141,11 @@ OCTET_STRING_encode_oer(asn_TYPE_descriptor_t *td,
break;
}
if(st->size != unit_bytes * csiz->lower_bound) {
if(st->size != unit_bytes * ct_size) {
ASN_DEBUG(
"Trying to encode %s (%zu bytes) which doesn't fit SIZE "
"constraint (%d)",
td->name, st->size, csiz->lower_bound);
td->name, st->size, ct_size);
ASN__ENCODE_FAILED;
}
} else {

View File

@ -15,17 +15,13 @@ extern "C" {
/*
* Pre-computed OER constraints.
*/
typedef const struct asn_oer_constraint_s {
enum asn_oer_constraint_flags {
AOC_HAS_LOWER_BOUND = 0x01,
AOC_HAS_UPPER_BOUND = 0x02
} flags;
intmax_t lower_bound;
intmax_t upper_bound;
} asn_oer_constraint_t;
typedef const struct asn_oer_constraint_number_s {
unsigned width; /* ±8,4,2,1 fixed bytes */
unsigned positive; /* 1 for unsigned number, 0 for signed */
} asn_oer_constraint_number_t;
typedef const struct asn_oer_constraints_s {
struct asn_oer_constraint_s value;
struct asn_oer_constraint_s size;
asn_oer_constraint_number_t value;
ssize_t size; /* -1 (no constraint) or >= 0 */
} asn_oer_constraints_t;

View File

@ -9,53 +9,24 @@
#define CHECK_ENCODE_OK(a, b, c) check_encode(__LINE__, 0, a, b, c)
#define CHECK_ENCODE_BAD(a, b, c) check_encode(__LINE__, 1, a, b, c);
static const intmax_t NoBound = -4200024;
static asn_oer_constraints_t *
setup_constraints(int lineno, const char *process, intmax_t lower_bound,
intmax_t upper_bound) {
setup_constraints(int lineno, const char *process, unsigned width,
unsigned positive) {
static struct asn_oer_constraints_s empty_constraints;
struct asn_oer_constraints_s *constraints = &empty_constraints;
struct asn_oer_constraint_s *ct_value = &constraints->value;
struct asn_oer_constraint_number_s *ct_value = &constraints->value;
memset(&empty_constraints, 0, sizeof(empty_constraints));
/* Setup integer constraints as requested */
if(lower_bound == NoBound && upper_bound == NoBound) {
fprintf(stderr, "%d: OER %s without constraints\n", lineno,
process);
constraints = NULL;
} else {
if(lower_bound != NoBound) {
ct_value->flags |= AOC_HAS_LOWER_BOUND;
ct_value->lower_bound = lower_bound;
}
if(upper_bound != NoBound) {
ct_value->flags |= AOC_HAS_UPPER_BOUND;
ct_value->upper_bound = upper_bound;
}
if(lower_bound != NoBound && upper_bound != NoBound) {
fprintf(stderr,
"%d: OER %s constraints %" PRIdMAX
"..%" PRIdMAX "\n",
lineno, process, lower_bound, upper_bound);
} else if(lower_bound != NoBound) {
fprintf(stderr,
"%d: OER %s constraints %" PRIdMAX
"..MAX\n",
lineno, process, lower_bound);
} else if(upper_bound != NoBound) {
fprintf(stderr,
"%d: OER %s constraints MIN..%" PRIdMAX "\n",
lineno, process, upper_bound);
}
}
ct_value->width = width;
ct_value->positive = positive;
return constraints;
}
static void
check_decode(int lineno, enum asn_dec_rval_code_e code, intmax_t control, const char *buf, size_t size, const char *dummy, intmax_t lower_bound, intmax_t upper_bound) {
check_decode(int lineno, enum asn_dec_rval_code_e code, intmax_t control, const char *buf, size_t size, const char *dummy, unsigned width, unsigned positive) {
static char *code_s[] = { "RC_OK", "RC_WMORE", "RC_FAIL", "<error>" };
fprintf(stderr, "\n%d: OER decode (control %" PRIdMAX ")\n", lineno, control);
@ -63,7 +34,7 @@ check_decode(int lineno, enum asn_dec_rval_code_e code, intmax_t control, const
INTEGER_t *st = NULL;
asn_dec_rval_t ret;
asn_oer_constraints_t *constraints =
setup_constraints(lineno, "decoding", lower_bound, upper_bound);
setup_constraints(lineno, "decoding", width, positive);
fprintf(stderr, "%d: buf[%zu]={%d, %d, ...}\n", lineno, size,
((const uint8_t *)buf)[0],
@ -186,7 +157,7 @@ check_roundtrip(int lineno, intmax_t value, intmax_t lower_bound, intmax_t upper
}
static void
check_encode(int lineno, int bad, intmax_t value, intmax_t lower_bound, intmax_t upper_bound) {
check_encode(int lineno, int bad, intmax_t value, unsigned width, unsigned positive) {
uint8_t tmpbuf[32];
fprintf(stderr, "\n%d: OER encode value %" PRIdMAX "\n", lineno, value);
@ -194,7 +165,7 @@ check_encode(int lineno, int bad, intmax_t value, intmax_t lower_bound, intmax_t
INTEGER_t *stOut = (INTEGER_t *)calloc(1, sizeof(*stOut));
asn_enc_rval_t er;
asn_oer_constraints_t *constraints =
setup_constraints(lineno, "encoding", lower_bound, upper_bound);
setup_constraints(lineno, "encoding", width, positive);
if(asn_imax2INTEGER(stOut, value) == -1) {
assert(!"Unreachable imax2INTEGER failure");
@ -223,96 +194,104 @@ check_encode(int lineno, int bad, intmax_t value, intmax_t lower_bound, intmax_t
int
main() {
CHECK_DECODE(RC_WMORE, 0, "", 0, "bounds=", NoBound, NoBound);
CHECK_DECODE(RC_FAIL, 0, "\x00", 1, "bounds=", NoBound, NoBound);
CHECK_DECODE(RC_WMORE, 0, "", 0, "bounds=", 0, 0);
CHECK_DECODE(RC_WMORE, 0, "", 0, "bounds=", 0, 1);
CHECK_DECODE(RC_OK, 0, "\x00", 1, "bounds=", 0, 1);
CHECK_DECODE(RC_OK, 0, "\x00", 1, "bounds=", -1, 1);
CHECK_DECODE(RC_FAIL, 0, "\x00", 1, "bounds=", 0, 0);
CHECK_DECODE(RC_WMORE, 0, "", 0, "bounds=", 1, 0);
CHECK_DECODE(RC_WMORE, 0, "", 0, "bounds=", 1, 1);
CHECK_DECODE(RC_OK, 0, "\x00", 1, "bounds=", 1, 1);
CHECK_DECODE(RC_OK, 0, "\x00", 1, "bounds=", 1, 0);
CHECK_DECODE(RC_OK, 0, "\x00", 1, "bounds=", -1, 1);
CHECK_DECODE(RC_OK, 1, "\x01", 1, "bounds=", -1, 1);
CHECK_DECODE(RC_OK, -1, "\xff", 1, "bounds=", -1, 1);
CHECK_DECODE(RC_OK, -1, "\xff", 1, "bounds=", -1, 1);
CHECK_DECODE(RC_OK, 127, "\x7f", 1, "bounds=", 0, 127);
CHECK_DECODE(RC_OK, 255, "\xff", 1, "bounds=", 0, 127);
CHECK_DECODE(RC_OK, 255, "\xff", 1, "bounds=", 0, 255);
CHECK_DECODE(RC_WMORE, 0, "\xff", 1, "bounds=", 0, 256);
CHECK_DECODE(RC_OK, 65535, "\xff\xff", 2, "bounds=", 0, 256);
CHECK_DECODE(RC_OK, 0, "\x00", 1, "bounds=", 1, 0);
CHECK_DECODE(RC_OK, 1, "\x01", 1, "bounds=", 1, 0);
CHECK_DECODE(RC_OK, -1, "\xff", 1, "bounds=", 1, 0);
CHECK_DECODE(RC_OK, -1, "\xff", 1, "bounds=", 1, 0);
CHECK_DECODE(RC_OK, 127, "\x7f", 1, "bounds=", 1, 1);
CHECK_DECODE(RC_OK, 255, "\xff", 1, "bounds=", 1, 1);
CHECK_DECODE(RC_OK, 255, "\xff", 1, "bounds=", 1, 1);
CHECK_DECODE(RC_WMORE, 0, "\xff", 1, "bounds=", 2, 1);
CHECK_DECODE(RC_OK, 65535, "\xff\xff", 2, "bounds=", 2, 1);
CHECK_DECODE(RC_OK, 0, "\x01\x00", 2, "bounds=", NoBound, 1);
CHECK_DECODE(RC_OK, 1, "\x01\x01", 2, "bounds=", NoBound, 1);
CHECK_DECODE(RC_OK, -1, "\x1\xff", 2, "bounds=", NoBound, 1);
CHECK_DECODE(RC_OK, -1, "\x1\xff", 2, "bounds=", NoBound, 1);
CHECK_DECODE(RC_OK, -1, "\x1\xff", 2, "bounds=", NoBound, 255);
CHECK_DECODE(RC_WMORE, -1, "\x02\x00\xff", 2, "bounds=", NoBound, 200);
CHECK_DECODE(RC_OK, 255, "\x02\x00\xff", 3, "bounds=", NoBound, 200);
CHECK_DECODE(RC_OK, 0, "\x01\x00", 2, "bounds=", 0, 0);
CHECK_DECODE(RC_OK, 1, "\x01\x01", 2, "bounds=", 0, 0);
CHECK_DECODE(RC_OK, -1, "\x1\xff", 2, "bounds=", 0, 0);
CHECK_DECODE(RC_OK, -1, "\x1\xff", 2, "bounds=", 0, 0);
CHECK_DECODE(RC_OK, -1, "\x1\xff", 2, "bounds=", 0, 0);
CHECK_DECODE(RC_OK, 255, "\x1\xff", 2, "bounds=", 0, 1);
CHECK_DECODE(RC_WMORE, -1, "\x02\x00\xff", 2, "bounds=", 0, 0);
CHECK_DECODE(RC_OK, 255, "\x02\x00\xff", 3, "bounds=", 0, 0);
CHECK_ROUNDTRIP(0, NoBound, NoBound);
CHECK_ROUNDTRIP(1, NoBound, NoBound);
CHECK_ROUNDTRIP(-1, NoBound, NoBound);
CHECK_ROUNDTRIP(-65000, NoBound, NoBound);
CHECK_ROUNDTRIP(65000, NoBound, NoBound);
CHECK_ROUNDTRIP(65535, NoBound, NoBound);
CHECK_ROUNDTRIP(-65535, NoBound, NoBound);
CHECK_ROUNDTRIP(-65536, NoBound, NoBound);
CHECK_ROUNDTRIP(65536, NoBound, NoBound);
CHECK_ROUNDTRIP(0, -128, 127);
CHECK_ROUNDTRIP(1, -128, 127);
CHECK_ROUNDTRIP(-1, -128, 127);
CHECK_ROUNDTRIP(-127, -128, 127);
CHECK_ROUNDTRIP(-128, -128, 127);
CHECK_ROUNDTRIP(127, -128, 127);
CHECK_ROUNDTRIP(1, 32000, 32000); /* Sic! */
CHECK_ROUNDTRIP(32000, 0, 32000); /* Sic! */
CHECK_ROUNDTRIP(32000, -32000, 32000); /* Sic! */
CHECK_ROUNDTRIP(1, 65000, 65000); /* Sic! */
CHECK_ROUNDTRIP(65535, 0, 65000); /* Sic! */
CHECK_ROUNDTRIP(65535, -65000, 65000); /* Sic! */
CHECK_ROUNDTRIP(0, 0, 0);
CHECK_ROUNDTRIP(1, 0, 0);
CHECK_ROUNDTRIP(-1, 0, 0);
CHECK_ROUNDTRIP(-65000, 0, 0);
CHECK_ROUNDTRIP(65000, 0, 0);
CHECK_ROUNDTRIP(65535, 0, 0);
CHECK_ROUNDTRIP(-65535, 0, 0);
CHECK_ROUNDTRIP(-65536, 0, 0);
CHECK_ROUNDTRIP(65536, 0, 0);
CHECK_ROUNDTRIP(0, 1, 0);
CHECK_ROUNDTRIP(1, 1, 0);
CHECK_ROUNDTRIP(-1, 1, 0);
CHECK_ROUNDTRIP(-127, 1, 0);
CHECK_ROUNDTRIP(-128, 1, 0);
CHECK_ROUNDTRIP(127, 1, 0);
CHECK_ROUNDTRIP(1, 2, 1);
CHECK_ROUNDTRIP(32000, 2, 1);
CHECK_ROUNDTRIP(32000, 2, 0);
CHECK_ROUNDTRIP(1, 2, 1);
CHECK_ROUNDTRIP(65535, 2, 1);
CHECK_ROUNDTRIP(65535, 4, 0);
CHECK_ENCODE_OK(0, 0, 255);
CHECK_ENCODE_OK(255, 0, 255);
CHECK_ENCODE_BAD(256, 0, 255);
CHECK_ENCODE_OK(0, -128, 127);
CHECK_ENCODE_OK(127, -128, 127);
CHECK_ENCODE_OK(-128, -128, 127);
CHECK_ENCODE_BAD(-129, -128, 127);
CHECK_ENCODE_BAD(128, -128, 127);
CHECK_ENCODE_OK(-4900000000, -5000000000, 5000000000);
CHECK_ENCODE_OK(4900000000, -5000000000, 5000000000);
CHECK_ENCODE_OK(-2000000000, -4000000000, 0);
CHECK_ENCODE_OK(-4000000000, -4000000000, 0);
CHECK_ENCODE_BAD(-4900000000, 0, 4000000000);
CHECK_ENCODE_OK(0, 1, 1);
CHECK_ENCODE_OK(255, 1, 1);
CHECK_ENCODE_BAD(256, 1, 1);
CHECK_ENCODE_OK(0, 1, 0);
CHECK_ENCODE_OK(127, 1, 0);
CHECK_ENCODE_OK(-128, 1, 0);
CHECK_ENCODE_BAD(-129, 1, 0);
CHECK_ENCODE_BAD(128, 1, 0);
CHECK_ENCODE_OK(-4900000000, 8, 0);
CHECK_ENCODE_OK(4900000000, 8, 0);
CHECK_ENCODE_OK(-2000000000, 8, 0);
CHECK_ENCODE_OK(-4000000000, 8, 0);
CHECK_ENCODE_BAD(-4900000000, 4, 1);
CHECK_ENCODE_BAD(-1, 0, 4000000000);
CHECK_ENCODE_BAD(4900000000, 0, 4000000000);
CHECK_ENCODE_OK(4100000000, 0, 4000000000); /* Sic! */
CHECK_ENCODE_BAD(4900000000, 4, 1);
CHECK_ENCODE_OK(4100000000, 4, 1);
for(size_t i = 0; i < 7 ; i++) {
intmax_t value = (intmax_t)1 << i;
CHECK_ROUNDTRIP(value, 0, 255);
CHECK_ROUNDTRIP(value, 1, 1);
value = -value;
CHECK_ROUNDTRIP(value, -128, 127);
CHECK_ROUNDTRIP(value, 1, 0);
}
for(size_t i = 0; i < 16 ; i++) {
intmax_t value = (intmax_t)1 << i;
CHECK_ROUNDTRIP(value, 0, 65535);
CHECK_ROUNDTRIP(value, 2, 1);
value = -value;
CHECK_ROUNDTRIP(value, -32768, 32767);
CHECK_ROUNDTRIP(value, 2, 0);
}
for(size_t i = 0; i < 32 ; i++) {
intmax_t value = (intmax_t)1 << i;
CHECK_ROUNDTRIP(value, 0, 4294967296UL);
CHECK_ROUNDTRIP(value, 4, 1);
value = -value;
CHECK_ROUNDTRIP(value, -2147483648L, 2147483647L);
CHECK_ROUNDTRIP(value, 4, 0);
}
for(size_t i = 0; i < 8 * sizeof(intmax_t) ; i++) {
intmax_t value = (intmax_t)1 << i;
CHECK_ROUNDTRIP(value, NoBound, NoBound);
CHECK_ROUNDTRIP(value, 8, 0);
value = -value;
CHECK_ROUNDTRIP(value, NoBound, NoBound);
CHECK_ROUNDTRIP(value, 8, 0);
}
for(size_t i = 0; i < 8 * sizeof(intmax_t) ; i++) {
intmax_t value = (intmax_t)1 << i;
CHECK_ROUNDTRIP(value, 0, 0);
value = -value;
CHECK_ROUNDTRIP(value, 0, 0);
}
}