blob: 124e7f0f17600579376eab6f38787be41fe424ef [file] [log] [blame]
Harald Welte92c45f32010-06-12 18:59:38 +02001/*-
Harald Welteec0e2172010-07-20 00:03:44 +02002 * Copyright (c) 2004, 2007 Lev Walkin <vlm@lionet.info>. All rights reserved.
Harald Welte92c45f32010-06-12 18:59:38 +02003 * Redistribution and modifications are permitted subject to BSD license.
4 */
5/*
6 * Read the NativeInteger.h for the explanation wrt. differences between
7 * 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 */
12#include <asn_internal.h>
13#include <NativeEnumerated.h>
14
15/*
16 * NativeEnumerated basic type description.
17 */
Harald Welte41b85d52015-08-31 08:56:53 +020018static const ber_tlv_tag_t asn_DEF_NativeEnumerated_tags[] = {
Harald Welte92c45f32010-06-12 18:59:38 +020019 (ASN_TAG_CLASS_UNIVERSAL | (10 << 2))
20};
21asn_TYPE_descriptor_t asn_DEF_NativeEnumerated = {
22 "ENUMERATED", /* The ASN.1 type is still ENUMERATED */
23 "ENUMERATED",
24 NativeInteger_free,
Sylvain Munautb9d517e2010-06-14 23:31:04 +020025 NativeEnumerated_print,
Harald Welte92c45f32010-06-12 18:59:38 +020026 asn_generic_no_constraint,
27 NativeInteger_decode_ber,
28 NativeInteger_encode_der,
29 NativeInteger_decode_xer,
30 NativeEnumerated_encode_xer,
31 NativeEnumerated_decode_uper,
32 NativeEnumerated_encode_uper,
Harald Welte41b85d52015-08-31 08:56:53 +020033 NativeEnumerated_decode_aper,
34 NativeEnumerated_encode_aper,
Harald Welte92c45f32010-06-12 18:59:38 +020035 0, /* Use generic outmost tag fetcher */
36 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]),
40 0, /* No PER visible constraints */
41 0, 0, /* No members */
42 0 /* No specifics */
43};
44
45asn_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;
67 _ASN_ENCODED_OK(er);
68 } else {
69 ASN_DEBUG("ASN.1 forbids dealing with "
70 "unknown value of ENUMERATED type");
71 _ASN_ENCODE_FAILED;
72 }
73}
74
75asn_dec_rval_t
76NativeEnumerated_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) {
79 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);
101 if(inext < 0) _ASN_DECODE_STARVED;
102 if(inext) ct = 0;
103 }
104
105 if(ct && ct->range_bits >= 0) {
106 value = per_get_few_bits(pd, ct->range_bits);
107 if(value < 0) _ASN_DECODE_STARVED;
108 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);
118 if(value < 0) _ASN_DECODE_STARVED;
119 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;
128}
129
Harald Welte41b85d52015-08-31 08:56:53 +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
Harald Welte92c45f32010-06-12 18:59:38 +0200185static 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;
Harald Welte41b85d52015-08-31 08:56:53 +0200205 const asn_INTEGER_enum_map_t *kf;
Harald Welte92c45f32010-06-12 18:59:38 +0200206
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) {
Harald Welteec0e2172010-07-20 00:03:44 +0200237 if(per_put_few_bits(po, inext, 1))
Harald Welte92c45f32010-06-12 18:59:38 +0200238 _ASN_ENCODE_FAILED;
Harald Welteec0e2172010-07-20 00:03:44 +0200239 if(inext) ct = 0;
Harald Welte92c45f32010-06-12 18:59:38 +0200240 } 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 */
Harald Welteec0e2172010-07-20 00:03:44 +0200256 ASN_DEBUG("value = %ld, ext = %d, inext = %d, res = %ld",
257 value, specs->extension, inext,
258 value - (inext ? (specs->extension - 1) : 0));
259 if(uper_put_nsnnwn(po, value - (inext ? (specs->extension - 1) : 0)))
Harald Welte92c45f32010-06-12 18:59:38 +0200260 _ASN_ENCODE_FAILED;
261
262 _ASN_ENCODED_OK(er);
263}
264
Harald Welte41b85d52015-08-31 08:56:53 +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 Munautb9d517e2010-06-14 23:31:04 +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}