SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / NativeReal_uper.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 /*
12  * Decode REAL type using PER.
13  */
14 asn_dec_rval_t
15 NativeReal_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
16                        const asn_TYPE_descriptor_t *td,
17                        const asn_per_constraints_t *constraints, void **sptr,
18                        asn_per_data_t *pd) {
19     asn_dec_rval_t rval;
20     double d;
21     REAL_t tmp;
22     void *ptmp = &tmp;
23     int ret;
24
25     (void)constraints;
26
27     memset(&tmp, 0, sizeof(tmp));
28     rval = OCTET_STRING_decode_uper(opt_codec_ctx, &asn_DEF_REAL,
29                                     NULL, &ptmp, pd);
30     if(rval.code != RC_OK) {
31         ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
32         return rval;
33     }
34
35     ret = asn_REAL2double(&tmp, &d);
36     ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
37     if(ret) ASN__DECODE_FAILED;
38
39     if(NativeReal__set(td, sptr, d) < 0 )
40         ASN__DECODE_FAILED;
41
42     return rval;
43 }
44
45 /*
46  * Encode the NativeReal using the OCTET STRING PER encoder.
47  */
48 asn_enc_rval_t
49 NativeReal_encode_uper(const asn_TYPE_descriptor_t *td,
50                        const asn_per_constraints_t *constraints,
51                        const void *sptr, asn_per_outp_t *po) {
52     double d = NativeReal__get_double(td, sptr);
53     asn_enc_rval_t erval = {0,0,0};
54     REAL_t tmp;
55
56     (void)constraints;
57
58     /* Prepare a temporary clean structure */
59     memset(&tmp, 0, sizeof(tmp));
60
61     if(asn_double2REAL(&tmp, d))
62         ASN__ENCODE_FAILED;
63
64     /* Encode a DER REAL */
65     erval = OCTET_STRING_encode_uper(&asn_DEF_REAL, NULL, &tmp, po);
66     if(erval.encoded == -1)
67         erval.structure_ptr = sptr;
68
69     /* Free possibly allocated members of the temporary structure */
70     ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
71
72     return erval;
73 }