Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 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 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 18 | static 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 | }; |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 21 | asn_TYPE_descriptor_t asn_DEF_NativeEnumerated = { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 22 | "ENUMERATED", /* The ASN.1 type is still ENUMERATED */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 23 | "ENUMERATED", |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 24 | NativeInteger_free, |
| 25 | NativeInteger_print, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 26 | asn_generic_no_constraint, |
| 27 | NativeInteger_decode_ber, |
| 28 | NativeInteger_encode_der, |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 29 | NativeInteger_decode_xer, |
Lev Walkin | c235011 | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 30 | NativeEnumerated_encode_xer, |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 31 | NativeEnumerated_decode_uper, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 32 | 0, /* Use generic outmost tag fetcher */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 33 | 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 Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 37 | 0, /* No PER visible constraints */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 38 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 39 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 40 | }; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 41 | |
Lev Walkin | c235011 | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 42 | asn_enc_rval_t |
| 43 | NativeEnumerated_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 Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 64 | _ASN_ENCODED_OK(er); |
Lev Walkin | c235011 | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 65 | } else { |
| 66 | ASN_DEBUG("ASN.1 forbids dealing with " |
| 67 | "unknown value of ENUMERATED type"); |
| 68 | _ASN_ENCODE_FAILED; |
| 69 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 70 | } |
Lev Walkin | c235011 | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 71 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 72 | asn_dec_rval_t |
| 73 | NativeEnumerated_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 Walkin | c235011 | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 124 | } |
| 125 | |