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