blob: 0278657d5394dd379068a0f197c3c8611e9439c6 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin39248f12007-07-23 07:47:04 +00002 * Copyright (c) 2004, 2007 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
5/*
Lev Walkin41635d32006-03-18 05:06:57 +00006 * Read the NativeInteger.h for the explanation wrt. differences between
Lev Walkinf15320b2004-06-03 03:38:44 +00007 * INTEGER and NativeInteger.
8 * Basically, both are decoders and encoders of ASN.1 INTEGER type, but this
9 * implementation deals with the standard (machine-specific) representation
10 * of them instead of using the platform-independent buffer.
11 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000012#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000013#include <NativeEnumerated.h>
14
15/*
16 * NativeEnumerated basic type description.
17 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070018static const ber_tlv_tag_t asn_DEF_NativeEnumerated_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000019 (ASN_TAG_CLASS_UNIVERSAL | (10 << 2))
20};
Lev Walkin5e033762004-09-29 13:26:15 +000021asn_TYPE_descriptor_t asn_DEF_NativeEnumerated = {
Lev Walkinf15320b2004-06-03 03:38:44 +000022 "ENUMERATED", /* The ASN.1 type is still ENUMERATED */
Lev Walkindc06f6b2004-10-20 15:50:55 +000023 "ENUMERATED",
Lev Walkina9cc46e2004-09-22 16:06:28 +000024 NativeInteger_free,
25 NativeInteger_print,
Lev Walkincd2f48e2017-08-10 02:14:59 -070026 NativeInteger_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +000027 asn_generic_no_constraint,
28 NativeInteger_decode_ber,
29 NativeInteger_encode_der,
Lev Walkin435cb282004-10-21 14:13:48 +000030 NativeInteger_decode_xer,
Lev Walkinc2350112005-03-29 17:19:53 +000031 NativeEnumerated_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070032#ifdef ASN_DISABLE_OER_SUPPORT
33 0,
34 0,
35#else
36 0,
37 0,
38#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040039#ifdef ASN_DISABLE_PER_SUPPORT
40 0,
41 0,
42#else
43 NativeEnumerated_decode_uper,
44 NativeEnumerated_encode_uper,
45#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000046 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000047 asn_DEF_NativeEnumerated_tags,
48 sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]),
49 asn_DEF_NativeEnumerated_tags, /* Same as above */
50 sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070051 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000052 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000053 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000054 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000055};
Lev Walkin449f8322004-08-20 13:23:42 +000056
Lev Walkinc2350112005-03-29 17:19:53 +000057asn_enc_rval_t
58NativeEnumerated_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
59 int ilevel, enum xer_encoder_flags_e flags,
60 asn_app_consume_bytes_f *cb, void *app_key) {
61 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
62 asn_enc_rval_t er;
63 const long *native = (const long *)sptr;
64 const asn_INTEGER_enum_map_t *el;
65
66 (void)ilevel;
67 (void)flags;
68
Lev Walkin7c1dc052016-03-14 03:08:15 -070069 if(!native) ASN__ENCODE_FAILED;
Lev Walkinc2350112005-03-29 17:19:53 +000070
71 el = INTEGER_map_value2enum(specs, *native);
72 if(el) {
73 size_t srcsize = el->enum_len + 5;
74 char *src = (char *)alloca(srcsize);
75
76 er.encoded = snprintf(src, srcsize, "<%s/>", el->enum_name);
77 assert(er.encoded > 0 && (size_t)er.encoded < srcsize);
Lev Walkin7c1dc052016-03-14 03:08:15 -070078 if(cb(src, er.encoded, app_key) < 0) ASN__ENCODE_FAILED;
79 ASN__ENCODED_OK(er);
Lev Walkinc2350112005-03-29 17:19:53 +000080 } else {
81 ASN_DEBUG("ASN.1 forbids dealing with "
82 "unknown value of ENUMERATED type");
Lev Walkin7c1dc052016-03-14 03:08:15 -070083 ASN__ENCODE_FAILED;
Lev Walkinc2350112005-03-29 17:19:53 +000084 }
Lev Walkin59b176e2005-11-26 11:25:14 +000085}
Lev Walkinc2350112005-03-29 17:19:53 +000086
Lev Walkin59b176e2005-11-26 11:25:14 +000087asn_dec_rval_t
Lev Walkin523de9e2006-08-18 01:34:18 +000088NativeEnumerated_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin494fb702017-08-07 20:07:00 -070089 asn_TYPE_descriptor_t *td, const asn_per_constraints_t *constraints,
Lev Walkin523de9e2006-08-18 01:34:18 +000090 void **sptr, asn_per_data_t *pd) {
Lev Walkin59b176e2005-11-26 11:25:14 +000091 asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics;
92 asn_dec_rval_t rval = { RC_OK, 0 };
93 long *native = (long *)*sptr;
Lev Walkin494fb702017-08-07 20:07:00 -070094 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +000095 long value;
96
97 (void)opt_codec_ctx;
98
99 if(constraints) ct = &constraints->value;
100 else if(td->per_constraints) ct = &td->per_constraints->value;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700101 else ASN__DECODE_FAILED; /* Mandatory! */
102 if(!specs) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000103
104 if(!native) {
105 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700106 if(!native) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000107 }
108
109 ASN_DEBUG("Decoding %s as NativeEnumerated", td->name);
110
111 if(ct->flags & APC_EXTENSIBLE) {
112 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700113 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000114 if(inext) ct = 0;
115 }
116
117 if(ct && ct->range_bits >= 0) {
118 value = per_get_few_bits(pd, ct->range_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700119 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000120 if(value >= (specs->extension
121 ? specs->extension - 1 : specs->map_count))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700122 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000123 } else {
124 if(!specs->extension)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700125 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000126 /*
127 * X.691, #10.6: normally small non-negative whole number;
128 */
129 value = uper_get_nsnnwn(pd);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700130 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000131 value += specs->extension - 1;
132 if(value >= specs->map_count)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700133 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000134 }
135
136 *native = specs->value2enum[value].nat_value;
137 ASN_DEBUG("Decoded %s = %ld", td->name, *native);
138
139 return rval;
Lev Walkinc2350112005-03-29 17:19:53 +0000140}
141
Lev Walkin523de9e2006-08-18 01:34:18 +0000142static int
143NativeEnumerated__compar_value2enum(const void *ap, const void *bp) {
144 const asn_INTEGER_enum_map_t *a = ap;
145 const asn_INTEGER_enum_map_t *b = bp;
146 if(a->nat_value == b->nat_value)
147 return 0;
148 if(a->nat_value < b->nat_value)
149 return -1;
150 return 1;
151}
152
153asn_enc_rval_t
154NativeEnumerated_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700155 const asn_per_constraints_t *constraints,
156 void *sptr, asn_per_outp_t *po) {
157 asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000158 asn_enc_rval_t er;
159 long native, value;
Lev Walkin494fb702017-08-07 20:07:00 -0700160 const asn_per_constraint_t *ct;
Lev Walkin523de9e2006-08-18 01:34:18 +0000161 int inext = 0;
162 asn_INTEGER_enum_map_t key;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700163 const asn_INTEGER_enum_map_t *kf;
Lev Walkin523de9e2006-08-18 01:34:18 +0000164
Lev Walkin7c1dc052016-03-14 03:08:15 -0700165 if(!sptr) ASN__ENCODE_FAILED;
166 if(!specs) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000167
168 if(constraints) ct = &constraints->value;
169 else if(td->per_constraints) ct = &td->per_constraints->value;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700170 else ASN__ENCODE_FAILED; /* Mandatory! */
Lev Walkin523de9e2006-08-18 01:34:18 +0000171
172 ASN_DEBUG("Encoding %s as NativeEnumerated", td->name);
173
174 er.encoded = 0;
175
176 native = *(long *)sptr;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700177 if(native < 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000178
179 key.nat_value = native;
180 kf = bsearch(&key, specs->value2enum, specs->map_count,
181 sizeof(key), NativeEnumerated__compar_value2enum);
182 if(!kf) {
183 ASN_DEBUG("No element corresponds to %ld", native);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700184 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000185 }
186 value = kf - specs->value2enum;
187
188 if(ct->range_bits >= 0) {
189 int cmpWith = specs->extension
190 ? specs->extension - 1 : specs->map_count;
191 if(value >= cmpWith)
192 inext = 1;
193 }
194 if(ct->flags & APC_EXTENSIBLE) {
Lev Walkin39248f12007-07-23 07:47:04 +0000195 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700196 ASN__ENCODE_FAILED;
Lev Walkined502112007-07-23 09:57:00 +0000197 if(inext) ct = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +0000198 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700199 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000200 }
201
202 if(ct && ct->range_bits >= 0) {
203 if(per_put_few_bits(po, value, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700204 ASN__ENCODE_FAILED;
205 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000206 }
207
208 if(!specs->extension)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700209 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000210
211 /*
212 * X.691, #10.6: normally small non-negative whole number;
213 */
Lev Walkin8bb57a22007-12-03 13:41:36 +0000214 ASN_DEBUG("value = %ld, ext = %d, inext = %d, res = %ld",
Lev Walkin39248f12007-07-23 07:47:04 +0000215 value, specs->extension, inext,
216 value - (inext ? (specs->extension - 1) : 0));
217 if(uper_put_nsnnwn(po, value - (inext ? (specs->extension - 1) : 0)))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700218 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000219
Lev Walkin7c1dc052016-03-14 03:08:15 -0700220 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000221}
222