blob: e130749fc3ecfcaf0b5bb7df096d7d37912d1009 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5/*
6 * Please 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 */
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 */
Lev Walkin5e033762004-09-29 13:26:15 +000018static 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 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 Walkinf15320b2004-06-03 03:38:44 +000032 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000033 asn_DEF_NativeEnumerated_tags,
34 sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]),
35 asn_DEF_NativeEnumerated_tags, /* Same as above */
36 sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000037 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000038 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000039 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000040};
Lev Walkin449f8322004-08-20 13:23:42 +000041
Lev Walkinc2350112005-03-29 17:19:53 +000042asn_enc_rval_t
43NativeEnumerated_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
44 int ilevel, enum xer_encoder_flags_e flags,
45 asn_app_consume_bytes_f *cb, void *app_key) {
46 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
47 asn_enc_rval_t er;
48 const long *native = (const long *)sptr;
49 const asn_INTEGER_enum_map_t *el;
50
51 (void)ilevel;
52 (void)flags;
53
54 if(!native) _ASN_ENCODE_FAILED;
55
56 el = INTEGER_map_value2enum(specs, *native);
57 if(el) {
58 size_t srcsize = el->enum_len + 5;
59 char *src = (char *)alloca(srcsize);
60
61 er.encoded = snprintf(src, srcsize, "<%s/>", el->enum_name);
62 assert(er.encoded > 0 && (size_t)er.encoded < srcsize);
63 if(cb(src, er.encoded, app_key) < 0) _ASN_ENCODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +000064 _ASN_ENCODED_OK(er);
Lev Walkinc2350112005-03-29 17:19:53 +000065 } else {
66 ASN_DEBUG("ASN.1 forbids dealing with "
67 "unknown value of ENUMERATED type");
68 _ASN_ENCODE_FAILED;
69 }
Lev Walkin59b176e2005-11-26 11:25:14 +000070}
Lev Walkinc2350112005-03-29 17:19:53 +000071
Lev Walkin59b176e2005-11-26 11:25:14 +000072asn_dec_rval_t
73NativeEnumerated_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
74 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
75 asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics;
76 asn_dec_rval_t rval = { RC_OK, 0 };
77 long *native = (long *)*sptr;
78 asn_per_constraint_t *ct;
79 long value;
80
81 (void)opt_codec_ctx;
82
83 if(constraints) ct = &constraints->value;
84 else if(td->per_constraints) ct = &td->per_constraints->value;
85 else _ASN_DECODE_FAILED; /* Mandatory! */
86 if(!specs) _ASN_DECODE_FAILED;
87
88 if(!native) {
89 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
90 if(!native) _ASN_DECODE_FAILED;
91 }
92
93 ASN_DEBUG("Decoding %s as NativeEnumerated", td->name);
94
95 if(ct->flags & APC_EXTENSIBLE) {
96 int inext = per_get_few_bits(pd, 1);
97 if(inext < 0) _ASN_DECODE_FAILED;
98 if(inext) ct = 0;
99 }
100
101 if(ct && ct->range_bits >= 0) {
102 value = per_get_few_bits(pd, ct->range_bits);
103 if(value < 0) _ASN_DECODE_FAILED;
104 if(value >= (specs->extension
105 ? specs->extension - 1 : specs->map_count))
106 _ASN_DECODE_FAILED;
107 } else {
108 if(!specs->extension)
109 _ASN_DECODE_FAILED;
110 /*
111 * X.691, #10.6: normally small non-negative whole number;
112 */
113 value = uper_get_nsnnwn(pd);
114 if(value < 0) _ASN_DECODE_FAILED;
115 value += specs->extension - 1;
116 if(value >= specs->map_count)
117 _ASN_DECODE_FAILED;
118 }
119
120 *native = specs->value2enum[value].nat_value;
121 ASN_DEBUG("Decoded %s = %ld", td->name, *native);
122
123 return rval;
Lev Walkinc2350112005-03-29 17:19:53 +0000124}
125