SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / NativeEnumerated_aper.c
1 /*
2  * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
3  * All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
6 #include <asn_internal.h>
7 #include <NativeEnumerated.h>
8
9 asn_dec_rval_t
10 NativeEnumerated_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
11                              const asn_TYPE_descriptor_t *td,
12                              const asn_per_constraints_t *constraints,
13                              void **sptr, asn_per_data_t *pd) {
14     const asn_INTEGER_specifics_t *specs = (const asn_INTEGER_specifics_t *)td->specifics;
15     asn_dec_rval_t rval = { RC_OK, 0 };
16     long *native = (long *)*sptr;
17     const asn_per_constraint_t *ct;
18     long value;
19
20     (void)opt_codec_ctx;
21
22     if(constraints) ct = &constraints->value;
23     else if(td->encoding_constraints.per_constraints)
24         ct = &td->encoding_constraints.per_constraints->value;
25     else ASN__DECODE_FAILED;  /* Mandatory! */
26     if(!specs) ASN__DECODE_FAILED;
27
28     if(!native) {
29         native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
30         if(!native) ASN__DECODE_FAILED;
31     }
32
33     ASN_DEBUG("Decoding %s as NativeEnumerated", td->name);
34
35     if(ct->flags & APC_EXTENSIBLE) {
36         int inext = per_get_few_bits(pd, 1);
37         if(inext < 0) ASN__DECODE_STARVED;
38         if(inext) ct = 0;
39     }
40
41     /* Deal with APER padding */
42     if(ct && ct->upper_bound >= 255) {
43         int padding = 0;
44         padding = (8 - (pd->moved % 8)) % 8;
45         ASN_DEBUG("For NativeEnumerated %s,offset = %zu Padding bits = %d",
46                   td->name, pd->moved, padding);
47         ASN_DEBUG("For NativeEnumerated %s, upper bound = %llu",
48                   td->name, (unsigned long long)ct->upper_bound);
49         if(padding > 0)
50             per_get_few_bits(pd, padding);
51     }
52
53     if(ct && ct->range_bits >= 0) {
54         value = per_get_few_bits(pd, ct->range_bits);
55         if(value < 0) ASN__DECODE_STARVED;
56         if(value >= (specs->extension
57              ? specs->extension - 1 : specs->map_count))
58         ASN__DECODE_FAILED;
59     } else {
60         if(!specs->extension)
61             ASN__DECODE_FAILED;
62         /*
63          * X.691, #10.6: normally small non-negative whole number;
64          */
65
66         /* XXX handle indefinite index length > 64k */
67         value = aper_get_nsnnwn(pd, 65537);
68         if(value < 0) ASN__DECODE_STARVED;
69         value += specs->extension - 1;
70         //if(value >= specs->map_count)
71         //    ASN__DECODE_FAILED;
72         if(value >= specs->map_count) {
73             ASN_DEBUG("Decoded unknown index value %s = %ld", td->name, value);
74             /* unknown index. Workaround => set the first enumeration value */
75             *native = specs->value2enum[0].nat_value;
76             return rval;
77         }
78     }
79
80     *native = specs->value2enum[value].nat_value;
81     ASN_DEBUG("Decoded %s = %ld", td->name, *native);
82
83     return rval;
84 }
85
86 asn_enc_rval_t
87 NativeEnumerated_encode_aper(const asn_TYPE_descriptor_t *td,
88                              const asn_per_constraints_t *constraints,
89                              const void *sptr, asn_per_outp_t *po) {
90     const asn_INTEGER_specifics_t *specs = (const asn_INTEGER_specifics_t *)td->specifics;
91     asn_enc_rval_t er = {0,0,0};
92     long native, value;
93     const asn_per_constraint_t *ct;
94     int inext = 0, range_bits = 1;
95     asn_INTEGER_enum_map_t key;
96     asn_INTEGER_enum_map_t *kf;
97
98     if(!sptr) ASN__ENCODE_FAILED;
99     if(!specs) ASN__ENCODE_FAILED;
100
101     if(constraints) ct = &constraints->value;
102     else if(td->encoding_constraints.per_constraints)
103         ct = &td->encoding_constraints.per_constraints->value;
104     else ASN__ENCODE_FAILED;  /* Mandatory! */
105
106     ASN_DEBUG("Encoding %s as NativeEnumerated", td->name);
107
108     er.encoded = 0;
109
110     native = *(const long *)sptr;
111     if(native < 0) ASN__ENCODE_FAILED;
112
113     key.nat_value = native;
114     kf = bsearch(&key, specs->value2enum, specs->map_count,
115                  sizeof(key), NativeEnumerated__compar_value2enum);
116     if(!kf) {
117         ASN_DEBUG("No element corresponds to %ld", native);
118         ASN__ENCODE_FAILED;
119     }
120     value = kf - specs->value2enum;
121
122     if(ct->range_bits >= 0) {
123         int cmpWith = specs->extension
124               ? specs->extension - 1 : specs->map_count;
125         if(value >= cmpWith)
126             inext = 1;
127     }
128     if(ct->flags & APC_EXTENSIBLE) {
129         if(per_put_few_bits(po, inext, 1))
130             ASN__ENCODE_FAILED;
131         if(inext) range_bits = 0;
132     } else if(inext) {
133         ASN__ENCODE_FAILED;
134     }
135
136     if(range_bits && ct && ct->range_bits >= 0) {
137         if(per_put_few_bits(po, value, ct->range_bits))
138             ASN__ENCODE_FAILED;
139         ASN__ENCODED_OK(er);
140     }
141
142     if(!specs->extension)
143         ASN__ENCODE_FAILED;
144
145     /*
146      * X.691, #10.6: normally small non-negative whole number;
147      */
148     ASN_DEBUG("value = %ld, ext = %d, inext = %d, res = %ld",
149               value, specs->extension, inext,
150               value - (inext ? (specs->extension - 1) : 0));
151     if(aper_put_nsnnwn(po,
152                        ct->upper_bound - ct->lower_bound + 1,
153                        value - (inext ? (specs->extension - 1) : 0)))
154         ASN__ENCODE_FAILED;
155
156     ASN__ENCODED_OK(er);
157 }