b6ac8b2cc7665fe38dd5eb9c0192b95f8fec8a2e
[ric-plt/resource-status-manager.git] / RSM / 3rdparty / asn1codec / e2ap_engine / constraints.c
1 /*
2  * Copyright 2019 AT&T Intellectual Property
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20  */
21
22
23
24 #include <asn_internal.h>
25 #include <constraints.h>
26
27 int
28 asn_generic_no_constraint(const asn_TYPE_descriptor_t *type_descriptor,
29                           const void *struct_ptr,
30                           asn_app_constraint_failed_f *cb, void *key) {
31     (void)type_descriptor;      /* Unused argument */
32         (void)struct_ptr;       /* Unused argument */
33         (void)cb;       /* Unused argument */
34         (void)key;      /* Unused argument */
35
36         /* Nothing to check */
37         return 0;
38 }
39
40 int
41 asn_generic_unknown_constraint(const asn_TYPE_descriptor_t *type_descriptor,
42                                const void *struct_ptr,
43                                asn_app_constraint_failed_f *cb, void *key) {
44     (void)type_descriptor;      /* Unused argument */
45         (void)struct_ptr;       /* Unused argument */
46         (void)cb;       /* Unused argument */
47         (void)key;      /* Unused argument */
48
49         /* Unknown how to check */
50         return 0;
51 }
52
53 struct errbufDesc {
54     const asn_TYPE_descriptor_t *failed_type;
55     const void *failed_struct_ptr;
56         char *errbuf;
57         size_t errlen;
58 };
59
60 static void
61 _asn_i_ctfailcb(void *key, const asn_TYPE_descriptor_t *td, const void *sptr,
62                 const char *fmt, ...) {
63     struct errbufDesc *arg = key;
64         va_list ap;
65         ssize_t vlen;
66         ssize_t maxlen;
67
68         arg->failed_type = td;
69         arg->failed_struct_ptr = sptr;
70
71         maxlen = arg->errlen;
72         if(maxlen <= 0)
73                 return;
74
75         va_start(ap, fmt);
76         vlen = vsnprintf(arg->errbuf, maxlen, fmt, ap);
77         va_end(ap);
78         if(vlen >= maxlen) {
79                 arg->errbuf[maxlen-1] = '\0';   /* Ensuring libc correctness */
80                 arg->errlen = maxlen - 1;       /* Not counting termination */
81                 return;
82         } else if(vlen >= 0) {
83                 arg->errbuf[vlen] = '\0';       /* Ensuring libc correctness */
84                 arg->errlen = vlen;             /* Not counting termination */
85         } else {
86                 /*
87                  * The libc on this system is broken.
88                  */
89                 vlen = sizeof("<broken vsnprintf>") - 1;
90                 maxlen--;
91                 arg->errlen = vlen < maxlen ? vlen : maxlen;
92                 memcpy(arg->errbuf, "<broken vsnprintf>", arg->errlen);
93                 arg->errbuf[arg->errlen] = 0;
94         }
95
96         return;
97 }
98
99 int
100 asn_check_constraints(const asn_TYPE_descriptor_t *type_descriptor,
101                       const void *struct_ptr, char *errbuf, size_t *errlen) {
102     struct errbufDesc arg;
103     int ret;
104
105     arg.failed_type = 0;
106     arg.failed_struct_ptr = 0;
107     arg.errbuf = errbuf;
108     arg.errlen = errlen ? *errlen : 0;
109
110     ret = type_descriptor->encoding_constraints.general_constraints(
111         type_descriptor, struct_ptr, _asn_i_ctfailcb, &arg);
112     if(ret == -1 && errlen) *errlen = arg.errlen;
113
114     return ret;
115 }
116