From 8d05d716b069a4726b243e563471a17f6f29369f Mon Sep 17 00:00:00 2001 From: Mouse Date: Tue, 20 Aug 2024 21:35:22 -0400 Subject: [PATCH] Make APER encoding/decoding of constrained OCTET_STRING correct --- skeletons/OCTET_STRING_aper.c | 66 +++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/skeletons/OCTET_STRING_aper.c b/skeletons/OCTET_STRING_aper.c index 54efdef0d..b27c3cd21 100644 --- a/skeletons/OCTET_STRING_aper.c +++ b/skeletons/OCTET_STRING_aper.c @@ -61,28 +61,36 @@ OCTET_STRING_decode_aper(const asn_codec_ctx_t *opt_codec_ctx, switch(specs->subvariant) { case ASN_OSUBV_ANY: canonical_unit_bits = unit_bits = 8; - if(cval->flags & APC_CONSTRAINED) - unit_bits = cval->range_bits; - bpc = OS__BPC_CHAR; - break; - case ASN_OSUBV_STR: - canonical_unit_bits = unit_bits = 8; - /* - if(cval->flags & APC_CONSTRAINED) - unit_bits = cval->range_bits; - */ + if(cval->flags & APC_CONSTRAINED) { + /* follow power of 2 rule */ + if (cval->range_bits <= 2) { + unit_bits = 2; + } else { + if (cval->range_bits <= 4) { + unit_bits = 4; + /* otherwise, unit_bits = 8; */ + } + } + /* unit_bits = cval->range_bits; */ + ASN_DEBUG("APER decoding ASN_OSUBV_STR range_bits = %d unit_bits = %d\n", + cval->range_bits, unit_bits); + } bpc = OS__BPC_CHAR; break; case ASN_OSUBV_U16: canonical_unit_bits = unit_bits = 16; - if(cval->flags & APC_CONSTRAINED) + if(cval->flags & APC_CONSTRAINED) { unit_bits = cval->range_bits; + ASN_DEBUG("APER decoding ASN_OSUBV_U16 range_bits = %d\n", cval->range_bits); + } bpc = OS__BPC_U16; break; case ASN_OSUBV_U32: canonical_unit_bits = unit_bits = 32; - if(cval->flags & APC_CONSTRAINED) + if(cval->flags & APC_CONSTRAINED) { unit_bits = cval->range_bits; + ASN_DEBUG("APER decoding ASN_OSUBV_U32 range_bits = %d\n", cval->range_bits); + } bpc = OS__BPC_U32; break; /* @@ -275,30 +283,37 @@ OCTET_STRING_encode_aper(const asn_TYPE_descriptor_t *td, switch(specs->subvariant) { case ASN_OSUBV_ANY: canonical_unit_bits = unit_bits = 8; - if(cval->flags & APC_CONSTRAINED) - unit_bits = 8; - bpc = OS__BPC_CHAR; - break; - case ASN_OSUBV_STR: - canonical_unit_bits = unit_bits = 8; -/* - if(cval->flags & APC_CONSTRAINED) - unit_bits = 8; -*/ + if(cval->flags & APC_CONSTRAINED) { + /* follow power of 2 rule */ + if (cval->range_bits <= 2) { + unit_bits = 2; + } else { + if (cval->range_bits <= 4) + unit_bits = 4; + /* otherwise, unit_bits = 8; */ + } + /* unit_bits = cval->range_bits; */ + ASN_DEBUG("APER encoding ASN_OSUBV_STR range_bits = %d unit_bits = %d\n", + cval->range_bits, unit_bits); + } bpc = OS__BPC_CHAR; sizeinunits = st->size; break; case ASN_OSUBV_U16: canonical_unit_bits = unit_bits = 16; - if(cval->flags & APC_CONSTRAINED) + if(cval->flags & APC_CONSTRAINED) { unit_bits = cval->range_bits; + ASN_DEBUG("APER encoding ASN_OSUBV_U16 range_bits = %d\n", cval->range_bits); + } bpc = OS__BPC_U16; sizeinunits = st->size / 2; break; case ASN_OSUBV_U32: canonical_unit_bits = unit_bits = 32; - if(cval->flags & APC_CONSTRAINED) + if(cval->flags & APC_CONSTRAINED) { unit_bits = cval->range_bits; + ASN_DEBUG("APER encoding ASN_OSUBV_U32 range_bits = %d\n", cval->range_bits); + } bpc = OS__BPC_U32; sizeinunits = st->size / 4; break; @@ -311,8 +326,7 @@ OCTET_STRING_encode_aper(const asn_TYPE_descriptor_t *td, canonical_unit_bits = unit_bits = 1; bpc = OS__BPC_BIT; sizeinunits = st->size * 8 - (st->bits_unused & 0x07); - ASN_DEBUG("BIT STRING of %d bytes", - sizeinunits); + ASN_DEBUG("BIT STRING of %d bytes", sizeinunits); break; }