Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | #include "urldata.h" |
27 | | #include "bufref.h" |
28 | | #include "strdup.h" |
29 | | |
30 | | #include "curl_memory.h" |
31 | | #include "memdebug.h" |
32 | | |
33 | | #ifdef DEBUGBUILD |
34 | 7.74k | #define SIGNATURE 0x5c48e9b2 /* Random pattern. */ |
35 | | #endif |
36 | | |
37 | | /* |
38 | | * Init a bufref struct. |
39 | | */ |
40 | | void Curl_bufref_init(struct bufref *br) |
41 | 7.74k | { |
42 | 7.74k | DEBUGASSERT(br); |
43 | 7.74k | br->dtor = NULL; |
44 | 7.74k | br->ptr = NULL; |
45 | 7.74k | br->len = 0; |
46 | | |
47 | 7.74k | #ifdef DEBUGBUILD |
48 | 7.74k | br->signature = SIGNATURE; |
49 | 7.74k | #endif |
50 | 7.74k | } |
51 | | |
52 | | /* |
53 | | * Free the buffer and re-init the necessary fields. It does not touch the |
54 | | * 'signature' field and thus this buffer reference can be reused. |
55 | | */ |
56 | | |
57 | | void Curl_bufref_free(struct bufref *br) |
58 | 13.8k | { |
59 | 13.8k | DEBUGASSERT(br); |
60 | 13.8k | DEBUGASSERT(br->signature == SIGNATURE); |
61 | 13.8k | DEBUGASSERT(br->ptr || !br->len); |
62 | | |
63 | 13.8k | if(br->ptr && br->dtor) |
64 | 6.03k | br->dtor(CURL_UNCONST(br->ptr)); |
65 | | |
66 | 13.8k | br->dtor = NULL; |
67 | 13.8k | br->ptr = NULL; |
68 | 13.8k | br->len = 0; |
69 | 13.8k | } |
70 | | |
71 | | /* |
72 | | * Set the buffer reference to new values. The previously referenced buffer |
73 | | * is released before assignment. |
74 | | */ |
75 | | void Curl_bufref_set(struct bufref *br, const void *ptr, size_t len, |
76 | | void (*dtor)(void *)) |
77 | 6.63k | { |
78 | 6.63k | DEBUGASSERT(ptr || !len); |
79 | 6.63k | DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH); |
80 | | |
81 | 6.63k | Curl_bufref_free(br); |
82 | 6.63k | br->ptr = (const unsigned char *) ptr; |
83 | 6.63k | br->len = len; |
84 | 6.63k | br->dtor = dtor; |
85 | 6.63k | } |
86 | | |
87 | | /* |
88 | | * Get a pointer to the referenced buffer. |
89 | | */ |
90 | | const unsigned char *Curl_bufref_ptr(const struct bufref *br) |
91 | 8.59k | { |
92 | 8.59k | DEBUGASSERT(br); |
93 | 8.59k | DEBUGASSERT(br->signature == SIGNATURE); |
94 | 8.59k | DEBUGASSERT(br->ptr || !br->len); |
95 | | |
96 | 8.59k | return br->ptr; |
97 | 8.59k | } |
98 | | |
99 | | /* |
100 | | * Get the length of the referenced buffer data. |
101 | | */ |
102 | | size_t Curl_bufref_len(const struct bufref *br) |
103 | 11.5k | { |
104 | 11.5k | DEBUGASSERT(br); |
105 | 11.5k | DEBUGASSERT(br->signature == SIGNATURE); |
106 | 11.5k | DEBUGASSERT(br->ptr || !br->len); |
107 | | |
108 | 11.5k | return br->len; |
109 | 11.5k | } |
110 | | |
111 | | CURLcode Curl_bufref_memdup(struct bufref *br, const void *ptr, size_t len) |
112 | 49 | { |
113 | 49 | unsigned char *cpy = NULL; |
114 | | |
115 | 49 | DEBUGASSERT(br); |
116 | 49 | DEBUGASSERT(br->signature == SIGNATURE); |
117 | 49 | DEBUGASSERT(br->ptr || !br->len); |
118 | 49 | DEBUGASSERT(ptr || !len); |
119 | 49 | DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH); |
120 | | |
121 | 49 | if(ptr) { |
122 | 49 | cpy = Curl_memdup0(ptr, len); |
123 | 49 | if(!cpy) |
124 | 0 | return CURLE_OUT_OF_MEMORY; |
125 | 49 | } |
126 | | |
127 | 49 | Curl_bufref_set(br, cpy, len, curl_free); |
128 | 49 | return CURLE_OK; |
129 | 49 | } |