/src/rtpproxy/modules/acct_rtcp_hep/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 | | #if defined(RTPP_MODULE) |
36 | | #include "rtpp_module.h" |
37 | | #endif |
38 | | |
39 | | #include "rtpp_sbuf.h" |
40 | | |
41 | | int |
42 | | rtpp_sbuf_write(struct rtpp_sbuf *sbp, const char *format, ...) |
43 | 41.4k | { |
44 | 41.4k | va_list ap; |
45 | 41.4k | int rlen, len; |
46 | | |
47 | 41.4k | len = sbp->alen - RS_ULEN(sbp); |
48 | 41.4k | assert(len > 0); |
49 | 0 | va_start(ap, format); |
50 | 41.4k | rlen = vsnprintf(sbp->cp, len, format, ap); |
51 | 41.4k | va_end(ap); |
52 | 41.4k | if (rlen < 0) |
53 | 0 | return (SBW_ERR); |
54 | 41.4k | if (rlen >= len) { |
55 | 660 | sbp->cp[0] = '\0'; |
56 | 660 | return (SBW_SHRT); |
57 | 660 | } |
58 | 40.7k | sbp->cp += rlen; |
59 | 40.7k | return (SBW_OK); |
60 | 41.4k | } |
61 | | |
62 | | struct rtpp_sbuf * |
63 | | rtpp_sbuf_ctor(int ilen) |
64 | 375 | { |
65 | 375 | struct rtpp_sbuf *sbp; |
66 | | |
67 | 375 | sbp = malloc(sizeof(struct rtpp_sbuf)); |
68 | 375 | if (sbp == NULL) |
69 | 0 | return (NULL); |
70 | 375 | memset(sbp, '\0', sizeof(struct rtpp_sbuf)); |
71 | 375 | sbp->bp = sbp->cp = malloc(ilen); |
72 | 375 | if (sbp->bp == NULL) { |
73 | 0 | free(sbp); |
74 | 0 | return (NULL); |
75 | 0 | } |
76 | 375 | sbp->cp[0] = '\0'; |
77 | 375 | sbp->alen = ilen; |
78 | 375 | return(sbp); |
79 | 375 | } |
80 | | |
81 | | void |
82 | | rtpp_sbuf_dtor(struct rtpp_sbuf *sbp) |
83 | 375 | { |
84 | | |
85 | 375 | free(sbp->bp); |
86 | 375 | free(sbp); |
87 | 375 | } |
88 | | |
89 | | int |
90 | | rtpp_sbuf_extend(struct rtpp_sbuf *sbp, int nlen) |
91 | 660 | { |
92 | 660 | void *nbp, *ncp; |
93 | | |
94 | 660 | assert(nlen > sbp->alen); |
95 | 0 | nbp = realloc(sbp->bp, nlen); |
96 | 660 | if (nbp == NULL) |
97 | 0 | return (-1); |
98 | 660 | sbp->alen = nlen; |
99 | 660 | if (sbp->bp != nbp) { |
100 | 301 | ncp = nbp + RS_ULEN(sbp); |
101 | 301 | sbp->cp = ncp; |
102 | 301 | sbp->bp = nbp; |
103 | 301 | } |
104 | 660 | return (0); |
105 | 660 | } |
106 | | |
107 | | #if defined(rtpp_sbuf_selftest) |
108 | | #include <stdint.h> |
109 | | #include "rtpp_memdeb_internal.h" |
110 | | #include "libexecinfo/stacktraverse.h" |
111 | | #include "libexecinfo/execinfo.h" |
112 | | |
113 | | #include "config_pp.h" |
114 | | |
115 | | #if !defined(NO_ERR_H) |
116 | | #include <err.h> |
117 | | #include "rtpp_util.h" |
118 | | #else |
119 | | #include "rtpp_util.h" |
120 | | #endif |
121 | | |
122 | | #define errx_ifnot(expr) \ |
123 | | if (!(expr)) \ |
124 | | errx(1, "`%s` check has failed in %s() at %s:%d", #expr, __func__, \ |
125 | | __FILE__, __LINE__); |
126 | | |
127 | | |
128 | | RTPP_MEMDEB_APP_STATIC; |
129 | | |
130 | | int |
131 | | rtpp_sbuf_selftest(void) |
132 | | { |
133 | | struct rtpp_sbuf *sbp; |
134 | | int rval; |
135 | | const char *longtest = "INFO:GLOBAL:rtpp_proc_async_run: ncycles=2600 load=0.068641"; |
136 | | |
137 | | RTPP_MEMDEB_APP_INIT(); |
138 | | |
139 | | sbp = rtpp_sbuf_ctor(6); |
140 | | errx_ifnot(sbp != NULL); |
141 | | errx_ifnot(sbp->alen == 6); |
142 | | errx_ifnot(sbp->cp == sbp->bp); |
143 | | errx_ifnot(RS_ULEN(sbp) == 0); |
144 | | rval = rtpp_sbuf_write(sbp, "%d", 12345); |
145 | | errx_ifnot(rval == SBW_OK); |
146 | | errx_ifnot(sbp->cp[0] == '\0'); |
147 | | errx_ifnot(strcmp(sbp->bp, "12345") == 0); |
148 | | errx_ifnot(RS_ULEN(sbp) == 5); |
149 | | rval = rtpp_sbuf_write(sbp, "%s", "F"); |
150 | | errx_ifnot(rval == SBW_SHRT); |
151 | | errx_ifnot(sbp->cp[0] == '\0'); |
152 | | errx_ifnot(strcmp(sbp->bp, "12345") == 0); |
153 | | errx_ifnot(RS_ULEN(sbp) == 5); |
154 | | errx_ifnot(rtpp_sbuf_extend(sbp, 7) == 0); |
155 | | errx_ifnot(RS_ULEN(sbp) == 5); |
156 | | errx_ifnot(strcmp(sbp->bp, "12345") == 0); |
157 | | rval = rtpp_sbuf_write(sbp, "%s", "F"); |
158 | | errx_ifnot(rval == SBW_OK); |
159 | | errx_ifnot(RS_ULEN(sbp) == 6); |
160 | | errx_ifnot(strcmp(sbp->bp, "12345F") == 0); |
161 | | do { |
162 | | errx_ifnot(rtpp_sbuf_extend(sbp, sbp->alen + 1) == 0); |
163 | | 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", |
164 | | longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, |
165 | | longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest, longtest); |
166 | | } while (rval == SBW_SHRT); |
167 | | errx_ifnot(rval == SBW_OK); |
168 | | errx_ifnot(RS_ULEN(sbp) == 1446); |
169 | | errx_ifnot(sbp->alen == RS_ULEN(sbp) + 1); |
170 | | errx_ifnot(strncmp(sbp->bp, "12345F", 6) == 0); |
171 | | rval = RS_ULEN(sbp); |
172 | | rtpp_sbuf_reset(sbp); |
173 | | errx_ifnot(sbp->cp == sbp->bp); |
174 | | errx_ifnot(sbp->cp[0] == '\0'); |
175 | | errx_ifnot(sbp->alen == rval + 1); |
176 | | rtpp_sbuf_dtor(sbp); |
177 | | |
178 | | rval = rtpp_memdeb_dumpstats(MEMDEB_SYM, 0); |
179 | | return (rval); |
180 | | } |
181 | | #endif /* rtpp_sbuf_selftest */ |
182 | | |
183 | | void |
184 | | rtpp_sbuf_reset(struct rtpp_sbuf *sbp) |
185 | 0 | { |
186 | |
|
187 | 0 | sbp->cp = sbp->bp; |
188 | 0 | sbp->cp[0] = '\0'; |
189 | 0 | } |