From ef8dd441fcb492a37008bf7b8ec1c85b7b3c2a5d Mon Sep 17 00:00:00 2001 From: Lev Walkin <vlm@lionet.info> Date: Mon, 18 Sep 2017 22:38:03 -0700 Subject: [PATCH] print enumeration values for native integer in debug printouts --- skeletons/NativeEnumerated.c | 49 ++++++++++++++++++------------------ skeletons/NativeInteger.c | 36 ++++++++++++++++---------- 2 files changed, 48 insertions(+), 37 deletions(-) diff --git a/skeletons/NativeEnumerated.c b/skeletons/NativeEnumerated.c index 098c44e1..137b951d 100644 --- a/skeletons/NativeEnumerated.c +++ b/skeletons/NativeEnumerated.c @@ -58,34 +58,35 @@ asn_TYPE_descriptor_t asn_DEF_NativeEnumerated = { }; asn_enc_rval_t -NativeEnumerated_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, - int ilevel, enum xer_encoder_flags_e flags, - asn_app_consume_bytes_f *cb, void *app_key) { +NativeEnumerated_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, int ilevel, + enum xer_encoder_flags_e flags, + asn_app_consume_bytes_f *cb, void *app_key) { const asn_INTEGER_specifics_t *specs = (const asn_INTEGER_specifics_t *)td->specifics; asn_enc_rval_t er; const long *native = (const long *)sptr; - const asn_INTEGER_enum_map_t *el; - - (void)ilevel; - (void)flags; - - if(!native) ASN__ENCODE_FAILED; - - el = INTEGER_map_value2enum(specs, *native); - if(el) { - size_t srcsize = el->enum_len + 5; - char *src = (char *)alloca(srcsize); - - er.encoded = snprintf(src, srcsize, "<%s/>", el->enum_name); - assert(er.encoded > 0 && (size_t)er.encoded < srcsize); - if(cb(src, er.encoded, app_key) < 0) ASN__ENCODE_FAILED; - ASN__ENCODED_OK(er); - } else { - ASN_DEBUG("ASN.1 forbids dealing with " - "unknown value of ENUMERATED type"); - ASN__ENCODE_FAILED; - } + const asn_INTEGER_enum_map_t *el; + + (void)ilevel; + (void)flags; + + if(!native) ASN__ENCODE_FAILED; + + el = INTEGER_map_value2enum(specs, *native); + if(el) { + size_t srcsize = el->enum_len + 5; + char *src = (char *)alloca(srcsize); + + er.encoded = snprintf(src, srcsize, "<%s/>", el->enum_name); + assert(er.encoded > 0 && (size_t)er.encoded < srcsize); + if(cb(src, er.encoded, app_key) < 0) ASN__ENCODE_FAILED; + ASN__ENCODED_OK(er); + } else { + ASN_DEBUG( + "ASN.1 forbids dealing with " + "unknown value of ENUMERATED type"); + ASN__ENCODE_FAILED; + } } asn_dec_rval_t diff --git a/skeletons/NativeInteger.c b/skeletons/NativeInteger.c index 286233e6..5ba65e14 100644 --- a/skeletons/NativeInteger.c +++ b/skeletons/NativeInteger.c @@ -323,22 +323,32 @@ NativeInteger_encode_uper(asn_TYPE_descriptor_t *td, */ int NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, - asn_app_consume_bytes_f *cb, void *app_key) { + asn_app_consume_bytes_f *cb, void *app_key) { const asn_INTEGER_specifics_t *specs = (const asn_INTEGER_specifics_t *)td->specifics; const long *native = (const long *)sptr; - char scratch[32]; /* Enough for 64-bit int */ - int ret; - - (void)td; /* Unused argument */ - (void)ilevel; /* Unused argument */ - - if(native) { - ret = snprintf(scratch, sizeof(scratch), - (specs && specs->field_unsigned) - ? "%lu" : "%ld", *native); - assert(ret > 0 && (size_t)ret < sizeof(scratch)); - return (cb(scratch, ret, app_key) < 0) ? -1 : 0; + char scratch[32]; /* Enough for 64-bit int */ + int ret; + + (void)td; /* Unused argument */ + (void)ilevel; /* Unused argument */ + + if(native) { + long value = *native; + ret = snprintf(scratch, sizeof(scratch), + (specs && specs->field_unsigned) ? "%lu" : "%ld", value); + assert(ret > 0 && (size_t)ret < sizeof(scratch)); + if(cb(scratch, ret, app_key) < 0) return -1; + if(specs && (value >= 0 || !specs->field_unsigned)) { + const asn_INTEGER_enum_map_t *el = + INTEGER_map_value2enum(specs, value); + if(el) { + if(cb(" (", 2, app_key) < 0) return -1; + if(cb(el->enum_name, el->enum_len, app_key) < 0) return -1; + if(cb(")", 1, app_key) < 0) return -1; + } + } + return 0; } else { return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; } -- 2.26.2