blob: 124e7f0f17600579376eab6f38787be41fe424ef [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,
Sylvain Munaut485a9722015-08-31 08:43:36 +020025 NativeEnumerated_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000026 asn_generic_no_constraint,
27 NativeInteger_decode_ber,
28 NativeInteger_encode_der,
Lev Walkin435cb282004-10-21 14:13:48 +000029 NativeInteger_decode_xer,
Lev Walkinc2350112005-03-29 17:19:53 +000030 NativeEnumerated_encode_xer,
Lev Walkin59b176e2005-11-26 11:25:14 +000031 NativeEnumerated_decode_uper,
Lev Walkin523de9e2006-08-18 01:34:18 +000032 NativeEnumerated_encode_uper,
Harald Welte498c9712015-08-30 16:33:07 +020033 NativeEnumerated_decode_aper,
34 NativeEnumerated_encode_aper,
Lev Walkinf15320b2004-06-03 03:38:44 +000035 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000036 asn_DEF_NativeEnumerated_tags,
37 sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]),
38 asn_DEF_NativeEnumerated_tags, /* Same as above */
39 sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000040 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000041 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000042 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000043};
Lev Walkin449f8322004-08-20 13:23:42 +000044
Lev Walkinc2350112005-03-29 17:19:53 +000045asn_enc_rval_t
46NativeEnumerated_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
47 int ilevel, enum xer_encoder_flags_e flags,
48 asn_app_consume_bytes_f *cb, void *app_key) {
49 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
50 asn_enc_rval_t er;
51 const long *native = (const long *)sptr;
52 const asn_INTEGER_enum_map_t *el;
53
54 (void)ilevel;
55 (void)flags;
56
57 if(!native) _ASN_ENCODE_FAILED;
58
59 el = INTEGER_map_value2enum(specs, *native);
60 if(el) {
61 size_t srcsize = el->enum_len + 5;
62 char *src = (char *)alloca(srcsize);
63
64 er.encoded = snprintf(src, srcsize, "<%s/>", el->enum_name);
65 assert(er.encoded > 0 && (size_t)er.encoded < srcsize);
66 if(cb(src, er.encoded, app_key) < 0) _ASN_ENCODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +000067 _ASN_ENCODED_OK(er);
Lev Walkinc2350112005-03-29 17:19:53 +000068 } else {
69 ASN_DEBUG("ASN.1 forbids dealing with "
70 "unknown value of ENUMERATED type");
71 _ASN_ENCODE_FAILED;
72 }
Lev Walkin59b176e2005-11-26 11:25:14 +000073}
Lev Walkinc2350112005-03-29 17:19:53 +000074
Lev Walkin59b176e2005-11-26 11:25:14 +000075asn_dec_rval_t
Lev Walkin523de9e2006-08-18 01:34:18 +000076NativeEnumerated_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
77 asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints,
78 void **sptr, asn_per_data_t *pd) {
Lev Walkin59b176e2005-11-26 11:25:14 +000079 asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics;
80 asn_dec_rval_t rval = { RC_OK, 0 };
81 long *native = (long *)*sptr;
82 asn_per_constraint_t *ct;
83 long value;
84
85 (void)opt_codec_ctx;
86
87 if(constraints) ct = &constraints->value;
88 else if(td->per_constraints) ct = &td->per_constraints->value;
89 else _ASN_DECODE_FAILED; /* Mandatory! */
90 if(!specs) _ASN_DECODE_FAILED;
91
92 if(!native) {
93 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
94 if(!native) _ASN_DECODE_FAILED;
95 }
96
97 ASN_DEBUG("Decoding %s as NativeEnumerated", td->name);
98
99 if(ct->flags & APC_EXTENSIBLE) {
100 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000101 if(inext < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000102 if(inext) ct = 0;
103 }
104
105 if(ct && ct->range_bits >= 0) {
106 value = per_get_few_bits(pd, ct->range_bits);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000107 if(value < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000108 if(value >= (specs->extension
109 ? specs->extension - 1 : specs->map_count))
110 _ASN_DECODE_FAILED;
111 } else {
112 if(!specs->extension)
113 _ASN_DECODE_FAILED;
114 /*
115 * X.691, #10.6: normally small non-negative whole number;
116 */
117 value = uper_get_nsnnwn(pd);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000118 if(value < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000119 value += specs->extension - 1;
120 if(value >= specs->map_count)
121 _ASN_DECODE_FAILED;
122 }
123
124 *native = specs->value2enum[value].nat_value;
125 ASN_DEBUG("Decoded %s = %ld", td->name, *native);
126
127 return rval;
Lev Walkinc2350112005-03-29 17:19:53 +0000128}
129
Harald Welte498c9712015-08-30 16:33:07 +0200130asn_dec_rval_t
131NativeEnumerated_decode_aper(asn_codec_ctx_t *opt_codec_ctx,
132 asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints,
133 void **sptr, asn_per_data_t *pd) {
134 asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics;
135 asn_dec_rval_t rval = { RC_OK, 0 };
136 long *native = (long *)*sptr;
137 asn_per_constraint_t *ct;
138 long value;
139
140 (void)opt_codec_ctx;
141
142 if(constraints) ct = &constraints->value;
143 else if(td->per_constraints) ct = &td->per_constraints->value;
144 else _ASN_DECODE_FAILED; /* Mandatory! */
145 if(!specs) _ASN_DECODE_FAILED;
146
147 if(!native) {
148 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
149 if(!native) _ASN_DECODE_FAILED;
150 }
151
152 ASN_DEBUG("Decoding %s as NativeEnumerated", td->name);
153
154 if(ct->flags & APC_EXTENSIBLE) {
155 int inext = per_get_few_bits(pd, 1);
156 if(inext < 0) _ASN_DECODE_STARVED;
157 if(inext) ct = 0;
158 }
159
160 if(ct && ct->range_bits >= 0) {
161 value = per_get_few_bits(pd, ct->range_bits);
162 if(value < 0) _ASN_DECODE_STARVED;
163 if(value >= (specs->extension
164 ? specs->extension - 1 : specs->map_count))
165 _ASN_DECODE_FAILED;
166 } else {
167 if(!specs->extension)
168 _ASN_DECODE_FAILED;
169 /*
170 * X.691, #10.6: normally small non-negative whole number;
171 */
172 value = uper_get_nsnnwn(pd);
173 if(value < 0) _ASN_DECODE_STARVED;
174 value += specs->extension - 1;
175 if(value >= specs->map_count)
176 _ASN_DECODE_FAILED;
177 }
178
179 *native = specs->value2enum[value].nat_value;
180 ASN_DEBUG("Decoded %s = %ld", td->name, *native);
181
182 return rval;
183}
184
Lev Walkin523de9e2006-08-18 01:34:18 +0000185static int
186NativeEnumerated__compar_value2enum(const void *ap, const void *bp) {
187 const asn_INTEGER_enum_map_t *a = ap;
188 const asn_INTEGER_enum_map_t *b = bp;
189 if(a->nat_value == b->nat_value)
190 return 0;
191 if(a->nat_value < b->nat_value)
192 return -1;
193 return 1;
194}
195
196asn_enc_rval_t
197NativeEnumerated_encode_uper(asn_TYPE_descriptor_t *td,
198 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
199 asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics;
200 asn_enc_rval_t er;
201 long native, value;
202 asn_per_constraint_t *ct;
203 int inext = 0;
204 asn_INTEGER_enum_map_t key;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700205 const asn_INTEGER_enum_map_t *kf;
Lev Walkin523de9e2006-08-18 01:34:18 +0000206
207 if(!sptr) _ASN_ENCODE_FAILED;
208 if(!specs) _ASN_ENCODE_FAILED;
209
210 if(constraints) ct = &constraints->value;
211 else if(td->per_constraints) ct = &td->per_constraints->value;
212 else _ASN_ENCODE_FAILED; /* Mandatory! */
213
214 ASN_DEBUG("Encoding %s as NativeEnumerated", td->name);
215
216 er.encoded = 0;
217
218 native = *(long *)sptr;
219 if(native < 0) _ASN_ENCODE_FAILED;
220
221 key.nat_value = native;
222 kf = bsearch(&key, specs->value2enum, specs->map_count,
223 sizeof(key), NativeEnumerated__compar_value2enum);
224 if(!kf) {
225 ASN_DEBUG("No element corresponds to %ld", native);
226 _ASN_ENCODE_FAILED;
227 }
228 value = kf - specs->value2enum;
229
230 if(ct->range_bits >= 0) {
231 int cmpWith = specs->extension
232 ? specs->extension - 1 : specs->map_count;
233 if(value >= cmpWith)
234 inext = 1;
235 }
236 if(ct->flags & APC_EXTENSIBLE) {
Lev Walkin39248f12007-07-23 07:47:04 +0000237 if(per_put_few_bits(po, inext, 1))
Lev Walkin523de9e2006-08-18 01:34:18 +0000238 _ASN_ENCODE_FAILED;
Lev Walkined502112007-07-23 09:57:00 +0000239 if(inext) ct = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +0000240 } else if(inext) {
241 _ASN_ENCODE_FAILED;
242 }
243
244 if(ct && ct->range_bits >= 0) {
245 if(per_put_few_bits(po, value, ct->range_bits))
246 _ASN_ENCODE_FAILED;
247 _ASN_ENCODED_OK(er);
248 }
249
250 if(!specs->extension)
251 _ASN_ENCODE_FAILED;
252
253 /*
254 * X.691, #10.6: normally small non-negative whole number;
255 */
Lev Walkin8bb57a22007-12-03 13:41:36 +0000256 ASN_DEBUG("value = %ld, ext = %d, inext = %d, res = %ld",
Lev Walkin39248f12007-07-23 07:47:04 +0000257 value, specs->extension, inext,
258 value - (inext ? (specs->extension - 1) : 0));
259 if(uper_put_nsnnwn(po, value - (inext ? (specs->extension - 1) : 0)))
Lev Walkin523de9e2006-08-18 01:34:18 +0000260 _ASN_ENCODE_FAILED;
261
262 _ASN_ENCODED_OK(er);
263}
264
Harald Welte498c9712015-08-30 16:33:07 +0200265asn_enc_rval_t
266NativeEnumerated_encode_aper(asn_TYPE_descriptor_t *td,
267 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
268 asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics;
269 asn_enc_rval_t er;
270 long native, value;
271 asn_per_constraint_t *ct;
272 int inext = 0;
273 asn_INTEGER_enum_map_t key;
274 asn_INTEGER_enum_map_t *kf;
275
276 if(!sptr) _ASN_ENCODE_FAILED;
277 if(!specs) _ASN_ENCODE_FAILED;
278
279 if(constraints) ct = &constraints->value;
280 else if(td->per_constraints) ct = &td->per_constraints->value;
281 else _ASN_ENCODE_FAILED; /* Mandatory! */
282
283 ASN_DEBUG("Encoding %s as NativeEnumerated", td->name);
284
285 er.encoded = 0;
286
287 native = *(long *)sptr;
288 if(native < 0) _ASN_ENCODE_FAILED;
289
290 key.nat_value = native;
291 kf = bsearch(&key, specs->value2enum, specs->map_count,
292 sizeof(key), NativeEnumerated__compar_value2enum);
293 if(!kf) {
294 ASN_DEBUG("No element corresponds to %ld", native);
295 _ASN_ENCODE_FAILED;
296 }
297 value = kf - specs->value2enum;
298
299 if(ct->range_bits >= 0) {
300 int cmpWith = specs->extension
301 ? specs->extension - 1 : specs->map_count;
302 if(value >= cmpWith)
303 inext = 1;
304 }
305 if(ct->flags & APC_EXTENSIBLE) {
306 if(per_put_few_bits(po, inext, 1))
307 _ASN_ENCODE_FAILED;
308 if(inext) ct = 0;
309 } else if(inext) {
310 _ASN_ENCODE_FAILED;
311 }
312
313 if(ct && ct->range_bits >= 0) {
314 if(per_put_few_bits(po, value, ct->range_bits))
315 _ASN_ENCODE_FAILED;
316 _ASN_ENCODED_OK(er);
317 }
318
319 if(!specs->extension)
320 _ASN_ENCODE_FAILED;
321
322 /*
323 * X.691, #10.6: normally small non-negative whole number;
324 */
325 ASN_DEBUG("value = %ld, ext = %d, inext = %d, res = %ld",
326 value, specs->extension, inext,
327 value - (inext ? (specs->extension - 1) : 0));
328 if(uper_put_nsnnwn(po, value - (inext ? (specs->extension - 1) : 0)))
329 _ASN_ENCODE_FAILED;
330
331 _ASN_ENCODED_OK(er);
332}
333
Sylvain Munaut485a9722015-08-31 08:43:36 +0200334int
335NativeEnumerated_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
336 asn_app_consume_bytes_f *cb, void *app_key) {
337 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
338 const long *native = (const long *)sptr;
339 char scratch[256];
340 int ret;
341
342 (void)td; /* Unused argument */
343 (void)ilevel; /* Unused argument */
344
345 if(native) {
346 const asn_INTEGER_enum_map_t *map = INTEGER_map_value2enum(specs, *native);
347 if (map && map->enum_len && map->enum_name) {
348 ret = snprintf(scratch, sizeof(scratch),
349 "%s", map->enum_name);
350 } else {
351 ret = snprintf(scratch, sizeof(scratch),
352 (specs && specs->field_unsigned)
353 ? "%lu" : "%ld", *native);
354 }
355 assert(ret > 0 && (size_t)ret < sizeof(scratch));
356 return (cb(scratch, ret, app_key) < 0) ? -1 : 0;
357 } else {
358 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
359 }
360}