Change version after creation of r2 branch
[ric-plt/resource-status-manager.git] / RSM / asn1codec / e2ap_engine / constraints.c
1 /*
2  *
3  * Copyright 2019 AT&T Intellectual Property
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
20 #include <asn_internal.h>
21 #include <constraints.h>
22
23 int
24 asn_generic_no_constraint(const asn_TYPE_descriptor_t *type_descriptor,
25                           const void *struct_ptr,
26                           asn_app_constraint_failed_f *cb, void *key) {
27     (void)type_descriptor;      /* Unused argument */
28         (void)struct_ptr;       /* Unused argument */
29         (void)cb;       /* Unused argument */
30         (void)key;      /* Unused argument */
31
32         /* Nothing to check */
33         return 0;
34 }
35
36 int
37 asn_generic_unknown_constraint(const asn_TYPE_descriptor_t *type_descriptor,
38                                const void *struct_ptr,
39                                asn_app_constraint_failed_f *cb, void *key) {
40     (void)type_descriptor;      /* Unused argument */
41         (void)struct_ptr;       /* Unused argument */
42         (void)cb;       /* Unused argument */
43         (void)key;      /* Unused argument */
44
45         /* Unknown how to check */
46         return 0;
47 }
48
49 struct errbufDesc {
50     const asn_TYPE_descriptor_t *failed_type;
51     const void *failed_struct_ptr;
52         char *errbuf;
53         size_t errlen;
54 };
55
56 static void
57 _asn_i_ctfailcb(void *key, const asn_TYPE_descriptor_t *td, const void *sptr,
58                 const char *fmt, ...) {
59     struct errbufDesc *arg = key;
60         va_list ap;
61         ssize_t vlen;
62         ssize_t maxlen;
63
64         arg->failed_type = td;
65         arg->failed_struct_ptr = sptr;
66
67         maxlen = arg->errlen;
68         if(maxlen <= 0)
69                 return;
70
71         va_start(ap, fmt);
72         vlen = vsnprintf(arg->errbuf, maxlen, fmt, ap);
73         va_end(ap);
74         if(vlen >= maxlen) {
75                 arg->errbuf[maxlen-1] = '\0';   /* Ensuring libc correctness */
76                 arg->errlen = maxlen - 1;       /* Not counting termination */
77                 return;
78         } else if(vlen >= 0) {
79                 arg->errbuf[vlen] = '\0';       /* Ensuring libc correctness */
80                 arg->errlen = vlen;             /* Not counting termination */
81         } else {
82                 /*
83                  * The libc on this system is broken.
84                  */
85                 vlen = sizeof("<broken vsnprintf>") - 1;
86                 maxlen--;
87                 arg->errlen = vlen < maxlen ? vlen : maxlen;
88                 memcpy(arg->errbuf, "<broken vsnprintf>", arg->errlen);
89                 arg->errbuf[arg->errlen] = 0;
90         }
91
92         return;
93 }
94
95 int
96 asn_check_constraints(const asn_TYPE_descriptor_t *type_descriptor,
97                       const void *struct_ptr, char *errbuf, size_t *errlen) {
98     struct errbufDesc arg;
99     int ret;
100
101     arg.failed_type = 0;
102     arg.failed_struct_ptr = 0;
103     arg.errbuf = errbuf;
104     arg.errlen = errlen ? *errlen : 0;
105
106     ret = type_descriptor->encoding_constraints.general_constraints(
107         type_descriptor, struct_ptr, _asn_i_ctfailcb, &arg);
108     if(ret == -1 && errlen) *errlen = arg.errlen;
109
110     return ret;
111 }
112