blob: 4ed8749d0897adcb6bc42fea20a19f28759739a4 [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};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080021asn_TYPE_operation_t asn_OP_NativeEnumerated = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000022 NativeInteger_free,
23 NativeInteger_print,
Lev Walkincd2f48e2017-08-10 02:14:59 -070024 NativeInteger_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +000025 NativeInteger_decode_ber,
26 NativeInteger_encode_der,
Lev Walkin435cb282004-10-21 14:13:48 +000027 NativeInteger_decode_xer,
Lev Walkinc2350112005-03-29 17:19:53 +000028 NativeEnumerated_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070029#ifdef ASN_DISABLE_OER_SUPPORT
30 0,
31 0,
32#else
Lev Walkinf9f3e062017-08-27 20:34:58 -070033 NativeEnumerated_decode_oer,
34 NativeEnumerated_encode_oer,
Lev Walkincc159472017-07-06 08:26:36 -070035#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040036#ifdef ASN_DISABLE_PER_SUPPORT
37 0,
38 0,
39#else
40 NativeEnumerated_decode_uper,
41 NativeEnumerated_encode_uper,
42#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -070043 NativeEnumerated_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080044 0 /* Use generic outmost tag fetcher */
45};
46asn_TYPE_descriptor_t asn_DEF_NativeEnumerated = {
47 "ENUMERATED", /* The ASN.1 type is still ENUMERATED */
48 "ENUMERATED",
49 &asn_OP_NativeEnumerated,
Lev Walkin5e033762004-09-29 13:26:15 +000050 asn_DEF_NativeEnumerated_tags,
51 sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]),
52 asn_DEF_NativeEnumerated_tags, /* Same as above */
53 sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]),
Lev Walkina5972be2017-09-29 23:15:58 -070054 { 0, 0, asn_generic_no_constraint },
Lev Walkin449f8322004-08-20 13:23:42 +000055 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000056 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000057};
Lev Walkin449f8322004-08-20 13:23:42 +000058
Lev Walkinc2350112005-03-29 17:19:53 +000059asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -070060NativeEnumerated_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
61 int ilevel, enum xer_encoder_flags_e flags,
Lev Walkinef8dd442017-09-18 22:38:03 -070062 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkind62a6622017-08-22 02:31:02 -070063 const asn_INTEGER_specifics_t *specs =
64 (const asn_INTEGER_specifics_t *)td->specifics;
65 asn_enc_rval_t er;
66 const long *native = (const long *)sptr;
Lev Walkinef8dd442017-09-18 22:38:03 -070067 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +000068
Lev Walkinef8dd442017-09-18 22:38:03 -070069 (void)ilevel;
70 (void)flags;
Lev Walkinc2350112005-03-29 17:19:53 +000071
Lev Walkinef8dd442017-09-18 22:38:03 -070072 if(!native) ASN__ENCODE_FAILED;
Lev Walkinc2350112005-03-29 17:19:53 +000073
Lev Walkinef8dd442017-09-18 22:38:03 -070074 el = INTEGER_map_value2enum(specs, *native);
75 if(el) {
Lev Walkine2bbbf82017-10-08 18:52:37 -070076 er.encoded =
77 asn__format_to_callback(cb, app_key, "<%s/>", el->enum_name);
78 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkinef8dd442017-09-18 22:38:03 -070079 ASN__ENCODED_OK(er);
80 } else {
81 ASN_DEBUG(
82 "ASN.1 forbids dealing with "
83 "unknown value of ENUMERATED type");
84 ASN__ENCODE_FAILED;
85 }
Lev Walkin59b176e2005-11-26 11:25:14 +000086}
Lev Walkinc2350112005-03-29 17:19:53 +000087
Lev Walkin59b176e2005-11-26 11:25:14 +000088asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -070089NativeEnumerated_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin20696a42017-10-17 21:27:33 -070090 const asn_TYPE_descriptor_t *td,
91 const asn_per_constraints_t *constraints,
92 void **sptr, asn_per_data_t *pd) {
93 const asn_INTEGER_specifics_t *specs = td->specifics;
Lev Walkind62a6622017-08-22 02:31:02 -070094 asn_dec_rval_t rval = { RC_OK, 0 };
Lev Walkin59b176e2005-11-26 11:25:14 +000095 long *native = (long *)*sptr;
Lev Walkin494fb702017-08-07 20:07:00 -070096 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +000097 long value;
98
99 (void)opt_codec_ctx;
100
101 if(constraints) ct = &constraints->value;
Lev Walkina5972be2017-09-29 23:15:58 -0700102 else if(td->encoding_constraints.per_constraints)
103 ct = &td->encoding_constraints.per_constraints->value;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700104 else ASN__DECODE_FAILED; /* Mandatory! */
105 if(!specs) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000106
107 if(!native) {
108 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700109 if(!native) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000110 }
111
112 ASN_DEBUG("Decoding %s as NativeEnumerated", td->name);
113
114 if(ct->flags & APC_EXTENSIBLE) {
115 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700116 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000117 if(inext) ct = 0;
118 }
119
120 if(ct && ct->range_bits >= 0) {
121 value = per_get_few_bits(pd, ct->range_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700122 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000123 if(value >= (specs->extension
124 ? specs->extension - 1 : specs->map_count))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700125 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000126 } else {
127 if(!specs->extension)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700128 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000129 /*
130 * X.691, #10.6: normally small non-negative whole number;
131 */
132 value = uper_get_nsnnwn(pd);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700133 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000134 value += specs->extension - 1;
135 if(value >= specs->map_count)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700136 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000137 }
138
139 *native = specs->value2enum[value].nat_value;
140 ASN_DEBUG("Decoded %s = %ld", td->name, *native);
141
142 return rval;
Lev Walkinc2350112005-03-29 17:19:53 +0000143}
144
Lev Walkin523de9e2006-08-18 01:34:18 +0000145static int
146NativeEnumerated__compar_value2enum(const void *ap, const void *bp) {
147 const asn_INTEGER_enum_map_t *a = ap;
148 const asn_INTEGER_enum_map_t *b = bp;
149 if(a->nat_value == b->nat_value)
150 return 0;
151 if(a->nat_value < b->nat_value)
152 return -1;
153 return 1;
154}
155
156asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700157NativeEnumerated_encode_uper(const asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700158 const asn_per_constraints_t *constraints,
Lev Walkin20696a42017-10-17 21:27:33 -0700159 const void *sptr, asn_per_outp_t *po) {
Lev Walkind62a6622017-08-22 02:31:02 -0700160 const asn_INTEGER_specifics_t *specs =
161 (const asn_INTEGER_specifics_t *)td->specifics;
162 asn_enc_rval_t er;
Lev Walkin523de9e2006-08-18 01:34:18 +0000163 long native, value;
Lev Walkin494fb702017-08-07 20:07:00 -0700164 const asn_per_constraint_t *ct;
Lev Walkin523de9e2006-08-18 01:34:18 +0000165 int inext = 0;
166 asn_INTEGER_enum_map_t key;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700167 const asn_INTEGER_enum_map_t *kf;
Lev Walkin523de9e2006-08-18 01:34:18 +0000168
Lev Walkin7c1dc052016-03-14 03:08:15 -0700169 if(!sptr) ASN__ENCODE_FAILED;
170 if(!specs) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000171
172 if(constraints) ct = &constraints->value;
Lev Walkina5972be2017-09-29 23:15:58 -0700173 else if(td->encoding_constraints.per_constraints)
174 ct = &td->encoding_constraints.per_constraints->value;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700175 else ASN__ENCODE_FAILED; /* Mandatory! */
Lev Walkin523de9e2006-08-18 01:34:18 +0000176
177 ASN_DEBUG("Encoding %s as NativeEnumerated", td->name);
178
179 er.encoded = 0;
180
Lev Walkin20696a42017-10-17 21:27:33 -0700181 native = *(const long *)sptr;
Lev Walkin523de9e2006-08-18 01:34:18 +0000182
183 key.nat_value = native;
184 kf = bsearch(&key, specs->value2enum, specs->map_count,
185 sizeof(key), NativeEnumerated__compar_value2enum);
186 if(!kf) {
187 ASN_DEBUG("No element corresponds to %ld", native);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700188 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000189 }
190 value = kf - specs->value2enum;
191
192 if(ct->range_bits >= 0) {
193 int cmpWith = specs->extension
194 ? specs->extension - 1 : specs->map_count;
195 if(value >= cmpWith)
196 inext = 1;
197 }
198 if(ct->flags & APC_EXTENSIBLE) {
Lev Walkin39248f12007-07-23 07:47:04 +0000199 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700200 ASN__ENCODE_FAILED;
Lev Walkined502112007-07-23 09:57:00 +0000201 if(inext) ct = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +0000202 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700203 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000204 }
205
206 if(ct && ct->range_bits >= 0) {
207 if(per_put_few_bits(po, value, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700208 ASN__ENCODE_FAILED;
209 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000210 }
211
212 if(!specs->extension)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700213 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000214
215 /*
216 * X.691, #10.6: normally small non-negative whole number;
217 */
Lev Walkin8bb57a22007-12-03 13:41:36 +0000218 ASN_DEBUG("value = %ld, ext = %d, inext = %d, res = %ld",
Lev Walkin39248f12007-07-23 07:47:04 +0000219 value, specs->extension, inext,
220 value - (inext ? (specs->extension - 1) : 0));
221 if(uper_put_nsnnwn(po, value - (inext ? (specs->extension - 1) : 0)))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700222 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000223
Lev Walkin7c1dc052016-03-14 03:08:15 -0700224 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000225}
226