SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / ANY_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 <ANY.h>
8 #include <errno.h>
9
10 #undef RETURN
11 #define RETURN(_code)                       \
12     do {                                    \
13         asn_dec_rval_t tmprval;             \
14         tmprval.code = _code;               \
15         tmprval.consumed = consumed_myself; \
16         return tmprval;                     \
17     } while(0)
18
19 asn_dec_rval_t
20 ANY_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
21                 const asn_TYPE_descriptor_t *td,
22                 const asn_per_constraints_t *constraints, void **sptr,
23                 asn_per_data_t *pd) {
24     const asn_OCTET_STRING_specifics_t *specs =
25         td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
26                       : &asn_SPC_ANY_specs;
27     size_t consumed_myself = 0;
28     int repeat;
29     ANY_t *st = (ANY_t *)*sptr;
30
31     (void)opt_codec_ctx;
32     (void)constraints;
33
34     /*
35      * Allocate the structure.
36      */
37     if(!st) {
38         st = (ANY_t *)(*sptr = CALLOC(1, specs->struct_size));
39         if(!st) RETURN(RC_FAIL);
40     }
41
42     ASN_DEBUG("UPER Decoding ANY type");
43
44     st->size = 0;
45     do {
46         ssize_t raw_len;
47         ssize_t len_bytes;
48         ssize_t len_bits;
49         void *p;
50         int ret;
51
52         /* Get the PER length */
53         raw_len = uper_get_length(pd, -1, 0, &repeat);
54         if(raw_len < 0) RETURN(RC_WMORE);
55         if(raw_len == 0 && st->buf) break;
56
57         ASN_DEBUG("Got PER length len %" ASN_PRI_SIZE ", %s (%s)", raw_len,
58                   repeat ? "repeat" : "once", td->name);
59         len_bytes = raw_len;
60         len_bits = len_bytes * 8;
61
62         p = REALLOC(st->buf, st->size + len_bytes + 1);
63         if(!p) RETURN(RC_FAIL);
64         st->buf = (uint8_t *)p;
65
66         ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
67         if(ret < 0) RETURN(RC_WMORE);
68         consumed_myself += len_bits;
69         st->size += len_bytes;
70     } while(repeat);
71     st->buf[st->size] = 0; /* nul-terminate */
72
73     RETURN(RC_OK);
74 }
75
76 asn_enc_rval_t
77 ANY_encode_uper(const asn_TYPE_descriptor_t *td,
78                 const asn_per_constraints_t *constraints, const void *sptr,
79                 asn_per_outp_t *po) {
80     const ANY_t *st = (const ANY_t *)sptr;
81     asn_enc_rval_t er = {0, 0, 0};
82     const uint8_t *buf;
83     size_t size;
84     int ret;
85
86     (void)constraints;
87
88     if(!st || (!st->buf && st->size)) ASN__ENCODE_FAILED;
89
90     buf = st->buf;
91     size = st->size;
92     do {
93         int need_eom = 0;
94         ssize_t may_save = uper_put_length(po, size, &need_eom);
95         if(may_save < 0) ASN__ENCODE_FAILED;
96
97         ret = per_put_many_bits(po, buf, may_save * 8);
98         if(ret) ASN__ENCODE_FAILED;
99
100         buf += may_save;
101         size -= may_save;
102         assert(!(may_save & 0x07) || !size);
103         if(need_eom && uper_put_length(po, 0, 0))
104             ASN__ENCODE_FAILED; /* End of Message length */
105     } while(size);
106
107     ASN__ENCODED_OK(er);
108 }