/src/gdal/curl/lib/bufref.c
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 | | #define SIGNATURE 0x5c48e9b2 /* Random pattern. */ |
35 | | #endif |
36 | | |
37 | | /* |
38 | | * Init a bufref struct. |
39 | | */ |
40 | | void Curl_bufref_init(struct bufref *br) |
41 | 0 | { |
42 | 0 | DEBUGASSERT(br); |
43 | 0 | br->dtor = NULL; |
44 | 0 | br->ptr = NULL; |
45 | 0 | br->len = 0; |
46 | |
|
47 | | #ifdef DEBUGBUILD |
48 | | br->signature = SIGNATURE; |
49 | | #endif |
50 | 0 | } |
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 | 0 | { |
59 | 0 | DEBUGASSERT(br); |
60 | 0 | DEBUGASSERT(br->signature == SIGNATURE); |
61 | 0 | DEBUGASSERT(br->ptr || !br->len); |
62 | |
|
63 | 0 | if(br->ptr && br->dtor) |
64 | 0 | br->dtor(CURL_UNCONST(br->ptr)); |
65 | |
|
66 | 0 | br->dtor = NULL; |
67 | 0 | br->ptr = NULL; |
68 | 0 | br->len = 0; |
69 | 0 | } |
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 | 0 | { |
78 | 0 | DEBUGASSERT(ptr || !len); |
79 | 0 | DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH); |
80 | |
|
81 | 0 | Curl_bufref_free(br); |
82 | 0 | br->ptr = (const unsigned char *) ptr; |
83 | 0 | br->len = len; |
84 | 0 | br->dtor = dtor; |
85 | 0 | } |
86 | | |
87 | | /* |
88 | | * Get a pointer to the referenced buffer. |
89 | | */ |
90 | | const unsigned char *Curl_bufref_ptr(const struct bufref *br) |
91 | 0 | { |
92 | 0 | DEBUGASSERT(br); |
93 | 0 | DEBUGASSERT(br->signature == SIGNATURE); |
94 | 0 | DEBUGASSERT(br->ptr || !br->len); |
95 | |
|
96 | 0 | return br->ptr; |
97 | 0 | } |
98 | | |
99 | | /* |
100 | | * Get the length of the referenced buffer data. |
101 | | */ |
102 | | size_t Curl_bufref_len(const struct bufref *br) |
103 | 0 | { |
104 | 0 | DEBUGASSERT(br); |
105 | 0 | DEBUGASSERT(br->signature == SIGNATURE); |
106 | 0 | DEBUGASSERT(br->ptr || !br->len); |
107 | |
|
108 | 0 | return br->len; |
109 | 0 | } |
110 | | |
111 | | CURLcode Curl_bufref_memdup(struct bufref *br, const void *ptr, size_t len) |
112 | 0 | { |
113 | 0 | unsigned char *cpy = NULL; |
114 | |
|
115 | 0 | DEBUGASSERT(br); |
116 | 0 | DEBUGASSERT(br->signature == SIGNATURE); |
117 | 0 | DEBUGASSERT(br->ptr || !br->len); |
118 | 0 | DEBUGASSERT(ptr || !len); |
119 | 0 | DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH); |
120 | |
|
121 | 0 | if(ptr) { |
122 | 0 | cpy = Curl_memdup0(ptr, len); |
123 | 0 | if(!cpy) |
124 | 0 | return CURLE_OUT_OF_MEMORY; |
125 | 0 | } |
126 | | |
127 | 0 | Curl_bufref_set(br, cpy, len, curl_free); |
128 | 0 | return CURLE_OK; |
129 | 0 | } |