Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / ASN1c / NativeInteger_oer.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 #ifndef ASN_DISABLE_OER_SUPPORT
7
8 #include <asn_internal.h>
9 #include <NativeInteger.h>
10 #include <errno.h>
11
12 asn_dec_rval_t
13 NativeInteger_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
14                          const asn_TYPE_descriptor_t *td,
15                          const asn_oer_constraints_t *constraints,
16                          void **nint_ptr, const void *ptr, size_t size) {
17     const asn_INTEGER_specifics_t *specs =
18         (const asn_INTEGER_specifics_t *)td->specifics;
19     asn_dec_rval_t rval = {RC_OK, 0};
20     long *native = (long *)*nint_ptr;
21     INTEGER_t tmpint;
22     INTEGER_t *tmpintptr = &tmpint;
23
24     memset(&tmpint, 0, sizeof(tmpint));
25
26     if(!native) {
27         native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
28         if(!native) ASN__DECODE_FAILED;
29     }
30
31     /*
32      * OPTIMIZATION: Encode directly rather than passing through INTEGER.
33      * Saves a memory allocation.
34      */
35     rval = INTEGER_decode_oer(opt_codec_ctx, td, constraints,
36                               (void **)&tmpintptr, ptr, size);
37     if(rval.code != RC_OK) {
38         ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
39         return rval;
40     }
41
42     if(specs && specs->field_unsigned) {
43         unsigned long ul;
44         int ok = asn_INTEGER2ulong(&tmpint, &ul) == 0;
45         ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
46         if(ok) {
47             *native = ul;
48         } else {
49             rval.code = RC_FAIL;
50             return rval;
51         }
52     } else {
53         long l;
54         int ok = asn_INTEGER2long(&tmpint, &l) == 0;
55         ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
56         if(ok) {
57             *native = l;
58         } else {
59             rval.code = RC_FAIL;
60             return rval;
61         }
62     }
63
64     return rval;
65 }
66
67 /*
68  * Encode as Canonical OER.
69  */
70 asn_enc_rval_t
71 NativeInteger_encode_oer(const asn_TYPE_descriptor_t *td,
72                          const asn_oer_constraints_t *constraints,
73                          const void *sptr, asn_app_consume_bytes_f *cb,
74                          void *app_key) {
75     const asn_INTEGER_specifics_t *specs =
76         (const asn_INTEGER_specifics_t *)td->specifics;
77     INTEGER_t tmpint;
78     long native;
79
80     if(!sptr) ASN__ENCODE_FAILED;
81
82     native = *(const long *)sptr;
83     memset(&tmpint, 0, sizeof(tmpint));
84
85     ASN_DEBUG("Encoding %s %ld as NativeInteger", td ? td->name : "", native);
86
87     if((specs && specs->field_unsigned) ? asn_ulong2INTEGER(&tmpint, native)
88                                         : asn_long2INTEGER(&tmpint, native)) {
89         ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
90         ASN__ENCODE_FAILED;
91     } else {
92         asn_enc_rval_t er =
93             INTEGER_encode_oer(td, constraints, &tmpint, cb, app_key);
94         ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
95         return er;
96     }
97 }
98
99 #endif  /* ASN_DISABLE_OER_SUPPORT */