blob: 098c44e1b18960527396d94a60c6d522f1c1ff34 [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 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080043 0 /* Use generic outmost tag fetcher */
44};
45asn_TYPE_descriptor_t asn_DEF_NativeEnumerated = {
46 "ENUMERATED", /* The ASN.1 type is still ENUMERATED */
47 "ENUMERATED",
48 &asn_OP_NativeEnumerated,
49 asn_generic_no_constraint,
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 Walkin76780762017-07-07 10:07:30 -070054 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000055 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000056 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000057 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000058};
Lev Walkin449f8322004-08-20 13:23:42 +000059
Lev Walkinc2350112005-03-29 17:19:53 +000060asn_enc_rval_t
61NativeEnumerated_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
62 int ilevel, enum xer_encoder_flags_e flags,
63 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkind62a6622017-08-22 02:31:02 -070064 const asn_INTEGER_specifics_t *specs =
65 (const asn_INTEGER_specifics_t *)td->specifics;
66 asn_enc_rval_t er;
67 const long *native = (const long *)sptr;
Lev Walkinc2350112005-03-29 17:19:53 +000068 const asn_INTEGER_enum_map_t *el;
69
70 (void)ilevel;
71 (void)flags;
72
Lev Walkin7c1dc052016-03-14 03:08:15 -070073 if(!native) ASN__ENCODE_FAILED;
Lev Walkinc2350112005-03-29 17:19:53 +000074
75 el = INTEGER_map_value2enum(specs, *native);
76 if(el) {
77 size_t srcsize = el->enum_len + 5;
78 char *src = (char *)alloca(srcsize);
79
80 er.encoded = snprintf(src, srcsize, "<%s/>", el->enum_name);
81 assert(er.encoded > 0 && (size_t)er.encoded < srcsize);
Lev Walkin7c1dc052016-03-14 03:08:15 -070082 if(cb(src, er.encoded, app_key) < 0) ASN__ENCODE_FAILED;
83 ASN__ENCODED_OK(er);
Lev Walkinc2350112005-03-29 17:19:53 +000084 } else {
85 ASN_DEBUG("ASN.1 forbids dealing with "
86 "unknown value of ENUMERATED type");
Lev Walkin7c1dc052016-03-14 03:08:15 -070087 ASN__ENCODE_FAILED;
Lev Walkinc2350112005-03-29 17:19:53 +000088 }
Lev Walkin59b176e2005-11-26 11:25:14 +000089}
Lev Walkinc2350112005-03-29 17:19:53 +000090
Lev Walkin59b176e2005-11-26 11:25:14 +000091asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -070092NativeEnumerated_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin494fb702017-08-07 20:07:00 -070093 asn_TYPE_descriptor_t *td, const asn_per_constraints_t *constraints,
Lev Walkin523de9e2006-08-18 01:34:18 +000094 void **sptr, asn_per_data_t *pd) {
Lev Walkind62a6622017-08-22 02:31:02 -070095 const asn_INTEGER_specifics_t *specs =
96 (const asn_INTEGER_specifics_t *)td->specifics;
97 asn_dec_rval_t rval = { RC_OK, 0 };
Lev Walkin59b176e2005-11-26 11:25:14 +000098 long *native = (long *)*sptr;
Lev Walkin494fb702017-08-07 20:07:00 -070099 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +0000100 long value;
101
102 (void)opt_codec_ctx;
103
104 if(constraints) ct = &constraints->value;
105 else if(td->per_constraints) ct = &td->per_constraints->value;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700106 else ASN__DECODE_FAILED; /* Mandatory! */
107 if(!specs) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000108
109 if(!native) {
110 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700111 if(!native) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000112 }
113
114 ASN_DEBUG("Decoding %s as NativeEnumerated", td->name);
115
116 if(ct->flags & APC_EXTENSIBLE) {
117 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700118 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000119 if(inext) ct = 0;
120 }
121
122 if(ct && ct->range_bits >= 0) {
123 value = per_get_few_bits(pd, ct->range_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700124 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000125 if(value >= (specs->extension
126 ? specs->extension - 1 : specs->map_count))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700127 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000128 } else {
129 if(!specs->extension)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700130 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000131 /*
132 * X.691, #10.6: normally small non-negative whole number;
133 */
134 value = uper_get_nsnnwn(pd);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700135 if(value < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000136 value += specs->extension - 1;
137 if(value >= specs->map_count)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700138 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000139 }
140
141 *native = specs->value2enum[value].nat_value;
142 ASN_DEBUG("Decoded %s = %ld", td->name, *native);
143
144 return rval;
Lev Walkinc2350112005-03-29 17:19:53 +0000145}
146
Lev Walkin523de9e2006-08-18 01:34:18 +0000147static int
148NativeEnumerated__compar_value2enum(const void *ap, const void *bp) {
149 const asn_INTEGER_enum_map_t *a = ap;
150 const asn_INTEGER_enum_map_t *b = bp;
151 if(a->nat_value == b->nat_value)
152 return 0;
153 if(a->nat_value < b->nat_value)
154 return -1;
155 return 1;
156}
157
158asn_enc_rval_t
159NativeEnumerated_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700160 const asn_per_constraints_t *constraints,
161 void *sptr, asn_per_outp_t *po) {
Lev Walkind62a6622017-08-22 02:31:02 -0700162 const asn_INTEGER_specifics_t *specs =
163 (const asn_INTEGER_specifics_t *)td->specifics;
164 asn_enc_rval_t er;
Lev Walkin523de9e2006-08-18 01:34:18 +0000165 long native, value;
Lev Walkin494fb702017-08-07 20:07:00 -0700166 const asn_per_constraint_t *ct;
Lev Walkin523de9e2006-08-18 01:34:18 +0000167 int inext = 0;
168 asn_INTEGER_enum_map_t key;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700169 const asn_INTEGER_enum_map_t *kf;
Lev Walkin523de9e2006-08-18 01:34:18 +0000170
Lev Walkin7c1dc052016-03-14 03:08:15 -0700171 if(!sptr) ASN__ENCODE_FAILED;
172 if(!specs) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000173
174 if(constraints) ct = &constraints->value;
175 else if(td->per_constraints) ct = &td->per_constraints->value;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700176 else ASN__ENCODE_FAILED; /* Mandatory! */
Lev Walkin523de9e2006-08-18 01:34:18 +0000177
178 ASN_DEBUG("Encoding %s as NativeEnumerated", td->name);
179
180 er.encoded = 0;
181
182 native = *(long *)sptr;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700183 if(native < 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000184
185 key.nat_value = native;
186 kf = bsearch(&key, specs->value2enum, specs->map_count,
187 sizeof(key), NativeEnumerated__compar_value2enum);
188 if(!kf) {
189 ASN_DEBUG("No element corresponds to %ld", native);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700190 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000191 }
192 value = kf - specs->value2enum;
193
194 if(ct->range_bits >= 0) {
195 int cmpWith = specs->extension
196 ? specs->extension - 1 : specs->map_count;
197 if(value >= cmpWith)
198 inext = 1;
199 }
200 if(ct->flags & APC_EXTENSIBLE) {
Lev Walkin39248f12007-07-23 07:47:04 +0000201 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700202 ASN__ENCODE_FAILED;
Lev Walkined502112007-07-23 09:57:00 +0000203 if(inext) ct = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +0000204 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700205 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000206 }
207
208 if(ct && ct->range_bits >= 0) {
209 if(per_put_few_bits(po, value, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700210 ASN__ENCODE_FAILED;
211 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000212 }
213
214 if(!specs->extension)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700215 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000216
217 /*
218 * X.691, #10.6: normally small non-negative whole number;
219 */
Lev Walkin8bb57a22007-12-03 13:41:36 +0000220 ASN_DEBUG("value = %ld, ext = %d, inext = %d, res = %ld",
Lev Walkin39248f12007-07-23 07:47:04 +0000221 value, specs->extension, inext,
222 value - (inext ? (specs->extension - 1) : 0));
223 if(uper_put_nsnnwn(po, value - (inext ? (specs->extension - 1) : 0)))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700224 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000225
Lev Walkin7c1dc052016-03-14 03:08:15 -0700226 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000227}
228