Adding new comments for Oran in all files with licenses
[ric-plt/resource-status-manager.git] / RSM / 3rdparty / asn1codec / e2ap_engine / asn_internal.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
26 ssize_t
27 asn__format_to_callback(int (*cb)(const void *, size_t, void *key), void *key,
28                         const char *fmt, ...) {
29     char scratch[64];
30     char *buf = scratch;
31     size_t buf_size = sizeof(scratch);
32     int wrote;
33     int cb_ret;
34
35     do {
36         va_list args;
37         va_start(args, fmt);
38
39         wrote = vsnprintf(buf, buf_size, fmt, args);
40         if(wrote < (ssize_t)buf_size) {
41             if(wrote < 0) {
42                 if(buf != scratch) FREEMEM(buf);
43                 va_end(args);
44                 return -1;
45             }
46             break;
47         }
48
49         buf_size <<= 1;
50         if(buf == scratch) {
51             buf = MALLOC(buf_size);
52             if(!buf) return -1;
53         } else {
54             void *p = REALLOC(buf, buf_size);
55             if(!p) {
56                 FREEMEM(buf);
57                 return -1;
58             }
59             buf = p;
60         }
61     } while(1);
62
63     cb_ret = cb(buf, wrote, key);
64     if(buf != scratch) FREEMEM(buf);
65     if(cb_ret < 0) {
66         return -1;
67     }
68
69     return wrote;
70 }
71