SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / NativeReal_aper.c
1 /*
2  * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
3  * All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
6 #include <asn_internal.h>
7 #include <NativeReal.h>
8 #include <REAL.h>
9 #include <OCTET_STRING.h>
10
11 asn_dec_rval_t
12 NativeReal_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
13                        const asn_TYPE_descriptor_t *td,
14                        const asn_per_constraints_t *constraints,
15                        void **dbl_ptr, asn_per_data_t *pd) {
16     double *Dbl = (double *)*dbl_ptr;
17     asn_dec_rval_t rval;
18     REAL_t tmp;
19     void *ptmp = &tmp;
20     int ret;
21
22     (void)constraints;
23
24     /*
25      * If the structure is not there, allocate it.
26      */
27     if(Dbl == NULL) {
28         *dbl_ptr = CALLOC(1, sizeof(*Dbl));
29         Dbl = (double *)*dbl_ptr;
30         if(Dbl == NULL)
31             ASN__DECODE_FAILED;
32     }
33
34     memset(&tmp, 0, sizeof(tmp));
35     rval = OCTET_STRING_decode_aper(opt_codec_ctx, td, NULL,
36             &ptmp, pd);
37     if(rval.code != RC_OK) {
38         ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
39         return rval;
40     }
41
42     ret = asn_REAL2double(&tmp, Dbl);
43     ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
44     if(ret) ASN__DECODE_FAILED;
45
46     return rval;
47 }
48
49 asn_enc_rval_t
50 NativeReal_encode_aper(const asn_TYPE_descriptor_t *td,
51                        const asn_per_constraints_t *constraints,
52                        const void *sptr, asn_per_outp_t *po) {
53     double Dbl = *(const double *)sptr;
54     asn_enc_rval_t erval = {0,0,0};
55     REAL_t tmp;
56
57     (void)constraints;
58
59     /* Prepare a temporary clean structure */
60     memset(&tmp, 0, sizeof(tmp));
61
62     if(asn_double2REAL(&tmp, Dbl))
63         ASN__ENCODE_FAILED;
64
65     /* Encode a DER REAL */
66     erval = OCTET_STRING_encode_aper(td, NULL, &tmp, po);
67     if(erval.encoded == -1)
68         erval.structure_ptr = sptr;
69
70     /* Free possibly allocated members of the temporary structure */
71     ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
72
73     return erval;
74 }