Skip to content

Commit

Permalink
aper: Rework aper_get_length to gain lb & ub information
Browse files Browse the repository at this point in the history
This should help aper_put_length() to take proper decisions on the way
to encode the length, since the range alone is not enough.
A contraint of lb=1 ub=65536 would yield a range=65536, but according to
ITU-T X.691 11.9 it shouldn't be encoded using nsnnwn since that only
applies in case ub<65536.
As a result, it would end up encoding/decoding it using 2 bytes while it
should use only 1.

Related: #94
  • Loading branch information
pespin authored and mouse07410 committed Jul 15, 2022
1 parent 9f19823 commit 24247e2
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion skeletons/ANY_aper.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ ANY_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
int ret;

/* Get the PER length */
raw_len = aper_get_length(pd, -1, 0, &repeat);
raw_len = aper_get_length(pd, -1, -1, 0, &repeat);
if(raw_len < 0) RETURN(RC_WMORE);
if(raw_len == 0 && st->buf) break;

Expand Down
2 changes: 1 addition & 1 deletion skeletons/INTEGER_aper.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ INTEGER_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
int ret;

/* Get the PER length */
len = aper_get_length(pd, -1, -1, &repeat);
len = aper_get_length(pd, -1, -1, -1, &repeat);
if(len < 0) ASN__DECODE_STARVED;

p = REALLOC(st->buf, st->size + len + 1);
Expand Down
5 changes: 3 additions & 2 deletions skeletons/OCTET_STRING_aper.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,10 @@ OCTET_STRING_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
/* Get the PER length */
if (csiz->upper_bound - csiz->lower_bound == 0)
/* Indefinite length case */
raw_len = aper_get_length(pd, -1, csiz->effective_bits, &repeat);
raw_len = aper_get_length(pd, -1, -1, csiz->effective_bits, &repeat);
else
raw_len = aper_get_length(pd, csiz->upper_bound - csiz->lower_bound + 1, csiz->effective_bits, &repeat);
raw_len = aper_get_length(pd, csiz->lower_bound, csiz->upper_bound,
csiz->effective_bits, &repeat);
if(raw_len < 0) RETURN(RC_WMORE);
raw_len += csiz->lower_bound;

Expand Down
3 changes: 1 addition & 2 deletions skeletons/OPEN_TYPE_aper.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ OPEN_TYPE_aper_unknown_type_discard_bytes (asn_per_data_t *pd) {
rv.code = RC_FAIL;

do {
bytes = aper_get_length(pd, -1, -1, &repeat);
bytes = aper_get_length(pd, -1, -1, -1, &repeat);
if (bytes > 10 * ASN_DUMMY_BYTES)
{
return rv;
Expand All @@ -171,4 +171,3 @@ OPEN_TYPE_aper_unknown_type_discard_bytes (asn_per_data_t *pd) {
return rv;
#undef ASN_DUMMY_BYTES
}

2 changes: 1 addition & 1 deletion skeletons/aper_opentype.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ aper_open_type_get_simple(const asn_codec_ctx_t *ctx,
ASN_DEBUG("Getting open type %s...", td->name);

do {
chunk_bytes = aper_get_length(pd, -1, -1, &repeat);
chunk_bytes = aper_get_length(pd, -1, -1, -1, &repeat);
if(chunk_bytes < 0) {
FREEMEM(buf);
ASN__DECODE_STARVED;
Expand Down
10 changes: 7 additions & 3 deletions skeletons/aper_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ aper_get_align(asn_per_data_t *pd) {
}

ssize_t
aper_get_length(asn_per_data_t *pd, int range, int ebits, int *repeat) {
aper_get_length(asn_per_data_t *pd, ssize_t lb, ssize_t ub,
int ebits, int *repeat) {
int constrained = (lb >= 0) && (ub >= 0);
ssize_t value;

*repeat = 0;

if (range <= 65536 && range >= 0)
if (constrained && ub < 65536) {
int range = ub - lb + 1;
return aper_get_nsnnwn(pd, range);
}

if (aper_get_align(pd) < 0)
return -1;
Expand Down Expand Up @@ -59,7 +63,7 @@ aper_get_nslength(asn_per_data_t *pd) {
return length;
} else {
int repeat;
length = aper_get_length(pd, -1, -1, &repeat);
length = aper_get_length(pd, -1, -1, -1, &repeat);
if(length >= 0 && !repeat) return length;
return -1; /* Error, or do not support >16K extensions */
}
Expand Down
2 changes: 1 addition & 1 deletion skeletons/aper_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern "C" {
* X.691 (08/2015) #11.9 "General rules for encoding a length determinant"
* Get the length "n" from the Aligned PER stream.
*/
ssize_t aper_get_length(asn_per_data_t *pd, int range,
ssize_t aper_get_length(asn_per_data_t *pd, ssize_t lb, ssize_t ub,
int effective_bound_bits, int *repeat);

/*
Expand Down
7 changes: 5 additions & 2 deletions skeletons/constr_SET_OF_aper.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,11 @@ SET_OF_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
do {
int i;
if(nelems < 0) {
nelems = aper_get_length(pd, ct ? ct->upper_bound - ct->lower_bound + 1 : -1,
ct ? ct->effective_bits : -1, &repeat);
if (ct)
nelems = aper_get_length(pd, ct->lower_bound, ct->upper_bound,
ct->effective_bits, &repeat);
else
nelems = aper_get_length(pd, -1, -1, -1, &repeat);
ASN_DEBUG("Got to decode %d elements (eff %d)",
(int)nelems, (int)(ct ? ct->effective_bits : -1));
if(nelems < 0) ASN__DECODE_STARVED;
Expand Down
2 changes: 1 addition & 1 deletion tests/tests-skeletons/check-APER-support.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static size_t get(asn_per_outp_t *po, ssize_t lb, ssize_t ub) {
size_t length = 0;
int repeat = 0;
do {
ssize_t n = aper_get_length(&data, ub - lb + 1, -1, &repeat);
ssize_t n = aper_get_length(&data, lb, ub, -1, &repeat);
fprintf(stderr, " get = %zu +%zd\n", length, n);
assert(n >= 0);
length += n;
Expand Down

0 comments on commit 24247e2

Please sign in to comment.