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 | /* |
| 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 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 | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 31 | 0, /* Use generic outmost tag fetcher */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 32 | asn_DEF_NativeEnumerated_tags, |
| 33 | sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]), |
| 34 | asn_DEF_NativeEnumerated_tags, /* Same as above */ |
| 35 | sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]), |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 36 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 37 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 38 | }; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 39 | |
Lev Walkin | c235011 | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 40 | asn_enc_rval_t |
| 41 | NativeEnumerated_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
| 42 | int ilevel, enum xer_encoder_flags_e flags, |
| 43 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 44 | asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics; |
| 45 | asn_enc_rval_t er; |
| 46 | const long *native = (const long *)sptr; |
| 47 | const asn_INTEGER_enum_map_t *el; |
| 48 | |
| 49 | (void)ilevel; |
| 50 | (void)flags; |
| 51 | |
| 52 | if(!native) _ASN_ENCODE_FAILED; |
| 53 | |
| 54 | el = INTEGER_map_value2enum(specs, *native); |
| 55 | if(el) { |
| 56 | size_t srcsize = el->enum_len + 5; |
| 57 | char *src = (char *)alloca(srcsize); |
| 58 | |
| 59 | er.encoded = snprintf(src, srcsize, "<%s/>", el->enum_name); |
| 60 | assert(er.encoded > 0 && (size_t)er.encoded < srcsize); |
| 61 | if(cb(src, er.encoded, app_key) < 0) _ASN_ENCODE_FAILED; |
| 62 | return er; |
| 63 | } else { |
| 64 | ASN_DEBUG("ASN.1 forbids dealing with " |
| 65 | "unknown value of ENUMERATED type"); |
| 66 | _ASN_ENCODE_FAILED; |
| 67 | } |
| 68 | |
| 69 | return er; |
| 70 | } |
| 71 | |