From 3c846a7f36f46769b7bee9529c090625bd77574d Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Mon, 18 Jul 2022 11:45:34 +0200 Subject: [PATCH] sbcap: Update asn1c skeleton files Update skeleton files using newest asn1c with APER support [1], commit 24247e2813a7510ebabe6a9b6b6b29fffa0eb27b. This contains some APER decoding and encoding fixes for length determinants. See [2] for more information. [1] https://github.com/mouse07410/asn1c/tree/vlm_master/ [2] https://github.com/mouse07410/asn1c/issues/94 Change-Id: I581fc53b124a443e150508811df4cca4593038c4 --- src/sbcap/gen/ANY_aper.c | 6 +- src/sbcap/gen/INTEGER_aper.c | 7 +- src/sbcap/gen/OCTET_STRING_aper.c | 16 +-- src/sbcap/gen/OPEN_TYPE_aper.c | 3 +- src/sbcap/gen/aper_opentype.c | 6 +- src/sbcap/gen/aper_support.c | 160 +++++++++--------------- src/sbcap/gen/constr_SEQUENCE_OF_aper.c | 6 +- src/sbcap/gen/constr_SET_OF_aper.c | 13 +- src/sbcap/skel/aper_support.h | 11 +- 9 files changed, 94 insertions(+), 134 deletions(-) diff --git a/src/sbcap/gen/ANY_aper.c b/src/sbcap/gen/ANY_aper.c index 917e5ef..91d7277 100644 --- a/src/sbcap/gen/ANY_aper.c +++ b/src/sbcap/gen/ANY_aper.c @@ -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; @@ -173,7 +173,7 @@ ANY_encode_aper(const asn_TYPE_descriptor_t *td, size = st->size; do { int need_eom = 0; - ssize_t may_save = aper_put_length(po, -1, size, &need_eom); + ssize_t may_save = aper_put_length(po, -1, -1, size, &need_eom); if(may_save < 0) ASN__ENCODE_FAILED; ret = per_put_many_bits(po, buf, may_save * 8); @@ -182,7 +182,7 @@ ANY_encode_aper(const asn_TYPE_descriptor_t *td, buf += may_save; size -= may_save; assert(!(may_save & 0x07) || !size); - if(need_eom && aper_put_length(po, -1, 0, 0)) + if(need_eom && aper_put_length(po, -1, -1, 0, NULL)) ASN__ENCODE_FAILED; /* End of Message length */ } while(size); diff --git a/src/sbcap/gen/INTEGER_aper.c b/src/sbcap/gen/INTEGER_aper.c index 2ff9db0..bd19de1 100644 --- a/src/sbcap/gen/INTEGER_aper.c +++ b/src/sbcap/gen/INTEGER_aper.c @@ -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); @@ -291,13 +291,14 @@ INTEGER_encode_aper(const asn_TYPE_descriptor_t *td, for(buf = st->buf, end = st->buf + st->size; buf < end;) { int need_eom = 0; - ssize_t mayEncode = aper_put_length(po, -1, end - buf, &need_eom); + ssize_t mayEncode = aper_put_length(po, -1, -1, end - buf, &need_eom); if(mayEncode < 0) ASN__ENCODE_FAILED; if(per_put_many_bits(po, buf, 8 * mayEncode)) ASN__ENCODE_FAILED; buf += mayEncode; - if(need_eom && (aper_put_length(po, -1, 0, 0) < 0)) ASN__ENCODE_FAILED; + if(need_eom && (aper_put_length(po, -1, -1, 0, NULL) < 0)) + ASN__ENCODE_FAILED; } ASN__ENCODED_OK(er); diff --git a/src/sbcap/gen/OCTET_STRING_aper.c b/src/sbcap/gen/OCTET_STRING_aper.c index eb2ea39..fb760c4 100644 --- a/src/sbcap/gen/OCTET_STRING_aper.c +++ b/src/sbcap/gen/OCTET_STRING_aper.c @@ -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; @@ -342,9 +343,8 @@ OCTET_STRING_encode_aper(const asn_TYPE_descriptor_t *td, st->size, sizeinunits - csiz->lower_bound, csiz->effective_bits); if (csiz->effective_bits > 0) { - ret = aper_put_length(po, - csiz->upper_bound - csiz->lower_bound + 1, - sizeinunits - csiz->lower_bound, 0); + ret = aper_put_length(po, csiz->lower_bound, csiz->upper_bound, + sizeinunits - csiz->lower_bound, NULL); if(ret < 0) ASN__ENCODE_FAILED; } if (csiz->effective_bits > 0 || (st->size > 2) @@ -372,7 +372,7 @@ OCTET_STRING_encode_aper(const asn_TYPE_descriptor_t *td, ASN_DEBUG("Encoding %lu bytes", st->size); if(sizeinunits == 0) { - if(aper_put_length(po, -1, 0, 0) < 0) + if(aper_put_length(po, -1, -1, 0, NULL) < 0) ASN__ENCODE_FAILED; ASN__ENCODED_OK(er); } @@ -380,7 +380,7 @@ OCTET_STRING_encode_aper(const asn_TYPE_descriptor_t *td, buf = st->buf; while(sizeinunits) { int need_eom = 0; - ssize_t maySave = aper_put_length(po, -1, sizeinunits, &need_eom); + ssize_t maySave = aper_put_length(po, -1, -1, sizeinunits, &need_eom); if(maySave < 0) ASN__ENCODE_FAILED; @@ -404,7 +404,7 @@ OCTET_STRING_encode_aper(const asn_TYPE_descriptor_t *td, buf += maySave >> 3; sizeinunits -= maySave; assert(!(maySave & 0x07) || !sizeinunits); - if(need_eom && (aper_put_length(po, -1, 0, 0) < 0)) + if(need_eom && (aper_put_length(po, -1, -1, 0, NULL) < 0)) ASN__ENCODE_FAILED; /* End of Message length */ } diff --git a/src/sbcap/gen/OPEN_TYPE_aper.c b/src/sbcap/gen/OPEN_TYPE_aper.c index a1a8e19..3e2ab18 100644 --- a/src/sbcap/gen/OPEN_TYPE_aper.c +++ b/src/sbcap/gen/OPEN_TYPE_aper.c @@ -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; @@ -171,4 +171,3 @@ OPEN_TYPE_aper_unknown_type_discard_bytes (asn_per_data_t *pd) { return rv; #undef ASN_DUMMY_BYTES } - diff --git a/src/sbcap/gen/aper_opentype.c b/src/sbcap/gen/aper_opentype.c index 1b22395..97cc410 100644 --- a/src/sbcap/gen/aper_opentype.c +++ b/src/sbcap/gen/aper_opentype.c @@ -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; @@ -101,12 +101,12 @@ aper_open_type_put(const asn_TYPE_descriptor_t *td, for(bptr = buf, toGo = size; toGo;) { int need_eom = 0; - ssize_t maySave = aper_put_length(po, -1, toGo, &need_eom); + ssize_t maySave = aper_put_length(po, -1, -1, toGo, &need_eom); if(maySave < 0) break; if(per_put_many_bits(po, bptr, maySave * 8)) break; bptr = (char *)bptr + maySave; toGo -= maySave; - if(need_eom && (aper_put_length(po, -1, 0, 0) < 0)) { + if(need_eom && (aper_put_length(po, -1, -1, 0, NULL) < 0)) { FREEMEM(buf); return -1; } diff --git a/src/sbcap/gen/aper_support.c b/src/sbcap/gen/aper_support.c index d6fa6b0..9ca8614 100644 --- a/src/sbcap/gen/aper_support.c +++ b/src/sbcap/gen/aper_support.c @@ -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; @@ -32,14 +36,14 @@ aper_get_length(asn_per_data_t *pd, int range, int ebits, int *repeat) { value = per_get_few_bits(pd, 8); if(value < 0) return -1; - if((value & 128) == 0) /* #10.9.3.6 */ + if((value & 128) == 0) /* #11.9.3.6 */ return (value & 0x7F); - if((value & 64) == 0) { /* #10.9.3.7 */ + if((value & 64) == 0) { /* #11.9.3.7 */ value = ((value & 63) << 8) | per_get_few_bits(pd, 8); if(value < 0) return -1; return value; } - value &= 63; /* this is "m" from X.691, #10.9.3.8 */ + value &= 63; /* this is "m" from X.691, #11.9.3.8 */ if(value < 1 || value > 4) return -1; *repeat = 1; @@ -59,13 +63,12 @@ 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 */ } } -#if !defined(USE_OLDER_APER_NSNNWN) ssize_t aper_get_nsnnwn(asn_per_data_t *pd, int range) { ssize_t value; @@ -95,53 +98,28 @@ aper_get_nsnnwn(asn_per_data_t *pd, int range) { //return -1; int length; - /* handle indefinite range */ - length = per_get_few_bits(pd, 1); - if (length == 0) - return per_get_few_bits(pd, 6); - - if (aper_get_align(pd) < 0) - return -1; - - length = per_get_few_bits(pd, 8); - /* the length is not likely to be that big */ - if (length > 4) - return -1; - value = 0; - if (per_get_many_bits(pd, (uint8_t *)&value, 0, length * 8) < 0) - return -1; - return value; + /* handle indefinite range */ + length = per_get_few_bits(pd, 1); + if (length == 0) + return per_get_few_bits(pd, 6); + + if (aper_get_align(pd) < 0) + return -1; + + length = per_get_few_bits(pd, 8); + /* the length is not likely to be that big */ + if (length > 4) + return -1; + value = 0; + if (per_get_many_bits(pd, (uint8_t *)&value, 0, length * 8) < 0) + return -1; + return value; } if (aper_get_align(pd) < 0) return -1; value = per_get_few_bits(pd, 8 * bytes); return value; } -#else /* old APER codec */ -ssize_t -aper_get_nsnnwn(asn_per_data_t *pd, int dummy_range) { - ssize_t value; - - ASN_DEBUG("Get the normally small non-negative whole number APER"); - - value = per_get_few_bits(pd, 7); - if(value & 64) { /* implicit (value < 0) */ - value &= 63; - value <<= 2; - value |= per_get_few_bits(pd, 2); - if(value & 128) /* implicit (value < 0) */ - return -1; - if(value == 0) - return 0; - if(value >= 3) - return -1; - value = per_get_few_bits(pd, 8 * value); - return value; - } - - return value; -} -#endif /* don't use old APER */ int aper_put_align(asn_per_outp_t *po) { @@ -154,38 +132,42 @@ int aper_put_align(asn_per_outp_t *po) { } ssize_t -aper_put_length(asn_per_outp_t *po, int range, size_t length, int *need_eom) { - int dummy = 0; - if(!need_eom) need_eom = &dummy; +aper_put_length(asn_per_outp_t *po, ssize_t lb, ssize_t ub, size_t n, int *need_eom) { + int constrained = (lb >= 0) && (ub >= 0); + int dummy = 0; + if(!need_eom) need_eom = &dummy; + + *need_eom = 0; - *need_eom = 0; - ASN_DEBUG("APER put length %zu with range %d", length, range); + ASN_DEBUG("APER put length %zu with range (%zd..%zd)", n, lb, ub); - /* 10.9 X.691 Note 2 */ - if (range <= 65536 && range >= 0) - return aper_put_nsnnwn(po, range, length) ? -1 : (ssize_t)length; + /* 11.9 X.691 Note 2 */ + if (constrained && ub < 65536) { + int range = ub - lb + 1; + return aper_put_nsnnwn(po, range, n) ? -1 : (ssize_t)n; + } if (aper_put_align(po) < 0) return -1; - if(length <= 127) /* #10.9.3.6 */{ - return per_put_few_bits(po, length, 8) - ? -1 : (ssize_t)length; + if(n <= 127) { /* #11.9.3.6 */ + return per_put_few_bits(po, n, 8) + ? -1 : (ssize_t)n; + } + else if(n < 16384) /* #11.9.3.7 */ + return per_put_few_bits(po, n|0x8000, 16) + ? -1 : (ssize_t)n; + + *need_eom = 0 == (n & 16383); + n >>= 14; + if(n > 4) { + *need_eom = 0; + n = 4; } - else if(length < 16384) /* #10.9.3.7 */ - return per_put_few_bits(po, length|0x8000, 16) - ? -1 : (ssize_t)length; - - *need_eom = 0 == (length & 16383); - length >>= 14; - if(length > 4) { - *need_eom = 0; - length = 4; - } - - return per_put_few_bits(po, 0xC0 | length, 8) - ? -1 : (ssize_t)(length << 14); + + return per_put_few_bits(po, 0xC0 | n, 8) + ? -1 : (ssize_t)(n << 14); } @@ -193,11 +175,11 @@ int aper_put_nslength(asn_per_outp_t *po, size_t length) { if(length <= 64) { - /* #10.9.3.4 */ + /* #11.9.3.4 */ if(length == 0) return -1; return per_put_few_bits(po, length-1, 7) ? -1 : 0; } else { - if(aper_put_length(po, -1, length, 0) != (ssize_t)length) { + if(aper_put_length(po, -1, -1, length, NULL) != (ssize_t)length) { /* This might happen in case of >16K extensions */ return -1; } @@ -206,12 +188,11 @@ aper_put_nslength(asn_per_outp_t *po, size_t length) { return 0; } -#if !defined(USE_OLDER_APER_NSNNWN) int aper_put_nsnnwn(asn_per_outp_t *po, int range, int number) { int bytes; - ASN_DEBUG("aper put nsnnwn %d with range %d", number, range); + ASN_DEBUG("aper put nsnnwn %d with range %d", number, range); /* 10.5.7.1 X.691 */ if(range < 0) { int i; @@ -254,30 +235,5 @@ aper_put_nsnnwn(asn_per_outp_t *po, int range, int number) { /* if(per_put_few_bits(po, bytes, 8)) return -1; */ - return per_put_few_bits(po, number, 8 * bytes); -} -#else /* preserve old code base in case */ -int -aper_put_nsnnwn(asn_per_outp_t *po, int dummy_range, int n) { - int bytes; - - ASN_DEBUG("aper_put_nsnnwn"); - - if(n <= 63) { - if(n < 0) return -1; - return per_put_few_bits(po, n, 7); - } - if(n < 256) - bytes = 1; - else if(n < 65536) - bytes = 2; - else if(n < 256 * 65536) - bytes = 3; - else - return -1; /* This is not a "normally small" value */ - if(per_put_few_bits(po, bytes, 8)) - return -1; - - return per_put_few_bits(po, n, 8 * bytes); + return per_put_few_bits(po, number, 8 * bytes); } -#endif /* which aper_put_nsnnwn() */ diff --git a/src/sbcap/gen/constr_SEQUENCE_OF_aper.c b/src/sbcap/gen/constr_SEQUENCE_OF_aper.c index c64b697..038657a 100644 --- a/src/sbcap/gen/constr_SEQUENCE_OF_aper.c +++ b/src/sbcap/gen/constr_SEQUENCE_OF_aper.c @@ -54,7 +54,7 @@ SEQUENCE_OF_encode_aper(const asn_TYPE_descriptor_t *td, */ if (ct->lower_bound == ct->upper_bound && ct->upper_bound < 65536) { /* No length determinant */ - } else if (aper_put_length(po, ct->upper_bound - ct->lower_bound + 1, list->count - ct->lower_bound, 0) < 0) + } else if (aper_put_length(po, ct->lower_bound, ct->upper_bound, list->count - ct->lower_bound, 0) < 0) ASN__ENCODE_FAILED; } @@ -65,7 +65,7 @@ SEQUENCE_OF_encode_aper(const asn_TYPE_descriptor_t *td, if(ct && ct->effective_bits >= 0) { mayEncode = list->count; } else { - mayEncode = aper_put_length(po, -1, list->count - seq, &need_eom); + mayEncode = aper_put_length(po, -1, -1, list->count - seq, &need_eom); if(mayEncode < 0) ASN__ENCODE_FAILED; } @@ -79,7 +79,7 @@ SEQUENCE_OF_encode_aper(const asn_TYPE_descriptor_t *td, ASN__ENCODE_FAILED; } - if(need_eom && (aper_put_length(po, -1, 0, 0) < 0)) + if(need_eom && (aper_put_length(po, -1, -1, 0, NULL) < 0)) ASN__ENCODE_FAILED; /* End of Message length */ } diff --git a/src/sbcap/gen/constr_SET_OF_aper.c b/src/sbcap/gen/constr_SET_OF_aper.c index 7a62616..72d52a6 100644 --- a/src/sbcap/gen/constr_SET_OF_aper.c +++ b/src/sbcap/gen/constr_SET_OF_aper.c @@ -52,7 +52,7 @@ SET_OF_encode_aper(const asn_TYPE_descriptor_t *td, ct->effective_bits)) ASN__ENCODE_FAILED;*/ - if (aper_put_length(po, ct->upper_bound - ct->lower_bound + 1, list->count - ct->lower_bound, 0) < 0) { + if (aper_put_length(po, ct->lower_bound, ct->upper_bound, list->count - ct->lower_bound, 0) < 0) { ASN__ENCODE_FAILED; } } @@ -70,7 +70,7 @@ SET_OF_encode_aper(const asn_TYPE_descriptor_t *td, may_encode = list->count; } else { may_encode = - aper_put_length(po, -1, list->count - seq, &need_eom); + aper_put_length(po, -1, -1, list->count - seq, &need_eom); if(may_encode < 0) ASN__ENCODE_FAILED; } @@ -81,7 +81,7 @@ SET_OF_encode_aper(const asn_TYPE_descriptor_t *td, break; } } - if(need_eom && aper_put_length(po, -1, 0, 0)) + if(need_eom && (aper_put_length(po, -1, -1, 0, NULL) < 0)) ASN__ENCODE_FAILED; /* End of Message length */ } @@ -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; diff --git a/src/sbcap/skel/aper_support.h b/src/sbcap/skel/aper_support.h index 16e8afe..a372445 100644 --- a/src/sbcap/skel/aper_support.h +++ b/src/sbcap/skel/aper_support.h @@ -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); /* @@ -31,13 +31,14 @@ ssize_t aper_get_nsnnwn(asn_per_data_t *pd, int range); /* * X.691 (08/2015) #11.9 "General rules for encoding a length determinant" - * Put the length "whole_length" to the Aligned PER stream. - * If (opt_need_eom) is given, it will be set to 1 if final 0-length is needed. - * In that case, invoke uper_put_length(po, 0, 0) after encoding the last block. + * Put the length "n" to the Aligned PER stream. + * If (opt_need_eom) is given, it will be set to 1 if final 0-n is needed. + * In that case, invoke aper_put_length(po, -1, -1, 0, NULL) after encoding the + * last block. * This function returns the number of units which may be flushed * in the next units saving iteration. */ -ssize_t aper_put_length(asn_per_outp_t *po, int range, size_t length, +ssize_t aper_put_length(asn_per_outp_t *po, ssize_t lb, ssize_t ub, size_t n, int *opt_need_eom); /* Align the current bit position to octet bundary */