From 9a1736da96bcd8c0980824ec6426f1eb79804bb6 Mon Sep 17 00:00:00 2001
From: Lev Walkin <vlm@lionet.info>
Date: Sun, 27 Aug 2017 23:46:34 -0700
Subject: [PATCH] CHOICE stub for OER

---
 skeletons/Makefile.am         |   1 +
 skeletons/constr_CHOICE.c     |   4 +-
 skeletons/constr_CHOICE.h     |   2 +
 skeletons/constr_CHOICE_oer.c | 264 ++++++++++++++++++++++++++++++++++
 skeletons/file-dependencies   |   1 +
 5 files changed, 270 insertions(+), 2 deletions(-)
 create mode 100644 skeletons/constr_CHOICE_oer.c

diff --git a/skeletons/Makefile.am b/skeletons/Makefile.am
index 7f998cbd..3cc18f4e 100644
--- a/skeletons/Makefile.am
+++ b/skeletons/Makefile.am
@@ -66,6 +66,7 @@ libasn1cskeletons_la_SOURCES = 				\
 	ber_tlv_length.c ber_tlv_length.h		\
 	ber_tlv_tag.c ber_tlv_tag.h			\
 	constr_CHOICE.c constr_CHOICE.h			\
+	constr_CHOICE_oer.c 			\
 	constr_SEQUENCE.c constr_SEQUENCE.h		\
 	constr_SEQUENCE_oer.c \
 	constr_SEQUENCE_OF.c constr_SEQUENCE_OF.h	\
diff --git a/skeletons/constr_CHOICE.c b/skeletons/constr_CHOICE.c
index 16791409..d6b6141d 100644
--- a/skeletons/constr_CHOICE.c
+++ b/skeletons/constr_CHOICE.c
@@ -1252,8 +1252,8 @@ asn_TYPE_operation_t asn_OP_CHOICE = {
 	0,
 	0,
 #else
-	0,
-	0,
+	CHOICE_decode_oer,
+	CHOICE_encode_oer,
 #endif  /* ASN_DISABLE_OER_SUPPORT */
 #ifdef ASN_DISABLE_PER_SUPPORT
 	0,
diff --git a/skeletons/constr_CHOICE.h b/skeletons/constr_CHOICE.h
index 6af1805a..c7e0b908 100644
--- a/skeletons/constr_CHOICE.h
+++ b/skeletons/constr_CHOICE.h
@@ -47,6 +47,8 @@ ber_type_decoder_f CHOICE_decode_ber;
 der_type_encoder_f CHOICE_encode_der;
 xer_type_decoder_f CHOICE_decode_xer;
 xer_type_encoder_f CHOICE_encode_xer;
+oer_type_decoder_f CHOICE_decode_oer;
+oer_type_encoder_f CHOICE_encode_oer;
 per_type_decoder_f CHOICE_decode_uper;
 per_type_encoder_f CHOICE_encode_uper;
 asn_outmost_tag_f CHOICE_outmost_tag;
diff --git a/skeletons/constr_CHOICE_oer.c b/skeletons/constr_CHOICE_oer.c
new file mode 100644
index 00000000..0ddf9d28
--- /dev/null
+++ b/skeletons/constr_CHOICE_oer.c
@@ -0,0 +1,264 @@
+/*
+ * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
+ * All rights reserved.
+ * Redistribution and modifications are permitted subject to BSD license.
+ */
+#ifndef ASN_DISABLE_OER_SUPPORT
+
+#include <asn_internal.h>
+#include <constr_CHOICE.h>
+#include <errno.h>
+
+/*
+ * Return a standardized complex structure.
+ */
+#undef  RETURN
+#define RETURN(_code)                    \
+    do {                                 \
+        rval.code = _code;               \
+        rval.consumed = consumed_myself; \
+        return rval;                     \
+    } while(0)
+
+#undef  ADVANCE
+#define ADVANCE(num_bytes)               \
+    do {                                 \
+        size_t num = num_bytes;          \
+        ptr = ((const char *)ptr) + num; \
+        size -= num;                     \
+        consumed_myself += num;          \
+    } while(0)
+
+/*
+ * Switch to the next phase of parsing.
+ */
+#undef  NEXT_PHASE
+#define NEXT_PHASE(ctx) \
+    do {                \
+        ctx->phase++;   \
+        ctx->step = 0;  \
+    } while(0)
+
+
+/*
+ * Tags are canonically sorted in the tag to member table.
+ */
+static int
+_search4tag(const void *ap, const void *bp) {
+    const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
+    const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
+
+    int a_class = BER_TAG_CLASS(a->el_tag);
+    int b_class = BER_TAG_CLASS(b->el_tag);
+
+    if(a_class == b_class) {
+        ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
+        ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
+
+        if(a_value == b_value)
+            return 0;
+        else if(a_value < b_value)
+            return -1;
+        else
+            return 1;
+    } else if(a_class < b_class) {
+        return -1;
+    } else {
+        return 1;
+    }
+}
+
+/*
+ * X.696 (08/2015) #8.7 Encoding of tags
+ */
+static ssize_t
+oer_fetch_tag(const void *ptr, size_t size, ber_tlv_tag_t *tag_r) {
+    ber_tlv_tag_t val;
+    ber_tlv_tag_t tclass;
+    size_t skipped;
+
+    if(size == 0)
+        return 0;
+
+    val = *(const uint8_t *)ptr;
+    tclass = (val >> 6);
+    if((val & 0x3F) != 0x3F) {
+        /* #8.7.1 */
+        *tag_r = ((val & 0x3F) << 2) | tclass;
+        fprintf(stderr, "byte %02x, tag_r = %d, %s", *(const uint8_t *)ptr,
+                *tag_r, ber_tlv_tag_string(*tag_r));
+        return 1;
+    }
+
+    /*
+     * Each octet contains 7 bits of useful information.
+     * The MSB is 0 if it is the last octet of the tag.
+     */
+    for(val = 0, ptr = ((const char *)ptr) + 1, skipped = 2; skipped <= size;
+        ptr = ((const char *)ptr) + 1, skipped++) {
+        unsigned int oct = *(const uint8_t *)ptr;
+        if(oct & 0x80) {
+            val = (val << 7) | (oct & 0x7F);
+            /*
+             * Make sure there are at least 9 bits spare
+             * at the MS side of a value.
+             */
+            if(val >> ((8 * sizeof(val)) - 9)) {
+                /*
+                 * We would not be able to accomodate
+                 * any more tag bits.
+                 */
+                return -1;
+            }
+        } else {
+            val = (val << 7) | oct;
+            *tag_r = (val << 2) | tclass;
+            return skipped;
+        }
+    }
+
+
+    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,
+                  const void *ptr, size_t size) {
+    /*
+     * Bring closer parts of structure description.
+     */
+    asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
+    asn_TYPE_member_t *elements = td->elements;
+
+    /*
+     * Parts of the structure being constructed.
+     */
+    void *st = *struct_ptr; /* Target structure. */
+    asn_struct_ctx_t *ctx;  /* Decoder context */
+
+    asn_dec_rval_t rval;   /* Return code from subparsers */
+
+    ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
+
+    (void)constraints;
+
+    ASN_DEBUG("Decoding %s as CHOICE", td->name);
+
+    /*
+     * Create the target structure if it is not present already.
+     */
+    if(st == 0) {
+        st = *struct_ptr = CALLOC(1, specs->struct_size);
+        if(st == 0) {
+            RETURN(RC_FAIL);
+        }
+    }
+
+    /*
+     * Restore parsing context.
+     */
+    ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
+    switch(ctx->phase) {
+    case 0: {
+        /*
+         * Discover the tag.
+         */
+        ber_tlv_tag_t tlv_tag; /* T from TLV */
+        ssize_t tag_len;       /* Length of TLV's T */
+
+        tag_len = oer_fetch_tag(ptr, size, &tlv_tag);
+        switch(tag_len) {
+        case 0:
+            ASN__DECODE_STARVED;
+        case -1:
+            ASN__DECODE_FAILED;
+        }
+
+        do {
+            const asn_TYPE_tag2member_t *t2m;
+            asn_TYPE_tag2member_t key = {tlv_tag, 0, 0, 0};
+
+            t2m = (const asn_TYPE_tag2member_t *)bsearch(
+                &key, specs->tag2el, specs->tag2el_count,
+                sizeof(specs->tag2el[0]), _search4tag);
+            if(t2m) {
+                /*
+                 * Found the element corresponding to the tag.
+                 */
+                NEXT_PHASE(ctx);
+                ctx->step = t2m->el_no;
+                break;
+            } else if(specs->ext_start == -1) {
+                ASN_DEBUG(
+                    "Unexpected tag %s "
+                    "in non-extensible CHOICE %s",
+                    ber_tlv_tag_string(tlv_tag), td->name);
+                RETURN(RC_FAIL);
+            } else {
+                /* Skip open type extension */
+                RETURN(RC_FAIL);
+            }
+        } while(0);
+
+
+        ADVANCE(tag_len);
+    }
+    case 1: {
+        asn_TYPE_member_t *elm = &elements[ctx->step]; /* CHOICE's element */
+        void *memb_ptr;         /* Pointer to the member */
+        void **memb_ptr2;       /* Pointer to that pointer */
+
+        /*
+         * Compute the position of the member inside a structure,
+         * and also a type of containment (it may be contained
+         * as pointer or using inline inclusion).
+         */
+        if(elm->flags & ATF_POINTER) {
+            /* Member is a pointer to another structure */
+            memb_ptr2 = (void **)((char *)st + elm->memb_offset);
+        } else {
+            /*
+             * A pointer to a pointer
+             * holding the start of the structure
+             */
+            memb_ptr = (char *)st + elm->memb_offset;
+            memb_ptr2 = &memb_ptr;
+        }
+
+        /* Set presence to be able to free it properly at any time */
+        (void)CHOICE_variant_set_presence(td, st, ctx->step + 1);
+
+        if(specs->ext_start <= ctx->step) {
+            /* We're in the extensions group. #20.2 requires Open Type */
+            RETURN(RC_FAIL);
+        }
+
+        rval = elm->type->op->oer_decoder(opt_codec_ctx, elm->type,
+                                          elm->oer_constraints, memb_ptr2, ptr,
+                                          size);
+        rval.consumed += consumed_myself;
+        return rval;
+    }
+    }
+
+    RETURN(RC_FAIL);
+}
+
+/*
+ * 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) {
+
+    (void)constraints;
+    (void)cb;
+    (void)app_key;
+
+    ASN__ENCODE_FAILED;
+}
+
+#endif  /* ASN_DISABLE_OER_SUPPORT */
diff --git a/skeletons/file-dependencies b/skeletons/file-dependencies
index 84cda0e3..dc82eb82 100644
--- a/skeletons/file-dependencies
+++ b/skeletons/file-dependencies
@@ -77,5 +77,6 @@ OCTET_STRING_oer.c
 NativeInteger_oer.c
 NativeEnumerated_oer.c
 constr_SEQUENCE_oer.c
+constr_CHOICE_oer.c
 
 CODEC-PER:			# THIS IS A SPECIAL SECTION
-- 
2.26.2