/***************************************************************************1* _ _ ____ _2* Project ___| | | | _ \| |3* / __| | | | |_) | |4* | (__| |_| | _ <| |___5* \___|\___/|_| \_\_____|6*7* Copyright (C) Daniel Stenberg, <[email protected]>, et al.8*9* This software is licensed as described in the file COPYING, which10* you should have received as part of this distribution. The terms11* are also available at https://curl.se/docs/copyright.html.12*13* You may opt to use, copy, modify, merge, publish, distribute and/or sell14* copies of the Software, and permit persons to whom the Software is15* furnished to do so, under the terms of the COPYING file.16*17* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY18* KIND, either express or implied.19*20* SPDX-License-Identifier: curl21*22***************************************************************************/2324#include "curl_setup.h"25#include "urldata.h"26#include "bufref.h"27#include "strdup.h"2829#include "curl_memory.h"30#include "memdebug.h"3132#ifdef DEBUGBUILD33#define SIGNATURE 0x5c48e9b2 /* Random pattern. */34#endif3536/*37* Init a bufref struct.38*/39void Curl_bufref_init(struct bufref *br)40{41DEBUGASSERT(br);42br->dtor = NULL;43br->ptr = NULL;44br->len = 0;4546#ifdef DEBUGBUILD47br->signature = SIGNATURE;48#endif49}5051/*52* Free the buffer and re-init the necessary fields. It does not touch the53* 'signature' field and thus this buffer reference can be reused.54*/5556void Curl_bufref_free(struct bufref *br)57{58DEBUGASSERT(br);59DEBUGASSERT(br->signature == SIGNATURE);60DEBUGASSERT(br->ptr || !br->len);6162if(br->ptr && br->dtor)63br->dtor(CURL_UNCONST(br->ptr));6465br->dtor = NULL;66br->ptr = NULL;67br->len = 0;68}6970/*71* Set the buffer reference to new values. The previously referenced buffer72* is released before assignment.73*/74void Curl_bufref_set(struct bufref *br, const void *ptr, size_t len,75void (*dtor)(void *))76{77DEBUGASSERT(ptr || !len);78DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH);7980Curl_bufref_free(br);81br->ptr = (const unsigned char *) ptr;82br->len = len;83br->dtor = dtor;84}8586/*87* Get a pointer to the referenced buffer.88*/89const unsigned char *Curl_bufref_ptr(const struct bufref *br)90{91DEBUGASSERT(br);92DEBUGASSERT(br->signature == SIGNATURE);93DEBUGASSERT(br->ptr || !br->len);9495return br->ptr;96}9798/*99* Get the length of the referenced buffer data.100*/101size_t Curl_bufref_len(const struct bufref *br)102{103DEBUGASSERT(br);104DEBUGASSERT(br->signature == SIGNATURE);105DEBUGASSERT(br->ptr || !br->len);106107return br->len;108}109110CURLcode Curl_bufref_memdup(struct bufref *br, const void *ptr, size_t len)111{112unsigned char *cpy = NULL;113114DEBUGASSERT(br);115DEBUGASSERT(br->signature == SIGNATURE);116DEBUGASSERT(br->ptr || !br->len);117DEBUGASSERT(ptr || !len);118DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH);119120if(ptr) {121cpy = Curl_memdup0(ptr, len);122if(!cpy)123return CURLE_OUT_OF_MEMORY;124}125126Curl_bufref_set(br, cpy, len, curl_free);127return CURLE_OK;128}129130131