2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
7 #include <NativeEnumerated.h>
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;
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;
29 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
30 if(!native) ASN__DECODE_FAILED;
33 ASN_DEBUG("Decoding %s as NativeEnumerated", td->name);
35 if(ct->flags & APC_EXTENSIBLE) {
36 int inext = per_get_few_bits(pd, 1);
37 if(inext < 0) ASN__DECODE_STARVED;
41 /* Deal with APER padding */
42 if(ct && ct->upper_bound >= 255) {
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);
50 per_get_few_bits(pd, padding);
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))
63 * X.691, #10.6: normally small non-negative whole number;
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;
80 *native = specs->value2enum[value].nat_value;
81 ASN_DEBUG("Decoded %s = %ld", td->name, *native);
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};
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;
98 if(!sptr) ASN__ENCODE_FAILED;
99 if(!specs) ASN__ENCODE_FAILED;
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! */
106 ASN_DEBUG("Encoding %s as NativeEnumerated", td->name);
110 native = *(const long *)sptr;
111 if(native < 0) ASN__ENCODE_FAILED;
113 key.nat_value = native;
114 kf = bsearch(&key, specs->value2enum, specs->map_count,
115 sizeof(key), NativeEnumerated__compar_value2enum);
117 ASN_DEBUG("No element corresponds to %ld", native);
120 value = kf - specs->value2enum;
122 if(ct->range_bits >= 0) {
123 int cmpWith = specs->extension
124 ? specs->extension - 1 : specs->map_count;
128 if(ct->flags & APC_EXTENSIBLE) {
129 if(per_put_few_bits(po, inext, 1))
131 if(inext) range_bits = 0;
136 if(range_bits && ct && ct->range_bits >= 0) {
137 if(per_put_few_bits(po, value, ct->range_bits))
142 if(!specs->extension)
146 * X.691, #10.6: normally small non-negative whole number;
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)))