/src/open5gs/lib/asn1c/common/constraints.c
Line | Count | Source |
1 | | #include <asn_internal.h> |
2 | | #include <constraints.h> |
3 | | |
4 | | int |
5 | | asn_generic_no_constraint(const asn_TYPE_descriptor_t *type_descriptor, |
6 | | const void *struct_ptr, |
7 | 0 | asn_app_constraint_failed_f *cb, void *key) { |
8 | 0 | (void)type_descriptor; /* Unused argument */ |
9 | 0 | (void)struct_ptr; /* Unused argument */ |
10 | 0 | (void)cb; /* Unused argument */ |
11 | 0 | (void)key; /* Unused argument */ |
12 | | |
13 | | /* Nothing to check */ |
14 | 0 | return 0; |
15 | 0 | } |
16 | | |
17 | | int |
18 | | asn_generic_unknown_constraint(const asn_TYPE_descriptor_t *type_descriptor, |
19 | | const void *struct_ptr, |
20 | 0 | asn_app_constraint_failed_f *cb, void *key) { |
21 | 0 | (void)type_descriptor; /* Unused argument */ |
22 | 0 | (void)struct_ptr; /* Unused argument */ |
23 | 0 | (void)cb; /* Unused argument */ |
24 | 0 | (void)key; /* Unused argument */ |
25 | | |
26 | | /* Unknown how to check */ |
27 | 0 | return 0; |
28 | 0 | } |
29 | | |
30 | | struct errbufDesc { |
31 | | const asn_TYPE_descriptor_t *failed_type; |
32 | | const void *failed_struct_ptr; |
33 | | char *errbuf; |
34 | | size_t errlen; |
35 | | }; |
36 | | |
37 | | static void |
38 | | CC_PRINTFLIKE(4, 5) |
39 | | _asn_i_ctfailcb(void *key, const asn_TYPE_descriptor_t *td, const void *sptr, |
40 | 0 | const char *fmt, ...) { |
41 | 0 | struct errbufDesc *arg = key; |
42 | 0 | va_list ap; |
43 | 0 | ssize_t vlen; |
44 | 0 | ssize_t maxlen; |
45 | |
|
46 | 0 | arg->failed_type = td; |
47 | 0 | arg->failed_struct_ptr = sptr; |
48 | |
|
49 | 0 | maxlen = arg->errlen; |
50 | 0 | if(maxlen <= 0) |
51 | 0 | return; |
52 | | |
53 | 0 | va_start(ap, fmt); |
54 | 0 | vlen = vsnprintf(arg->errbuf, maxlen, fmt, ap); |
55 | 0 | va_end(ap); |
56 | 0 | if(vlen >= maxlen) { |
57 | 0 | arg->errbuf[maxlen-1] = '\0'; /* Ensuring libc correctness */ |
58 | 0 | arg->errlen = maxlen - 1; /* Not counting termination */ |
59 | 0 | return; |
60 | 0 | } else if(vlen >= 0) { |
61 | 0 | arg->errbuf[vlen] = '\0'; /* Ensuring libc correctness */ |
62 | 0 | arg->errlen = vlen; /* Not counting termination */ |
63 | 0 | } else { |
64 | | /* |
65 | | * The libc on this system is broken. |
66 | | */ |
67 | 0 | vlen = sizeof("<broken vsnprintf>") - 1; |
68 | 0 | maxlen--; |
69 | 0 | arg->errlen = vlen < maxlen ? vlen : maxlen; |
70 | 0 | memcpy(arg->errbuf, "<broken vsnprintf>", arg->errlen); |
71 | 0 | arg->errbuf[arg->errlen] = 0; |
72 | 0 | } |
73 | | |
74 | 0 | return; |
75 | 0 | } |
76 | | |
77 | | int |
78 | | asn_check_constraints(const asn_TYPE_descriptor_t *type_descriptor, |
79 | 0 | const void *struct_ptr, char *errbuf, size_t *errlen) { |
80 | 0 | struct errbufDesc arg; |
81 | 0 | int ret; |
82 | |
|
83 | 0 | arg.failed_type = 0; |
84 | 0 | arg.failed_struct_ptr = 0; |
85 | 0 | arg.errbuf = errbuf; |
86 | 0 | arg.errlen = errlen ? *errlen : 0; |
87 | |
|
88 | 0 | ret = type_descriptor->encoding_constraints.general_constraints( |
89 | 0 | type_descriptor, struct_ptr, _asn_i_ctfailcb, &arg); |
90 | 0 | if(ret == -1 && errlen) *errlen = arg.errlen; |
91 | |
|
92 | 0 | return ret; |
93 | 0 | } |
94 | | |