SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / uper_encoder.c
1 #include <asn_application.h>
2 #include <asn_internal.h>
3 #include <uper_encoder.h>
4
5 static int _uper_encode_flush_outp(asn_per_outp_t *po);
6
7 asn_enc_rval_t
8 uper_encode(const asn_TYPE_descriptor_t *td,
9             const asn_per_constraints_t *constraints, const void *sptr,
10             asn_app_consume_bytes_f *cb, void *app_key) {
11     asn_per_outp_t po;
12     asn_enc_rval_t er = {0,0,0};
13
14     /*
15      * Invoke type-specific encoder.
16      */
17     if(!td || !td->op->uper_encoder)
18         ASN__ENCODE_FAILED;     /* PER is not compiled in */
19
20     po.buffer = po.tmpspace;
21     po.nboff = 0;
22     po.nbits = 8 * sizeof(po.tmpspace);
23     po.output = cb ? cb : ignore_output;
24     po.op_key = app_key;
25     po.flushed_bytes = 0;
26
27     er = td->op->uper_encoder(td, constraints, sptr, &po);
28     if(er.encoded != -1) {
29         size_t bits_to_flush;
30
31         bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
32
33         /* Set number of bits encoded to a firm value */
34         er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
35
36         if(_uper_encode_flush_outp(&po)) ASN__ENCODE_FAILED;
37     }
38
39     return er;
40 }
41
42 /*
43  * Argument type and callback necessary for uper_encode_to_buffer().
44  */
45 typedef struct enc_to_buf_arg {
46         void *buffer;
47         size_t left;
48 } enc_to_buf_arg;
49 static int encode_to_buffer_cb(const void *buffer, size_t size, void *key) {
50         enc_to_buf_arg *arg = (enc_to_buf_arg *)key;
51
52         if(arg->left < size)
53                 return -1;      /* Data exceeds the available buffer size */
54
55         memcpy(arg->buffer, buffer, size);
56         arg->buffer = ((char *)arg->buffer) + size;
57         arg->left -= size;
58
59         return 0;
60 }
61
62 asn_enc_rval_t
63 uper_encode_to_buffer(const asn_TYPE_descriptor_t *td,
64                       const asn_per_constraints_t *constraints,
65                       const void *sptr, void *buffer, size_t buffer_size) {
66     enc_to_buf_arg key;
67
68     key.buffer = buffer;
69     key.left = buffer_size;
70
71     if(td) ASN_DEBUG("Encoding \"%s\" using UNALIGNED PER", td->name);
72
73     return uper_encode(td, constraints, sptr, encode_to_buffer_cb, &key);
74 }
75
76 ssize_t
77 uper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td,
78                           const asn_per_constraints_t *constraints,
79                           const void *sptr, void **buffer_r) {
80     asn_enc_rval_t er = {0,0,0};
81         enc_dyn_arg key;
82
83         memset(&key, 0, sizeof(key));
84
85         er = uper_encode(td, constraints, sptr, encode_dyn_cb, &key);
86         switch(er.encoded) {
87         case -1:
88                 FREEMEM(key.buffer);
89                 return -1;
90         case 0:
91                 FREEMEM(key.buffer);
92                 key.buffer = MALLOC(1);
93                 if(key.buffer) {
94                         *(char *)key.buffer = '\0';
95                         *buffer_r = key.buffer;
96                         return 1;
97                 } else {
98                         return -1;
99                 }
100         default:
101                 *buffer_r = key.buffer;
102                 ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
103                 return ((er.encoded + 7) >> 3);
104         }
105 }
106
107 /*
108  * Internally useful functions.
109  */
110
111 /* Flush partially filled buffer */
112 static int
113 _uper_encode_flush_outp(asn_per_outp_t *po) {
114         uint8_t *buf;
115
116         if(po->nboff == 0 && po->buffer == po->tmpspace)
117                 return 0;
118
119         buf = po->buffer + (po->nboff >> 3);
120         /* Make sure we account for the last, partially filled */
121         if(po->nboff & 0x07) {
122                 buf[0] &= 0xff << (8 - (po->nboff & 0x07));
123                 buf++;
124         }
125
126         return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
127 }