/src/rtpproxy/src/rtpp_sbuf.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2018 Sippy Software, Inc., http://www.sippysoft.com |
3 | | * All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions |
7 | | * are met: |
8 | | * 1. Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * 2. Redistributions in binary form must reproduce the above copyright |
11 | | * notice, this list of conditions and the following disclaimer in the |
12 | | * documentation and/or other materials provided with the distribution. |
13 | | * |
14 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
15 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
17 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
18 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
19 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
20 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
21 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
22 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
23 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
24 | | * SUCH DAMAGE. |
25 | | * |
26 | | */ |
27 | | |
28 | | #include <assert.h> |
29 | | #include <stdarg.h> |
30 | | #include <stdbool.h> |
31 | | #include <stdio.h> |
32 | | #include <stdlib.h> |
33 | | #include <string.h> |
34 | | |
35 | | #include "rtpp_types.h" |
36 | | #include "rtpp_codeptr.h" |
37 | | #include "rtpp_mallocs.h" |
38 | | #include "rtpp_sbuf.h" |
39 | | |
40 | | int |
41 | | rtpp_sbuf_write(struct rtpp_sbuf *sbp, const char *format, ...) |
42 | 22.9k | { |
43 | 22.9k | va_list ap; |
44 | 22.9k | int rlen, len; |
45 | | |
46 | 22.9k | len = sbp->alen - RS_ULEN(sbp); |
47 | 22.9k | assert(len > 0); |
48 | 22.9k | va_start(ap, format); |
49 | 22.9k | rlen = vsnprintf(sbp->cp, len, format, ap); |
50 | 22.9k | va_end(ap); |
51 | 22.9k | if (rlen < 0) |
52 | 0 | return (SBW_ERR); |
53 | 22.9k | if (rlen >= len) { |
54 | 0 | sbp->cp[0] = '\0'; |
55 | 0 | return (SBW_SHRT); |
56 | 0 | } |
57 | 22.9k | sbp->cp += rlen; |
58 | 22.9k | return (SBW_OK); |
59 | 22.9k | } |
60 | | |
61 | | struct rtpp_sbuf * |
62 | | #if !defined(RTPP_CHECK_LEAKS) |
63 | | rtpp_sbuf_ctor(int ilen) |
64 | | #else |
65 | | _rtpp_sbuf_ctor(int ilen, void *memdeb_p, const struct rtpp_codeptr *mlp) |
66 | | #endif |
67 | 2 | { |
68 | 2 | struct rtpp_sbuf *sbp; |
69 | | |
70 | 2 | #if !defined(RTPP_CHECK_LEAKS) |
71 | 2 | sbp = malloc(sizeof(struct rtpp_sbuf)); |
72 | | #else |
73 | | sbp = rtpp_memdeb_malloc(sizeof(struct rtpp_sbuf), memdeb_p, mlp); |
74 | | #endif |
75 | 2 | if (sbp == NULL) |
76 | 0 | return (NULL); |
77 | 2 | memset(sbp, '\0', sizeof(struct rtpp_sbuf)); |
78 | 2 | #if !defined(RTPP_CHECK_LEAKS) |
79 | 2 | sbp->bp = sbp->cp = malloc(ilen); |
80 | | #else |
81 | | sbp->bp = sbp->cp = rtpp_memdeb_malloc(ilen, memdeb_p, mlp); |
82 | | #endif |
83 | 2 | if (sbp->bp == NULL) { |
84 | 0 | free(sbp); |
85 | 0 | return (NULL); |
86 | 0 | } |
87 | 2 | sbp->cp[0] = '\0'; |
88 | 2 | sbp->alen = ilen; |
89 | 2 | return(sbp); |
90 | 2 | } |
91 | | |
92 | | void |
93 | | #if !defined(RTPP_CHECK_LEAKS) |
94 | | rtpp_sbuf_dtor(struct rtpp_sbuf *sbp) |
95 | | #else |
96 | | _rtpp_sbuf_dtor(struct rtpp_sbuf *sbp, void *memdeb_p, const struct rtpp_codeptr *mlp) |
97 | | #endif |
98 | 2 | { |
99 | | |
100 | 2 | #if !defined(RTPP_CHECK_LEAKS) |
101 | 2 | free(sbp->bp); |
102 | 2 | free(sbp); |
103 | | #else |
104 | | rtpp_memdeb_free(sbp->bp, memdeb_p, mlp); |
105 | | rtpp_memdeb_free(sbp, memdeb_p, mlp); |
106 | | #endif |
107 | 2 | } |
108 | | |
109 | | int |
110 | | #if !defined(RTPP_CHECK_LEAKS) |
111 | | rtpp_sbuf_extend(struct rtpp_sbuf *sbp, int nlen) |
112 | | #else |
113 | | _rtpp_sbuf_extend(struct rtpp_sbuf *sbp, int nlen, void *memdeb_p, const struct rtpp_codeptr *mlp) |
114 | | #endif |
115 | 0 | { |
116 | 0 | void *nbp, *ncp; |
117 | |
|
118 | 0 | assert(nlen > sbp->alen); |
119 | 0 | #if !defined(RTPP_CHECK_LEAKS) |
120 | 0 | nbp = realloc(sbp->bp, nlen); |
121 | | #else |
122 | | nbp = rtpp_memdeb_realloc(sbp->bp, nlen, memdeb_p, mlp); |
123 | | #endif |
124 | 0 | if (nbp == NULL) |
125 | 0 | return (-1); |
126 | 0 | sbp->alen = nlen; |
127 | 0 | if (sbp->bp != nbp) { |
128 | 0 | ncp = nbp + RS_ULEN(sbp); |
129 | 0 | sbp->cp = ncp; |
130 | 0 | sbp->bp = nbp; |
131 | 0 | } |
132 | 0 | return (0); |
133 | 0 | } |
134 | | |
135 | | #if defined(rtpp_sbuf_selftest) |
136 | | #include <stdint.h> |
137 | | #include "rtpp_memdeb_internal.h" |
138 | | #include "libexecinfo/stacktraverse.h" |
139 | | #include "libexecinfo/execinfo.h" |
140 | | |
141 | | #include "config_pp.h" |
142 | | |
143 | | #if !defined(NO_ERR_H) |
144 | | #include <err.h> |
145 | | #include "rtpp_util.h" |
146 | | #else |
147 | | #include "rtpp_util.h" |
148 | | #endif |
149 | | |
150 | | #define errx_ifnot(expr) \ |
151 | | if (!(expr)) \ |
152 | | errx(1, "`%s` check has failed in %s() at %s:%d", #expr, __func__, \ |
153 | | __FILE__, __LINE__); |
154 | | |
155 | | |
156 | | RTPP_MEMDEB_APP_STATIC; |
157 | | |
158 | | int |
159 | | rtpp_sbuf_selftest(void) |
160 | | { |
161 | | struct rtpp_sbuf *sbp; |
162 | | int rval; |
163 | | const char *longtest = "INFO:GLOBAL:rtpp_proc_async_run: ncycles=2600 load=0.068641"; |
164 | | |
165 | | RTPP_MEMDEB_APP_INIT(); |
166 | | |
167 | | sbp = rtpp_sbuf_ctor(6); |
168 | | errx_ifnot(sbp != NULL); |
169 | | errx_ifnot(sbp->alen == 6); |
170 | | errx_ifnot(sbp->cp == sbp->bp); |
171 | | errx_ifnot(RS_ULEN(sbp) == 0); |
172 | | rval = rtpp_sbuf_write(sbp, "%d", 12345); |
173 | | errx_ifnot(rval == SBW_OK); |
174 | | errx_ifnot(sbp->cp[0] == '\0'); |
175 | | errx_ifnot(strcmp(sbp->bp, "12345") == 0); |
176 | | errx_ifnot(RS_ULEN(sbp) == 5); |
177 | | rval = rtpp_sbuf_write(sbp, "%s", "F"); |
178 | | errx_ifnot(rval == SBW_SHRT); |
179 | | errx_ifnot(sbp->cp[0] == '\0'); |
180 | | errx_ifnot(strcmp(sbp->bp, "12345") == 0); |
181 | | errx_ifnot(RS_ULEN(sbp) == 5); |
182 | | errx_ifnot(rtpp_sbuf_extend(sbp, 7) == 0); |
183 | | errx_ifnot(RS_ULEN(sbp) == 5); |
184 | | errx_ifnot(strcmp(sbp->bp, "12345") == 0); |
185 | | rval = rtpp_sbuf_write(sbp, "%s", "F"); |
186 | | errx_ifnot(rval == SBW_OK); |
187 | | errx_ifnot(RS_ULEN(sbp) == 6); |
188 | | errx_ifnot(strcmp(sbp->bp, "12345F") == 0); |
189 | | do { |
190 | | errx_ifnot(rtpp_sbuf_extend(sbp, sbp->alen + 1) == 0); |
191 | | rval = rtpp_sbuf_write(sbp, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", |
192 | | longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, |
193 | | longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest); |
194 | | } while (rval == SBW_SHRT); |
195 | | errx_ifnot(rval == SBW_OK); |
196 | | errx_ifnot(RS_ULEN(sbp) == 1446); |
197 | | errx_ifnot(sbp->alen == RS_ULEN(sbp) + 1); |
198 | | errx_ifnot(strncmp(sbp->bp, "12345F", 6) == 0); |
199 | | rval = RS_ULEN(sbp); |
200 | | rtpp_sbuf_reset(sbp); |
201 | | errx_ifnot(sbp->cp == sbp->bp); |
202 | | errx_ifnot(sbp->cp[0] == '\0'); |
203 | | errx_ifnot(sbp->alen == rval + 1); |
204 | | rtpp_sbuf_dtor(sbp); |
205 | | |
206 | | rval = rtpp_memdeb_dumpstats(MEMDEB_SYM, 0); |
207 | | return (rval); |
208 | | } |
209 | | #endif /* rtpp_sbuf_selftest */ |
210 | | |
211 | | void |
212 | | rtpp_sbuf_reset(struct rtpp_sbuf *sbp) |
213 | 22.9k | { |
214 | | |
215 | 22.9k | sbp->cp = sbp->bp; |
216 | 22.9k | sbp->cp[0] = '\0'; |
217 | 22.9k | } |