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
29#include "curl_memory.h"
30#include "memdebug.h"
31
32#define SIGNATURE 0x5c48e9b2 /* Random pattern. */
33
34/*
35 * Init a bufref struct.
36 */
37void Curl_bufref_init(struct bufref *br)
38{
39 DEBUGASSERT(br);
40 br->dtor = NULL;
41 br->ptr = NULL;
42 br->len = 0;
43
44#ifdef DEBUGBUILD
45 br->signature = SIGNATURE;
46#endif
47}
48
49/*
50 * Free the buffer and re-init the necessary fields. It doesn't touch the
51 * 'signature' field and thus this buffer reference can be reused.
52 */
53
54void Curl_bufref_free(struct bufref *br)
55{
56 DEBUGASSERT(br);
57 DEBUGASSERT(br->signature == SIGNATURE);
58 DEBUGASSERT(br->ptr || !br->len);
59
60 if(br->ptr && br->dtor)
61 br->dtor((void *) br->ptr);
62
63 br->dtor = NULL;
64 br->ptr = NULL;
65 br->len = 0;
66}
67
68/*
69 * Set the buffer reference to new values. The previously referenced buffer
70 * is released before assignment.
71 */
72void Curl_bufref_set(struct bufref *br, const void *ptr, size_t len,
73 void (*dtor)(void *))
74{
75 DEBUGASSERT(ptr || !len);
76 DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH);
77
78 Curl_bufref_free(br);
79 br->ptr = (const unsigned char *) ptr;
80 br->len = len;
81 br->dtor = dtor;
82}
83
84/*
85 * Get a pointer to the referenced buffer.
86 */
87const unsigned char *Curl_bufref_ptr(const struct bufref *br)
88{
89 DEBUGASSERT(br);
90 DEBUGASSERT(br->signature == SIGNATURE);
91 DEBUGASSERT(br->ptr || !br->len);
92
93 return br->ptr;
94}
95
96/*
97 * Get the length of the referenced buffer data.
98 */
99size_t Curl_bufref_len(const struct bufref *br)
100{
101 DEBUGASSERT(br);
102 DEBUGASSERT(br->signature == SIGNATURE);
103 DEBUGASSERT(br->ptr || !br->len);
104
105 return br->len;
106}
107
108CURLcode Curl_bufref_memdup(struct bufref *br, const void *ptr, size_t len)
109{
110 unsigned char *cpy = NULL;
111
112 DEBUGASSERT(br);
113 DEBUGASSERT(br->signature == SIGNATURE);
114 DEBUGASSERT(br->ptr || !br->len);
115 DEBUGASSERT(ptr || !len);
116 DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH);
117
118 if(ptr) {
119 cpy = malloc(len + 1);
120 if(!cpy)
121 return CURLE_OUT_OF_MEMORY;
122 if(len)
123 memcpy(dest: cpy, src: ptr, n: len);
124 cpy[len] = '\0';
125 }
126
127 Curl_bufref_set(br, ptr: cpy, len, dtor: curl_free);
128 return CURLE_OK;
129}
130