diff options
author | Harald Welte <laforge@gnumonks.org> | 2015-08-31 08:56:53 +0200 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2015-08-31 09:35:13 +0200 |
commit | 41b85d5597119fa8105dc0641ffe5cd1bbae39b2 (patch) | |
tree | 1a425d76fafe089cca547eb9c79c6a9a631c34e2 /src/NativeReal.c | |
parent | f6b9173b02bfac093177cf6fd0f48ad56c5786c7 (diff) |
update to asn1c aper branch commit 6e00cbce7304a6972e82a12bb5fa82e41fa541be
which is closes to Lev Walkins master 62913d8b8e1eb96d74315ff748475ca818b69752
Diffstat (limited to 'src/NativeReal.c')
-rw-r--r-- | src/NativeReal.c | 104 |
1 files changed, 99 insertions, 5 deletions
diff --git a/src/NativeReal.c b/src/NativeReal.c index a1ff91e..3bd5a28 100644 --- a/src/NativeReal.c +++ b/src/NativeReal.c @@ -17,7 +17,7 @@ /* * NativeReal basic type description. */ -static ber_tlv_tag_t asn_DEF_NativeReal_tags[] = { +static const ber_tlv_tag_t asn_DEF_NativeReal_tags[] = { (ASN_TAG_CLASS_UNIVERSAL | (9 << 2)) }; asn_TYPE_descriptor_t asn_DEF_NativeReal = { @@ -32,6 +32,8 @@ asn_TYPE_descriptor_t asn_DEF_NativeReal = { NativeReal_encode_xer, NativeReal_decode_uper, NativeReal_encode_uper, + NativeReal_decode_aper, + NativeReal_encode_aper, 0, /* Use generic outmost tag fetcher */ asn_DEF_NativeReal_tags, sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]), @@ -107,10 +109,39 @@ NativeReal_decode_ber(asn_codec_ctx_t *opt_codec_ctx, tmp.buf = (uint8_t *)unconst_buf.nonconstbuf; tmp.size = length; - if(asn_REAL2double(&tmp, &d)) { - rval.code = RC_FAIL; - rval.consumed = 0; - return rval; + if(length < (ber_tlv_len_t)size) { + int ret; + uint8_t saved_byte = tmp.buf[tmp.size]; + tmp.buf[tmp.size] = '\0'; + ret = asn_REAL2double(&tmp, &d); + tmp.buf[tmp.size] = saved_byte; + if(ret) { + rval.code = RC_FAIL; + rval.consumed = 0; + return rval; + } + } else if(length < 48 /* Enough for longish %f value. */) { + tmp.buf = alloca(length + 1); + tmp.size = length; + memcpy(tmp.buf, buf_ptr, length); + tmp.buf[tmp.size] = '\0'; + if(asn_REAL2double(&tmp, &d)) { + rval.code = RC_FAIL; + rval.consumed = 0; + return rval; + } + } else { + /* This should probably never happen: impractically long value */ + tmp.buf = CALLOC(1, length + 1); + tmp.size = length; + if(tmp.buf) memcpy(tmp.buf, buf_ptr, length); + if(!tmp.buf || asn_REAL2double(&tmp, &d)) { + FREEMEM(tmp.buf); + rval.code = RC_FAIL; + rval.consumed = 0; + return rval; + } + FREEMEM(tmp.buf); } *Dbl = d; @@ -199,6 +230,43 @@ NativeReal_decode_uper(asn_codec_ctx_t *opt_codec_ctx, return rval; } +asn_dec_rval_t +NativeReal_decode_aper(asn_codec_ctx_t *opt_codec_ctx, + asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints, + void **dbl_ptr, asn_per_data_t *pd) { + double *Dbl = (double *)*dbl_ptr; + asn_dec_rval_t rval; + REAL_t tmp; + void *ptmp = &tmp; + int ret; + + (void)constraints; + + /* + * If the structure is not there, allocate it. + */ + if(Dbl == NULL) { + *dbl_ptr = CALLOC(1, sizeof(*Dbl)); + Dbl = (double *)*dbl_ptr; + if(Dbl == NULL) + _ASN_DECODE_FAILED; + } + + memset(&tmp, 0, sizeof(tmp)); + rval = OCTET_STRING_decode_aper(opt_codec_ctx, td, NULL, + &ptmp, pd); + if(rval.code != RC_OK) { + ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); + return rval; + } + + ret = asn_REAL2double(&tmp, Dbl); + ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); + if(ret) _ASN_DECODE_FAILED; + + return rval; +} + /* * Encode the NativeReal using the OCTET STRING PER encoder. */ @@ -228,6 +296,32 @@ NativeReal_encode_uper(asn_TYPE_descriptor_t *td, return erval; } +asn_enc_rval_t +NativeReal_encode_aper(asn_TYPE_descriptor_t *td, + asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) { + double Dbl = *(const double *)sptr; + asn_enc_rval_t erval; + REAL_t tmp; + + (void)constraints; + + /* Prepare a temporary clean structure */ + memset(&tmp, 0, sizeof(tmp)); + + if(asn_double2REAL(&tmp, Dbl)) + _ASN_ENCODE_FAILED; + + /* Encode a DER REAL */ + erval = OCTET_STRING_encode_aper(td, NULL, &tmp, po); + if(erval.encoded == -1) + erval.structure_ptr = sptr; + + /* Free possibly allocated members of the temporary structure */ + ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); + + return erval; +} + /* * Decode the chunk of XML text encoding REAL. */ |