Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | 39248f1 | 2007-07-23 07:47:04 +0000 | [diff] [blame] | 2 | * Copyright (c) 2004, 2007 Lev Walkin <vlm@lionet.info>. All rights reserved. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | /* |
Lev Walkin | 41635d3 | 2006-03-18 05:06:57 +0000 | [diff] [blame] | 6 | * Read the NativeInteger.h for the explanation wrt. differences between |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 12 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 13 | #include <NativeEnumerated.h> |
| 14 | |
| 15 | /* |
| 16 | * NativeEnumerated basic type description. |
| 17 | */ |
Wim Lewis | 18c2ec9 | 2014-07-29 11:30:10 -0700 | [diff] [blame] | 18 | static const ber_tlv_tag_t asn_DEF_NativeEnumerated_tags[] = { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 19 | (ASN_TAG_CLASS_UNIVERSAL | (10 << 2)) |
| 20 | }; |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 21 | asn_TYPE_operation_t asn_OP_NativeEnumerated = { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 22 | NativeInteger_free, |
| 23 | NativeInteger_print, |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 24 | NativeInteger_compare, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 25 | asn_generic_no_constraint, |
| 26 | NativeInteger_decode_ber, |
| 27 | NativeInteger_encode_der, |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 28 | NativeInteger_decode_xer, |
Lev Walkin | c235011 | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 29 | NativeEnumerated_encode_xer, |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 30 | #ifdef ASN_DISABLE_OER_SUPPORT |
| 31 | 0, |
| 32 | 0, |
| 33 | #else |
Lev Walkin | f9f3e06 | 2017-08-27 20:34:58 -0700 | [diff] [blame] | 34 | NativeEnumerated_decode_oer, |
| 35 | NativeEnumerated_encode_oer, |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 36 | #endif /* ASN_DISABLE_OER_SUPPORT */ |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 37 | #ifdef ASN_DISABLE_PER_SUPPORT |
| 38 | 0, |
| 39 | 0, |
| 40 | #else |
| 41 | NativeEnumerated_decode_uper, |
| 42 | NativeEnumerated_encode_uper, |
| 43 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 44 | 0 /* Use generic outmost tag fetcher */ |
| 45 | }; |
| 46 | asn_TYPE_descriptor_t asn_DEF_NativeEnumerated = { |
| 47 | "ENUMERATED", /* The ASN.1 type is still ENUMERATED */ |
| 48 | "ENUMERATED", |
| 49 | &asn_OP_NativeEnumerated, |
| 50 | asn_generic_no_constraint, |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 51 | asn_DEF_NativeEnumerated_tags, |
| 52 | sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]), |
| 53 | asn_DEF_NativeEnumerated_tags, /* Same as above */ |
| 54 | sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]), |
Lev Walkin | 7678076 | 2017-07-07 10:07:30 -0700 | [diff] [blame] | 55 | 0, /* No OER visible constraints */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 56 | 0, /* No PER visible constraints */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 57 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 58 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 59 | }; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 60 | |
Lev Walkin | c235011 | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 61 | asn_enc_rval_t |
| 62 | NativeEnumerated_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
| 63 | int ilevel, enum xer_encoder_flags_e flags, |
| 64 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 65 | const asn_INTEGER_specifics_t *specs = |
| 66 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 67 | asn_enc_rval_t er; |
| 68 | const long *native = (const long *)sptr; |
Lev Walkin | c235011 | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 69 | const asn_INTEGER_enum_map_t *el; |
| 70 | |
| 71 | (void)ilevel; |
| 72 | (void)flags; |
| 73 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 74 | if(!native) ASN__ENCODE_FAILED; |
Lev Walkin | c235011 | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 75 | |
| 76 | el = INTEGER_map_value2enum(specs, *native); |
| 77 | if(el) { |
| 78 | size_t srcsize = el->enum_len + 5; |
| 79 | char *src = (char *)alloca(srcsize); |
| 80 | |
| 81 | er.encoded = snprintf(src, srcsize, "<%s/>", el->enum_name); |
| 82 | assert(er.encoded > 0 && (size_t)er.encoded < srcsize); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 83 | if(cb(src, er.encoded, app_key) < 0) ASN__ENCODE_FAILED; |
| 84 | ASN__ENCODED_OK(er); |
Lev Walkin | c235011 | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 85 | } else { |
| 86 | ASN_DEBUG("ASN.1 forbids dealing with " |
| 87 | "unknown value of ENUMERATED type"); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 88 | ASN__ENCODE_FAILED; |
Lev Walkin | c235011 | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 89 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 90 | } |
Lev Walkin | c235011 | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 91 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 92 | asn_dec_rval_t |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 93 | NativeEnumerated_decode_uper(asn_codec_ctx_t *opt_codec_ctx, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 94 | asn_TYPE_descriptor_t *td, const asn_per_constraints_t *constraints, |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 95 | void **sptr, asn_per_data_t *pd) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 96 | const asn_INTEGER_specifics_t *specs = |
| 97 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 98 | asn_dec_rval_t rval = { RC_OK, 0 }; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 99 | long *native = (long *)*sptr; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 100 | const asn_per_constraint_t *ct; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 101 | long value; |
| 102 | |
| 103 | (void)opt_codec_ctx; |
| 104 | |
| 105 | if(constraints) ct = &constraints->value; |
| 106 | else if(td->per_constraints) ct = &td->per_constraints->value; |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 107 | else ASN__DECODE_FAILED; /* Mandatory! */ |
| 108 | if(!specs) ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 109 | |
| 110 | if(!native) { |
| 111 | native = (long *)(*sptr = CALLOC(1, sizeof(*native))); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 112 | if(!native) ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | ASN_DEBUG("Decoding %s as NativeEnumerated", td->name); |
| 116 | |
| 117 | if(ct->flags & APC_EXTENSIBLE) { |
| 118 | int inext = per_get_few_bits(pd, 1); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 119 | if(inext < 0) ASN__DECODE_STARVED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 120 | if(inext) ct = 0; |
| 121 | } |
| 122 | |
| 123 | if(ct && ct->range_bits >= 0) { |
| 124 | value = per_get_few_bits(pd, ct->range_bits); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 125 | if(value < 0) ASN__DECODE_STARVED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 126 | if(value >= (specs->extension |
| 127 | ? specs->extension - 1 : specs->map_count)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 128 | ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 129 | } else { |
| 130 | if(!specs->extension) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 131 | ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 132 | /* |
| 133 | * X.691, #10.6: normally small non-negative whole number; |
| 134 | */ |
| 135 | value = uper_get_nsnnwn(pd); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 136 | if(value < 0) ASN__DECODE_STARVED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 137 | value += specs->extension - 1; |
| 138 | if(value >= specs->map_count) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 139 | ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | *native = specs->value2enum[value].nat_value; |
| 143 | ASN_DEBUG("Decoded %s = %ld", td->name, *native); |
| 144 | |
| 145 | return rval; |
Lev Walkin | c235011 | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 148 | static int |
| 149 | NativeEnumerated__compar_value2enum(const void *ap, const void *bp) { |
| 150 | const asn_INTEGER_enum_map_t *a = ap; |
| 151 | const asn_INTEGER_enum_map_t *b = bp; |
| 152 | if(a->nat_value == b->nat_value) |
| 153 | return 0; |
| 154 | if(a->nat_value < b->nat_value) |
| 155 | return -1; |
| 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | asn_enc_rval_t |
| 160 | NativeEnumerated_encode_uper(asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 161 | const asn_per_constraints_t *constraints, |
| 162 | void *sptr, asn_per_outp_t *po) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 163 | const asn_INTEGER_specifics_t *specs = |
| 164 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 165 | asn_enc_rval_t er; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 166 | long native, value; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 167 | const asn_per_constraint_t *ct; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 168 | int inext = 0; |
| 169 | asn_INTEGER_enum_map_t key; |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 170 | const asn_INTEGER_enum_map_t *kf; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 171 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 172 | if(!sptr) ASN__ENCODE_FAILED; |
| 173 | if(!specs) ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 174 | |
| 175 | if(constraints) ct = &constraints->value; |
| 176 | else if(td->per_constraints) ct = &td->per_constraints->value; |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 177 | else ASN__ENCODE_FAILED; /* Mandatory! */ |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 178 | |
| 179 | ASN_DEBUG("Encoding %s as NativeEnumerated", td->name); |
| 180 | |
| 181 | er.encoded = 0; |
| 182 | |
| 183 | native = *(long *)sptr; |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 184 | if(native < 0) ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 185 | |
| 186 | key.nat_value = native; |
| 187 | kf = bsearch(&key, specs->value2enum, specs->map_count, |
| 188 | sizeof(key), NativeEnumerated__compar_value2enum); |
| 189 | if(!kf) { |
| 190 | ASN_DEBUG("No element corresponds to %ld", native); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 191 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 192 | } |
| 193 | value = kf - specs->value2enum; |
| 194 | |
| 195 | if(ct->range_bits >= 0) { |
| 196 | int cmpWith = specs->extension |
| 197 | ? specs->extension - 1 : specs->map_count; |
| 198 | if(value >= cmpWith) |
| 199 | inext = 1; |
| 200 | } |
| 201 | if(ct->flags & APC_EXTENSIBLE) { |
Lev Walkin | 39248f1 | 2007-07-23 07:47:04 +0000 | [diff] [blame] | 202 | if(per_put_few_bits(po, inext, 1)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 203 | ASN__ENCODE_FAILED; |
Lev Walkin | ed50211 | 2007-07-23 09:57:00 +0000 | [diff] [blame] | 204 | if(inext) ct = 0; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 205 | } else if(inext) { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 206 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | if(ct && ct->range_bits >= 0) { |
| 210 | if(per_put_few_bits(po, value, ct->range_bits)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 211 | ASN__ENCODE_FAILED; |
| 212 | ASN__ENCODED_OK(er); |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | if(!specs->extension) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 216 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 217 | |
| 218 | /* |
| 219 | * X.691, #10.6: normally small non-negative whole number; |
| 220 | */ |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 221 | ASN_DEBUG("value = %ld, ext = %d, inext = %d, res = %ld", |
Lev Walkin | 39248f1 | 2007-07-23 07:47:04 +0000 | [diff] [blame] | 222 | value, specs->extension, inext, |
| 223 | value - (inext ? (specs->extension - 1) : 0)); |
| 224 | if(uper_put_nsnnwn(po, value - (inext ? (specs->extension - 1) : 0))) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 225 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 226 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 227 | ASN__ENCODED_OK(er); |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 228 | } |
| 229 | |