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