Commit 4118ccf9 authored by Lev Walkin's avatar Lev Walkin

simplify numeric constraints checking in runtime

parent 5861e8a0
......@@ -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(arg_t *arg, asn1cnst_range_t *range, char *type) {
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, 0 }");
return 0;
OUT("{ 0, 0 }");
return 0;
}
if(range->incompatible || range->not_OER_visible) {
OUT("{ 0, 0, 0 }");
} else {
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;
}
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);
......
......@@ -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,41 +32,32 @@ 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) {
/* X.969 08/2015 10.2(a) */
unsigned msb; /* Most significant bit */
size_t useful_size;
if(ct.width) {
req_bytes = ct.width;
} else {
/* No lower bound and no upper bound, effectively */
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;
}
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 == 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;
}
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;
/* Check most significant bit */
msb = *(const uint8_t *)ptr >> 7; /* yields 0 or 1 */
......@@ -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;
}
if(req_bytes > size) {
ASN__DECODE_STARVED;
}
memcpy(st->buf, ptr, req_bytes);
st->buf[req_bytes] = '\0'; /* Just in case, 0-terminate */
st->size = req_bytes;
st->buf = (uint8_t *)MALLOC(req_bytes + 1);
if(!st->buf) {
ASN__DECODE_FAILED;
rval.consumed += req_bytes;
return rval;
}
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;
}
......
......@@ -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.
......
......@@ -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 {
......
......@@ -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;
......
This diff is collapsed.
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