Sync from Azure to LF
[ric-plt/resource-status-manager.git] / RSM / asn1codec / src / asn1codec_utils.c
1 /*
2  *
3  * Copyright 2019 AT&T Intellectual Property
4  * Copyright 2019 Nokia
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23 #undef NDEBUG
24 #include <assert.h>
25 #include <asn1codec_utils.h>
26 #include <constr_TYPE.h>
27 #include <xer_encoder.h>
28
29 /*
30  * Printer for the e2ap pdu.
31  * The string representation of the pdu stored in buf.
32  *
33  * Input:
34  * pdu - the pdu to print.
35  * buf_size - the size of the storage buffer.
36  * buf - hold the string representation of the pdu.
37  */
38 bool
39 asn1_pdu_printer(E2AP_PDU_t const *pdu, size_t buf_size, char *buf)
40 {
41         bool rc = true;
42         char *bufloc = 0;
43         size_t sizeloc = 0;
44         buf[0] = 0;
45         FILE *stream = open_memstream(&bufloc, &sizeloc);
46
47         errno = 0;
48         if (asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu)){
49                 snprintf(buf, buf_size, "#%s.%s - Failed to print %s, error = %d ", __FILE__, __func__, asn_DEF_E2AP_PDU.name, errno);
50                 strerror_r(errno, buf+strlen(buf), buf_size - strlen(buf));
51                 rc = false;
52         } else {
53                 buf_size = buf_size > sizeloc ? sizeloc: buf_size -1;
54                 memcpy(buf, bufloc, buf_size);
55                 buf[buf_size] = 0;
56         }
57
58         fclose(stream);
59         free(bufloc);
60         return rc;
61 }
62
63
64 /*
65  * XML Printer for the e2ap pdu.
66  * The string representation of the pdu stored in buf.
67  *
68  * Input:
69  * pdu - the pdu to print.
70  * buf_size - the size of the storage buffer.
71  * buf - hold the string representation of the pdu.
72  */
73 bool
74 asn1_pdu_xer_printer(E2AP_PDU_t const *pdu, size_t buf_size, char *buf)
75 {
76         bool rc = true;
77         char *bufloc = 0;
78         size_t sizeloc = 0;
79         buf[0] = 0;
80         FILE *stream = open_memstream(&bufloc, &sizeloc);
81
82         errno = 0;
83         if (xer_fprint(stream, &asn_DEF_E2AP_PDU, pdu)){
84                 snprintf(buf, buf_size, "#%s.%s - Failed to print %s, error = %d ", __FILE__, __func__, asn_DEF_E2AP_PDU.name, errno);
85                 strerror_r(errno, buf+strlen(buf), buf_size - strlen(buf));
86                 rc = false;
87         } else {
88                 buf_size = buf_size > sizeloc ? sizeloc: buf_size -1;
89                 memcpy(buf, bufloc, buf_size);
90                 buf[buf_size] = 0;
91         }
92
93         fclose(stream);
94         free(bufloc);
95         return rc;
96 }
97
98 /*
99  * Unpack the pdu from ASN.1 PER encoding.
100  *
101  * Input:
102  * pdu - storage for unpacked pdu.
103  * packed_buf_size - size of the encoded data.
104  * packed_buf - storage of the packed pdu
105  * err_buf_size - size of the err_buf which may hold the error string in case of
106  * an error. err_buf - storage for the error string
107  *
108  * Return: true in case of success, false in case of failure.
109  */
110 bool
111 per_unpack_pdu(E2AP_PDU_t *pdu, size_t packed_buf_size, unsigned char* packed_buf,size_t err_buf_size, char* err_buf)
112 {
113         return unpack_pdu_aux(pdu, packed_buf_size, packed_buf,err_buf_size, err_buf,ATS_ALIGNED_BASIC_PER);
114 }
115
116 bool
117 unpack_pdu_aux(E2AP_PDU_t *pdu, size_t packed_buf_size, unsigned char* packed_buf,size_t err_buf_size, char* err_buf,enum asn_transfer_syntax syntax)
118 {
119         char spec[256];
120         size_t err_msg_size = err_buf_size;
121
122         //ATS_BASIC_XER ATS_ALIGNED_BASIC_PER, ATS_UNALIGNED_BASIC_PER,ATS_ALIGNED_CANONICAL_PER
123         errno = 0;
124         asn_dec_rval_t rval =
125         asn_decode(0,syntax , &asn_DEF_E2AP_PDU, (void **)&pdu, packed_buf, packed_buf_size);
126         switch(rval.code) {
127         case RC_OK:
128                 if (asn_check_constraints(&asn_DEF_E2AP_PDU, pdu,err_buf, &err_msg_size)){
129                         snprintf(spec, sizeof(spec), "#%s.%s - Constraint check failed: ", __FILE__, __func__);
130                         size_t spec_actual_size = strlen(spec);
131                         if (spec_actual_size + err_msg_size < err_buf_size){
132                                 memmove(err_buf + spec_actual_size, err_buf, err_msg_size + 1);
133                                 memcpy(err_buf, spec, spec_actual_size);
134                         }
135                         return false;
136                 }
137                 return true;
138
139         break;
140         case RC_WMORE:
141         case RC_FAIL:
142         default:
143                 break;
144         }
145
146         snprintf(err_buf, err_buf_size, "#%s.%s - Failed to decode %s (consumed %zu), error = %d ", __FILE__, __func__, asn_DEF_E2AP_PDU.name, rval.consumed, errno);
147         strerror_r(errno, err_buf+strlen(err_buf), err_buf_size - strlen(err_buf));
148         return false;
149 }
150
151 /*
152  * Pack the pdu using ASN.1 PER encoding.
153  *
154  * Input:
155  * pdu - the pdu to pack.
156  * packed_buf_size - in: size of packed_buf; out: number of chars used.
157  * packed_buf - storage for the packed pdu
158  * err_buf_size - size of the err_buf which may hold the error string in case of
159  * an error. err_buf - storage for the error string
160  *
161  * Return: true in case of success, false in case of failure.
162  */
163 bool
164 per_pack_pdu(E2AP_PDU_t *pdu, size_t *packed_buf_size, unsigned char* packed_buf,size_t err_buf_size, char* err_buf)
165 {
166         return pack_pdu_aux(pdu, packed_buf_size, packed_buf,err_buf_size, err_buf,ATS_ALIGNED_BASIC_PER);
167 }
168
169 bool
170 pack_pdu_aux(E2AP_PDU_t *pdu, size_t *packed_buf_size, unsigned char* packed_buf,size_t err_buf_size, char* err_buf,enum asn_transfer_syntax syntax)
171 {
172         char spec[256];
173         size_t err_msg_size = err_buf_size;
174
175     if (asn_check_constraints(&asn_DEF_E2AP_PDU, pdu,err_buf, &err_msg_size)){
176                 snprintf(spec, sizeof(spec), "#%s.%s - Constraint check failed: ", __FILE__, __func__);
177                 size_t spec_actual_size = strlen(spec);
178                 if (spec_actual_size + err_msg_size < err_buf_size){
179                         memmove(err_buf + spec_actual_size, err_buf, err_msg_size + 1);
180                         memcpy(err_buf, spec, spec_actual_size);
181                 }
182         return false;
183     }
184
185         errno = 0;
186 asn_enc_rval_t res =
187                 asn_encode_to_buffer(0, syntax, &asn_DEF_E2AP_PDU, pdu, packed_buf, *packed_buf_size);
188         if(res.encoded == -1) {
189                 snprintf(err_buf, err_buf_size, "#%s.%s - Failed to encode %s, error = %d ", __FILE__, __func__, asn_DEF_E2AP_PDU.name, errno);
190                 strerror_r(errno, err_buf+strlen(err_buf), err_buf_size - strlen(err_buf));
191                 return false;
192         } else {
193                 /* Encoded successfully. */
194                 if (*packed_buf_size < res.encoded){
195                         snprintf(err_buf, err_buf_size, "#%s.%s - Encoded output of %s, is too big:%zu", __FILE__, __func__, asn_DEF_E2AP_PDU.name,res.encoded);
196                         return false;
197                 } else {
198                         *packed_buf_size = res.encoded;
199                 }
200         }
201         return true;
202 }
203
204 /*
205  * Create a new pdu.
206  * Abort the process on allocation failure.
207  */
208 E2AP_PDU_t *new_pdu()
209 {
210         E2AP_PDU_t *pdu = calloc(1, sizeof(E2AP_PDU_t));
211         assert(pdu != 0);
212         return pdu;
213 }
214
215 void delete_pdu(E2AP_PDU_t *pdu)
216 {
217         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
218 }
219