/***************************************************************************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***************************************************************************/23#include "curl_setup.h"2425#include "urldata.h"26#include "bufref.h"27#include "strdup.h"2829#ifdef DEBUGBUILD30#define SIGNATURE 0x5c48e9b2 /* Random pattern. */31#endif3233/*34* Init a bufref struct.35*/36void Curl_bufref_init(struct bufref *br)37{38DEBUGASSERT(br);39br->dtor = NULL;40br->ptr = NULL;41br->len = 0;4243#ifdef DEBUGBUILD44br->signature = SIGNATURE;45#endif46}4748/*49* Free the buffer and re-init the necessary fields. It does not touch the50* 'signature' field and thus this buffer reference can be reused.51*/5253void Curl_bufref_free(struct bufref *br)54{55DEBUGASSERT(br);56DEBUGASSERT(br->signature == SIGNATURE);57DEBUGASSERT(br->ptr || !br->len);5859if(br->ptr && br->dtor)60br->dtor(CURL_UNCONST(br->ptr));6162br->dtor = NULL;63br->ptr = NULL;64br->len = 0;65}6667/*68* Set the buffer reference to new values. The previously referenced buffer69* is released before assignment.70*/71void Curl_bufref_set(struct bufref *br, const void *ptr, size_t len,72void (*dtor)(void *))73{74DEBUGASSERT(ptr || !len);75DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH);7677Curl_bufref_free(br);78br->ptr = (const unsigned char *)ptr;79br->len = len;80br->dtor = dtor;81}8283/*84* Get a pointer to the referenced buffer.85*/86const unsigned char *Curl_bufref_uptr(const struct bufref *br)87{88DEBUGASSERT(br);89DEBUGASSERT(br->signature == SIGNATURE);90DEBUGASSERT(br->ptr || !br->len);9192return br->ptr;93}9495/*96* Get a pointer to the referenced string.97*/98const char *Curl_bufref_ptr(const struct bufref *br)99{100DEBUGASSERT(br);101DEBUGASSERT(br->signature == SIGNATURE);102DEBUGASSERT(br->ptr || !br->len);103104return (const char *)br->ptr;105}106107/*108* Get the length of the referenced buffer data.109*/110size_t Curl_bufref_len(const struct bufref *br)111{112DEBUGASSERT(br);113DEBUGASSERT(br->signature == SIGNATURE);114DEBUGASSERT(br->ptr || !br->len);115116return br->len;117}118119CURLcode Curl_bufref_memdup0(struct bufref *br, const void *ptr, size_t len)120{121unsigned char *cpy = NULL;122123DEBUGASSERT(br);124DEBUGASSERT(br->signature == SIGNATURE);125DEBUGASSERT(br->ptr || !br->len);126DEBUGASSERT(ptr || !len);127DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH);128129if(ptr) {130cpy = Curl_memdup0(ptr, len);131if(!cpy)132return CURLE_OUT_OF_MEMORY;133}134135Curl_bufref_set(br, cpy, len, curl_free);136return CURLE_OK;137}138139140