/src/rtpproxy/src/rtpp_wi_data.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2014-2019 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 <string.h> |
30 | | #include <stddef.h> |
31 | | #include <stdlib.h> |
32 | | |
33 | | #include "rtpp_types.h" |
34 | | #include "rtpp_debug.h" |
35 | | #include "rtpp_mallocs.h" |
36 | | #include "rtpp_refcnt.h" |
37 | | #include "rtpp_wi.h" |
38 | | #include "rtpp_wi_data.h" |
39 | | |
40 | | struct rtpp_wi_data { |
41 | | struct rtpp_wi pub; |
42 | | size_t data_len; |
43 | | char data[0]; |
44 | | }; |
45 | | |
46 | | struct rtpp_wi * |
47 | | rtpp_wi_malloc_data(void *dataptr, size_t datalen) |
48 | 0 | { |
49 | 0 | struct rtpp_wi_data *wipp; |
50 | |
|
51 | 0 | wipp = rtpp_rmalloc(sizeof(struct rtpp_wi_data) + datalen, PVT_RCOFFS(wipp)); |
52 | 0 | if (wipp == NULL) { |
53 | 0 | return (NULL); |
54 | 0 | } |
55 | 0 | *wipp = (const struct rtpp_wi_data) { |
56 | 0 | .pub.wi_type = RTPP_WI_TYPE_DATA, |
57 | 0 | .pub.rcnt = wipp->pub.rcnt |
58 | 0 | }; |
59 | 0 | if (datalen > 0) { |
60 | 0 | wipp->data_len = datalen; |
61 | 0 | memcpy(wipp->data, dataptr, datalen); |
62 | 0 | } |
63 | 0 | return (&(wipp->pub)); |
64 | 0 | } |
65 | | |
66 | | struct rtpp_wi * |
67 | | rtpp_wi_malloc_udata(void **dataptr, size_t datalen) |
68 | 342k | { |
69 | 342k | struct rtpp_wi_data *wipp; |
70 | | |
71 | 342k | wipp = rtpp_rmalloc(sizeof(struct rtpp_wi_data) + datalen, PVT_RCOFFS(wipp)); |
72 | 342k | if (wipp == NULL) { |
73 | 0 | return (NULL); |
74 | 0 | } |
75 | 342k | *wipp = (const struct rtpp_wi_data) { |
76 | 342k | .pub.wi_type = RTPP_WI_TYPE_DATA, |
77 | 342k | .pub.rcnt = wipp->pub.rcnt |
78 | 342k | }; |
79 | 342k | if (datalen > 0) { |
80 | 342k | wipp->data_len = datalen; |
81 | 342k | *dataptr = wipp->data; |
82 | 342k | } |
83 | 342k | return (&(wipp->pub)); |
84 | 342k | } |
85 | | |
86 | | void * |
87 | | rtpp_wi_data_get_ptr(struct rtpp_wi *wi, size_t min_len, size_t max_len) |
88 | 364k | { |
89 | 364k | struct rtpp_wi_data *wipp; |
90 | | |
91 | 364k | RTPP_DBG_ASSERT(wi->wi_type == RTPP_WI_TYPE_DATA); |
92 | 364k | PUB2PVT(wi, wipp); |
93 | 364k | RTPP_DBG_ASSERT(wipp->data_len >= min_len); |
94 | 364k | RTPP_DBG_ASSERT(max_len == 0 || wipp->data_len <= max_len); |
95 | | |
96 | 364k | return(wipp->data); |
97 | 364k | } |