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