encode BIT STRING and CHOICE for OER
diff --git a/skeletons/BIT_STRING_oer.c b/skeletons/BIT_STRING_oer.c
index 55cee4a..1034d4b 100644
--- a/skeletons/BIT_STRING_oer.c
+++ b/skeletons/BIT_STRING_oer.c
@@ -90,11 +90,81 @@
BIT_STRING_encode_oer(asn_TYPE_descriptor_t *td,
const asn_oer_constraints_t *constraints, void *sptr,
asn_app_consume_bytes_f *cb, void *app_key) {
- asn_enc_rval_t er = {-1,td,sptr};
- (void)constraints;
- (void)cb;
- (void)app_key;
- return er;
+ BIT_STRING_t *st = (BIT_STRING_t *)sptr;
+ asn_enc_rval_t erval = {0, 0, 0};
+ const asn_oer_constraints_t *cts =
+ constraints ? constraints : td->oer_constraints;
+ ssize_t ct_size = cts ? cts->size : -1;
+ size_t effective_size;
+ size_t trailing_zeros = 0;
+ int fix_last_byte = 0;
+
+ if(!st) ASN__ENCODE_FAILED;
+
+ if(st->bits_unused & ~7) {
+ ASN_DEBUG("BIT STRING unused bits %d out of 0..7 range",
+ st->bits_unused);
+ ASN__ENCODE_FAILED;
+ }
+ if(st->bits_unused && !(st->size && st->buf)) {
+ ASN_DEBUG("BIT STRING %s size 0 can't support unused bits %d", td->name,
+ st->bits_unused);
+ ASN__ENCODE_FAILED;
+ }
+
+ if(ct_size >= 0) {
+ size_t ct_bytes = (ct_size + 7) >> 3;
+ if(st->size > ct_bytes) {
+ ASN_DEBUG("More bits in BIT STRING %s (%zd) than constrained %zd",
+ td->name, 8 * st->size - st->bits_unused, ct_size);
+ ASN__ENCODE_FAILED;
+ }
+ trailing_zeros = ct_bytes - st->size; /* Allow larger constraint */
+ } else {
+ uint8_t ub = st->bits_unused & 7;
+ size_t len_len = oer_serialize_length(1 + st->size, cb, app_key);
+ if(len_len < 0) ASN__ENCODE_FAILED;
+ if(cb(&ub, 1, app_key) < 0) {
+ ASN__ENCODE_FAILED;
+ }
+ erval.encoded += len_len + 1;
+ }
+
+ if(st->bits_unused) {
+ if(st->buf[st->size - 1] & (0xff << st->bits_unused)) {
+ fix_last_byte = 1;
+ }
+ }
+
+ if(cb(st->buf, st->size - fix_last_byte, app_key) < 0) {
+ ASN__ENCODE_FAILED;
+ }
+
+ if(fix_last_byte) {
+ uint8_t b = st->buf[st->size - 1] & (0xff << st->bits_unused);
+ if(cb(&b, 1, app_key) < 0) {
+ ASN__ENCODE_FAILED;
+ }
+ }
+
+ erval.encoded += st->size;
+
+ if(trailing_zeros) {
+ static uint8_t zeros[16];
+ while(trailing_zeros > 0) {
+ int ret;
+ if(trailing_zeros < sizeof(zeros)) {
+ ret = cb(zeros, trailing_zeros, app_key);
+ erval.encoded += trailing_zeros;
+ } else {
+ ret = cb(zeros, sizeof(zeros), app_key);
+ erval.encoded += sizeof(zeros);
+ }
+ if(ret < 0) ASN__ENCODE_FAILED;
+ }
+ }
+
+ return erval;
}
diff --git a/skeletons/constr_CHOICE_oer.c b/skeletons/constr_CHOICE_oer.c
index a0feaa0..13d3dca 100644
--- a/skeletons/constr_CHOICE_oer.c
+++ b/skeletons/constr_CHOICE_oer.c
@@ -121,11 +121,9 @@
}
}
-
return 0; /* Want more */
}
-
asn_dec_rval_t
CHOICE_decode_oer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
const asn_oer_constraints_t *constraints, void **struct_ptr,
@@ -270,18 +268,95 @@
}
/*
+ * X.696 (08/2015) #8.7 Encoding of tags
+ */
+static ssize_t
+oer_put_tag(ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, void *app_key) {
+ uint8_t tclass = BER_TAG_CLASS(tag);
+ ber_tlv_tag_t tval = BER_TAG_VALUE(tag);
+
+ if(tval < 0x3F) {
+ uint8_t b = (uint8_t)((tclass << 6) | tval);
+ if(cb(&b, 1, app_key) < 0) {
+ return -1;
+ }
+ return 1;
+ } else {
+ uint8_t buf[1 + 2 * sizeof(tval)];
+ uint8_t *b = &buf[sizeof(buf)-1]; /* Last addressable */
+ size_t encoded;
+ for(; ; tval >>= 7) {
+ if(tval >> 7) {
+ *b-- = 0x80 | (tval & 0x7f);
+ } else {
+ *b-- = tval & 0x7f;
+ break;
+ }
+ }
+ *b = (uint8_t)((tclass << 6) | 0x3F);
+ encoded = sizeof(buf) - (b - buf);
+ if(cb(b, encoded, app_key) < 0) {
+ return -1;
+ }
+ return encoded;
+ }
+
+}
+
+/*
* Encode as Canonical OER.
*/
asn_enc_rval_t
CHOICE_encode_oer(asn_TYPE_descriptor_t *td,
const asn_oer_constraints_t *constraints, void *sptr,
asn_app_consume_bytes_f *cb, void *app_key) {
+ asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
+ asn_TYPE_member_t *elm; /* CHOICE element */
+ unsigned present;
+ void *memb_ptr;
+ ber_tlv_tag_t tag;
+ ssize_t tag_len;
+ asn_enc_rval_t er;
(void)constraints;
- (void)cb;
- (void)app_key;
- ASN__ENCODE_FAILED;
+ if(!sptr) ASN__ENCODE_FAILED;
+
+ ASN_DEBUG("OER %s encoding as CHOICE", td->name);
+
+ present = CHOICE_variant_get_presence(td, sptr);
+ if(present == 0 || present > td->elements_count) {
+ ASN_DEBUG("CHOICE %s member is not selected", td->name);
+ ASN__ENCODE_FAILED;
+ }
+
+ elm = &td->elements[present-1];
+ if(elm->flags & ATF_POINTER) {
+ memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
+ if(memb_ptr == 0) {
+ /* Mandatory element absent */
+ ASN__ENCODE_FAILED;
+ }
+ } else {
+ memb_ptr = (void *)((char *)sptr + elm->memb_offset);
+ }
+
+ tag = asn_TYPE_outmost_tag(elm->type, memb_ptr, elm->tag_mode, elm->tag);
+ if(tag == 0) {
+ ASN__ENCODE_FAILED;
+ }
+
+ tag_len = oer_put_tag(tag, cb, app_key);
+ if(tag_len < 0) {
+ ASN__ENCODE_FAILED;
+ }
+
+ er = elm->type->op->oer_encoder(elm->type, elm->oer_constraints, memb_ptr,
+ cb, app_key);
+ if(er.encoded > 0)
+ er.encoded += tag_len;
+
+ return er;
}
#endif /* ASN_DISABLE_OER_SUPPORT */
diff --git a/skeletons/constr_SET_OF_oer.c b/skeletons/constr_SET_OF_oer.c
index 1350eb1..2205d4f 100644
--- a/skeletons/constr_SET_OF_oer.c
+++ b/skeletons/constr_SET_OF_oer.c
@@ -78,7 +78,7 @@
}
const uint8_t *b = (const uint8_t *)ptr + len_len;
- const uint8_t *bend = b + len_len;
+ const uint8_t *bend = b + len;
/* Skip the leading 0-bytes */
@@ -204,6 +204,24 @@
return rval;
}
+static ssize_t
+oer_put_quantity(size_t qty, asn_app_consume_bytes_f *cb, void *app_key) {
+ uint8_t buf[1 + sizeof(size_t)];
+ uint8_t *b = &buf[sizeof(size_t)]; /* Last addressable */
+ size_t encoded;
+
+ do {
+ *b-- = qty;
+ qty >>= 8;
+ } while(qty);
+
+ *b = sizeof(buf) - (b-buf) - 1;
+ encoded = sizeof(buf) - (b-buf);
+ if(cb(b, encoded, app_key) < 0)
+ return -1;
+ return encoded;
+}
+
/*
* Encode as Canonical OER.
*/
@@ -212,14 +230,39 @@
const asn_oer_constraints_t *constraints, void *sptr,
asn_app_consume_bytes_f *cb, void *app_key) {
size_t computed_size = 0;
+ ssize_t qty_len;
+ asn_TYPE_member_t *elm;
+ asn_anonymous_set_ *list;
+ size_t n;
(void)constraints;
- (void)cb;
- (void)app_key;
+
+ if(!sptr) ASN__ENCODE_FAILED;
+
+ elm = td->elements;
+ list = _A_SET_FROM_VOID(sptr);
+
+ qty_len = oer_put_quantity(list->count, cb, app_key);
+ if(qty_len < 0) {
+ ASN__ENCODE_FAILED;
+ }
+ computed_size += qty_len;
+
+ for(n = 0; n < list->count; n++) {
+ void *memb_ptr = list->array[n];
+ asn_enc_rval_t er;
+ er = elm->type->op->oer_encoder(elm->type, elm->oer_constraints,
+ memb_ptr, cb, app_key);
+ if(er.encoded < 0) {
+ return er;
+ } else {
+ computed_size += er.encoded;
+ }
+ }
{
- asn_enc_rval_t er = {computed_size, td, sptr};
- ASN__ENCODED_OK(er);
+ asn_enc_rval_t erval = {computed_size, 0, 0};
+ return erval;
}
}