Fixing memory leak in python support function
[ric-plt/lib/rmr.git] / src / rmr / common / src / wrapper.c
1 // :vi sw=4 ts=4 noet:
2 /*
3 ==================================================================================
4         Copyright (c) 2019 Nokia
5         Copyright (c) 2018-2019 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11            http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22         Mnemonic:       wrapper.c
23         Abstract:       Functions that may make life easier for wrappers written on top
24                                 of the library which don't have access to header files.
25         Author:         E. Scott Daniels
26         Date:           3 May 2019
27 */
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 #include "../include/rmr.h"
34
35 #define ADD_SEP         1
36 #define NO_SEP          0
37
38 /*
39         Build a "name": value  sequence with an optional trailing
40         seperator. Caller must free the buffer after use.
41 */
42 static char* build_ival( char* name, int val, int add_sep ) {
43         char    wbuf[512];
44
45         snprintf( wbuf, sizeof( wbuf ), "\"%s\": %d%s", name, val, add_sep ? "," : "" );
46         return strdup( wbuf );
47 }
48
49 /*
50         Builds a "name": "string" sequence with an optional trailing
51         separator. Caller must free resulting string.
52 */
53 static char* build_sval( char* name, char* val, int add_sep ) {
54         char    wbuf[512];
55
56         snprintf( wbuf, sizeof( wbuf ), "\"%s\": \"%s\"%s", name, val, add_sep ? "," : "" );
57         return strdup( wbuf );
58 }
59
60
61 /*
62         Similar to strcat, bangs src onto the end of target, but UNLIKE
63         strcat src is freed as a convenience. Max is the max amount
64         that target can accept; we don't bang on if src len is
65         larger than max.  Return is the size of src; 0 if the
66         target was not modified.
67
68         Source is ALWAYS freed!
69 */
70 static int bang_on( char* target, char* src, int max ) {
71         int             len;
72         int             rc = 0;         // return code, assume error
73
74         if( src && target ) {
75                 len = strlen( src );
76                 if( (rc = len <= max ? len : 0 ) > 0 ) {        // if it fits, add it.
77                         strncat( target, src, len );
78                 }
79         }
80
81         if( src ) {
82                 free( src );
83         }
84         return rc;
85 }
86
87 /*
88         Frees the string that was allocated and returned using rmr_get_consts()
89 */
90 extern void rmr_free_consts( char* p) {
91         free(p);
92 }
93
94 /*
95         Returns a set of json with the constants which are set in the header.
96         Caller must free the returned string using rmr_free_consts()
97 */
98 extern char* rmr_get_consts( ) {
99         int             remain;                         // bytes remaining in wbuf
100         int             slen = 0;                       // length added
101         char    wbuf[2048];
102         char*   phrase;
103
104         snprintf( wbuf, sizeof( wbuf ), "{ " );
105         remain = sizeof( wbuf ) - strlen( wbuf ) -10;   // reserve some bytes allowing us to close the json
106
107         phrase = build_ival( "RMR_MAX_XID",       RMR_MAX_XID, ADD_SEP );
108         remain -= bang_on( wbuf, phrase, remain );
109         phrase = build_ival( "RMR_MAX_SID",       RMR_MAX_SID, ADD_SEP );
110         remain -= bang_on( wbuf, phrase, remain );
111         phrase = build_ival( "RMR_MAX_MEID",      RMR_MAX_MEID, ADD_SEP );
112         remain -= bang_on( wbuf, phrase, remain );
113         phrase = build_ival( "RMR_MAX_SRC",       RMR_MAX_SRC, ADD_SEP );
114         remain -= bang_on( wbuf, phrase, remain );
115         phrase = build_ival( "RMR_MAX_RCV_BYTES", RMR_MAX_RCV_BYTES, ADD_SEP );
116         remain -= bang_on( wbuf, phrase, remain );
117
118         phrase = build_ival( "RMRFL_NONE",        RMRFL_NONE, ADD_SEP );
119         remain -= bang_on( wbuf, phrase, remain );
120         phrase = build_ival( "RMRFL_AUTO_ALLOC",  RMRFL_AUTO_ALLOC, ADD_SEP );
121         remain -= bang_on( wbuf, phrase, remain );
122         phrase = build_ival( "RMRFL_MTCALL",      RMRFL_MTCALL, ADD_SEP );
123         remain -= bang_on( wbuf, phrase, remain );
124
125         phrase = build_ival( "RMR_DEF_SIZE",      RMR_DEF_SIZE, ADD_SEP );
126         remain -= bang_on( wbuf, phrase, remain );
127
128         phrase = build_ival( "RMR_VOID_MSGTYPE",  RMR_VOID_MSGTYPE, ADD_SEP );
129         remain -= bang_on( wbuf, phrase, remain );
130         phrase = build_ival( "RMR_VOID_SUBID",    RMR_VOID_SUBID , ADD_SEP );
131         remain -= bang_on( wbuf, phrase, remain );
132
133         phrase = build_ival( "RMR_OK",            RMR_OK, ADD_SEP );
134         remain -= bang_on( wbuf, phrase, remain );
135         phrase = build_ival( "RMR_ERR_BADARG",    RMR_ERR_BADARG, ADD_SEP );
136         remain -= bang_on( wbuf, phrase, remain );
137         phrase = build_ival( "RMR_ERR_NOENDPT",   RMR_ERR_NOENDPT, ADD_SEP );
138         remain -= bang_on( wbuf, phrase, remain );
139         phrase = build_ival( "RMR_ERR_EMPTY",     RMR_ERR_EMPTY, ADD_SEP );
140         remain -= bang_on( wbuf, phrase, remain );
141         phrase = build_ival( "RMR_ERR_NOHDR",     RMR_ERR_NOHDR, ADD_SEP );
142         remain -= bang_on( wbuf, phrase, remain );
143         phrase = build_ival( "RMR_ERR_SENDFAILED", RMR_ERR_SENDFAILED, ADD_SEP );
144         remain -= bang_on( wbuf, phrase, remain );
145         phrase = build_ival( "RMR_ERR_CALLFAILED", RMR_ERR_CALLFAILED, ADD_SEP );
146         remain -= bang_on( wbuf, phrase, remain );
147         phrase = build_ival( "RMR_ERR_NOWHOPEN",  RMR_ERR_NOWHOPEN, ADD_SEP );
148         remain -= bang_on( wbuf, phrase, remain );
149         phrase = build_ival( "RMR_ERR_WHID",      RMR_ERR_WHID, ADD_SEP );
150         remain -= bang_on( wbuf, phrase, remain );
151         phrase = build_ival( "RMR_ERR_OVERFLOW",  RMR_ERR_OVERFLOW, ADD_SEP );
152         remain -= bang_on( wbuf, phrase, remain );
153         phrase = build_ival( "RMR_ERR_RETRY",     RMR_ERR_RETRY, ADD_SEP );
154         remain -= bang_on( wbuf, phrase, remain );
155         phrase = build_ival( "RMR_ERR_RCVFAILED", RMR_ERR_RCVFAILED, ADD_SEP );
156         remain -= bang_on( wbuf, phrase, remain );
157         phrase = build_ival( "RMR_ERR_TIMEOUT",   RMR_ERR_TIMEOUT, ADD_SEP );
158         remain -= bang_on( wbuf, phrase, remain );
159         phrase = build_ival( "RMR_ERR_UNSET",     RMR_ERR_UNSET, ADD_SEP );
160         remain -= bang_on( wbuf, phrase, remain );
161         phrase = build_ival( "RMR_ERR_TRUNC",     RMR_ERR_TRUNC, ADD_SEP );
162         remain -= bang_on( wbuf, phrase, remain );
163         phrase = build_ival( "RMR_ERR_INITFAILED", RMR_ERR_INITFAILED, NO_SEP );
164         remain -= bang_on( wbuf, phrase, remain );
165
166         strncat( wbuf, " }", remain );
167         return strdup( wbuf );                  // chop unused space and return
168 }