fix OER NULL and SEQUENCE extensions round-trip

This commit is contained in:
Lev Walkin 2017-10-20 02:39:08 -07:00
parent a460cbd13e
commit 42f6c88018
7 changed files with 118 additions and 20 deletions

View File

@ -135,15 +135,24 @@ NULL_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
asn_dec_rval_t
NULL_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
const asn_TYPE_descriptor_t *td,
const asn_oer_constraints_t *constraints, void **struct_ptr,
const asn_oer_constraints_t *constraints, void **sptr,
const void *ptr, size_t size) {
asn_dec_rval_t rv = {RC_OK, 0};
(void)opt_codec_ctx;
(void)td;
(void)constraints;
(void)struct_ptr;
(void)ptr;
(void)size;
if(!*sptr) {
*sptr = MALLOC(sizeof(NULL_t));
if(*sptr) {
*(NULL_t *)*sptr = 0;
} else {
ASN__DECODE_FAILED;
}
}
return rv;
}

View File

@ -1517,8 +1517,18 @@ SEQUENCE_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
*(const void *const *)((const char *)bptr + elm->memb_offset);
if(!amemb) {
if(!bmemb) continue;
if(elm->default_value_cmp
&& elm->default_value_cmp(bmemb) == 0) {
/* A is absent, but B is present and equal to DEFAULT */
continue;
}
return -1;
} else if(!bmemb) {
if(elm->default_value_cmp
&& elm->default_value_cmp(amemb) == 0) {
/* B is absent, but A is present and equal to DEFAULT */
continue;
}
return 1;
}
} else {

View File

@ -207,9 +207,10 @@ SEQUENCE_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
memb_ptr2 = element_ptrptr(st, elm, &save_memb_ptr);
rval = elm->type->op->oer_decoder(opt_codec_ctx, elm->type,
elm->encoding_constraints.oer_constraints,
memb_ptr2, ptr, size);
rval = elm->type->op->oer_decoder(
opt_codec_ctx, elm->type,
elm->encoding_constraints.oer_constraints, memb_ptr2, ptr,
size);
}
switch(rval.code) {
case RC_OK:
@ -268,7 +269,7 @@ SEQUENCE_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
len_len = oer_fetch_length(ptr, size, &len);
if(len_len > 0) {
ADVANCE(len_len);
} if(len_len < 0) {
} else if(len_len < 0) {
RETURN(RC_FAIL);
} else {
RETURN(RC_WMORE);
@ -314,7 +315,8 @@ SEQUENCE_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
asn_bit_data_t *extadds = ctx->ptr;
size_t edx = ctx->step;
asn_TYPE_member_t *elm = &td->elements[edx];
void **memb_ptr2 = element_ptrptr(st, elm, 0);
void *tmp_memb_ptr;
void **memb_ptr2 = element_ptrptr(st, elm, &tmp_memb_ptr);
switch(asn_get_few_bits(extadds, 1)) {
case -1:
@ -332,9 +334,11 @@ SEQUENCE_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
continue;
case 1: {
/* Read OER open type */
ssize_t ot_size = oer_open_type_get(opt_codec_ctx, elm->type,
elm->encoding_constraints.oer_constraints,
memb_ptr2, ptr, size);
ssize_t ot_size =
oer_open_type_get(opt_codec_ctx, elm->type,
elm->encoding_constraints.oer_constraints,
memb_ptr2, ptr, size);
assert(ot_size <= (ssize_t)size);
if(ot_size > 0) {
ADVANCE(ot_size);
} else if(ot_size < 0) {
@ -342,8 +346,12 @@ SEQUENCE_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
} else {
/* Roll back open type parsing */
asn_get_undo(extadds, 1);
ASN_STRUCT_FREE(*elm->type, *memb_ptr2);
*memb_ptr2 = NULL;
if(memb_ptr2 == &tmp_memb_ptr) {
ASN_STRUCT_RESET(*elm->type, *memb_ptr2);
} else {
ASN_STRUCT_FREE(*elm->type, *memb_ptr2);
*memb_ptr2 = NULL;
}
RETURN(RC_WMORE);
}
break;
@ -385,7 +393,7 @@ SEQUENCE_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
}
}
return rval;
RETURN(RC_OK);
}
/*
@ -552,13 +560,14 @@ SEQUENCE_encode_oer(const asn_TYPE_descriptor_t *td,
&& elm->default_value_cmp(memb_ptr) == 0) {
/* Do not encode default value. */
} else {
asn_enc_rval_t er = elm->type->op->oer_encoder(
ssize_t wrote = oer_open_type_put(
elm->type, elm->encoding_constraints.oer_constraints,
memb_ptr, cb, app_key);
if(er.encoded == -1) {
return er;
ASN_DEBUG("Open type %s encoded in %zd, +computed=%zu", elm->type->name, wrote, computed_size);
if(wrote == -1) {
ASN__ENCODE_FAILED;
}
computed_size += er.encoded;
computed_size += wrote;
}
} else if(!elm->optional) {
ASN__ENCODE_FAILED;

View File

@ -136,6 +136,6 @@ oer_open_type_put(const asn_TYPE_descriptor_t *td,
if(er.encoded < 0) return -1;
assert(serialized_byte_count == (size_t)er.encoded);
return er.encoded + len_len;
return len_len + er.encoded;
}

View File

@ -48,8 +48,8 @@ typedef asn_enc_rval_t(oer_type_encoder_f)(
/*
* Write out the Open Type (X.696 (08/2015), #30).
* RETURN VALUES:
* -1: Fatal error encoding the type.
* >=0: Number of bytes serialized.
* -1: Fatal error encoding the type.
* >0: Number of bytes serialized.
*/
ssize_t oer_open_type_put(const struct asn_TYPE_descriptor_s *td,
const asn_oer_constraints_t *constraints,

View File

@ -40,6 +40,7 @@ TESTS += bundles/12-UniversalString-bundle.txt
TESTS += bundles/13-UTCTime-bundle.txt
TESTS += bundles/14-GeneralizedTime-bundle.txt
TESTS += bundles/15-CHOICE-bundle.txt
TESTS += bundles/16-SEQUENCE-bundle.txt
EXTRA_DIST = \
random-test-driver.c \

View File

@ -0,0 +1,69 @@
SEQUENCE { }
SEQUENCE { ... }
SEQUENCE { null NULL }
SEQUENCE { null NULL OPTIONAL }
SEQUENCE { ..., null NULL }
SEQUENCE { ..., null NULL OPTIONAL }
SEQUENCE { ..., null BOOLEAN }
SEQUENCE { ..., null BOOLEAN DEFAULT FALSE }
SEQUENCE { ..., null BOOLEAN DEFAULT TRUE }
SEQUENCE { null NULL }
SEQUENCE { null NULL, ... }
SEQUENCE { one NULL, two [2] NULL }
SEQUENCE { one [1] NULL, two [2] NULL }
SEQUENCE { one [2] NULL, two [1] NULL }
SEQUENCE { one [1] NULL, two [3] NULL }
SEQUENCE { one [3] NULL, two [1] NULL }
SEQUENCE { one [3] NULL, two [1] NULL, three [2] NULL }
SEQUENCE { one [4] NULL, two [3] NULL, three [1] NULL, four [2] NULL }
SEQUENCE { one [5] NULL, two [4] NULL, ..., three [3] NULL, four [2] NULL }
SEQUENCE { null NULL, ..., one [5] NULL, two [4] NULL, three [3] NULL, four [2] NULL }
SEQUENCE { one NULL, two [2] NULL, ... }
SEQUENCE { one NULL, ..., two [2] NULL }
SEQUENCE { one NULL, two [2] NULL, ..., three [3] NULL }
SEQUENCE { one NULL, ..., two [2] NULL, three [3] NULL }
SEQUENCE { one BOOLEAN, ..., two [2] BOOLEAN, three [3] BOOLEAN }
SEQUENCE { one BOOLEAN, two BIT STRING (SIZE(1..3)) }
SEQUENCE { null NULL, ..., one BOOLEAN, two BIT STRING (SIZE(1..3)) }
SEQUENCE { one NULL, two BOOLEAN, three BIT STRING (SIZE(1..3)) }
SEQUENCE { null NULL, ..., one [1] NULL, two BOOLEAN, three BIT STRING (SIZE(1..3)) }
SEQUENCE { one NULL, ..., two [2] NULL }
SEQUENCE { one [1] NULL, ..., two [2] NULL }
SEQUENCE { one [2] NULL, ..., two [1] NULL }
SEQUENCE { one [1] NULL, ..., two [3] NULL }
SEQUENCE { one [3] NULL, ..., two [1] NULL }
SEQUENCE { one [3] NULL, ..., two [1] NULL, three [2] NULL }
SEQUENCE { one [4] NULL, ..., two [3] NULL, three [1] NULL, four [2] NULL }
SEQUENCE { one [5] NULL, ..., two [4] NULL, ..., three [3] NULL, four [2] NULL }
SEQUENCE { one NULL, ..., two [2] NULL, ... }
SEQUENCE { one NULL, ..., two [2] NULL, ..., three [3] NULL }
SEQUENCE { one NULL, ..., two [2] NULL, three [3] NULL }
SEQUENCE { one BOOLEAN, ..., two [2] BOOLEAN, three [3] BOOLEAN, ... }
SEQUENCE { one BOOLEAN, ..., two BIT STRING (SIZE(1..3)) }
SEQUENCE { null NULL, ..., one BOOLEAN, two BIT STRING (SIZE(1..3)), ... }
SEQUENCE { one NULL, ..., two BOOLEAN, three BIT STRING (SIZE(1..3)) }
SEQUENCE { null NULL, ..., one [1] NULL, two BOOLEAN, three BIT STRING (SIZE(1..3)), ... }
SEQUENCE { null NULL, ..., one [1] NULL, two BOOLEAN, three BIT STRING (SIZE(1..3)), ..., four IA5String (SIZE(0)) }
SEQUENCE { ..., null NULL }
SEQUENCE { ..., null NULL, ... }
SEQUENCE { ..., one NULL, two [2] NULL }
SEQUENCE { ..., one [1] NULL, two [2] NULL }
SEQUENCE { ..., one [2] NULL, two [1] NULL }
SEQUENCE { ..., one [1] NULL, two [3] NULL }
SEQUENCE { ..., one [3] NULL, two [1] NULL }
SEQUENCE { ..., one [3] NULL, two [1] NULL, three [2] NULL }
SEQUENCE { ..., one [4] NULL, two [3] NULL, three [1] NULL, four [2] NULL }
SEQUENCE { ..., one [5] NULL, two [4] NULL, ..., three [3] NULL, four [2] NULL }
SEQUENCE { ..., null NULL, ..., one [5] NULL, two [4] NULL, three [3] NULL, four [2] NULL }
SEQUENCE { ..., one NULL, two [2] NULL, ... }
SEQUENCE { ..., one NULL, ..., two [2] NULL }
SEQUENCE { ..., one NULL, two [2] NULL, ..., three [3] NULL }
SEQUENCE { ..., one NULL, ..., two [2] NULL, three [3] NULL }
SEQUENCE { ..., one BOOLEAN, ..., two [2] BOOLEAN, three [3] BOOLEAN }
SEQUENCE { ..., one BOOLEAN, two BIT STRING (SIZE(1..3)) }
SEQUENCE { ..., null NULL, ..., one BOOLEAN, two BIT STRING (SIZE(1..3)) }
SEQUENCE { ..., one NULL, two BOOLEAN, three BIT STRING (SIZE(1..3)) }
SEQUENCE { ..., null NULL, ..., one [1] NULL, two BOOLEAN, three BIT STRING (SIZE(1..3)) }