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